Skip to content

Commit 78727e3

Browse files
committed
fix(maestro): fail killApp loud when the target survives
am kill only reaps background processes, so a foreground killApp used to succeed without killing anything. Refuse a foreground target naming the background-first precondition, and verify the process is gone after the kill instead of reporting an unproven success.
1 parent 369a51b commit 78727e3

2 files changed

Lines changed: 117 additions & 4 deletions

File tree

‎packages/platform-android/src/__tests__/app-lifecycle-open.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,108 @@ test('killAndroidApp dispatches am kill rather than am force-stop', async () =>
196196
);
197197

198198
assert.deepEqual(calls, [
199+
['shell', 'dumpsys', 'window', 'windows'],
199200
['shell', 'am', 'kill', 'com.example.app'],
200201
['shell', 'dumpsys', 'window', 'windows'],
201202
['shell', 'pidof', 'com.example.app'],
202203
['shell', 'pidof', 'com.example.app'],
204+
['shell', 'pidof', 'com.example.app'],
203205
]);
204206
});
205207

208+
test('killAndroidApp refuses a foreground target before killing', async () => {
209+
const device: DeviceInfo = {
210+
platform: 'android',
211+
id: 'emulator-5554',
212+
name: 'Pixel',
213+
kind: 'emulator',
214+
booted: true,
215+
};
216+
const calls: (readonly string[])[] = [];
217+
218+
await withAndroidAdbProvider(
219+
{
220+
exec: async (args) => {
221+
calls.push(args);
222+
if (args.join(' ') === 'shell dumpsys window windows') {
223+
return {
224+
stdout: 'mCurrentFocus=Window{42 u0 com.example.app/.MainActivity}\n',
225+
stderr: '',
226+
exitCode: 0,
227+
};
228+
}
229+
return { stdout: '', stderr: '', exitCode: 0 };
230+
},
231+
reverse: {
232+
ensure: async () => {},
233+
remove: async () => {},
234+
removeAllOwned: async () => {},
235+
},
236+
},
237+
{ serial: 'emulator-5554' },
238+
async () => {
239+
await assertRejectsAppError(() => killAndroidApp(device, 'com.example.app'), {
240+
code: 'COMMAND_FAILED',
241+
hint: /Background the app before killApp/,
242+
});
243+
try {
244+
await killAndroidApp(device, 'com.example.app');
245+
assert.fail('expected killAndroidApp to reject for a foreground app');
246+
} catch (error) {
247+
assert.equal(
248+
(error as InstanceType<typeof AppError>).details?.reason,
249+
'android-kill-requires-background-app',
250+
);
251+
}
252+
},
253+
);
254+
255+
assert.deepEqual(calls, [
256+
['shell', 'dumpsys', 'window', 'windows'],
257+
['shell', 'dumpsys', 'window', 'windows'],
258+
]);
259+
});
260+
261+
test('killAndroidApp fails when the process survives the kill', async () => {
262+
const device: DeviceInfo = {
263+
platform: 'android',
264+
id: 'emulator-5554',
265+
name: 'Pixel',
266+
kind: 'emulator',
267+
booted: true,
268+
};
269+
270+
await withAndroidAdbProvider(
271+
{
272+
exec: async (args) => {
273+
if (args.join(' ') === 'shell dumpsys window windows') {
274+
return {
275+
stdout: 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n',
276+
stderr: '',
277+
exitCode: 0,
278+
};
279+
}
280+
if (args.join(' ') === 'shell pidof com.example.app') {
281+
return { stdout: '12345\n', stderr: '', exitCode: 0 };
282+
}
283+
return { stdout: '', stderr: '', exitCode: 0 };
284+
},
285+
reverse: {
286+
ensure: async () => {},
287+
remove: async () => {},
288+
removeAllOwned: async () => {},
289+
},
290+
},
291+
{ serial: 'emulator-5554' },
292+
async () => {
293+
await assertRejectsAppError(() => killAndroidApp(device, 'com.example.app'), {
294+
code: 'COMMAND_FAILED',
295+
hint: /Background the app before killApp/,
296+
});
297+
},
298+
);
299+
});
300+
206301
test('openAndroidApp ensures Android reverse before localhost deep link launch', async () => {
207302
const device: DeviceInfo = {
208303
platform: 'android',

‎packages/platform-android/src/app-lifecycle.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,20 +492,38 @@ export async function closeAndroidApp(device: DeviceInfo, app: string): Promise<
492492
* Maestro `killApp` on Android: system-initiated process death (`am kill`),
493493
* which only reaps a backgrounded/cached process — unlike `closeAndroidApp`'s
494494
* `am force-stop`. Callers background the app first (e.g. `pressKey: Home`).
495+
* A foreground target fails loud naming the precondition, and a process that
496+
* survives the kill fails rather than reading as success.
495497
*/
496498
export async function killAndroidApp(device: DeviceInfo, app: string): Promise<void> {
497499
const trimmed = app.trim();
498500
if (trimmed.toLowerCase() === 'settings') {
499-
await runAndroidShell(device, ['am', 'kill', 'com.android.settings']);
500-
await waitForAndroidPackageStopped(device, 'com.android.settings');
501+
await killAndroidPackage(device, 'com.android.settings');
501502
return;
502503
}
503504
const resolved = await resolveAndroidApp(device, app);
504505
if (resolved.type === 'intent') {
505506
throw new AppError('INVALID_ARGS', 'Kill requires a package name, not an intent');
506507
}
507-
await runAndroidShell(device, ['am', 'kill', resolved.value]);
508-
await waitForAndroidPackageStopped(device, resolved.value);
508+
await killAndroidPackage(device, resolved.value);
509+
}
510+
511+
async function killAndroidPackage(device: DeviceInfo, packageName: string): Promise<void> {
512+
const foreground = await readAndroidForegroundApp(device);
513+
if (foreground?.package === packageName) {
514+
throw new AppError('COMMAND_FAILED', `Cannot kill foreground app ${packageName}`, {
515+
reason: 'android-kill-requires-background-app',
516+
hint: 'Background the app before killApp (for example pressKey: Home): am kill only reaps background processes.',
517+
});
518+
}
519+
await runAndroidShell(device, ['am', 'kill', packageName]);
520+
await waitForAndroidPackageStopped(device, packageName);
521+
if (await isAndroidPackageProcessRunning(device, packageName)) {
522+
throw new AppError('COMMAND_FAILED', `am kill did not stop ${packageName}`, {
523+
reason: 'android-kill-requires-background-app',
524+
hint: 'Background the app before killApp (for example pressKey: Home): am kill only reaps background processes.',
525+
});
526+
}
509527
}
510528

511529
async function waitForAndroidPackageStopped(

0 commit comments

Comments
 (0)