1 /*
2  * Copyright (C) 2021 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 android.car.test.mocks;
18 
19 import static android.car.test.mocks.CarArgumentMatchers.intentFor;
20 import static android.car.test.mocks.CarArgumentMatchers.isProperty;
21 import static android.car.test.mocks.CarArgumentMatchers.isPropertyWithValues;
22 import static android.car.test.mocks.CarArgumentMatchers.isUserHandle;
23 import static android.car.test.mocks.CarArgumentMatchers.isUserInfo;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.car.test.util.UserTestingHelper;
31 import android.content.Intent;
32 import android.content.pm.UserInfo;
33 import android.hardware.automotive.vehicle.RawPropValues;
34 import android.hardware.automotive.vehicle.StatusCode;
35 import android.hardware.automotive.vehicle.VehiclePropValue;
36 import android.os.UserHandle;
37 import android.os.UserManager;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
44 
45 @RunWith(MockitoJUnitRunner.class)
46 public final class CarArgumentMatchersTest {
47 
48     private static final int VEHICLE_PROP_NAME = 42;
49     private static final int VEHICLE_PROP_VALUE = 100;
50     private static final int USER_ID = 11;
51 
52     private static final String INTENT_PACKAGE_NAME =
53             "android.car.test.mocks/.CarArgumentMatchersTest";
54 
55     private FakeService mFakeService;
56 
57     @Mock
58     private UserManager mUserManager;
59 
60     @Mock
61     private IVehicle mIVehicle;
62 
63     @Mock
64     private IntentFirer mIntentFirer;
65 
66     @Mock
67     private UserInfoCheck mUserInfoCheck;
68 
69     @Before
setUp()70     public void setUp() {
71         mFakeService = new FakeService(mUserManager, mIVehicle, mIntentFirer, mUserInfoCheck);
72     }
73 
74     @Test
testIsUserInfo()75     public void testIsUserInfo() {
76         UserTestingHelper.UserInfoBuilder userInfoBuilder =
77                 new UserTestingHelper.UserInfoBuilder(USER_ID);
78 
79         mFakeService.setUserInfo(userInfoBuilder.build());
80 
81         verify(mUserInfoCheck).setUserInfo(isUserInfo(USER_ID));
82     }
83 
84     @Test
testIsUserHandle()85     public void testIsUserHandle() {
86         when(mUserManager.isUserUnlockingOrUnlocked(
87                 isUserHandle(UserHandle.USER_SYSTEM))).thenReturn(true);
88 
89         assertThat(mFakeService.setUserHandle(UserHandle.SYSTEM)).isTrue();
90         verify(mUserManager).isUserUnlockingOrUnlocked(UserHandle.SYSTEM);
91     }
92 
93     @Test
testIntentFor()94     public void testIntentFor() {
95         Intent intent = new Intent(Intent.ACTION_SEND).setPackage(INTENT_PACKAGE_NAME);
96 
97         mFakeService.fireIntent(intent);
98 
99         verify(mIntentFirer).fireIntent(
100                 intentFor(Intent.ACTION_SEND, INTENT_PACKAGE_NAME));
101     }
102 
103     @Test
testIsProperty()104     public void testIsProperty() throws Exception {
105         when(mIVehicle.set(isProperty(VEHICLE_PROP_NAME))).thenReturn(StatusCode.OK);
106 
107         VehiclePropValue prop = new VehiclePropValue();
108         prop.prop = VEHICLE_PROP_NAME;
109         prop.value = new RawPropValues();
110         int actualStatusCode = mFakeService.setVehiclePropValue(prop);
111 
112         assertThat(actualStatusCode).isEqualTo(StatusCode.OK);
113         verify(mIVehicle).set(prop);
114     }
115 
116     @Test
testIsPropertyWithValues()117     public void testIsPropertyWithValues() throws Exception {
118         when(mIVehicle.set(isPropertyWithValues(VEHICLE_PROP_NAME, VEHICLE_PROP_VALUE))).thenReturn(
119                 StatusCode.OK);
120 
121         VehiclePropValue prop = new VehiclePropValue();
122         prop.prop = VEHICLE_PROP_NAME;
123         prop.value = new RawPropValues();
124         prop.value.int32Values = new int[]{VEHICLE_PROP_VALUE};
125         int actualStatusCode = mFakeService.setVehiclePropValue(prop);
126 
127         assertThat(actualStatusCode).isEqualTo(StatusCode.OK);
128         verify(mIVehicle).set(prop);
129     }
130 
131     private static class FakeService {
132 
133         private final UserManager mUserManager;
134         private final IVehicle mIVehicle;
135         private final IntentFirer mIntentFirer;
136         private final UserInfoCheck mUserInfoCheck;
137 
FakeService(UserManager userManager, IVehicle iVehicle, IntentFirer intentFirer, UserInfoCheck userInfoCheck)138         FakeService(UserManager userManager, IVehicle iVehicle,
139                 IntentFirer intentFirer, UserInfoCheck userInfoCheck) {
140             mUserManager = userManager;
141             mIVehicle = iVehicle;
142             mIntentFirer = intentFirer;
143             mUserInfoCheck = userInfoCheck;
144         }
145 
setUserHandle(UserHandle userHandle)146         public boolean setUserHandle(UserHandle userHandle) {
147             return mUserManager.isUserUnlockingOrUnlocked(userHandle);
148         }
149 
setVehiclePropValue(VehiclePropValue vehicleProp)150         public int setVehiclePropValue(VehiclePropValue vehicleProp) throws Exception {
151             return mIVehicle.set(vehicleProp);
152         }
153 
fireIntent(Intent intent)154         public void fireIntent(Intent intent) {
155             mIntentFirer.fireIntent(intent);
156         }
157 
setUserInfo(UserInfo userInfo)158         public void setUserInfo(UserInfo userInfo) {
159             mUserInfoCheck.setUserInfo(userInfo);
160         }
161     }
162 
163     private interface IntentFirer {
fireIntent(Intent intent)164         void fireIntent(Intent intent);
165     }
166 
167     private interface UserInfoCheck {
setUserInfo(UserInfo userInfo)168         void setUserInfo(UserInfo userInfo);
169     }
170 
171     private interface IVehicle {
set(VehiclePropValue value)172         int set(VehiclePropValue value);
173     }
174 }
175