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