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