FileDocCategorySizeDatePackage
VirtualDisplayTest.javaAPI DocAndroid 5.1 API20399Thu Mar 12 22:22:12 GMT 2015android.hardware.display

VirtualDisplayTest

public class VirtualDisplayTest extends android.test.AndroidTestCase
Tests that applications can create virtual displays and present content on them. Contains additional tests that cannot be included in CTS because they require system permissions. See also the CTS version of VirtualDisplayTest.

Fields Summary
private static final String
TAG
private static final String
NAME
private static final int
WIDTH
private static final int
HEIGHT
private static final int
DENSITY
private static final int
TIMEOUT
private static final int
BLUEISH
private static final int
GREENISH
private android.hardware.display.DisplayManager
mDisplayManager
private android.os.Handler
mHandler
private final Lock
mImageReaderLock
private android.media.ImageReader
mImageReader
private android.view.Surface
mSurface
private ImageListener
mImageListener
Constructors Summary
Methods Summary
private voidassertDisplayCanShowPresentation(java.lang.String message, android.view.Display display, int color, int windowType, int windowFlags)

        // At this point, we should not have seen any blue.
        assertTrue(message + ": display should not show content before window is shown",
                mImageListener.getColor() != color);

        final TestPresentation[] presentation = new TestPresentation[1];
        try {
            // Show the presentation.
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    presentation[0] = new TestPresentation(getContext(), display,
                            color, windowType, windowFlags);
                    presentation[0].show();
                }
            });

            // Wait for the blue to be seen.
            assertTrue(message + ": display should show content after window is shown",
                    mImageListener.waitForColor(color, TIMEOUT));
        } finally {
            if (presentation[0] != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        presentation[0].dismiss();
                    }
                });
            }
        }
    
private voidassertDisplayRegistered(android.view.Display display, int flags)

        assertNotNull("display object must not be null", display);
        assertTrue("display must be valid", display.isValid());
        assertTrue("display id must be unique",
                display.getDisplayId() != Display.DEFAULT_DISPLAY);
        assertEquals("display must have correct flags", flags, display.getFlags());
        assertEquals("display name must match supplied name", NAME, display.getName());
        Point size = new Point();
        display.getSize(size);
        assertEquals("display width must match supplied width", WIDTH, size.x);
        assertEquals("display height must match supplied height", HEIGHT, size.y);
        assertEquals("display rotation must be 0",
                Surface.ROTATION_0, display.getRotation());
        assertNotNull("display must be registered",
                findDisplay(mDisplayManager.getDisplays(), NAME));

        if ((flags & Display.FLAG_PRESENTATION) != 0) {
            assertNotNull("display must be registered as a presentation display",
                    findDisplay(mDisplayManager.getDisplays(
                            DisplayManager.DISPLAY_CATEGORY_PRESENTATION), NAME));
        } else {
            assertNull("display must not be registered as a presentation display",
                    findDisplay(mDisplayManager.getDisplays(
                            DisplayManager.DISPLAY_CATEGORY_PRESENTATION), NAME));
        }
    
private voidassertDisplayUnregistered(android.view.Display display)

        assertNull("display must no longer be registered after being removed",
                findDisplay(mDisplayManager.getDisplays(), NAME));
        assertFalse("display must no longer be valid", display.isValid());
    
private android.view.DisplayfindDisplay(android.view.Display[] displays, java.lang.String name)

        for (int i = 0; i < displays.length; i++) {
            if (displays[i].getName().equals(name)) {
                return displays[i];
            }
        }
        return null;
    
private voidrunOnUiThread(java.lang.Runnable runnable)

        Runnable waiter = new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    notifyAll();
                }
            }
        };
        synchronized (waiter) {
            mHandler.post(runnable);
            mHandler.post(waiter);
            try {
                waiter.wait(TIMEOUT);
            } catch (InterruptedException ex) {
            }
        }
    
protected voidsetUp()


    
         
        super.setUp();

        mDisplayManager = (DisplayManager)mContext.getSystemService(Context.DISPLAY_SERVICE);
        mHandler = new Handler(Looper.getMainLooper());
        mImageListener = new ImageListener();

        mImageReaderLock.lock();
        try {
            mImageReader = ImageReader.newInstance(WIDTH, HEIGHT, PixelFormat.RGBA_8888, 2);
            mImageReader.setOnImageAvailableListener(mImageListener, mHandler);
            mSurface = mImageReader.getSurface();
        } finally {
            mImageReaderLock.unlock();
        }
    
protected voidtearDown()

        super.tearDown();

        mImageReaderLock.lock();
        try {
            mImageReader.close();
            mImageReader = null;
            mSurface = null;
        } finally {
            mImageReaderLock.unlock();
        }
    
