|
14 | 14 |
|
15 | 15 | package com.google.firebase.crashlytics.internal.common; |
16 | 16 |
|
| 17 | +import static org.mockito.AdditionalMatchers.not; |
17 | 18 | import static org.mockito.Mockito.any; |
18 | 19 | import static org.mockito.Mockito.anyLong; |
19 | 20 | import static org.mockito.Mockito.anyString; |
|
38 | 39 | import com.google.firebase.crashlytics.internal.NativeSessionFileProvider; |
39 | 40 | import com.google.firebase.crashlytics.internal.analytics.AnalyticsEventLogger; |
40 | 41 | import com.google.firebase.crashlytics.internal.metadata.LogFileManager; |
| 42 | +import com.google.firebase.crashlytics.internal.metadata.UserMetadata; |
41 | 43 | import com.google.firebase.crashlytics.internal.model.CrashlyticsReport; |
42 | 44 | import com.google.firebase.crashlytics.internal.persistence.FileStore; |
43 | 45 | import com.google.firebase.crashlytics.internal.settings.Settings; |
|
52 | 54 | import java.util.TreeSet; |
53 | 55 | import java.util.concurrent.Executor; |
54 | 56 | import java.util.concurrent.TimeUnit; |
| 57 | +import org.junit.Test; |
55 | 58 | import org.mockito.ArgumentCaptor; |
56 | 59 |
|
57 | 60 | public class CrashlyticsControllerTest extends CrashlyticsTestCase { |
@@ -101,22 +104,33 @@ private class ControllerBuilder { |
101 | 104 | private CrashlyticsNativeComponent nativeComponent = null; |
102 | 105 | private AnalyticsEventLogger analyticsEventLogger; |
103 | 106 | private SessionReportingCoordinator sessionReportingCoordinator; |
| 107 | + |
| 108 | + private CrashlyticsBackgroundWorker backgroundWorker; |
104 | 109 | private LogFileManager logFileManager = null; |
105 | 110 |
|
| 111 | + private UserMetadata userMetadata = null; |
| 112 | + |
106 | 113 | ControllerBuilder() { |
107 | 114 | dataCollectionArbiter = mockDataCollectionArbiter; |
108 | 115 | nativeComponent = mockNativeComponent; |
109 | 116 |
|
110 | 117 | analyticsEventLogger = mock(AnalyticsEventLogger.class); |
111 | 118 |
|
112 | 119 | sessionReportingCoordinator = mockSessionReportingCoordinator; |
| 120 | + |
| 121 | + backgroundWorker = new CrashlyticsBackgroundWorker(new SameThreadExecutorService()); |
113 | 122 | } |
114 | 123 |
|
115 | 124 | ControllerBuilder setDataCollectionArbiter(DataCollectionArbiter arbiter) { |
116 | 125 | dataCollectionArbiter = arbiter; |
117 | 126 | return this; |
118 | 127 | } |
119 | 128 |
|
| 129 | + ControllerBuilder setUserMetadata(UserMetadata userMetadata) { |
| 130 | + this.userMetadata = userMetadata; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
120 | 134 | public ControllerBuilder setNativeComponent(CrashlyticsNativeComponent nativeComponent) { |
121 | 135 | this.nativeComponent = nativeComponent; |
122 | 136 | return this; |
@@ -153,13 +167,13 @@ public CrashlyticsController build() { |
153 | 167 | final CrashlyticsController controller = |
154 | 168 | new CrashlyticsController( |
155 | 169 | testContext.getApplicationContext(), |
156 | | - new CrashlyticsBackgroundWorker(new SameThreadExecutorService()), |
| 170 | + backgroundWorker, |
157 | 171 | idManager, |
158 | 172 | dataCollectionArbiter, |
159 | 173 | testFileStore, |
160 | 174 | crashMarker, |
161 | 175 | appData, |
162 | | - null, |
| 176 | + userMetadata, |
163 | 177 | logFileManager, |
164 | 178 | sessionReportingCoordinator, |
165 | 179 | nativeComponent, |
@@ -210,6 +224,26 @@ public void testFatalException_callsSessionReportingCoordinatorPersistFatal() th |
210 | 224 | .persistFatalEvent(eq(fatal), eq(thread), eq(sessionId), anyLong()); |
211 | 225 | } |
212 | 226 |
|
| 227 | + @Test |
| 228 | + public void testOnDemandFatal_callLogFatalException() { |
| 229 | + Thread thread = Thread.currentThread(); |
| 230 | + Exception fatal = new RuntimeException("Fatal"); |
| 231 | + Thread.UncaughtExceptionHandler exceptionHandler = mock(Thread.UncaughtExceptionHandler.class); |
| 232 | + UserMetadata mockUserMetadata = mock(UserMetadata.class); |
| 233 | + when(mockSessionReportingCoordinator.listSortedOpenSessionIds()) |
| 234 | + .thenReturn(new TreeSet<>(Collections.singleton(SESSION_ID)).descendingSet()); |
| 235 | + |
| 236 | + final CrashlyticsController controller = |
| 237 | + builder() |
| 238 | + .setLogFileManager(new LogFileManager(testFileStore)) |
| 239 | + .setUserMetadata(mockUserMetadata) |
| 240 | + .build(); |
| 241 | + controller.enableExceptionHandling(SESSION_ID, exceptionHandler, testSettingsProvider); |
| 242 | + controller.logFatalException(thread, fatal); |
| 243 | + |
| 244 | + verify(mockUserMetadata).setNewSession(not(eq(SESSION_ID))); |
| 245 | + } |
| 246 | + |
213 | 247 | public void testNativeCrashDataCausesNativeReport() throws Exception { |
214 | 248 | final String sessionId = "sessionId_1_new"; |
215 | 249 | final String previousSessionId = "sessionId_0_previous"; |
|
0 commit comments