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; 18 19 import android.content.Context; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.widget.Spinner; 23 import android.widget.TextView; 24 25 import com.android.settings.R; 26 import com.android.settings.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.testutils.shadow.ShadowConnectivityManager; 29 import com.android.settingslib.wifi.AccessPoint; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RuntimeEnvironment; 37 import org.robolectric.annotation.Config; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Mockito.when; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 44 shadows = ShadowConnectivityManager.class) 45 public class WifiConfigControllerTest { 46 47 @Mock private WifiConfigUiBase mConfigUiBase; 48 @Mock private Context mContext; 49 @Mock private View mView; 50 @Mock private AccessPoint mAccessPoint; 51 52 public WifiConfigController mController; 53 54 // An invalid PSK pass phrase. It is 64 characters long, must not be greater than 63 55 private static final String LONG_PSK = 56 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"; 57 // An invalid PSK pass phrase. It is 7 characters long, must be at least 8 58 private static final String SHORT_PSK = "abcdefg"; 59 // Valid PSK pass phrase 60 private static final String GOOD_PSK = "abcdefghijklmnopqrstuvwxyz"; 61 private static final int DHCP = 0; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 mContext = RuntimeEnvironment.application; 67 when(mConfigUiBase.getContext()).thenReturn(mContext); 68 when(mAccessPoint.getSecurity()).thenReturn(AccessPoint.SECURITY_PSK); 69 mView = LayoutInflater.from(mContext).inflate(R.layout.wifi_dialog, null); 70 final Spinner ipSettingsSpinner = mView.findViewById(R.id.ip_settings); 71 ipSettingsSpinner.setSelection(DHCP); 72 73 mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint, 74 WifiConfigUiBase.MODE_CONNECT); 75 } 76 @Test isSubmittable_noSSID_shouldReturnFalse()77 public void isSubmittable_noSSID_shouldReturnFalse() { 78 final TextView ssid = mView.findViewById(R.id.ssid); 79 ssid.setText(""); 80 assertThat(mController.isSubmittable()).isFalse(); 81 } 82 83 @Test isSubmittable_longPsk_shouldReturnFalse()84 public void isSubmittable_longPsk_shouldReturnFalse() { 85 final TextView password = mView.findViewById(R.id.password); 86 password.setText(LONG_PSK); 87 assertThat(mController.isSubmittable()).isFalse(); 88 89 } 90 @Test isSubmittable_shortPsk_shouldReturnFalse()91 public void isSubmittable_shortPsk_shouldReturnFalse() { 92 final TextView password = mView.findViewById(R.id.password); 93 password.setText(SHORT_PSK); 94 assertThat(mController.isSubmittable()).isFalse(); 95 96 } 97 @Test isSubmittable_goodPsk_shouldReturnTrue()98 public void isSubmittable_goodPsk_shouldReturnTrue() { 99 final TextView password = mView.findViewById(R.id.password); 100 password.setText(GOOD_PSK); 101 assertThat(mController.isSubmittable()).isTrue(); 102 103 } 104 @Test isSubmittable_savedConfigZeroLengthPassword_shouldReturnTrue()105 public void isSubmittable_savedConfigZeroLengthPassword_shouldReturnTrue() { 106 final TextView password = mView.findViewById(R.id.password); 107 password.setText(""); 108 when(mAccessPoint.isSaved()).thenReturn(true); 109 assertThat(mController.isSubmittable()).isTrue(); 110 } 111 112 @Test isSubmittable_nullAccessPoint_noException()113 public void isSubmittable_nullAccessPoint_noException() { 114 mController = new TestWifiConfigController(mConfigUiBase, mView, null, 115 WifiConfigUiBase.MODE_CONNECT); 116 mController.isSubmittable(); 117 } 118 119 @Test getSignalString_notReachable_shouldHaveNoSignalString()120 public void getSignalString_notReachable_shouldHaveNoSignalString() { 121 when(mAccessPoint.isReachable()).thenReturn(false); 122 123 assertThat(mController.getSignalString()).isNull(); 124 } 125 126 public class TestWifiConfigController extends WifiConfigController { 127 TestWifiConfigController(WifiConfigUiBase parent, View view, AccessPoint accessPoint, int mode)128 public TestWifiConfigController(WifiConfigUiBase parent, View view, 129 AccessPoint accessPoint, int mode) { 130 super(parent, view, accessPoint, mode); 131 } 132 133 @Override isSplitSystemUser()134 boolean isSplitSystemUser() { 135 return false; 136 } 137 } 138 } 139