1 /* 2 * Copyright (C) 2014 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.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 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#setScreenCaptureDisabled} and 31 * {@link DevicePolicyManager#getScreenCaptureDisabled} APIs. 32 */ 33 public class ScreenCaptureDisabledTest extends BaseDeviceAdminTest { 34 35 private static final String TAG = "ScreenCaptureDisabledTest"; 36 37 private ScreenCaptureBroadcastReceiver mReceiver = new ScreenCaptureBroadcastReceiver(); 38 39 protected void setUp() throws Exception { 40 super.setUp(); 41 mContext.registerReceiver(mReceiver, new IntentFilter( 42 ScreenCaptureDisabledActivity.ACTIVITY_RESUMED)); 43 } 44 45 protected void tearDown() throws Exception { 46 mContext.unregisterReceiver(mReceiver); 47 super.tearDown(); 48 } 49 50 public void testSetScreenCaptureDisabled_false() throws Exception { 51 mDevicePolicyManager.setScreenCaptureDisabled(ADMIN_RECEIVER_COMPONENT, false); 52 assertFalse(mDevicePolicyManager.getScreenCaptureDisabled(ADMIN_RECEIVER_COMPONENT)); 53 assertFalse(mDevicePolicyManager.getScreenCaptureDisabled(null /* any admin */)); 54 startTestActivity(); 55 assertNotNull(getInstrumentation().getUiAutomation().takeScreenshot()); 56 } 57 58 public void testSetScreenCaptureDisabled_true() throws Exception { 59 mDevicePolicyManager.setScreenCaptureDisabled(ADMIN_RECEIVER_COMPONENT, true); 60 assertTrue(mDevicePolicyManager.getScreenCaptureDisabled(ADMIN_RECEIVER_COMPONENT)); 61 assertTrue(mDevicePolicyManager.getScreenCaptureDisabled(null /* any admin */)); 62 startTestActivity(); 63 assertNull(getInstrumentation().getUiAutomation().takeScreenshot()); 64 } 65 66 public void testScreenCapturePossible() throws Exception { 67 assertNotNull(getInstrumentation().getUiAutomation().takeScreenshot()); 68 } 69 70 // We need to launch an activity before trying to take a screen shot, because screenshots are 71 // only blocked on a per-user basis in the profile owner case depending on the owner of the 72 // foreground activity. 73 private void startTestActivity() throws Exception { 74 Intent launchIntent = new Intent(); 75 launchIntent.setComponent(new ComponentName(PACKAGE_NAME, 76 ScreenCaptureDisabledActivity.class.getName())); 77 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 78 mContext.startActivity(launchIntent); 79 assertTrue(mReceiver.waitForBroadcast()); 80 Thread.sleep(1000); 81 } 82 83 private class ScreenCaptureBroadcastReceiver extends BroadcastReceiver { 84 private final Semaphore mSemaphore = new Semaphore(0); 85 86 @Override 87 public void onReceive(Context context, Intent intent) { 88 Log.d(TAG, "Broadcast received"); 89 mSemaphore.release(); 90 } 91 92 public boolean waitForBroadcast() throws Exception { 93 if (mSemaphore.tryAcquire(5, TimeUnit.SECONDS)) { 94 return true; 95 } 96 return false; 97 } 98 } 99 } 100