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.car.pm;
18 
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertTrue;
21 
22 import android.content.Context;
23 
24 import androidx.test.ext.junit.runners.AndroidJUnit4;
25 import androidx.test.platform.app.InstrumentationRegistry;
26 
27 import com.android.car.CarUxRestrictionsManagerService;
28 import com.android.car.SystemActivityMonitoringService;
29 import com.android.car.user.CarUserService;
30 
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnit;
37 import org.mockito.junit.MockitoRule;
38 
39 import java.util.HashMap;
40 import java.util.Map;
41 import java.util.Set;
42 
43 /**
44  * Tests for {@link CarPackageManagerService}.
45  */
46 @RunWith(AndroidJUnit4.class)
47 public class CarPackageManagerServiceTest {
48     CarPackageManagerService mService;
49 
50     @Rule
51     public final MockitoRule rule = MockitoJUnit.rule();
52 
53     private Context mContext;
54     @Mock
55     private CarUxRestrictionsManagerService mMockUxrService;
56     @Mock
57     private SystemActivityMonitoringService mMockSamService;
58     @Mock
59     private CarUserService mMockUserService;
60 
61     @Before
setUp()62     public void setUp() {
63         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
64 
65         mService = new CarPackageManagerService(mContext, mMockUxrService, mMockSamService,
66                 mMockUserService);
67     }
68 
69     @Test
testParseConfigList_SingleActivity()70     public void testParseConfigList_SingleActivity() {
71         String config = "com.android.test/.TestActivity";
72         Map<String, Set<String>> map = new HashMap<>();
73 
74         mService.parseConfigList(config, map);
75 
76         assertTrue(map.get("com.android.test").size() == 1);
77         assertEquals(".TestActivity", map.get("com.android.test").iterator().next());
78     }
79 
80     @Test
testParseConfigList_Package()81     public void testParseConfigList_Package() {
82         String config = "com.android.test";
83         Map<String, Set<String>> map = new HashMap<>();
84 
85         mService.parseConfigList(config, map);
86 
87         assertTrue(map.get("com.android.test").size() == 0);
88     }
89 
90     @Test
testParseConfigList_MultipleActivities()91     public void testParseConfigList_MultipleActivities() {
92         String config = "com.android.test/.TestActivity0,com.android.test/.TestActivity1";
93         Map<String, Set<String>> map = new HashMap<>();
94 
95         mService.parseConfigList(config, map);
96 
97         assertTrue(map.get("com.android.test").size() == 2);
98         assertTrue(map.get("com.android.test").contains(".TestActivity0"));
99         assertTrue(map.get("com.android.test").contains(".TestActivity1"));
100     }
101 
102     @Test
testParseConfigList_PackageAndActivity()103     public void testParseConfigList_PackageAndActivity() {
104         String config = "com.android.test/.TestActivity0,com.android.test";
105         Map<String, Set<String>> map = new HashMap<>();
106 
107         mService.parseConfigList(config, map);
108 
109         assertTrue(map.get("com.android.test").size() == 0);
110     }
111 }
112