@@ -15,6 +15,7 @@ import type { AndroidAdbExecutor, AndroidAdbProvider } from '../adb-executor.ts'
1515import { mkdtempForTest } from './test-utils/tmp-dir.ts' ;
1616
1717const PACKAGE = 'com.callstack.agentdevice.imehelper' ;
18+ const LATIN_IME = 'com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME' ;
1819
1920const manifest = {
2021 name : 'android-ime-helper' as const ,
@@ -90,6 +91,9 @@ test('ensureAndroidImeHelper installs with semantic provider install options', a
9091 if ( args . includes ( '--show-versioncode' ) ) {
9192 return { exitCode : 1 , stdout : '' , stderr : 'not found' } ;
9293 }
94+ if ( isImeListCall ( args ) ) {
95+ return { exitCode : 0 , stdout : `${ manifest . serviceComponent } \n` , stderr : '' } ;
96+ }
9397 throw new Error ( `unexpected adb call: ${ args . join ( ' ' ) } ` ) ;
9498 } ;
9599 const adbProvider : AndroidAdbProvider = {
@@ -147,6 +151,118 @@ test('ensureAndroidImeHelper skips install when a newer version is already prese
147151 assert . equal ( result . reason , 'current' ) ;
148152} ) ;
149153
154+ test ( 'ensureAndroidImeHelper returns from an install once the IME is registered' , async ( ) => {
155+ const tmpDir = await mkdtempForTest ( 'ime-helper-registration-' ) ;
156+ const apkPath = path . join ( tmpDir , 'helper.apk' ) ;
157+ await fs . writeFile ( apkPath , 'helper-apk' ) ;
158+ let imeListCalls = 0 ;
159+ const adb : AndroidAdbExecutor = async ( args ) => {
160+ if ( args . includes ( '--show-versioncode' ) ) {
161+ return { exitCode : 0 , stdout : '' , stderr : '' } ;
162+ }
163+ if ( isImeListCall ( args ) ) {
164+ imeListCalls += 1 ;
165+ const helper = imeListCalls > 1 ? `${ manifest . serviceComponent } \n` : '' ;
166+ return { exitCode : 0 , stdout : `${ LATIN_IME } \n${ helper } ` , stderr : '' } ;
167+ }
168+ throw new Error ( `unexpected adb call: ${ args . join ( ' ' ) } ` ) ;
169+ } ;
170+ const adbProvider : AndroidAdbProvider = {
171+ exec : adb ,
172+ install : async ( ) => ( { exitCode : 0 , stdout : 'Success' , stderr : '' } ) ,
173+ } ;
174+
175+ const result = await ensureAndroidImeHelper ( {
176+ adb,
177+ adbProvider,
178+ artifact : { apkPath, manifest : { ...manifest , sha256 : sha256Text ( 'helper-apk' ) } } ,
179+ deviceKey : 'android:emulator-5554' ,
180+ } ) ;
181+
182+ assert . equal ( result . installed , true ) ;
183+ assert . equal ( imeListCalls , 2 ) ;
184+ } ) ;
185+
186+ test ( 'ensureAndroidImeHelper returns the install when the registration listing times out' , async ( ) => {
187+ const tmpDir = await mkdtempForTest ( 'ime-helper-registration-timeout-' ) ;
188+ const apkPath = path . join ( tmpDir , 'helper.apk' ) ;
189+ await fs . writeFile ( apkPath , 'helper-apk' ) ;
190+ const adb : AndroidAdbExecutor = async ( args ) => {
191+ if ( args . includes ( '--show-versioncode' ) ) {
192+ return { exitCode : 0 , stdout : '' , stderr : '' } ;
193+ }
194+ if ( isImeListCall ( args ) ) {
195+ throw new AppError ( 'COMMAND_FAILED' , 'adb timed out after 5000ms' , {
196+ stdout : '' ,
197+ stderr : '' ,
198+ exitCode : 1 ,
199+ timeoutMs : 5_000 ,
200+ } ) ;
201+ }
202+ throw new Error ( `unexpected adb call: ${ args . join ( ' ' ) } ` ) ;
203+ } ;
204+ const adbProvider : AndroidAdbProvider = {
205+ exec : adb ,
206+ install : async ( ) => ( { exitCode : 0 , stdout : 'Success' , stderr : '' } ) ,
207+ } ;
208+
209+ const result = await ensureAndroidImeHelper ( {
210+ adb,
211+ adbProvider,
212+ artifact : { apkPath, manifest : { ...manifest , sha256 : sha256Text ( 'helper-apk' ) } } ,
213+ deviceKey : 'android:emulator-5554' ,
214+ } ) ;
215+
216+ assert . equal ( result . installed , true ) ;
217+ } ) ;
218+
219+ test ( 'ensureAndroidImeHelper reinstalls a remembered helper the device no longer has' , async ( ) => {
220+ const tmpDir = await mkdtempForTest ( 'ime-helper-removed-' ) ;
221+ const apkPath = path . join ( tmpDir , 'helper.apk' ) ;
222+ await fs . writeFile ( apkPath , 'helper-apk' ) ;
223+ let present = false ;
224+ const adb : AndroidAdbExecutor = async ( args ) => {
225+ if ( args . includes ( '--show-versioncode' ) ) {
226+ return present
227+ ? {
228+ exitCode : 0 ,
229+ stdout : `package:${ manifest . packageName } versionCode:${ manifest . versionCode } ` ,
230+ stderr : '' ,
231+ }
232+ : { exitCode : 0 , stdout : '' , stderr : '' } ;
233+ }
234+ if ( isImeListCall ( args ) ) {
235+ return { exitCode : 0 , stdout : `${ manifest . serviceComponent } \n` , stderr : '' } ;
236+ }
237+ throw new Error ( `unexpected adb call: ${ args . join ( ' ' ) } ` ) ;
238+ } ;
239+ let installs = 0 ;
240+ const adbProvider : AndroidAdbProvider = {
241+ exec : adb ,
242+ install : async ( ) => {
243+ installs += 1 ;
244+ present = true ;
245+ return { exitCode : 0 , stdout : '' , stderr : '' } ;
246+ } ,
247+ } ;
248+ const ensure = ( ) =>
249+ ensureAndroidImeHelper ( {
250+ adb,
251+ adbProvider,
252+ artifact : { apkPath, manifest : { ...manifest , sha256 : sha256Text ( 'helper-apk' ) } } ,
253+ deviceKey : 'android:emulator-5554' ,
254+ } ) ;
255+
256+ assert . equal ( ( await ensure ( ) ) . installed , true ) ;
257+ assert . equal ( ( await ensure ( ) ) . installed , false ) ;
258+ present = false ;
259+ const result = await ensure ( ) ;
260+
261+ assert . equal ( result . installed , true ) ;
262+ assert . equal ( result . reason , 'missing' ) ;
263+ assert . equal ( installs , 2 ) ;
264+ } ) ;
265+
150266test ( 'ensureAndroidImeHelper install timeout points at the OEM install dialog' , async ( ) => {
151267 const tmpDir = await mkdtempForTest ( 'ime-helper-install-timeout-' ) ;
152268 const apkPath = path . join ( tmpDir , 'helper.apk' ) ;
@@ -189,6 +305,10 @@ test('ensureAndroidImeHelper install timeout points at the OEM install dialog',
189305 ) ;
190306} ) ;
191307
308+ function isImeListCall ( args : readonly string [ ] ) : boolean {
309+ return args . includes ( 'ime' ) && args . includes ( 'list' ) ;
310+ }
311+
192312function sha256Text ( text : string ) : string {
193313 return crypto . createHash ( 'sha256' ) . update ( text ) . digest ( 'hex' ) ;
194314}
0 commit comments