19
19
import static org .mockito .Mockito .when ;
20
20
21
21
import com .google .firebase .crashlytics .internal .CrashlyticsTestCase ;
22
+ import com .google .firebase .crashlytics .internal .common .CrashlyticsAppQualitySessionsSubscriber ;
22
23
import com .google .firebase .crashlytics .internal .common .CrashlyticsReportWithSessionId ;
23
24
import com .google .firebase .crashlytics .internal .model .CrashlyticsReport ;
24
25
import com .google .firebase .crashlytics .internal .model .CrashlyticsReport .Session ;
42
43
import org .mockito .internal .util .collections .Sets ;
43
44
44
45
public class CrashlyticsReportPersistenceTest extends CrashlyticsTestCase {
45
-
46
46
private static final int VERY_LARGE_UPPER_LIMIT = 9999 ;
47
+ private static final String APP_QUALITY_SESSION_ID = "9" ;
47
48
48
49
private CrashlyticsReportPersistence reportPersistence ;
49
50
private FileStore fileStore ;
@@ -62,12 +63,22 @@ private static SettingsProvider createSettingsProviderMock(
62
63
return settingsProvider ;
63
64
}
64
65
66
+ private static CrashlyticsAppQualitySessionsSubscriber createSessionsSubscriberMock (
67
+ String appQualitySessionId ) {
68
+ CrashlyticsAppQualitySessionsSubscriber sessionsSubscriber =
69
+ mock (CrashlyticsAppQualitySessionsSubscriber .class );
70
+ when (sessionsSubscriber .getAppQualitySessionId ()).thenReturn (appQualitySessionId );
71
+ return sessionsSubscriber ;
72
+ }
73
+
65
74
@ Override
66
75
public void setUp () throws Exception {
67
76
fileStore = new FileStore (getContext ());
68
77
reportPersistence =
69
78
new CrashlyticsReportPersistence (
70
- fileStore , createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , VERY_LARGE_UPPER_LIMIT ));
79
+ fileStore ,
80
+ createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , VERY_LARGE_UPPER_LIMIT ),
81
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
71
82
}
72
83
73
84
public void testListSortedOpenSessionIds () {
@@ -147,6 +158,7 @@ public void testLoadFinalizedReports_reportThenEvent_returnsReportWithEvent() {
147
158
assertEquals (
148
159
testReport
149
160
.withSessionEndFields (endedAt , false , null )
161
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
150
162
.withEvents (ImmutableList .from (testEvent )),
151
163
finalizedReport );
152
164
}
@@ -172,6 +184,7 @@ public void testLoadFinalizedReports_reportThenMultipleEvents_returnsReportWithM
172
184
assertEquals (
173
185
testReport
174
186
.withSessionEndFields (endedAt , false , null )
187
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
175
188
.withEvents (ImmutableList .from (testEvent , testEvent2 )),
176
189
finalizedReport );
177
190
}
@@ -201,12 +214,14 @@ public void testLoadFinalizedReports_reportThenMultipleEvents_returnsReportWithM
201
214
assertEquals (
202
215
testReport1
203
216
.withSessionEndFields (endedAt , false , null )
217
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
204
218
.withEvents (ImmutableList .from (testEvent1 )),
205
219
finalizedReport1 );
206
220
final CrashlyticsReport finalizedReport2 = finalizedReports .get (0 ).getReport ();
207
221
assertEquals (
208
222
testReport2
209
223
.withSessionEndFields (endedAt , false , null )
224
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
210
225
.withEvents (ImmutableList .from (testEvent2 )),
211
226
finalizedReport2 );
212
227
}
@@ -274,7 +289,9 @@ public void testFinalizeReports_skipsCappingCurrentSession() throws IOException
274
289
public void testFinalizeReports_capsReports () {
275
290
reportPersistence =
276
291
new CrashlyticsReportPersistence (
277
- fileStore , createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ));
292
+ fileStore ,
293
+ createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ),
294
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
278
295
for (int i = 0 ; i < 10 ; i ++) {
279
296
persistReportWithEvent (reportPersistence , "testSession" + i , true );
280
297
}
@@ -298,7 +315,9 @@ public void testFinalizeReports_whenSettingsChanges_capsReports() throws IOExcep
298
315
new Settings (0 , sessionData2 , new FeatureFlagData (true , true , false ), 3 , 0 , 1.0 , 1.0 , 1 );
299
316
300
317
when (settingsProvider .getSettingsSync ()).thenReturn (settings1 );
301
- reportPersistence = new CrashlyticsReportPersistence (fileStore , settingsProvider );
318
+ reportPersistence =
319
+ new CrashlyticsReportPersistence (
320
+ fileStore , settingsProvider , createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
302
321
303
322
DecimalFormat format = new DecimalFormat ("00" );
304
323
for (int i = 0 ; i < 16 ; i ++) {
@@ -324,7 +343,9 @@ public void testFinalizeReports_whenSettingsChanges_capsReports() throws IOExcep
324
343
public void testFinalizeReports_removesLowPriorityReportsFirst () throws IOException {
325
344
reportPersistence =
326
345
new CrashlyticsReportPersistence (
327
- fileStore , createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ));
346
+ fileStore ,
347
+ createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ),
348
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
328
349
329
350
for (int i = 0 ; i < 10 ; i ++) {
330
351
boolean priority = i >= 3 && i <= 8 ;
@@ -347,7 +368,9 @@ public void testFinalizeReports_prioritizesNativeAndNonnativeFatals() throws IOE
347
368
CrashlyticsReport .FilesPayload filesPayload = makeFilePayload ();
348
369
reportPersistence =
349
370
new CrashlyticsReportPersistence (
350
- fileStore , createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ));
371
+ fileStore ,
372
+ createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ),
373
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
351
374
352
375
persistReportWithEvent (reportPersistence , "testSession1" , true );
353
376
reportPersistence .finalizeSessionWithNativeEvent ("testSession1" , filesPayload , null );
@@ -370,7 +393,9 @@ public void testFinalizeReports_prioritizesNativeAndNonnativeFatals() throws IOE
370
393
public void testFinalizeReports_removesOldestReportsFirst () throws IOException {
371
394
reportPersistence =
372
395
new CrashlyticsReportPersistence (
373
- fileStore , createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ));
396
+ fileStore ,
397
+ createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ),
398
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
374
399
for (int i = 0 ; i < 8 ; i ++) {
375
400
String sessionId = "testSession" + i ;
376
401
persistReportWithEvent (reportPersistence , sessionId , true );
@@ -519,7 +544,9 @@ public void testDeleteAllReports_removesAllReports() {
519
544
public void testPersistEvent_keepsAppropriateNumberOfMostRecentEvents () throws IOException {
520
545
reportPersistence =
521
546
new CrashlyticsReportPersistence (
522
- fileStore , createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , 4 ));
547
+ fileStore ,
548
+ createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , 4 ),
549
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
523
550
final String sessionId = "testSession" ;
524
551
final CrashlyticsReport testReport = makeTestReport (sessionId );
525
552
final CrashlyticsReport .Session .Event testEvent1 = makeTestEvent ("type1" , "reason1" );
@@ -547,6 +574,7 @@ public void testPersistEvent_keepsAppropriateNumberOfMostRecentEvents() throws I
547
574
assertEquals (
548
575
testReport
549
576
.withSessionEndFields (endedAt , false , null )
577
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
550
578
.withEvents (ImmutableList .from (testEvent2 , testEvent3 , testEvent4 , testEvent5 )),
551
579
finalizedReport );
552
580
}
@@ -563,7 +591,9 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
563
591
new Settings (0 , sessionData2 , new FeatureFlagData (true , true , false ), 3 , 0 , 1.0 , 1.0 , 1 );
564
592
565
593
when (settingsProvider .getSettingsSync ()).thenReturn (settings1 );
566
- reportPersistence = new CrashlyticsReportPersistence (fileStore , settingsProvider );
594
+ reportPersistence =
595
+ new CrashlyticsReportPersistence (
596
+ fileStore , settingsProvider , createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
567
597
568
598
final String sessionId = "testSession" ;
569
599
final CrashlyticsReport testReport = makeTestReport (sessionId );
@@ -592,6 +622,7 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
592
622
assertEquals (
593
623
testReport
594
624
.withSessionEndFields (endedAt , false , null )
625
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
595
626
.withEvents (ImmutableList .from (testEvent2 , testEvent3 , testEvent4 , testEvent5 )),
596
627
finalizedReport );
597
628
@@ -629,6 +660,7 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
629
660
assertEquals (
630
661
testReport2
631
662
.withSessionEndFields (endedAt , false , null )
663
+ .withAppQualitySessionId (APP_QUALITY_SESSION_ID )
632
664
.withEvents (
633
665
ImmutableList .from (
634
666
testEvent3 ,
@@ -645,7 +677,9 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
645
677
public void testPersistReportWithAnrEvent () throws IOException {
646
678
reportPersistence =
647
679
new CrashlyticsReportPersistence (
648
- fileStore , createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , 4 ));
680
+ fileStore ,
681
+ createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , 4 ),
682
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID ));
649
683
final String sessionId = "testSession" ;
650
684
final CrashlyticsReport testReport = makeTestReport (sessionId );
651
685
final Event testEvent = makeTestAnrEvent ();
@@ -663,6 +697,81 @@ public void testPersistReportWithAnrEvent() throws IOException {
663
697
assertEquals (1 , finalizedReport .getSession ().getEvents ().size ());
664
698
}
665
699
700
+ public void testFinalizeReports_missingAppQualitySessionId () {
701
+ reportPersistence =
702
+ new CrashlyticsReportPersistence (
703
+ fileStore ,
704
+ createSettingsProviderMock (4 , VERY_LARGE_UPPER_LIMIT ),
705
+ // Simulate Sessions subscriber failure by setting appQualitySessionId to null.
706
+ createSessionsSubscriberMock (/* appQualitySessionId= */ null ));
707
+
708
+ String sessionId = "testSession" ;
709
+ CrashlyticsReport testReport = makeTestReport (sessionId );
710
+ CrashlyticsReport .Session .Event testEvent = makeTestEvent ();
711
+
712
+ reportPersistence .persistReport (testReport );
713
+ reportPersistence .persistEvent (testEvent , sessionId );
714
+
715
+ long endedAt = System .currentTimeMillis ();
716
+
717
+ reportPersistence .finalizeReports ("skippedSession" , endedAt );
718
+
719
+ List <CrashlyticsReportWithSessionId > finalizedReports =
720
+ reportPersistence .loadFinalizedReports ();
721
+ assertEquals (1 , finalizedReports .size ());
722
+ CrashlyticsReport finalizedReport = finalizedReports .get (0 ).getReport ();
723
+ assertNotNull (finalizedReport .getSession ());
724
+ assertEquals (
725
+ testReport
726
+ .withSessionEndFields (endedAt , false , null )
727
+ .withEvents (ImmutableList .from (testEvent )),
728
+ finalizedReport );
729
+
730
+ // getAppQualitySessionId should return null since sessions subscriber never got an id.
731
+ assertNull (finalizedReport .getSession ().getAppQualitySessionId ());
732
+ }
733
+
734
+ public void testPersistEvent_updatesLatestAppQualitySession () {
735
+ CrashlyticsAppQualitySessionsSubscriber mockSessionsSubscriber =
736
+ createSessionsSubscriberMock (APP_QUALITY_SESSION_ID );
737
+ CrashlyticsReportPersistence reportPersistence =
738
+ new CrashlyticsReportPersistence (
739
+ fileStore ,
740
+ createSettingsProviderMock (VERY_LARGE_UPPER_LIMIT , VERY_LARGE_UPPER_LIMIT ),
741
+ mockSessionsSubscriber );
742
+
743
+ String sessionId = "testSession" ;
744
+ CrashlyticsReport testReport = makeTestReport (sessionId );
745
+ CrashlyticsReport .Session .Event testEvent1 = makeTestEvent ("type1" , "reason1" );
746
+ CrashlyticsReport .Session .Event testEvent2 = makeTestEvent ("type2" , "reason2" );
747
+ CrashlyticsReport .Session .Event testEvent3 = makeTestEvent ("type3" , "reason3" );
748
+
749
+ reportPersistence .persistReport (testReport );
750
+ reportPersistence .persistEvent (testEvent1 , sessionId );
751
+ reportPersistence .persistEvent (testEvent2 , sessionId );
752
+
753
+ // Simulate a new app quality sessions session before the last event.
754
+ String latestAppQualitySessionId = "300" ;
755
+ when (mockSessionsSubscriber .getAppQualitySessionId ()).thenReturn (latestAppQualitySessionId );
756
+ reportPersistence .persistEvent (testEvent3 , sessionId );
757
+
758
+ long endedAt = System .currentTimeMillis ();
759
+
760
+ reportPersistence .finalizeReports ("skippedSession" , endedAt );
761
+
762
+ List <CrashlyticsReportWithSessionId > finalizedReports =
763
+ reportPersistence .loadFinalizedReports ();
764
+ assertEquals (1 , finalizedReports .size ());
765
+ CrashlyticsReport finalizedReport = finalizedReports .get (0 ).getReport ();
766
+ assertNotNull (finalizedReport .getSession ());
767
+ assertEquals (
768
+ testReport
769
+ .withSessionEndFields (endedAt , false , null )
770
+ .withAppQualitySessionId (latestAppQualitySessionId )
771
+ .withEvents (ImmutableList .from (testEvent1 , testEvent2 , testEvent3 )),
772
+ finalizedReport );
773
+ }
774
+
666
775
private static void persistReportWithEvent (
667
776
CrashlyticsReportPersistence reportPersistence , String sessionId , boolean isHighPriority ) {
668
777
CrashlyticsReport testReport = makeTestReport (sessionId );
0 commit comments