Search in sources :

Example 1 with Device

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

the class ErrorLogHelperTest method getErrorReportFromErrorLog.

@Test
public void getErrorReportFromErrorLog() throws DeviceInfoHelper.DeviceInfoException {
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(SystemClock.elapsedRealtime()).thenReturn(1000L);
    when(Process.myPid()).thenReturn(123);
    /* Mock device. */
    Device mockDevice = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
    /* Mock process name. */
    ActivityManager activityManager = mock(ActivityManager.class);
    RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
    runningAppProcessInfo.pid = 123;
    runningAppProcessInfo.processName = "right.process";
    when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
    when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
    /* Mock architecture. */
    Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 23);
    Whitebox.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
    /* Create an error log. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new TestCrashException()), java.lang.Thread.getAllStackTraces(), 900, true);
    assertNotNull(errorLog);
    /* Test. */
    Throwable throwable = new RuntimeException();
    ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
    assertNotNull(report);
    assertEquals(errorLog.getId().toString(), report.getId());
    assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
    assertEquals(throwable, report.getThrowable());
    assertEquals(errorLog.getToffset() - errorLog.getAppLaunchTOffset(), report.getAppStartTime().getTime());
    assertEquals(errorLog.getToffset(), report.getAppErrorTime().getTime());
    assertEquals(errorLog.getDevice(), report.getDevice());
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Device(com.microsoft.azure.mobile.ingestion.models.Device) Build(android.os.Build) ActivityManager(android.app.ActivityManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Device

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

the class ErrorLogHelperTest method createErrorLog.

@Test
public void createErrorLog() throws DeviceInfoHelper.DeviceInfoException {
    /* Dummy coverage of utils class. */
    new ErrorLogHelper();
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(SystemClock.elapsedRealtime()).thenReturn(1000L);
    when(Process.myPid()).thenReturn(123);
    /* Mock device. */
    Device mockDevice = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
    /* Mock process name. */
    ActivityManager activityManager = mock(ActivityManager.class);
    RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
    runningAppProcessInfo.pid = 123;
    runningAppProcessInfo.processName = "right.process";
    when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
    when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
    /* Mock architecture. */
    Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 23);
    Whitebox.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
    /* Test. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), 900, true);
    assertNotNull(errorLog);
    assertNotNull(errorLog.getId());
    assertTrue(System.currentTimeMillis() - errorLog.getToffset() <= 1000);
    assertEquals(mockDevice, errorLog.getDevice());
    assertEquals(Integer.valueOf(123), errorLog.getProcessId());
    assertEquals("right.process", errorLog.getProcessName());
    assertNull(errorLog.getParentProcessId());
    assertNull(errorLog.getParentProcessName());
    assertEquals("armeabi-v7a", errorLog.getArchitecture());
    assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
    assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
    assertEquals(Boolean.TRUE, errorLog.getFatal());
    assertEquals(Long.valueOf(100), errorLog.getAppLaunchTOffset());
    /* Check first exception. */
    Exception topException = errorLog.getException();
    sanityCheck(topException);
    assertEquals(RuntimeException.class.getName(), topException.getType());
    assertNotNull(topException.getMessage());
    assertNotNull(topException.getInnerExceptions());
    assertEquals(1, topException.getInnerExceptions().size());
    /* Check second exception. */
    Exception middleException = topException.getInnerExceptions().get(0);
    sanityCheck(middleException);
    assertEquals(IOException.class.getName(), middleException.getType());
    assertNotNull(middleException.getInnerExceptions());
    assertEquals(1, middleException.getInnerExceptions().size());
    /* Check third exception. */
    Exception rootCauseException = middleException.getInnerExceptions().get(0);
    sanityCheck(rootCauseException);
    assertEquals(TestCrashException.class.getName(), rootCauseException.getType());
    assertNotNull(rootCauseException.getMessage());
    assertNull(rootCauseException.getInnerExceptions());
    /* Check threads. */
    assertNotNull(errorLog.getThreads());
    assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size());
    for (Thread thread : errorLog.getThreads()) {
        assertNotNull(thread);
        assertTrue(thread.getId() > 0);
        assertNotNull(thread.getName());
        assertNotNull(thread.getFrames());
        for (StackFrame frame : thread.getFrames()) {
            assertNotNull(frame);
            assertNotNull(frame.getClassName());
            assertNotNull(frame.getMethodName());
        }
    }
}
Also used : Context(android.content.Context) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) Device(com.microsoft.azure.mobile.ingestion.models.Device) IOException(java.io.IOException) ActivityManager(android.app.ActivityManager) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) IOException(java.io.IOException) Exception(com.microsoft.azure.mobile.crashes.ingestion.models.Exception) Thread(com.microsoft.azure.mobile.crashes.ingestion.models.Thread) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Build(android.os.Build) StackFrame(com.microsoft.azure.mobile.crashes.ingestion.models.StackFrame) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with Device

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

the class DeviceInfoHelperTest method getDeviceInfoMissingCarrierInfo.

