Search in sources :

Example 31 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project AppCenter-SDK-Android by Microsoft.

the class DeviceInfoHelperTest method getDeviceInfoEmptyCarrierInfo.

@Test
public void getDeviceInfoEmptyCarrierInfo() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    PackageManager packageManagerMock = mock(PackageManager.class);
    /* Delegates to mock instances. */
    when(contextMock.getPackageManager()).thenReturn(packageManagerMock);
    // noinspection WrongConstant
    when(packageManagerMock.getPackageInfo(anyString(), anyInt())).thenReturn(mock(PackageInfo.class));
    TelephonyManager telephonyManager = mock(TelephonyManager.class);
    when(telephonyManager.getNetworkCountryIso()).thenReturn("");
    when(telephonyManager.getNetworkOperatorName()).thenReturn("");
    when(contextMock.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(telephonyManager);
    mockStatic(TextUtils.class);
    when(TextUtils.isEmpty(anyString())).thenReturn(true);
    /* Verify. */
    Device device = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertNull(device.getCarrierCountry());
    assertNull(device.getCarrierName());
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) TelephonyManager(android.telephony.TelephonyManager) PackageInfo(android.content.pm.PackageInfo) Device(com.microsoft.appcenter.ingestion.models.Device) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project AppCenter-SDK-Android by Microsoft.

the class DeviceInfoHelperTest method getDeviceInfo.

