use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method printErrorOnJSONException.
@Test
public void printErrorOnJSONException() throws JSONException {
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
JSONException jsonException = new JSONException("Fake JSON exception");
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
Crashes crashes = Crashes.getInstance();
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
crashes.setLogSerializer(logSerializer);
crashes.onStarted(mockContext, "", mockChannel);
verify(mockChannel, never()).enqueue(any(Log.class), anyString());
verifyStatic();
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method trackExceptionForWrapperSdk.
@Test
public void trackExceptionForWrapperSdk() {
StackFrame frame = new StackFrame();
frame.setClassName("1");
frame.setFileName("2");
frame.setLineNumber(3);
frame.setMethodName("4");
final com.microsoft.azure.mobile.crashes.ingestion.models.Exception exception = new com.microsoft.azure.mobile.crashes.ingestion.models.Exception();
exception.setType("5");
exception.setMessage("6");
exception.setFrames(singletonList(frame));
Crashes crashes = Crashes.getInstance();
Channel mockChannel = mock(Channel.class);
Crashes.getInstance().trackException(exception);
verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
crashes.onStarted(mock(Context.class), "", mockChannel);
Crashes.getInstance().trackException(exception);
verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
return item instanceof ManagedErrorLog && exception.equals(((ManagedErrorLog) item).getException());
}
}), eq(crashes.getGroupName()));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class MobileCenterTest method enableTest.
@Test
public void enableTest() throws Exception {
/* Mock handler for asynchronous persistence */
HandlerThread mockHandlerThread = PowerMockito.mock(HandlerThread.class);
Looper mockLooper = PowerMockito.mock(Looper.class);
whenNew(HandlerThread.class).withArguments(THREAD_NAME).thenReturn(mockHandlerThread);
when(mockHandlerThread.getLooper()).thenReturn(mockLooper);
Handler mockPersistenceHandler = PowerMockito.mock(Handler.class);
whenNew(Handler.class).withArguments(mockLooper).thenReturn(mockPersistenceHandler);
when(mockPersistenceHandler.post(any(Runnable.class))).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
((Runnable) invocation.getArguments()[0]).run();
return true;
}
});
/* Start MobileCenter SDK */
MobileCenter.start(application, DUMMY_APP_SECRET, DummyService.class, AnotherDummyService.class);
MobileCenter mobileCenter = MobileCenter.getInstance();
/* Verify services are enabled by default */
Set<MobileCenterService> services = mobileCenter.getServices();
assertTrue(MobileCenter.isEnabled());
DummyService dummyService = DummyService.getInstance();
AnotherDummyService anotherDummyService = AnotherDummyService.getInstance();
for (MobileCenterService service : services) {
assertTrue(service.isInstanceEnabled());
}
/* Explicit set enabled should not change that */
MobileCenter.setEnabled(true);
assertTrue(MobileCenter.isEnabled());
for (MobileCenterService service : services) {
assertTrue(service.isInstanceEnabled());
}
verify(dummyService, never()).setInstanceEnabled(anyBoolean());
verify(anotherDummyService, never()).setInstanceEnabled(anyBoolean());
verify(mChannel, times(2)).setEnabled(true);
/* Verify disabling base disables all services */
MobileCenter.setEnabled(false);
assertFalse(MobileCenter.isEnabled());
for (MobileCenterService service : services) {
assertFalse(service.isInstanceEnabled());
}
verify(dummyService).setInstanceEnabled(false);
verify(anotherDummyService).setInstanceEnabled(false);
verify(application).unregisterActivityLifecycleCallbacks(dummyService);
verify(application).unregisterActivityLifecycleCallbacks(anotherDummyService);
verify(mChannel).setEnabled(false);
/* Verify re-enabling base re-enables all services */
MobileCenter.setEnabled(true);
assertTrue(MobileCenter.isEnabled());
for (MobileCenterService service : services) {
assertTrue(service.isInstanceEnabled());
}
verify(dummyService).setInstanceEnabled(true);
verify(anotherDummyService).setInstanceEnabled(true);
verify(application, times(2)).registerActivityLifecycleCallbacks(dummyService);
verify(application, times(2)).registerActivityLifecycleCallbacks(anotherDummyService);
verify(mChannel, times(3)).setEnabled(true);
/* Verify that disabling one service leaves base and other services enabled */
dummyService.setInstanceEnabled(false);
assertFalse(dummyService.isInstanceEnabled());
assertTrue(MobileCenter.isEnabled());
assertTrue(anotherDummyService.isInstanceEnabled());
/* Enable back via main class. */
MobileCenter.setEnabled(true);
assertTrue(MobileCenter.isEnabled());
for (MobileCenterService service : services) {
assertTrue(service.isInstanceEnabled());
}
verify(dummyService, times(2)).setInstanceEnabled(true);
verify(anotherDummyService).setInstanceEnabled(true);
verify(mChannel, times(4)).setEnabled(true);
/* Enable service after the SDK is disabled. */
MobileCenter.setEnabled(false);
assertFalse(MobileCenter.isEnabled());
for (MobileCenterService service : services) {
assertFalse(service.isInstanceEnabled());
}
dummyService.setInstanceEnabled(true);
assertFalse(dummyService.isInstanceEnabled());
PowerMockito.verifyStatic();
MobileCenterLog.error(eq(LOG_TAG), anyString());
assertFalse(MobileCenter.isEnabled());
verify(mChannel, times(2)).setEnabled(false);
/* Disable back via main class. */
MobileCenter.setEnabled(false);
assertFalse(MobileCenter.isEnabled());
for (MobileCenterService service : services) {
assertFalse(service.isInstanceEnabled());
}
verify(mChannel, times(3)).setEnabled(false);
/* Check factories / channel only once interactions. */
verify(dummyService).getLogFactories();
verify(dummyService).onStarted(any(Context.class), eq(DUMMY_APP_SECRET), any(Channel.class));
verify(anotherDummyService).getLogFactories();
verify(anotherDummyService).onStarted(any(Context.class), eq(DUMMY_APP_SECRET), any(Channel.class));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AbstractMobileCenterServiceTest method onChannelReadyDisabledThenEnable.
@Test
public void onChannelReadyDisabledThenEnable() {
Channel channel = mock(Channel.class);
service.setInstanceEnabled(false);
service.onStarted(mock(Context.class), "", channel);
verify(channel).removeGroup(service.getGroupName());
verify(channel).clear(service.getGroupName());
verifyNoMoreInteractions(channel);
Assert.assertSame(channel, service.mChannel);
service.setInstanceEnabled(true);
verify(channel).addGroup(service.getGroupName(), service.getTriggerCount(), service.getTriggerInterval(), service.getTriggerMaxParallelRequests(), service.getChannelListener());
verifyNoMoreInteractions(channel);
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AbstractMobileCenterServiceTest method onChannelReadyEnabledThenDisable.
@Test
public void onChannelReadyEnabledThenDisable() {
Channel channel = mock(Channel.class);
service.onStarted(mock(Context.class), "", channel);
verify(channel).removeGroup(service.getGroupName());
verify(channel).addGroup(service.getGroupName(), service.getTriggerCount(), service.getTriggerInterval(), service.getTriggerMaxParallelRequests(), service.getChannelListener());
verifyNoMoreInteractions(channel);
Assert.assertSame(channel, service.mChannel);
service.setInstanceEnabled(false);
verify(channel, times(2)).removeGroup(service.getGroupName());
verify(channel).clear(service.getGroupName());
verifyNoMoreInteractions(channel);
}
Aggregations