Search in sources :

Example 51 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesAndroidTest method getLastSessionCrashReportNative.

@Test
public void getLastSessionCrashReportNative() throws Exception {
    /* Null before start. */
    Crashes.unsetInstance();
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
    assertNull(Crashes.getMinidumpDirectory().get());
    /* Simulate we have minidump from previous run. */
    File minidumpDirectory = getNewMinidumpDirectory();
    File sNewMinidumpDirectory = new File(minidumpDirectory, UUID.randomUUID().toString());
    FileManager.mkdir(sNewMinidumpDirectory.getPath());
    /* Simulate we have a minidump. */
    File newMinidumpDirectory = ErrorLogHelper.getNewMinidumpSubfolder();
    File minidumpFile = new File(newMinidumpDirectory, "minidump.dmp");
    FileManager.write(minidumpFile, "{\"DEVICE_INFO\":\"{mock minidump}\"}");
    /* Start crashes now. */
    startFresh(null);
    /* We can access directory now. */
    assertEquals(newMinidumpDirectory.getAbsolutePath(), Crashes.getMinidumpDirectory().get());
    ErrorReport errorReport = Crashes.getLastSessionCrashReport().get();
    assertNotNull(errorReport);
    assertTrue(Crashes.hasCrashedInLastSession().get());
    assertNotNull(errorReport.getStackTrace());
    /* File has been deleted. */
    assertFalse(minidumpFile.exists());
    /* After restart, it's processed. */
    Crashes.unsetInstance();
    startFresh(null);
    assertNull(Crashes.getLastSessionCrashReport().get());
    assertFalse(Crashes.hasCrashedInLastSession().get());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) File(java.io.File) Test(org.junit.Test)

Example 52 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class WrapperSdkExceptionManagerTest method handledErrorReportFailedToGetDeviceInfo.

@Test
@PrepareForTest(DeviceInfoHelper.class)
public void handledErrorReportFailedToGetDeviceInfo() throws DeviceInfoHelper.DeviceInfoException {
    /* If device info fails. */
    mockStatic(DeviceInfoHelper.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new java.lang.Exception()));
    /* When we build an handled error report. */
    String errorReportId = UUID.randomUUID().toString();
    /* Then error report is returned but without device info. */
    ErrorReport errorReport = WrapperSdkExceptionManager.buildHandledErrorReport(mock(Context.class), errorReportId);
    assertNotNull(errorReport);
    assertEquals(errorReportId, errorReport.getId());
    assertNotNull(errorReport.getAppErrorTime());
    assertNotNull(errorReport.getAppStartTime());
    assertNull(errorReport.getDevice());
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 53 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class WrapperSdkExceptionManagerAndroidTest method buildHandledErrorReport.

@Test
public void buildHandledErrorReport() {
    /* If we start the Crashes sdk. */
    long beforeStartTime = System.currentTimeMillis();
    startFresh();
    long afterStartTime = System.currentTimeMillis();
    /* When we build an error report for an handled error. */
    long beforeBuildTime = System.currentTimeMillis();
    String errorReportId = UUID.randomUUID().toString();
    ErrorReport errorReport = WrapperSdkExceptionManager.buildHandledErrorReport(sApplication, errorReportId);
    long afterBuildTime = System.currentTimeMillis();
    /* Then it contains the following properties. */
    assertNotNull(errorReport);
    assertEquals(errorReportId, errorReport.getId());
    assertNotNull(errorReport.getDevice());
    assertNotNull(errorReport.getAppErrorTime());
    assertNotNull(errorReport.getAppStartTime());
    assertNull(errorReport.getStackTrace());
    assertNull(errorReport.getThreadName());
    /* Check start time is consistent. */
    assertTrue(errorReport.getAppStartTime().getTime() >= beforeStartTime);
    assertTrue(errorReport.getAppStartTime().getTime() <= afterStartTime);
    /* Check error time is consistent. */
    assertTrue(errorReport.getAppErrorTime().getTime() >= beforeBuildTime);
    assertTrue(errorReport.getAppErrorTime().getTime() <= afterBuildTime);
    /* Check device info is cached. */
    ErrorReport errorReport2 = WrapperSdkExceptionManager.buildHandledErrorReport(sApplication, UUID.randomUUID().toString());
    assertSame(errorReport.getDevice(), errorReport2.getDevice());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) Test(org.junit.Test)

