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