25
25
import static org .mockito .Mockito .verify ;
26
26
import static org .mockito .Mockito .when ;
27
27
import static org .mockito .MockitoAnnotations .initMocks ;
28
- import static org .robolectric .util .ReflectionHelpers .setStaticField ;
29
28
30
29
import android .os .Bundle ;
31
30
import androidx .test .core .app .ApplicationProvider ;
32
31
import com .google .firebase .perf .BuildConfig ;
33
32
import com .google .firebase .perf .FirebasePerformanceTestBase ;
34
33
import com .google .firebase .perf .util .ImmutableBundle ;
35
34
import com .google .firebase .perf .util .Optional ;
35
+ import java .lang .reflect .Field ;
36
+ import java .lang .reflect .Modifier ;
36
37
import org .junit .Before ;
37
38
import org .junit .Test ;
38
39
import org .junit .runner .RunWith ;
39
40
import org .mockito .Mock ;
40
41
import org .robolectric .RobolectricTestRunner ;
41
- import org .robolectric .util .ReflectionHelpers ;
42
42
43
43
/** Unit tests for {@link ConfigResolver}. */
44
44
@ RunWith (RobolectricTestRunner .class )
@@ -176,7 +176,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionIsRed_returnsFalse()
176
176
.thenReturn (Optional .of (true ));
177
177
178
178
// Disable SDK version.
179
- setStaticField (
179
+ setStaticFinalField (
180
180
BuildConfig .class ,
181
181
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
182
182
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -204,7 +204,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionAndSdkDisabled_retur
204
204
.thenReturn (Optional .of (false ));
205
205
206
206
// Disable SDK version.
207
- setStaticField (
207
+ setStaticFinalField (
208
208
BuildConfig .class ,
209
209
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
210
210
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -303,7 +303,7 @@ public void getIsServiceCollectionEnabled_sdkEnabledFlagNoFrc_returnDefaultValue
303
303
.thenReturn (Optional .of (true ));
304
304
305
305
// Mock that current Fireperf SDK version is disabled.
306
- setStaticField (
306
+ setStaticFinalField (
307
307
BuildConfig .class ,
308
308
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
309
309
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -328,7 +328,7 @@ public void getIsServiceCollectionEnabled_sdkEnabledFlagNoFrc_returnDefaultValue
328
328
329
329
// Mock that Fireperf SDK version is disabled by FRC, but fetch status is failure.
330
330
when (mockRemoteConfigManager .isLastFetchFailed ()).thenReturn (true );
331
- setStaticField (
331
+ setStaticFinalField (
332
332
BuildConfig .class ,
333
333
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
334
334
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -355,7 +355,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnCach
355
355
.thenReturn (Optional .absent ());
356
356
357
357
// Mock device caching value that includes current SDK version.
358
- setStaticField (
358
+ setStaticFinalField (
359
359
BuildConfig .class ,
360
360
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
361
361
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -400,7 +400,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa
400
400
.thenReturn (Optional .of (true ));
401
401
when (mockRemoteConfigManager .isLastFetchFailed ()).thenReturn (false );
402
402
403
- setStaticField (
403
+ setStaticFinalField (
404
404
BuildConfig .class ,
405
405
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
406
406
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -455,7 +455,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa
455
455
456
456
when (mockRemoteConfigManager .getString (FIREBASE_PERFORMANCE_DISABLED_VERSIONS_FRC_KEY ))
457
457
.thenReturn (Optional .absent ());
458
- setStaticField (
458
+ setStaticFinalField (
459
459
BuildConfig .class ,
460
460
/* fieldName= */ "FIREPERF_VERSION_NAME" ,
461
461
/* fieldNewValue= */ "1.0.0.111111111" );
@@ -2430,9 +2430,8 @@ public void getRateLimitSec_invalidCache_returnsDefault() {
2430
2430
2431
2431
@ Test
2432
2432
public void getAndCacheLogSourceName_noRemoteConfigOrCacheValue_returnsDefaultButNotSaveCache () {
2433
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2434
- ReflectionHelpers .setStaticField (
2435
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2433
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2434
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2436
2435
2437
2436
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2438
2437
.thenReturn (-1L );
@@ -2447,9 +2446,8 @@ public void getAndCacheLogSourceName_noRemoteConfigOrCacheValue_returnsDefaultBu
2447
2446
2448
2447
@ Test
2449
2448
public void getAndCacheLogSourceName_notNullCacheValue_returnsCache () {
2450
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2451
- ReflectionHelpers .setStaticField (
2452
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2449
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2450
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2453
2451
2454
2452
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2455
2453
.thenReturn (-1L );
@@ -2463,9 +2461,8 @@ public void getAndCacheLogSourceName_notNullCacheValue_returnsCache() {
2463
2461
2464
2462
@ Test
2465
2463
public void getAndCacheLogSourceName_nullCacheValue_returnsDefault () {
2466
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2467
- ReflectionHelpers .setStaticField (
2468
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2464
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2465
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2469
2466
2470
2467
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2471
2468
.thenReturn (-1L );
@@ -2479,10 +2476,8 @@ public void getAndCacheLogSourceName_nullCacheValue_returnsDefault() {
2479
2476
2480
2477
@ Test
2481
2478
public void getAndCacheLogSourceName_defaultValueIsNotFireperf_returnsNewDefault () {
2482
- ReflectionHelpers .setStaticField (
2483
- BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF_INTERNAL_HIGH" );
2484
- ReflectionHelpers .setStaticField (
2485
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2479
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF_INTERNAL_HIGH" );
2480
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2486
2481
2487
2482
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2488
2483
.thenReturn (-1L );
@@ -2516,9 +2511,8 @@ public void getAndCacheLogSourceName_defaultValueIsNotFireperf_returnsNewDefault
2516
2511
2517
2512
@ Test
2518
2513
public void getAndCacheLogSourceName_getFromRemoteConfig_returnsCacheAtNextTime () {
2519
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2520
- ReflectionHelpers .setStaticField (
2521
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2514
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2515
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2522
2516
2523
2517
// #1 call: valid remote config value is returned, therefore returns this value and store at
2524
2518
// cache.
@@ -2545,9 +2539,8 @@ public void getAndCacheLogSourceName_getFromRemoteConfig_returnsCacheAtNextTime(
2545
2539
2546
2540
@ Test
2547
2541
public void getAndCacheLogSourceName_cacheExistsAndGetNewFromRemoteConfig_cacheUpdated () {
2548
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2549
- ReflectionHelpers .setStaticField (
2550
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2542
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2543
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2551
2544
2552
2545
// #1 call: valid remote config value is returned, therefore returns this value and store at
2553
2546
// cache.
@@ -2574,9 +2567,8 @@ public void getAndCacheLogSourceName_cacheExistsAndGetNewFromRemoteConfig_cacheU
2574
2567
2575
2568
@ Test
2576
2569
public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsDefault () {
2577
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2578
- ReflectionHelpers .setStaticField (
2579
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2570
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2571
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2580
2572
2581
2573
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2582
2574
.thenReturn (123L ); // invalid log source.
@@ -2590,9 +2582,8 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsDefault() {
2590
2582
2591
2583
@ Test
2592
2584
public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsCache () {
2593
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2594
- ReflectionHelpers .setStaticField (
2595
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2585
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2586
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2596
2587
2597
2588
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2598
2589
.thenReturn (123L ); // invalid log source.
@@ -2607,9 +2598,8 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsCache() {
2607
2598
@ Test
2608
2599
public void
2609
2600
getAndCacheLogSourceName_bothRemoteConfigAndCacheExist_returnsAndCacheRemoteConfigData () {
2610
- ReflectionHelpers .setStaticField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2611
- ReflectionHelpers .setStaticField (
2612
- BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2601
+ setStaticFinalField (BuildConfig .class , "TRANSPORT_LOG_SRC" , "FIREPERF" );
2602
+ setStaticFinalField (BuildConfig .class , "ENFORCE_DEFAULT_LOG_SRC" , Boolean .valueOf (false ));
2613
2603
2614
2604
when (mockRemoteConfigManager .getRemoteConfigValueOrDefault ("fpr_log_source" , -1L ))
2615
2605
.thenReturn (675L ); // FIREPERF_INTERNAL_LOW.
@@ -2856,4 +2846,21 @@ public void getFragmentSamplingRate_invalidCache_returnDefaultValue() {
2856
2846
2857
2847
assertThat (testConfigResolver .getFragmentSamplingRate ()).isEqualTo (0.3 );
2858
2848
}
2849
+
2850
+ private static void setStaticFinalField (Class clazz , String fieldName , Object value ) {
2851
+ try {
2852
+ Field field = clazz .getDeclaredField (fieldName );
2853
+ if (field != null ) {
2854
+ field .setAccessible (true );
2855
+
2856
+ Field modifiersField = Field .class .getDeclaredField ("modifiers" );
2857
+ modifiersField .setAccessible (true );
2858
+ modifiersField .setInt (field , field .getModifiers () & ~Modifier .FINAL );
2859
+
2860
+ field .set (null , value );
2861
+ }
2862
+ } catch (Exception e ) {
2863
+ throw new RuntimeException (e );
2864
+ }
2865
+ }
2859
2866
}
0 commit comments