Skip to content

Commit 98f7de8

Browse files
authored
Fix personalization test and add check for optionalProvider. (#2562)
1 parent 0a5b6ba commit 98f7de8

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

firebase-config/firebase-config.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ dependencies {
6464

6565
testImplementation 'org.mockito:mockito-core:2.25.0'
6666
testImplementation 'com.google.truth:truth:0.44'
67+
testImplementation 'androidx.test.ext:truth:1.0.0'
6768
testImplementation 'junit:junit:4.12'
6869
testImplementation "org.robolectric:robolectric:$robolectricVersion"
6970
testImplementation "org.skyscreamer:jsonassert:1.5.0"

firebase-config/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigTest.java

+51-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.remoteconfig;
1616

17+
import static androidx.test.ext.truth.os.BundleSubject.assertThat;
1718
import static com.google.common.truth.Truth.assertThat;
1819
import static com.google.common.truth.Truth.assertWithMessage;
1920
import static com.google.firebase.remoteconfig.AbtExperimentHelper.createAbtExperiment;
@@ -23,6 +24,9 @@
2324
import static com.google.firebase.remoteconfig.FirebaseRemoteConfig.DEFAULT_VALUE_FOR_STRING;
2425
import static com.google.firebase.remoteconfig.FirebaseRemoteConfig.LAST_FETCH_STATUS_THROTTLED;
2526
import static com.google.firebase.remoteconfig.FirebaseRemoteConfig.toExperimentInfoMaps;
27+
import static com.google.firebase.remoteconfig.internal.Personalization.EXTERNAL_ARM_VALUE_PARAM;
28+
import static com.google.firebase.remoteconfig.internal.Personalization.EXTERNAL_PERSONALIZATION_ID_PARAM;
29+
import static org.mockito.ArgumentMatchers.anyString;
2630
import static org.mockito.ArgumentMatchers.eq;
2731
import static org.mockito.Matchers.any;
2832
import static org.mockito.Mockito.doAnswer;
@@ -48,6 +52,7 @@
4852
import com.google.firebase.abt.AbtException;
4953
import com.google.firebase.abt.FirebaseABTesting;
5054
import com.google.firebase.analytics.connector.AnalyticsConnector;
55+
import com.google.firebase.inject.Provider;
5156
import com.google.firebase.installations.FirebaseInstallationsApi;
5257
import com.google.firebase.installations.InstallationTokenResult;
5358
import com.google.firebase.remoteconfig.internal.ConfigCacheClient;
@@ -94,6 +99,7 @@ public final class FirebaseRemoteConfigTest {
9499
private static final String PROJECT_ID = "fake-frc-test-id";
95100

96101
private static final String FIREPERF_NAMESPACE = "fireperf";
102+
private static final String PERSONALIZATION_NAMESPACE = "personalization";
97103

98104
private static final String STRING_KEY = "string_key";
99105
private static final String BOOLEAN_KEY = "boolean_key";
@@ -136,6 +142,7 @@ public final class FirebaseRemoteConfigTest {
136142

137143
@Mock private FirebaseABTesting mockFirebaseAbt;
138144
@Mock private FirebaseInstallationsApi mockFirebaseInstallations;
145+
@Mock private Provider<AnalyticsConnector> mockAnalyticsConnectorProvider;
139146

140147
private FirebaseRemoteConfig frc;
141148
private FirebaseRemoteConfig fireperfFrc;
@@ -163,7 +170,7 @@ public void setUp() throws Exception {
163170
Context context = RuntimeEnvironment.application;
164171
FirebaseApp firebaseApp = initializeFirebaseApp(context);
165172

166-
Personalization personalization = new Personalization(() -> mockAnalyticsConnector);
173+
Personalization personalization = new Personalization(mockAnalyticsConnectorProvider);
167174
ConfigGetParameterHandler parameterHandler =
168175
new ConfigGetParameterHandler(directExecutor, mockActivatedCache, mockDefaultsCache);
169176
parameterHandler.addListener(personalization::logArmActive);
@@ -210,7 +217,7 @@ public void setUp() throws Exception {
210217
.get(RemoteConfigComponent.class)
211218
.get(
212219
firebaseApp,
213-
RemoteConfigComponent.DEFAULT_NAMESPACE,
220+
PERSONALIZATION_NAMESPACE,
214221
mockFirebaseInstallations,
215222
/*firebaseAbt=*/ null,
216223
directExecutor,
@@ -219,7 +226,8 @@ public void setUp() throws Exception {
219226
mockDefaultsCache,
220227
mockFetchHandler,
221228
parameterHandler,
222-
metadataClient);
229+
RemoteConfigComponent.getMetadataClient(
230+
context, APP_ID, PERSONALIZATION_NAMESPACE));
223231

224232
firstFetchedContainer =
225233
ConfigContainer.newBuilder()
@@ -954,6 +962,7 @@ public void setConfigSettingsAsync_updatesMetadata() {
954962
@Test
955963
public void personalization_hasMetadata_successful() throws Exception {
956964
List<Bundle> fakeLogs = new ArrayList<>();
965+
when(mockAnalyticsConnectorProvider.get()).thenReturn(mockAnalyticsConnector);
957966
doAnswer(invocation -> fakeLogs.add(invocation.getArgument(2)))
958967
.when(mockAnalyticsConnector)
959968
.logEvent(
@@ -967,7 +976,7 @@ public void personalization_hasMetadata_successful() throws Exception {
967976
.withFetchTime(new Date(1))
968977
.withPersonalizationMetadata(
969978
new JSONObject(
970-
"{key1: {personalizationId: 'id1'}, key2: {personalizationId: 'id2'}}"))
979+
"{key1: {personalizationId: 'id1', choiceId: '1'}, key2: {personalizationId: 'id2', choiceId: '2'}}"))
971980
.build();
972981

973982
when(mockFetchHandler.fetch())
@@ -990,15 +999,45 @@ public void personalization_hasMetadata_successful() throws Exception {
990999
any(Bundle.class));
9911000
assertThat(fakeLogs).hasSize(2);
9921001

993-
Bundle params1 = new Bundle();
994-
params1.putString(Personalization.EXTERNAL_RC_PARAMETER_PARAM, "id1");
995-
params1.putString(Personalization.EXTERNAL_ARM_VALUE_PARAM, "value1");
996-
assertThat(fakeLogs.get(0).toString()).isEqualTo(params1.toString());
1002+
assertThat(fakeLogs.get(0))
1003+
.string(EXTERNAL_PERSONALIZATION_ID_PARAM)
1004+
.isEqualTo("id1");
1005+
assertThat(fakeLogs.get(0)).string(EXTERNAL_ARM_VALUE_PARAM).isEqualTo("value1");
1006+
assertThat(fakeLogs.get(1))
1007+
.string(EXTERNAL_PERSONALIZATION_ID_PARAM)
1008+
.isEqualTo("id2");
1009+
assertThat(fakeLogs.get(1)).string(EXTERNAL_ARM_VALUE_PARAM).isEqualTo("value2");
1010+
});
1011+
}
1012+
1013+
@Test
1014+
public void personalization_hasMetadata_successful_without_analytics() throws Exception {
1015+
List<Bundle> fakeLogs = new ArrayList<>();
1016+
when(mockAnalyticsConnectorProvider.get()).thenReturn(null);
1017+
1018+
ConfigContainer configContainer =
1019+
ConfigContainer.newBuilder()
1020+
.replaceConfigsWith(new JSONObject("{key1: 'value1', key2: 'value2'}"))
1021+
.withFetchTime(new Date(1))
1022+
.withPersonalizationMetadata(
1023+
new JSONObject("{key1: {personalizationId: 'id1', choiceId: '1'}}"))
1024+
.build();
1025+
1026+
when(mockFetchHandler.fetch())
1027+
.thenReturn(
1028+
Tasks.forResult(FetchResponse.forBackendUpdatesFetched(configContainer, "Etag")));
1029+
1030+
when(mockActivatedCache.getBlocking()).thenReturn(configContainer);
1031+
1032+
personalizationFrc
1033+
.fetchAndActivate()
1034+
.addOnCompleteListener(
1035+
success -> {
1036+
personalizationFrc.getString("key1");
9971037

998-
Bundle params2 = new Bundle();
999-
params2.putString(Personalization.EXTERNAL_RC_PARAMETER_PARAM, "id2");
1000-
params2.putString(Personalization.EXTERNAL_ARM_VALUE_PARAM, "value2");
1001-
assertThat(fakeLogs.get(1).toString()).isEqualTo(params2.toString());
1038+
verify(mockAnalyticsConnector, never())
1039+
.logEvent(anyString(), anyString(), any(Bundle.class));
1040+
assertThat(fakeLogs).isEmpty();
10021041
});
10031042
}
10041043

0 commit comments

Comments
 (0)