16
16
17
17
import static com .google .common .truth .Truth .assertThat ;
18
18
import static org .mockito .ArgumentMatchers .any ;
19
+ import static org .mockito .ArgumentMatchers .anyString ;
20
+ import static org .mockito .ArgumentMatchers .eq ;
21
+ import static org .mockito .ArgumentMatchers .nullable ;
19
22
import static org .mockito .Mockito .mock ;
20
23
import static org .mockito .Mockito .never ;
21
24
import static org .mockito .Mockito .spy ;
37
40
import org .junit .Before ;
38
41
import org .junit .Test ;
39
42
import org .junit .runner .RunWith ;
43
+ import org .mockito .AdditionalMatchers ;
40
44
import org .mockito .ArgumentMatchers ;
41
45
import org .mockito .InOrder ;
42
46
import org .mockito .Mock ;
@@ -79,9 +83,123 @@ public void setApplicationContext_initializeGaugeMetadataManager()
79
83
testSessionManager .setApplicationContext (mockApplicationContext );
80
84
81
85
testSessionManager .getSyncInitFuture ().get ();
82
- inOrder
83
- .verify (mockGaugeManager )
84
- .initializeGaugeMetadataManager (any (), ApplicationProcessState .FOREGROUND );
86
+ inOrder .verify (mockGaugeManager ).initializeGaugeMetadataManager (any ());
87
+ }
88
+
89
+ @ Test
90
+ public void testOnUpdateAppStateDoesNothingDuringAppStart () {
91
+ String oldSessionId = SessionManager .getInstance ().perfSession ().sessionId ();
92
+
93
+ assertThat (oldSessionId ).isNotNull ();
94
+ assertThat (oldSessionId ).isEqualTo (SessionManager .getInstance ().perfSession ().sessionId ());
95
+
96
+ AppStateMonitor .getInstance ().setIsColdStart (true );
97
+
98
+ SessionManager .getInstance ().onUpdateAppState (ApplicationProcessState .FOREGROUND );
99
+ assertThat (oldSessionId ).isEqualTo (SessionManager .getInstance ().perfSession ().sessionId ());
100
+ }
101
+
102
+ @ Test
103
+ public void testOnUpdateAppStateGeneratesNewSessionIdOnForegroundState () {
104
+ String oldSessionId = SessionManager .getInstance ().perfSession ().sessionId ();
105
+
106
+ assertThat (oldSessionId ).isNotNull ();
107
+ assertThat (oldSessionId ).isEqualTo (SessionManager .getInstance ().perfSession ().sessionId ());
108
+
109
+ SessionManager .getInstance ().onUpdateAppState (ApplicationProcessState .FOREGROUND );
110
+ assertThat (oldSessionId ).isNotEqualTo (SessionManager .getInstance ().perfSession ().sessionId ());
111
+ }
112
+
113
+ @ Test
114
+ public void testOnUpdateAppStateDoesntGenerateNewSessionIdOnBackgroundState () {
115
+ String oldSessionId = SessionManager .getInstance ().perfSession ().sessionId ();
116
+
117
+ assertThat (oldSessionId ).isNotNull ();
118
+ assertThat (oldSessionId ).isEqualTo (SessionManager .getInstance ().perfSession ().sessionId ());
119
+
120
+ SessionManager .getInstance ().onUpdateAppState (ApplicationProcessState .BACKGROUND );
121
+ assertThat (oldSessionId ).isEqualTo (SessionManager .getInstance ().perfSession ().sessionId ());
122
+ }
123
+
124
+ @ Test
125
+ public void testOnUpdateAppStateGeneratesNewSessionIdOnBackgroundStateIfPerfSessionExpires () {
126
+ when (mockPerfSession .isSessionRunningTooLong ()).thenReturn (true );
127
+ SessionManager testSessionManager =
128
+ new SessionManager (mockGaugeManager , mockPerfSession , mockAppStateMonitor );
129
+ String oldSessionId = testSessionManager .perfSession ().sessionId ();
130
+
131
+ assertThat (oldSessionId ).isNotNull ();
132
+ assertThat (oldSessionId ).isEqualTo (testSessionManager .perfSession ().sessionId ());
133
+
134
+ testSessionManager .onUpdateAppState (ApplicationProcessState .BACKGROUND );
135
+ assertThat (oldSessionId ).isNotEqualTo (testSessionManager .perfSession ().sessionId ());
136
+ }
137
+
138
+ @ Test
139
+ public void
140
+ testOnUpdateAppStateDoesntMakeGaugeManagerLogGaugeMetadataOnForegroundStateIfSessionIsNonVerbose () {
141
+ forceNonVerboseSession ();
142
+
143
+ SessionManager testSessionManager =
144
+ new SessionManager (mockGaugeManager , mockPerfSession , mockAppStateMonitor );
145
+ testSessionManager .onUpdateAppState (ApplicationProcessState .FOREGROUND );
146
+
147
+ verify (mockGaugeManager , never ())
148
+ .logGaugeMetadata (
149
+ anyString (), nullable (com .google .firebase .perf .v1 .ApplicationProcessState .class ));
150
+ }
151
+
152
+ @ Test
153
+ public void
154
+ testOnUpdateAppStateDoesntMakeGaugeManagerLogGaugeMetadataOnBackgroundStateEvenIfSessionIsVerbose () {
155
+ forceVerboseSession ();
156
+
157
+ SessionManager testSessionManager =
158
+ new SessionManager (mockGaugeManager , mockPerfSession , mockAppStateMonitor );
159
+ testSessionManager .onUpdateAppState (ApplicationProcessState .BACKGROUND );
160
+
161
+ verify (mockGaugeManager , never ())
162
+ .logGaugeMetadata (
163
+ anyString (), nullable (com .google .firebase .perf .v1 .ApplicationProcessState .class ));
164
+ }
165
+
166
+ @ Test
167
+ public void testOnUpdateAppStateMakesGaugeManagerStartCollectingGaugesIfSessionIsVerbose () {
168
+ forceVerboseSession ();
169
+
170
+ SessionManager testSessionManager =
171
+ new SessionManager (mockGaugeManager , mockPerfSession , mockAppStateMonitor );
172
+ testSessionManager .onUpdateAppState (ApplicationProcessState .FOREGROUND );
173
+
174
+ verify (mockGaugeManager )
175
+ .startCollectingGauges (AdditionalMatchers .not (eq (mockPerfSession )), any ());
176
+ }
177
+
178
+ // LogGaugeData on new perf session when Verbose
179
+ // NotLogGaugeData on new perf session when not Verbose
180
+ // Mark Session as expired after time limit.
181
+
182
+ @ Test
183
+ public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesIfSessionIsNonVerbose () {
184
+ forceNonVerboseSession ();
185
+
186
+ SessionManager testSessionManager =
187
+ new SessionManager (mockGaugeManager , mockPerfSession , mockAppStateMonitor );
188
+ testSessionManager .updatePerfSession (PerfSession .createWithId ("testSessionId" ));
189
+
190
+ verify (mockGaugeManager ).stopCollectingGauges ();
191
+ }
192
+
193
+ @ Test
194
+ public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesWhenSessionsDisabled () {
195
+ forceSessionsFeatureDisabled ();
196
+
197
+ SessionManager testSessionManager =
198
+ new SessionManager (
199
+ mockGaugeManager , PerfSession .createWithId ("testSessionId" ), mockAppStateMonitor );
200
+ testSessionManager .updatePerfSession (PerfSession .createWithId ("testSessionId2" ));
201
+
202
+ verify (mockGaugeManager ).stopCollectingGauges ();
85
203
}
86
204
87
205
@ Test
@@ -118,7 +236,7 @@ public void testPerfSessionExpiredMakesGaugeManagerStopsCollectingGaugesIfSessio
118
236
.thenReturn (TimeUnit .HOURS .toMicros (5 )); // Default Max Session Length is 4 hours
119
237
120
238
assertThat (session .isSessionRunningTooLong ()).isTrue ();
121
- verify (mockGaugeManager , times (0 )).logGaugeMetadata (any ());
239
+ verify (mockGaugeManager , times (0 )).logGaugeMetadata (any (), any () );
122
240
}
123
241
124
242
@ Test
@@ -129,7 +247,7 @@ public void testPerfSession_sessionAwareObjects_doesntNotifyIfNotRegistered() {
129
247
FakeSessionAwareObject spySessionAwareObjectOne = spy (new FakeSessionAwareObject ());
130
248
FakeSessionAwareObject spySessionAwareObjectTwo = spy (new FakeSessionAwareObject ());
131
249
132
- testSessionManager .updatePerfSession (PerfSession .createNewSession ( ));
250
+ testSessionManager .updatePerfSession (PerfSession .createWithId ( "testSessionId1" ));
133
251
134
252
verify (spySessionAwareObjectOne , never ())
135
253
.updateSession (ArgumentMatchers .nullable (PerfSession .class ));
@@ -148,8 +266,8 @@ public void testPerfSession_sessionAwareObjects_NotifiesIfRegistered() {
148
266
testSessionManager .registerForSessionUpdates (new WeakReference <>(spySessionAwareObjectOne ));
149
267
testSessionManager .registerForSessionUpdates (new WeakReference <>(spySessionAwareObjectTwo ));
150
268
151
- testSessionManager .updatePerfSession (PerfSession .createNewSession ( ));
152
- testSessionManager .updatePerfSession (PerfSession .createNewSession ( ));
269
+ testSessionManager .updatePerfSession (PerfSession .createWithId ( "testSessionId1" ));
270
+ testSessionManager .updatePerfSession (PerfSession .createWithId ( "testSessionId2" ));
153
271
154
272
verify (spySessionAwareObjectOne , times (2 ))
155
273
.updateSession (ArgumentMatchers .nullable (PerfSession .class ));
@@ -173,11 +291,11 @@ public void testPerfSession_sessionAwareObjects_DoesNotNotifyIfUnregistered() {
173
291
testSessionManager .registerForSessionUpdates (weakSpySessionAwareObjectOne );
174
292
testSessionManager .registerForSessionUpdates (weakSpySessionAwareObjectTwo );
175
293
176
- testSessionManager .updatePerfSession (PerfSession .createNewSession ( ));
294
+ testSessionManager .updatePerfSession (PerfSession .createWithId ( "testSessionId1" ));
177
295
178
296
testSessionManager .unregisterForSessionUpdates (weakSpySessionAwareObjectOne );
179
297
testSessionManager .unregisterForSessionUpdates (weakSpySessionAwareObjectTwo );
180
- testSessionManager .updatePerfSession (PerfSession .createNewSession ( ));
298
+ testSessionManager .updatePerfSession (PerfSession .createWithId ( "testSessionId2" ));
181
299
182
300
verify (spySessionAwareObjectOne , times (1 ))
183
301
.updateSession (ArgumentMatchers .nullable (PerfSession .class ));
0 commit comments