1 /*
2  * Copyright (C) 2017 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 android.os.UserManager.DISALLOW_AUTOFILL;
20 import static android.provider.Settings.Secure.AUTOFILL_SERVICE;
21 import static android.provider.Settings.Secure.USER_SETUP_COMPLETE;
22 
23 import android.content.Intent;
24 
25 public class AutofillRestrictionsTest extends BaseDeviceAdminTest {
26 
27     private static final String SERVICE_NAME =
28             "com.android.cts.devicepolicy.autofillapp/.SimpleAutofillService";
29     private static final String AUTOFILL_PACKAGE_NAME = "com.android.cts.devicepolicy.autofillapp";
30     private static final String AUTOFILL_ACTIVITY_NAME = AUTOFILL_PACKAGE_NAME + ".SimpleActivity";
31 
32     // Before, autofill_service was a cloned service, so it was only set in the default user,
33     // and we were using a guard to decide how to set it.
34     // Autofill_service has been changed now to be a per-user service so we are currently
35     // setting this to false.
36     private final boolean USES_CLONED_SETTINGS = false;
37 
38     int mUserId;
39 
40     @Override
setUp()41     protected void setUp() throws Exception {
42         super.setUp();
43 
44         mUserId = getInstrumentation().getContext().getUserId();
45     }
46 
47     @Override
tearDown()48     protected void tearDown() throws Exception {
49         try {
50             disableService();
51         } finally {
52             mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT, DISALLOW_AUTOFILL);
53         }
54         super.tearDown();
55     }
56 
testDisallowAutofill_allowed()57     public void testDisallowAutofill_allowed() throws Exception {
58         enableService();
59 
60         final boolean enabledBefore = launchActivityAndGetEnabled();
61         assertTrue(enabledBefore);
62 
63         mDevicePolicyManager.addUserRestriction(ADMIN_RECEIVER_COMPONENT, DISALLOW_AUTOFILL);
64 
65         // Must try a couple times because it will be disabled asynchronously.
66         for (int i = 1; i <= 15; i++) {
67             final boolean disabledAfter = !launchActivityAndGetEnabled();
68             if (disabledAfter) {
69                 return;
70             }
71             Thread.sleep(100);
72         }
73         fail("Not disabled after a period of time");
74     }
75 
launchActivityAndGetEnabled()76     private boolean launchActivityAndGetEnabled() throws Exception {
77         final Intent launchIntent = new Intent();
78         launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
79         launchIntent.setClassName(AUTOFILL_PACKAGE_NAME, AUTOFILL_ACTIVITY_NAME);
80         final AutofillActivity activity = launchActivity("com.android.cts.deviceandprofileowner",
81                 AutofillActivity.class, null);
82         return activity.isAutofillEnabled();
83     }
84 
enableService()85     private void enableService() throws Exception {
86         runShellCommand("settings put secure --user %d %s %d default",
87                 mUserId, USER_SETUP_COMPLETE, 1);
88 
89         if (USES_CLONED_SETTINGS) {
90             runShellCommand("settings put secure %s %s default", AUTOFILL_SERVICE, SERVICE_NAME);
91         } else {
92             runShellCommand("settings put --user %d secure %s %s default",
93                     mUserId, AUTOFILL_SERVICE, SERVICE_NAME);
94         }
95         waitForServiceSettingSaved(SERVICE_NAME);
96     }
97 
disableService()98     private void disableService() {
99         if (USES_CLONED_SETTINGS) {
100             runShellCommand("settings delete secure %s", AUTOFILL_SERVICE);
101         } else {
102             runShellCommand("settings delete --user %d secure %s", mUserId, AUTOFILL_SERVICE);
103         }
104     }
105 
waitForServiceSettingSaved(String expected)106     private void waitForServiceSettingSaved(String expected) throws Exception {
107         String actual = null;
108         // Wait up to 0.5 seconds until setting is saved.
109         for (int i = 0; i < 5; i++) {
110             if (USES_CLONED_SETTINGS) {
111                 actual = runShellCommand("settings get secure %s", AUTOFILL_SERVICE);
112             } else {
113                 actual = runShellCommand("settings get --user %d secure %s", mUserId,
114                         AUTOFILL_SERVICE);
115             }
116             if (expected.equals(actual)) {
117                 return;
118             }
119             Thread.sleep(100);
120         }
121         fail("Expected service status for user " + mUserId + ": " + expected
122                 + "; actual: " + actual + " after 0.5 seconds");
123     }
124 }
125