Skip to content

Fix personalization test and add check for optionalProvider. #2562

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
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions firebase-config/firebase-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {

testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation 'com.google.truth:truth:0.44'
testImplementation 'androidx.test.ext:truth:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "org.skyscreamer:jsonassert:1.5.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.remoteconfig;

import static androidx.test.ext.truth.os.BundleSubject.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.firebase.remoteconfig.AbtExperimentHelper.createAbtExperiment;
Expand All @@ -23,6 +24,9 @@
import static com.google.firebase.remoteconfig.FirebaseRemoteConfig.DEFAULT_VALUE_FOR_STRING;
import static com.google.firebase.remoteconfig.FirebaseRemoteConfig.LAST_FETCH_STATUS_THROTTLED;
import static com.google.firebase.remoteconfig.FirebaseRemoteConfig.toExperimentInfoMaps;
import static com.google.firebase.remoteconfig.internal.Personalization.EXTERNAL_ARM_VALUE_PARAM;
import static com.google.firebase.remoteconfig.internal.Personalization.EXTERNAL_PERSONALIZATION_ID_PARAM;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
Expand All @@ -48,6 +52,7 @@
import com.google.firebase.abt.AbtException;
import com.google.firebase.abt.FirebaseABTesting;
import com.google.firebase.analytics.connector.AnalyticsConnector;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.installations.InstallationTokenResult;
import com.google.firebase.remoteconfig.internal.ConfigCacheClient;
Expand Down Expand Up @@ -94,6 +99,7 @@ public final class FirebaseRemoteConfigTest {
private static final String PROJECT_ID = "fake-frc-test-id";

private static final String FIREPERF_NAMESPACE = "fireperf";
private static final String PERSONALIZATION_NAMESPACE = "personalization";

private static final String STRING_KEY = "string_key";
private static final String BOOLEAN_KEY = "boolean_key";
Expand Down Expand Up @@ -136,6 +142,7 @@ public final class FirebaseRemoteConfigTest {

@Mock private FirebaseABTesting mockFirebaseAbt;
@Mock private FirebaseInstallationsApi mockFirebaseInstallations;
@Mock private Provider<AnalyticsConnector> mockAnalyticsConnectorProvider;

private FirebaseRemoteConfig frc;
private FirebaseRemoteConfig fireperfFrc;
Expand Down Expand Up @@ -163,7 +170,7 @@ public void setUp() throws Exception {
Context context = RuntimeEnvironment.application;
FirebaseApp firebaseApp = initializeFirebaseApp(context);

Personalization personalization = new Personalization(() -> mockAnalyticsConnector);
Personalization personalization = new Personalization(mockAnalyticsConnectorProvider);
ConfigGetParameterHandler parameterHandler =
new ConfigGetParameterHandler(directExecutor, mockActivatedCache, mockDefaultsCache);
parameterHandler.addListener(personalization::logArmActive);
Expand Down Expand Up @@ -210,7 +217,7 @@ public void setUp() throws Exception {
.get(RemoteConfigComponent.class)
.get(
firebaseApp,
RemoteConfigComponent.DEFAULT_NAMESPACE,
PERSONALIZATION_NAMESPACE,
mockFirebaseInstallations,
/*firebaseAbt=*/ null,
directExecutor,
Expand All @@ -219,7 +226,8 @@ public void setUp() throws Exception {
mockDefaultsCache,
mockFetchHandler,
parameterHandler,
metadataClient);
RemoteConfigComponent.getMetadataClient(
context, APP_ID, PERSONALIZATION_NAMESPACE));

firstFetchedContainer =
ConfigContainer.newBuilder()
Expand Down Expand Up @@ -954,6 +962,7 @@ public void setConfigSettingsAsync_updatesMetadata() {
@Test
public void personalization_hasMetadata_successful() throws Exception {
List<Bundle> fakeLogs = new ArrayList<>();
when(mockAnalyticsConnectorProvider.get()).thenReturn(mockAnalyticsConnector);
doAnswer(invocation -> fakeLogs.add(invocation.getArgument(2)))
.when(mockAnalyticsConnector)
.logEvent(
Expand All @@ -967,7 +976,7 @@ public void personalization_hasMetadata_successful() throws Exception {
.withFetchTime(new Date(1))
.withPersonalizationMetadata(
new JSONObject(
"{key1: {personalizationId: 'id1'}, key2: {personalizationId: 'id2'}}"))
"{key1: {personalizationId: 'id1', choiceId: '1'}, key2: {personalizationId: 'id2', choiceId: '2'}}"))
.build();

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

Bundle params1 = new Bundle();
params1.putString(Personalization.EXTERNAL_RC_PARAMETER_PARAM, "id1");
params1.putString(Personalization.EXTERNAL_ARM_VALUE_PARAM, "value1");
assertThat(fakeLogs.get(0).toString()).isEqualTo(params1.toString());
assertThat(fakeLogs.get(0))
.string(EXTERNAL_PERSONALIZATION_ID_PARAM)
.isEqualTo("id1");
assertThat(fakeLogs.get(0)).string(EXTERNAL_ARM_VALUE_PARAM).isEqualTo("value1");
assertThat(fakeLogs.get(1))
.string(EXTERNAL_PERSONALIZATION_ID_PARAM)
.isEqualTo("id2");
assertThat(fakeLogs.get(1)).string(EXTERNAL_ARM_VALUE_PARAM).isEqualTo("value2");
});
}

@Test
public void personalization_hasMetadata_successful_without_analytics() throws Exception {
List<Bundle> fakeLogs = new ArrayList<>();
when(mockAnalyticsConnectorProvider.get()).thenReturn(null);

ConfigContainer configContainer =
ConfigContainer.newBuilder()
.replaceConfigsWith(new JSONObject("{key1: 'value1', key2: 'value2'}"))
.withFetchTime(new Date(1))
.withPersonalizationMetadata(
new JSONObject("{key1: {personalizationId: 'id1', choiceId: '1'}}"))
.build();

when(mockFetchHandler.fetch())
.thenReturn(
Tasks.forResult(FetchResponse.forBackendUpdatesFetched(configContainer, "Etag")));

when(mockActivatedCache.getBlocking()).thenReturn(configContainer);

personalizationFrc
.fetchAndActivate()
.addOnCompleteListener(
success -> {
personalizationFrc.getString("key1");

Bundle params2 = new Bundle();
params2.putString(Personalization.EXTERNAL_RC_PARAMETER_PARAM, "id2");
params2.putString(Personalization.EXTERNAL_ARM_VALUE_PARAM, "value2");
assertThat(fakeLogs.get(1).toString()).isEqualTo(params2.toString());
verify(mockAnalyticsConnector, never())
.logEvent(anyString(), anyString(), any(Bundle.class));
assertThat(fakeLogs).isEmpty();
});
}

Expand Down