1 /* 2 * Copyright (C) 2019 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_CONTENT_SUGGESTIONS; 20 21 import static com.android.cts.deviceandprofileowner.ContentSuggestionsActivity.CONTENT_SUGGESTIONS_ACTIVITY_NAME; 22 import static com.android.cts.deviceandprofileowner.ContentSuggestionsActivity.CONTENT_SUGGESTIONS_PACKAGE_NAME; 23 24 import android.content.Intent; 25 26 public class ContentSuggestionsRestrictionsTest extends BaseDeviceAdminTest { 27 28 private static final int MAX_TIME_TEMPORARY_SERVICE_CAN_BE_SET= 12000; 29 30 private static final int SLEEP_TIME_WAITING_FOR_SERVICE_CONNECTION_MS = 100; 31 32 private static final String SERVICE_NAME = 33 "com.android.cts.devicepolicy.contentsuggestions/.SimpleContentSuggestionsService"; 34 35 int mUserId; 36 37 @Override 38 protected void setUp() throws Exception { 39 super.setUp(); 40 41 mUserId = getInstrumentation().getContext().getUserId(); 42 } 43 44 @Override 45 protected void tearDown() throws Exception { 46 try { 47 disableService(); 48 } finally { 49 mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT, 50 DISALLOW_CONTENT_SUGGESTIONS); 51 } 52 super.tearDown(); 53 } 54 55 public void testDisallowContentSuggestions_allowed() throws Exception { 56 enableService(); 57 58 final boolean enabledBefore = launchActivityAndGetEnabled(); 59 assertTrue(enabledBefore); 60 61 mDevicePolicyManager.addUserRestriction( 62 ADMIN_RECEIVER_COMPONENT, DISALLOW_CONTENT_SUGGESTIONS); 63 64 // Must try a couple times because it will be disabled asynchronously. 65 for (int i = 1; i <= 5; i++) { 66 final boolean disabledAfter = !launchActivityAndGetEnabled(); 67 if (disabledAfter) { 68 return; 69 } 70 Thread.sleep(100); 71 } 72 fail("Not disabled after 2.5s"); 73 } 74 75 private boolean launchActivityAndGetEnabled() throws Exception { 76 final Intent launchIntent = new Intent(); 77 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 78 launchIntent.setClassName( 79 CONTENT_SUGGESTIONS_PACKAGE_NAME, CONTENT_SUGGESTIONS_ACTIVITY_NAME); 80 final ContentSuggestionsActivity activity = launchActivity( 81 "com.android.cts.deviceandprofileowner", ContentSuggestionsActivity.class, null); 82 return activity.isContentSuggestionsEnabled(); 83 } 84 85 private void enableService() throws Exception { 86 runShellCommand("cmd content_suggestions set temporary-service %d %s %d", mUserId, 87 SERVICE_NAME, MAX_TIME_TEMPORARY_SERVICE_CAN_BE_SET); 88 // TODO: ideally it should wait until the service's onConnected() is called, but that 89 // would be too complicated 90 Thread.sleep(SLEEP_TIME_WAITING_FOR_SERVICE_CONNECTION_MS); 91 } 92 93 private void disableService() { 94 runShellCommand("cmd content_suggestions set temporary-service %d", mUserId); 95 } 96 } 97