|
| 1 | +import { test, vi } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { closeAndroidApp, openAndroidApp } from '../app-lifecycle.ts'; |
| 4 | +import { withAndroidAdbProvider } from '../adb-executor.ts'; |
| 5 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 6 | +import { AppError } from '@agent-device/kernel/errors'; |
| 7 | +import { ANDROID_EMULATOR } from './test-utils/device-fixtures.ts'; |
| 8 | +import './test-utils/android-host-test-setup.ts'; |
| 9 | + |
| 10 | +test('closeAndroidApp waits until package is no longer foreground', async () => { |
| 11 | + const device: DeviceInfo = { |
| 12 | + platform: 'android', |
| 13 | + id: 'emulator-5554', |
| 14 | + name: 'Pixel', |
| 15 | + kind: 'emulator', |
| 16 | + booted: true, |
| 17 | + }; |
| 18 | + const calls: (readonly string[])[] = []; |
| 19 | + let focusPolls = 0; |
| 20 | + |
| 21 | + await withAndroidAdbProvider( |
| 22 | + { |
| 23 | + exec: async (args) => { |
| 24 | + calls.push(args); |
| 25 | + if (args.join(' ') === 'shell dumpsys window windows') { |
| 26 | + focusPolls += 1; |
| 27 | + return { |
| 28 | + stdout: |
| 29 | + focusPolls === 1 |
| 30 | + ? 'mCurrentFocus=Window{42 u0 com.example.app/.MainActivity}\n' |
| 31 | + : 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n', |
| 32 | + stderr: '', |
| 33 | + exitCode: 0, |
| 34 | + }; |
| 35 | + } |
| 36 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 37 | + }, |
| 38 | + reverse: { |
| 39 | + ensure: async () => {}, |
| 40 | + remove: async () => {}, |
| 41 | + removeAllOwned: async () => {}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { serial: 'emulator-5554' }, |
| 45 | + async () => await closeAndroidApp(device, 'com.example.app'), |
| 46 | + ); |
| 47 | + |
| 48 | + assert.deepEqual(calls, [ |
| 49 | + ['shell', 'am', 'force-stop', 'com.example.app'], |
| 50 | + ['shell', 'dumpsys', 'window', 'windows'], |
| 51 | + ['shell', 'dumpsys', 'window', 'windows'], |
| 52 | + ['shell', 'pidof', 'com.example.app'], |
| 53 | + ['shell', 'pidof', 'com.example.app'], |
| 54 | + ]); |
| 55 | +}); |
| 56 | + |
| 57 | +test('closeAndroidApp returns after force-stop when package is already not foreground', async () => { |
| 58 | + const device: DeviceInfo = { |
| 59 | + platform: 'android', |
| 60 | + id: 'emulator-5554', |
| 61 | + name: 'Pixel', |
| 62 | + kind: 'emulator', |
| 63 | + booted: true, |
| 64 | + }; |
| 65 | + const calls: (readonly string[])[] = []; |
| 66 | + |
| 67 | + await withAndroidAdbProvider( |
| 68 | + { |
| 69 | + exec: async (args) => { |
| 70 | + calls.push(args); |
| 71 | + if (args.join(' ') === 'shell dumpsys window windows') { |
| 72 | + return { |
| 73 | + stdout: 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n', |
| 74 | + stderr: '', |
| 75 | + exitCode: 0, |
| 76 | + }; |
| 77 | + } |
| 78 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 79 | + }, |
| 80 | + reverse: { |
| 81 | + ensure: async () => {}, |
| 82 | + remove: async () => {}, |
| 83 | + removeAllOwned: async () => {}, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { serial: 'emulator-5554' }, |
| 87 | + async () => await closeAndroidApp(device, 'com.example.app'), |
| 88 | + ); |
| 89 | + |
| 90 | + assert.deepEqual(calls, [ |
| 91 | + ['shell', 'am', 'force-stop', 'com.example.app'], |
| 92 | + ['shell', 'dumpsys', 'window', 'windows'], |
| 93 | + ['shell', 'pidof', 'com.example.app'], |
| 94 | + ['shell', 'pidof', 'com.example.app'], |
| 95 | + ]); |
| 96 | +}); |
| 97 | + |
| 98 | +test('closeAndroidApp waits until package process exits after force-stop', async () => { |
| 99 | + const device: DeviceInfo = { |
| 100 | + platform: 'android', |
| 101 | + id: 'emulator-5554', |
| 102 | + name: 'Pixel', |
| 103 | + kind: 'emulator', |
| 104 | + booted: true, |
| 105 | + }; |
| 106 | + const calls: (readonly string[])[] = []; |
| 107 | + let processPolls = 0; |
| 108 | + |
| 109 | + await withAndroidAdbProvider( |
| 110 | + { |
| 111 | + exec: async (args) => { |
| 112 | + calls.push(args); |
| 113 | + if (args.join(' ') === 'shell dumpsys window windows') { |
| 114 | + return { |
| 115 | + stdout: 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n', |
| 116 | + stderr: '', |
| 117 | + exitCode: 0, |
| 118 | + }; |
| 119 | + } |
| 120 | + if (args.join(' ') === 'shell pidof com.example.app') { |
| 121 | + processPolls += 1; |
| 122 | + return { |
| 123 | + stdout: processPolls === 1 ? '12345\n' : '', |
| 124 | + stderr: '', |
| 125 | + exitCode: processPolls === 1 ? 0 : 1, |
| 126 | + }; |
| 127 | + } |
| 128 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 129 | + }, |
| 130 | + reverse: { |
| 131 | + ensure: async () => {}, |
| 132 | + remove: async () => {}, |
| 133 | + removeAllOwned: async () => {}, |
| 134 | + }, |
| 135 | + }, |
| 136 | + { serial: 'emulator-5554' }, |
| 137 | + async () => await closeAndroidApp(device, 'com.example.app'), |
| 138 | + ); |
| 139 | + |
| 140 | + assert.deepEqual(calls, [ |
| 141 | + ['shell', 'am', 'force-stop', 'com.example.app'], |
| 142 | + ['shell', 'dumpsys', 'window', 'windows'], |
| 143 | + ['shell', 'pidof', 'com.example.app'], |
| 144 | + ['shell', 'pidof', 'com.example.app'], |
| 145 | + ['shell', 'pidof', 'com.example.app'], |
| 146 | + ]); |
| 147 | +}); |
| 148 | + |
| 149 | +test.each(['foreground', 'process'] as const)( |
| 150 | + 'closeAndroidApp refuses an exhausted %s fence', |
| 151 | + async (phase) => { |
| 152 | + vi.useFakeTimers(); |
| 153 | + const calls: (readonly string[])[] = []; |
| 154 | + try { |
| 155 | + await withAndroidAdbProvider( |
| 156 | + { |
| 157 | + exec: async (args) => { |
| 158 | + calls.push(args); |
| 159 | + const stdout = |
| 160 | + args[1] === 'pidof' |
| 161 | + ? '12345\n' |
| 162 | + : args[1] === 'dumpsys' |
| 163 | + ? `mCurrentFocus=Window{42 u0 ${phase === 'foreground' ? 'com.example.app/.MainActivity' : 'com.android.launcher/.Launcher'}}\n` |
| 164 | + : ''; |
| 165 | + return { stdout, stderr: '', exitCode: 0 }; |
| 166 | + }, |
| 167 | + }, |
| 168 | + { serial: ANDROID_EMULATOR.id }, |
| 169 | + async () => { |
| 170 | + const rejection = assert.rejects( |
| 171 | + closeAndroidApp(ANDROID_EMULATOR, 'com.example.app'), |
| 172 | + (error: unknown) => { |
| 173 | + assert.ok(error instanceof AppError); |
| 174 | + assert.equal(error.code, 'COMMAND_FAILED'); |
| 175 | + assert.deepEqual(error.details, { |
| 176 | + reason: 'android-app-close-timeout', |
| 177 | + phase, |
| 178 | + packageName: 'com.example.app', |
| 179 | + timeoutMs: 2_000, |
| 180 | + }); |
| 181 | + return true; |
| 182 | + }, |
| 183 | + ); |
| 184 | + await Promise.all([rejection, vi.runAllTimersAsync()]); |
| 185 | + }, |
| 186 | + ); |
| 187 | + assert.deepEqual(calls[0], ['shell', 'am', 'force-stop', 'com.example.app']); |
| 188 | + assert.equal( |
| 189 | + calls.filter((args) => args[1] === 'pidof').length, |
| 190 | + phase === 'process' ? 40 : 0, |
| 191 | + ); |
| 192 | + } finally { |
| 193 | + vi.useRealTimers(); |
| 194 | + } |
| 195 | + }, |
| 196 | +); |
| 197 | + |
| 198 | +test.each([ |
| 199 | + ['foreground', 1, 1_999, true, 2], |
| 200 | + ['foreground', 1, 2_000, false, 0], |
| 201 | + ['foreground', 1, 2_001, false, 0], |
| 202 | + ['process', 1, 1_849, true, 2], |
| 203 | + ['process', 1, 1_850, false, 1], |
| 204 | + ['process', 1, 2_000, false, 1], |
| 205 | + ['process', 1, 2_001, false, 1], |
| 206 | + ['process', 2, 1_999, true, 2], |
| 207 | + ['process', 2, 2_000, false, 2], |
| 208 | + ['process', 2, 2_001, false, 2], |
| 209 | +] as const)( |
| 210 | + 'closeAndroidApp admits %s probe %i completing at %ims only within its fence (success=%s)', |
| 211 | + async (phase, probe, completionMs, succeeds, expectedProcessPolls) => { |
| 212 | + vi.useFakeTimers(); |
| 213 | + vi.setSystemTime(0); |
| 214 | + let processPolls = 0; |
| 215 | + try { |
| 216 | + await withAndroidAdbProvider( |
| 217 | + { |
| 218 | + exec: async (args) => { |
| 219 | + if (args[1] === 'pidof') processPolls += 1; |
| 220 | + if ( |
| 221 | + (phase === 'foreground' && args[1] === 'dumpsys') || |
| 222 | + (phase === 'process' && args[1] === 'pidof' && processPolls === probe) |
| 223 | + ) { |
| 224 | + await new Promise((resolve) => setTimeout(resolve, completionMs - Date.now())); |
| 225 | + } |
| 226 | + return { |
| 227 | + stdout: |
| 228 | + args[1] === 'dumpsys' |
| 229 | + ? 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n' |
| 230 | + : '', |
| 231 | + stderr: '', |
| 232 | + exitCode: args[1] === 'pidof' ? 1 : 0, |
| 233 | + }; |
| 234 | + }, |
| 235 | + }, |
| 236 | + { serial: ANDROID_EMULATOR.id }, |
| 237 | + async () => { |
| 238 | + const close = closeAndroidApp(ANDROID_EMULATOR, 'com.example.app'); |
| 239 | + const result = succeeds |
| 240 | + ? close |
| 241 | + : assert.rejects(close, (error: unknown) => { |
| 242 | + assert.ok(error instanceof AppError); |
| 243 | + assert.equal(error.code, 'COMMAND_FAILED'); |
| 244 | + assert.deepEqual(error.details, { |
| 245 | + reason: 'android-app-close-timeout', |
| 246 | + phase, |
| 247 | + packageName: 'com.example.app', |
| 248 | + timeoutMs: 2_000, |
| 249 | + }); |
| 250 | + return true; |
| 251 | + }); |
| 252 | + await Promise.all([result, vi.runAllTimersAsync()]); |
| 253 | + }, |
| 254 | + ); |
| 255 | + assert.equal(processPolls, expectedProcessPolls); |
| 256 | + } finally { |
| 257 | + vi.useRealTimers(); |
| 258 | + } |
| 259 | + }, |
| 260 | +); |
| 261 | + |
| 262 | +test('openAndroidApp refuses to relaunch a package whose stop fence expires', async () => { |
| 263 | + vi.useFakeTimers(); |
| 264 | + const calls: (readonly string[])[] = []; |
| 265 | + try { |
| 266 | + await withAndroidAdbProvider( |
| 267 | + { |
| 268 | + exec: async (args) => { |
| 269 | + calls.push(args); |
| 270 | + const stdout = |
| 271 | + args[1] === 'dumpsys' |
| 272 | + ? 'mCurrentFocus=Window{42 u0 com.example.app/.MainActivity}\n' |
| 273 | + : ''; |
| 274 | + return { stdout, stderr: '', exitCode: 0 }; |
| 275 | + }, |
| 276 | + }, |
| 277 | + { serial: ANDROID_EMULATOR.id }, |
| 278 | + async () => { |
| 279 | + const rejection = assert.rejects( |
| 280 | + openAndroidApp(ANDROID_EMULATOR, 'com.example.app', { terminateRunningApp: true }), |
| 281 | + (error: unknown) => |
| 282 | + error instanceof AppError && |
| 283 | + error.details?.reason === 'android-app-close-timeout' && |
| 284 | + error.details?.phase === 'foreground', |
| 285 | + ); |
| 286 | + await Promise.all([rejection, vi.runAllTimersAsync()]); |
| 287 | + }, |
| 288 | + ); |
| 289 | + assert.deepEqual(calls[0], ['shell', 'am', 'force-stop', 'com.example.app']); |
| 290 | + assert.equal(calls.filter((args) => args[2] === 'start').length, 0, 'no launch after refusal'); |
| 291 | + } finally { |
| 292 | + vi.useRealTimers(); |
| 293 | + } |
| 294 | +}); |
0 commit comments