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 ;
22
19
import static org .mockito .Mockito .mock ;
23
20
import static org .mockito .Mockito .never ;
24
21
import static org .mockito .Mockito .spy ;
40
37
import org .junit .Before ;
41
38
import org .junit .Test ;
42
39
import org .junit .runner .RunWith ;
43
- import org .mockito .AdditionalMatchers ;
44
40
import org .mockito .ArgumentMatchers ;
45
41
import org .mockito .InOrder ;
46
42
import org .mockito .Mock ;
@@ -86,101 +82,12 @@ public void setApplicationContext_initializeGaugeMetadataManager()
86
82
inOrder .verify (mockGaugeManager ).initializeGaugeMetadataManager (any ());
87
83
}
88
84
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
85
// LogGaugeData on new perf session when Verbose
179
86
// NotLogGaugeData on new perf session when not Verbose
180
87
// Mark Session as expired after time limit.
181
88
182
89
@ Test
183
- public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesIfSessionIsNonVerbose () {
90
+ public void testUpdatePerfSessionMakesGaugeManagerStopCollectingGaugesIfSessionIsNonVerbose () {
184
91
forceNonVerboseSession ();
185
92
186
93
SessionManager testSessionManager =
@@ -191,7 +98,7 @@ public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesIfSessionIs
191
98
}
192
99
193
100
@ Test
194
- public void testOnUpdateAppStateMakesGaugeManagerStopCollectingGaugesWhenSessionsDisabled () {
101
+ public void testUpdatePerfSessionMakesGaugeManagerStopCollectingGaugesWhenSessionsDisabled () {
195
102
forceSessionsFeatureDisabled ();
196
103
197
104
SessionManager testSessionManager =
@@ -221,22 +128,25 @@ public void testSessionIdDoesNotUpdateIfPerfSessionRunsTooLong() {
221
128
}
222
129
223
130
@ Test
224
- public void testPerfSessionExpiredMakesGaugeManagerStopsCollectingGaugesIfSessionIsVerbose () {
225
- forceVerboseSession ();
131
+ public void testUpdatePerfSessionStartsCollectingGaugesIfSessionIsVerbose () {
226
132
Timer mockTimer = mock (Timer .class );
227
133
when (mockClock .getTime ()).thenReturn (mockTimer );
134
+ when (mockAppStateMonitor .getAppState ()).thenReturn (ApplicationProcessState .FOREGROUND );
228
135
229
- PerfSession session = new PerfSession ("sessionId" , mockClock );
230
- SessionManager testSessionManager =
231
- new SessionManager (mockGaugeManager , session , mockAppStateMonitor );
136
+ PerfSession previousSession = new PerfSession ("previousSession" , mockClock );
137
+ previousSession .setGaugeAndEventCollectionEnabled (false );
232
138
233
- assertThat (session .isSessionRunningTooLong ()).isFalse ();
139
+ PerfSession newSession = new PerfSession ("newSession" , mockClock );
140
+ newSession .setGaugeAndEventCollectionEnabled (true );
234
141
235
- when (mockTimer .getDurationMicros ())
236
- .thenReturn (TimeUnit .HOURS .toMicros (5 )); // Default Max Session Length is 4 hours
142
+ SessionManager testSessionManager =
143
+ new SessionManager (mockGaugeManager , previousSession , mockAppStateMonitor );
144
+ testSessionManager .updatePerfSession (newSession );
145
+ testSessionManager .setApplicationContext (mockApplicationContext );
237
146
238
- assertThat (session .isSessionRunningTooLong ()).isTrue ();
239
- verify (mockGaugeManager , times (0 )).logGaugeMetadata (any (), any ());
147
+ verify (mockGaugeManager , times (1 )).initializeGaugeMetadataManager (mockApplicationContext );
148
+ verify (mockGaugeManager , times (1 ))
149
+ .startCollectingGauges (newSession , ApplicationProcessState .FOREGROUND );
240
150
}
241
151
242
152
@ Test
0 commit comments