Search in sources :

Example 1 with SessionContext

use of com.microsoft.appcenter.utils.context.SessionContext in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method testNativeCrashLog.

private ManagedErrorLog testNativeCrashLog(long appStartTime, long crashTime, boolean correlateSession, boolean hasDeviceInfo, boolean hasUserId) throws Exception {
    /* Create minidump sub-folder. */
    File minidumpSubfolder = mTemporaryFolder.newFolder("mockFolder");
    String mockUserId = "user-id";
    /* Create a file for a crash in disk. */
    File minidumpFile = new File(minidumpSubfolder, "mockFile.dmp");
    assertTrue(minidumpFile.createNewFile());
    assertTrue(minidumpFile.setLastModified(crashTime));
    /* Create an additional file in a folder to be filtered later. */
    File otherFile = new File(minidumpSubfolder, "otherFile.txt");
    long fakeCrashTime = new Date().getTime();
    assertTrue(otherFile.createNewFile());
    assertTrue(otherFile.setLastModified(fakeCrashTime));
    /* Mock session context. */
    mockStatic(SessionContext.class);
    SessionContext sessionContext = mock(SessionContext.class);
    when(SessionContext.getInstance()).thenReturn(sessionContext);
    if (correlateSession) {
        SessionContext.SessionInfo sessionInfo = mock(SessionContext.SessionInfo.class);
        when(sessionContext.getSessionAt(crashTime)).thenReturn(sessionInfo);
        when(sessionInfo.getAppLaunchTimestamp()).thenReturn(appStartTime);
    }
    /* Mock device info and ErrorLogHelper. */
    mockStatic(ErrorLogHelper.class);
    mockStatic(DeviceInfoHelper.class);
    Device device = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class));
    when(ErrorLogHelper.getStoredDeviceInfo(any(File.class))).thenReturn(hasDeviceInfo ? device : null);
    when(ErrorLogHelper.getStoredUserInfo(any(File.class))).thenReturn(hasUserId ? mockUserId : null);
    ErrorReport report = new ErrorReport();
    File errorLogFile = mock(File.class);
    when(errorLogFile.length()).thenReturn(1L);
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(errorLogFile);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[] { minidumpSubfolder });
    File pendingDir = mock(File.class);
    Whitebox.setInternalState(pendingDir, "path", "");
    when(ErrorLogHelper.getPendingMinidumpDirectory()).thenReturn(pendingDir);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), anyString())).thenReturn(report);
    when(ErrorLogHelper.parseLogFolderUuid(any(File.class))).thenReturn(UUID.randomUUID());
    when(FileManager.read(any(File.class))).thenReturn("");
    LogSerializer logSerializer = mock(LogSerializer.class);
    ArgumentCaptor<Log> log = ArgumentCaptor.forClass(Log.class);
    when(logSerializer.serializeLog(log.capture())).thenReturn("{}");
    when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<ManagedErrorLog>() {

        @Override
        public ManagedErrorLog answer(InvocationOnMock invocation) {
            com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
            mockException.setType(MINIDUMP_FILE);
            mockException.setMessage("message");
            ManagedErrorLog log = mock(ManagedErrorLog.class);
            when(log.getId()).thenReturn(UUID.randomUUID());
            when(log.getException()).thenReturn(mockException);
            return log;
        }
    });
    /* Start crashes. */
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), mock(Channel.class), "", null, true);
    /* Verify timestamps on the crash log. */
    assertTrue(Crashes.hasCrashedInLastSession().get());
    assertTrue(log.getValue() instanceof ManagedErrorLog);
    assertEquals(1, log.getAllValues().size());
    assertNotEquals(new Date(fakeCrashTime), log.getValue().getTimestamp());
    return (ManagedErrorLog) log.getValue();
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Device(com.microsoft.appcenter.ingestion.models.Device) Channel(com.microsoft.appcenter.channel.Channel) Date(java.util.Date) JSONException(org.json.JSONException) IOException(java.io.IOException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SessionContext(com.microsoft.appcenter.utils.context.SessionContext) File(java.io.File)

Example 2 with SessionContext

use of com.microsoft.appcenter.utils.context.SessionContext in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method dontEnqueueDistributionStartSessionLogIfLastSessionIdIsNull.

@Test
public void dontEnqueueDistributionStartSessionLogIfLastSessionIdIsNull() {
    /* Setup mock. */
    mockStatic(SessionContext.class);
    SessionContext sessionContext = mock(SessionContext.class);
    when(SessionContext.getInstance()).thenReturn(sessionContext);
    SessionContext.SessionInfo sessionInfo = mock(SessionContext.SessionInfo.class);
    when(sessionContext.getSessionAt(anyLong())).thenReturn(sessionInfo);
    when(sessionInfo.getSessionId()).thenReturn(null);
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn("r");
    /* Enable in-app updates. */
    start();
    Distribute.getInstance().storeRedirectionParameters("r", "g", null);
    /* Verify the log was sent. */
    verify(mChannel, never()).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), eq(DEFAULTS));
}
Also used : DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) SessionContext(com.microsoft.appcenter.utils.context.SessionContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with SessionContext

use of com.microsoft.appcenter.utils.context.SessionContext in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeDownloadTest method mockSessionContext.

private void mockSessionContext() {
    mockStatic(SessionContext.class);
    SessionContext sessionContext = mock(SessionContext.class);
    when(SessionContext.getInstance()).thenReturn(sessionContext);
    SessionContext.SessionInfo sessionInfo = mock(SessionContext.SessionInfo.class);
    when(sessionContext.getSessionAt(anyLong())).thenReturn(sessionInfo);
    when(sessionInfo.getSessionId()).thenReturn(UUID.randomUUID());
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext)

Example 4 with SessionContext

use of com.microsoft.appcenter.utils.context.SessionContext in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method enqueueDistributionStartSessionLogAfterEnablingUpdates.

@Test
public void enqueueDistributionStartSessionLogAfterEnablingUpdates() {
    /* Setup mock. */
    mockStatic(SessionContext.class);
    SessionContext sessionContext = mock(SessionContext.class);
    when(SessionContext.getInstance()).thenReturn(sessionContext);
    SessionContext.SessionInfo sessionInfo = mock(SessionContext.SessionInfo.class);
    when(sessionContext.getSessionAt(anyLong())).thenReturn(sessionInfo);
    when(sessionInfo.getSessionId()).thenReturn(UUID.randomUUID());
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn("r");
    /* Enable in-app updates. */
    start();
    Distribute.getInstance().storeRedirectionParameters("r", "g", null);
    /* Verify the log was sent. */
    verify(mChannel).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), eq(DEFAULTS));
}
Also used : DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) SessionContext(com.microsoft.appcenter.utils.context.SessionContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with SessionContext

use of com.microsoft.appcenter.utils.context.SessionContext in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method dontEnqueueDistributionStartSessionLogIfNoSessionsWereLoggedBefore.

@Test
public void dontEnqueueDistributionStartSessionLogIfNoSessionsWereLoggedBefore() {
    /* Setup mock. */
    mockStatic(SessionContext.class);
    SessionContext sessionContext = mock(SessionContext.class);
    when(SessionContext.getInstance()).thenReturn(sessionContext);
    when(sessionContext.getSessionAt(anyLong())).thenReturn(null);
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn("r");
    /* Enable in-app updates. */
    start();
    Distribute.getInstance().storeRedirectionParameters("r", "g", null);
    /* Verify the log was sent. */
    verify(mChannel, never()).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), anyInt());
}
Also used : DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) SessionContext(com.microsoft.appcenter.utils.context.SessionContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SessionContext (com.microsoft.appcenter.utils.context.SessionContext)5 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Context (android.content.Context)1 Log.getStackTraceString (android.util.Log.getStackTraceString)1 Channel (com.microsoft.appcenter.channel.Channel)1 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)1 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)1 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)1 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)1 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)1 Device (com.microsoft.appcenter.ingestion.models.Device)1 Log (com.microsoft.appcenter.ingestion.models.Log)1 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)1 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)1 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1