@Test(expected = DeviceInfoHelper.DeviceInfoException.class) public void getDeviceInfoWithException() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException { /* 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(), eq(0))).thenThrow(new PackageManager.NameNotFoundException()); DeviceInfoHelper.getDeviceInfo(contextMock); }
@NonNull static String computeReleaseHash(@NonNull PackageInfo packageInfo) { return HashUtils.sha256(packageInfo.packageName + ":" + packageInfo.versionName + ":" + DeviceInfoHelper.getVersionCode(packageInfo)); }
/** * {@link #setWrapperSdk(WrapperSdk)} implementation at instance level. * * @param wrapperSdk wrapper SDK information. */ private synchronized void setInstanceWrapperSdk(WrapperSdk wrapperSdk) { DeviceInfoHelper.setWrapperSdk(wrapperSdk); /* If SDK already configured, reset device info cache. */ if (mHandler != null) { /* Every channel operation must be in background since it uses locks and accesses disks. */ mHandler.post(new Runnable() { @Override public void run() { mChannel.invalidateDeviceCache(); } }); } }
Device device = DeviceInfoHelper.getDeviceInfo(contextMock); device = DeviceInfoHelper.getDeviceInfo(contextMock); assertEquals(screenSizePortrait, device.getScreenSize()); device = DeviceInfoHelper.getDeviceInfo(contextMock); assertEquals(screenSizeLandscape, device.getScreenSize()); device = DeviceInfoHelper.getDeviceInfo(contextMock); assertEquals(screenSizePortrait, device.getScreenSize()); wrapperSdk.setLiveUpdateDeploymentKey("staging"); wrapperSdk.setLiveUpdatePackageHash("aa896f791b26a7f464c0f62b0ba69f2b"); DeviceInfoHelper.setWrapperSdk(wrapperSdk); Device device2 = DeviceInfoHelper.getDeviceInfo(contextMock); assertEquals(wrapperSdk.getWrapperSdkVersion(), device2.getWrapperSdkVersion()); assertEquals(wrapperSdk.getWrapperSdkName(), device2.getWrapperSdkName()); DeviceInfoHelper.setWrapperSdk(null); assertEquals(device, DeviceInfoHelper.getDeviceInfo(contextMock));
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); device.setScreenSize(getScreenSize(context)); } catch (Exception e) { AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve screen size", e);
@Test public void deviceInfoHelper() { new DeviceInfoHelper(); }
@Test public void packageManagerIsBroken() throws Persistence.PersistenceException, DeviceInfoHelper.DeviceInfoException { /* Setup mocking to make device properties generation fail. */ when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException())); Persistence persistence = mock(Persistence.class); @SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(AppCenterIngestion.class), mAppCenterHandler); channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null); Channel.Listener listener = mock(Channel.Listener.class); channel.addListener(listener); /* Enqueue a log: listener is called before but then attaching device properties fails before saving the log. */ Log log = mock(Log.class); channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS); verify(listener).onPreparingLog(log, TEST_GROUP); verify(listener, never()).shouldFilter(log); verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt()); }
@Before @After public void cleanWrapperSdk() { DeviceInfoHelper.setWrapperSdk(null); }
/** * Check if the fetched release information should be installed. * * @param releaseDetails latest release on server. * @return true if latest release on server should be used. */ private boolean isMoreRecent(ReleaseDetails releaseDetails) { boolean moreRecent; int versionCode = DeviceInfoHelper.getVersionCode(mPackageInfo); if (releaseDetails.getVersion() == versionCode) { moreRecent = !releaseDetails.getReleaseHash().equals(DistributeUtils.computeReleaseHash(mPackageInfo)); } else { moreRecent = releaseDetails.getVersion() > versionCode; } AppCenterLog.debug(LOG_TAG, "Latest release more recent=" + moreRecent); return moreRecent; }
errorLog.setDevice(DeviceInfoHelper.getDeviceInfo(context)); } catch (DeviceInfoHelper.DeviceInfoException e) { AppCenterLog.error(Crashes.LOG_TAG, "Could not attach device properties snapshot to error log, will attach at sending time", e);
@Test public void setWrapperSdkTest() { /* Call method. */ WrapperSdk wrapperSdk = new WrapperSdk(); AppCenter.setWrapperSdk(wrapperSdk); /* Check propagation. */ verifyStatic(); DeviceInfoHelper.setWrapperSdk(wrapperSdk); /* Since the channel was not created when setting wrapper, no need to refresh channel after start. */ AppCenter.start(mApplication, DUMMY_APP_SECRET, DummyService.class); verify(mChannel, never()).invalidateDeviceCache(); /* Update wrapper SDK and check channel refreshed. */ wrapperSdk = new WrapperSdk(); AppCenter.setWrapperSdk(wrapperSdk); verify(mChannel).invalidateDeviceCache(); }
@Test public void getDeviceInfoMissingScreenSize() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException { /* Mocking instances. */ Context contextMock = mock(Context.class); PackageManager packageManagerMock = mock(PackageManager.class); mockStatic(AppCenterLog.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(); AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(Exception.class)); } }
mockStatic(DeviceInfoHelper.class); Device device = mock(Device.class); when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(device); mockStatic(IdHelper.class); Channel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mock(Persistence.class), mock(Ingestion.class), mock(Handler.class)); DeviceInfoHelper.getDeviceInfo(any(Context.class)); when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(device2); channel.invalidateDeviceCache(); DeviceInfoHelper.getDeviceInfo(any(Context.class));
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class)); ErrorReport report = new ErrorReport(); mockStatic(ErrorLogHelper.class);
@Before public void setUp() throws Exception { mockStatic(AppCenterLog.class); mockStatic(IdHelper.class, new Returns(UUIDUtils.randomUUID())); mockStatic(DeviceInfoHelper.class); when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class)); when(mAppCenterHandler.post(any(Runnable.class))).then(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) { ((Runnable) invocation.getArguments()[0]).run(); return true; } }); mockStatic(HandlerUtils.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) { ((Runnable) invocation.getArguments()[0]).run(); return null; } }).when(HandlerUtils.class); HandlerUtils.runOnUiThread(any(Runnable.class)); } }
@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)); }
@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()); }
mDevice = DeviceInfoHelper.getDeviceInfo(mContext); } catch (DeviceInfoHelper.DeviceInfoException e) { AppCenterLog.error(LOG_TAG, "Device log cannot be generated", e);
errorLog.setDevice(DeviceInfoHelper.getDeviceInfo(mContext)); errorLog.getDevice().setWrapperSdkName(Constants.WRAPPER_SDK_NAME_NDK); saveErrorLogFiles(nativeException, errorLog);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);