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 package com.android.settings.wifi; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.doNothing; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.app.Activity; 31 import android.content.ContentResolver; 32 import android.content.Context; 33 import android.content.Intent; 34 import android.content.res.Resources; 35 import android.net.wifi.WifiConfiguration; 36 import android.net.wifi.WifiManager; 37 import android.net.wifi.hotspot2.PasspointConfiguration; 38 import android.net.wifi.hotspot2.pps.HomeSp; 39 import android.os.Bundle; 40 import android.os.PowerManager; 41 import android.os.UserManager; 42 import android.provider.Settings; 43 44 import android.view.ContextMenu; 45 import android.view.View; 46 import androidx.fragment.app.FragmentActivity; 47 import androidx.preference.Preference; 48 49 import com.android.settings.R; 50 import com.android.settings.datausage.DataUsagePreference; 51 import com.android.settings.search.SearchIndexableRaw; 52 import com.android.settings.testutils.shadow.ShadowDataUsageUtils; 53 import com.android.settingslib.wifi.AccessPoint; 54 import com.android.settingslib.wifi.WifiTracker; 55 56 import java.util.ArrayList; 57 import org.junit.Before; 58 import org.junit.Test; 59 import org.junit.runner.RunWith; 60 import org.mockito.Mock; 61 import org.mockito.MockitoAnnotations; 62 import org.robolectric.RobolectricTestRunner; 63 import org.robolectric.RuntimeEnvironment; 64 import org.robolectric.annotation.Config; 65 66 import java.util.List; 67 68 @RunWith(RobolectricTestRunner.class) 69 public class WifiSettingsTest { 70 71 private static final int NUM_NETWORKS = 4; 72 73 @Mock 74 private WifiTracker mWifiTracker; 75 @Mock 76 private PowerManager mPowerManager; 77 @Mock 78 private DataUsagePreference mDataUsagePreference; 79 @Mock 80 private WifiManager mWifiManager; 81 private Context mContext; 82 private WifiSettings mWifiSettings; 83 84 @Before setUp()85 public void setUp() { 86 MockitoAnnotations.initMocks(this); 87 mContext = spy(RuntimeEnvironment.application); 88 89 mWifiSettings = spy(new WifiSettings()); 90 doReturn(mContext).when(mWifiSettings).getContext(); 91 doReturn(mPowerManager).when(mContext).getSystemService(PowerManager.class); 92 mWifiSettings.mAddWifiNetworkPreference = new AddWifiNetworkPreference(mContext); 93 mWifiSettings.mSavedNetworksPreference = new Preference(mContext); 94 mWifiSettings.mConfigureWifiSettingsPreference = new Preference(mContext); 95 mWifiSettings.mWifiTracker = mWifiTracker; 96 mWifiSettings.mWifiManager = mWifiManager; 97 } 98 99 @Test testSearchIndexProvider_shouldIndexFragmentTitle()100 public void testSearchIndexProvider_shouldIndexFragmentTitle() { 101 final List<SearchIndexableRaw> indexRes = 102 WifiSettings.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(mContext, 103 true /* enabled */); 104 105 assertThat(indexRes).isNotNull(); 106 assertThat(indexRes.get(0).key).isEqualTo(WifiSettings.DATA_KEY_REFERENCE); 107 } 108 109 @Test 110 @Config(qualifiers = "mcc999") testSearchIndexProvider_ifWifiSettingsNotVisible_shouldNotIndexFragmentTitle()111 public void testSearchIndexProvider_ifWifiSettingsNotVisible_shouldNotIndexFragmentTitle() { 112 final List<SearchIndexableRaw> indexRes = 113 WifiSettings.SEARCH_INDEX_DATA_PROVIDER.getRawDataToIndex(mContext, 114 true /* enabled */); 115 116 assertThat(indexRes).isEmpty(); 117 } 118 119 @Test addNetworkFragmentSendResult_onActivityResult_shouldHandleEvent()120 public void addNetworkFragmentSendResult_onActivityResult_shouldHandleEvent() { 121 final WifiSettings wifiSettings = spy(new WifiSettings()); 122 final Intent intent = new Intent(); 123 doNothing().when(wifiSettings).handleAddNetworkRequest(anyInt(), any(Intent.class)); 124 125 wifiSettings.onActivityResult(WifiSettings.ADD_NETWORK_REQUEST, Activity.RESULT_OK, intent); 126 127 verify(wifiSettings).handleAddNetworkRequest(anyInt(), any(Intent.class)); 128 } 129 createMockWifiConfigurations(int count)130 private List<WifiConfiguration> createMockWifiConfigurations(int count) { 131 final List<WifiConfiguration> mockConfigs = new ArrayList<>(); 132 for (int i = 0; i < count; i++) { 133 mockConfigs.add(new WifiConfiguration()); 134 } 135 return mockConfigs; 136 } 137 createMockPasspointConfigurations(int count)138 private List<PasspointConfiguration> createMockPasspointConfigurations(int count) { 139 final List<PasspointConfiguration> mockConfigs = new ArrayList<>(); 140 for (int i = 0; i < count; i++) { 141 final HomeSp sp = new HomeSp(); 142 sp.setFqdn("fqdn"); 143 final PasspointConfiguration config = new PasspointConfiguration(); 144 config.setHomeSp(sp); 145 mockConfigs.add(config); 146 } 147 return mockConfigs; 148 } 149 150 @Test setAdditionalSettingsSummaries_hasSavedNetwork_preferenceVisible()151 public void setAdditionalSettingsSummaries_hasSavedNetwork_preferenceVisible() { 152 when(mWifiManager.getConfiguredNetworks()) 153 .thenReturn(createMockWifiConfigurations(NUM_NETWORKS)); 154 155 mWifiSettings.setAdditionalSettingsSummaries(); 156 157 assertThat(mWifiSettings.mSavedNetworksPreference.isVisible()).isTrue(); 158 assertThat(mWifiSettings.mSavedNetworksPreference.getSummary()).isEqualTo( 159 mContext.getResources().getQuantityString( 160 R.plurals.wifi_saved_access_points_summary, 161 NUM_NETWORKS, NUM_NETWORKS)); 162 } 163 164 @Test setAdditionalSettingsSummaries_hasSavedPasspointNetwork_preferenceVisible()165 public void setAdditionalSettingsSummaries_hasSavedPasspointNetwork_preferenceVisible() { 166 when(mWifiManager.getPasspointConfigurations()) 167 .thenReturn(createMockPasspointConfigurations(NUM_NETWORKS)); 168 169 mWifiSettings.setAdditionalSettingsSummaries(); 170 171 assertThat(mWifiSettings.mSavedNetworksPreference.isVisible()).isTrue(); 172 assertThat(mWifiSettings.mSavedNetworksPreference.getSummary()).isEqualTo( 173 mContext.getResources().getQuantityString( 174 R.plurals.wifi_saved_passpoint_access_points_summary, 175 NUM_NETWORKS, NUM_NETWORKS)); 176 } 177 178 @Test setAdditionalSettingsSummaries_hasTwoKindsSavedNetwork_preferenceVisible()179 public void setAdditionalSettingsSummaries_hasTwoKindsSavedNetwork_preferenceVisible() { 180 when(mWifiManager.getConfiguredNetworks()) 181 .thenReturn(createMockWifiConfigurations(NUM_NETWORKS)); 182 when(mWifiManager.getPasspointConfigurations()) 183 .thenReturn(createMockPasspointConfigurations(NUM_NETWORKS)); 184 185 mWifiSettings.setAdditionalSettingsSummaries(); 186 187 assertThat(mWifiSettings.mSavedNetworksPreference.isVisible()).isTrue(); 188 assertThat(mWifiSettings.mSavedNetworksPreference.getSummary()).isEqualTo( 189 mContext.getResources().getQuantityString( 190 R.plurals.wifi_saved_all_access_points_summary, 191 NUM_NETWORKS*2, NUM_NETWORKS*2)); 192 } 193 194 @Test setAdditionalSettingsSummaries_noSavedNetwork_preferenceInvisible()195 public void setAdditionalSettingsSummaries_noSavedNetwork_preferenceInvisible() { 196 when(mWifiManager.getConfiguredNetworks()) 197 .thenReturn(createMockWifiConfigurations(0 /* count */)); 198 199 mWifiSettings.setAdditionalSettingsSummaries(); 200 201 assertThat(mWifiSettings.mSavedNetworksPreference.isVisible()).isFalse(); 202 } 203 204 @Test setAdditionalSettingsSummaries_wifiWakeupEnabled_displayOn()205 public void setAdditionalSettingsSummaries_wifiWakeupEnabled_displayOn() { 206 final ContentResolver contentResolver = mContext.getContentResolver(); 207 Settings.Global.putInt(contentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 1); 208 Settings.Global.putInt(contentResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1); 209 Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0); 210 when(mPowerManager.isPowerSaveMode()).thenReturn(false); 211 212 mWifiSettings.setAdditionalSettingsSummaries(); 213 214 assertThat(mWifiSettings.mConfigureWifiSettingsPreference.getSummary()).isEqualTo( 215 mContext.getString(R.string.wifi_configure_settings_preference_summary_wakeup_on)); 216 } 217 218 @Test setAdditionalSettingsSummaries_wifiWakeupDisabled_displayOff()219 public void setAdditionalSettingsSummaries_wifiWakeupDisabled_displayOff() { 220 final ContentResolver contentResolver = mContext.getContentResolver(); 221 Settings.Global.putInt(contentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 0); 222 223 mWifiSettings.setAdditionalSettingsSummaries(); 224 225 assertThat(mWifiSettings.mConfigureWifiSettingsPreference.getSummary()).isEqualTo( 226 mContext.getString(R.string.wifi_configure_settings_preference_summary_wakeup_off)); 227 } 228 229 @Test checkAddWifiNetworkPrefernce_preferenceVisible()230 public void checkAddWifiNetworkPrefernce_preferenceVisible() { 231 assertThat(mWifiSettings.mAddWifiNetworkPreference.isVisible()).isTrue(); 232 assertThat(mWifiSettings.mAddWifiNetworkPreference.getTitle()).isEqualTo( 233 mContext.getString(R.string.wifi_add_network)); 234 } 235 setUpForOnCreate()236 private void setUpForOnCreate() { 237 final FragmentActivity activity = mock(FragmentActivity.class); 238 when(mWifiSettings.getActivity()).thenReturn(activity); 239 final Resources.Theme theme = mContext.getTheme(); 240 when(activity.getTheme()).thenReturn(theme); 241 UserManager userManager = mock(UserManager.class); 242 when(activity.getSystemService(Context.USER_SERVICE)) 243 .thenReturn(userManager); 244 245 when(mWifiSettings.findPreference(WifiSettings.PREF_KEY_DATA_USAGE)) 246 .thenReturn(mDataUsagePreference); 247 } 248 249 @Test 250 @Config(shadows = {ShadowDataUsageUtils.class}) checkDataUsagePreference_perferenceInvisibleIfWifiNotSupported()251 public void checkDataUsagePreference_perferenceInvisibleIfWifiNotSupported() { 252 setUpForOnCreate(); 253 ShadowDataUsageUtils.IS_WIFI_SUPPORTED = false; 254 255 mWifiSettings.onCreate(Bundle.EMPTY); 256 257 verify(mDataUsagePreference).setVisible(false); 258 } 259 260 @Test 261 @Config(shadows = {ShadowDataUsageUtils.class}) checkDataUsagePreference_perferenceVisibleIfWifiSupported()262 public void checkDataUsagePreference_perferenceVisibleIfWifiSupported() { 263 setUpForOnCreate(); 264 ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true; 265 266 mWifiSettings.onCreate(Bundle.EMPTY); 267 268 verify(mDataUsagePreference).setVisible(true); 269 verify(mDataUsagePreference).setTemplate(any(), eq(0) /*subId*/, eq(null) /*service*/); 270 } 271 272 @Test onCreateContextMenu_shouldHaveForgetMenuForConnectedAccessPreference()273 public void onCreateContextMenu_shouldHaveForgetMenuForConnectedAccessPreference() { 274 final FragmentActivity mockActivity = mock(FragmentActivity.class); 275 when(mockActivity.getApplicationContext()).thenReturn(mContext); 276 when(mWifiSettings.getActivity()).thenReturn(mockActivity); 277 278 final AccessPoint accessPoint = mock(AccessPoint.class); 279 when(accessPoint.isConnectable()).thenReturn(false); 280 when(accessPoint.isSaved()).thenReturn(true); 281 when(accessPoint.isActive()).thenReturn(true); 282 283 final ConnectedAccessPointPreference connectedPreference = 284 mWifiSettings.createConnectedAccessPointPreference(accessPoint, mContext); 285 final View view = mock(View.class); 286 when(view.getTag()).thenReturn(connectedPreference); 287 288 final ContextMenu menu = mock(ContextMenu.class); 289 mWifiSettings.onCreateContextMenu(menu, view, null /* info */); 290 291 verify(menu).add(anyInt(), eq(WifiSettings.MENU_ID_FORGET), anyInt(), anyInt()); 292 } 293 } 294