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.android.settings.development.BluetoothAbsoluteVolumePreferenceController
20         .BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.os.SystemProperties;
29 
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class BluetoothAbsoluteVolumePreferenceControllerTest {
43 
44     @Mock
45     private SwitchPreference mPreference;
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48 
49     private Context mContext;
50     private BluetoothAbsoluteVolumePreferenceController mController;
51 
52     @Before
setup()53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mController = new BluetoothAbsoluteVolumePreferenceController(mContext);
57         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
58             .thenReturn(mPreference);
59         mController.displayPreference(mPreferenceScreen);
60     }
61 
62     @Test
onPreferenceChanged_settingEnabled_shouldTurnOnBluetoothDisableAbsoluteVolume()63     public void onPreferenceChanged_settingEnabled_shouldTurnOnBluetoothDisableAbsoluteVolume() {
64         mController.onPreferenceChange(mPreference, true /* new value */);
65 
66         final boolean mode = SystemProperties.getBoolean(
67                 BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false /* default */);
68 
69         assertThat(mode).isTrue();
70     }
71 
72     @Test
onPreferenceChanged_settingDisabled_shouldTurnOffBluetoothDisableAbsoluteVolume()73     public void onPreferenceChanged_settingDisabled_shouldTurnOffBluetoothDisableAbsoluteVolume() {
74         mController.onPreferenceChange(mPreference, false /* new value */);
75 
76         final boolean mode = SystemProperties.getBoolean(
77                 BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false /* default */);
78 
79         assertThat(mode).isFalse();
80     }
81 
82     @Test
updateState_settingEnabled_preferenceShouldBeChecked()83     public void updateState_settingEnabled_preferenceShouldBeChecked() {
84         SystemProperties.set(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, Boolean.toString(true));
85         mController.updateState(mPreference);
86 
87         verify(mPreference).setChecked(true);
88     }
89 
90     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()91     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
92         SystemProperties.set(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, Boolean.toString(false));
93         mController.updateState(mPreference);
94 
95         verify(mPreference).setChecked(false);
96     }
97 
98     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()99     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
100         mController.onDeveloperOptionsDisabled();
101 
102         final boolean mode = SystemProperties.getBoolean(
103                 BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false /* default */);
104 
105         assertThat(mode).isFalse();
106         verify(mPreference).setEnabled(false);
107         verify(mPreference).setChecked(false);
108     }
109 }
110