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 17package com.android.settings.wifi.tether; 18 19import static com.google.common.truth.Truth.assertThat; 20import static org.mockito.Matchers.anyString; 21import static org.mockito.Mockito.times; 22import static org.mockito.Mockito.verify; 23import static org.mockito.Mockito.when; 24 25import android.content.Context; 26import android.net.ConnectivityManager; 27import android.net.wifi.WifiConfiguration; 28import android.net.wifi.WifiManager; 29import android.support.v7.preference.PreferenceScreen; 30 31import com.android.settings.testutils.SettingsRobolectricTestRunner; 32import com.android.settings.widget.ValidatedEditTextPreference; 33 34import org.junit.Before; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37import org.mockito.Answers; 38import org.mockito.Mock; 39import org.mockito.MockitoAnnotations; 40import org.robolectric.RuntimeEnvironment; 41 42@RunWith(SettingsRobolectricTestRunner.class) 43public class WifiTetherPasswordPreferenceControllerTest { 44 45 private static final String VALID_PASS = "12345678"; 46 private static final String VALID_PASS2 = "23456789"; 47 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 48 private Context mContext; 49 @Mock 50 private ConnectivityManager mConnectivityManager; 51 @Mock 52 private WifiManager mWifiManager; 53 @Mock 54 private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener; 55 @Mock 56 private PreferenceScreen mScreen; 57 58 private WifiTetherPasswordPreferenceController mController; 59 private ValidatedEditTextPreference mPreference; 60 private WifiConfiguration mConfig; 61 62 @Before 63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application); 66 mConfig = new WifiConfiguration(); 67 mConfig.SSID = "test_1234"; 68 mConfig.preSharedKey = "test_password"; 69 70 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 71 when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig); 72 when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) 73 .thenReturn(mConnectivityManager); 74 when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"}); 75 when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources()); 76 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 77 78 mController = new WifiTetherPasswordPreferenceController(mContext, mListener); 79 } 80 81 @Test 82 public void displayPreference_shouldStylePreference() { 83 mController.displayPreference(mScreen); 84 85 assertThat(mPreference.getText()).isEqualTo(mConfig.preSharedKey); 86 assertThat(mPreference.getSummary()).isEqualTo(mConfig.preSharedKey); 87 } 88 89 @Test 90 public void changePreference_shouldUpdateValue() { 91 mController.displayPreference(mScreen); 92 mController.onPreferenceChange(mPreference, VALID_PASS); 93 assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) 94 .isEqualTo(VALID_PASS); 95 96 mController.onPreferenceChange(mPreference, VALID_PASS2); 97 assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) 98 .isEqualTo(VALID_PASS2); 99 100 verify(mListener, times(2)).onTetherConfigUpdated(); 101 } 102 103 @Test 104 public void updateDisplay_shouldUpdateValue() { 105 // Set controller password to anything and verify is set. 106 mController.displayPreference(mScreen); 107 mController.onPreferenceChange(mPreference, VALID_PASS); 108 assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) 109 .isEqualTo(VALID_PASS); 110 111 // Create a new config using different password 112 final WifiConfiguration config = new WifiConfiguration(); 113 config.preSharedKey = VALID_PASS2; 114 when(mWifiManager.getWifiApConfiguration()).thenReturn(config); 115 116 // Call updateDisplay and verify it's changed. 117 mController.updateDisplay(); 118 assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) 119 .isEqualTo(config.preSharedKey); 120 assertThat(mPreference.getSummary()).isEqualTo(config.preSharedKey); 121 } 122 123 @Test 124 public void updateDisplay_shouldSetInputType() { 125 // Set controller password to anything and verify is set. 126 mController.displayPreference(mScreen); 127 mController.onPreferenceChange(mPreference, VALID_PASS); 128 assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK)) 129 .isEqualTo(VALID_PASS); 130 131 // Create a new config using different password 132 final WifiConfiguration config = new WifiConfiguration(); 133 config.preSharedKey = VALID_PASS2; 134 when(mWifiManager.getWifiApConfiguration()).thenReturn(config); 135 136 // Call updateDisplay and verify it's changed. 137 mController.updateDisplay(); 138 assertThat(mPreference.isPassword()).isTrue(); 139 } 140} 141