26
26
import static com .google .firebase .remoteconfig .RemoteConfigConstants .ResponseFieldKey .STATE ;
27
27
import static com .google .firebase .remoteconfig .internal .ConfigFetchHandler .BACKOFF_TIME_DURATIONS_IN_MINUTES ;
28
28
import static com .google .firebase .remoteconfig .internal .ConfigFetchHandler .DEFAULT_MINIMUM_FETCH_INTERVAL_IN_SECONDS ;
29
+ import static com .google .firebase .remoteconfig .internal .ConfigFetchHandler .FIRST_OPEN_TIME_KEY ;
29
30
import static com .google .firebase .remoteconfig .internal .ConfigFetchHandler .HTTP_TOO_MANY_REQUESTS ;
30
31
import static com .google .firebase .remoteconfig .internal .ConfigMetadataClient .LAST_FETCH_TIME_NO_FETCH_YET ;
31
32
import static com .google .firebase .remoteconfig .internal .ConfigMetadataClient .NO_BACKOFF_TIME ;
42
43
import static java .util .concurrent .TimeUnit .MINUTES ;
43
44
import static java .util .concurrent .TimeUnit .SECONDS ;
44
45
import static org .mockito .ArgumentMatchers .any ;
46
+ import static org .mockito .ArgumentMatchers .anyBoolean ;
45
47
import static org .mockito .ArgumentMatchers .eq ;
46
48
import static org .mockito .Mockito .doReturn ;
47
49
import static org .mockito .Mockito .doThrow ;
@@ -195,6 +197,7 @@ public void fetch_firstFetch_includesInstallationAuthToken() throws Exception {
195
197
/* analyticsUserProperties= */ any (),
196
198
/* lastFetchETag= */ any (),
197
199
/* customHeaders= */ any (),
200
+ /* firstOpenTime= */ any (),
198
201
/* currentTime= */ any ());
199
202
}
200
203
@@ -394,7 +397,7 @@ public void fetch_gettingFetchCacheFails_doesNotThrowException() throws Exceptio
394
397
395
398
@ Test
396
399
public void fetch_fetchBackendCallFails_taskThrowsException () throws Exception {
397
- when (mockBackendFetchApiClient .fetch (any (), any (), any (), any (), any (), any (), any ()))
400
+ when (mockBackendFetchApiClient .fetch (any (), any (), any (), any (), any (), any (), any (), any () ))
398
401
.thenThrow (
399
402
new FirebaseRemoteConfigClientException ("Fetch failed due to an unexpected error." ));
400
403
@@ -628,21 +631,45 @@ public void fetch_serverReturnsUnexpectedCode_throwsServerException() throws Exc
628
631
}
629
632
630
633
@ Test
631
- public void fetch_hasAnalyticsSdk_sendsUserProperties () throws Exception {
634
+ public void fetch_hasAnalyticsSdk_sendsUserPropertiesAndFirstOpenTime () throws Exception {
632
635
// Provide the mock Analytics SDK.
633
636
AnalyticsConnector mockAnalyticsConnector = mock (AnalyticsConnector .class );
634
637
fetchHandler = getNewFetchHandler (mockAnalyticsConnector );
638
+ long firstOpenTime = 1636146000000L ;
635
639
636
- Map <String , String > userProperties =
640
+ Map <String , String > customUserProperties =
637
641
ImmutableMap .of ("up_key1" , "up_val1" , "up_key2" , "up_val2" );
642
+ Map <String , Object > allUserProperties =
643
+ ImmutableMap .of (
644
+ "up_key1" , "up_val1" , "up_key2" , "up_val2" , FIRST_OPEN_TIME_KEY , firstOpenTime );
638
645
when (mockAnalyticsConnector .getUserProperties (/*includeInternal=*/ false ))
646
+ .thenReturn (ImmutableMap .copyOf (customUserProperties ));
647
+ when (mockAnalyticsConnector .getUserProperties (/*includeInternal=*/ true ))
648
+ .thenReturn (ImmutableMap .copyOf (allUserProperties ));
649
+
650
+ fetchCallToHttpClientUpdatesClockAndReturnsConfig (firstFetchedContainer );
651
+
652
+ assertWithMessage ("Fetch() failed!" ).that (fetchHandler .fetch ().isSuccessful ()).isTrue ();
653
+
654
+ verifyBackendIsCalled (customUserProperties , firstOpenTime );
655
+ }
656
+
657
+ @ Test
658
+ public void fetch_hasAnalyticsSdk_sendsUserPropertiesNoFirstOpenTime () throws Exception {
659
+ // Provide the mock Analytics SDK.
660
+ AnalyticsConnector mockAnalyticsConnector = mock (AnalyticsConnector .class );
661
+ fetchHandler = getNewFetchHandler (mockAnalyticsConnector );
662
+
663
+ Map <String , String > userProperties =
664
+ ImmutableMap .of ("up_key1" , "up_val1" , "up_key2" , "up_val2" );
665
+ when (mockAnalyticsConnector .getUserProperties (anyBoolean ()))
639
666
.thenReturn (ImmutableMap .copyOf (userProperties ));
640
667
641
668
fetchCallToHttpClientUpdatesClockAndReturnsConfig (firstFetchedContainer );
642
669
643
670
assertWithMessage ("Fetch() failed!" ).that (fetchHandler .fetch ().isSuccessful ()).isTrue ();
644
671
645
- verifyBackendIsCalled (userProperties );
672
+ verifyBackendIsCalled (userProperties , null );
646
673
}
647
674
648
675
@ Test
@@ -751,6 +778,7 @@ private void setBackendResponseConfigsTo(ConfigContainer container) throws Excep
751
778
/* analyticsUserProperties= */ any (),
752
779
/* lastFetchETag= */ any (),
753
780
/* customHeaders= */ any (),
781
+ /* firstOpenTime= */ any (),
754
782
/* currentTime= */ any ());
755
783
}
756
784
@@ -762,6 +790,7 @@ private void setBackendResponseToNoChange(Date date) throws Exception {
762
790
/* analyticsUserProperties= */ any (),
763
791
/* lastFetchETag= */ any (),
764
792
/* customHeaders= */ any (),
793
+ /* firstOpenTime= */ any (),
765
794
/* currentTime= */ any ()))
766
795
.thenReturn (FetchResponse .forBackendHasNoUpdates (date ));
767
796
}
@@ -776,6 +805,7 @@ private void fetchCallToBackendThrowsException(int httpErrorCode) throws Excepti
776
805
/* analyticsUserProperties= */ any (),
777
806
/* lastFetchETag= */ any (),
778
807
/* customHeaders= */ any (),
808
+ /* firstOpenTime= */ any (),
779
809
/* currentTime= */ any ());
780
810
}
781
811
@@ -855,10 +885,12 @@ private void verifyBackendIsCalled() throws Exception {
855
885
/* analyticsUserProperties= */ any (),
856
886
/* lastFetchETag= */ any (),
857
887
/* customHeaders= */ any (),
888
+ /* firstOpenTime= */ any (),
858
889
/* currentTime= */ any ());
859
890
}
860
891
861
- private void verifyBackendIsCalled (Map <String , String > userProperties ) throws Exception {
892
+ private void verifyBackendIsCalled (Map <String , String > userProperties , Long firstOpenTime )
893
+ throws Exception {
862
894
verify (mockBackendFetchApiClient )
863
895
.fetch (
864
896
any (HttpURLConnection .class ),
@@ -867,6 +899,7 @@ private void verifyBackendIsCalled(Map<String, String> userProperties) throws Ex
867
899
/* analyticsUserProperties= */ eq (userProperties ),
868
900
/* lastFetchETag= */ any (),
869
901
/* customHeaders= */ any (),
902
+ /* firstOpenTime= */ eq (firstOpenTime ),
870
903
/* currentTime= */ any ());
871
904
}
872
905
@@ -879,6 +912,7 @@ private void verifyBackendIsNeverCalled() throws Exception {
879
912
/* analyticsUserProperties= */ any (),
880
913
/* lastFetchETag= */ any (),
881
914
/* customHeaders= */ any (),
915
+ /* firstOpenTime= */ any (),
882
916
/* currentTime= */ any ());
883
917
}
884
918
@@ -891,6 +925,7 @@ private void verifyETags(@Nullable String requestETag, String responseETag) thro
891
925
/* analyticsUserProperties= */ any (),
892
926
/* lastFetchETag= */ eq (requestETag ),
893
927
/* customHeaders= */ any (),
928
+ /* firstOpenTime= */ any (),
894
929
/* currentTime= */ any ());
895
930
assertThat (metadataClient .getLastFetchETag ()).isEqualTo (responseETag );
896
931
}
0 commit comments