Search in sources :

Example 36 with Channel

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

the class CrashesTest method disabledDuringProcessPendingErrors.

@Test
public void disabledDuringProcessPendingErrors() throws IOException, ClassNotFoundException, JSONException {
    ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
    File errorStorageDirectory = mock(File.class);
    when(errorStorageDirectory.listFiles()).thenReturn(new File[0]);
    CrashesListener listener = mock(CrashesListener.class);
    when(listener.shouldProcess(errorReport)).thenReturn(true);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    Channel channel = mock(Channel.class);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) }).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport);
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Crashes.setEnabled(false);
            return "";
        }
    });
    /* Disabled while Crashes service is processing pending errors. */
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(listener);
    crashes.onStarted(mock(Context.class), "", channel);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    verify(listener).shouldProcess(errorReport);
    verifyNoMoreInteractions(listener);
    /* Disabled right before handling user confirmation. */
    Crashes.unsetInstance();
    Crashes.setEnabled(true);
    crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(listener);
    crashes.onStarted(mock(Context.class), "", channel);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    verify(listener, times(2)).shouldProcess(errorReport);
    verifyNoMoreInteractions(listener);
}
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) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) File(java.io.File) 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