• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.ComponentName;
22 
23 import androidx.test.runner.AndroidJUnit4;
24 
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 
30 @RunWith(AndroidJUnit4.class)
31 public class VendorServiceInfoTest {
32     private static final String SERVICE_NAME = "com.andorid.car/.MyService";
33 
34     @Rule
35     public ExpectedException exception = ExpectedException.none();
36 
37     @Test
emptyString()38     public void emptyString() {
39         exception.expect(IllegalArgumentException.class);
40         VendorServiceInfo.parse("");
41     }
42 
43     @Test
multipleHashTags()44     public void multipleHashTags() {
45         exception.expect(IllegalArgumentException.class);
46         VendorServiceInfo.parse(SERVICE_NAME + "#user=system#bind=bind");
47     }
48 
49     @Test
unknownArg()50     public void unknownArg() {
51         exception.expect(IllegalArgumentException.class);
52         VendorServiceInfo.parse(SERVICE_NAME + "#user=system,unknownKey=blah");
53     }
54 
55     @Test
invalidComponentName()56     public void invalidComponentName() {
57         exception.expect(IllegalArgumentException.class);
58         VendorServiceInfo.parse("invalidComponentName");
59     }
60 
61     @Test
testServiceNameWithDefaults()62     public void testServiceNameWithDefaults() {
63         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME);
64         assertThat(info.getIntent().getComponent())
65                 .isEqualTo(ComponentName.unflattenFromString(SERVICE_NAME));
66         assertThat(info.shouldBeBound()).isFalse();
67         assertThat(info.shouldBeStartedInForeground()).isFalse();
68         assertThat(info.isSystemUserService()).isTrue();
69         assertThat(info.isForegroundUserService()).isTrue();
70         assertThat(info.shouldStartOnUnlock()).isTrue();
71     }
72 
73     @Test
startService()74     public void startService() {
75         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#bind=start");
76         assertThat(info.shouldBeBound()).isFalse();
77         assertThat(info.shouldBeStartedInForeground()).isFalse();
78         assertThat(info.getIntent().getComponent())
79                 .isEqualTo(ComponentName.unflattenFromString(SERVICE_NAME));
80     }
81 
82     @Test
bindService()83     public void bindService() {
84         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#bind=bind");
85         assertThat(info.shouldBeBound()).isTrue();
86         assertThat(info.shouldBeStartedInForeground()).isFalse();
87     }
88 
89     @Test
startServiceInForeground()90     public void startServiceInForeground() {
91         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#bind=startForeground");
92         assertThat(info.shouldBeBound()).isFalse();
93         assertThat(info.shouldBeStartedInForeground()).isTrue();
94     }
95 
96     @Test
triggerAsap()97     public void triggerAsap() {
98         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#trigger=asap");
99         assertThat(info.shouldStartOnUnlock()).isFalse();
100     }
101 
102     @Test
triggerUnlocked()103     public void triggerUnlocked() {
104         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#trigger=userUnlocked");
105         assertThat(info.shouldStartOnUnlock()).isTrue();
106     }
107 
108     @Test
triggerUnknown()109     public void triggerUnknown() {
110         exception.expect(IllegalArgumentException.class);
111         VendorServiceInfo.parse(SERVICE_NAME + "#trigger=whenever");
112     }
113 
114     @Test
userScopeForeground()115     public void userScopeForeground() {
116         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=foreground");
117         assertThat(info.isForegroundUserService()).isTrue();
118         assertThat(info.isSystemUserService()).isFalse();
119     }
120 
121     @Test
userScopeSystem()122     public void userScopeSystem() {
123         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=system");
124         assertThat(info.isForegroundUserService()).isFalse();
125         assertThat(info.isSystemUserService()).isTrue();
126     }
127 
128     @Test
userScopeAll()129     public void userScopeAll() {
130         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME + "#user=all");
131         assertThat(info.isForegroundUserService()).isTrue();
132         assertThat(info.isSystemUserService()).isTrue();
133     }
134 
135     @Test
userUnknown()136     public void userUnknown() {
137         exception.expect(IllegalArgumentException.class);
138         VendorServiceInfo.parse(SERVICE_NAME + "#user=whoever");
139     }
140 
141     @Test
allArgs()142     public void allArgs() {
143         VendorServiceInfo info = VendorServiceInfo.parse(SERVICE_NAME
144                 + "#bind=bind,user=foreground,trigger=userUnlocked");
145         assertThat(info.getIntent().getComponent())
146                 .isEqualTo(ComponentName.unflattenFromString(SERVICE_NAME));
147         assertThat(info.shouldBeBound()).isTrue();
148         assertThat(info.isForegroundUserService()).isTrue();
149         assertThat(info.isSystemUserService()).isFalse();
150         assertThat(info.shouldStartOnUnlock()).isTrue();
151         assertThat(info.shouldStartAsap()).isFalse();
152     }
153 }
154