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 android.car;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.car.drivingstate.CarUxRestrictions;
26 import android.car.drivingstate.CarUxRestrictionsManager;
27 import android.car.drivingstate.CarUxRestrictionsManager.OnUxRestrictionsChangedListener;
28 import android.car.testapi.CarUxRestrictionsController;
29 import android.car.testapi.FakeCar;
30 import android.content.Context;
31 import android.os.RemoteException;
32 import android.os.UserManager;
33 import android.view.Display;
34 
35 import androidx.test.core.app.ApplicationProvider;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.junit.MockitoJUnit;
44 import org.mockito.junit.MockitoRule;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.annotation.internal.DoNotInstrument;
47 
48 @RunWith(RobolectricTestRunner.class)
49 @DoNotInstrument
50 public class CarUxRestrictionsManagerTest {
51     @Rule
52     public final MockitoRule rule = MockitoJUnit.rule();
53 
54     @Spy
55     private final Context mContext = ApplicationProvider.getApplicationContext();
56 
57     @Mock
58     private UserManager mUserManager;
59 
60     private CarUxRestrictionsManager mCarUxRestrictionsManager;
61     private CarUxRestrictionsController mCarUxRestrictionsController;
62 
63     @Mock
64     OnUxRestrictionsChangedListener mListener;
65 
66     @Before
setUp()67     public void setUp() {
68         FakeCar fakeCar = FakeCar.createFakeCar(mContext);
69         Car carApi = fakeCar.getCar();
70 
71         mCarUxRestrictionsManager =
72                 (CarUxRestrictionsManager) carApi.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE);
73         mCarUxRestrictionsController = fakeCar.getCarUxRestrictionController();
74 
75         when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
76         when(mUserManager.getMainDisplayIdAssignedToUser()).thenReturn(Display.DEFAULT_DISPLAY);
77     }
78 
79     @Test
getRestrictions_noRestrictionsSet_noRestrictionsPresent()80     public void getRestrictions_noRestrictionsSet_noRestrictionsPresent() {
81         assertThat(mCarUxRestrictionsManager.getCurrentCarUxRestrictions().getActiveRestrictions())
82                 .isEqualTo(CarUxRestrictions.UX_RESTRICTIONS_BASELINE);
83     }
84 
85     @Test
setUxRestrictions_restrictionsRegistered()86     public void setUxRestrictions_restrictionsRegistered() throws RemoteException {
87         mCarUxRestrictionsController.setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO);
88 
89         assertThat(mCarUxRestrictionsManager.getCurrentCarUxRestrictions().getActiveRestrictions())
90                 .isEqualTo(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO);
91     }
92 
93     @Test
clearUxRestrictions_restrictionsCleared()94     public void clearUxRestrictions_restrictionsCleared() throws RemoteException {
95         mCarUxRestrictionsController
96                 .setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_FULLY_RESTRICTED);
97         mCarUxRestrictionsController.clearUxRestrictions();
98 
99         assertThat(mCarUxRestrictionsManager.getCurrentCarUxRestrictions().getActiveRestrictions())
100                 .isEqualTo(CarUxRestrictions.UX_RESTRICTIONS_BASELINE);
101     }
102 
103     @Test
isListenerRegistered_noListenerSet_returnsFalse()104     public void isListenerRegistered_noListenerSet_returnsFalse() {
105         assertThat(mCarUxRestrictionsController.isListenerRegistered()).isFalse();
106     }
107 
108     @Test
isListenerRegistered_listenerSet_returnsTrue()109     public void isListenerRegistered_listenerSet_returnsTrue() {
110         mCarUxRestrictionsManager.registerListener(mListener);
111 
112         assertThat(mCarUxRestrictionsController.isListenerRegistered()).isTrue();
113     }
114 
115     @Test
setUxRestrictions_listenerRegistered_listenerTriggered()116     public void setUxRestrictions_listenerRegistered_listenerTriggered() throws RemoteException {
117         mCarUxRestrictionsManager.registerListener(mListener);
118         mCarUxRestrictionsController
119                 .setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_NO_TEXT_MESSAGE);
120 
121         verify(mListener).onUxRestrictionsChanged(any());
122     }
123 }
124 
125