27
27
import android .util .Log ;
28
28
import com .google .firebase .crashlytics .internal .CrashlyticsTestCase ;
29
29
import com .google .firebase .crashlytics .internal .Logger ;
30
+ import java .util .ArrayList ;
31
+ import java .util .Arrays ;
30
32
import java .util .HashMap ;
33
+ import java .util .List ;
31
34
import java .util .Map ;
32
35
33
36
public class CommonUtilsTest extends CrashlyticsTestCase {
@@ -278,6 +281,82 @@ public void testResolveBuildId_bothIds() throws Exception {
278
281
});
279
282
}
280
283
284
+ public void testResolveNativeBuildIds_missing_returnsEmptyList () {
285
+ assertNativeBuildIds (new ArrayList <BuildIdInfo >(), new HashMap <String , List <String >>());
286
+ }
287
+
288
+ public void testResolveNativeBuildIds_missingResourceArray_returnsEmptyList () {
289
+ assertNativeBuildIds (
290
+ new ArrayList <BuildIdInfo >(),
291
+ new HashMap <String , List <String >>() {
292
+ {
293
+ put (
294
+ CommonUtils .BUILD_IDS_LIB_NAMES_RESOURCE_NAME ,
295
+ new ArrayList <String >(Arrays .asList ("lib.so" )));
296
+ put (
297
+ CommonUtils .BUILD_IDS_BUILD_ID_RESOURCE_NAME ,
298
+ new ArrayList <String >(Arrays .asList ("aabb" )));
299
+ }
300
+ });
301
+ }
302
+
303
+ public void testResolveNativeBuildIds_returnsSingleLibrary () {
304
+ assertNativeBuildIds (
305
+ new ArrayList <BuildIdInfo >(Arrays .asList (new BuildIdInfo ("lib.so" , "x86" , "aabb" ))),
306
+ new HashMap <String , List <String >>() {
307
+ {
308
+ put (
309
+ CommonUtils .BUILD_IDS_LIB_NAMES_RESOURCE_NAME ,
310
+ new ArrayList <String >(Arrays .asList ("lib.so" )));
311
+ put (
312
+ CommonUtils .BUILD_IDS_ARCH_RESOURCE_NAME ,
313
+ new ArrayList <String >(Arrays .asList ("x86" )));
314
+ put (
315
+ CommonUtils .BUILD_IDS_BUILD_ID_RESOURCE_NAME ,
316
+ new ArrayList <String >(Arrays .asList ("aabb" )));
317
+ }
318
+ });
319
+ }
320
+
321
+ public void testResolveNativeBuildIds_returnsMultipleLibrary () {
322
+ assertNativeBuildIds (
323
+ new ArrayList <BuildIdInfo >(
324
+ Arrays .asList (
325
+ new BuildIdInfo ("lib.so" , "x86" , "aabb" ),
326
+ new BuildIdInfo ("lib.so" , "x86_64" , "bbaa" ))),
327
+ new HashMap <String , List <String >>() {
328
+ {
329
+ put (
330
+ CommonUtils .BUILD_IDS_LIB_NAMES_RESOURCE_NAME ,
331
+ new ArrayList <String >(Arrays .asList ("lib.so" , "lib.so" )));
332
+ put (
333
+ CommonUtils .BUILD_IDS_ARCH_RESOURCE_NAME ,
334
+ new ArrayList <String >(Arrays .asList ("x86" , "x86_64" )));
335
+ put (
336
+ CommonUtils .BUILD_IDS_BUILD_ID_RESOURCE_NAME ,
337
+ new ArrayList <String >(Arrays .asList ("aabb" , "bbaa" )));
338
+ }
339
+ });
340
+ }
341
+
342
+ public void testResolveNativeBuildIds_mismatchedArray_returnsEmptyList () {
343
+ assertNativeBuildIds (
344
+ new ArrayList <BuildIdInfo >(),
345
+ new HashMap <String , List <String >>() {
346
+ {
347
+ put (
348
+ CommonUtils .BUILD_IDS_LIB_NAMES_RESOURCE_NAME ,
349
+ new ArrayList <String >(Arrays .asList ("lib.so" , "lib.so" )));
350
+ put (
351
+ CommonUtils .BUILD_IDS_ARCH_RESOURCE_NAME ,
352
+ new ArrayList <String >(Arrays .asList ("x86" )));
353
+ put (
354
+ CommonUtils .BUILD_IDS_BUILD_ID_RESOURCE_NAME ,
355
+ new ArrayList <String >(Arrays .asList ("aabb" , "baab" )));
356
+ }
357
+ });
358
+ }
359
+
281
360
private void assertBuildId (String expectedValue , Map <String , String > buildIds ) {
282
361
final Context mockContext = mock (Context .class );
283
362
final Context mockAppContext = mock (Context .class );
@@ -303,6 +382,40 @@ private void assertBuildId(String expectedValue, Map<String, String> buildIds) {
303
382
assertEquals (expectedValue , CommonUtils .getMappingFileId (mockContext ));
304
383
}
305
384
385
+ private void assertNativeBuildIds (
386
+ ArrayList <BuildIdInfo > expected , Map <String , List <String >> nativeBuildIds ) {
387
+ final Context mockContext = mock (Context .class );
388
+ final Context mockAppContext = mock (Context .class );
389
+ final Resources mockResources = mock (Resources .class );
390
+
391
+ final String packageName = "package.name" ;
392
+ final ApplicationInfo info = new ApplicationInfo ();
393
+ info .icon = 0 ;
394
+
395
+ when (mockContext .getResources ()).thenReturn (mockResources );
396
+ when (mockContext .getApplicationContext ()).thenReturn (mockAppContext );
397
+
398
+ when (mockAppContext .getApplicationInfo ()).thenReturn (info );
399
+ when (mockContext .getPackageName ()).thenReturn (packageName );
400
+
401
+ int id = -1 ;
402
+ when (mockResources .getIdentifier (anyString (), anyString (), anyString ())).thenReturn (++id );
403
+ for (String buildIdKey : nativeBuildIds .keySet ()) {
404
+ when (mockResources .getIdentifier (buildIdKey , "array" , packageName )).thenReturn (++id );
405
+ when (mockResources .getStringArray (eq (id )))
406
+ .thenReturn (nativeBuildIds .get (buildIdKey ).toArray (new String [] {}));
407
+ }
408
+ List <BuildIdInfo > output = CommonUtils .getBuildIdInfo (mockContext );
409
+ assertEquals (output .size (), expected .size ());
410
+ for (int i = 0 ; i < expected .size (); i ++) {
411
+ BuildIdInfo expectedBuildIdInfo = expected .get (i );
412
+ BuildIdInfo outputBuildIdInfo = output .get (i );
413
+ assertEquals (expectedBuildIdInfo .getLibraryName (), outputBuildIdInfo .getLibraryName ());
414
+ assertEquals (expectedBuildIdInfo .getArch (), outputBuildIdInfo .getArch ());
415
+ assertEquals (expectedBuildIdInfo .getBuildId (), outputBuildIdInfo .getBuildId ());
416
+ }
417
+ }
418
+
306
419
private Context mockPermissionContext (boolean isGranted ) {
307
420
Context mockContext = mock (Context .class );
308
421
when (mockContext .checkCallingOrSelfPermission (anyString ()))
0 commit comments