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