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.testutils.SettingsRobolectricTestRunner;
     32 import com.android.settings.widget.ValidatedEditTextPreference;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Answers;
     38 import org.mockito.Mock;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.RuntimeEnvironment;
     41 
     42 @RunWith(SettingsRobolectricTestRunner.class)
     43 public class WifiTetherSSIDPreferenceControllerTest {
     44 
     45     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     46     private Context mContext;
     47     @Mock
     48     private ConnectivityManager mConnectivityManager;
     49     @Mock
     50     private WifiManager mWifiManager;
     51     @Mock
     52     private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener;
     53     @Mock
     54     private PreferenceScreen mScreen;
     55 
     56     private WifiTetherSSIDPreferenceController mController;
     57     private ValidatedEditTextPreference mPreference;
     58 
     59     @Before
     60     public void setUp() {
     61         MockitoAnnotations.initMocks(this);
     62         mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application);
     63 
     64         when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
     65         when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
     66                 .thenReturn(mConnectivityManager);
     67         when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
     68         when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources());
     69         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
     70 
     71         mController = new WifiTetherSSIDPreferenceController(mContext, mListener);
     72     }
     73 
     74     @Test
     75     public void displayPreference_noWifiConfig_shouldDisplayDefaultSSID() {
     76         when(mWifiManager.getWifiApConfiguration()).thenReturn(null);
     77 
     78         mController.displayPreference(mScreen);
     79         assertThat(mController.getSSID())
     80                 .isEqualTo(WifiTetherSSIDPreferenceController.DEFAULT_SSID);
     81     }
     82 
     83     @Test
     84     public void displayPreference_hasCustomWifiConfig_shouldDisplayCustomSSID() {
     85         final WifiConfiguration config = new WifiConfiguration();
     86         config.SSID = "test_1234";
     87         when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
     88 
     89         mController.displayPreference(mScreen);
     90         assertThat(mController.getSSID()).isEqualTo(config.SSID);
     91     }
     92 
     93     @Test
     94     public void changePreference_shouldUpdateValue() {
     95         mController.displayPreference(mScreen);
     96         mController.onPreferenceChange(mPreference, "1");
     97         assertThat(mController.getSSID()).isEqualTo("1");
     98 
     99         mController.onPreferenceChange(mPreference, "0");
    100         assertThat(mController.getSSID()).isEqualTo("0");
    101 
    102         verify(mListener, times(2)).onTetherConfigUpdated();
    103     }
    104 
    105     @Test
    106     public void updateDisplay_shouldUpdateValue() {
    107         // Set controller ssid to anything and verify is set.
    108         mController.displayPreference(mScreen);
    109         mController.onPreferenceChange(mPreference, "1");
    110         assertThat(mController.getSSID()).isEqualTo("1");
    111 
    112         // Create a new config using different SSID
    113         final WifiConfiguration config = new WifiConfiguration();
    114         config.SSID = "test_1234";
    115         when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
    116 
    117         // Call updateDisplay and verify it's changed.
    118         mController.updateDisplay();
    119         assertThat(mController.getSSID()).isEqualTo(config.SSID);
    120         assertThat(mPreference.getSummary()).isEqualTo(config.SSID);
    121     }
    122 }
    123