@Test
public void getDeviceInfo() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException {
    /* Mock data. */
    final String appVersion = "1.0";
    final String appBuild = "1";
    // noinspection SpellCheckingInspection
    final String appNamespace = "com.contoso.app";
    final String carrierCountry = "us";
    final String carrierName = "mock-service";
    final Locale locale = Locale.KOREA;
    final String model = "mock-model";
    final String oemName = "mock-manufacture";
    final Integer osApiLevel = 23;
    final String osName = "Android";
    final String osVersion = "mock-version";
    final String osBuild = "mock-os-build";
    final String screenSizeLandscape = "100x200";
    final String screenSizePortrait = "200x100";
    final TimeZone timeZone = TimeZone.getTimeZone("KST");
    final Integer timeZoneOffset = timeZone.getOffset(System.currentTimeMillis());
    Locale.setDefault(locale);
    TimeZone.setDefault(timeZone);
    /* Mocking instances. */
    Context contextMock = mock(Context.class);
    PackageManager packageManagerMock = mock(PackageManager.class);
    PackageInfo packageInfoMock = mock(PackageInfo.class);
    WindowManager windowManagerMock = mock(WindowManager.class);
    TelephonyManager telephonyManagerMock = mock(TelephonyManager.class);
    Display displayMock = mock(Display.class);
    /* Delegates to mock instances. */
    when(contextMock.getPackageName()).thenReturn(appNamespace);
    when(contextMock.getPackageManager()).thenReturn(packageManagerMock);
    // noinspection WrongConstant
    when(contextMock.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(telephonyManagerMock);
    // noinspection WrongConstant
    when(contextMock.getSystemService(eq(Context.WINDOW_SERVICE))).thenReturn(windowManagerMock);
    // noinspection WrongConstant
    when(packageManagerMock.getPackageInfo(anyString(), eq(0))).thenReturn(packageInfoMock);
    when(telephonyManagerMock.getNetworkCountryIso()).thenReturn(carrierCountry);
    when(telephonyManagerMock.getNetworkOperatorName()).thenReturn(carrierName);
    when(windowManagerMock.getDefaultDisplay()).thenReturn(displayMock);
    when(displayMock.getRotation()).thenReturn(Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, Surface.ROTATION_270);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            /* DO NOT call set method and assign values directly to variables. */
            ((Point) args[0]).x = 100;
            ((Point) args[0]).y = 200;
            return null;
        }
    }).when(displayMock).getSize(any(Point.class));
    /* Sets values of fields for static classes. */
    Whitebox.setInternalState(packageInfoMock, "versionName", appVersion);
    Whitebox.setInternalState(packageInfoMock, "versionCode", Integer.parseInt(appBuild));
    Whitebox.setInternalState(Build.class, "MODEL", model);
    Whitebox.setInternalState(Build.class, "MANUFACTURER", oemName);
    Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", osApiLevel);
    Whitebox.setInternalState(Build.class, "ID", osBuild);
    Whitebox.setInternalState(Build.VERSION.class, "RELEASE", osVersion);
    /* First call */
    Device device = DeviceInfoHelper.getDeviceInfo(contextMock);
    /* Verify device information. */
    assertNull(device.getWrapperSdkName());
    assertNull(device.getWrapperSdkVersion());
    assertEquals(BuildConfig.VERSION_NAME, device.getSdkVersion());
    assertEquals(appVersion, device.getAppVersion());
    assertEquals(appBuild, device.getAppBuild());
    assertEquals(appNamespace, device.getAppNamespace());
    assertEquals(carrierCountry, device.getCarrierCountry());
    assertEquals(carrierName, device.getCarrierName());
    assertEquals(locale.toString(), device.getLocale());
    assertEquals(model, device.getModel());
    assertEquals(oemName, device.getOemName());
    assertEquals(osApiLevel, device.getOsApiLevel());
    assertEquals(osName, device.getOsName());
    assertEquals(osVersion, device.getOsVersion());
    assertEquals(osBuild, device.getOsBuild());
    assertEquals(screenSizeLandscape, device.getScreenSize());
    assertEquals(timeZoneOffset, device.getTimeZoneOffset());
    /* Verify screen size based on different orientations (Surface.ROTATION_90). */
    device = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertEquals(screenSizePortrait, device.getScreenSize());
    /* Verify screen size based on different orientations (Surface.ROTATION_180). */
    device = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertEquals(screenSizeLandscape, device.getScreenSize());
    /* Verify screen size based on different orientations (Surface.ROTATION_270). */
    device = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertEquals(screenSizePortrait, device.getScreenSize());
    /* Make sure screen size is verified for all orientations. */
    verify(displayMock, times(4)).getRotation();
    /* Set wrapper sdk information. */
    WrapperSdk wrapperSdk = new WrapperSdk();
    wrapperSdk.setWrapperSdkVersion("1.2.3.4");
    wrapperSdk.setWrapperSdkName("ReactNative");
    wrapperSdk.setWrapperRuntimeVersion("4.13");
    wrapperSdk.setLiveUpdateReleaseLabel("2.0.3-beta2");
    wrapperSdk.setLiveUpdateDeploymentKey("staging");
    wrapperSdk.setLiveUpdatePackageHash("aa896f791b26a7f464c0f62b0ba69f2b");
    DeviceInfoHelper.setWrapperSdk(wrapperSdk);
    Device device2 = DeviceInfoHelper.getDeviceInfo(contextMock);
    assertEquals(wrapperSdk.getWrapperSdkVersion(), device2.getWrapperSdkVersion());
    assertEquals(wrapperSdk.getWrapperSdkName(), device2.getWrapperSdkName());
    assertEquals(wrapperSdk.getWrapperRuntimeVersion(), device2.getWrapperRuntimeVersion());
    assertEquals(wrapperSdk.getLiveUpdateReleaseLabel(), device2.getLiveUpdateReleaseLabel());
    assertEquals(wrapperSdk.getLiveUpdateDeploymentKey(), device2.getLiveUpdateDeploymentKey());
    assertEquals(wrapperSdk.getLiveUpdatePackageHash(), device2.getLiveUpdatePackageHash());
    /* Check non wrapped sdk information are still generated correctly. */
    device2.setWrapperSdkVersion(null);
    device2.setWrapperSdkName(null);
    device2.setWrapperRuntimeVersion(null);
    device2.setLiveUpdateReleaseLabel(null);
    device2.setLiveUpdateDeploymentKey(null);
    device2.setLiveUpdatePackageHash(null);
    assertEquals(device, device2);
    /* Remove wrapper SDK information. */
    DeviceInfoHelper.setWrapperSdk(null);
    assertEquals(device, DeviceInfoHelper.getDeviceInfo(contextMock));
}
Also used : Locale(java.util.Locale) Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) Device(com.microsoft.appcenter.ingestion.models.Device) Mockito.anyString(org.mockito.Mockito.anyString) Point(android.graphics.Point) WindowManager(android.view.WindowManager) TimeZone(java.util.TimeZone) PackageManager(android.content.pm.PackageManager) TelephonyManager(android.telephony.TelephonyManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Build(android.os.Build) WrapperSdk(com.microsoft.appcenter.ingestion.models.WrapperSdk) Display(android.view.Display) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project AppCenter-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(AppCenterLog.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();
    AppCenterLog.error(eq(AppCenter.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.appcenter.ingestion.models.Device) WindowManager(android.view.WindowManager) Display(android.view.Display) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with Device

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

the class ErrorLogHelperAndroidTest method parseDevice.

@Test
public void parseDevice() {
    String contextInfoString = "{\"DEVICE_INFO\":{\"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\"}, \"USER_ID\":\"qwerty12345\"}";
    Device device = ErrorLogHelper.parseDevice(contextInfoString);
    String userId = ErrorLogHelper.parseUserId(contextInfoString);
    assertNotNull(device);
    assertNotNull(userId);
    assertEquals(device.getAppBuild(), "59");
    assertEquals(device.getAppVersion(), "2.5.4.2");
    assertEquals(device.getSdkName(), "appcenter.android");
    /* Test empty string. */
    String contextInfo2 = "";
    Device device2 = ErrorLogHelper.parseDevice(contextInfo2);
    String userId2 = ErrorLogHelper.parseUserId(contextInfo2);
    assertNull(device2);
    assertNull(userId2);
    /* Test malformed string. */
    String contextInfo3 = "abcd";
    Device device3 = ErrorLogHelper.parseDevice(contextInfo3);
    String userId3 = ErrorLogHelper.parseUserId(contextInfo3);
    assertNull(device3);
    assertNull(userId3);
    /* Test parse old device data. */
    String contextInfo4 = "{\"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\"}";
    Device device4 = ErrorLogHelper.parseDevice(contextInfo4);
    String userId4 = ErrorLogHelper.parseUserId(contextInfo4);
    assertNotNull(device4);
    assertNull(userId4);
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Test(org.junit.Test)

Example 35 with Device

use of com.microsoft.appcenter.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("appcenter.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.appcenter.ingestion.models.Device) NonNull(androidx.annotation.NonNull)

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