• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.CarNotConnectedException;
24 import android.car.content.pm.AppBlockingPackageInfo;
25 import android.car.content.pm.CarAppBlockingPolicy;
26 import android.car.content.pm.CarPackageManager;
27 import android.support.test.filters.SmallTest;
28 import android.support.test.runner.AndroidJUnit4;
29 import android.util.Log;
30 
31 import com.android.car.pm.CarPackageManagerService;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 @RunWith(AndroidJUnit4.class)
37 @SmallTest
38 public class CarPackageManagerTest extends MockedCarTestBase {
39     private static final String TAG = CarPackageManagerTest.class.getSimpleName();
40 
41     private static final int POLLING_MAX_RETRY = 10;
42     private static final long POLLING_SLEEP = 100;
43 
44     private CarPackageManager mCarPm;
45     private CarPackageManagerService mCarPmService;
46 
init(boolean policyFromService)47     private void init(boolean policyFromService) throws Exception {
48         Log.i(TAG, "init started");
49         TestAppBlockingPolicyService.controlPolicySettingFromService(policyFromService);
50         mCarPm = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE);
51         assertNotNull(mCarPm);
52         mCarPmService = getPackageManagerService();
53         assertNotNull(mCarPmService);
54         mCarPmService.startAppBlockingPolicies();
55     }
56 
57     @Test
testServiceLaunched()58     public void testServiceLaunched() throws Exception {
59         init(true);
60         Log.i(TAG, "testServiceLaunched, init called");
61         assertTrue(pollingCheck(new PollingChecker() {
62             @Override
63             public boolean check() {
64                 Log.i(TAG, "checking instance ...");
65                 return TestAppBlockingPolicyService.getInstance() != null;
66             }
67         }, POLLING_MAX_RETRY, POLLING_SLEEP));
68         final String thisPackage = getContext().getPackageName();
69         final String serviceClassName = "DOES_NOT_MATTER";
70         assertTrue(pollingCheck(new PollingChecker() {
71             @Override
72             public boolean check() {
73                 try {
74                     return mCarPm.isServiceDistractionOptimized(thisPackage, serviceClassName);
75                 } catch (CarNotConnectedException e) {
76                     return false;
77                 }
78             }
79         }, POLLING_MAX_RETRY, POLLING_SLEEP));
80         assertTrue(mCarPm.isServiceDistractionOptimized(thisPackage, null));
81         assertFalse(mCarPm.isServiceDistractionOptimized(serviceClassName, serviceClassName));
82         assertFalse(mCarPm.isServiceDistractionOptimized(serviceClassName, null));
83     }
84 
85     @Test
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