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.wifi.tether; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.wifi.WifiConfiguration; 22 import android.net.wifi.WifiManager; 23 import android.support.v7.preference.PreferenceScreen; 24 25 import com.android.settings.TestConfig; 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.widget.ValidatedEditTextPreference; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Answers; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.annotation.Config; 37 38 import static com.google.common.truth.Truth.assertThat; 39 import static org.mockito.Matchers.anyString; 40 import static org.mockito.Mockito.times; 41 import static org.mockito.Mockito.verify; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class WifiTetherSSIDPreferenceControllerTest { 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 @Mock 51 private ConnectivityManager mConnectivityManager; 52 @Mock 53 private WifiManager mWifiManager; 54 @Mock 55 private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener; 56 @Mock 57 private PreferenceScreen mScreen; 58 59 private WifiTetherSSIDPreferenceController mController; 60 private ValidatedEditTextPreference mPreference; 61 62 @Before 63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application); 66 67 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 68 when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) 69 .thenReturn(mConnectivityManager); 70 when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); 71 when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources()); 72 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 73 74 mController = new WifiTetherSSIDPreferenceController(mContext, mListener); 75 } 76 77 @Test 78 public void displayPreference_noWifiConfig_shouldDisplayDefaultSSID() { 79 when(mWifiManager.getWifiApConfiguration()).thenReturn(null); 80 81 mController.displayPreference(mScreen); 82 assertThat(mController.getSSID()) 83 .isEqualTo(WifiTetherSSIDPreferenceController.DEFAULT_SSID); 84 } 85 86 @Test 87 public void displayPreference_hasCustomWifiConfig_shouldDisplayCustomSSID() { 88 final WifiConfiguration config = new WifiConfiguration(); 89 config.SSID = "test_1234"; 90 when(mWifiManager.getWifiApConfiguration()).thenReturn(config); 91 92 mController.displayPreference(mScreen); 93 assertThat(mController.getSSID()).isEqualTo(config.SSID); 94 } 95 96 @Test 97 public void changePreference_shouldUpdateValue() { 98 mController.displayPreference(mScreen); 99 mController.onPreferenceChange(mPreference, "1"); 100 assertThat(mController.getSSID()).isEqualTo("1"); 101 102 mController.onPreferenceChange(mPreference, "0"); 103 assertThat(mController.getSSID()).isEqualTo("0"); 104 105 verify(mListener, times(2)).onTetherConfigUpdated(); 106 } 107 } 108