@@ -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 : / B a c k g r o u n d t h e a p p b e f o r e k i l l A p p / ,
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 : / B a c k g r o u n d t h e a p p b e f o r e k i l l A p p / ,
296+ } ) ;
297+ } ,
298+ ) ;
299+ } ) ;
300+
206301test ( 'openAndroidApp ensures Android reverse before localhost deep link launch' , async ( ) => {
207302 const device : DeviceInfo = {
208303 platform : 'android' ,
0 commit comments