Test that trusted display cannot be created without permission

Bug: 162627132
Test: atest DisplayTest
Test: atest VirtualDisplayTest#testTrustedVirtualDisplay
Test: atest VirtualDisplayTest#testUntrustedSysDecorVirtualDisplay

Change-Id: If462fd383117273852c928d93f03e3d41a1b2810
Merged-In: If462fd383117273852c928d93f03e3d41a1b2810
(cherry picked from commit bab35f508b264c9a663ae7264db41efab018d812)
diff --git a/tests/tests/display/src/android/display/cts/VirtualDisplayTest.java b/tests/tests/display/src/android/display/cts/VirtualDisplayTest.java
index db57eed..3b2406f 100644
--- a/tests/tests/display/src/android/display/cts/VirtualDisplayTest.java
+++ b/tests/tests/display/src/android/display/cts/VirtualDisplayTest.java
@@ -28,8 +28,8 @@
 import android.media.ImageReader;
 import android.os.Bundle;
 import android.os.Handler;
-import android.os.Looper;
 import android.os.HandlerThread;
+import android.os.Looper;
 import android.os.SystemClock;
 import android.test.AndroidTestCase;
 import android.util.DisplayMetrics;
@@ -37,7 +37,6 @@
 import android.view.Display;
 import android.view.Surface;
 import android.view.ViewGroup.LayoutParams;
-import android.view.WindowManager;
 import android.widget.ImageView;
 
 import java.nio.ByteBuffer;
@@ -76,6 +75,9 @@
     private HandlerThread mCheckThread;
     private Handler mCheckHandler;
 
+    private static final int VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS = 1 << 9;
+    private static final int VIRTUAL_DISPLAY_FLAG_TRUSTED = 1 << 10;
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -190,6 +192,49 @@
         assertDisplayUnregistered(display);
     }
 
+    /**
+     * Ensures that {@link DisplayManager#VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS} will
+     * be clear if an application creates an virtual display without the
+     * flag {@link DisplayManager#VIRTUAL_DISPLAY_FLAG_TRUSTED}.
+     */
+    public void testUntrustedSysDecorVirtualDisplay() throws Exception {
+        VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
+                WIDTH, HEIGHT, DENSITY, mSurface,
+                VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS);
+        assertNotNull("virtual display must not be null", virtualDisplay);
+
+        Display display = virtualDisplay.getDisplay();
+        try {
+            // Verify that the created virtual display doesn't have flags
+            // FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS.
+            assertDisplayRegistered(display, Display.FLAG_PRIVATE);
+            assertEquals(mSurface, virtualDisplay.getSurface());
+
+            // Show a private presentation on the display.
+            assertDisplayCanShowPresentation("private presentation window",
+                    display, BLUEISH, 0);
+        } finally {
+            virtualDisplay.release();
+        }
+        assertDisplayUnregistered(display);
+    }
+
+    /**
+     * Ensures that throws {@link SecurityException} when an application creates a trusted virtual
+     * display without holding the permission {@code ADD_TRUSTED_DISPLAY}.
+     */
+    public void testTrustedVirtualDisplay() throws Exception {
+        try {
+            VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
+                    WIDTH, HEIGHT, DENSITY, mSurface, VIRTUAL_DISPLAY_FLAG_TRUSTED);
+        } catch (SecurityException e) {
+            // Expected.
+            return;
+        }
+        fail("SecurityException must be thrown if a trusted virtual display is created without"
+                + "holding the permission ADD_TRUSTED_DISPLAY.");
+    }
+
     private void assertDisplayRegistered(Display display, int flags) {
         assertNotNull("display object must not be null", display);
         assertTrue("display must be valid", display.isValid());