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.BluetoothDeviceNoNamePreferenceController
20         .BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_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 BluetoothDeviceNoNamePreferenceControllerTest {
43 
44     @Mock
45     private SwitchPreference mPreference;
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48 
49     private Context mContext;
50     private BluetoothDeviceNoNamePreferenceController mController;
51 
52     @Before
setup()53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mController = new BluetoothDeviceNoNamePreferenceController(mContext);
57         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
58                 .thenReturn(mPreference);
59         mController.displayPreference(mPreferenceScreen);
60     }
61 
62     @Test
onPreferenceChanged_settingEnabled_shouldTurnOnBluetoothDeviceNoName()63     public void onPreferenceChanged_settingEnabled_shouldTurnOnBluetoothDeviceNoName() {
64         mController.onPreferenceChange(mPreference, true /* new value */);
65 
66         final boolean mode = SystemProperties.getBoolean(
67                 BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY,
68                 false /* default */);
69 
70         assertThat(mode).isTrue();
71     }
72 
73     @Test
onPreferenceChanged_settingDisabled_shouldTurnOffBluetoothDeviceNoName()74     public void onPreferenceChanged_settingDisabled_shouldTurnOffBluetoothDeviceNoName() {
75         mController.onPreferenceChange(mPreference, false /* new value */);
76 
77         final boolean mode = SystemProperties.getBoolean(
78                 BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, true /* default */);
79 
80         assertThat(mode).isFalse();
81     }
82 
83     @Test
updateState_settingEnabled_preferenceShouldBeChecked()84     public void updateState_settingEnabled_preferenceShouldBeChecked() {
85         SystemProperties.set(BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY,
86                 Boolean.toString(true));
87         mController.updateState(mPreference);
88 
89         verify(mPreference).setChecked(true);
90     }
91 
92     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()93     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
94         SystemProperties.set(BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY,
95                 Boolean.toString(false));
96         mController.updateState(mPreference);
97 
98         verify(mPreference).setChecked(false);
99     }
100 
101     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()102     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
103         mController.onDeveloperOptionsDisabled();
104 
105         final boolean mode = SystemProperties.getBoolean(
106                 BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY,
107                 true /* default */);
108 
109         assertThat(mode).isFalse();
110         verify(mPreference).setEnabled(false);
111         verify(mPreference).setChecked(false);
112     }
113 }
114