Skip to content

Commit 6a6935b

Browse files
committed
firebase-perf: Add custom method to set final static method for test
Robolectric's ReflectionHelpers removes supporting of setting fnial static method becasuse it not the reposibility of Robolectric, and it will not be allowed by the latest JDK. This CL just adds custom method to do the same things as a workaround. When the project updates the JDK someday, the maintainer needs to rewrite the test or provides some test helper method to avoid set final static method for test. Signed-off-by: utzcoz <[email protected]>
1 parent d2d4cdb commit 6a6935b

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@
2525
import static org.mockito.Mockito.verify;
2626
import static org.mockito.Mockito.when;
2727
import static org.mockito.MockitoAnnotations.initMocks;
28-
import static org.robolectric.util.ReflectionHelpers.setStaticField;
2928

3029
import android.os.Bundle;
3130
import androidx.test.core.app.ApplicationProvider;
3231
import com.google.firebase.perf.BuildConfig;
3332
import com.google.firebase.perf.FirebasePerformanceTestBase;
3433
import com.google.firebase.perf.util.ImmutableBundle;
3534
import com.google.firebase.perf.util.Optional;
35+
import java.lang.reflect.Field;
36+
import java.lang.reflect.Modifier;
3637
import org.junit.Before;
3738
import org.junit.Test;
3839
import org.junit.runner.RunWith;
3940
import org.mockito.Mock;
4041
import org.robolectric.RobolectricTestRunner;
41-
import org.robolectric.util.ReflectionHelpers;
4242

4343
/** Unit tests for {@link ConfigResolver}. */
4444
@RunWith(RobolectricTestRunner.class)
@@ -176,7 +176,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionIsRed_returnsFalse()
176176
.thenReturn(Optional.of(true));
177177

178178
// Disable SDK version.
179-
setStaticField(
179+
setStaticFinalField(
180180
BuildConfig.class,
181181
/* fieldName= */ "FIREPERF_VERSION_NAME",
182182
/* fieldNewValue= */ "1.0.0.111111111");
@@ -204,7 +204,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionAndSdkDisabled_retur
204204
.thenReturn(Optional.of(false));
205205

206206
// Disable SDK version.
207-
setStaticField(
207+
setStaticFinalField(
208208
BuildConfig.class,
209209
/* fieldName= */ "FIREPERF_VERSION_NAME",
210210
/* fieldNewValue= */ "1.0.0.111111111");
@@ -303,7 +303,7 @@ public void getIsServiceCollectionEnabled_sdkEnabledFlagNoFrc_returnDefaultValue
303303
.thenReturn(Optional.of(true));
304304

305305
// Mock that current Fireperf SDK version is disabled.
306-
setStaticField(
306+
setStaticFinalField(
307307
BuildConfig.class,
308308
/* fieldName= */ "FIREPERF_VERSION_NAME",
309309
/* fieldNewValue= */ "1.0.0.111111111");
@@ -328,7 +328,7 @@ public void getIsServiceCollectionEnabled_sdkEnabledFlagNoFrc_returnDefaultValue
328328

329329
// Mock that Fireperf SDK version is disabled by FRC, but fetch status is failure.
330330
when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(true);
331-
setStaticField(
331+
setStaticFinalField(
332332
BuildConfig.class,
333333
/* fieldName= */ "FIREPERF_VERSION_NAME",
334334
/* fieldNewValue= */ "1.0.0.111111111");
@@ -355,7 +355,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnCach
355355
.thenReturn(Optional.absent());
356356

357357
// Mock device caching value that includes current SDK version.
358-
setStaticField(
358+
setStaticFinalField(
359359
BuildConfig.class,
360360
/* fieldName= */ "FIREPERF_VERSION_NAME",
361361
/* fieldNewValue= */ "1.0.0.111111111");
@@ -400,7 +400,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa
400400
.thenReturn(Optional.of(true));
401401
when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(false);
402402

403-
setStaticField(
403+
setStaticFinalField(
404404
BuildConfig.class,
405405
/* fieldName= */ "FIREPERF_VERSION_NAME",
406406
/* fieldNewValue= */ "1.0.0.111111111");
@@ -455,7 +455,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa
455455

456456
when(mockRemoteConfigManager.getString(FIREBASE_PERFORMANCE_DISABLED_VERSIONS_FRC_KEY))
457457
.thenReturn(Optional.absent());
458-
setStaticField(
458+
setStaticFinalField(
459459
BuildConfig.class,
460460
/* fieldName= */ "FIREPERF_VERSION_NAME",
461461
/* fieldNewValue= */ "1.0.0.111111111");
@@ -2430,9 +2430,8 @@ public void getRateLimitSec_invalidCache_returnsDefault() {
24302430

24312431
@Test
24322432
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));
24362435

24372436
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
24382437
.thenReturn(-1L);
@@ -2447,9 +2446,8 @@ public void getAndCacheLogSourceName_noRemoteConfigOrCacheValue_returnsDefaultBu
24472446

24482447
@Test
24492448
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));
24532451

24542452
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
24552453
.thenReturn(-1L);
@@ -2463,9 +2461,8 @@ public void getAndCacheLogSourceName_notNullCacheValue_returnsCache() {
24632461

24642462
@Test
24652463
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));
24692466

24702467
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
24712468
.thenReturn(-1L);
@@ -2479,10 +2476,8 @@ public void getAndCacheLogSourceName_nullCacheValue_returnsDefault() {
24792476

24802477
@Test
24812478
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));
24862481

24872482
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
24882483
.thenReturn(-1L);
@@ -2516,9 +2511,8 @@ public void getAndCacheLogSourceName_defaultValueIsNotFireperf_returnsNewDefault
25162511

25172512
@Test
25182513
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));
25222516

25232517
// #1 call: valid remote config value is returned, therefore returns this value and store at
25242518
// cache.
@@ -2545,9 +2539,8 @@ public void getAndCacheLogSourceName_getFromRemoteConfig_returnsCacheAtNextTime(
25452539

25462540
@Test
25472541
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));
25512544

25522545
// #1 call: valid remote config value is returned, therefore returns this value and store at
25532546
// cache.
@@ -2574,9 +2567,8 @@ public void getAndCacheLogSourceName_cacheExistsAndGetNewFromRemoteConfig_cacheU
25742567

25752568
@Test
25762569
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));
25802572

25812573
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
25822574
.thenReturn(123L); // invalid log source.
@@ -2590,9 +2582,8 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsDefault() {
25902582

25912583
@Test
25922584
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));
25962587

25972588
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
25982589
.thenReturn(123L); // invalid log source.
@@ -2607,9 +2598,8 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsCache() {
26072598
@Test
26082599
public void
26092600
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));
26132603

26142604
when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L))
26152605
.thenReturn(675L); // FIREPERF_INTERNAL_LOW.
@@ -2856,4 +2846,21 @@ public void getFragmentSamplingRate_invalidCache_returnDefaultValue() {
28562846

28572847
assertThat(testConfigResolver.getFragmentSamplingRate()).isEqualTo(0.3);
28582848
}
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+
}
28592866
}

0 commit comments

Comments
 (0)