Skip to content

Commit 39269a5

Browse files
committed
unit test for RCc cache saving
1 parent 2c36e1f commit 39269a5

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import androidx.annotation.NonNull;
2626
import com.google.android.gms.tasks.Task;
2727
import com.google.android.gms.tasks.TaskCompletionSource;
28+
import com.google.common.util.concurrent.MoreExecutors;
2829
import com.google.firebase.inject.Provider;
2930
import com.google.firebase.perf.FirebasePerformanceTestBase;
3031
import com.google.firebase.perf.provider.FirebasePerfProvider;
@@ -36,6 +37,7 @@
3637
import com.google.testing.timing.FakeScheduledExecutorService;
3738
import java.util.HashMap;
3839
import java.util.Map;
40+
import java.util.concurrent.ExecutorService;
3941
import java.util.concurrent.TimeUnit;
4042
import org.junit.Before;
4143
import org.junit.Test;
@@ -48,19 +50,26 @@
4850
public final class RemoteConfigManagerTest extends FirebasePerformanceTestBase {
4951

5052
private static final String FIREPERF_FRC_NAMESPACE_NAME = "fireperf";
53+
private static final FirebaseRemoteConfigValue TRUE_VALUE =
54+
new RemoteConfigValueImplForTest("true");
55+
private static final FirebaseRemoteConfigValue FALSE_VALUE =
56+
new RemoteConfigValueImplForTest("false");
5157

5258
@Mock private FirebaseRemoteConfig mockFirebaseRemoteConfig;
5359
@Mock private RemoteConfigComponent mockFirebaseRemoteConfigComponent;
5460
@Mock private Provider<RemoteConfigComponent> mockFirebaseRemoteConfigProvider;
55-
@Mock private DeviceCacheManager mockCacheManager;
5661

62+
private DeviceCacheManager cacheManager;
63+
private ExecutorService directExecutor;
5764
private FakeScheduledExecutorService fakeExecutor;
5865

5966
@Before
6067
public void setUp() {
6168
initMocks(this);
6269

6370
fakeExecutor = new FakeScheduledExecutorService();
71+
directExecutor = MoreExecutors.newDirectExecutorService();
72+
cacheManager = new DeviceCacheManager(directExecutor);
6473

6574
when(mockFirebaseRemoteConfigProvider.get()).thenReturn(mockFirebaseRemoteConfigComponent);
6675
when(mockFirebaseRemoteConfigComponent.get(FIREPERF_FRC_NAMESPACE_NAME))
@@ -231,6 +240,29 @@ public void getString_validFrcValue_updatesWithNewValue() {
231240
assertThat(testRemoteConfigManager.getString("some_key3").isAvailable()).isFalse();
232241
}
233242

243+
@Test
244+
public void syncConfigValues_savesNewlyFetchedValueToDeviceCache() {
245+
Map<String, FirebaseRemoteConfigValue> configs = new HashMap<>();
246+
ConfigurationConstants.ExperimentTTID flag =
247+
ConfigurationConstants.ExperimentTTID.getInstance();
248+
configs.put(flag.getRemoteConfigFlag(), TRUE_VALUE);
249+
RemoteConfigManager testRemoteConfigManager = setupTestRemoteConfigManager(configs);
250+
251+
assertThat(cacheManager.getBoolean(flag.getDeviceCacheFlag()).isAvailable()).isFalse();
252+
253+
configs.put(flag.getRemoteConfigFlag(), FALSE_VALUE);
254+
testRemoteConfigManager.syncConfigValues(configs);
255+
256+
assertThat(cacheManager.getBoolean(flag.getDeviceCacheFlag()).isAvailable()).isTrue();
257+
assertThat(cacheManager.getBoolean(flag.getDeviceCacheFlag()).get()).isFalse();
258+
259+
configs.put(flag.getRemoteConfigFlag(), TRUE_VALUE);
260+
testRemoteConfigManager.syncConfigValues(configs);
261+
262+
assertThat(cacheManager.getBoolean(flag.getDeviceCacheFlag()).isAvailable()).isTrue();
263+
assertThat(cacheManager.getBoolean(flag.getDeviceCacheFlag()).get()).isTrue();
264+
}
265+
234266
@Test
235267
public void getRemoteConfigValueOrDefaultLong_invalidFrcValue_returnsDefaultValue() {
236268
Map<String, FirebaseRemoteConfigValue> configs = createDefaultRcConfigMap();
@@ -890,10 +922,10 @@ private RemoteConfigManager setupTestRemoteConfigManager(
890922
when(mockFirebaseRemoteConfig.getAll()).thenReturn(configs);
891923
if (initializeFrc) {
892924
return new RemoteConfigManager(
893-
mockCacheManager, fakeExecutor, mockFirebaseRemoteConfig, appStartConfigFetchDelayInMs);
925+
cacheManager, fakeExecutor, mockFirebaseRemoteConfig, appStartConfigFetchDelayInMs);
894926
} else {
895927
return new RemoteConfigManager(
896-
mockCacheManager,
928+
cacheManager,
897929
fakeExecutor,
898930
/* firebaseRemoteConfig= */ null,
899931
appStartConfigFetchDelayInMs);

0 commit comments

Comments
 (0)