Merge "Fix VirtualDisplayTest failures" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-03-31 08:57:14 +00:00
committed by Android (Google) Code Review

View File

@@ -16,6 +16,10 @@
package android.hardware.display; package android.hardware.display;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
import android.annotation.ColorInt;
import android.app.Dialog;
import android.app.Presentation; import android.app.Presentation;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
@@ -33,8 +37,11 @@ import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.Display; import android.view.Display;
import android.view.Surface; import android.view.Surface;
import android.view.View;
import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.WindowManager.LayoutParams.Flags;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
@@ -110,7 +117,7 @@ public class VirtualDisplayTest extends AndroidTestCase {
* Ensures that an application can create a private virtual display and show * Ensures that an application can create a private virtual display and show
* its own windows on it. * its own windows on it.
*/ */
public void testPrivateVirtualDisplay() throws Exception { public void testPrivateVirtualDisplay() {
VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME, VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
WIDTH, HEIGHT, DENSITY, mSurface, 0); WIDTH, HEIGHT, DENSITY, mSurface, 0);
assertNotNull("virtual display must not be null", virtualDisplay); assertNotNull("virtual display must not be null", virtualDisplay);
@@ -120,9 +127,7 @@ public class VirtualDisplayTest extends AndroidTestCase {
assertDisplayRegistered(display, Display.FLAG_PRIVATE); assertDisplayRegistered(display, Display.FLAG_PRIVATE);
// Show a private presentation on the display. // Show a private presentation on the display.
assertDisplayCanShowPresentation("private presentation window", assertDisplayCanShowPresentation("private presentation window", display, BLUEISH, 0);
display, BLUEISH,
WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION, 0);
} finally { } finally {
virtualDisplay.release(); virtualDisplay.release();
} }
@@ -133,10 +138,9 @@ public class VirtualDisplayTest extends AndroidTestCase {
* Ensures that an application can create a private presentation virtual display and show * Ensures that an application can create a private presentation virtual display and show
* its own windows on it. * its own windows on it.
*/ */
public void testPrivatePresentationVirtualDisplay() throws Exception { public void testPrivatePresentationVirtualDisplay() {
VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME, VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
WIDTH, HEIGHT, DENSITY, mSurface, WIDTH, HEIGHT, DENSITY, mSurface, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION);
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION);
assertNotNull("virtual display must not be null", virtualDisplay); assertNotNull("virtual display must not be null", virtualDisplay);
Display display = virtualDisplay.getDisplay(); Display display = virtualDisplay.getDisplay();
@@ -144,9 +148,7 @@ public class VirtualDisplayTest extends AndroidTestCase {
assertDisplayRegistered(display, Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION); assertDisplayRegistered(display, Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION);
// Show a private presentation on the display. // Show a private presentation on the display.
assertDisplayCanShowPresentation("private presentation window", assertDisplayCanShowPresentation("private presentation window", display, BLUEISH, 0);
display, BLUEISH,
WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION, 0);
} finally { } finally {
virtualDisplay.release(); virtualDisplay.release();
} }
@@ -161,7 +163,7 @@ public class VirtualDisplayTest extends AndroidTestCase {
* type to create the window. Another choice might be SYSTEM_ALERT_WINDOW but * type to create the window. Another choice might be SYSTEM_ALERT_WINDOW but
* that requires a permission. * that requires a permission.
*/ */
public void testPublicPresentationVirtualDisplay() throws Exception { public void testPublicPresentationVirtualDisplay() {
VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME, VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
WIDTH, HEIGHT, DENSITY, mSurface, WIDTH, HEIGHT, DENSITY, mSurface,
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
@@ -177,29 +179,22 @@ public class VirtualDisplayTest extends AndroidTestCase {
// virtual display automatically. // virtual display automatically.
Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
assertDisplayCanShowPresentation("mirrored window", assertDisplayCanShowPresentation("mirrored window",
defaultDisplay, GREENISH, defaultDisplay, GREENISH, 0);
WindowManager.LayoutParams.TYPE_TOAST, 0);
// Mirroring case with secure window (but display is not secure). // Mirroring case with secure window (but display is not secure).
// Show a window on the default display. It should be replaced with black on // Show a window on the default display. It should be replaced with black on
// the virtual display. // the virtual display.
assertDisplayCanShowPresentation("mirrored secure window on non-secure display", assertDisplayCanShowPresentation("mirrored secure window on non-secure display",
defaultDisplay, Color.BLACK, defaultDisplay, Color.BLACK, WindowManager.LayoutParams.FLAG_SECURE);
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_SECURE);
// Presentation case. // Presentation case.
// Show a normal presentation on the display. // Show a normal presentation on the display.
assertDisplayCanShowPresentation("presentation window", assertDisplayCanShowPresentation("presentation window", display, BLUEISH, 0);
display, BLUEISH,
WindowManager.LayoutParams.TYPE_TOAST, 0);
// Presentation case with secure window (but display is not secure). // Presentation case with secure window (but display is not secure).
// Show a normal presentation on the display. It should be replaced with black. // Show a normal presentation on the display. It should be replaced with black.
assertDisplayCanShowPresentation("secure presentation window on non-secure display", assertDisplayCanShowPresentation("secure presentation window on non-secure display",
display, Color.BLACK, display, Color.BLACK, WindowManager.LayoutParams.FLAG_SECURE);
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_SECURE);
} finally { } finally {
virtualDisplay.release(); virtualDisplay.release();
} }
@@ -214,7 +209,7 @@ public class VirtualDisplayTest extends AndroidTestCase {
* type to create the window. Another choice might be SYSTEM_ALERT_WINDOW but * type to create the window. Another choice might be SYSTEM_ALERT_WINDOW but
* that requires a permission. * that requires a permission.
*/ */
public void testSecurePublicPresentationVirtualDisplay() throws Exception { public void testSecurePublicPresentationVirtualDisplay() {
VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME, VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
WIDTH, HEIGHT, DENSITY, mSurface, WIDTH, HEIGHT, DENSITY, mSurface,
DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE
@@ -231,16 +226,12 @@ public class VirtualDisplayTest extends AndroidTestCase {
// virtual display automatically. // virtual display automatically.
Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
assertDisplayCanShowPresentation("mirrored secure window on secure display", assertDisplayCanShowPresentation("mirrored secure window on secure display",
defaultDisplay, GREENISH, defaultDisplay, GREENISH, WindowManager.LayoutParams.FLAG_SECURE);
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_SECURE);
// Presentation case with secure window (and display is secure). // Presentation case with secure window (and display is secure).
// Show a normal presentation on the display. // Show a normal presentation on the display.
assertDisplayCanShowPresentation("secure presentation window on secure display", assertDisplayCanShowPresentation("secure presentation window on secure display",
display, BLUEISH, display, BLUEISH, WindowManager.LayoutParams.FLAG_SECURE);
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_SECURE);
} finally { } finally {
virtualDisplay.release(); virtualDisplay.release();
} }
@@ -251,7 +242,7 @@ public class VirtualDisplayTest extends AndroidTestCase {
* Ensures that an application can create a trusted virtual display with the permission * Ensures that an application can create a trusted virtual display with the permission
* {@code ADD_TRUSTED_DISPLAY}. * {@code ADD_TRUSTED_DISPLAY}.
*/ */
public void testTrustedVirtualDisplay() throws Exception { public void testTrustedVirtualDisplay() {
VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME, VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
WIDTH, HEIGHT, DENSITY, mSurface, WIDTH, HEIGHT, DENSITY, mSurface,
DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED); DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED);
@@ -299,35 +290,43 @@ public class VirtualDisplayTest extends AndroidTestCase {
assertFalse("display must no longer be valid", display.isValid()); assertFalse("display must no longer be valid", display.isValid());
} }
/**
* Verifies that virtual display shows the expected {@code color}.
* <p>
* If {@code display} is the {@link Display#DEFAULT_DISPLAY default display}, show a fullscreen
* overlay {@link Dialog} on the default display and verify if the dialog is mirrored to the
* virtual display.
* </p><p>
* If {@code display} is a {@link VirtualDisplay}, show a {@link Presentation} on that virtual
* display and verify the content.
* </p>
*/
private void assertDisplayCanShowPresentation(String message, final Display display, private void assertDisplayCanShowPresentation(String message, final Display display,
final int color, final int windowType, final int windowFlags) { @ColorInt final int color, @Flags final int windowFlags) {
// At this point, we should not have seen any blue. // At this point, we should not have seen any blue.
assertTrue(message + ": display should not show content before window is shown", assertTrue(message + ": display should not show content before window is shown",
mImageListener.getColor() != color); mImageListener.getColor() != color);
final TestPresentation[] presentation = new TestPresentation[1]; final Dialog[] dialogs = new Dialog[1];
try { try {
// Show the presentation. // Show the presentation.
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override if (display.getDisplayId() == Display.DEFAULT_DISPLAY) {
public void run() { final Context windowContext = getContext().createWindowContext(display,
presentation[0] = new TestPresentation(getContext(), display, TYPE_APPLICATION_OVERLAY, null /* options */);
color, windowType, windowFlags); dialogs[0] = new TestDialog(windowContext, color, windowFlags);
presentation[0].show(); } else {
dialogs[0] = new TestPresentation(getContext(), display, color, windowFlags);
} }
dialogs[0].show();
}); });
// Wait for the blue to be seen. // Wait for the color to be seen.
assertTrue(message + ": display should show content after window is shown", assertTrue(message + ": display should show content after window is shown",
mImageListener.waitForColor(color, TIMEOUT)); mImageListener.waitForColor(color, TIMEOUT));
} finally { } finally {
if (presentation[0] != null) { if (dialogs[0] != null) {
runOnUiThread(new Runnable() { runOnUiThread(() -> dialogs[0].dismiss());
@Override
public void run() {
presentation[0].dismiss();
}
});
} }
} }
} }
@@ -360,13 +359,15 @@ public class VirtualDisplayTest extends AndroidTestCase {
return null; return null;
} }
private final class TestPresentation extends Presentation { private static final class TestPresentation extends Presentation {
@ColorInt
private final int mColor; private final int mColor;
@Flags
private final int mWindowFlags; private final int mWindowFlags;
public TestPresentation(Context context, Display display, TestPresentation(Context context, Display display, @ColorInt int color,
int color, int windowType, int windowFlags) { @Flags int windowFlags) {
super(context, display, 0 /* theme */, windowType); super(context, display);
mColor = color; mColor = color;
mWindowFlags = windowFlags; mWindowFlags = windowFlags;
} }
@@ -378,15 +379,44 @@ public class VirtualDisplayTest extends AndroidTestCase {
setTitle(TAG); setTitle(TAG);
getWindow().addFlags(mWindowFlags); getWindow().addFlags(mWindowFlags);
// Create a solid color image to use as the content of the presentation. setContentView(createImageView(getContext(), mColor));
ImageView view = new ImageView(getContext());
view.setImageDrawable(new ColorDrawable(mColor));
view.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(view);
} }
} }
private static final class TestDialog extends Dialog {
@ColorInt
private final int mColor;
@Flags
private final int mWindowFlags;
TestDialog(Context context, @ColorInt int color, @Flags int windowFlags) {
super(context, android.R.style.Theme_Material_NoActionBar_Fullscreen);
mColor = color;
mWindowFlags = windowFlags;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(TAG);
final Window window = getWindow();
window.setType(TYPE_APPLICATION_OVERLAY);
window.addFlags(mWindowFlags);
setContentView(createImageView(getContext(), mColor));
}
}
private static View createImageView(Context context, @ColorInt int color) {
// Create a solid color image to use as the content of the presentation.
ImageView view = new ImageView(context);
view.setImageDrawable(new ColorDrawable(color));
view.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return view;
}
/** /**
* Watches for an image with a large amount of some particular solid color to be shown. * Watches for an image with a large amount of some particular solid color to be shown.
*/ */