Search in sources :

Example 36 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method checkStacktraceWithLegacyThrowableFile.

@Test
public void checkStacktraceWithLegacyThrowableFile() throws Exception {
    String expectedStacktrace = "type: message\n" + " ClassName.MethodName(FileName:1)";
    /* Mock FileManager call. */
    when(FileManager.read(any(File.class))).thenReturn(expectedStacktrace);
    /* Create throwable file. */
    UUID logId = UUID.randomUUID();
    String throwableFileName = logId + ErrorLogHelper.THROWABLE_FILE_EXTENSION;
    File errorStorageDirectory = mTemporaryFolder.newFolder("error");
    ErrorLogHelper.setErrorLogDirectory(errorStorageDirectory);
    File throwableFile = new File(errorStorageDirectory, throwableFileName);
    assertTrue(throwableFile.createNewFile());
    /* Write file content. */
    BufferedWriter writer = new BufferedWriter(new FileWriter(throwableFile));
    writer.write(expectedStacktrace);
    writer.close();
    /* Mock log. */
    ManagedErrorLog mockLog = new ManagedErrorLog();
    mockLog.setId(logId);
    mockLog.setErrorThreadName("Thread name");
    mockLog.setAppLaunchTimestamp(new Date());
    mockLog.setTimestamp(new Date());
    mockLog.setDevice(new Device());
    /* Build error report. */
    Crashes crashes = Mockito.spy(Crashes.getInstance());
    ErrorReport report = crashes.buildErrorReport(mockLog);
    /* Verify. */
    assertEquals(expectedStacktrace, report.getStackTrace());
    verify(crashes, never()).buildStackTrace(any(com.microsoft.appcenter.crashes.ingestion.models.Exception.class));
    verifyStatic();
    FileManager.read(any(File.class));
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) FileWriter(java.io.FileWriter) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) Date(java.util.Date) JSONException(org.json.JSONException) IOException(java.io.IOException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) BufferedWriter(java.io.BufferedWriter) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) UUID(java.util.UUID) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 37 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method checkStacktraceWhenThrowableFileIsEmpty.

@Test
public void checkStacktraceWhenThrowableFileIsEmpty() throws Exception {
    String expectedStacktrace = "type: message\n" + "\t at ClassName.MethodName(FileName:1)";
    /* Create empty throwable file. */
    UUID logId = UUID.randomUUID();
    String throwableFileName = logId + ErrorLogHelper.THROWABLE_FILE_EXTENSION;
    File errorStorageDirectory = mTemporaryFolder.newFolder("error");
    ErrorLogHelper.setErrorLogDirectory(errorStorageDirectory);
    File throwableFile = new File(errorStorageDirectory, throwableFileName);
    assertTrue(throwableFile.createNewFile());
    /* Prepare first frame. */
    final StackFrame stackFrame1 = new StackFrame();
    stackFrame1.setClassName("ClassName");
    stackFrame1.setMethodName("MethodName");
    stackFrame1.setFileName("FileName");
    stackFrame1.setLineNumber(1);
    /* Mock exception value. */
    com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
    when(mockException.getType()).thenReturn("type");
    when(mockException.getMessage()).thenReturn("message");
    when(mockException.getFrames()).thenReturn(Arrays.asList(stackFrame1));
    /* Mock log. */
    ManagedErrorLog mockLog = new ManagedErrorLog();
    mockLog.setId(logId);
    mockLog.setErrorThreadName("Thread name");
    mockLog.setAppLaunchTimestamp(new Date());
    mockLog.setTimestamp(new Date());
    mockLog.setDevice(new Device());
    mockLog.setException(mockException);
    /* Build error report. */
    Crashes crashes = Crashes.getInstance();
    /* Verify that stacktrace is null. */
    ErrorReport report = crashes.buildErrorReport(mockLog);
    assertEquals(expectedStacktrace, report.getStackTrace());
    verifyStatic(never());
    FileManager.read(any(File.class));
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) Date(java.util.Date) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) UUID(java.util.UUID) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 38 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method getChannelListener.

