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