Home | History | Annotate | Download | only in wifi
      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 static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Mockito.any;
     21 import static org.mockito.Mockito.anyBoolean;
     22 import static org.mockito.Mockito.anyInt;
     23 import static org.mockito.Mockito.anyString;
     24 import static org.mockito.Mockito.mock;
     25 import static org.mockito.Mockito.verify;
     26 import static org.mockito.Mockito.when;
     27 
     28 import android.content.Context;
     29 import android.os.ServiceSpecificException;
     30 import android.security.KeyStore;
     31 import android.view.LayoutInflater;
     32 import android.view.View;
     33 import android.view.ViewGroup;
     34 import android.widget.Spinner;
     35 import android.widget.TextView;
     36 
     37 import com.android.settings.R;
     38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     39 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
     40 import com.android.settingslib.wifi.AccessPoint;
     41 
     42 import org.junit.Before;
     43 import org.junit.Test;
     44 import org.junit.runner.RunWith;
     45 import org.mockito.Mock;
     46 import org.mockito.MockitoAnnotations;
     47 import org.robolectric.RuntimeEnvironment;
     48 import org.robolectric.annotation.Config;
     49 
     50 @RunWith(SettingsRobolectricTestRunner.class)
     51 @Config(shadows = ShadowConnectivityManager.class)
     52 public class WifiConfigControllerTest {
     53 
     54     @Mock
     55     private WifiConfigUiBase mConfigUiBase;
     56     @Mock
     57     private Context mContext;
     58     @Mock
     59     private View mView;
     60     @Mock
     61     private AccessPoint mAccessPoint;
     62     @Mock
     63     private KeyStore mKeyStore;
     64     private Spinner mHiddenSettingsSpinner;
     65 
     66     public WifiConfigController mController;
     67     private static final String HEX_PSK = "01234567012345670123456701234567012345670123456701234567"
     68             + "01abcdef";
     69     // An invalid ASCII PSK pass phrase. It is 64 characters long, must not be greater than 63
     70     private static final String LONG_PSK =
     71             "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl";
     72     // An invalid PSK pass phrase. It is 7 characters long, must be at least 8
     73     private static final String SHORT_PSK = "abcdefg";
     74     // Valid PSK pass phrase
     75     private static final String GOOD_PSK = "abcdefghijklmnopqrstuvwxyz";
     76     private static final int DHCP = 0;
     77 
     78     @Before
     79     public void setUp() {
     80         MockitoAnnotations.initMocks(this);
     81         mContext = RuntimeEnvironment.application;
     82         when(mConfigUiBase.getContext()).thenReturn(mContext);
     83         when(mAccessPoint.getSecurity()).thenReturn(AccessPoint.SECURITY_PSK);
     84         mView = LayoutInflater.from(mContext).inflate(R.layout.wifi_dialog, null);
     85         final Spinner ipSettingsSpinner = mView.findViewById(R.id.ip_settings);
     86         mHiddenSettingsSpinner = mView.findViewById(R.id.hidden_settings);
     87         ipSettingsSpinner.setSelection(DHCP);
     88 
     89         mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
     90                 WifiConfigUiBase.MODE_CONNECT);
     91     }
     92 
     93     @Test
     94     public void ssidExceeds32Bytes_shouldShowSsidTooLongWarning() {
     95         mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
     96                 WifiConfigUiBase.MODE_CONNECT);
     97         final TextView ssid = mView.findViewById(R.id.ssid);
     98         assertThat(ssid).isNotNull();
     99         ssid.setText("");
    100         mController.showWarningMessagesIfAppropriate();
    101 
    102         assertThat(mView.findViewById(R.id.ssid_too_long_warning).getVisibility())
    103                 .isEqualTo(View.VISIBLE);
    104     }
    105 
    106     @Test
    107     public void ssidShorterThan32Bytes_shouldNotShowSsidTooLongWarning() {
    108         mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
    109                 WifiConfigUiBase.MODE_CONNECT);
    110 
    111         final TextView ssid = mView.findViewById(R.id.ssid);
    112         assertThat(ssid).isNotNull();
    113         ssid.setText("123456789012345678901234567890");
    114         mController.showWarningMessagesIfAppropriate();
    115 
    116         assertThat(mView.findViewById(R.id.ssid_too_long_warning).getVisibility())
    117                 .isEqualTo(View.GONE);
    118 
    119         ssid.setText("123");
    120         mController.showWarningMessagesIfAppropriate();
    121 
    122         assertThat(mView.findViewById(R.id.ssid_too_long_warning).getVisibility())
    123                 .isEqualTo(View.GONE);
    124     }
    125 
    126     @Test
    127     public void isSubmittable_noSSID_shouldReturnFalse() {
    128         final TextView ssid = mView.findViewById(R.id.ssid);
    129         assertThat(ssid).isNotNull();
    130         ssid.setText("");
    131         assertThat(mController.isSubmittable()).isFalse();
    132     }
    133 
    134     @Test
    135     public void isSubmittable_longPsk_shouldReturnFalse() {
    136         final TextView password = mView.findViewById(R.id.password);
    137         assertThat(password).isNotNull();
    138         password.setText(LONG_PSK);
    139         assertThat(mController.isSubmittable()).isFalse();
    140 
    141     }
    142 
    143     @Test
    144     public void isSubmittable_shortPsk_shouldReturnFalse() {
    145         final TextView password = mView.findViewById(R.id.password);
    146         assertThat(password).isNotNull();
    147         password.setText(SHORT_PSK);
    148         assertThat(mController.isSubmittable()).isFalse();
    149     }
    150 
    151     @Test
    152     public void isSubmittable_goodPsk_shouldReturnTrue() {
    153         final TextView password = mView.findViewById(R.id.password);
    154         assertThat(password).isNotNull();
    155         password.setText(GOOD_PSK);
    156         assertThat(mController.isSubmittable()).isTrue();
    157 
    158     }
    159 
    160     @Test
    161     public void isSubmittable_hexPsk_shouldReturnTrue() {
    162         final TextView password = mView.findViewById(R.id.password);
    163         assertThat(password).isNotNull();
    164         password.setText(HEX_PSK);
    165         assertThat(mController.isSubmittable()).isTrue();
    166 
    167     }
    168 
    169     @Test
    170     public void isSubmittable_savedConfigZeroLengthPassword_shouldReturnTrue() {
    171         final TextView password = mView.findViewById(R.id.password);
    172         assertThat(password).isNotNull();
    173         password.setText("");
    174         when(mAccessPoint.isSaved()).thenReturn(true);
    175         assertThat(mController.isSubmittable()).isTrue();
    176     }
    177 
    178     @Test
    179     public void isSubmittable_nullAccessPoint_noException() {
    180         mController =
    181             new TestWifiConfigController(mConfigUiBase, mView, null, WifiConfigUiBase.MODE_CONNECT);
    182         mController.isSubmittable();
    183     }
    184 
    185     @Test
    186     public void getSignalString_notReachable_shouldHaveNoSignalString() {
    187         when(mAccessPoint.isReachable()).thenReturn(false);
    188 
    189         assertThat(mController.getSignalString()).isNull();
    190     }
    191 
    192     @Test
    193     public void showForCarrierAp() {
    194         // Setup the mock view for wifi dialog.
    195         View view = mock(View.class);
    196         TextView nameText = mock(TextView.class);
    197         TextView valueText = mock(TextView.class);
    198         when(view.findViewById(R.id.name)).thenReturn(nameText);
    199         when(view.findViewById(R.id.value)).thenReturn(valueText);
    200         LayoutInflater inflater = mock(LayoutInflater.class);
    201         when(inflater.inflate(anyInt(), any(ViewGroup.class), anyBoolean())).thenReturn(view);
    202         when(mConfigUiBase.getLayoutInflater()).thenReturn(inflater);
    203 
    204         String carrierName = "Test Carrier";
    205         when(mAccessPoint.isCarrierAp()).thenReturn(true);
    206         when(mAccessPoint.getCarrierName()).thenReturn(carrierName);
    207         mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
    208                 WifiConfigUiBase.MODE_CONNECT);
    209         // Verify the content of the text fields.
    210         verify(nameText).setText(R.string.wifi_carrier_connect);
    211         verify(valueText).setText(
    212                 String.format(mContext.getString(R.string.wifi_carrier_content), carrierName));
    213         // Verify that the advance toggle is not visible.
    214         assertThat(mView.findViewById(R.id.wifi_advanced_toggle).getVisibility())
    215                 .isEqualTo(View.GONE);
    216         // Verify that the EAP method menu is not visible.
    217         assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.GONE);
    218     }
    219 
    220     @Test
    221     public void loadCertificates_keyStoreListFail_shouldNotCrash() {
    222         // Set up
    223         when(mAccessPoint.getSecurity()).thenReturn(AccessPoint.SECURITY_EAP);
    224         when(mKeyStore.list(anyString()))
    225             .thenThrow(new ServiceSpecificException(-1, "permission error"));
    226 
    227         mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
    228               WifiConfigUiBase.MODE_CONNECT);
    229 
    230         // Verify that the EAP method menu is visible.
    231         assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.VISIBLE);
    232         // No Crash
    233     }
    234 
    235     @Test
    236     public void ssidGetFocus_addNewNetwork_shouldReturnTrue() {
    237         mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
    238                 WifiConfigUiBase.MODE_CONNECT);
    239         final TextView ssid = mView.findViewById(R.id.ssid);
    240         // Verify ssid text get focus when add new network (accesspoint is null)
    241         assertThat(ssid.isFocused()).isTrue();
    242     }
    243 
    244     @Test
    245     public void passwordGetFocus_connectSecureWifi_shouldReturnTrue() {
    246         final TextView password = mView.findViewById(R.id.password);
    247         // Verify password get focus when connect to secure wifi without eap type
    248         assertThat(password.isFocused()).isTrue();
    249     }
    250 
    251     @Test
    252     public void hiddenWarning_warningVisibilityProperlyUpdated() {
    253         View warningView = mView.findViewById(R.id.hidden_settings_warning);
    254         mController.onItemSelected(mHiddenSettingsSpinner, null, mController.HIDDEN_NETWORK, 0);
    255         assertThat(warningView.getVisibility()).isEqualTo(View.VISIBLE);
    256 
    257         mController.onItemSelected(mHiddenSettingsSpinner, null, mController.NOT_HIDDEN_NETWORK, 0);
    258         assertThat(warningView.getVisibility()).isEqualTo(View.GONE);
    259     }
    260 
    261     @Test
    262     public void hiddenField_visibilityUpdatesCorrectly() {
    263         View hiddenField = mView.findViewById(R.id.hidden_settings_field);
    264         assertThat(hiddenField.getVisibility()).isEqualTo(View.GONE);
    265 
    266         mController = new TestWifiConfigController(mConfigUiBase, mView, null /* accessPoint */,
    267                 WifiConfigUiBase.MODE_CONNECT);
    268         assertThat(hiddenField.getVisibility()).isEqualTo(View.VISIBLE);
    269     }
    270 
    271     public class TestWifiConfigController extends WifiConfigController {
    272 
    273         private TestWifiConfigController(
    274             WifiConfigUiBase parent, View view, AccessPoint accessPoint, int mode) {
    275             super(parent, view, accessPoint, mode);
    276         }
    277 
    278         @Override
    279         boolean isSplitSystemUser() {
    280             return false;
    281         }
    282 
    283         @Override
    284         KeyStore getKeyStore() { return mKeyStore; }
    285     }
    286 }
    287