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.pressBackButton; 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.ext.junit.runners.AndroidJUnit4; 31 import androidx.test.rule.ActivityTestRule; 32 33 import com.android.compatibility.common.util.PollingCheck; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 /** 41 * Build/Install/Run: 42 * atest KeyguardInputTests 43 */ 44 @Presubmit 45 public class KeyguardInputTests extends KeyguardTestBase { 46 @Rule 47 public ActivityTestRule<KeyEventActivity> mActivityRule = 48 new ActivityTestRule<>(KeyEventActivity.class); 49 private KeyEventActivity mActivity; 50 51 @Before 52 @Override setUp()53 public void setUp() { 54 assumeTrue(supportsInsecureLock()); 55 56 mActivity = mActivityRule.getActivity(); 57 PollingCheck.waitFor(mActivity::hasWindowFocus); 58 } 59 60 /** 61 * Launch an activity on top of the keyguard, and inject BACK key. Ensure that the BACK key goes 62 * to the activity. 63 */ 64 @Test testReceiveKeysOnTopOfKeyguard()65 public void testReceiveKeysOnTopOfKeyguard() { 66 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 67 final ComponentName KEY_EVENT_ACTIVITY = new ComponentName("android.server.wm.cts", 68 "android.server.wm.KeyEventActivity"); 69 lockScreenSession.gotoKeyguard(KEY_EVENT_ACTIVITY); 70 pressBackButton(); 71 assertReceivedKey(KeyEvent.KEYCODE_BACK); 72 assertNoMoreEvents(); 73 } 74 assertReceivedKey(int keycode)75 private void assertReceivedKey(int keycode) { 76 KeyEvent event = mActivity.popKey(); 77 assertNotNull(event); 78 assertEquals(keycode, event.getKeyCode()); 79 assertEquals(KeyEvent.ACTION_DOWN, event.getAction()); 80 event = mActivity.popKey(); 81 assertNotNull(event); 82 assertEquals(keycode, event.getKeyCode()); 83 assertEquals(KeyEvent.ACTION_UP, event.getAction()); 84 } 85 assertNoMoreEvents()86 private void assertNoMoreEvents() { 87 KeyEvent event = mActivity.popKey(); 88 assertNull(event); 89 } 90 } 91