/** * Adds a package to the {@link PackageManager}, but doesn't set any default values on it. * * Right now it will not set {@link ApplicationInfo#FLAG_INSTALLED} flag on its application, so * if not set explicitly, it will be treated as not installed. */ public void addPackageNoDefaults(PackageInfo packageInfo) { PackageStats packageStats = new PackageStats(packageInfo.packageName); addPackage(packageInfo, packageStats); }
@Test public void addPackageMultipleTimesShouldWork() throws Exception { shadowPackageManager.addPackage("test.package"); // Shouldn't throw exception shadowPackageManager.addPackage("test.package"); }
@Test public void addPackageSetsStorage() throws Exception { shadowPackageManager.addPackage("test.package"); PackageInfo packageInfo = packageManager.getPackageInfo("test.package", 0); assertThat(packageInfo.applicationInfo.sourceDir).isNotNull(); assertThat(new File(packageInfo.applicationInfo.sourceDir).exists()).isTrue(); assertThat(packageInfo.applicationInfo.publicSourceDir) .isEqualTo(packageInfo.applicationInfo.sourceDir); }
@Test @Config(minSdk = N, maxSdk = N_MR1) // Functionality removed in O public void whenPreconfigured_getPackageSizeInfo_callsBackWithConfiguredValues() throws Exception { PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = "org.robolectric"; PackageStats packageStats = new PackageStats("org.robolectric"); shadowPackageManager.addPackage(packageInfo, packageStats); IPackageStatsObserver packageStatsObserver = mock(IPackageStatsObserver.class); packageManager.getPackageSizeInfo("org.robolectric", packageStatsObserver); verify(packageStatsObserver).onGetStatsCompleted(packageStatsCaptor.capture(), eq(true)); assertThat(packageStatsCaptor.getValue().packageName).isEqualTo("org.robolectric"); assertThat(packageStatsCaptor.getValue().toString()).isEqualTo(packageStats.toString()); }
@Test @Config(minSdk = N, maxSdk = N_MR1) // Functionality removed in O public void whenPreconfiguredForAnotherPackage_getPackageSizeInfo_callsBackWithConfiguredValues() throws Exception { PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = "org.other"; PackageStats packageStats = new PackageStats("org.other"); shadowPackageManager.addPackage(packageInfo, packageStats); IPackageStatsObserver packageStatsObserver = mock(IPackageStatsObserver.class); packageManager.getPackageSizeInfo("org.other", packageStatsObserver); verify(packageStatsObserver).onGetStatsCompleted(packageStatsCaptor.capture(), eq(true)); assertThat(packageStatsCaptor.getValue().packageName).isEqualTo("org.other"); assertThat(packageStatsCaptor.getValue().toString()).isEqualTo(packageStats.toString()); }
@Test @Config(minSdk = N) public void isPackagesSuspended_defaultsFalse() throws Exception { shadowOf(devicePolicyManager).setProfileOwner(testComponent); shadowOf(packageManager).addPackage("package"); assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isFalse(); }
@Test @Config(minSdk = LOLLIPOP) public void isApplicationHiddenShouldReturnFalseForAppsByDefault() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it's never be set hidden or non hidden String app = "com.example.non.hidden"; shadowOf(packageManager).addPackage(app); // WHEN DevicePolicyManager#isApplicationHidden is called on the app // THEN it should return false assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isFalse(); }
@Test @Config(minSdk = N) public void setPackagesSuspended_suspendsPossible() throws Exception { shadowOf(devicePolicyManager).setProfileOwner(testComponent); shadowOf(packageManager).addPackage("installed"); String[] packages = new String[] {"installed", "not.installed"}; assertThat(devicePolicyManager.setPackagesSuspended(testComponent, packages, true)) .isEqualTo(new String[] {"not.installed"}); }
@Test @Config(minSdk = N) public void setPackagesSuspended_activateActive() throws Exception { shadowOf(devicePolicyManager).setProfileOwner(testComponent); shadowOf(packageManager).addPackage("package"); assertThat( devicePolicyManager.setPackagesSuspended( testComponent, new String[] {"package"}, false)) .isEmpty(); assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isFalse(); }
@Test public void packageManager() throws Exception { PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = "test.package"; shadowOf(RuntimeEnvironment.application.getPackageManager()).addPackage(packageInfo); } }
@Test @Config(minSdk = N) public void isPackagesSuspended_trueForSuspended() throws Exception { shadowOf(devicePolicyManager).setProfileOwner(testComponent); shadowOf(packageManager).addPackage("package"); devicePolicyManager.setPackagesSuspended(testComponent, new String[] {"package"}, true); assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isTrue(); }
@Test @Config(minSdk = LOLLIPOP) public void wasPackageEverHiddenShouldReturnFalseForPackageNeverHidden() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it's never be set hidden or non hidden String app = "com.example.non.hidden"; shadowOf(packageManager).addPackage(app); // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app // THEN it should return false assertThat(shadowOf(devicePolicyManager).wasPackageEverHidden(app)).isFalse(); }
@Test @Config(minSdk = LOLLIPOP) public void isApplicationHiddenShouldReturnFalseForNonHiddenApps() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it is not hidden String nonHiddenApp = "com.example.non.hidden"; shadowOf(packageManager).addPackage(nonHiddenApp); devicePolicyManager.setApplicationHidden(testComponent, nonHiddenApp, false); // WHEN DevicePolicyManager#isApplicationHidden is called on the app // THEN it should return false assertThat(devicePolicyManager.isApplicationHidden(testComponent, nonHiddenApp)).isFalse(); }
@Test @Config(minSdk = LOLLIPOP) public void isApplicationHiddenShouldReturnTrueForHiddenApps() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it is hidden String hiddenApp = "com.example.hidden"; shadowOf(packageManager).addPackage(hiddenApp); devicePolicyManager.setApplicationHidden(testComponent, hiddenApp, true); // WHEN DevicePolicyManager#isApplicationHidden is called on the app // THEN it should return true assertThat(devicePolicyManager.isApplicationHidden(testComponent, hiddenApp)).isTrue(); }
@Test @Config(minSdk = N) public void setPackagesSuspended_cycleSuspension() throws Exception { shadowOf(devicePolicyManager).setProfileOwner(testComponent); shadowOf(packageManager).addPackage("package"); devicePolicyManager.setPackagesSuspended(testComponent, new String[] {"package"}, true); devicePolicyManager.setPackagesSuspended(testComponent, new String[] {"package"}, false); assertThat(devicePolicyManager.isPackageSuspended(testComponent, "package")).isFalse(); }
@Test @Config(minSdk = LOLLIPOP) public void setApplicationHiddenShouldBeAbleToUnhideHiddenApps() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it is hidden String app = "com.example.hidden"; shadowOf(packageManager).addPackage(app); devicePolicyManager.setApplicationHidden(testComponent, app, true); // WHEN DevicePolicyManager#setApplicationHidden is called on the app to unhide it devicePolicyManager.setApplicationHidden(testComponent, app, false); // THEN the app shouldn't be hidden anymore assertThat(devicePolicyManager.isApplicationHidden(testComponent, app)).isFalse(); }
@Test @Config(minSdk = LOLLIPOP) public void wasPackageEverHiddenShouldReturnTrueForPackageWhichIsHidden() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it's hidden String hiddenApp = "com.example.hidden"; shadowOf(packageManager).addPackage(hiddenApp); devicePolicyManager.setApplicationHidden(testComponent, hiddenApp, true); // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app // THEN it should return true assertThat(shadowOf(devicePolicyManager).wasPackageEverHidden(hiddenApp)).isTrue(); }
@Test @Config(minSdk = LOLLIPOP) public void wasPackageEverHiddenShouldReturnTrueForPackageWhichWasHidden() { // GIVEN the caller is the device owner, and thus an active admin shadowOf(devicePolicyManager).setDeviceOwner(testComponent); // GIVEN an app and it was hidden String app = "com.example.hidden"; shadowOf(packageManager).addPackage(app); devicePolicyManager.setApplicationHidden(testComponent, app, true); devicePolicyManager.setApplicationHidden(testComponent, app, false); // WHEN ShadowDevicePolicyManager#wasPackageEverHidden is called with the app // THEN it should return true assertThat(shadowOf(devicePolicyManager).wasPackageEverHidden(app)).isTrue(); }
/** * Adds a package to the {@link PackageManager}, but doesn't set any default values on it. * * <p>Right now it will not set {@link ApplicationInfo#FLAG_INSTALLED} flag on its application, so * if not set explicitly, it will be treated as not installed. */ public void addPackageNoDefaults(PackageInfo packageInfo) { PackageStats packageStats = new PackageStats(packageInfo.packageName); addPackage(packageInfo, packageStats); }