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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.content.ComponentName;
24 
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 public class VendorServiceInfoTest {
31     private static final int MAX_RETRIES = 23;
32     private static final String SERVICE_NAME = "com.andorid.car/.MyService";
33 
34     @Test
emptyString()35     public void emptyString() {
36         assertThrows(IllegalArgumentException.class, () -> VendorServiceInfo.parse(""));
37     }
38 
39     @Test
multipleHashTags()40     public void multipleHashTags() {
41         assertThrows(IllegalArgumentException.class,
42                 () -> VendorServiceInfo.parse(SERVICE_NAME + "#user=system#bind=bind"));
43     }
44 
45     @Test
unknownArg()46     public void unknownArg() {
47         assertThrows(IllegalArgumentException.class,
48                 () -> VendorServiceInfo.parse(SERVICE_NAME + "#user=system,unknownKey=blah"));
49     }
50 
51     @Test
invalidComponentName()52     public void invalidComponentName() {
53         assertThrows(IllegalArgumentException.class,
54                 () -> VendorServiceInfo.parse("invalidComponentName"));
55     }
56 
57     @Test
testParse_maxRetriesValueNotANumber()58     public void testParse_maxRetriesValueNotANumber() {
59         assertThrows(IllegalArgumentException.class,
60                 () -> VendorServiceInfo.parse(SERVICE_NAME + "#maxRetries=seven"));
61     }
62 
63     @Test
testServiceNameWithDefaults()64     public void testServiceNameWithDefaults() {
65         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME);
66 
67         assertThat(info.getIntent().getComponent())
68                 .isEqualTo(ComponentName.unflattenFromString(SERVICE_NAME));
69         assertThat(info.shouldBeBound()).isFalse();
70         assertThat(info.shouldBeStartedInForeground()).isFalse();
71         assertThat(info.isSystemUserService()).isTrue();
72         assertThat(info.isForegroundUserService()).isTrue();
73         assertThat(info.shouldStartOnUnlock()).isTrue();
74     }
75 
76     @Test
startService()77     public void startService() {
78         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#bind=start");
79 
80         assertThat(info.shouldBeBound()).isFalse();
81         assertThat(info.shouldBeStartedInForeground()).isFalse();
82         assertThat(info.getIntent().getComponent())
83                 .isEqualTo(ComponentName.unflattenFromString(SERVICE_NAME));
84     }
85 
86     @Test
bindService()87     public void bindService() {
88         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#bind=bind");
89 
90         assertThat(info.shouldBeBound()).isTrue();
91         assertThat(info.shouldBeStartedInForeground()).isFalse();
92     }
93 
94     @Test
startServiceInForeground()95     public void startServiceInForeground() {
96         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#bind=startForeground");
97 
98         assertThat(info.shouldBeBound()).isFalse();
99         assertThat(info.shouldBeStartedInForeground()).isTrue();
100     }
101 
102     @Test
triggerAsap()103     public void triggerAsap() {
104         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#trigger=asap");
105 
106         assertThat(info.shouldStartOnUnlock()).isFalse();
107     }
108 
109     @Test
triggerUnlocked()110     public void triggerUnlocked() {
111         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#trigger=userUnlocked");
112 
113         assertThat(info.shouldStartOnUnlock()).isTrue();
114     }
115 
116     @Test
triggerPostUnlocked()117     public void triggerPostUnlocked() {
118         VendorServiceInfo info = VendorServiceInfo.parse(
119                 SERVICE_NAME + "#trigger=userPostUnlocked");
120 
121         assertThat(info.shouldStartOnPostUnlock()).isTrue();
122     }
123 
124     @Test
triggerResume()125     public void triggerResume() {
126         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#trigger=resume");
127 
128         assertThat(info.shouldStartOnResume()).isTrue();
129     }
130 
131     @Test
triggerUnknown()132     public void triggerUnknown() {
133         assertThrows(IllegalArgumentException.class,
134                 () -> VendorServiceInfo.parse(SERVICE_NAME + "#trigger=whenever"));
135     }
136 
137     @Test
userScopeForeground()138     public void userScopeForeground() {
139         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=foreground");
140 
141         assertThat(info.isAllUserService()).isFalse();
142         assertThat(info.isForegroundUserService()).isTrue();
143         assertThat(info.isSystemUserService()).isFalse();
144         assertThat(info.isVisibleUserService()).isFalse();
145         assertThat(info.isBackgroundVisibleUserService()).isFalse();
146     }
147 
148     @Test
userScopeSystem()149     public void userScopeSystem() {
150         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=system");
151 
152         assertThat(info.isAllUserService()).isFalse();
153         assertThat(info.isForegroundUserService()).isFalse();
154         assertThat(info.isSystemUserService()).isTrue();
155         assertThat(info.isVisibleUserService()).isFalse();
156         assertThat(info.isBackgroundVisibleUserService()).isFalse();
157     }
158 
159     @Test
userScopeVisible()160     public void userScopeVisible() {
161         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=visible");
162 
163         assertThat(info.isAllUserService()).isFalse();
164         assertThat(info.isForegroundUserService()).isFalse();
165         assertThat(info.isSystemUserService()).isFalse();
166         assertThat(info.isVisibleUserService()).isTrue();
167         assertThat(info.isBackgroundVisibleUserService()).isFalse();
168     }
169 
170     @Test
userScopeBackgroundVisible()171     public void userScopeBackgroundVisible() {
172         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=backgroundVisible");
173 
174         assertThat(info.isAllUserService()).isFalse();
175         assertThat(info.isForegroundUserService()).isFalse();
176         assertThat(info.isSystemUserService()).isFalse();
177         assertThat(info.isVisibleUserService()).isFalse();
178         assertThat(info.isBackgroundVisibleUserService()).isTrue();
179     }
180 
181     @Test
userScopeAll()182     public void userScopeAll() {
183         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=all");
184 
185         assertThat(info.isAllUserService()).isTrue();
186         assertThat(info.isForegroundUserService()).isTrue();
187         assertThat(info.isSystemUserService()).isTrue();
188         assertThat(info.isVisibleUserService()).isTrue();
189         assertThat(info.isBackgroundVisibleUserService()).isTrue();
190     }
191 
192     @Test
userUnknown()193     public void userUnknown() {
194         assertThrows(IllegalArgumentException.class,
195                 () -> VendorServiceInfo.parse(SERVICE_NAME + "#user=whoever"));
196     }
197 
198     @Test
testGetMaxRetries()199     public void testGetMaxRetries() {
200         VendorServiceInfo info =
201                 VendorServiceInfo.parse(SERVICE_NAME + "#maxRetries=" + MAX_RETRIES);
202 
203         assertThat(info.getMaxRetries()).isEqualTo(MAX_RETRIES);
204     }
205 
206     @Test
testGetMaxRetries_defaultMaxRetries()207     public void testGetMaxRetries_defaultMaxRetries() {
208         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME);
209 
210         assertThat(info.getMaxRetries()).isEqualTo(VendorServiceInfo.DEFAULT_MAX_RETRIES);
211     }
212 
213     @Test
allArgs()214     public void allArgs() {
215         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME
216                 + "#bind=bind,user=foreground,trigger=userUnlocked,maxRetries=" + MAX_RETRIES);
217 
218         assertThat(info.getIntent().getComponent())
219                 .isEqualTo(ComponentName.unflattenFromString(SERVICE_NAME));
220         assertThat(info.shouldBeBound()).isTrue();
221         assertThat(info.isForegroundUserService()).isTrue();
222         assertThat(info.isSystemUserService()).isFalse();
223         assertThat(info.shouldStartOnUnlock()).isTrue();
224         assertThat(info.shouldStartAsap()).isFalse();
225         assertThat(info.getMaxRetries()).isEqualTo(MAX_RETRIES);
226     }
227 
228     @Test
testToString_bindForegroundUserPostUnlocked()229     public void testToString_bindForegroundUserPostUnlocked() {
230         String result = VendorServiceInfo.parse(SERVICE_NAME
231                 + "#bind=bind,user=backgroundVisible,trigger=asap").toString();
232 
233         assertThat(result).contains("component=" + SERVICE_NAME);
234         assertThat(result).contains("bind=BIND");
235         assertThat(result).contains("userScope=BACKGROUND_VISIBLE");
236         assertThat(result).contains("trigger=ASAP");
237     }
238 
239     @Test
testToString_bindBackgroundVisibleUserAsap()240     public void testToString_bindBackgroundVisibleUserAsap() {
241         String result = VendorServiceInfo.parse(SERVICE_NAME
242                 + "#bind=start,user=visible,trigger=userUnlocked").toString();
243 
244         assertThat(result).contains("component=" + SERVICE_NAME);
245         assertThat(result).contains("bind=START");
246         assertThat(result).contains("userScope=VISIBLE");
247         assertThat(result).contains("trigger=UNLOCKED");
248     }
249 
250     @Test
testToString_startVisibleUserUnlocked()251     public void testToString_startVisibleUserUnlocked() {
252         String result = VendorServiceInfo.parse(SERVICE_NAME
253                 + "#bind=start,user=visible,trigger=userUnlocked").toString();
254 
255         assertThat(result).contains("component=" + SERVICE_NAME);
256         assertThat(result).contains("bind=START");
257         assertThat(result).contains("userScope=VISIBLE");
258         assertThat(result).contains("trigger=UNLOCKED");
259     }
260 
261     @Test
testToString_startSystemUserResume()262     public void testToString_startSystemUserResume() {
263         String result = VendorServiceInfo.parse(SERVICE_NAME
264                 + "#bind=start,user=system,trigger=resume").toString();
265 
266         assertThat(result).contains("component=" + SERVICE_NAME);
267         assertThat(result).contains("bind=START");
268         assertThat(result).contains("userScope=SYSTEM");
269         assertThat(result).contains("trigger=RESUME");
270     }
271 
272     @Test
testToString_maxRetries()273     public void testToString_maxRetries() {
274         String result = VendorServiceInfo.parse(SERVICE_NAME + "#maxRetries=" + MAX_RETRIES)
275                 .toString();
276 
277         assertThat(result).contains("component=" + SERVICE_NAME);
278         assertThat(result).contains("maxRetries=" + MAX_RETRIES);
279     }
280 
281     @Test
testToString_defaultMaxRetries()282     public void testToString_defaultMaxRetries() {
283         String result = VendorServiceInfo.parse(SERVICE_NAME)
284                 .toString();
285 
286         assertThat(result).contains("component=" + SERVICE_NAME);
287         assertThat(result).doesNotContain("maxRetries=");
288     }
289 }
290