CarPackageManagerTest.java revision 9cea114837dd6a72d3a4f7f2618fec880fe1aca8
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 */
16package com.android.car.test;
17
18import android.car.Car;
19import android.car.CarNotConnectedException;
20import android.car.content.pm.AppBlockingPackageInfo;
21import android.car.content.pm.CarAppBlockingPolicy;
22import android.car.content.pm.CarPackageManager;
23import android.content.pm.PackageManager;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.util.Log;
26
27import com.android.car.test.MockedCarTestBase;
28import com.android.car.test.TestAppBlockingPolicyService;
29
30@SmallTest
31public class CarPackageManagerTest extends MockedCarTestBase {
32    private static final String TAG = CarPackageManagerTest.class.getSimpleName();
33
34    private static final int POLLING_MAX_RETRY = 10;
35    private static final long POLLING_SLEEP = 100;
36
37    private CarPackageManager mCarPm;
38    private PackageManager mPm;
39
40
41    @Override
42    protected void setUp() throws Exception {
43        super.setUp();
44        mPm = getContext().getPackageManager();
45
46    }
47
48    private void init(boolean policyFromService) throws Exception {
49        TestAppBlockingPolicyService.controlPolicySettingFromService(policyFromService);
50        getVehicleHalEmulator().start();
51        mCarPm = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE);
52        assertNotNull(mCarPm);
53    }
54
55    public void testServiceLaunched() throws Exception {
56        init(true);
57        assertTrue(pollingCheck(new PollingChecker() {
58            @Override
59            public boolean check() {
60                return TestAppBlockingPolicyService.getInstance() != null;
61            }
62        }, POLLING_MAX_RETRY, POLLING_SLEEP));
63        final String thisPackage = getContext().getPackageName();
64        final String serviceClassName = "DOES_NOT_MATTER";
65        assertTrue(pollingCheck(new PollingChecker() {
66            @Override
67            public boolean check() {
68                try {
69                    return mCarPm.isServiceAllowedWhileDriving(thisPackage, serviceClassName);
70                } catch (CarNotConnectedException e) {
71                    return false;
72                }
73            }
74        }, POLLING_MAX_RETRY, POLLING_SLEEP));
75        assertTrue(mCarPm.isServiceAllowedWhileDriving(thisPackage, null));
76        assertFalse(mCarPm.isServiceAllowedWhileDriving(serviceClassName, serviceClassName));
77        assertFalse(mCarPm.isServiceAllowedWhileDriving(serviceClassName, null));
78    }
79
80    public void testSettingWhitelist() throws Exception {
81        init(false);
82        final String carServicePackageName = "com.android.car";
83        final String activityAllowed = "NO_SUCH_ACTIVITY_BUT_ALLOWED";
84        final String activityNotAllowed = "NO_SUCH_ACTIVITY_AND_NOT_ALLOWED";
85        final String acticityAllowed2 = "NO_SUCH_ACTIVITY_BUT_ALLOWED2";
86        final String thisPackage = getContext().getPackageName();
87
88        AppBlockingPackageInfo info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
89                AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed });
90        CarAppBlockingPolicy policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
91                , null);
92        Log.i(TAG, "setting policy");
93        mCarPm.setAppBlockingPolicy(thisPackage, policy,
94                CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE);
95        Log.i(TAG, "setting policy done");
96        assertTrue(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed));
97        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName,
98                activityNotAllowed));
99
100        // replace policy
101        info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
102                AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { acticityAllowed2 });
103        policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
104                , null);
105        mCarPm.setAppBlockingPolicy(thisPackage, policy,
106                CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE);
107        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed));
108        assertTrue(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, acticityAllowed2));
109        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName,
110                activityNotAllowed));
111
112        //add, it replace the whole package policy. So activities are not added.
113        info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
114                AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed });
115        policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
116                , null);
117        mCarPm.setAppBlockingPolicy(thisPackage, policy,
118                CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE |
119                CarPackageManager.FLAG_SET_POLICY_ADD);
120        assertTrue(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed));
121        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, acticityAllowed2));
122        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName,
123                activityNotAllowed));
124
125        //remove
126        info = new AppBlockingPackageInfo(carServicePackageName, 0, 0,
127                AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed });
128        policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info }
129                , null);
130        mCarPm.setAppBlockingPolicy(thisPackage, policy,
131                CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE |
132                CarPackageManager.FLAG_SET_POLICY_REMOVE);
133        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed));
134        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, acticityAllowed2));
135        assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName,
136                activityNotAllowed));
137    }
138
139    public interface PollingChecker {
140        boolean check();
141    }
142
143    public static boolean pollingCheck(PollingChecker checker, int maxRetry, long sleepMs)
144            throws Exception {
145        int retry = 0;
146        boolean checked = checker.check();
147        while (!checked && (retry < maxRetry)) {
148            Thread.sleep(sleepMs);
149            retry++;
150            checked = checker.check();
151        }
152        return checked;
153    }
154}
155