DO NOT MERGE - Add test for BAL with VirtualDisplay

Verifies that a virtualdisplay with a presentation doesn't let the app
bypass BAL

Bug: 205130886
Test: atest CtsActivityManagerBackgroundActivityTestCases
Change-Id: I3c66ef758fef067b73278ee5d2c6a81ab0c139b7
Merged-In: I3c66ef758fef067b73278ee5d2c6a81ab0c139b7
(cherry picked from commit 15358ddab1234f17f1ef92a7826bd702c7bfe6c9)
diff --git a/tests/framework/base/windowmanager/backgroundactivity/AppA/AndroidManifest.xml b/tests/framework/base/windowmanager/backgroundactivity/AppA/AndroidManifest.xml
index eb156b6..2f8b2dd 100755
--- a/tests/framework/base/windowmanager/backgroundactivity/AppA/AndroidManifest.xml
+++ b/tests/framework/base/windowmanager/backgroundactivity/AppA/AndroidManifest.xml
@@ -37,6 +37,9 @@
                 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
             </intent-filter>
         </receiver>
+        <receiver
+            android:name=".VirtualDisplayReceiver"
+            android:exported="true"/>
         <activity
             android:name=".ForegroundActivity"
             android:taskAffinity=".am_cts_bg_task_a"
diff --git a/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/Components.java b/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/Components.java
index 5023481..ce25d48 100644
--- a/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/Components.java
+++ b/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/Components.java
@@ -33,6 +33,8 @@
             component(Components.class, "StartBackgroundActivityReceiver");
     public static final ComponentName APP_A_SIMPLE_ADMIN_RECEIVER =
             component(Components.class, "SimpleAdminReceiver");
+    public static final ComponentName APP_A_VIRTUAL_DISPLAY_RECEIVER =
+            component(Components.class, "VirtualDisplayReceiver");
 
     /** Extra key constants for {@link #APP_A_FOREGROUND_ACTIVITY}. */
     public static class ForegroundActivity {
diff --git a/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/VirtualDisplayReceiver.java b/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/VirtualDisplayReceiver.java
new file mode 100644
index 0000000..f445a17
--- /dev/null
+++ b/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/VirtualDisplayReceiver.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.server.wm.backgroundactivity.appa;
+
+import android.app.Presentation;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.hardware.display.DisplayManager;
+import android.hardware.display.VirtualDisplay;
+
+/**
+ * A class to help test case to start background activity.
+ */
+public class VirtualDisplayReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        createVirtualDisplayAndShowPresentation(context);
+    }
+
+    private void createVirtualDisplayAndShowPresentation(Context context) {
+        VirtualDisplay virtualDisplay = context.getSystemService(
+                DisplayManager.class).createVirtualDisplay(
+                "VirtualDisplay1", 10, 10, 10, null, 0);
+        new Presentation(context, virtualDisplay.getDisplay()).show();
+    }
+}
diff --git a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
index b4d3ee9..2632b2b 100644
--- a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
+++ b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
@@ -30,6 +30,7 @@
 import static android.server.wm.backgroundactivity.appa.Components.APP_A_SEND_PENDING_INTENT_RECEIVER;
 import static android.server.wm.backgroundactivity.appa.Components.APP_A_SIMPLE_ADMIN_RECEIVER;
 import static android.server.wm.backgroundactivity.appa.Components.APP_A_START_ACTIVITY_RECEIVER;
+import static android.server.wm.backgroundactivity.appa.Components.APP_A_VIRTUAL_DISPLAY_RECEIVER;
 import static android.server.wm.backgroundactivity.appa.Components.ForegroundActivity.ACTION_LAUNCH_BACKGROUND_ACTIVITIES;
 import static android.server.wm.backgroundactivity.appa.Components.ForegroundActivity.LAUNCH_BACKGROUND_ACTIVITY_EXTRA;
 import static android.server.wm.backgroundactivity.appa.Components.ForegroundActivity.LAUNCH_INTENTS_EXTRA;
@@ -40,6 +41,7 @@
 import static android.server.wm.backgroundactivity.appa.Components.SendPendingIntentReceiver.IS_BROADCAST_EXTRA;
 import static android.server.wm.backgroundactivity.appa.Components.StartBackgroundActivityReceiver.START_ACTIVITY_DELAY_MS_EXTRA;
 import static android.server.wm.backgroundactivity.appb.Components.APP_B_FOREGROUND_ACTIVITY;
+import static android.view.WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION;
 
 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
 import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
@@ -149,6 +151,32 @@
     }
 
     @Test
+    public void testBackgroundActivityBlocked_VirtualDisplay() throws Exception {
+        mContext.sendBroadcast(new Intent().setComponent(APP_A_VIRTUAL_DISPLAY_RECEIVER));
+        boolean foundPresentation = false;
+        long retryIntervalMs = 1000L;
+        int maxTries = 5;
+        for (int i = 0; i < maxTries; i++) {
+            mAmWmState.getWmState().computeState();
+            if (mAmWmState.getWmState().getWindowByPackageName(APP_A_PACKAGE_NAME,
+                    TYPE_PRIVATE_PRESENTATION) != null) {
+                foundPresentation = true;
+                break;
+            }
+            SystemClock.sleep(retryIntervalMs);
+        }
+        assertTrue("Private presentation was never created", foundPresentation);
+
+        // Start AppA background activity and blocked
+        Intent intent = new Intent();
+        intent.setComponent(APP_A_START_ACTIVITY_RECEIVER);
+        mContext.sendBroadcast(intent);
+        boolean result = waitForActivityFocused(APP_A_BACKGROUND_ACTIVITY);
+        assertFalse("Should not able to launch background activity", result);
+        assertTaskStack(null, APP_A_BACKGROUND_ACTIVITY);
+    }
+
+    @Test
     public void testBackgroundActivityNotBlockedWhenSystemAlertWindowGranted() throws Exception {
         // enable appopp for SAW for this test
         AppOpsUtils.setOpMode(APP_A_PACKAGE_NAME, "android:system_alert_window", MODE_ALLOWED);