Search in sources :

Example 41 with Device

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

the class ErrorLogHelperTest method getStoredDeviceInfoAndUserInfoCannotRead.

@Test
public void getStoredDeviceInfoAndUserInfoCannotRead() throws IOException {
    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(null);
    Device storedDeviceInfo = ErrorLogHelper.getStoredDeviceInfo(minidumpFolder);
    assertNull(storedDeviceInfo);
    String userInfo = ErrorLogHelper.getStoredUserInfo(minidumpFolder);
    assertNull(userInfo);
}
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)

Example 42 with Device

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

the class ChannelLogDecorateTest method checkLogAttributes.

@Test
public void checkLogAttributes() throws Exception {
    mockStatic(DeviceInfoHelper.class);
    Device device = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(device);
    mockStatic(IdHelper.class);
    String mockToken = UUID.randomUUID().toString();
    Channel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mock(Persistence.class), mock(Ingestion.class), mock(Handler.class));
    channel.addGroup("", 0, 0, 0, null, null);
    /* Test a log that should be decorated. */
    for (int i = 0; i < 3; i++) {
        Log log = mock(Log.class);
        channel.enqueue(log, "", DEFAULTS);
        verify(log).setDevice(device);
        verify(log).setTimestamp(any(Date.class));
    }
    /* Check cache was used, meaning only 1 call to generate a device. */
    verifyStatic();
    DeviceInfoHelper.getDeviceInfo(any(Context.class));
    /* Test a log that is already decorated. */
    Log log2 = mock(Log.class);
    when(log2.getDevice()).thenReturn(device);
    when(log2.getTimestamp()).thenReturn(new Date(123L));
    channel.enqueue(log2, "", DEFAULTS);
    verify(log2, never()).setDevice(any(Device.class));
    verify(log2, never()).setTimestamp(any(Date.class));
    /* Simulate update to wrapper SDK. */
    Device device2 = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(device2);
    channel.invalidateDeviceCache();
    /* Generate some logs to verify device properties have been updated. */
    for (int i = 0; i < 3; i++) {
        Log log3 = mock(Log.class);
        channel.enqueue(log3, "", DEFAULTS);
        verify(log3).setDevice(device2);
        verify(log3).setTimestamp(any(Date.class));
    }
    /* Check only 1 device has been generated after cache invalidate. */
    verifyStatic(times(2));
    DeviceInfoHelper.getDeviceInfo(any(Context.class));
}
Also used : Context(android.content.Context) Persistence(com.microsoft.appcenter.persistence.Persistence) Log(com.microsoft.appcenter.ingestion.models.Log) Device(com.microsoft.appcenter.ingestion.models.Device) Handler(android.os.Handler) Date(java.util.Date) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 43 with Device

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

the class DeviceInfoHelperTest method verifyCountryCodeWithInvalidLength.

public void verifyCountryCodeWithInvalidLength(String countryCode) throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mock system calls. */
    String expectedCountryCode = "aa";
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo);
    when(mTelephonyManager.getNetworkCountryIso()).thenReturn(expectedCountryCode);
    when(mTelephonyManager.getNetworkOperatorName()).thenReturn(expectedCountryCode);
    when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
    /* Mocking instances. */
    mockStatic(AppCenterLog.class);
    /* Set invalid country code. */
    DeviceInfoHelper.setCountryCode(countryCode);
    /* Verify that log was called.*/
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), eq("App Center accepts only the two-letter ISO country code."));
    /* Verify that invalid value wasn't set. */
    Device device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertEquals(device.getCarrierCountry(), expectedCountryCode);
    verify(mTelephonyManager).getNetworkCountryIso();
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Mockito.anyString(org.mockito.Mockito.anyString)

Example 44 with Device

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

the class DeviceInfoHelperTest method deviceInfo.

@Test
public void deviceInfo() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException {
    /* Mock system calls. */
    // noinspection WrongConstant
    when(mContext.getSystemService(eq(Context.DISPLAY_SERVICE))).thenReturn(mDisplayManager);
    when(mDisplayManager.getDisplay(anyInt())).thenReturn(mDisplay);
    // noinspection WrongConstant
    when(mContext.getResources()).thenReturn(mResources);
    // noinspection WrongConstant
    when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics);
    mDisplayMetrics.widthPixels = SCREEN_WIDTH;
    mDisplayMetrics.heightPixels = SCREEN_HEIGHT;
    /* Mock data. */
    final Integer osApiLevel = 21;
    final String appVersion = "1.0";
    final String appBuild = "1";
    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 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);
    /* Delegates to mock instances. */
    when(mContext.getPackageName()).thenReturn(appNamespace);
    // noinspection WrongConstant
    when(mContext.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(mTelephonyManager);
    // noinspection WrongConstant
    when(mPackageManager.getPackageInfo(anyString(), eq(0))).thenReturn(mPackageInfo);
    when(mTelephonyManager.getNetworkCountryIso()).thenReturn(carrierCountry);
    when(mTelephonyManager.getNetworkOperatorName()).thenReturn(carrierName);
    when(mDisplay.getRotation()).thenReturn(Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, Surface.ROTATION_270);
    /* Sets values of fields for static classes. */
    Whitebox.setInternalState(mPackageInfo, "versionName", appVersion);
    Whitebox.setInternalState(mPackageInfo, "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(mContext);
    /* 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(mContext);
    assertEquals(screenSizePortrait, device.getScreenSize());
    /* Verify screen size based on different orientations (Surface.ROTATION_180). */
    device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertEquals(screenSizeLandscape, device.getScreenSize());
    /* Verify screen size based on different orientations (Surface.ROTATION_270). */
    device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertEquals(screenSizePortrait, device.getScreenSize());
    /* Make sure screen size is verified for all orientations. */
    verify(mDisplay, 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(mContext);
    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(mContext));
    /* Verify the right API was called to get a screen size. */
    verify(mContext, atLeastOnce()).getSystemService(eq(Context.DISPLAY_SERVICE));
    verify(mContext, atLeastOnce()).getResources();
    verify(mResources, atLeastOnce()).getDisplayMetrics();
    // noinspection deprecation
    verify(mDisplay, never()).getSize(any(Point.class));
    verify(mContext, never()).getSystemService(eq(Context.WINDOW_SERVICE));
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) Build(android.os.Build) Device(com.microsoft.appcenter.ingestion.models.Device) WrapperSdk(com.microsoft.appcenter.ingestion.models.WrapperSdk) Mockito.anyString(org.mockito.Mockito.anyString) Point(android.graphics.Point) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 45 with Device

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

the class DeviceInfoHelperTest method setCountryCode.

@Test
public void setCountryCode() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mock system calls. */
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo);
    when(mTelephonyManager.getNetworkCountryIso()).thenReturn("");
    when(mTelephonyManager.getNetworkOperatorName()).thenReturn("");
    when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
    /* Set invalid country code. */
    String expectedCountryCode = "aa";
    DeviceInfoHelper.setCountryCode(expectedCountryCode);
    /* Get device info. */
    Device device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertEquals(device.getCarrierCountry(), expectedCountryCode);
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Mockito.anyString(org.mockito.Mockito.anyString) 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