Home | History | Annotate | Download | only in deviceandprofileowner
      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 android.content.Intent;
     20 import android.content.pm.PackageManager;
     21 import android.content.pm.PackageManager.NameNotFoundException;
     22 import android.content.pm.ResolveInfo;
     23 
     24 import java.util.Arrays;
     25 import java.util.HashSet;
     26 
     27 public class SuspendPackageTest extends BaseDeviceAdminTest {
     28     private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver";
     29 
     30     public void testSetPackagesSuspended() throws NameNotFoundException {
     31         String[] notHandledPackages =
     32                 mDevicePolicyManager.setPackagesSuspended(ADMIN_RECEIVER_COMPONENT, new String[]
     33                         {INTENT_RECEIVER_PKG}, true);
     34         // all packages should be handled.
     35         assertEquals(0, notHandledPackages.length);
     36         // test isPackageSuspended
     37         boolean isSuspended =
     38                 mDevicePolicyManager.isPackageSuspended(
     39                         ADMIN_RECEIVER_COMPONENT, INTENT_RECEIVER_PKG);
     40         assertTrue(isSuspended);
     41     }
     42 
     43     public void testSetPackagesNotSuspended() throws NameNotFoundException {
     44         String[] notHandledPackages = mDevicePolicyManager.setPackagesSuspended(
     45                 ADMIN_RECEIVER_COMPONENT,
     46                 new String[] {INTENT_RECEIVER_PKG},
     47                 false);
     48         // all packages should be handled.
     49         assertEquals(0, notHandledPackages.length);
     50         // test isPackageSuspended
     51         boolean isSuspended =
     52                 mDevicePolicyManager.isPackageSuspended(
     53                         ADMIN_RECEIVER_COMPONENT, INTENT_RECEIVER_PKG);
     54         assertFalse(isSuspended);
     55     }
     56 
     57     /**
     58      * Verify that we cannot suspend launcher and dpc app.
     59      */
     60     public void testSuspendNotSuspendablePackages() {
     61         String launcherPackage = getLauncherPackage();
     62         String dpcPackage = ADMIN_RECEIVER_COMPONENT.getPackageName();
     63         String[] unsuspendablePackages = new String[] {launcherPackage, dpcPackage};
     64         String[] notHandledPackages = mDevicePolicyManager.setPackagesSuspended(
     65                 ADMIN_RECEIVER_COMPONENT,
     66                 unsuspendablePackages,
     67                 true);
     68         // no package should be handled.
     69         assertArrayEqualIgnoreOrder(unsuspendablePackages, notHandledPackages);
     70     }
     71 
     72     /**
     73      * @return the package name of launcher.
     74      */
     75     private String getLauncherPackage() {
     76         Intent intent = new Intent(Intent.ACTION_MAIN);
     77         intent.addCategory(Intent.CATEGORY_HOME);
     78         ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(intent,
     79                 PackageManager.MATCH_DEFAULT_ONLY);
     80         return resolveInfo.activityInfo.packageName;
     81     }
     82 
     83     private static <T> void assertArrayEqualIgnoreOrder(T[] a, T[] b) {
     84         assertEquals(a.length, b.length);
     85         assertTrue(new HashSet(Arrays.asList(a)).containsAll(new HashSet(Arrays.asList(b))));
     86     }
     87 
     88 }
     89