Home | History | Annotate | Download | only in managedprofile
      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 
     17 package com.android.cts.managedprofile;
     18 
     19 import android.app.admin.DevicePolicyManager;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.hardware.camera2.CameraManager;
     23 import android.os.Handler;
     24 import android.os.HandlerThread;
     25 import android.test.AndroidTestCase;
     26 
     27 
     28 public class CameraPolicyTest extends AndroidTestCase {
     29 
     30     protected static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
     31 
     32     private static final String PRIMARY_ADMIN_RECEIVER_TEST_CLASS =
     33             MANAGED_PROFILE_PKG + ".PrimaryUserDeviceAdmin";
     34 
     35     private static final String MANAGED_PROFILE_ADMIN_RECEIVER_TEST_CLASS =
     36             MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
     37 
     38     private DevicePolicyManager mDevicePolicyManager;
     39 
     40     private CameraManager mCameraManager;
     41 
     42     private ComponentName mPrimaryAdminComponent;
     43 
     44     private ComponentName mManagedProfileAdminComponent;
     45 
     46     private HandlerThread mBackgroundThread;
     47 
     48     /**
     49      * A {@link Handler} for running tasks in the background.
     50      */
     51     private Handler mBackgroundHandler;
     52 
     53     @Override
     54     protected void setUp() throws Exception {
     55         super.setUp();
     56         mDevicePolicyManager = (DevicePolicyManager) getContext()
     57                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
     58         mCameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
     59         mPrimaryAdminComponent = new ComponentName(MANAGED_PROFILE_PKG,
     60                 PRIMARY_ADMIN_RECEIVER_TEST_CLASS);
     61         mManagedProfileAdminComponent = new ComponentName(MANAGED_PROFILE_PKG,
     62                 MANAGED_PROFILE_ADMIN_RECEIVER_TEST_CLASS);
     63         startBackgroundThread();
     64     }
     65 
     66     @Override
     67     protected void tearDown() throws Exception {
     68         stopBackgroundThread();
     69         super.tearDown();
     70     }
     71 
     72     public void testDisableCameraInManagedProfile() throws Exception {
     73         mDevicePolicyManager.setCameraDisabled(mManagedProfileAdminComponent, true);
     74         assertTrue(mDevicePolicyManager.getCameraDisabled(mManagedProfileAdminComponent));
     75         assertTrue(mDevicePolicyManager.getCameraDisabled(null));
     76         checkCanOpenCamera(false);
     77     }
     78 
     79     public void testEnableCameraInManagedProfile() throws Exception {
     80         mDevicePolicyManager.setCameraDisabled(mManagedProfileAdminComponent, false);
     81         assertFalse(mDevicePolicyManager.getCameraDisabled(mManagedProfileAdminComponent));
     82         assertFalse(mDevicePolicyManager.getCameraDisabled(null));
     83         checkCanOpenCamera(true);
     84     }
     85 
     86     public void testDisableCameraInPrimaryProfile() throws Exception {
     87         mDevicePolicyManager.setCameraDisabled(mPrimaryAdminComponent, true);
     88         assertTrue(mDevicePolicyManager.getCameraDisabled(mPrimaryAdminComponent));
     89         assertTrue(mDevicePolicyManager.getCameraDisabled(null));
     90         checkCanOpenCamera(false);
     91     }
     92 
     93     public void testEnableCameraInPrimaryProfile() throws Exception {
     94         mDevicePolicyManager.setCameraDisabled(mPrimaryAdminComponent, false);
     95         assertFalse(mDevicePolicyManager.getCameraDisabled(mPrimaryAdminComponent));
     96         assertFalse(mDevicePolicyManager.getCameraDisabled(null));
     97         checkCanOpenCamera(true);
     98     }
     99 
    100     public void testIsCameraEnabledInPrimaryProfile() throws Exception {
    101         assertFalse(mDevicePolicyManager.getCameraDisabled(mPrimaryAdminComponent));
    102         assertFalse(mDevicePolicyManager.getCameraDisabled(null));
    103         checkCanOpenCamera(true);
    104     }
    105 
    106     public void testIsCameraEnabledInManagedProfile() throws Exception {
    107         assertFalse(mDevicePolicyManager.getCameraDisabled(mManagedProfileAdminComponent));
    108         assertFalse(mDevicePolicyManager.getCameraDisabled(null));
    109         checkCanOpenCamera(true);
    110     }
    111 
    112     /**
    113      * Beginning with Android 7.0, the camera restriction policy isn't kept in the
    114      * system property("sys.secpolicy.camera.off_<userId>") anymore, CameraService
    115      * now checks if camera is disabled via AppOpsService.
    116      *
    117      * The propagation of camera retriction policy from DevicePolicyManagerService
    118      * to UserManagerService and then finally to AppOpsService is NOT synchronous,
    119      * so here #blockUntilOpenCamera is called many times until policy is enforced
    120      * or timed out.
    121      *
    122      * @see android.app.AppOpsManager#checkOp(String, int, String)
    123      * @see android.app.AppOpsManager.OnOpChangedListener
    124      */
    125     private void checkCanOpenCamera(boolean canOpen) throws Exception {
    126         int retries = 10;
    127         boolean successToOpen = !canOpen;
    128         while (successToOpen != canOpen && retries > 0) {
    129             retries--;
    130             Thread.sleep(500);
    131             successToOpen = CameraUtils
    132                     .blockUntilOpenCamera(mCameraManager, mBackgroundHandler);
    133         }
    134         assertEquals(String.format("Timed out waiting the value to change to %b (actual=%b)",
    135                     canOpen, successToOpen), canOpen, successToOpen);
    136     }
    137 
    138     /**
    139      * Starts a background thread and its {@link Handler}.
    140      */
    141     private void startBackgroundThread() {
    142         mBackgroundThread = new HandlerThread("CameraBackground");
    143         mBackgroundThread.start();
    144         mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    145     }
    146 
    147     /**
    148      * Stops the background thread and its {@link Handler}.
    149      */
    150     private void stopBackgroundThread() {
    151         mBackgroundThread.quitSafely();
    152         try {
    153             mBackgroundThread.join();
    154             mBackgroundThread = null;
    155             mBackgroundHandler = null;
    156         } catch (InterruptedException e) {
    157             e.printStackTrace();
    158         }
    159     }
    160 }
    161