1 /* 2 * Copyright (C) 2020 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.android.settings.AllInOneTetherSettings.BLUETOOTH_TETHER_KEY; 20 import static com.android.settings.AllInOneTetherSettings.ETHERNET_TETHER_KEY; 21 import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_DEFAULT; 22 import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_MAX; 23 import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_WITH_SECURITY_NON; 24 import static com.android.settings.AllInOneTetherSettings.USB_TETHER_KEY; 25 import static com.android.settings.AllInOneTetherSettings.WIFI_TETHER_DISABLE_KEY; 26 27 import static com.google.common.truth.Truth.assertThat; 28 29 import static org.mockito.Mockito.doReturn; 30 import static org.mockito.Mockito.mock; 31 import static org.mockito.Mockito.spy; 32 import static org.mockito.Mockito.when; 33 34 import android.content.Context; 35 import android.net.ConnectivityManager; 36 import android.net.wifi.SoftApConfiguration; 37 import android.os.UserHandle; 38 import android.os.UserManager; 39 import android.util.FeatureFlagUtils; 40 41 import androidx.preference.PreferenceGroup; 42 import androidx.preference.PreferenceScreen; 43 44 import com.android.settings.core.FeatureFlags; 45 import com.android.settings.testutils.shadow.ShadowWifiManager; 46 import com.android.settings.wifi.tether.WifiTetherAutoOffPreferenceController; 47 import com.android.settings.wifi.tether.WifiTetherSecurityPreferenceController; 48 import com.android.settingslib.core.lifecycle.Lifecycle; 49 50 import org.junit.Before; 51 import org.junit.Test; 52 import org.junit.runner.RunWith; 53 import org.mockito.Mock; 54 import org.mockito.MockitoAnnotations; 55 import org.robolectric.RobolectricTestRunner; 56 import org.robolectric.RuntimeEnvironment; 57 import org.robolectric.annotation.Config; 58 import org.robolectric.util.ReflectionHelpers; 59 60 import java.util.ArrayList; 61 import java.util.List; 62 63 @RunWith(RobolectricTestRunner.class) 64 @Config(shadows = {ShadowWifiManager.class}) 65 public class AllInOneTetherSettingsTest { 66 private static final String[] WIFI_REGEXS = {"wifi_regexs"}; 67 private static final String[] USB_REGEXS = {"usb_regexs"}; 68 private static final String[] BT_REGEXS = {"bt_regexs"}; 69 private static final String[] ETHERNET_REGEXS = {"ethernet_regexs"}; 70 71 private Context mContext; 72 private AllInOneTetherSettings mAllInOneTetherSettings; 73 74 @Mock 75 private ConnectivityManager mConnectivityManager; 76 @Mock 77 private UserManager mUserManager; 78 @Mock 79 private WifiTetherSecurityPreferenceController mSecurityPreferenceController; 80 @Mock 81 private PreferenceScreen mPreferenceScreen; 82 @Mock 83 private PreferenceGroup mWifiTetherGroup; 84 85 @Before setUp()86 public void setUp() { 87 mContext = spy(RuntimeEnvironment.application); 88 89 MockitoAnnotations.initMocks(this); 90 doReturn(mConnectivityManager) 91 .when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE); 92 doReturn(WIFI_REGEXS).when(mConnectivityManager).getTetherableWifiRegexs(); 93 doReturn(USB_REGEXS).when(mConnectivityManager).getTetherableUsbRegexs(); 94 doReturn(BT_REGEXS).when(mConnectivityManager).getTetherableBluetoothRegexs(); 95 doReturn(ETHERNET_REGEXS).when(mConnectivityManager).getTetherableIfaces(); 96 doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE); 97 // Assume the feature is enabled for most test cases. 98 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true); 99 mAllInOneTetherSettings = spy(new AllInOneTetherSettings()); 100 doReturn(mPreferenceScreen).when(mAllInOneTetherSettings).getPreferenceScreen(); 101 ReflectionHelpers.setField(mAllInOneTetherSettings, "mLifecycle", mock(Lifecycle.class)); 102 ReflectionHelpers.setField(mAllInOneTetherSettings, "mSecurityPreferenceController", 103 mSecurityPreferenceController); 104 ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTetherGroup", mWifiTetherGroup); 105 } 106 107 @Test getNonIndexableKeys_tetherAvailable_featureEnabled_keysReturnedCorrectly()108 public void getNonIndexableKeys_tetherAvailable_featureEnabled_keysReturnedCorrectly() { 109 // To let TetherUtil.isTetherAvailable return true, select one of the combinations 110 setupIsTetherAvailable(true); 111 112 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true); 113 final List<String> niks = 114 AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 115 116 assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME); 117 assertThat(niks).doesNotContain( 118 AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD); 119 assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF); 120 assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND); 121 assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY); 122 assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY); 123 assertThat(niks).doesNotContain(USB_TETHER_KEY); 124 assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY); 125 126 // This key should be returned because it's not visible by default. 127 assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY); 128 } 129 130 @Test getNonIndexableKeys_tetherAvailable_featureDisabled_keysReturned()131 public void getNonIndexableKeys_tetherAvailable_featureDisabled_keysReturned() { 132 setupIsTetherAvailable(true); 133 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false); 134 135 final List<String> niks = 136 AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 137 138 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME); 139 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD); 140 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF); 141 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND); 142 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY); 143 assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY); 144 assertThat(niks).contains(BLUETOOTH_TETHER_KEY); 145 assertThat(niks).contains(USB_TETHER_KEY); 146 assertThat(niks).contains(ETHERNET_TETHER_KEY); 147 } 148 149 @Test getNonIndexableKeys_tetherNotAvailable_keysReturned()150 public void getNonIndexableKeys_tetherNotAvailable_keysReturned() { 151 // To let TetherUtil.isTetherAvailable return false, select one of the combinations 152 setupIsTetherAvailable(false); 153 154 final List<String> niks = 155 AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 156 157 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME); 158 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD); 159 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF); 160 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND); 161 assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY); 162 assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY); 163 assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY); 164 assertThat(niks).doesNotContain(USB_TETHER_KEY); 165 assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY); 166 } 167 168 @Test getPreferenceControllers_notEmpty()169 public void getPreferenceControllers_notEmpty() { 170 assertThat(AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER 171 .getPreferenceControllers(mContext)).isNotEmpty(); 172 } 173 174 @Test createPreferenceControllers_hasAutoOffPreference()175 public void createPreferenceControllers_hasAutoOffPreference() { 176 assertThat(mAllInOneTetherSettings.createPreferenceControllers(mContext) 177 .stream() 178 .filter(controller -> controller instanceof WifiTetherAutoOffPreferenceController) 179 .count()) 180 .isEqualTo(1); 181 } 182 183 @Test getInitialChildCount_withSecurity()184 public void getInitialChildCount_withSecurity() { 185 when(mSecurityPreferenceController.getSecurityType()) 186 .thenReturn(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 187 assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo( 188 EXPANDED_CHILD_COUNT_DEFAULT); 189 } 190 191 @Test getInitialChildCount_withoutSecurity()192 public void getInitialChildCount_withoutSecurity() { 193 when(mSecurityPreferenceController.getSecurityType()) 194 .thenReturn(SoftApConfiguration.SECURITY_TYPE_OPEN); 195 assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo( 196 EXPANDED_CHILD_COUNT_WITH_SECURITY_NON); 197 } 198 199 @Test getInitialExpandedChildCount_expandAllChild()200 public void getInitialExpandedChildCount_expandAllChild() { 201 assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()) 202 .isNotEqualTo(EXPANDED_CHILD_COUNT_MAX); 203 ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", false); 204 assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()) 205 .isEqualTo(EXPANDED_CHILD_COUNT_MAX); 206 ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", true); 207 assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()) 208 .isEqualTo(EXPANDED_CHILD_COUNT_MAX); 209 } 210 setupIsTetherAvailable(boolean returnValue)211 private void setupIsTetherAvailable(boolean returnValue) { 212 when(mConnectivityManager.isTetheringSupported()).thenReturn(true); 213 214 // For RestrictedLockUtils.checkIfRestrictionEnforced 215 final int userId = UserHandle.myUserId(); 216 List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>(); 217 when(mUserManager.getUserRestrictionSources( 218 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId))) 219 .thenReturn(enforcingUsers); 220 221 // For RestrictedLockUtils.hasBaseUserRestriction 222 when(mUserManager.hasBaseUserRestriction( 223 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId))) 224 .thenReturn(!returnValue); 225 } 226 } 227