use of com.microsoft.appcenter.AppCenterHandler in project AppCenter-SDK-Android by Microsoft.
the class UncaughtExceptionHandlerTest method setUp.
@Before
public void setUp() throws java.lang.Exception {
Crashes.unsetInstance();
mockStatic(AppCenter.class);
mockStatic(AppCenterLog.class);
mockStatic(SystemClock.class);
mockStatic(StorageHelper.PreferencesStorage.class);
mockStatic(StorageHelper.InternalStorage.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(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
/* Then simulate further changes to state. */
PowerMockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
/* Whenever the new state is persisted, make further calls return the new state. */
boolean enabled = (Boolean) invocation.getArguments()[1];
Mockito.when(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(enabled);
return null;
}
}).when(StorageHelper.PreferencesStorage.class);
StorageHelper.PreferencesStorage.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());
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) throws Throwable {
((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 AppCenter-SDK-Android by Microsoft.
the class WrapperSdkExceptionManagerTest method setUp.
@Before
public void setUp() {
Crashes.unsetInstance();
mockStatic(AppCenter.class);
mockStatic(StorageHelper.PreferencesStorage.class);
mockStatic(StorageHelper.InternalStorage.class);
mockStatic(AppCenterLog.class);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory.getRoot());
ManagedErrorLog errorLogMock = mock(ManagedErrorLog.class);
when(errorLogMock.getId()).thenReturn(UUID.randomUUID());
when(ErrorLogHelper.createErrorLog(any(Context.class), any(Thread.class), any(Exception.class), Matchers.<Map<Thread, StackTraceElement[]>>any(), anyLong(), anyBoolean())).thenReturn(errorLogMock);
@SuppressWarnings("unchecked") AppCenterFuture<Boolean> future = (AppCenterFuture<Boolean>) mock(AppCenterFuture.class);
when(AppCenter.isEnabled()).thenReturn(future);
when(future.get()).thenReturn(true);
when(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(true);
/* Mock handlers. */
mockStatic(HandlerUtils.class);
Answer<Void> runNow = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((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 addTicketToLogBeforeStart.
@Test
public void addTicketToLogBeforeStart() {
/* Simulate not started. */
Analytics.unsetInstance();
when(AppCenter.isConfigured()).thenReturn(false);
/* No actions are prepared without authentication provider. */
CommonSchemaLog log = new CommonSchemaEventLog();
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
/* Add authentication provider before start. */
AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider).acquireTokenAsync();
/* Start analytics. */
when(AppCenter.isConfigured()).thenReturn(true);
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> normalRunnable = ArgumentCaptor.forClass(Runnable.class);
ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(normalRunnable.capture(), disabledRunnable.capture());
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, null, false);
/* No actions are prepared with no CommonSchemaLog. */
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(mock(Log.class), "test");
verify(authenticationProvider, never()).checkTokenExpiry();
/* Call prepare log. */
final ProtocolExtension protocol = new ProtocolExtension();
log.setExt(new Extensions() {
{
setProtocol(protocol);
}
});
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
/* Verify log. */
assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());
/* And that we check expiry. */
verify(authenticationProvider).checkTokenExpiry();
}
use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTransmissionTargetTest method updateAuthProviderAndLog.
@Test
public void updateAuthProviderAndLog() {
/* When we enqueue app center log from track event. */
final List<CommonSchemaLog> sentLogs = new ArrayList<>();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) {
ProtocolExtension protocol = new ProtocolExtension();
Extensions ext = new Extensions();
ext.setProtocol(protocol);
CommonSchemaLog log = new CommonSchemaEventLog();
log.setExt(ext);
sentLogs.add(log);
/* Call the listener after conversion of common schema for authentication decoration. */
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
return null;
}
}).when(mChannel).enqueue(any(Log.class), anyString(), anyInt());
/* 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);
ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(backgroundRunnable.capture(), any(Runnable.class));
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
/* Add first authentication provider. */
AuthenticationProvider.TokenProvider tokenProvider1 = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider1 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider1));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider1);
/* Check provider updated in background thread only when AppCenter is configured/started. */
assertNull(AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider1, never()).acquireTokenAsync();
assertNotNull(backgroundRunnable.getValue());
/* Run background thread. */
backgroundRunnable.getValue().run();
/* Check update. */
assertEquals(authenticationProvider1, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider1).acquireTokenAsync();
/* Track an event. */
Analytics.trackEvent("test1");
Runnable trackEvent1Command = backgroundRunnable.getValue();
/* Update authentication provider before the commands run and track a second event. */
AuthenticationProvider.TokenProvider tokenProvider2 = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider2 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key2", tokenProvider2));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider2);
Runnable addAuthProvider2Command = backgroundRunnable.getValue();
Analytics.trackEvent("test2");
Runnable trackEvent2Command = backgroundRunnable.getValue();
/* Simulate background thread doing everything in a sequence. */
trackEvent1Command.run();
addAuthProvider2Command.run();
trackEvent2Command.run();
/* Verify first log has first ticket. */
assertEquals(Collections.singletonList(authenticationProvider1.getTicketKeyHash()), sentLogs.get(0).getExt().getProtocol().getTicketKeys());
/* And that we checked expiry. */
verify(authenticationProvider1).checkTokenExpiry();
/* Verify second log has the second ticket. */
assertEquals(Collections.singletonList(authenticationProvider2.getTicketKeyHash()), sentLogs.get(1).getExt().getProtocol().getTicketKeys());
/* And that we checked expiry. */
verify(authenticationProvider2).checkTokenExpiry();
}
use of com.microsoft.appcenter.AppCenterHandler in project mobile-center-sdk-android by Microsoft.
the class WrapperSdkExceptionManagerTest method setUp.
@Before
public void setUp() {
Crashes.unsetInstance();
mockStatic(AppCenter.class);
mockStatic(FileManager.class);
mockStatic(SharedPreferencesManager.class);
mockStatic(AppCenterLog.class);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory.getRoot());
ManagedErrorLog errorLogMock = mock(ManagedErrorLog.class);
when(errorLogMock.getId()).thenReturn(UUID.randomUUID());
when(errorLogMock.getException()).thenReturn(new com.microsoft.appcenter.crashes.ingestion.models.Exception());
when(ErrorLogHelper.createErrorLog(any(Context.class), any(Thread.class), any(Exception.class), Matchers.<Map<Thread, StackTraceElement[]>>any(), anyLong(), anyBoolean())).thenReturn(errorLogMock);
@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);
/* 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));
}
Aggregations