|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { assertCommandCall } from './assertions.ts'; |
| 4 | +import { createAndroidSettingsWorld } from './android-world.ts'; |
| 5 | +import { |
| 6 | + androidAppOwnedSheetXml, |
| 7 | + androidButtonlessAlertXml, |
| 8 | + androidNativeAlertXml, |
| 9 | + androidRuntimePermissionXml, |
| 10 | + androidSystemDialogXml, |
| 11 | + dismissibleDialog, |
| 12 | +} from './android-dialog-fixtures.ts'; |
| 13 | +import { withProviderScenarioResource } from './harness.ts'; |
| 14 | + |
| 15 | +test('Provider-backed integration Android alert handles runtime permission dialog', async () => { |
| 16 | + const dialog = dismissibleDialog(androidRuntimePermissionXml); |
| 17 | + await withProviderScenarioResource( |
| 18 | + async () => await createAndroidSettingsWorld(dialog), |
| 19 | + async (world) => { |
| 20 | + const client = world.daemon.client(); |
| 21 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 22 | + |
| 23 | + const alertGet = await client.command.alert({ action: 'get', ...world.selection }); |
| 24 | + assert.equal(alertGet.kind, 'alertStatus'); |
| 25 | + assert.deepEqual(alertGet.alert, { |
| 26 | + title: 'Allow Demo to send you notifications?', |
| 27 | + buttons: ['Don’t allow', 'Allow'], |
| 28 | + platform: 'android', |
| 29 | + source: 'permission', |
| 30 | + packageName: 'com.google.android.permissioncontroller', |
| 31 | + }); |
| 32 | + |
| 33 | + const alertAccept = await client.command.alert({ action: 'accept', ...world.selection }); |
| 34 | + assert.equal(alertAccept.kind, 'alertHandled'); |
| 35 | + assert.equal(alertAccept.button, 'Allow'); |
| 36 | + assert.deepEqual( |
| 37 | + world.adbCalls.filter((call) => call.join(' ') === 'shell input tap 274 638'), |
| 38 | + [['shell', 'input', 'tap', '274', '638']], |
| 39 | + ); |
| 40 | + |
| 41 | + dialog.show(); |
| 42 | + const alertDismiss = await client.command.alert({ action: 'dismiss', ...world.selection }); |
| 43 | + assert.equal(alertDismiss.kind, 'alertHandled'); |
| 44 | + assert.equal(alertDismiss.button, 'Don’t allow'); |
| 45 | + assert.deepEqual( |
| 46 | + world.adbCalls.filter((call) => call.join(' ') === 'shell input tap 116 638'), |
| 47 | + [['shell', 'input', 'tap', '116', '638']], |
| 48 | + ); |
| 49 | + }, |
| 50 | + ); |
| 51 | +}); |
| 52 | + |
| 53 | +test('Provider-backed integration Android alert handles native AlertDialog actions', async () => { |
| 54 | + const dialog = dismissibleDialog(androidNativeAlertXml); |
| 55 | + await withProviderScenarioResource( |
| 56 | + async () => await createAndroidSettingsWorld(dialog), |
| 57 | + async (world) => { |
| 58 | + const client = world.daemon.client(); |
| 59 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 60 | + |
| 61 | + const alertGet = await client.command.alert({ action: 'get', ...world.selection }); |
| 62 | + assert.deepEqual(alertGet.alert, { |
| 63 | + title: 'Unsaved changes', |
| 64 | + message: 'Leave without saving?', |
| 65 | + buttons: ['Cancel', 'Discard'], |
| 66 | + platform: 'android', |
| 67 | + source: 'native-dialog', |
| 68 | + packageName: 'com.example.demo', |
| 69 | + }); |
| 70 | + |
| 71 | + const alertAccept = await client.command.alert({ action: 'accept', ...world.selection }); |
| 72 | + assert.equal(alertAccept.button, 'Discard'); |
| 73 | + dialog.show(); |
| 74 | + const alertDismiss = await client.command.alert({ action: 'dismiss', ...world.selection }); |
| 75 | + assert.equal(alertDismiss.button, 'Cancel'); |
| 76 | + assert.deepEqual( |
| 77 | + world.adbCalls.filter((call) => |
| 78 | + ['shell input tap 274 638', 'shell input tap 116 638'].includes(call.join(' ')), |
| 79 | + ), |
| 80 | + [ |
| 81 | + ['shell', 'input', 'tap', '274', '638'], |
| 82 | + ['shell', 'input', 'tap', '116', '638'], |
| 83 | + ], |
| 84 | + ); |
| 85 | + }, |
| 86 | + ); |
| 87 | +}); |
| 88 | + |
| 89 | +test('Provider-backed integration Android alert handles system dialogs', async () => { |
| 90 | + await withProviderScenarioResource( |
| 91 | + async () => await createAndroidSettingsWorld(dismissibleDialog(androidSystemDialogXml)), |
| 92 | + async (world) => { |
| 93 | + const client = world.daemon.client(); |
| 94 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 95 | + |
| 96 | + const alertGet = await client.command.alert({ action: 'get', ...world.selection }); |
| 97 | + assert.deepEqual(alertGet.alert, { |
| 98 | + title: "Demo isn't responding", |
| 99 | + message: 'Do you want to close it?', |
| 100 | + buttons: ['Close app', 'Wait'], |
| 101 | + platform: 'android', |
| 102 | + source: 'system-dialog', |
| 103 | + packageName: 'com.android.systemui', |
| 104 | + }); |
| 105 | + |
| 106 | + const alertDismiss = await client.command.alert({ action: 'dismiss', ...world.selection }); |
| 107 | + assert.equal(alertDismiss.button, 'Close app'); |
| 108 | + assertCommandCall(world.adbCalls, ['shell', 'input', 'tap', '116', '638']); |
| 109 | + }, |
| 110 | + ); |
| 111 | +}); |
| 112 | + |
| 113 | +test('Provider-backed integration Android alert dismiss falls back to Back without a dismiss button', async () => { |
| 114 | + await withProviderScenarioResource( |
| 115 | + async () => await createAndroidSettingsWorld(dismissibleDialog(androidButtonlessAlertXml)), |
| 116 | + async (world) => { |
| 117 | + const client = world.daemon.client(); |
| 118 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 119 | + |
| 120 | + const alertDismiss = await client.command.alert({ action: 'dismiss', ...world.selection }); |
| 121 | + assert.equal(alertDismiss.kind, 'alertHandled'); |
| 122 | + assert.equal(alertDismiss.button, 'Back'); |
| 123 | + assertCommandCall(world.adbCalls, ['shell', 'input', 'keyevent', '4']); |
| 124 | + }, |
| 125 | + ); |
| 126 | +}); |
| 127 | + |
| 128 | +test('Provider-backed integration Android alert accept fails when the dialog stays visible', async () => { |
| 129 | + await withProviderScenarioResource( |
| 130 | + async () => await createAndroidSettingsWorld({ snapshotXml: androidNativeAlertXml }), |
| 131 | + async (world) => { |
| 132 | + const client = world.daemon.client(); |
| 133 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 134 | + |
| 135 | + await assert.rejects( |
| 136 | + client.command.alert({ action: 'accept', ...world.selection }), |
| 137 | + (error: unknown) => |
| 138 | + error instanceof Error && |
| 139 | + error.message === 'alert accept did not dismiss the visible alert', |
| 140 | + ); |
| 141 | + assertCommandCall(world.adbCalls, ['shell', 'input', 'tap', '274', '638']); |
| 142 | + }, |
| 143 | + ); |
| 144 | +}); |
| 145 | + |
| 146 | +test('Provider-backed integration Android alert wait polls until a dialog appears', async () => { |
| 147 | + let snapshotCount = 0; |
| 148 | + await withProviderScenarioResource( |
| 149 | + async () => |
| 150 | + await createAndroidSettingsWorld({ |
| 151 | + snapshotXml: () => { |
| 152 | + snapshotCount += 1; |
| 153 | + return snapshotCount === 1 ? androidAppOwnedSheetXml() : androidRuntimePermissionXml(); |
| 154 | + }, |
| 155 | + }), |
| 156 | + async (world) => { |
| 157 | + const client = world.daemon.client(); |
| 158 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 159 | + |
| 160 | + const alertWait = await client.command.alert({ |
| 161 | + action: 'wait', |
| 162 | + timeoutMs: 1000, |
| 163 | + ...world.selection, |
| 164 | + }); |
| 165 | + assert.equal(alertWait.kind, 'alertWait'); |
| 166 | + // alert now returns the untyped CommandRequestResult bag (its iOS path is a |
| 167 | + // dynamic runner Record, so the public type is no longer a closed shape). |
| 168 | + const alertInfo = alertWait.alert as { source?: string } | null | undefined; |
| 169 | + assert.equal(alertInfo?.source, 'permission'); |
| 170 | + assert.ok(snapshotCount >= 2); |
| 171 | + }, |
| 172 | + ); |
| 173 | +}); |
| 174 | + |
| 175 | +test('Provider-backed integration Android alert ignores app-owned sheets', async () => { |
| 176 | + await withProviderScenarioResource( |
| 177 | + async () => await createAndroidSettingsWorld({ snapshotXml: androidAppOwnedSheetXml }), |
| 178 | + async (world) => { |
| 179 | + const client = world.daemon.client(); |
| 180 | + await client.apps.open({ app: 'com.example.demo', ...world.selection }); |
| 181 | + |
| 182 | + const alertGet = await client.command.alert({ action: 'get', ...world.selection }); |
| 183 | + assert.equal(alertGet.kind, 'alertStatus'); |
| 184 | + assert.equal(alertGet.alert, null); |
| 185 | + }, |
| 186 | + ); |
| 187 | +}); |
0 commit comments