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