1 /* 2 * Copyright (C) 2017 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.settings.development; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.app.admin.DevicePolicyManager; 30 import android.content.ComponentName; 31 import android.content.Context; 32 import android.os.UserHandle; 33 import android.provider.Settings; 34 35 import androidx.preference.PreferenceScreen; 36 37 import com.android.settingslib.RestrictedLockUtils; 38 import com.android.settingslib.RestrictedSwitchPreference; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class UsbAudioRoutingPreferenceControllerTest { 50 51 private static final ComponentName TEST_COMPONENT_NAME = new ComponentName("test", "test"); 52 53 @Mock 54 private PreferenceScreen mScreen; 55 @Mock 56 private RestrictedSwitchPreference mPreference; 57 @Mock 58 private DevicePolicyManager mDevicePolicyManager; 59 60 private Context mContext; 61 62 private UsbAudioRoutingPreferenceController mController; 63 64 @Before setUp()65 public void setUp() throws Exception { 66 MockitoAnnotations.initMocks(this); 67 mContext = spy(RuntimeEnvironment.application); 68 mController = new UsbAudioRoutingPreferenceController(mContext); 69 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 70 mController.displayPreference(mScreen); 71 when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager); 72 doReturn(mContext).when(mContext).createPackageContextAsUser( 73 any(String.class), anyInt(), any(UserHandle.class)); 74 } 75 76 @Test updateState_usbAudioRoutingEnabled_shouldCheckedPreference()77 public void updateState_usbAudioRoutingEnabled_shouldCheckedPreference() { 78 when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser( 79 UserHandle.myUserId())).thenReturn(true); 80 when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME); 81 Settings.Secure.putInt(mContext.getContentResolver(), 82 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 83 UsbAudioRoutingPreferenceController.SETTING_VALUE_ON); 84 85 mController.updateState(mPreference); 86 87 verify(mPreference).setChecked(true); 88 } 89 90 @Test updateState_usbAudioRoutingDisabled_shouldUncheckedPreference()91 public void updateState_usbAudioRoutingDisabled_shouldUncheckedPreference() { 92 when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser( 93 UserHandle.myUserId())).thenReturn(true); 94 when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME); 95 Settings.Secure.putInt(mContext.getContentResolver(), 96 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 97 UsbAudioRoutingPreferenceController.SETTING_VALUE_OFF); 98 99 mController.updateState(mPreference); 100 101 verify(mPreference).setChecked(false); 102 } 103 104 @Test updateState_usbDataSignalingDisabled_shouldDisablePreference()105 public void updateState_usbDataSignalingDisabled_shouldDisablePreference() { 106 when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser( 107 UserHandle.myUserId())).thenReturn(false); 108 when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME); 109 110 mController.updateState(mPreference); 111 112 verify(mPreference).setDisabledByAdmin(eq(new RestrictedLockUtils.EnforcedAdmin( 113 TEST_COMPONENT_NAME, null, UserHandle.SYSTEM))); 114 } 115 116 @Test onPreferenceChange_preferenceChecked_shouldEnableUsbAudioRouting()117 public void onPreferenceChange_preferenceChecked_shouldEnableUsbAudioRouting() { 118 mController.onPreferenceChange(mPreference, true /* new value */); 119 120 final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), 121 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, -1 /* default */); 122 123 assertThat(usbAudioRoutingMode).isEqualTo( 124 UsbAudioRoutingPreferenceController.SETTING_VALUE_ON); 125 } 126 127 @Test onPreferenceChange__preferenceUnchecked_shouldDisableUsbAudioRouting()128 public void onPreferenceChange__preferenceUnchecked_shouldDisableUsbAudioRouting() { 129 mController.onPreferenceChange(mPreference, false /* new value */); 130 131 final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), 132 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, -1 /* default */); 133 134 assertThat(usbAudioRoutingMode).isEqualTo( 135 UsbAudioRoutingPreferenceController.SETTING_VALUE_OFF); 136 } 137 138 @Test onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled()139 public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled() { 140 mController.onDeveloperOptionsSwitchDisabled(); 141 142 final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), 143 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, -1 /* default */); 144 145 assertThat(usbAudioRoutingMode).isEqualTo( 146 UsbAudioRoutingPreferenceController.SETTING_VALUE_OFF); 147 verify(mPreference).setEnabled(false); 148 verify(mPreference).setChecked(false); 149 } 150 151 @Test onDeveloperOptionsSwitchEnabled_usbEnabled_shouldNotDisablePreference()152 public void onDeveloperOptionsSwitchEnabled_usbEnabled_shouldNotDisablePreference() { 153 when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser( 154 UserHandle.myUserId())).thenReturn(true); 155 when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME); 156 157 mController.onDeveloperOptionsSwitchEnabled(); 158 159 verify(mPreference).setDisabledByAdmin(null); 160 } 161 162 @Test onDeveloperOptionsSwitchEnabled_usbDisabled_shouldDisablePreference()163 public void onDeveloperOptionsSwitchEnabled_usbDisabled_shouldDisablePreference() { 164 when(mDevicePolicyManager.isUsbDataSignalingEnabledForUser( 165 UserHandle.myUserId())).thenReturn(false); 166 when(mDevicePolicyManager.getProfileOwner()).thenReturn(TEST_COMPONENT_NAME); 167 168 mController.onDeveloperOptionsSwitchEnabled(); 169 170 verify(mPreference).setDisabledByAdmin(eq(new RestrictedLockUtils.EnforcedAdmin( 171 TEST_COMPONENT_NAME, null, UserHandle.SYSTEM))); 172 } 173 } 174