Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package android.server.wm;
     18 
     19 import static android.server.wm.UiDeviceUtils.pressBackButton;
     20 import static android.server.wm.app.Components.DISMISS_KEYGUARD_ACTIVITY;
     21 import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
     22 
     23 import static org.junit.Assert.assertFalse;
     24 import static org.junit.Assert.assertTrue;
     25 import static org.junit.Assume.assumeTrue;
     26 
     27 import android.platform.test.annotations.Presubmit;
     28 import android.server.wm.ActivityManagerState.ActivityDisplay;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 
     33 import java.util.List;
     34 
     35 /**
     36  * Display tests that require a keyguard.
     37  *
     38  * <p>Build/Install/Run:
     39  *     atest CtsWindowManagerDeviceTestCases:MultiDisplayKeyguardTests
     40  */
     41 @Presubmit
     42 public class MultiDisplayKeyguardTests extends MultiDisplayTestBase {
     43 
     44     @Before
     45     @Override
     46     public void setUp() throws Exception {
     47         super.setUp();
     48 
     49         assumeTrue(supportsMultiDisplay());
     50         assumeTrue(supportsInsecureLock());
     51     }
     52 
     53     /**
     54      * Tests whether a FLAG_DISMISS_KEYGUARD activity on a secondary display is visible (for an
     55      * insecure keyguard).
     56      */
     57     @Test
     58     public void testDismissKeyguardActivity_secondaryDisplay() throws Exception {
     59         try (final LockScreenSession lockScreenSession = new LockScreenSession();
     60              final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
     61             final ActivityDisplay newDisplay = virtualDisplaySession.createDisplay();
     62 
     63             lockScreenSession.gotoKeyguard();
     64             mAmWmState.assertKeyguardShowingAndNotOccluded();
     65             launchActivityOnDisplay(DISMISS_KEYGUARD_ACTIVITY, newDisplay.mId);
     66             mAmWmState.waitForKeyguardShowingAndNotOccluded();
     67             mAmWmState.assertKeyguardShowingAndNotOccluded();
     68             mAmWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
     69         }
     70     }
     71 
     72     /**
     73      * Tests keyguard dialog shows on secondary display.
     74      * @throws Exception
     75      */
     76     @Test
     77     public void testShowKeyguardDialogOnSecondaryDisplay() throws Exception {
     78         try (final LockScreenSession lockScreenSession = new LockScreenSession();
     79              final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
     80             final ActivityDisplay publicDisplay = virtualDisplaySession.setPublicDisplay(true)
     81                     .createDisplay();
     82             lockScreenSession.gotoKeyguard();
     83             mAmWmState.waitForWithWmState((state) -> isKeyguardOnDisplay(state, publicDisplay.mId),
     84                     "Waiting for keyguard window to show");
     85 
     86             assertTrue("KeyguardDialog must show on external public display",
     87                     isKeyguardOnDisplay(mAmWmState.getWmState(), publicDisplay.mId));
     88 
     89             // Keyguard dialog mustn't be removed when press back key
     90             pressBackButton();
     91             mAmWmState.computeState(true);
     92             assertTrue("KeyguardDialog must not be removed when press back key",
     93                     isKeyguardOnDisplay(mAmWmState.getWmState(), publicDisplay.mId));
     94         }
     95     }
     96 
     97     /**
     98      * Tests keyguard dialog cannot be shown on private display.
     99      * @throws Exception
    100      */
    101     @Test
    102     public void testNoKeyguardDialogOnPrivateDisplay() throws Exception {
    103         try (final LockScreenSession lockScreenSession = new LockScreenSession();
    104              final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
    105             final ActivityDisplay privateDisplay = virtualDisplaySession.setPublicDisplay(false)
    106                     .createDisplay();
    107             final ActivityDisplay publicDisplay = virtualDisplaySession.setPublicDisplay(true)
    108                     .createDisplay();
    109 
    110             lockScreenSession.gotoKeyguard();
    111             mAmWmState.waitForWithWmState((state) -> isKeyguardOnDisplay(state, publicDisplay.mId),
    112                     "Waiting for keyguard window to show");
    113 
    114             assertTrue("KeyguardDialog must show on external public display",
    115                     isKeyguardOnDisplay(mAmWmState.getWmState(), publicDisplay.mId));
    116             assertFalse("KeyguardDialog must not show on external private display",
    117                     isKeyguardOnDisplay(mAmWmState.getWmState(), privateDisplay.mId));
    118         }
    119     }
    120 
    121     private boolean isKeyguardOnDisplay(WindowManagerState windowManagerState, int displayId) {
    122         final List<WindowManagerState.WindowState> states =
    123                 windowManagerState.getMatchingWindowType(TYPE_KEYGUARD_DIALOG);
    124         for (WindowManagerState.WindowState ws : states) {
    125             if (ws.getDisplayId() == displayId) return true;
    126         }
    127         return false;
    128     }
    129 }
    130