@Test
public void getChannelListener() throws JSONException {
    /* Mock and set exception data. */
    com.microsoft.appcenter.crashes.ingestion.models.Exception mockException = mock(com.microsoft.appcenter.crashes.ingestion.models.Exception.class);
    when(mockException.getType()).thenReturn("type");
    when(mockException.getMessage()).thenReturn("message");
    mErrorLog.setException(mockException);
    ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
    mockStatic(ErrorLogHelper.class);
    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[0]);
    when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE)).thenReturn(errorReport);
    when(FileManager.read(any(File.class))).thenReturn("");
    when(FileManager.read(any(File.class))).thenReturn(STACK_TRACE);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mErrorLog);
    CrashesListener crashesListener = mock(CrashesListener.class);
    when(crashesListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    Crashes.setListener(crashesListener);
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), mock(Channel.class), "", null, true);
    /* The error report was created and cached but device is null here. */
    verifyStatic();
    ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
    assertNull(errorReport.getDevice());
    /* The channel sets a device. */
    mErrorLog.setDevice(mock(Device.class));
    ArgumentCaptor<ErrorReport> errorReportCaptor = ArgumentCaptor.forClass(ErrorReport.class);
    Channel.GroupListener channelListener = crashes.getChannelListener();
    /* Simulate onBeforeSending event. */
    channelListener.onBeforeSending(mErrorLog);
    verify(crashesListener).onBeforeSending(errorReportCaptor.capture());
    assertErrorEquals(mErrorLog, errorReportCaptor.getValue());
    /* Simulate onSuccess event. */
    channelListener.onSuccess(mErrorLog);
    verify(crashesListener).onSendingSucceeded(errorReportCaptor.capture());
    assertErrorEquals(mErrorLog, errorReportCaptor.getValue());
    /* No more error reports should be produced at the point. */
    verifyStatic();
    ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
    /* Simulate onFailure event. */
    channelListener.onFailure(mErrorLog, EXCEPTION);
    verify(crashesListener).onSendingFailed(errorReportCaptor.capture(), eq(EXCEPTION));
    assertErrorEquals(mErrorLog, errorReportCaptor.getValue());
    /* onSuccess and onFailure invalidate the cache, so one more call is expected. */
    verifyStatic();
    ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, STACK_TRACE);
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) Device(com.microsoft.appcenter.ingestion.models.Device) Channel(com.microsoft.appcenter.channel.Channel) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 39 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method throwDeviceInfoExceptionWhenGetMinidumpSubfolderWithDeviceInfo.

@Test
public void throwDeviceInfoExceptionWhenGetMinidumpSubfolderWithDeviceInfo() throws java.lang.Exception {
    /* Prepare data. */
    Device mockDevice = mock(Device.class);
    mockStatic(DeviceInfoHelper.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("crash", new java.lang.Exception()));
    Context mockContext = mock(Context.class);
    File mockFile = mock(File.class);
    whenNew(File.class).withAnyArguments().thenReturn(mockFile);
    /* Verify. */
    ErrorLogHelper.getNewMinidumpSubfolderWithContextData(mockContext);
    verify(mockFile).delete();
}
Also used : Context(android.content.Context) Device(com.microsoft.appcenter.ingestion.models.Device) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method getStoredDeviceInfo.

@Test
public void getStoredDeviceInfo() throws IOException {
    String deviceInfoString = "{\"sdkName\":\"appcenter.android\",\"sdkVersion\":\"2.5.4.2\",\"model\":\"Android SDK built for x86\",\"oemName\":\"Google\",\"osName\":\"Android\",\"osVersion\":\"9\",\"osBuild\":\"PSR1.180720.075\",\"osApiLevel\":28,\"locale\":\"en_US\",\"timeZoneOffset\":240,\"screenSize\":\"1080x1794\",\"appVersion\":\"2.5.4.2\",\"carrierName\":\"Android\",\"carrierCountry\":\"us\",\"appBuild\":\"59\",\"appNamespace\":\"com.microsoft.appcenter.sasquatch.project\"}";
    File minidumpFolder = mTemporaryFolder.newFolder("minidump");
    File deviceInfoFile = new File(minidumpFolder, ErrorLogHelper.DEVICE_INFO_FILE);
    assertTrue(deviceInfoFile.createNewFile());
    mockStatic(FileManager.class);
    when(FileManager.read(eq(deviceInfoFile))).thenReturn(deviceInfoString);
    Device storedDeviceInfo = ErrorLogHelper.getStoredDeviceInfo(minidumpFolder);
    assertNotNull(storedDeviceInfo);
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Device (com.microsoft.appcenter.ingestion.models.Device)46 Test (org.junit.Test)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 Context (android.content.Context)18 File (java.io.File)15 Date (java.util.Date)11 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)10 Matchers.anyString (org.mockito.Matchers.anyString)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9 IOException (java.io.IOException)9 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)8 Log (com.microsoft.appcenter.ingestion.models.Log)8 JSONException (org.json.JSONException)8 PackageInfo (android.content.pm.PackageInfo)7 PackageManager (android.content.pm.PackageManager)7 Build (android.os.Build)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 TelephonyManager (android.telephony.TelephonyManager)6 Log.getStackTraceString (android.util.Log.getStackTraceString)6 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)6