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.BluetoothSnoopLogPreferenceController.BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.os.SystemProperties;
27 
28 import androidx.preference.ListPreference;
29 import androidx.preference.PreferenceScreen;
30 
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.Spy;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class BluetoothSnoopLogPreferenceControllerTest {
43 
44     @Spy private Context mSpyContext = RuntimeEnvironment.application;
45     @Spy private Resources mSpyResources = RuntimeEnvironment.application.getResources();
46     private ListPreference mPreference;
47     @Mock private PreferenceScreen mPreferenceScreen;
48     private BluetoothSnoopLogPreferenceController mController;
49 
50     private CharSequence[] mListValues;
51     private CharSequence[] mListEntries;
52 
53     @Before
setup()54     public void setup() {
55         MockitoAnnotations.initMocks(this);
56         doReturn(mSpyResources).when(mSpyContext).getResources();
57         // Get XML values without mock
58         // Setup test list preference using XML values
59         mPreference = new ListPreference(mSpyContext);
60         mPreference.setEntries(com.android.settingslib.R.array.bt_hci_snoop_log_entries);
61         mPreference.setEntryValues(com.android.settingslib.R.array.bt_hci_snoop_log_values);
62         // Init the actual controller
63         mController = new BluetoothSnoopLogPreferenceController(mSpyContext, null);
64         // Construct preference in the controller via a mocked preference screen object
65         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
66                 .thenReturn(mPreference);
67         mController.displayPreference(mPreferenceScreen);
68         mListValues = mPreference.getEntryValues();
69         mListEntries = mPreference.getEntries();
70     }
71 
72     @Test
verifyResourceSizeAndRange()73     public void verifyResourceSizeAndRange() {
74         // Verify normal list entries and default preference entries have the same size
75         Assert.assertEquals(mListEntries.length, mListValues.length);
76         // Update the preference
77         mController.updateState(mPreference);
78         // Verify default preference value, entry and summary
79         final int defaultIndex = mController.getDefaultModeIndex();
80         Assert.assertEquals(mPreference.getValue(), mListValues[defaultIndex]);
81         Assert.assertEquals(mPreference.getEntry(), mListEntries[defaultIndex]);
82         Assert.assertEquals(mPreference.getSummary(), mListEntries[defaultIndex]);
83     }
84 
85     @Test
onPreferenceChanged_turnOnFullBluetoothSnoopLog()86     public void onPreferenceChanged_turnOnFullBluetoothSnoopLog() {
87         mController.onPreferenceChange(
88                 null,
89                 mListValues[BluetoothSnoopLogPreferenceController.BTSNOOP_LOG_MODE_FULL_INDEX]);
90         final String mode = SystemProperties.get(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY);
91         // "full" is hard-coded between Settings and system/bt
92         Assert.assertEquals(mode, "full");
93     }
94 
95     @Test
onPreferenceChanged_turnOnFilteredBluetoothSnoopLog()96     public void onPreferenceChanged_turnOnFilteredBluetoothSnoopLog() {
97         mController.onPreferenceChange(
98                 null,
99                 mListValues[BluetoothSnoopLogPreferenceController.BTSNOOP_LOG_MODE_FILTERED_INDEX]);
100         final String mode = SystemProperties.get(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY);
101         // "filtered" is hard-coded between Settings and system/bt
102         Assert.assertEquals(mode, "filtered");
103     }
104 
105     @Test
onPreferenceChanged_turnOffBluetoothSnoopLog()106     public void onPreferenceChanged_turnOffBluetoothSnoopLog() {
107         mController.onPreferenceChange(
108                 null,
109                 mListValues[BluetoothSnoopLogPreferenceController.BTSNOOP_LOG_MODE_DISABLED_INDEX]);
110         final String mode = SystemProperties.get(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY);
111         // "disabled" is hard-coded between Settings and system/bt
112         Assert.assertEquals(mode, "disabled");
113     }
114 
115     @Test
updateState_preferenceShouldBeSetToRightValue()116     public void updateState_preferenceShouldBeSetToRightValue() {
117         for (int i = 0; i < mListValues.length; ++i) {
118             SystemProperties.set(BLUETOOTH_BTSNOOP_LOG_MODE_PROPERTY, mListValues[i].toString());
119             mController.updateState(mPreference);
120             Assert.assertEquals(mPreference.getValue(), mListValues[i].toString());
121             Assert.assertEquals(mPreference.getSummary(), mListEntries[i].toString());
122         }
123     }
124 
125     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()126     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
127         mController.onDeveloperOptionsDisabled();
128         Assert.assertFalse(mPreference.isEnabled());
129         Assert.assertEquals(
130                 mPreference.getValue(), mListValues[mController.getDefaultModeIndex()].toString());
131         Assert.assertEquals(
132                 mPreference.getSummary(),
133                 mListEntries[mController.getDefaultModeIndex()].toString());
134     }
135 }
136