@Test
public void getDeviceInfoMissingCarrierInfo() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    PackageManager packageManagerMock = mock(PackageManager.class);
    WindowManager windowManagerMock = mock(WindowManager.class);
    mockStatic(MobileCenterLog.class);
    /* Delegates to mock instances. */
    when(contextMock.getPackageManager()).thenReturn(packageManagerMock);
    when(contextMock.getSystemService(Context.TELEPHONY_SERVICE)).thenThrow(new RuntimeException());
    when(contextMock.getSystemService(Context.WINDOW_SERVICE)).thenReturn(windowManagerMock);
    //noinspection WrongConstant
    when(packageManagerMock.getPackageInfo(anyString(), anyInt())).thenReturn(mock(PackageInfo.class));
    when(windowManagerMock.getDefaultDisplay()).thenReturn(mock(Display.class));
    /* Verify. */
    Device device = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertNull(device.getCarrierCountry());
    assertNull(device.getCarrierName());
    verifyStatic();
    MobileCenterLog.error(eq(MobileCenter.LOG_TAG), anyString(), any(Exception.class));
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo) Device(com.microsoft.azure.mobile.ingestion.models.Device) WindowManager(android.view.WindowManager) Display(android.view.Display) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with Device

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

the class DeviceInfoHelperTest method getDeviceInfoMissingScreenSize.

@Test
public void getDeviceInfoMissingScreenSize() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    PackageManager packageManagerMock = mock(PackageManager.class);
    mockStatic(MobileCenterLog.class);
    /* Delegates to mock instances. */
    when(contextMock.getPackageManager()).thenReturn(packageManagerMock);
    when(contextMock.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mock(TelephonyManager.class));
    when(contextMock.getSystemService(Context.WINDOW_SERVICE)).thenThrow(new RuntimeException());
    //noinspection WrongConstant
    when(packageManagerMock.getPackageInfo(anyString(), anyInt())).thenReturn(mock(PackageInfo.class));
    /* Verify. */
    Device device = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertNull(device.getScreenSize());
    verifyStatic();
    MobileCenterLog.error(eq(MobileCenter.LOG_TAG), anyString(), any(Exception.class));
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) TelephonyManager(android.telephony.TelephonyManager) PackageInfo(android.content.pm.PackageInfo) Device(com.microsoft.azure.mobile.ingestion.models.Device) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Device

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

the class AndroidTestUtils method generateMockDevice.

@NonNull
private static Device generateMockDevice() {
    Device device = new Device();
    device.setSdkName("mobilecenter.android");
    device.setSdkVersion(String.format(Locale.ENGLISH, "%d.%d.%d", (RANDOM.nextInt(5) + 1), RANDOM.nextInt(10), RANDOM.nextInt(100)));
    device.setModel("S5");
    device.setOemName("HTC");
    device.setOsName("Android");
    device.setOsVersion(String.format(Locale.ENGLISH, "%d.%d.%d", (RANDOM.nextInt(5) + 1), RANDOM.nextInt(10), RANDOM.nextInt(100)));
    device.setOsBuild("LMY47X");
    device.setOsApiLevel(RANDOM.nextInt(9) + 15);
    device.setLocale("en_US");
    device.setTimeZoneOffset(RANDOM.nextInt(52) * 30 - 720);
    device.setScreenSize(String.format(Locale.ENGLISH, "%dx%d", (RANDOM.nextInt(4) + 1) * 1000, (RANDOM.nextInt(10) + 1) * 100));
    device.setAppVersion(String.format(Locale.ENGLISH, "%d.%d.%d", (RANDOM.nextInt(5) + 1), RANDOM.nextInt(10), RANDOM.nextInt(100)));
    device.setAppBuild(Integer.toString(RANDOM.nextInt(1000) + 1));
    device.setAppNamespace("com.microsoft.unittest");
    device.setWrapperSdkVersion("1.2.3.4");
    device.setWrapperSdkName("ReactNative");
    device.setLiveUpdateReleaseLabel("2.0.3-beta2");
    device.setLiveUpdateDeploymentKey("staging");
    device.setLiveUpdatePackageHash("aa896f791b26a7f464c0f62b0ba69f2b");
    return device;
}
Also used : Device(com.microsoft.azure.mobile.ingestion.models.Device) NonNull(android.support.annotation.NonNull)

Aggregations

Device (com.microsoft.azure.mobile.ingestion.models.Device)11 Test (org.junit.Test)8 Context (android.content.Context)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 TelephonyManager (android.telephony.TelephonyManager)4 Build (android.os.Build)3 ActivityManager (android.app.ActivityManager)2 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)2 NonNull (android.support.annotation.NonNull)2 Display (android.view.Display)2 WindowManager (android.view.WindowManager)2 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)2 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)2 Log (com.microsoft.azure.mobile.ingestion.models.Log)2 Point (android.graphics.Point)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ArrayAdapter (android.widget.ArrayAdapter)1