• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.deviceandprofileowner;
18 
19 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.getDefaultLauncher;
20 
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import android.content.pm.SuspendDialogInfo;
24 import android.util.Log;
25 
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Set;
29 
30 public class SuspendPackageTest extends BaseDeviceAdminTest {
31 
32     private static final String TAG = SuspendPackageTest.class.getSimpleName();
33     private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver";
34 
35     @Override
setUp()36     protected void setUp() throws Exception {
37         super.setUp();
38 
39         Log.d(TAG, "Running test on user " + mContext.getUserId());
40     }
41 
testSetPackagesSuspended()42     public void testSetPackagesSuspended() throws Exception {
43         String[] notHandled = setSuspendedPackages(/* suspend= */ true, INTENT_RECEIVER_PKG);
44         // all packages should be handled.
45         assertWithMessage("packages not suspended").that(notHandled).isEmpty();
46 
47         assertPackageSuspended(INTENT_RECEIVER_PKG);
48     }
49 
testSetPackagesSuspendedWithPackageManager()50     public void testSetPackagesSuspendedWithPackageManager() throws Exception {
51         SuspendDialogInfo dialogInfo = new SuspendDialogInfo.Builder()
52                 .setMessage("Test message")
53                 .build();
54 
55         String[] notHandledPackages =
56                 mContext.getPackageManager().setPackagesSuspended(
57                         new String[] {INTENT_RECEIVER_PKG}, true, null, null, dialogInfo);
58         // all packages should be handled.
59         assertWithMessage("notHandlePackages").that(notHandledPackages).isEmpty();
60 
61         assertPackageSuspended(INTENT_RECEIVER_PKG);
62     }
63 
testSetPackagesNotSuspendedWithPackageManager()64     public void testSetPackagesNotSuspendedWithPackageManager() throws Exception {
65         String[] notHandled = mContext.getPackageManager().setPackagesSuspended(
66                 new String[] {INTENT_RECEIVER_PKG}, false, null, null, (SuspendDialogInfo) null);
67         // all packages should be handled.
68         assertWithMessage("packages not handled").that(notHandled).isEmpty();
69 
70         // test isPackageSuspended
71         assertPackageNotSuspended(INTENT_RECEIVER_PKG);
72     }
73 
testSetPackagesNotSuspended()74     public void testSetPackagesNotSuspended() throws Exception {
75 
76         String[] notHandled = setSuspendedPackages(/* suspend= */ false, INTENT_RECEIVER_PKG);
77         // all packages should be handled.
78         assertWithMessage("packages not suspended").that(notHandled).isEmpty();
79 
80         // test isPackageSuspended
81         assertPackageNotSuspended(INTENT_RECEIVER_PKG);
82     }
83 
84     /**
85      * Verify that we cannot suspend launcher and dpc app.
86      */
testSuspendNotSuspendablePackages()87     public void testSuspendNotSuspendablePackages() throws Exception {
88         String launcherPackage = getDefaultLauncher(getInstrumentation());
89         String dpcPackage = ADMIN_RECEIVER_COMPONENT.getPackageName();
90         String[] notHandledPackages = setSuspendedPackages(/* suspend= */ true,
91                 launcherPackage, dpcPackage);
92         // no package should be handled.
93         assertWithMessage("not handled packages").that(notHandledPackages).asList()
94                 .containsExactly(launcherPackage, dpcPackage);
95 
96         Set<String> exemptApps = mDevicePolicyManager.getPolicyExemptApps();
97         if (exemptApps.isEmpty()) {
98             Log.v(TAG, "testSuspendNotSuspendablePackages(): no exempt apps");
99             return;
100         }
101 
102         Log.v(TAG, "testSuspendNotSuspendablePackages(): testing exempt apps: " + exemptApps);
103         notHandledPackages = setSuspendedPackages(/* suspend= */ true, exemptApps);
104         assertWithMessage("exempt apps not suspended").that(notHandledPackages).asList()
105             .containsExactlyElementsIn(exemptApps);
106     }
107 
setSuspendedPackages(boolean suspend, Collection<String> pkgs)108     private String[] setSuspendedPackages(boolean suspend, Collection<String> pkgs) {
109         String[] pkgsArray = new String[pkgs.size()];
110         pkgs.toArray(pkgsArray);
111         return setSuspendedPackages(suspend, pkgsArray);
112     }
113 
setSuspendedPackages(boolean suspend, String... pkgs)114     private String[] setSuspendedPackages(boolean suspend, String... pkgs) {
115         Log.d(TAG, "Calling setPackagesSuspended(" + suspend + ", " + Arrays.toString(pkgs));
116         String[] notHandled =
117                 mDevicePolicyManager.setPackagesSuspended(ADMIN_RECEIVER_COMPONENT, pkgs, suspend);
118         Log.d(TAG, "Returning " + Arrays.toString(notHandled));
119         return notHandled;
120     }
121 
assertPackageSuspended(String pkg)122     private void assertPackageSuspended(String pkg) throws Exception {
123         assertPackageSuspension(pkg, /* expected= */ true);
124     }
125 
assertPackageNotSuspended(String pkg)126     private void assertPackageNotSuspended(String pkg) throws Exception {
127         assertPackageSuspension(pkg, /* expected= */ false);
128     }
129 
assertPackageSuspension(String pkg, boolean expected)130     private void assertPackageSuspension(String pkg, boolean expected) throws Exception {
131         boolean actual =
132                 mDevicePolicyManager.isPackageSuspended(ADMIN_RECEIVER_COMPONENT, pkg);
133         Log.d(TAG, "isPackageSuspended(" + pkg + "): " + actual);
134         assertWithMessage("package %s suspension", pkg).that(actual).isEqualTo(expected);
135     }
136 }
137