Search in sources :

Example 26 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method startSessionAfterUserApprovalWeakReference.

@Test
public void startSessionAfterUserApprovalWeakReference() {
    /*
         * Disable analytics while in background to set up the initial condition
         * simulating the optin use case.
         */
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.setEnabled(false);
    /* App in foreground: no log yet, we are disabled. */
    analytics.onActivityResumed(new Activity());
    System.gc();
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Enable: start session not sent retroactively, weak reference lost. */
    Analytics.setEnabled(true);
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Channel(com.microsoft.azure.mobile.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 27 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class CrashesAndroidTest method wrapperSdkOverrideLog.

@Test
public void wrapperSdkOverrideLog() throws InterruptedException {
    Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
    Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
    Channel channel = mock(Channel.class);
    Crashes.getInstance().onStarted(sContext, "", channel);
    Crashes.WrapperSdkListener wrapperSdkListener = mock(Crashes.WrapperSdkListener.class);
    Crashes.getInstance().setWrapperSdkListener(wrapperSdkListener);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ManagedErrorLog errorLog = (ManagedErrorLog) invocationOnMock.getArguments()[0];
            errorLog.setErrorThreadName("ReplacedErrorThreadName");
            WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
            return null;
        }
    }).when(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
    final RuntimeException exception = new RuntimeException("mock");
    final Thread thread = new Thread() {

        @Override
        public void run() {
            throw exception;
        }
    };
    thread.start();
    thread.join();
    verify(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
    Crashes.unsetInstance();
    Crashes.getInstance().onStarted(sContext, "", channel);
    waitForCrashesHandlerTasksToComplete();
    Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport errorReport) {
            assertNotNull(errorReport);
            assertEquals("ReplacedErrorThreadName", errorReport.getThreadName());
        }
    });
}
Also used : Channel(com.microsoft.azure.mobile.channel.Channel) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 28 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method queuePendingCrashesAlwaysSend.

@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()));
    verify(mockChannel, times(errorAttachmentLogList.size())).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 29 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method setEnabled.

@Test
public void setEnabled() {
    /* Setup mock. */
    Crashes crashes = Crashes.getInstance();
    mockStatic(ErrorLogHelper.class);
    Channel mockChannel = mock(Channel.class);
    File dir = mock(File.class);
    File file1 = mock(File.class);
    File file2 = mock(File.class);
    when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(dir);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[0]);
    when(dir.listFiles()).thenReturn(new File[] { file1, file2 });
    /* Test. */
    assertTrue(Crashes.isEnabled());
    Crashes.setEnabled(true);
    assertTrue(Crashes.isEnabled());
    verify(mMockLooper, never()).quit();
    assertTrue(crashes.getInitializeTimestamp() > 0);
    Crashes.setEnabled(false);
    assertFalse(Crashes.isEnabled());
    verify(mMockLooper).quit();
    crashes.onStarted(mock(Context.class), "", mockChannel);
    verify(mockChannel).clear(crashes.getGroupName());
    verify(mockChannel).removeGroup(eq(crashes.getGroupName()));
    assertEquals(crashes.getInitializeTimestamp(), -1);
    assertFalse(Thread.getDefaultUncaughtExceptionHandler() instanceof UncaughtExceptionHandler);
    assertFalse(verify(file1).delete());
    assertFalse(verify(file2).delete());
    Crashes.trackException(EXCEPTION);
    verifyNoMoreInteractions(mockChannel);
    /* Enable back, testing double calls. */
    Crashes.setEnabled(true);
    assertTrue(Crashes.isEnabled());
    assertTrue(crashes.getInitializeTimestamp() > 0);
    assertTrue(Thread.getDefaultUncaughtExceptionHandler() instanceof UncaughtExceptionHandler);
    Crashes.setEnabled(true);
    assertTrue(Crashes.isEnabled());
    verify(mockChannel).addGroup(eq(crashes.getGroupName()), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
    Crashes.trackException(EXCEPTION);
    verify(mockChannel, times(1)).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
}
Also used : Context(android.content.Context) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method trackException.

@Test
public void trackException() {
    /* Track exception test. */
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    Crashes.trackException(EXCEPTION);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof ManagedErrorLog && EXCEPTION.getMessage().equals(((ManagedErrorLog) item).getException().getMessage());
        }
    }), eq(crashes.getGroupName()));
    ManagedErrorLog mockLog = mock(ManagedErrorLog.class);
    when(mockLog.getFatal()).thenReturn(false);
    CrashesListener mockListener = mock(CrashesListener.class);
    crashes.setInstanceListener(mockListener);
    /* Crashes callback test for trackException. */
    crashes.getChannelListener().onBeforeSending(mockLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(mockLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(mockLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
    ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
    crashes.getChannelListener().onBeforeSending(attachmentLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(attachmentLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) 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