|
1 | 1 | package com.instabug.flutter;
|
2 | 2 |
|
3 |
| -import static com.instabug.flutter.util.GlobalMocks.reflected; |
4 |
| -import static com.instabug.flutter.util.MockResult.makeResult; |
5 |
| -import static org.junit.Assert.assertEquals; |
6 |
| -import static org.mockito.ArgumentMatchers.any; |
7 |
| -import static org.mockito.ArgumentMatchers.anyInt; |
8 |
| -import static org.mockito.ArgumentMatchers.eq; |
9 |
| -import static org.mockito.Mockito.mock; |
10 |
| -import static org.mockito.Mockito.mockConstruction; |
11 |
| -import static org.mockito.Mockito.mockStatic; |
12 |
| -import static org.mockito.Mockito.verify; |
13 |
| -import static org.mockito.Mockito.when; |
14 |
| - |
15 | 3 | import com.instabug.apm.APM;
|
| 4 | +import com.instabug.apm.InternalAPM; |
| 5 | +import com.instabug.apm.configuration.cp.APMFeature; |
| 6 | +import com.instabug.apm.configuration.cp.FeatureAvailabilityCallback; |
16 | 7 | import com.instabug.apm.model.ExecutionTrace;
|
17 | 8 | import com.instabug.apm.networking.APMNetworkLogger;
|
18 | 9 | import com.instabug.flutter.generated.ApmPigeon;
|
19 | 10 | import com.instabug.flutter.modules.ApmApi;
|
20 | 11 | import com.instabug.flutter.util.GlobalMocks;
|
21 | 12 | import com.instabug.flutter.util.MockReflected;
|
22 | 13 |
|
| 14 | +import io.flutter.plugin.common.BinaryMessenger; |
| 15 | + |
23 | 16 | import org.json.JSONObject;
|
24 | 17 | import org.junit.After;
|
25 | 18 | import org.junit.Assert;
|
|
31 | 24 | import java.util.HashMap;
|
32 | 25 | import java.util.Map;
|
33 | 26 |
|
34 |
| -import io.flutter.plugin.common.BinaryMessenger; |
| 27 | +import static com.instabug.flutter.util.GlobalMocks.reflected; |
| 28 | +import static com.instabug.flutter.util.MockResult.makeResult; |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.mockito.ArgumentMatchers.*; |
| 31 | +import static org.mockito.Mockito.*; |
35 | 32 |
|
36 | 33 |
|
37 | 34 | public class ApmApiTest {
|
| 35 | + |
| 36 | + private final BinaryMessenger mMessenger = mock(BinaryMessenger.class); |
38 | 37 | private final ApmApi api = new ApmApi();
|
39 | 38 | private MockedStatic<APM> mAPM;
|
| 39 | + private MockedStatic<InternalAPM> mInternalApmStatic; |
40 | 40 | private MockedStatic<ApmPigeon.ApmHostApi> mHostApi;
|
41 | 41 |
|
42 | 42 | @Before
|
43 | 43 | public void setUp() throws NoSuchMethodException {
|
44 | 44 | mAPM = mockStatic(APM.class);
|
| 45 | + mInternalApmStatic = mockStatic(InternalAPM.class); |
45 | 46 | mHostApi = mockStatic(ApmPigeon.ApmHostApi.class);
|
46 | 47 | GlobalMocks.setUp();
|
47 | 48 | }
|
48 | 49 |
|
49 | 50 | @After
|
50 | 51 | public void cleanUp() {
|
| 52 | + mInternalApmStatic.close(); |
51 | 53 | mAPM.close();
|
52 | 54 | mHostApi.close();
|
53 | 55 | GlobalMocks.close();
|
@@ -266,4 +268,108 @@ public void testNetworkLogAndroid() {
|
266 | 268 | mAPMNetworkLogger.close();
|
267 | 269 | mJSONObject.close();
|
268 | 270 | }
|
| 271 | + |
| 272 | + @Test |
| 273 | + public void testStartUiTraceCP() { |
| 274 | + String screenName = "screen-name"; |
| 275 | + long microTimeStamp = System.currentTimeMillis() / 1000; |
| 276 | + long traceId = System.currentTimeMillis(); |
| 277 | + |
| 278 | + |
| 279 | + api.startCpUiTrace(screenName, microTimeStamp, traceId); |
| 280 | + |
| 281 | + mInternalApmStatic.verify(() -> InternalAPM._startUiTraceCP(screenName, microTimeStamp, traceId)); |
| 282 | + mInternalApmStatic.verifyNoMoreInteractions(); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + public void testReportScreenLoadingCP() { |
| 287 | + long startTimeStampMicro = System.currentTimeMillis() / 1000; |
| 288 | + long durationMicro = System.currentTimeMillis() / 1000; |
| 289 | + long uiTraceId = System.currentTimeMillis(); |
| 290 | + |
| 291 | + api.reportScreenLoadingCP(startTimeStampMicro, durationMicro, uiTraceId); |
| 292 | + |
| 293 | + mInternalApmStatic.verify(() -> InternalAPM._reportScreenLoadingCP(startTimeStampMicro, durationMicro, uiTraceId)); |
| 294 | + mInternalApmStatic.verifyNoMoreInteractions(); |
| 295 | + } |
| 296 | + |
| 297 | + @Test |
| 298 | + public void testEndScreenLoading() { |
| 299 | + long timeStampMicro = System.currentTimeMillis() / 1000; |
| 300 | + long uiTraceId = System.currentTimeMillis(); |
| 301 | + |
| 302 | + api.endScreenLoadingCP(timeStampMicro, uiTraceId); |
| 303 | + |
| 304 | + mInternalApmStatic.verify(() -> InternalAPM._endScreenLoadingCP(timeStampMicro, uiTraceId)); |
| 305 | + mInternalApmStatic.verifyNoMoreInteractions(); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + public void testIsEnabled() { |
| 310 | + boolean expected = true; |
| 311 | + ApmPigeon.Result<Boolean> result = spy(makeResult((actual) -> assertEquals(expected, actual))); |
| 312 | + mInternalApmStatic.when(() -> InternalAPM._isFeatureEnabledCP(eq(APMFeature.SCREEN_LOADING), any(), any(FeatureAvailabilityCallback.class))).thenAnswer(invocation -> { |
| 313 | + FeatureAvailabilityCallback callback = invocation.getArgument(1); |
| 314 | + callback.invoke(expected); |
| 315 | + return null; |
| 316 | + }); |
| 317 | + |
| 318 | + api.isEnabled(result); |
| 319 | + |
| 320 | + verify(result).success(expected); |
| 321 | + } |
| 322 | + |
| 323 | + @Test |
| 324 | + public void testIsScreenLoadingEnabled() { |
| 325 | + boolean expected = true; |
| 326 | + ApmPigeon.Result<Boolean> result = spy(makeResult((actual) -> assertEquals(expected, actual))); |
| 327 | + |
| 328 | + mInternalApmStatic.when(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any())).thenAnswer( |
| 329 | + invocation -> { |
| 330 | + FeatureAvailabilityCallback callback = (FeatureAvailabilityCallback) invocation.getArguments()[2]; |
| 331 | + callback.invoke(expected); |
| 332 | + return null; |
| 333 | + }); |
| 334 | + |
| 335 | + |
| 336 | + api.isScreenLoadingEnabled(result); |
| 337 | + |
| 338 | + mInternalApmStatic.verify(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any())); |
| 339 | + mInternalApmStatic.verifyNoMoreInteractions(); |
| 340 | + |
| 341 | + verify(result).success(expected); |
| 342 | + } |
| 343 | + |
| 344 | + @Test |
| 345 | + public void testIsEndScreenLoadingEnabled() { |
| 346 | + boolean expected = true; |
| 347 | + ApmPigeon.Result<Boolean> result = spy(makeResult((actual) -> assertEquals(expected, actual))); |
| 348 | + |
| 349 | + mInternalApmStatic.when(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any())).thenAnswer( |
| 350 | + invocation -> { |
| 351 | + FeatureAvailabilityCallback callback = (FeatureAvailabilityCallback) invocation.getArguments()[2]; |
| 352 | + callback.invoke(expected); |
| 353 | + return null; |
| 354 | + }); |
| 355 | + |
| 356 | + |
| 357 | + api.isEndScreenLoadingEnabled(result); |
| 358 | + |
| 359 | + mInternalApmStatic.verify(() -> InternalAPM._isFeatureEnabledCP(any(), any(), any())); |
| 360 | + mInternalApmStatic.verifyNoMoreInteractions(); |
| 361 | + |
| 362 | + verify(result).success(expected); |
| 363 | + |
| 364 | + } |
| 365 | + |
| 366 | + |
| 367 | + @Test |
| 368 | + public void testSetScreenLoadingMonitoringEnabled() { |
| 369 | + boolean isEnabled = false; |
| 370 | + |
| 371 | + api.setScreenLoadingEnabled(isEnabled); |
| 372 | + |
| 373 | + mAPM.verify(() -> APM.setScreenLoadingEnabled(isEnabled)); |
| 374 | + } |
269 | 375 | }
|
0 commit comments