use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.
the class PropertyConfiguratorTest method overridingPartAIsNotRetroactive.
@Test
public void overridingPartAIsNotRetroactive() {
/* Mock deviceId. */
mockStatic(Secure.class);
when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId");
/* Start analytics and simulate background thread handler (we hold the thread command and run it in the test). */
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
final LinkedList<Runnable> backgroundCommands = new LinkedList<>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
backgroundCommands.add((Runnable) invocation.getArguments()[0]);
return null;
}
}).when(handler).post(notNull(Runnable.class), any(Runnable.class));
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, null, true);
/* Create a target. */
AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
/* Init target background code now. */
backgroundCommands.removeFirst().run();
/* Simulate a track event call with no property. */
CommonSchemaLog logBeforeSetProperty = new CommonSchemaEventLog();
logBeforeSetProperty.setExt(new Extensions());
logBeforeSetProperty.getExt().setApp(new AppExtension());
logBeforeSetProperty.getExt().setUser(new UserExtension());
logBeforeSetProperty.getExt().setDevice(new DeviceExtension());
/* Simulate what the pipeline does to convert from App Center to Common Schema. */
logBeforeSetProperty.addTransmissionTarget("test");
logBeforeSetProperty.setTag(target);
/* Set all common properties that should not be set retroactively before first log persisted. */
target.getPropertyConfigurator().setAppVersion("appVersion");
target.getPropertyConfigurator().setAppLocale("appLocale");
target.getPropertyConfigurator().setAppName("appName");
target.getPropertyConfigurator().setUserId("c:alice");
target.getPropertyConfigurator().collectDeviceId();
/* Run background commands in order: first prepare first log, then all the set property calls. */
target.getPropertyConfigurator().onPreparingLog(logBeforeSetProperty, "groupName");
for (Runnable command : backgroundCommands) {
command.run();
}
/* Simulate the second track event call. */
CommonSchemaLog logAfterSetProperty = new CommonSchemaEventLog();
logAfterSetProperty.setExt(new Extensions());
logAfterSetProperty.getExt().setApp(new AppExtension());
logAfterSetProperty.getExt().setUser(new UserExtension());
logAfterSetProperty.getExt().setDevice(new DeviceExtension());
/* Simulate what the pipeline does to convert from App Center to Common Schema. */
logAfterSetProperty.addTransmissionTarget("test");
logAfterSetProperty.setTag(target);
target.getPropertyConfigurator().onPreparingLog(logAfterSetProperty, "groupName");
/* Check first log has no property. */
assertNull(logBeforeSetProperty.getExt().getApp().getVer());
assertNull(logBeforeSetProperty.getExt().getApp().getLocale());
assertNull(logBeforeSetProperty.getExt().getApp().getName());
assertNull(logBeforeSetProperty.getExt().getUser().getLocalId());
assertNull(logBeforeSetProperty.getExt().getDevice().getLocalId());
/* Check second log has all of them. */
assertEquals("appVersion", logAfterSetProperty.getExt().getApp().getVer());
assertEquals("appLocale", logAfterSetProperty.getExt().getApp().getLocale());
assertEquals("appName", logAfterSetProperty.getExt().getApp().getName());
assertEquals("c:alice", logAfterSetProperty.getExt().getUser().getLocalId());
assertEquals("a:mockDeviceId", logAfterSetProperty.getExt().getDevice().getLocalId());
}
use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.
the class UncaughtExceptionHandlerTest method setUp.
@Before
public void setUp() {
Crashes.unsetInstance();
mockStatic(AppCenter.class);
mockStatic(AppCenterLog.class);
mockStatic(SystemClock.class);
mockStatic(FileManager.class);
mockStatic(SharedPreferencesManager.class);
mockStatic(ErrorLogHelper.class);
mockStatic(DeviceInfoHelper.class);
mockStatic(System.class);
@SuppressWarnings("unchecked") AppCenterFuture<Boolean> future = (AppCenterFuture<Boolean>) mock(AppCenterFuture.class);
when(AppCenter.isEnabled()).thenReturn(future);
when(future.get()).thenReturn(true);
when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
/* Then simulate further changes to state. */
PowerMockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) {
/* Whenever the new state is persisted, make further calls return the new state. */
boolean enabled = (Boolean) invocation.getArguments()[1];
Mockito.when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(enabled);
return null;
}
}).when(SharedPreferencesManager.class);
SharedPreferencesManager.putBoolean(eq(CRASHES_ENABLED_KEY), anyBoolean());
ManagedErrorLog errorLogMock = mock(ManagedErrorLog.class);
when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(new File("."));
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[0]);
when(ErrorLogHelper.createErrorLog(any(Context.class), any(Thread.class), any(Exception.class), Matchers.<Map<Thread, StackTraceElement[]>>any(), anyLong(), anyBoolean())).thenReturn(errorLogMock);
when(errorLogMock.getId()).thenReturn(UUID.randomUUID());
when(errorLogMock.getException()).thenReturn(new com.microsoft.appcenter.crashes.ingestion.models.Exception());
mDefaultExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(mDefaultExceptionHandler);
mExceptionHandler = new UncaughtExceptionHandler();
/* Mock handlers. */
mockStatic(HandlerUtils.class);
Answer<Void> runNow = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
((Runnable) invocation.getArguments()[0]).run();
return null;
}
};
doAnswer(runNow).when(HandlerUtils.class);
HandlerUtils.runOnUiThread(any(Runnable.class));
AppCenterHandler handler = mock(AppCenterHandler.class);
Crashes.getInstance().onStarting(handler);
doAnswer(runNow).when(handler).post(any(Runnable.class), any(Runnable.class));
}
use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTransmissionTargetTest method registerCallbackWhenDisabledWorks.
@Test
public void registerCallbackWhenDisabledWorks() {
/* Simulate disabling and background thread. */
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(backgroundRunnable.capture(), disabledRunnable.capture());
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
/* Disable. */
Analytics.setEnabled(false);
backgroundRunnable.getValue().run();
/* Add authentication provider while disabled. */
AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
/* Unlock command. */
disabledRunnable.getValue().run();
/* Verify update while disabled. */
assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider).acquireTokenAsync();
/* Enable. */
Analytics.setEnabled(true);
disabledRunnable.getValue().run();
/* Call prepare log. */
ProtocolExtension protocol = new ProtocolExtension();
Extensions ext = new Extensions();
ext.setProtocol(protocol);
CommonSchemaLog log = new CommonSchemaEventLog();
log.setExt(ext);
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
/* Verify log. */
assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());
/* And that we check expiry. */
verify(authenticationProvider).checkTokenExpiry();
}
Aggregations