Example 54 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sSharedPreferences = getSharedPreferences("Sasquatch", Context.MODE_PRIVATE);
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().build());
    /* Set custom log URL if one was configured in settings. */
    String startType = sSharedPreferences.getString(APPCENTER_START_TYPE, StartType.APP_SECRET.toString());
    String logUrl = sSharedPreferences.getString(LOG_URL_KEY, getLogUrl(this, startType));
    if (!TextUtils.isEmpty(logUrl)) {
        AppCenter.setLogUrl(logUrl);
    }
    /* Set listeners. */
    AnalyticsPrivateHelper.setListener(getAnalyticsListener());
    Crashes.setListener(getCrashesListener());
    Distribute.setListener(new SasquatchDistributeListener());
    /* Set distribute urls. */
    String installUrl = getString(R.string.install_url);
    if (!TextUtils.isEmpty(installUrl)) {
        Distribute.setInstallUrl(installUrl);
    }
    String apiUrl = getString(R.string.api_url);
    if (!TextUtils.isEmpty(apiUrl)) {
        Distribute.setApiUrl(apiUrl);
    }
    /* Set crash attachments. */
    AttachmentsUtil.getInstance();
    /* Set max storage size. */
    setMaxStorageSize();
    /* Set debug enabled for distribute. */
    setDistributeEnabledForDebuggableBuild();
    /* Start App Center. */
    startAppCenter(getApplication(), startType);
    /* Set user id. */
    String userId = sSharedPreferences.getString(USER_ID_KEY, null);
    if (userId != null) {
        setUserId(userId);
    }
    /* Attach NDK Crash Handler after SDK is initialized. */
    Crashes.getMinidumpDirectory().thenAccept(new AppCenterConsumer<String>() {

        @Override
        public void accept(String path) {
            /* Path is null when Crashes is disabled. */
            if (path != null) {
                setupNativeCrashesListener(path);
            }
        }
    });
    /* Use some App Center getters. */
    AppCenter.getInstallId().thenAccept(new AppCenterConsumer<UUID>() {

        @Override
        public void accept(UUID uuid) {
            Log.i(LOG_TAG, "InstallId=" + uuid);
        }
    });
    /* Print last crash. */
    Crashes.hasCrashedInLastSession().thenAccept(new AppCenterConsumer<Boolean>() {

        @Override
        public void accept(Boolean crashed) {
            Log.i(LOG_TAG, "Crashes.hasCrashedInLastSession=" + crashed);
        }
    });
    Crashes.getLastSessionCrashReport().thenAccept(new AppCenterConsumer<ErrorReport>() {

        @Override
        public void accept(ErrorReport data) {
            if (data != null) {
                Log.i(LOG_TAG, "Crashes.getLastSessionCrashReport().getStackTrace()=" + data.getStackTrace());
            }
        }
    });
    /* Populate UI. */
    ((TextView) findViewById(R.id.package_name)).setText(String.format(getString(R.string.sdk_source_format), getPackageName().substring(getPackageName().lastIndexOf(".") + 1)));
    TestFeatures.initialize(this);
    ListView listView = findViewById(R.id.list);
    listView.setAdapter(new TestFeaturesListAdapter(TestFeatures.getAvailableControls()));
    listView.setOnItemClickListener(TestFeatures.getOnItemClickListener());
    /* Restore the MSA authentication callback. */
    String msaUserId = sSharedPreferences.getString(MSA_TOKEN_KEY, null);
    String refreshToken = sSharedPreferences.getString(MSA_REFRESH_TOKEN_KEY, null);
    String refreshTokenScope = sSharedPreferences.getString(MSA_REFRESH_TOKEN_SCOPE_KEY, null);
    int rawAuthType = sSharedPreferences.getInt(MSA_AUTH_TYPE_KEY, 0);
    if (msaUserId != null && refreshToken != null && refreshTokenScope != null) {
        AuthenticationProvider.Type mAuthType = AuthenticationProvider.Type.values()[rawAuthType];
        MSAAuthenticationProvider tokenProvider = MSAAuthenticationProvider.getInstance(refreshToken, refreshTokenScope, this);
        AuthenticationProvider provider = new AuthenticationProvider(mAuthType, msaUserId, tokenProvider);
        AnalyticsTransmissionTarget.addAuthenticationProvider(provider);
    }
}
Also used : MSAAuthenticationProvider(com.microsoft.appcenter.sasquatch.MSAAuthenticationProvider) AuthenticationProvider(com.microsoft.appcenter.analytics.AuthenticationProvider) MSAAuthenticationProvider(com.microsoft.appcenter.sasquatch.MSAAuthenticationProvider) SasquatchDistributeListener(com.microsoft.appcenter.sasquatch.listeners.SasquatchDistributeListener) TestFeaturesListAdapter(com.microsoft.appcenter.sasquatch.features.TestFeaturesListAdapter) SuppressLint(android.annotation.SuppressLint) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) StrictMode(android.os.StrictMode) ListView(android.widget.ListView) TextView(android.widget.TextView) UUID(java.util.UUID)

Aggregations

ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)54 Test (org.junit.Test)40 File (java.io.File)36 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 Context (android.content.Context)26 Channel (com.microsoft.appcenter.channel.Channel)25 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)22 UUID (java.util.UUID)21 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)18 Device (com.microsoft.appcenter.ingestion.models.Device)12 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)12 SessionContext (com.microsoft.appcenter.utils.context.SessionContext)12 SessionContext (com.microsoft.appcenter.SessionContext)11 Log (com.microsoft.appcenter.ingestion.models.Log)11 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)10 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)10 JSONException (org.json.JSONException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9