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