Skip to content

firebase-perf: Add custom method to set final static method for test #5312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
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;
import com.google.firebase.perf.BuildConfig;
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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
}