1 /* 2 * Copyright (C) 2016 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.android.boothelper; 18 19 import android.app.Activity; 20 import android.app.Instrumentation; 21 import android.app.admin.DevicePolicyManager; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.test.uiautomator.By; 28 import android.support.test.uiautomator.UiDevice; 29 import android.support.test.uiautomator.UiObject2; 30 import android.support.test.uiautomator.Until; 31 import android.util.Log; 32 import android.view.KeyEvent; 33 34 import java.util.concurrent.CountDownLatch; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 39 40 41 public class BootHelperTest { 42 43 private static final long TIMEOUT = 10000; 44 private static final String TAG = "BootHelperTest"; 45 private static final String SETTINGS_PKG = "com.android.settings"; 46 private static final String LOCK_PIN_ID = "lock_pin"; 47 private static final String REQUIRE_PWD_ID = "encrypt_dont_require_password"; 48 private static final String PWD_ENTRY = "password_entry"; 49 private UiDevice mDevice; 50 private Context mProtectedContext; 51 52 @Before 53 public void setUp() throws Exception { 54 mDevice = UiDevice.getInstance(getInstrumentation()); 55 mProtectedContext = getInstrumentation().getContext() 56 .createDeviceProtectedStorageContext(); 57 } 58 59 @Test 60 public void setupLockScreenPin() throws Exception { 61 Activity activity = launchActivity(getInstrumentation().getTargetContext() 62 .getPackageName(), AwareActivity.class, new Intent(Intent.ACTION_MAIN)); 63 mDevice.waitForIdle(); 64 65 // Set a PIN for this user 66 final Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); 67 intent.addCategory(Intent.CATEGORY_DEFAULT); 68 activity.startActivity(intent); 69 mDevice.waitForIdle(); 70 71 // Pick PIN from the option list 72 selectOption(LOCK_PIN_ID); 73 74 // Ignore any interstitial options 75 selectOption(REQUIRE_PWD_ID); 76 77 // Set our PIN 78 selectOption(PWD_ENTRY); 79 80 // Enter it twice to confirm 81 enterTestPin(); 82 enterTestPin(); 83 mDevice.pressBack(); 84 85 } 86 87 @Test 88 public void unlockScreenWithPin() throws Exception { 89 final CountDownLatch latch = new CountDownLatch(1); 90 final BroadcastReceiver receiver = new BroadcastReceiver() { 91 @Override 92 public void onReceive(Context context, Intent intent) { 93 latch.countDown(); 94 } 95 }; 96 mProtectedContext.registerReceiver(receiver, new IntentFilter( 97 Intent.ACTION_USER_UNLOCKED)); 98 dismissKeyguard(); 99 } 100 101 private void dismissKeyguard() throws Exception { 102 mDevice.wakeUp(); 103 mDevice.waitForIdle(); 104 mDevice.pressMenu(); 105 mDevice.waitForIdle(); 106 enterTestPin(); 107 } 108 109 private void enterTestPin() throws Exception { 110 mDevice.waitForIdle(); 111 mDevice.pressKeyCode(KeyEvent.KEYCODE_1); 112 mDevice.pressKeyCode(KeyEvent.KEYCODE_2); 113 mDevice.pressKeyCode(KeyEvent.KEYCODE_3); 114 mDevice.pressKeyCode(KeyEvent.KEYCODE_4); 115 mDevice.pressKeyCode(KeyEvent.KEYCODE_5); 116 mDevice.waitForIdle(); 117 mDevice.pressEnter(); 118 Log.i(TAG, "Screen Unlocked"); 119 mDevice.waitForIdle(); 120 } 121 122 /** 123 * Return the instrumentation from the registry. 124 * 125 * @return 126 */ 127 private Instrumentation getInstrumentation() { 128 return InstrumentationRegistry.getInstrumentation(); 129 } 130 131 /** 132 * Click on the option based on the resource id in the settings package. 133 * 134 * @param optionId 135 */ 136 public void selectOption(String optionId) { 137 UiObject2 tos = mDevice.wait(Until.findObject(By.res(SETTINGS_PKG, optionId)), 138 TIMEOUT); 139 if (tos != null) { 140 tos.click(); 141 } 142 } 143 144 /** 145 * To launch an activity 146 * @param pkg 147 * @param activityCls 148 * @param intent 149 * @return 150 */ 151 public Activity launchActivity(String pkg, Class activityCls, Intent intent) { 152 intent.setClassName(pkg, activityCls.getName()); 153 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 154 return getInstrumentation().startActivitySync(intent); 155 } 156 157 158 } 159