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 
24 import android.app.Application;
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.os.RemoteException;
31 
32 import androidx.test.core.app.ApplicationProvider;
33 
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnit;
40 import org.mockito.junit.MockitoRule;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.annotation.internal.DoNotInstrument;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @DoNotInstrument
46 public class CarUxRestrictionsManagerTest {
47     @Rule
48     public MockitoRule rule = MockitoJUnit.rule();
49 
50     CarUxRestrictionsManager mCarUxRestrictionsManager;
51     CarUxRestrictionsController mCarUxRestrictionsController;
52 
53     @Mock
54     OnUxRestrictionsChangedListener mListener;
55 
56     @Before
setUp()57     public void setUp() {
58         Application context = ApplicationProvider.getApplicationContext();
59         FakeCar fakeCar = FakeCar.createFakeCar(context);
60         Car carApi = fakeCar.getCar();
61 
62         mCarUxRestrictionsManager =
63                 (CarUxRestrictionsManager) carApi.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE);
64         mCarUxRestrictionsController = fakeCar.getCarUxRestrictionController();
65     }
66 
67     @Test
getRestrictions_noRestrictionsSet_noRestrictionsPresent()68     public void getRestrictions_noRestrictionsSet_noRestrictionsPresent() {
69         assertThat(mCarUxRestrictionsManager.getCurrentCarUxRestrictions().getActiveRestrictions())
70                 .isEqualTo(CarUxRestrictions.UX_RESTRICTIONS_BASELINE);
71     }
72 
73     @Test
setUxRestrictions_restrictionsRegistered()74     public void setUxRestrictions_restrictionsRegistered() throws RemoteException {
75         mCarUxRestrictionsController.setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO);
76 
77         assertThat(mCarUxRestrictionsManager.getCurrentCarUxRestrictions().getActiveRestrictions())
78                 .isEqualTo(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO);
79     }
80 
81     @Test
clearUxRestrictions_restrictionsCleared()82     public void clearUxRestrictions_restrictionsCleared() throws RemoteException {
83         mCarUxRestrictionsController
84                 .setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_FULLY_RESTRICTED);
85         mCarUxRestrictionsController.clearUxRestrictions();
86 
87         assertThat(mCarUxRestrictionsManager.getCurrentCarUxRestrictions().getActiveRestrictions())
88                 .isEqualTo(CarUxRestrictions.UX_RESTRICTIONS_BASELINE);
89     }
90 
91     @Test
isListenerRegistered_noListenerSet_returnsFalse()92     public void isListenerRegistered_noListenerSet_returnsFalse() {
93         assertThat(mCarUxRestrictionsController.isListenerRegistered()).isFalse();
94     }
95 
96     @Test
isListenerRegistered_listenerSet_returnsTrue()97     public void isListenerRegistered_listenerSet_returnsTrue() {
98         mCarUxRestrictionsManager.registerListener(mListener);
99 
100         assertThat(mCarUxRestrictionsController.isListenerRegistered()).isTrue();
101     }
102 
103     @Test
setUxRestrictions_listenerRegistered_listenerTriggered()104     public void setUxRestrictions_listenerRegistered_listenerTriggered() throws RemoteException {
105         mCarUxRestrictionsManager.registerListener(mListener);
106         mCarUxRestrictionsController
107                 .setUxRestrictions(CarUxRestrictions.UX_RESTRICTIONS_NO_TEXT_MESSAGE);
108 
109         verify(mListener).onUxRestrictionsChanged(any());
110     }
111 }
112 
113