1 /* 2 * Copyright (C) 2015 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 package com.android.car.pm; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import android.car.Car; 21 import android.car.content.pm.AppBlockingPackageInfo; 22 import android.car.content.pm.CarAppBlockingPolicy; 23 import android.car.content.pm.CarPackageManager; 24 import android.util.Log; 25 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 import androidx.test.filters.FlakyTest; 28 import androidx.test.filters.SmallTest; 29 import androidx.test.filters.Suppress; 30 31 import com.android.car.CarLocalServices; 32 import com.android.car.MockedCarTestBase; 33 import com.android.car.TestAppBlockingPolicyService; 34 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 @RunWith(AndroidJUnit4.class) 39 @SmallTest 40 public class CarPackageManagerTest extends MockedCarTestBase { 41 private static final String TAG = CarPackageManagerTest.class.getSimpleName(); 42 43 private static final int POLLING_MAX_RETRY = 10; 44 private static final long POLLING_SLEEP = 100; 45 46 private CarPackageManager mCarPm; 47 private CarPackageManagerService mCarPmService; 48 init(boolean policyFromService)49 private void init(boolean policyFromService) throws Exception { 50 Log.i(TAG, "init started"); 51 TestAppBlockingPolicyService.controlPolicySettingFromService(policyFromService); 52 mCarPm = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE); 53 assertThat(mCarPm).isNotNull(); 54 mCarPmService = CarLocalServices.getService(CarPackageManagerService.class); 55 assertThat(mCarPmService).isNotNull(); 56 mCarPmService.startAppBlockingPolicies(); 57 } 58 59 @Test testServiceLaunched()60 public void testServiceLaunched() throws Exception { 61 init(true); 62 Log.i(TAG, "testServiceLaunched, init called"); 63 assertThat(pollingCheck(new PollingChecker() { 64 @Override 65 public boolean check() { 66 Log.i(TAG, "checking instance ..."); 67 return TestAppBlockingPolicyService.getInstance() != null; 68 } 69 }, POLLING_MAX_RETRY, POLLING_SLEEP)).isTrue(); 70 final String thisPackage = getContext().getPackageName(); 71 final String serviceClassName = "DOES_NOT_MATTER"; 72 assertThat(pollingCheck( 73 () -> mCarPm.isServiceDistractionOptimized(thisPackage, serviceClassName), 74 POLLING_MAX_RETRY, 75 POLLING_SLEEP)).isTrue(); 76 assertThat(mCarPm.isServiceDistractionOptimized(thisPackage, null)).isTrue(); 77 assertThat(mCarPm.isServiceDistractionOptimized(serviceClassName, 78 serviceClassName)).isFalse(); 79 assertThat(mCarPm.isServiceDistractionOptimized(serviceClassName, null)).isFalse(); 80 } 81 82 // TODO(b/113531788): Suppress this temporarily. Need to find the cause of issue and re-evaluate 83 // if the test is necessary. 84 @Suppress 85 @Test 86 @FlakyTest testSettingAllowlist()87 public void testSettingAllowlist() throws Exception { 88 init(false); 89 final String carServicePackageName = "com.android.car"; 90 final String activityAllowed = "NO_SUCH_ACTIVITY_BUT_ALLOWED"; 91 final String activityNotAllowed = "NO_SUCH_ACTIVITY_AND_NOT_ALLOWED"; 92 final String acticityAllowed2 = "NO_SUCH_ACTIVITY_BUT_ALLOWED2"; 93 final String thisPackage = getContext().getPackageName(); 94 95 AppBlockingPackageInfo info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 96 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed }); 97 CarAppBlockingPolicy policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 98 , null); 99 Log.i(TAG, "setting policy"); 100 mCarPm.setAppBlockingPolicy(thisPackage, policy, 101 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE); 102 Log.i(TAG, "setting policy done"); 103 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 104 activityAllowed)).isTrue(); 105 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 106 activityNotAllowed)).isFalse(); 107 108 // replace policy 109 info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 110 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { acticityAllowed2 }); 111 policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 112 , null); 113 mCarPm.setAppBlockingPolicy(thisPackage, policy, 114 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE); 115 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 116 activityAllowed)).isFalse(); 117 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 118 acticityAllowed2)).isTrue(); 119 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 120 activityNotAllowed)).isFalse(); 121 122 //add, it replace the whole package policy. So activities are not added. 123 info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 124 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed }); 125 policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 126 , null); 127 mCarPm.setAppBlockingPolicy(thisPackage, policy, 128 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE | 129 CarPackageManager.FLAG_SET_POLICY_ADD); 130 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 131 activityAllowed)).isTrue(); 132 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 133 acticityAllowed2)).isFalse(); 134 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 135 activityNotAllowed)).isFalse(); 136 137 //remove 138 info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 139 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed }); 140 policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 141 , null); 142 mCarPm.setAppBlockingPolicy(thisPackage, policy, 143 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE | 144 CarPackageManager.FLAG_SET_POLICY_REMOVE); 145 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 146 activityAllowed)).isFalse(); 147 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 148 acticityAllowed2)).isFalse(); 149 assertThat(mCarPm.isActivityDistractionOptimized(carServicePackageName, 150 activityNotAllowed)).isFalse(); 151 } 152 153 interface PollingChecker { check()154 boolean check(); 155 } 156 pollingCheck(PollingChecker checker, int maxRetry, long sleepMs)157 static boolean pollingCheck(PollingChecker checker, int maxRetry, long sleepMs) 158 throws Exception { 159 int retry = 0; 160 boolean checked = checker.check(); 161 while (!checked && (retry < maxRetry)) { 162 Thread.sleep(sleepMs); 163 retry++; 164 checked = checker.check(); 165 } 166 return checked; 167 } 168 } 169