1 /* 2 * Copyright (C) 2019 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 com.google.android.lockscreenautomation; 18 19 import org.junit.Assert; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.res.Resources; 25 import android.provider.Settings; 26 import androidx.test.uiautomator.By; 27 import androidx.test.uiautomator.BySelector; 28 import androidx.test.uiautomator.UiAutomatorTestCase; 29 import androidx.test.uiautomator.UiDevice; 30 import androidx.test.uiautomator.UiObject2; 31 import androidx.test.uiautomator.UiObjectNotFoundException; 32 import androidx.test.uiautomator.UiSelector; 33 import androidx.test.uiautomator.Until; 34 import android.view.KeyEvent; 35 36 /** 37 * Methods for configuring lock screen settings 38 */ 39 public class LockScreenAutomation extends UiAutomatorTestCase { 40 41 private static final String SETTINGS_PACKAGE = "com.android.settings"; 42 43 private static final long TIMEOUT = 2000L; 44 45 private Context mContext; 46 private UiDevice mDevice; 47 setPin()48 public void setPin() throws Exception { 49 mContext = getInstrumentation().getContext(); 50 mDevice = UiDevice.getInstance(getInstrumentation()); 51 52 mDevice.wakeUp(); 53 mDevice.pressKeyCode(KeyEvent.KEYCODE_MENU); 54 mDevice.waitForIdle(TIMEOUT); 55 launchLockScreenSettings(); 56 57 PackageManager pm = mContext.getPackageManager(); 58 Resources res = pm.getResourcesForApplication(SETTINGS_PACKAGE); 59 60 int resId = res.getIdentifier("unlock_set_unlock_pin_title", "string", SETTINGS_PACKAGE); 61 findAndClick(By.text(res.getString(resId))); 62 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 63 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 64 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 65 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 66 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 67 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 68 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 69 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 70 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 71 mDevice.pressEnter(); 72 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 73 74 // Re-enter PIN 75 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 76 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 77 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 78 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 79 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 80 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 81 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 82 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 83 mDevice.pressEnter(); 84 85 findAndClick(By.res(SETTINGS_PACKAGE, "redact_sensitive")); 86 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 87 findAndClick(By.clazz("android.widget.Button")); 88 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 89 } 90 unlock()91 public void unlock() throws Exception { 92 mContext = getInstrumentation().getContext(); 93 mDevice = UiDevice.getInstance(getInstrumentation()); 94 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 95 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 96 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 97 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 98 mDevice.pressKeyCode(KeyEvent.KEYCODE_ENTER); 99 } 100 removePin()101 public void removePin() throws Exception { 102 mContext = getInstrumentation().getContext(); 103 mDevice = UiDevice.getInstance(getInstrumentation()); 104 105 mDevice.wakeUp(); 106 mDevice.pressKeyCode(KeyEvent.KEYCODE_MENU); 107 mDevice.waitForIdle(TIMEOUT); 108 launchLockScreenSettings(); 109 110 PackageManager pm = mContext.getPackageManager(); 111 Resources res = pm.getResourcesForApplication(SETTINGS_PACKAGE); 112 113 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 114 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 115 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 116 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 117 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 118 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 119 mDevice.pressKeyCode(KeyEvent.KEYCODE_0); 120 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 121 mDevice.pressEnter(); 122 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 123 124 int resId = res.getIdentifier("unlock_set_unlock_off_title", "string", SETTINGS_PACKAGE); 125 findAndClick(By.text(res.getString(resId))); 126 mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5); 127 128 findAndClick(By.res("android", "button1")); 129 mDevice.waitForIdle(TIMEOUT); 130 } 131 findAndClick(BySelector selector)132 private void findAndClick(BySelector selector) 133 { 134 for (int i = 0; i < 3; i++) { 135 mDevice.wait(Until.findObject(selector), TIMEOUT); 136 UiObject2 obj = mDevice.findObject(selector); 137 if (obj != null) { 138 obj.click(); 139 return; 140 } 141 } 142 Assert.fail("Could not find and click " + selector); 143 } 144 launchLockScreenSettings()145 private void launchLockScreenSettings() { 146 final Intent intent = new Intent().setClassName(SETTINGS_PACKAGE, "com.android.settings.password.ChooseLockGeneric"); 147 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 148 mContext.startActivity(intent); 149 mDevice.wait(Until.hasObject(By.pkg(SETTINGS_PACKAGE).depth(0)), TIMEOUT); 150 } 151 } 152