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 com.android.cts.deviceandprofileowner;
18 
19 import static com.android.cts.deviceandprofileowner.BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT;
20 
21 import static junit.framework.Assert.assertFalse;
22 import static junit.framework.Assert.assertTrue;
23 
24 import static org.junit.Assume.assumeTrue;
25 
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.pm.PackageManager;
31 import android.content.pm.ResolveInfo;
32 import android.os.Process;
33 
34 import androidx.test.InstrumentationRegistry;
35 import androidx.test.filters.RequiresDevice;
36 import androidx.test.runner.AndroidJUnit4;
37 import androidx.test.uiautomator.By;
38 import androidx.test.uiautomator.UiDevice;
39 import androidx.test.uiautomator.Until;
40 
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 
47 import java.util.List;
48 
49 // TODO(b/184280023): remove @RequiresDevice and @Ignores.
50 @RunWith(AndroidJUnit4.class)
51 public class SecondaryLockscreenTest {
52 
53     private static final int UI_AUTOMATOR_WAIT_TIME_MILLIS = 10000;
54     private static final String TAG = "SecondaryLockscreenTest";
55 
56     private Context mContext;
57     private DevicePolicyManager mDevicePolicyManager;
58     private UiDevice mUiDevice;
59 
60     @Before
setUp()61     public void setUp() throws Exception {
62         mContext = InstrumentationRegistry.getContext();
63         mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
64         mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
65 
66         assumeTrue(
67                 "Device does not support secure lock",
68                 mContext.getPackageManager().hasSystemFeature(
69                         PackageManager.FEATURE_SECURE_LOCK_SCREEN));
70 
71         // TODO(b/182994391): Replace with more generic solution to override the supervision
72         // component.
73         mUiDevice.executeShellCommand("settings put global device_policy_constants "
74                 + "use_test_admin_as_supervision_component=true");
75         mUiDevice.executeShellCommand("locksettings set-disabled false");
76         mUiDevice.executeShellCommand("locksettings set-pin 1234");
77 
78         mDevicePolicyManager.clearPackagePersistentPreferredActivities(ADMIN_RECEIVER_COMPONENT,
79                 mContext.getPackageName());
80 
81         assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle()));
82         mDevicePolicyManager.setSecondaryLockscreenEnabled(ADMIN_RECEIVER_COMPONENT, true);
83         assertTrue(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle()));
84     }
85 
86     @After
tearDown()87     public void tearDown() throws Exception {
88         mDevicePolicyManager.setSecondaryLockscreenEnabled(ADMIN_RECEIVER_COMPONENT, false);
89         assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle()));
90         mUiDevice.executeShellCommand("settings delete global device_policy_constants");
91         mUiDevice.executeShellCommand("locksettings clear --old 1234");
92         mUiDevice.executeShellCommand("locksettings set-disabled true");
93     }
94 
95     @Test
96     @Ignore("b/184280023")
testSetSecondaryLockscreenEnabled()97     public void testSetSecondaryLockscreenEnabled() throws Exception {
98         enterKeyguardPin();
99         assertTrue("Lockscreen title not shown",
100                 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)),
101                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
102 
103         mDevicePolicyManager.setSecondaryLockscreenEnabled(ADMIN_RECEIVER_COMPONENT, false);
104 
105         // Verify that the lockscreen is dismissed after disabling the feature
106         assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle()));
107         verifyHomeLauncherIsShown();
108     }
109 
110     @Test
111     @Ignore("b/184280023")
testHomeButton()112     public void testHomeButton() throws Exception {
113         enterKeyguardPin();
114         assertTrue("Lockscreen title not shown",
115                 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)),
116                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
117 
118         // Verify that pressing home does not dismiss the lockscreen
119         mUiDevice.pressHome();
120         verifySecondaryLockscreenIsShown();
121     }
122 
123     @Test
124     @Ignore("b/184280023")
testDismiss()125     public void testDismiss() throws Exception {
126         enterKeyguardPin();
127         assertTrue("Lockscreen title not shown",
128                 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)),
129                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
130 
131         mUiDevice.findObject(By.text(SimpleKeyguardService.DISMISS_BUTTON_LABEL)).click();
132         verifyHomeLauncherIsShown();
133 
134         // Verify that the feature is not disabled after dismissal
135         enterKeyguardPin();
136         assertTrue(mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)),
137                 UI_AUTOMATOR_WAIT_TIME_MILLIS));
138         verifySecondaryLockscreenIsShown();
139     }
140 
141     @RequiresDevice
142     @Test(expected = SecurityException.class)
testSetSecondaryLockscreen_ineligibleAdmin_throwsSecurityException()143     public void testSetSecondaryLockscreen_ineligibleAdmin_throwsSecurityException() {
144         final ComponentName badAdmin = new ComponentName("com.foo.bar", ".NonProfileOwnerReceiver");
145         mDevicePolicyManager.setSecondaryLockscreenEnabled(badAdmin, true);
146     }
147 
enterKeyguardPin()148     private void enterKeyguardPin() throws Exception {
149         mUiDevice.executeShellCommand("input keyevent KEYCODE_SLEEP");
150         mUiDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP");
151         assertTrue("Keyguard unexpectedly not shown",
152                 mUiDevice.wait(Until.hasObject(
153                         By.res("com.android.systemui", "keyguard_status_view")),
154                                 UI_AUTOMATOR_WAIT_TIME_MILLIS));
155         mUiDevice.executeShellCommand("wm dismiss-keyguard");
156         assertTrue("Keyguard pin entry unexpectedly not shown",
157                 mUiDevice.wait(Until.hasObject(By.res("com.android.systemui", "pinEntry")),
158                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
159         mUiDevice.executeShellCommand("input text 1234");
160         mUiDevice.executeShellCommand("input keyevent KEYCODE_ENTER");
161     }
162 
verifyHomeLauncherIsShown()163     private void verifyHomeLauncherIsShown() {
164         String launcherPackageName = getLauncherPackageName();
165         assertTrue("Lockscreen title is unexpectedly shown",
166                 mUiDevice.wait(Until.gone(By.text(SimpleKeyguardService.TITLE_LABEL)),
167                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
168         assertTrue(String.format("Launcher (%s) is not shown", launcherPackageName),
169                 mUiDevice.wait(Until.hasObject(By.pkg(launcherPackageName)),
170                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
171     }
172 
verifySecondaryLockscreenIsShown()173     private void verifySecondaryLockscreenIsShown() {
174         String launcherPackageName = getLauncherPackageName();
175         assertTrue("Lockscreen title is unexpectedly not shown",
176                 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)),
177                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
178         assertTrue(String.format("Launcher (%s) is unexpectedly shown", launcherPackageName),
179                 mUiDevice.wait(Until.gone(By.pkg(launcherPackageName)),
180                         UI_AUTOMATOR_WAIT_TIME_MILLIS));
181     }
182 
getLauncherPackageName()183     private String getLauncherPackageName() {
184         Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
185         List<ResolveInfo> resolveInfos = mContext.getPackageManager().queryIntentActivities(
186                 homeIntent, 0);
187         StringBuilder sb = new StringBuilder();
188         for (ResolveInfo resolveInfo : resolveInfos) {
189             sb.append(resolveInfo.activityInfo.packageName).append("/").append(
190                     resolveInfo.activityInfo.name).append(", ");
191         }
192         return resolveInfos.isEmpty() ? null : resolveInfos.get(0).activityInfo.packageName;
193     }
194 }
195