Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 20 | |
Ted Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.Mockito.doNothing; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 23 | import static org.mockito.Mockito.doReturn; |
Arc Wang | 005fe69 | 2020-03-04 21:03:54 +0800 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.never; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.spy; |
Arc Wang | 005fe69 | 2020-03-04 21:03:54 +0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.verify; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 28 | import static org.mockito.Mockito.when; |
| 29 | |
Ted Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 30 | import android.bluetooth.BluetoothAdapter; |
| 31 | import android.content.BroadcastReceiver; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 32 | import android.content.Context; |
Ted Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 35 | import android.net.ConnectivityManager; |
Arc Wang | 005fe69 | 2020-03-04 21:03:54 +0800 | [diff] [blame] | 36 | import android.net.wifi.WifiManager; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 37 | import android.os.UserHandle; |
| 38 | import android.os.UserManager; |
Yi-Ling Chuang | 542f76a | 2020-03-11 18:18:36 +0800 | [diff] [blame] | 39 | import android.util.FeatureFlagUtils; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 40 | |
Ted Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 41 | import androidx.fragment.app.FragmentActivity; |
Arc Wang | 005fe69 | 2020-03-04 21:03:54 +0800 | [diff] [blame] | 42 | import androidx.preference.Preference; |
Ted Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 43 | import androidx.preference.SwitchPreference; |
Arc Wang | 005fe69 | 2020-03-04 21:03:54 +0800 | [diff] [blame] | 44 | |
Yi-Ling Chuang | 542f76a | 2020-03-11 18:18:36 +0800 | [diff] [blame] | 45 | import com.android.settings.core.FeatureFlags; |
| 46 | |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
| 49 | import org.junit.runner.RunWith; |
Ted Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 50 | import org.mockito.ArgumentCaptor; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
James Lemieux | f1dade4 | 2018-12-07 12:56:49 -0800 | [diff] [blame] | 53 | import org.robolectric.RobolectricTestRunner; |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 54 | import org.robolectric.RuntimeEnvironment; |
| 55 | |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.List; |
| 58 | |
James Lemieux | f1dade4 | 2018-12-07 12:56:49 -0800 | [diff] [blame] | 59 | @RunWith(RobolectricTestRunner.class) |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 60 | public 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 Lemieux | f1dade4 | 2018-12-07 12:56:49 -0800 | [diff] [blame] | 81 | when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]); |
| 82 | when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]); |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testTetherNonIndexableKeys_tetherAvailable_keysNotReturned() { |
Yi-Ling Chuang | 542f76a | 2020-03-11 18:18:36 +0800 | [diff] [blame] | 87 | FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false); |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 88 | // 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 Lemieux | f1dade4 | 2018-12-07 12:56:49 -0800 | [diff] [blame] | 112 | when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]); |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 113 | |
| 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 Chuang | 542f76a | 2020-03-11 18:18:36 +0800 | [diff] [blame] | 122 | FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false); |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 123 | // 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 Lemieux | f1dade4 | 2018-12-07 12:56:49 -0800 | [diff] [blame] | 135 | when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]); |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 136 | |
| 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 Chuang | 542f76a | 2020-03-11 18:18:36 +0800 | [diff] [blame] | 145 | FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false); |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 146 | 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 Wang | 005fe69 | 2020-03-04 21:03:54 +0800 | [diff] [blame] | 155 | @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 Wang | 3876d72 | 2020-09-01 14:45:46 +0800 | [diff] [blame^] | 172 | @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 | |
Chilun | d3358d8 | 2018-03-26 18:28:56 +0800 | [diff] [blame] | 246 | 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 | } |