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;
17 
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.car.Car;
23 import android.car.content.pm.AppBlockingPackageInfo;
24 import android.car.content.pm.CarAppBlockingPolicy;
25 import android.car.content.pm.CarPackageManager;
26 import android.util.Log;
27 
28 import androidx.test.ext.junit.runners.AndroidJUnit4;
29 import androidx.test.filters.FlakyTest;
30 import androidx.test.filters.SmallTest;
31 import androidx.test.filters.Suppress;
32 
33 import com.android.car.pm.CarPackageManagerService;
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         assertNotNull(mCarPm);
54         mCarPmService = getPackageManagerService();
55         assertNotNull(mCarPmService);
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         assertTrue(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));
70         final String thisPackage = getContext().getPackageName();
71         final String serviceClassName = "DOES_NOT_MATTER";
72         assertTrue(pollingCheck(
73                 () -> mCarPm.isServiceDistractionOptimized(thisPackage, serviceClassName),
74                 POLLING_MAX_RETRY,
75                 POLLING_SLEEP));
76         assertTrue(mCarPm.isServiceDistractionOptimized(thisPackage, null));
77         assertFalse(mCarPm.isServiceDistractionOptimized(serviceClassName, serviceClassName));
78         assertFalse(mCarPm.isServiceDistractionOptimized(serviceClassName, null));
79     }
80 
81     // TODO(b/113531788): Suppress this temporarily. Need to find the cause of issue and re-evaluate
82     // if the test is necessary.
83     @Suppress
84     @Test
85     @FlakyTest
testSettingWhitelist()86     public void testSettingWhitelist() throws Exception {
87         init(false);
88         final String carServicePackageName = "com.android.car";
89         final String activityAllowed = "NO_SUCH_ACTIVITY_BUT_ALLOWED";
90         final String activityNotAllowed = "NO_SUCH_ACTIVITY_AND_NOT_ALLOWED";
91         final String acticityAllowed2 = "NO_SUCH_ACTIVITY_BUT_ALLOWED2";
92         final String thisPackage = getContext().getPackageName();
93 
94         AppBlockingPackageInfo info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
95                 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed });
96         CarAppBlockingPolicy policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
97                 , null);
98         Log.i(TAG, "setting policy");
99         mCarPm.setAppBlockingPolicy(thisPackage, policy,
100                 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE);
101         Log.i(TAG, "setting policy done");
102         assertTrue(mCarPm.isActivityDistractionOptimized(carServicePackageName, activityAllowed));
103         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName,
104                 activityNotAllowed));
105 
106         // replace policy
107         info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
108                 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { acticityAllowed2 });
109         policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
110                 , null);
111         mCarPm.setAppBlockingPolicy(thisPackage, policy,
112                 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE);
113         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName, activityAllowed));
114         assertTrue(mCarPm.isActivityDistractionOptimized(carServicePackageName, acticityAllowed2));
115         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName,
116                 activityNotAllowed));
117 
118         //add, it replace the whole package policy. So activities are not added.
119         info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
120                 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed });
121         policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
122                 , null);
123         mCarPm.setAppBlockingPolicy(thisPackage, policy,
124                 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE |
125                 CarPackageManager.FLAG_SET_POLICY_ADD);
126         assertTrue(mCarPm.isActivityDistractionOptimized(carServicePackageName, activityAllowed));
127         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName, acticityAllowed2));
128         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName,
129                 activityNotAllowed));
130 
131         //remove
132         info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
133                 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed });
134         policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
135                 , null);
136         mCarPm.setAppBlockingPolicy(thisPackage, policy,
137                 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE |
138                 CarPackageManager.FLAG_SET_POLICY_REMOVE);
139         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName, activityAllowed));
140         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName, acticityAllowed2));
141         assertFalse(mCarPm.isActivityDistractionOptimized(carServicePackageName,
142                 activityNotAllowed));
143     }
144 
145     interface PollingChecker {
check()146         boolean check();
147     }
148 
pollingCheck(PollingChecker checker, int maxRetry, long sleepMs)149     static boolean pollingCheck(PollingChecker checker, int maxRetry, long sleepMs)
150             throws Exception {
151         int retry = 0;
152         boolean checked = checker.check();
153         while (!checked && (retry < maxRetry)) {
154             Thread.sleep(sleepMs);
155             retry++;
156             checked = checker.check();
157         }
158         return checked;
159     }
160 }
161