Search in sources :

Example 16 with Channel

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));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with Channel

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()));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) IOException(java.io.IOException) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) StackFrame(com.microsoft.azure.mobile.crashes.ingestion.models.StackFrame) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with Channel

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));
}
Also used : Context(android.content.Context) DefaultChannel(com.microsoft.azure.mobile.channel.DefaultChannel) Channel(com.microsoft.azure.mobile.channel.Channel) Handler(android.os.Handler) Looper(android.os.Looper) HandlerThread(android.os.HandlerThread) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with Channel

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);
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with 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);
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Channel (com.microsoft.azure.mobile.channel.Channel)36 Test (org.junit.Test)35 Context (android.content.Context)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Log (com.microsoft.azure.mobile.ingestion.models.Log)14 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)14 File (java.io.File)11 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)10 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)10 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)10 ArgumentMatcher (org.mockito.ArgumentMatcher)10 Matchers.anyString (org.mockito.Matchers.anyString)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)7 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)7 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)6 UUID (java.util.UUID)6 Answer (org.mockito.stubbing.Answer)4 TestUtils.generateString (com.microsoft.azure.mobile.test.TestUtils.generateString)3