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