1 package com.android.cts.deviceandprofileowner; 2 3 import static org.junit.Assert.assertEquals; 4 5 import android.app.Activity; 6 import android.content.BroadcastReceiver; 7 import android.content.ComponentName; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.graphics.Bitmap; 11 import android.util.Log; 12 13 import androidx.test.InstrumentationRegistry; 14 import androidx.test.runner.AndroidJUnit4; 15 16 import com.android.compatibility.common.util.BlockingBroadcastReceiver; 17 18 import org.junit.Assert; 19 import org.junit.Before; 20 import org.junit.Test; 21 import org.junit.runner.RunWith; 22 23 import java.util.concurrent.LinkedBlockingQueue; 24 import java.util.concurrent.TimeUnit; 25 26 /** 27 * Testing 28 * {@link android.app.admin.DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)} 29 * is enforced in {@link android.service.voice.VoiceInteractionSession#onHandleScreenshot(Bitmap)}. 30 */ 31 @RunWith(AndroidJUnit4.class) 32 public class AssistScreenCaptureDisabledTest { 33 private static final String TAG = "DevicePolicyAssistTest"; 34 35 private static final String ACTION_CHECK_IS_READY = "voice_interaction_service.is_ready"; 36 private static final String ACTION_SHOW_SESSION = "voice_interaction_service.show_session"; 37 private static final String ACTION_HANDLE_SCREENSHOT = 38 "voice_interaction_session_service.handle_screenshot"; 39 private static final String KEY_HAS_SCREENSHOT = "has_screenshot"; 40 private static final String ASSIST_PACKAGE = "com.android.cts.devicepolicy.assistapp"; 41 42 private static final int MAX_ATTEMPTS_COUNT = 5; 43 private static final int WAIT_IN_SECOND = 5; 44 private Context mContext; 45 46 @Before setup()47 public void setup() { 48 mContext = InstrumentationRegistry.getContext(); 49 } 50 51 @Test testScreenCaptureImpossible_assist()52 public void testScreenCaptureImpossible_assist() throws Exception { 53 assertScreenCapturePossible(false); 54 } 55 56 @Test testScreenCapturePossible_assist()57 public void testScreenCapturePossible_assist() throws Exception { 58 assertScreenCapturePossible(true); 59 } 60 assertScreenCapturePossible(boolean possible)61 private void assertScreenCapturePossible(boolean possible) throws InterruptedException { 62 // Wait until voice interaction service is ready by sending broadcast to ask for status. 63 Intent checkIsReadyIntent = new Intent(ACTION_CHECK_IS_READY); 64 checkIsReadyIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); 65 checkIsReadyIntent.setPackage(ASSIST_PACKAGE); 66 boolean isAssistReady = false; 67 for (int i = 0; i < MAX_ATTEMPTS_COUNT && !isAssistReady; i++) { 68 Log.d(TAG, "assertScreenCapturePossible: wait for assist service ready, attempt " + i); 69 final LinkedBlockingQueue<Boolean> q = new LinkedBlockingQueue<>(); 70 mContext.sendOrderedBroadcast(checkIsReadyIntent, null, new BroadcastReceiver() { 71 @Override 72 public void onReceive(Context context, Intent intent) { 73 q.offer(getResultCode() == Activity.RESULT_OK); 74 } 75 }, null, Activity.RESULT_CANCELED, null, null); 76 Boolean result = q.poll(WAIT_IN_SECOND, TimeUnit.SECONDS); 77 isAssistReady = result != null && result; 78 } 79 Assert.assertTrue(isAssistReady); 80 81 // Send broadcast to voice interaction service and ask for screnshot. 82 BlockingBroadcastReceiver receiver = new BlockingBroadcastReceiver( 83 mContext, ACTION_HANDLE_SCREENSHOT); 84 try { 85 receiver.register(); 86 Intent showSessionIntent = new Intent(ACTION_SHOW_SESSION); 87 showSessionIntent.setPackage(ASSIST_PACKAGE); 88 mContext.sendBroadcast(showSessionIntent); 89 Intent screenShotIntent = null; 90 for (int i = 0; i < MAX_ATTEMPTS_COUNT && (screenShotIntent == null); ++ i) { 91 Log.d(TAG, "has not received intent yet: wait for intent, attempt " + i); 92 screenShotIntent = receiver.awaitForBroadcast(); 93 } 94 Assert.assertNotNull(screenShotIntent); 95 Assert.assertTrue(screenShotIntent.hasExtra(KEY_HAS_SCREENSHOT)); 96 assertEquals(possible, screenShotIntent.getBooleanExtra(KEY_HAS_SCREENSHOT, false)); 97 } finally { 98 receiver.unregisterQuietly(); 99 } 100 } 101 } 102