Home | History | Annotate | Download | only in deviceandprofileowner
      1 /*
      2  * Copyright (C) 2015 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 package com.android.cts.deviceandprofileowner;
     17 
     18 import android.app.admin.DevicePolicyManager;
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.net.Uri;
     24 import android.util.Log;
     25 
     26 import java.util.concurrent.Semaphore;
     27 import java.util.concurrent.TimeUnit;
     28 
     29 /**
     30  * Tests for {@link DevicePolicyManager#setApplicationHidden} and
     31  * {@link DevicePolicyManager#isApplicationHidden} APIs.
     32  */
     33 public class ApplicationHiddenTest extends BaseDeviceAdminTest {
     34 
     35     private static final String TAG = "ApplicationHiddenTest";
     36 
     37     private static final String PACKAGE_TO_HIDE = "com.android.cts.permissionapp";
     38     private static final String NONEXISTING_PACKAGE_NAME = "a.b.c.d";
     39 
     40     private static final IntentFilter PACKAGE_INTENT_FILTER;
     41     static {
     42         PACKAGE_INTENT_FILTER = new IntentFilter();
     43         PACKAGE_INTENT_FILTER.addAction(Intent.ACTION_PACKAGE_ADDED);
     44         PACKAGE_INTENT_FILTER.addAction(Intent.ACTION_PACKAGE_REMOVED);
     45         PACKAGE_INTENT_FILTER.addDataScheme("package");
     46     }
     47     private final ApplicationHiddenReceiver mReceiver = new ApplicationHiddenReceiver();
     48 
     49     @Override
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52         mContext.registerReceiver(mReceiver, PACKAGE_INTENT_FILTER);
     53     }
     54 
     55     @Override
     56     protected void tearDown() throws Exception {
     57         mContext.unregisterReceiver(mReceiver);
     58         mDevicePolicyManager.setApplicationHidden(ADMIN_RECEIVER_COMPONENT, PACKAGE_TO_HIDE, false);
     59         super.tearDown();
     60     }
     61 
     62     public void testSetApplicationHidden() throws Exception {
     63         assertTrue(mDevicePolicyManager.setApplicationHidden(ADMIN_RECEIVER_COMPONENT,
     64                 PACKAGE_TO_HIDE, true));
     65         assertTrue(mDevicePolicyManager.isApplicationHidden(ADMIN_RECEIVER_COMPONENT,
     66                 PACKAGE_TO_HIDE));
     67         mReceiver.waitForRemovedBroadcast();
     68         assertTrue(mDevicePolicyManager.setApplicationHidden(ADMIN_RECEIVER_COMPONENT,
     69                 PACKAGE_TO_HIDE, false));
     70         assertFalse(mDevicePolicyManager.isApplicationHidden(ADMIN_RECEIVER_COMPONENT,
     71                 PACKAGE_TO_HIDE));
     72         mReceiver.waitForAddedBroadcast();
     73     }
     74 
     75     public void testCannotHideActiveAdmin() throws Exception {
     76         assertFalse(mDevicePolicyManager.setApplicationHidden(ADMIN_RECEIVER_COMPONENT,
     77                 PACKAGE_NAME, true));
     78     }
     79 
     80     public void testCannotHideNonExistingPackage() throws Exception {
     81         assertFalse(mDevicePolicyManager.setApplicationHidden(ADMIN_RECEIVER_COMPONENT,
     82                 NONEXISTING_PACKAGE_NAME, true));
     83     }
     84 
     85     private class ApplicationHiddenReceiver extends BroadcastReceiver {
     86         private final Semaphore mAddedSemaphore = new Semaphore(0);
     87         private final Semaphore mRemovedSemaphore = new Semaphore(0);
     88 
     89         @Override
     90         public void onReceive(Context context, Intent intent) {
     91             Uri uri = intent.getData();
     92             if (uri == null) {
     93                 return;
     94             }
     95             String pkgName = uri.getSchemeSpecificPart();
     96             if (!PACKAGE_TO_HIDE.equals(pkgName)) {
     97                 return;
     98             }
     99             if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
    100                 Log.d(TAG, "Received PACKAGE_ADDED broadcast");
    101                 mAddedSemaphore.release();
    102             } else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
    103                 Log.d(TAG, "Received PACKAGE_REMOVED broadcast");
    104                 mRemovedSemaphore.release();
    105             }
    106         }
    107 
    108         public void waitForAddedBroadcast() throws Exception {
    109             if (!mAddedSemaphore.tryAcquire(60, TimeUnit.SECONDS)) {
    110                 fail("Did not receive PACKAGE_ADDED broadcast.");
    111             }
    112         }
    113 
    114         public void waitForRemovedBroadcast() throws Exception {
    115             if (!mRemovedSemaphore.tryAcquire(60, TimeUnit.SECONDS)) {
    116                 fail("Did not receive PACKAGE_REMOVED broadcast.");
    117             }
    118         }
    119     }
    120 }
    121