blob: 99c112c1920faf9293a27ed782ef571ca1770500 [file] [log] [blame]
Chilund3358d82018-03-26 18:28:56 +08001/*
2 * Copyright (C) 2018 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
17package com.android.settings;
18
19import static com.google.common.truth.Truth.assertThat;
Fan Zhang23f8d592018-08-28 15:11:40 -070020
Ted Wang3876d722020-09-01 14:45:46 +080021import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.Mockito.doNothing;
Chilund3358d82018-03-26 18:28:56 +080023import static org.mockito.Mockito.doReturn;
Arc Wang005fe692020-03-04 21:03:54 +080024import static org.mockito.Mockito.mock;
25import static org.mockito.Mockito.never;
Chilund3358d82018-03-26 18:28:56 +080026import static org.mockito.Mockito.spy;
Arc Wang005fe692020-03-04 21:03:54 +080027import static org.mockito.Mockito.verify;
Chilund3358d82018-03-26 18:28:56 +080028import static org.mockito.Mockito.when;
29
Ted Wang3876d722020-09-01 14:45:46 +080030import android.bluetooth.BluetoothAdapter;
31import android.content.BroadcastReceiver;
Chilund3358d82018-03-26 18:28:56 +080032import android.content.Context;
Ted Wang3876d722020-09-01 14:45:46 +080033import android.content.Intent;
34import android.content.IntentFilter;
Fan Zhangc7162cd2018-06-18 15:21:41 -070035import android.net.ConnectivityManager;
Arc Wang005fe692020-03-04 21:03:54 +080036import android.net.wifi.WifiManager;
Chilund3358d82018-03-26 18:28:56 +080037import android.os.UserHandle;
38import android.os.UserManager;
Yi-Ling Chuang542f76a2020-03-11 18:18:36 +080039import android.util.FeatureFlagUtils;
Chilund3358d82018-03-26 18:28:56 +080040
Ted Wang3876d722020-09-01 14:45:46 +080041import androidx.fragment.app.FragmentActivity;
Arc Wang005fe692020-03-04 21:03:54 +080042import androidx.preference.Preference;
Ted Wang3876d722020-09-01 14:45:46 +080043import androidx.preference.SwitchPreference;
Arc Wang005fe692020-03-04 21:03:54 +080044
Yi-Ling Chuang542f76a2020-03-11 18:18:36 +080045import com.android.settings.core.FeatureFlags;
46
Chilund3358d82018-03-26 18:28:56 +080047import org.junit.Before;
48import org.junit.Test;
49import org.junit.runner.RunWith;
Ted Wang3876d722020-09-01 14:45:46 +080050import org.mockito.ArgumentCaptor;
Chilund3358d82018-03-26 18:28:56 +080051import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
James Lemieuxf1dade42018-12-07 12:56:49 -080053import org.robolectric.RobolectricTestRunner;
Chilund3358d82018-03-26 18:28:56 +080054import org.robolectric.RuntimeEnvironment;
55
56import java.util.ArrayList;
57import java.util.List;
58
James Lemieuxf1dade42018-12-07 12:56:49 -080059@RunWith(RobolectricTestRunner.class)
Chilund3358d82018-03-26 18:28:56 +080060public class TetherSettingsTest {
61
62 private Context mContext;
63
64 @Mock
65 private ConnectivityManager mConnectivityManager;
66 @Mock
67 private UserManager mUserManager;
68
69 @Before
70 public void setUp() {
71 mContext = spy(RuntimeEnvironment.application);
72
73 MockitoAnnotations.initMocks(this);
74 doReturn(mConnectivityManager)
75 .when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
76 doReturn(mUserManager)
77 .when(mContext).getSystemService(Context.USER_SERVICE);
78
79 setupIsTetherAvailable(true);
80
James Lemieuxf1dade42018-12-07 12:56:49 -080081 when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
82 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
Chilund3358d82018-03-26 18:28:56 +080083 }
84
85 @Test
86 public void testTetherNonIndexableKeys_tetherAvailable_keysNotReturned() {
Yi-Ling Chuang542f76a2020-03-11 18:18:36 +080087 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
Chilund3358d82018-03-26 18:28:56 +080088 // To let TetherUtil.isTetherAvailable return true, select one of the combinations
89 setupIsTetherAvailable(true);
90
91 final List<String> niks =
92 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
93
94 assertThat(niks).doesNotContain(TetherSettings.KEY_TETHER_PREFS_SCREEN);
95 assertThat(niks).doesNotContain(TetherSettings.KEY_WIFI_TETHER);
96 }
97
98 @Test
99 public void testTetherNonIndexableKeys_tetherNotAvailable_keysReturned() {
100 // To let TetherUtil.isTetherAvailable return false, select one of the combinations
101 setupIsTetherAvailable(false);
102
103 final List<String> niks =
104 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
105
106 assertThat(niks).contains(TetherSettings.KEY_TETHER_PREFS_SCREEN);
107 assertThat(niks).contains(TetherSettings.KEY_WIFI_TETHER);
108 }
109
110 @Test
111 public void testTetherNonIndexableKeys_usbNotAvailable_usbKeyReturned() {
James Lemieuxf1dade42018-12-07 12:56:49 -0800112 when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
Chilund3358d82018-03-26 18:28:56 +0800113
114 final List<String> niks =
115 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
116
117 assertThat(niks).contains(TetherSettings.KEY_USB_TETHER_SETTINGS);
118 }
119
120 @Test
121 public void testTetherNonIndexableKeys_usbAvailable_usbKeyNotReturned() {
Yi-Ling Chuang542f76a2020-03-11 18:18:36 +0800122 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
Chilund3358d82018-03-26 18:28:56 +0800123 // We can ignore the condition of Utils.isMonkeyRunning()
124 // In normal case, monkey and robotest should not execute at the same time
125 when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{"dummyRegex"});
126
127 final List<String> niks =
128 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
129
130 assertThat(niks).doesNotContain(TetherSettings.KEY_USB_TETHER_SETTINGS);
131 }
132
133 @Test
134 public void testTetherNonIndexableKeys_bluetoothNotAvailable_bluetoothKeyReturned() {
James Lemieuxf1dade42018-12-07 12:56:49 -0800135 when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
Chilund3358d82018-03-26 18:28:56 +0800136
137 final List<String> niks =
138 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
139
140 assertThat(niks).contains(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING);
141 }
142
143 @Test
144 public void testTetherNonIndexableKeys_bluetoothAvailable_bluetoothKeyNotReturned() {
Yi-Ling Chuang542f76a2020-03-11 18:18:36 +0800145 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
Chilund3358d82018-03-26 18:28:56 +0800146 when(mConnectivityManager.getTetherableBluetoothRegexs())
147 .thenReturn(new String[]{"dummyRegex"});
148
149 final List<String> niks =
150 TetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
151
152 assertThat(niks).doesNotContain(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING);
153 }
154
Arc Wang005fe692020-03-04 21:03:54 +0800155 @Test
156 public void testSetFooterPreferenceTitle_isStaApConcurrencySupported_showStaApString() {
157 final TetherSettings spyTetherSettings = spy(new TetherSettings());
158 when(spyTetherSettings.getContext()).thenReturn(mContext);
159 final Preference mockPreference = mock(Preference.class);
160 when(spyTetherSettings.findPreference(TetherSettings.KEY_TETHER_PREFS_FOOTER))
161 .thenReturn(mockPreference);
162 final WifiManager mockWifiManager = mock(WifiManager.class);
163 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mockWifiManager);
164 when(mockWifiManager.isStaApConcurrencySupported()).thenReturn(true);
165
166 spyTetherSettings.setFooterPreferenceTitle();
167
168 verify(mockPreference, never()).setTitle(R.string.tethering_footer_info);
169 verify(mockPreference).setTitle(R.string.tethering_footer_info_sta_ap_concurrency);
170 }
171
Ted Wang3876d722020-09-01 14:45:46 +0800172 @Test
173 public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOn() {
174 final TetherSettings spyTetherSettings = spy(new TetherSettings());
175 when(spyTetherSettings.getContext()).thenReturn(mContext);
176 final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
177 when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
178 .thenReturn(mockSwitchPreference);
179 final FragmentActivity mockActivity = mock(FragmentActivity.class);
180 when(spyTetherSettings.getActivity()).thenReturn(mockActivity);
181 final ArgumentCaptor<BroadcastReceiver> captor =
182 ArgumentCaptor.forClass(BroadcastReceiver.class);
183 when(mockActivity.registerReceiver(captor.capture(), any(IntentFilter.class)))
184 .thenReturn(null);
185 // Bluetooth tethering state is on
186 when(spyTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
187 when(spyTetherSettings.isBluetoothTetheringOn()).thenReturn(true);
188
189 spyTetherSettings.setupTetherPreference();
190 spyTetherSettings.registerReceiver();
191 updateOnlyBluetoothState(spyTetherSettings);
192
193 // Simulate Bluetooth tethering state changed
194 final BroadcastReceiver receiver = captor.getValue();
195 final Intent bluetoothTetheringChanged =
196 new Intent(TetherSettings.BLUETOOTH_TETHERING_STATE_CHANGED);
197 receiver.onReceive(mockActivity, bluetoothTetheringChanged);
198
199 verify(mockSwitchPreference).setEnabled(true);
200 verify(mockSwitchPreference).setChecked(true);
201 }
202
203 @Test
204 public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOff() {
205 final TetherSettings spyTetherSettings = spy(new TetherSettings());
206 when(spyTetherSettings.getContext()).thenReturn(mContext);
207 final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
208 when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
209 .thenReturn(mockSwitchPreference);
210 final FragmentActivity mockActivity = mock(FragmentActivity.class);
211 when(spyTetherSettings.getActivity()).thenReturn(mockActivity);
212 final ArgumentCaptor<BroadcastReceiver> captor =
213 ArgumentCaptor.forClass(BroadcastReceiver.class);
214 when(mockActivity.registerReceiver(captor.capture(), any(IntentFilter.class)))
215 .thenReturn(null);
216 // Bluetooth tethering state is off
217 when(spyTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
218 when(spyTetherSettings.isBluetoothTetheringOn()).thenReturn(false);
219
220 spyTetherSettings.setupTetherPreference();
221 spyTetherSettings.registerReceiver();
222 updateOnlyBluetoothState(spyTetherSettings);
223
224 // Simulate Bluetooth tethering state changed
225 final BroadcastReceiver receiver = captor.getValue();
226 final Intent bluetoothTetheringChanged =
227 new Intent(TetherSettings.BLUETOOTH_TETHERING_STATE_CHANGED);
228 receiver.onReceive(mockActivity, bluetoothTetheringChanged);
229
230 verify(mockSwitchPreference).setEnabled(true);
231 verify(mockSwitchPreference).setChecked(false);
232 }
233
234 private void updateOnlyBluetoothState(TetherSettings tetherSettings) {
235 doReturn(mConnectivityManager).when(tetherSettings)
236 .getSystemService(Context.CONNECTIVITY_SERVICE);
237 when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[0]);
238 when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[0]);
239 when(mConnectivityManager.getTetheringErroredIfaces()).thenReturn(new String[0]);
240 doNothing().when(tetherSettings).updateUsbState(any(String[].class), any(String[].class),
241 any(String[].class));
242 doNothing().when(tetherSettings).updateEthernetState(any(String[].class),
243 any(String[].class));
244 }
245
Chilund3358d82018-03-26 18:28:56 +0800246 private void setupIsTetherAvailable(boolean returnValue) {
247 when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
248
249 // For RestrictedLockUtils.checkIfRestrictionEnforced
250 final int userId = UserHandle.myUserId();
251 List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
252 when(mUserManager.getUserRestrictionSources(
253 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
254 .thenReturn(enforcingUsers);
255
256 // For RestrictedLockUtils.hasBaseUserRestriction
257 when(mUserManager.hasBaseUserRestriction(
258 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
259 .thenReturn(!returnValue);
260 }
261}