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.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.provider.Settings; 26 27 import androidx.preference.PreferenceScreen; 28 import androidx.preference.SwitchPreference; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class UsbAudioRoutingPreferenceControllerTest { 40 41 @Mock 42 private PreferenceScreen mScreen; 43 @Mock 44 private SwitchPreference mPreference; 45 46 private Context mContext; 47 48 private UsbAudioRoutingPreferenceController mController; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 mContext = RuntimeEnvironment.application; 54 mController = new UsbAudioRoutingPreferenceController(mContext); 55 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 56 mController.displayPreference(mScreen); 57 } 58 59 @Test updateState_usbAudioRoutingEnabled_shouldCheckedPreference()60 public void updateState_usbAudioRoutingEnabled_shouldCheckedPreference() { 61 Settings.Secure.putInt(mContext.getContentResolver(), 62 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 63 UsbAudioRoutingPreferenceController.SETTING_VALUE_ON); 64 65 mController.updateState(mPreference); 66 67 verify(mPreference).setChecked(true); 68 } 69 70 @Test updateState_usbAudioRoutingDisabled_shouldUncheckedPreference()71 public void updateState_usbAudioRoutingDisabled_shouldUncheckedPreference() { 72 Settings.Secure.putInt(mContext.getContentResolver(), 73 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 74 UsbAudioRoutingPreferenceController.SETTING_VALUE_OFF); 75 76 mController.updateState(mPreference); 77 78 verify(mPreference).setChecked(false); 79 } 80 81 @Test onPreferenceChange_preferenceChecked_shouldEnableUsbAudioRouting()82 public void onPreferenceChange_preferenceChecked_shouldEnableUsbAudioRouting() { 83 mController.onPreferenceChange(mPreference, true /* new value */); 84 85 final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), 86 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, -1 /* default */); 87 88 assertThat(usbAudioRoutingMode).isEqualTo( 89 UsbAudioRoutingPreferenceController.SETTING_VALUE_ON); 90 } 91 92 @Test onPreferenceChange__preferenceUnchecked_shouldDisableUsbAudioRouting()93 public void onPreferenceChange__preferenceUnchecked_shouldDisableUsbAudioRouting() { 94 mController.onPreferenceChange(mPreference, false /* new value */); 95 96 final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), 97 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, -1 /* default */); 98 99 assertThat(usbAudioRoutingMode).isEqualTo( 100 UsbAudioRoutingPreferenceController.SETTING_VALUE_OFF); 101 } 102 103 @Test onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled()104 public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled() { 105 mController.onDeveloperOptionsSwitchDisabled(); 106 107 final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), 108 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, -1 /* default */); 109 110 assertThat(usbAudioRoutingMode).isEqualTo( 111 UsbAudioRoutingPreferenceController.SETTING_VALUE_OFF); 112 verify(mPreference).setEnabled(false); 113 verify(mPreference).setChecked(false); 114 } 115 } 116