summaryrefslogtreecommitdiff
path: root/tests/LockScreenAutomation.java
blob: afefa1cf3b0eabc006277411de5c9fc613897214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.lockscreenautomation;

import org.junit.Assert;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.provider.Settings;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.UiAutomatorTestCase;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.Until;
import android.view.KeyEvent;

/**
 * Methods for configuring lock screen settings
 */
public class LockScreenAutomation extends UiAutomatorTestCase {

    private static final String SETTINGS_PACKAGE = "com.android.settings";

    private static final long TIMEOUT = 2000L;

    private Context mContext;
    private UiDevice mDevice;

    public void setPin() throws Exception {
        mContext = getInstrumentation().getContext();
        mDevice = UiDevice.getInstance(getInstrumentation());

        mDevice.wakeUp();
        mDevice.pressKeyCode(KeyEvent.KEYCODE_MENU);
        mDevice.waitForIdle(TIMEOUT);
        launchLockScreenSettings();

        PackageManager pm = mContext.getPackageManager();
        Resources res = pm.getResourcesForApplication(SETTINGS_PACKAGE);

        int resId = res.getIdentifier("unlock_set_unlock_pin_title", "string", SETTINGS_PACKAGE);
        findAndClick(By.text(res.getString(resId)));
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressEnter();
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);

        // Re-enter PIN
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressEnter();

        findAndClick(By.res(SETTINGS_PACKAGE, "redact_sensitive"));
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        findAndClick(By.clazz("android.widget.Button"));
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
    }

    public void unlock() throws Exception {
        mContext = getInstrumentation().getContext();
        mDevice = UiDevice.getInstance(getInstrumentation());
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_ENTER);
    }

    public void removePin() throws Exception {
        mContext = getInstrumentation().getContext();
        mDevice = UiDevice.getInstance(getInstrumentation());

        mDevice.wakeUp();
        mDevice.pressKeyCode(KeyEvent.KEYCODE_MENU);
        mDevice.waitForIdle(TIMEOUT);
        launchLockScreenSettings();

        PackageManager pm = mContext.getPackageManager();
        Resources res = pm.getResourcesForApplication(SETTINGS_PACKAGE);

        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_0);
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);
        mDevice.pressEnter();
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);

        int resId = res.getIdentifier("unlock_set_unlock_off_title", "string", SETTINGS_PACKAGE);
        findAndClick(By.text(res.getString(resId)));
        mDevice.waitForWindowUpdate(SETTINGS_PACKAGE, 5);

        findAndClick(By.res("android", "button1"));
        mDevice.waitForIdle(TIMEOUT);
    }

    private void findAndClick(BySelector selector)
    {
        for (int i = 0; i < 3; i++) {
            mDevice.wait(Until.findObject(selector), TIMEOUT);
            UiObject2 obj = mDevice.findObject(selector);
            if (obj != null) {
                obj.click();
                return;
            }
        }
        Assert.fail("Could not find and click " + selector);
    }

    private void launchLockScreenSettings() {
        final Intent intent = new Intent().setClassName(SETTINGS_PACKAGE, "com.android.settings.password.ChooseLockGeneric");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        mContext.startActivity(intent);
        mDevice.wait(Until.hasObject(By.pkg(SETTINGS_PACKAGE).depth(0)), TIMEOUT);
    }
}