@@ -211,6 +211,70 @@ test.each(['foreground', 'process'] as const)(
211211 } ,
212212) ;
213213
214+ test . each ( [
215+ [ 'foreground' , 1 , 1_999 , true , 2 ] ,
216+ [ 'foreground' , 1 , 2_000 , false , 0 ] ,
217+ [ 'foreground' , 1 , 2_001 , false , 0 ] ,
218+ [ 'process' , 1 , 1_849 , true , 2 ] ,
219+ [ 'process' , 1 , 1_850 , false , 1 ] ,
220+ [ 'process' , 1 , 2_000 , false , 1 ] ,
221+ [ 'process' , 1 , 2_001 , false , 1 ] ,
222+ [ 'process' , 2 , 1_999 , true , 2 ] ,
223+ [ 'process' , 2 , 2_000 , false , 2 ] ,
224+ [ 'process' , 2 , 2_001 , false , 2 ] ,
225+ ] as const ) (
226+ 'closeAndroidApp admits %s probe %i completing at %ims only within its fence (success=%s)' ,
227+ async ( phase , probe , completionMs , succeeds , expectedProcessPolls ) => {
228+ vi . useFakeTimers ( ) ;
229+ vi . setSystemTime ( 0 ) ;
230+ let processPolls = 0 ;
231+ try {
232+ await withAndroidAdbProvider (
233+ {
234+ exec : async ( args ) => {
235+ if ( args [ 1 ] === 'pidof' ) processPolls += 1 ;
236+ if (
237+ ( phase === 'foreground' && args [ 1 ] === 'dumpsys' ) ||
238+ ( phase === 'process' && args [ 1 ] === 'pidof' && processPolls === probe )
239+ ) {
240+ await new Promise ( ( resolve ) => setTimeout ( resolve , completionMs - Date . now ( ) ) ) ;
241+ }
242+ return {
243+ stdout :
244+ args [ 1 ] === 'dumpsys'
245+ ? 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n'
246+ : '' ,
247+ stderr : '' ,
248+ exitCode : args [ 1 ] === 'pidof' ? 1 : 0 ,
249+ } ;
250+ } ,
251+ } ,
252+ { serial : ANDROID_EMULATOR . id } ,
253+ async ( ) => {
254+ const close = closeAndroidApp ( ANDROID_EMULATOR , 'com.example.app' ) ;
255+ const result = succeeds
256+ ? close
257+ : assert . rejects ( close , ( error : unknown ) => {
258+ assert . ok ( error instanceof AppError ) ;
259+ assert . equal ( error . code , 'COMMAND_FAILED' ) ;
260+ assert . deepEqual ( error . details , {
261+ reason : 'android-app-close-timeout' ,
262+ phase,
263+ packageName : 'com.example.app' ,
264+ timeoutMs : 2_000 ,
265+ } ) ;
266+ return true ;
267+ } ) ;
268+ await Promise . all ( [ result , vi . runAllTimersAsync ( ) ] ) ;
269+ } ,
270+ ) ;
271+ assert . equal ( processPolls , expectedProcessPolls ) ;
272+ } finally {
273+ vi . useRealTimers ( ) ;
274+ }
275+ } ,
276+ ) ;
277+
214278test ( 'openAndroidApp ensures Android reverse before localhost deep link launch' , async ( ) => {
215279 const device : DeviceInfo = {
216280 platform : 'android' ,
0 commit comments