From 29c1817725bd7f125b33c5f8ae3da888c516d7e8 Mon Sep 17 00:00:00 2001 From: utzcoz Date: Sat, 2 Sep 2023 09:32:43 +0800 Subject: [PATCH 1/3] Bump Robolectric to 4.10.3 Signed-off-by: utzcoz --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0c3531e7246..e2c7a96d8ef 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,7 +9,7 @@ grpc = "1.52.1" javalite = "3.21.11" kotlin = "1.7.10" protoc = "3.21.11" -robolectric = "4.9" +robolectric = "4.10.3" truth = "1.1.2" protobufjavautil = "3.21.11" kotest = "5.5.5" From d2d4cdba2f894b86a4685403d4bd985ec4a44926 Mon Sep 17 00:00:00 2001 From: utzcoz <43091780+utzcoz@users.noreply.github.com> Date: Thu, 7 Sep 2023 12:53:25 +0800 Subject: [PATCH 2/3] Upgrade compileSdk to 33 for firebase-perf Fix Class not found for Robolectric --- firebase-perf/firebase-perf.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-perf/firebase-perf.gradle b/firebase-perf/firebase-perf.gradle index 9ba3de5ac45..3a18c1dc095 100644 --- a/firebase-perf/firebase-perf.gradle +++ b/firebase-perf/firebase-perf.gradle @@ -43,7 +43,7 @@ protobuf { android { namespace "com.google.firebase.perf" - compileSdk 30 + compileSdk 33 lintOptions { abortOnError true From 6a6935b2f31bafb15be5d011ddf36a6727c5d354 Mon Sep 17 00:00:00 2001 From: utzcoz Date: Mon, 11 Sep 2023 14:29:49 +0800 Subject: [PATCH 3/3] 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 --- .../perf/config/ConfigResolverTest.java | 81 ++++++++++--------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java b/firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java index 65762f3a89c..9147caa8676 100644 --- a/firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java +++ b/firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java @@ -25,7 +25,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; -import static org.robolectric.util.ReflectionHelpers.setStaticField; import android.os.Bundle; import androidx.test.core.app.ApplicationProvider; @@ -33,12 +32,13 @@ import com.google.firebase.perf.FirebasePerformanceTestBase; import com.google.firebase.perf.util.ImmutableBundle; import com.google.firebase.perf.util.Optional; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.robolectric.RobolectricTestRunner; -import org.robolectric.util.ReflectionHelpers; /** Unit tests for {@link ConfigResolver}. */ @RunWith(RobolectricTestRunner.class) @@ -176,7 +176,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionIsRed_returnsFalse() .thenReturn(Optional.of(true)); // Disable SDK version. - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -204,7 +204,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionAndSdkDisabled_retur .thenReturn(Optional.of(false)); // Disable SDK version. - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -303,7 +303,7 @@ public void getIsServiceCollectionEnabled_sdkEnabledFlagNoFrc_returnDefaultValue .thenReturn(Optional.of(true)); // Mock that current Fireperf SDK version is disabled. - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -328,7 +328,7 @@ public void getIsServiceCollectionEnabled_sdkEnabledFlagNoFrc_returnDefaultValue // Mock that Fireperf SDK version is disabled by FRC, but fetch status is failure. when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(true); - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -355,7 +355,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnCach .thenReturn(Optional.absent()); // Mock device caching value that includes current SDK version. - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -400,7 +400,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa .thenReturn(Optional.of(true)); when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(false); - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -455,7 +455,7 @@ public void getIsServiceCollectionEnabled_sdkDisabledVersionFlagNoFrc_returnDefa when(mockRemoteConfigManager.getString(FIREBASE_PERFORMANCE_DISABLED_VERSIONS_FRC_KEY)) .thenReturn(Optional.absent()); - setStaticField( + setStaticFinalField( BuildConfig.class, /* fieldName= */ "FIREPERF_VERSION_NAME", /* fieldNewValue= */ "1.0.0.111111111"); @@ -2430,9 +2430,8 @@ public void getRateLimitSec_invalidCache_returnsDefault() { @Test public void getAndCacheLogSourceName_noRemoteConfigOrCacheValue_returnsDefaultButNotSaveCache() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(-1L); @@ -2447,9 +2446,8 @@ public void getAndCacheLogSourceName_noRemoteConfigOrCacheValue_returnsDefaultBu @Test public void getAndCacheLogSourceName_notNullCacheValue_returnsCache() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(-1L); @@ -2463,9 +2461,8 @@ public void getAndCacheLogSourceName_notNullCacheValue_returnsCache() { @Test public void getAndCacheLogSourceName_nullCacheValue_returnsDefault() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(-1L); @@ -2479,10 +2476,8 @@ public void getAndCacheLogSourceName_nullCacheValue_returnsDefault() { @Test public void getAndCacheLogSourceName_defaultValueIsNotFireperf_returnsNewDefault() { - ReflectionHelpers.setStaticField( - BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF_INTERNAL_HIGH"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF_INTERNAL_HIGH"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(-1L); @@ -2516,9 +2511,8 @@ public void getAndCacheLogSourceName_defaultValueIsNotFireperf_returnsNewDefault @Test public void getAndCacheLogSourceName_getFromRemoteConfig_returnsCacheAtNextTime() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); // #1 call: valid remote config value is returned, therefore returns this value and store at // cache. @@ -2545,9 +2539,8 @@ public void getAndCacheLogSourceName_getFromRemoteConfig_returnsCacheAtNextTime( @Test public void getAndCacheLogSourceName_cacheExistsAndGetNewFromRemoteConfig_cacheUpdated() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); // #1 call: valid remote config value is returned, therefore returns this value and store at // cache. @@ -2574,9 +2567,8 @@ public void getAndCacheLogSourceName_cacheExistsAndGetNewFromRemoteConfig_cacheU @Test public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsDefault() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(123L); // invalid log source. @@ -2590,9 +2582,8 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsDefault() { @Test public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsCache() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(123L); // invalid log source. @@ -2607,9 +2598,8 @@ public void getAndCacheLogSourceName_invalidRemoteConfigData_returnsCache() { @Test public void getAndCacheLogSourceName_bothRemoteConfigAndCacheExist_returnsAndCacheRemoteConfigData() { - ReflectionHelpers.setStaticField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); - ReflectionHelpers.setStaticField( - BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); + setStaticFinalField(BuildConfig.class, "TRANSPORT_LOG_SRC", "FIREPERF"); + setStaticFinalField(BuildConfig.class, "ENFORCE_DEFAULT_LOG_SRC", Boolean.valueOf(false)); when(mockRemoteConfigManager.getRemoteConfigValueOrDefault("fpr_log_source", -1L)) .thenReturn(675L); // FIREPERF_INTERNAL_LOW. @@ -2856,4 +2846,21 @@ public void getFragmentSamplingRate_invalidCache_returnDefaultValue() { assertThat(testConfigResolver.getFragmentSamplingRate()).isEqualTo(0.3); } + + private static void setStaticFinalField(Class clazz, String fieldName, Object value) { + try { + Field field = clazz.getDeclaredField(fieldName); + if (field != null) { + field.setAccessible(true); + + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + field.set(null, value); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } }