DeviceInfoHelper { public static synchronized Device getDeviceInfo(Context context) throws DeviceInfoException { Device device = new Device(); PackageInfo packageInfo; try { PackageManager packageManager = context.getPackageManager(); packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); device.setAppVersion(packageInfo.versionName); device.setAppBuild(String.valueOf(getVersionCode(packageInfo))); } catch (Exception e) { AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve package info", e); throw new DeviceInfoException("Cannot retrieve package info", e); } device.setAppNamespace(context.getPackageName()); try { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String networkCountryIso = telephonyManager.getNetworkCountryIso(); if (!TextUtils.isEmpty(networkCountryIso)) { device.setCarrierCountry(networkCountryIso); } String networkOperatorName = telephonyManager.getNetworkOperatorName(); if (!TextUtils.isEmpty(networkOperatorName)) { device.setCarrierName(networkOperatorName); } } catch (Exception e) { AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve carrier info", e); } device.setLocale(Locale.getDefault().toString()); device.setModel(Build.MODEL); device.setOemName(Build.MANUFACTURER); device.setOsApiLevel(Build.VERSION.SDK_INT); device.setOsName(OS_NAME); device.setOsVersion(Build.VERSION.RELEASE); device.setOsBuild(Build.ID); try { device.setScreenSize(getScreenSize(context)); } catch (Exception e) { AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve screen size", e); } device.setSdkName(com.microsoft.appcenter.BuildConfig.SDK_NAME); device.setSdkVersion(com.microsoft.appcenter.BuildConfig.VERSION_NAME); device.setTimeZoneOffset(TimeZone.getDefault().getOffset(System.currentTimeMillis()) / 60 / 1000); if (sWrapperSdk != null) { device.setWrapperSdkVersion(sWrapperSdk.getWrapperSdkVersion()); device.setWrapperSdkName(sWrapperSdk.getWrapperSdkName()); device.setWrapperRuntimeVersion(sWrapperSdk.getWrapperRuntimeVersion()); device.setLiveUpdateReleaseLabel(sWrapperSdk.getLiveUpdateReleaseLabel()); device.setLiveUpdateDeploymentKey(sWrapperSdk.getLiveUpdateDeploymentKey()); device.setLiveUpdatePackageHash(sWrapperSdk.getLiveUpdatePackageHash()); } return device; }  static synchronized Device getDeviceInfo(Context context); @SuppressWarnings({"deprecation", "RedundantSuppression"}) static int getVersionCode(PackageInfo packageInfo); static synchronized void setWrapperSdk(WrapperSdk wrapperSdk);  }
@Test public void getDeviceInfoApi17() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException { when(mContext.getSystemService(eq(Context.DISPLAY_SERVICE))).thenReturn(mDisplayManager); when(mDisplayManager.getDisplay(anyInt())).thenReturn(mDisplay); when(mContext.getResources()).thenReturn(mResources); when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics); mDisplayMetrics.widthPixels = SCREEN_WIDTH; mDisplayMetrics.heightPixels = SCREEN_HEIGHT; getDeviceInfo(17); verify(mContext, atLeastOnce()).getSystemService(eq(Context.DISPLAY_SERVICE)); verify(mContext, atLeastOnce()).getResources(); verify(mResources, atLeastOnce()).getDisplayMetrics(); verify(mDisplay, never()).getSize(any(Point.class)); verify(mContext, never()).getSystemService(eq(Context.WINDOW_SERVICE)); verify(mWindowManager, never()).getDefaultDisplay(); }
@Test public void getDeviceInfoApi16() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException { when(mContext.getSystemService(eq(Context.WINDOW_SERVICE))).thenReturn(mWindowManager); when(mWindowManager.getDefaultDisplay()).thenReturn(mDisplay); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) { Object[] args = invocationOnMock.getArguments(); ((Point) args[0]).x = SCREEN_WIDTH; ((Point) args[0]).y = SCREEN_HEIGHT; return null; } }).when(mDisplay).getSize(any(Point.class)); getDeviceInfo(16); verify(mContext, never()).getSystemService(eq(Context.DISPLAY_SERVICE)); verify(mContext, never()).getResources(); verify(mResources, never()).getDisplayMetrics(); verify(mDisplay, atLeastOnce()).getSize(any(Point.class)); verify(mContext, atLeastOnce()).getSystemService(eq(Context.WINDOW_SERVICE)); verify(mWindowManager, atLeastOnce()).getDefaultDisplay(); }
@Test(expected = DeviceInfoHelper.DeviceInfoException.class) public void getDeviceInfoWithException() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException { when(mPackageManager.getPackageInfo(anyString(), eq(0))).thenThrow(new PackageManager.NameNotFoundException()); DeviceInfoHelper.getDeviceInfo(mContext); }
@Test public void getDeviceInfoMissingCarrierInfo() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException { mockStatic(AppCenterLog.class); when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenThrow(new RuntimeException()); when(mContext.getSystemService(Context.WINDOW_SERVICE)).thenReturn(mWindowManager); when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo); when(mWindowManager.getDefaultDisplay()).thenReturn(mDisplay); Device device = DeviceInfoHelper.getDeviceInfo(mContext); assertNull(device.getCarrierCountry()); assertNull(device.getCarrierName()); verifyStatic(); AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(Exception.class)); }
@Test public void getDeviceInfoEmptyCarrierInfo() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException { when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo); when(mTelephonyManager.getNetworkCountryIso()).thenReturn(""); when(mTelephonyManager.getNetworkOperatorName()).thenReturn(""); when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); mockStatic(TextUtils.class); when(TextUtils.isEmpty(anyString())).thenReturn(true); Device device = DeviceInfoHelper.getDeviceInfo(mContext); assertNull(device.getCarrierCountry()); assertNull(device.getCarrierName()); }