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 package com.android.settings.wifi;
     17 
     18 import static com.google.common.truth.Truth.assertThat;
     19 import static org.mockito.Matchers.any;
     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.app.FragmentManager;
     26 import android.content.BroadcastReceiver;
     27 import android.content.Context;
     28 import android.content.Intent;
     29 import android.content.IntentFilter;
     30 import android.net.wifi.WifiManager;
     31 import android.support.v7.preference.Preference;
     32 import android.support.v7.preference.PreferenceScreen;
     33 
     34 import com.android.settings.SettingsRobolectricTestRunner;
     35 import com.android.settings.TestConfig;
     36 import com.android.settings.core.lifecycle.Lifecycle;
     37 
     38 import org.junit.Before;
     39 import org.junit.Test;
     40 import org.junit.runner.RunWith;
     41 import org.mockito.Answers;
     42 import org.mockito.Mock;
     43 import org.mockito.MockitoAnnotations;
     44 import org.robolectric.annotation.Config;
     45 
     46 @RunWith(SettingsRobolectricTestRunner.class)
     47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     48 public class WpsPreferenceControllerTest {
     49 
     50     @Mock
     51     private Context mContext;
     52     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     53     private WifiManager mWifiManager;
     54     @Mock
     55     private FragmentManager mFragmentManager;
     56     @Mock
     57     private PreferenceScreen mScreen;
     58     @Mock
     59     private Preference mWpsPushPref;
     60     @Mock
     61     private Preference mWpsPinPref;
     62 
     63     private Lifecycle mLifecycle;
     64     private WpsPreferenceController mController;
     65 
     66     @Before
     67     public void setUp() {
     68         MockitoAnnotations.initMocks(this);
     69         mLifecycle = new Lifecycle();
     70         when(mContext.getSystemService(WifiManager.class))
     71                 .thenReturn(mWifiManager);
     72         when(mScreen.findPreference(anyString()))
     73                 .thenReturn(mWpsPushPref)
     74                 .thenReturn(mWpsPinPref);
     75         mController = new WpsPreferenceController(
     76                 mContext, mLifecycle, mWifiManager, mFragmentManager);
     77     }
     78 
     79     @Test
     80     public void testIsAvailable_shouldAlwaysReturnTrue() {
     81         assertThat(mController.isAvailable()).isTrue();
     82     }
     83 
     84     @Test
     85     public void testOnResume_shouldRegisterListener() {
     86         mLifecycle.onResume();
     87         verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
     88     }
     89     @Test
     90     public void testOnPause_shouldUnregisterListener() {
     91         mLifecycle.onPause();
     92         verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
     93     }
     94 
     95     @Test
     96     public void testWifiStateChange_shouldToggleEnabledState() {
     97         when(mWifiManager.isWifiEnabled()).thenReturn(true);
     98 
     99         //Sets the preferences.
    100         mController.displayPreference(mScreen);
    101         verify(mWpsPushPref).setEnabled(true);
    102         verify(mWpsPinPref).setEnabled(true);
    103 
    104         Intent dummyIntent = new Intent();
    105         mController.mReceiver.onReceive(mContext, dummyIntent);
    106         verify(mWpsPushPref, times(2)).setEnabled(true);
    107         verify(mWpsPinPref, times(2)).setEnabled(true);
    108 
    109         when(mWifiManager.isWifiEnabled()).thenReturn(false);
    110         mController.mReceiver.onReceive(mContext, dummyIntent);
    111         verify(mWpsPushPref).setEnabled(false);
    112         verify(mWpsPinPref).setEnabled(false);
    113     }
    114 
    115     @Test
    116     public void testDisplayPreference_shouldSetPreferenceClickListenerAndToggleEnabledState() {
    117         when(mWifiManager.isWifiEnabled()).thenReturn(true);
    118         mController.displayPreference(mScreen);
    119         verify(mWpsPushPref).setOnPreferenceClickListener(any());
    120         verify(mWpsPinPref).setOnPreferenceClickListener(any());
    121         verify(mWpsPushPref).setEnabled(true);
    122         verify(mWpsPinPref).setEnabled(true);
    123     }
    124 
    125     @Test
    126     public void testDisplayPreference_shouldDisablePreferenceWhenWifiDisabled() {
    127         when(mWifiManager.isWifiEnabled()).thenReturn(false);
    128         mController.displayPreference(mScreen);
    129         verify(mWpsPushPref).setEnabled(false);
    130         verify(mWpsPinPref).setEnabled(false);
    131     }
    132 }
    133