• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.pressEnterButton;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assume.assumeTrue;
25 
26 import android.content.ComponentName;
27 import android.platform.test.annotations.Presubmit;
28 import android.view.KeyEvent;
29 
30 import androidx.test.rule.ActivityTestRule;
31 
32 import com.android.compatibility.common.util.WindowUtil;
33 
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 
38 /**
39  * Build/Install/Run:
40  *     atest KeyguardInputTests
41  */
42 @Presubmit
43 public class KeyguardInputTests extends KeyguardTestBase {
44     @Rule
45     public ActivityTestRule<KeyEventActivity> mActivityRule =
46             new ActivityTestRule<>(KeyEventActivity.class);
47     private KeyEventActivity mActivity;
48 
49     @Before
50     @Override
setUp()51     public void setUp() {
52         assumeTrue(supportsInsecureLock());
53 
54         mActivity = mActivityRule.getActivity();
55         WindowUtil.waitForFocus(mActivity);
56     }
57 
58     /**
59      * Launch an activity on top of the keyguard, and inject ENTER key. Ensure that the ENTER key
60      * goes to the activity.
61      */
62     @Test
testReceiveKeysOnTopOfKeyguard()63     public void testReceiveKeysOnTopOfKeyguard() {
64         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
65         final ComponentName KEY_EVENT_ACTIVITY = new ComponentName("android.server.wm.cts",
66                 "android.server.wm.KeyEventActivity");
67         lockScreenSession.gotoKeyguard(KEY_EVENT_ACTIVITY);
68         pressEnterButton();
69         assertReceivedKey(KeyEvent.KEYCODE_ENTER);
70         assertNoMoreEvents();
71     }
72 
assertReceivedKey(int keycode)73     private void assertReceivedKey(int keycode) {
74         KeyEvent event = mActivity.popKey();
75         assertNotNull(event);
76         assertEquals(keycode, event.getKeyCode());
77         assertEquals(KeyEvent.ACTION_DOWN, event.getAction());
78         event = mActivity.popKey();
79         assertNotNull(event);
80         assertEquals(keycode, event.getKeyCode());
81         assertEquals(KeyEvent.ACTION_UP, event.getAction());
82     }
83 
assertNoMoreEvents()84     private void assertNoMoreEvents() {
85         KeyEvent event = mActivity.popKey();
86         assertNull(event);
87     }
88 }
89