public voidtestPrivatePresentationVirtualDisplay()
Ensures that an application can create a private presentation virtual display and show its own windows on it.

        VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
                WIDTH, HEIGHT, DENSITY, mSurface,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION);
        assertNotNull("virtual display must not be null", virtualDisplay);

        Display display = virtualDisplay.getDisplay();
        try {
            assertDisplayRegistered(display, Display.FLAG_PRIVATE | Display.FLAG_PRESENTATION);

            // Show a private presentation on the display.
            assertDisplayCanShowPresentation("private presentation window",
                    display, BLUEISH,
                    WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION, 0);
        } finally {
            virtualDisplay.release();
        }
        assertDisplayUnregistered(display);
    
public voidtestPrivateVirtualDisplay()
Ensures that an application can create a private virtual display and show its own windows on it.

        VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
                WIDTH, HEIGHT, DENSITY, mSurface, 0);
        assertNotNull("virtual display must not be null", virtualDisplay);

        Display display = virtualDisplay.getDisplay();
        try {
            assertDisplayRegistered(display, Display.FLAG_PRIVATE);

            // Show a private presentation on the display.
            assertDisplayCanShowPresentation("private presentation window",
                    display, BLUEISH,
                    WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION, 0);
        } finally {
            virtualDisplay.release();
        }
        assertDisplayUnregistered(display);
    
public voidtestPublicPresentationVirtualDisplay()
Ensures that an application can create a public virtual display and show its own windows on it. This test requires the CAPTURE_VIDEO_OUTPUT permission. Because this test does not have an activity token, we use the TOAST window type to create the window. Another choice might be SYSTEM_ALERT_WINDOW but that requires a permission.

        VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
                WIDTH, HEIGHT, DENSITY, mSurface,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
                        | DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION);
        assertNotNull("virtual display must not be null", virtualDisplay);

        Display display = virtualDisplay.getDisplay();
        try {
            assertDisplayRegistered(display, Display.FLAG_PRESENTATION);

            // Mirroring case.
            // Show a window on the default display.  It should be mirrored to the
            // virtual display automatically.
            Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
            assertDisplayCanShowPresentation("mirrored window",
                    defaultDisplay, GREENISH,
                    WindowManager.LayoutParams.TYPE_TOAST, 0);

            // Mirroring case with secure window (but display is not secure).
            // Show a window on the default display.  It should be replaced with black on
            // the virtual display.
            assertDisplayCanShowPresentation("mirrored secure window on non-secure display",
                    defaultDisplay, Color.BLACK,
                    WindowManager.LayoutParams.TYPE_TOAST,
                    WindowManager.LayoutParams.FLAG_SECURE);

            // Presentation case.
            // Show a normal presentation on the display.
            assertDisplayCanShowPresentation("presentation window",
                    display, BLUEISH,
                    WindowManager.LayoutParams.TYPE_TOAST, 0);

            // Presentation case with secure window (but display is not secure).
            // Show a normal presentation on the display.  It should be replaced with black.
            assertDisplayCanShowPresentation("secure presentation window on non-secure display",
                    display, Color.BLACK,
                    WindowManager.LayoutParams.TYPE_TOAST,
                    WindowManager.LayoutParams.FLAG_SECURE);
        } finally {
            virtualDisplay.release();
        }
        assertDisplayUnregistered(display);
    
public voidtestSecurePublicPresentationVirtualDisplay()
Ensures that an application can create a secure public virtual display and show its own windows on it. This test requires the CAPTURE_SECURE_VIDEO_OUTPUT permission. Because this test does not have an activity token, we use the TOAST window type to create the window. Another choice might be SYSTEM_ALERT_WINDOW but that requires a permission.

        VirtualDisplay virtualDisplay = mDisplayManager.createVirtualDisplay(NAME,
                WIDTH, HEIGHT, DENSITY, mSurface,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE
                        | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
                        | DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION);
        assertNotNull("virtual display must not be null", virtualDisplay);

        Display display = virtualDisplay.getDisplay();
        try {
            assertDisplayRegistered(display, Display.FLAG_PRESENTATION | Display.FLAG_SECURE);

            // Mirroring case with secure window (and display is secure).
            // Show a window on the default display.  It should be mirrored to the
            // virtual display automatically.
            Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
            assertDisplayCanShowPresentation("mirrored secure window on secure display",
                    defaultDisplay, GREENISH,
                    WindowManager.LayoutParams.TYPE_TOAST,
                    WindowManager.LayoutParams.FLAG_SECURE);

            // Presentation case with secure window (and display is secure).
            // Show a normal presentation on the display.
            assertDisplayCanShowPresentation("secure presentation window on secure display",
                    display, BLUEISH,
                    WindowManager.LayoutParams.TYPE_TOAST,
                    WindowManager.LayoutParams.FLAG_SECURE);
        } finally {
            virtualDisplay.release();
        }
        assertDisplayUnregistered(display);