14
14
15
15
package com .google .firebase .remoteconfig ;
16
16
17
+ import static androidx .test .ext .truth .os .BundleSubject .assertThat ;
17
18
import static com .google .common .truth .Truth .assertThat ;
18
19
import static com .google .common .truth .Truth .assertWithMessage ;
19
20
import static com .google .firebase .remoteconfig .AbtExperimentHelper .createAbtExperiment ;
23
24
import static com .google .firebase .remoteconfig .FirebaseRemoteConfig .DEFAULT_VALUE_FOR_STRING ;
24
25
import static com .google .firebase .remoteconfig .FirebaseRemoteConfig .LAST_FETCH_STATUS_THROTTLED ;
25
26
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 ;
26
30
import static org .mockito .ArgumentMatchers .eq ;
27
31
import static org .mockito .Matchers .any ;
28
32
import static org .mockito .Mockito .doAnswer ;
48
52
import com .google .firebase .abt .AbtException ;
49
53
import com .google .firebase .abt .FirebaseABTesting ;
50
54
import com .google .firebase .analytics .connector .AnalyticsConnector ;
55
+ import com .google .firebase .inject .Provider ;
51
56
import com .google .firebase .installations .FirebaseInstallationsApi ;
52
57
import com .google .firebase .installations .InstallationTokenResult ;
53
58
import com .google .firebase .remoteconfig .internal .ConfigCacheClient ;
@@ -94,6 +99,7 @@ public final class FirebaseRemoteConfigTest {
94
99
private static final String PROJECT_ID = "fake-frc-test-id" ;
95
100
96
101
private static final String FIREPERF_NAMESPACE = "fireperf" ;
102
+ private static final String PERSONALIZATION_NAMESPACE = "personalization" ;
97
103
98
104
private static final String STRING_KEY = "string_key" ;
99
105
private static final String BOOLEAN_KEY = "boolean_key" ;
@@ -136,6 +142,7 @@ public final class FirebaseRemoteConfigTest {
136
142
137
143
@ Mock private FirebaseABTesting mockFirebaseAbt ;
138
144
@ Mock private FirebaseInstallationsApi mockFirebaseInstallations ;
145
+ @ Mock private Provider <AnalyticsConnector > mockAnalyticsConnectorProvider ;
139
146
140
147
private FirebaseRemoteConfig frc ;
141
148
private FirebaseRemoteConfig fireperfFrc ;
@@ -163,7 +170,7 @@ public void setUp() throws Exception {
163
170
Context context = RuntimeEnvironment .application ;
164
171
FirebaseApp firebaseApp = initializeFirebaseApp (context );
165
172
166
- Personalization personalization = new Personalization (() -> mockAnalyticsConnector );
173
+ Personalization personalization = new Personalization (mockAnalyticsConnectorProvider );
167
174
ConfigGetParameterHandler parameterHandler =
168
175
new ConfigGetParameterHandler (directExecutor , mockActivatedCache , mockDefaultsCache );
169
176
parameterHandler .addListener (personalization ::logArmActive );
@@ -210,7 +217,7 @@ public void setUp() throws Exception {
210
217
.get (RemoteConfigComponent .class )
211
218
.get (
212
219
firebaseApp ,
213
- RemoteConfigComponent . DEFAULT_NAMESPACE ,
220
+ PERSONALIZATION_NAMESPACE ,
214
221
mockFirebaseInstallations ,
215
222
/*firebaseAbt=*/ null ,
216
223
directExecutor ,
@@ -219,7 +226,8 @@ public void setUp() throws Exception {
219
226
mockDefaultsCache ,
220
227
mockFetchHandler ,
221
228
parameterHandler ,
222
- metadataClient );
229
+ RemoteConfigComponent .getMetadataClient (
230
+ context , APP_ID , PERSONALIZATION_NAMESPACE ));
223
231
224
232
firstFetchedContainer =
225
233
ConfigContainer .newBuilder ()
@@ -954,6 +962,7 @@ public void setConfigSettingsAsync_updatesMetadata() {
954
962
@ Test
955
963
public void personalization_hasMetadata_successful () throws Exception {
956
964
List <Bundle > fakeLogs = new ArrayList <>();
965
+ when (mockAnalyticsConnectorProvider .get ()).thenReturn (mockAnalyticsConnector );
957
966
doAnswer (invocation -> fakeLogs .add (invocation .getArgument (2 )))
958
967
.when (mockAnalyticsConnector )
959
968
.logEvent (
@@ -967,7 +976,7 @@ public void personalization_hasMetadata_successful() throws Exception {
967
976
.withFetchTime (new Date (1 ))
968
977
.withPersonalizationMetadata (
969
978
new JSONObject (
970
- "{key1: {personalizationId: 'id1'}, key2: {personalizationId: 'id2'}}" ))
979
+ "{key1: {personalizationId: 'id1', choiceId: '1' }, key2: {personalizationId: 'id2', choiceId: '2 '}}" ))
971
980
.build ();
972
981
973
982
when (mockFetchHandler .fetch ())
@@ -990,15 +999,45 @@ public void personalization_hasMetadata_successful() throws Exception {
990
999
any (Bundle .class ));
991
1000
assertThat (fakeLogs ).hasSize (2 );
992
1001
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" );
997
1037
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 ();
1002
1041
});
1003
1042
}
1004
1043
0 commit comments