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 android.arch.lifecycle.Lifecycle.Event.ON_PAUSE; 19 import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Matchers.any; 22 import static org.mockito.Matchers.anyString; 23 import static org.mockito.Mockito.times; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.arch.lifecycle.LifecycleOwner; 28 import android.content.BroadcastReceiver; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.IntentFilter; 32 import android.net.wifi.WifiManager; 33 import android.support.v7.preference.Preference; 34 import android.support.v7.preference.PreferenceScreen; 35 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 import com.android.settingslib.core.lifecycle.Lifecycle; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Answers; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 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 LifecycleOwner mLifecycleOwner; 60 private WifiP2pPreferenceController mController; 61 62 @Before 63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mLifecycleOwner = () -> mLifecycle; 66 mLifecycle = new Lifecycle(mLifecycleOwner); 67 when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); 68 when(mScreen.findPreference(anyString())).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.handleLifecycleEvent(ON_RESUME); 80 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); 81 } 82 83 @Test 84 public void testOnPause_shouldUnregisterListener() { 85 mLifecycle.handleLifecycleEvent(ON_RESUME); 86 mLifecycle.handleLifecycleEvent(ON_PAUSE); 87 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 88 } 89 90 @Test 91 public void testWifiStateChange_shouldToggleEnabledState() { 92 when(mWifiManager.isWifiEnabled()).thenReturn(true); 93 94 //Sets the preferences. 95 mController.displayPreference(mScreen); 96 verify(mWifiDirectPreference).setEnabled(true); 97 98 Intent dummyIntent = new Intent(); 99 mController.mReceiver.onReceive(mContext, dummyIntent); 100 verify(mWifiDirectPreference, times(2)).setEnabled(true); 101 102 when(mWifiManager.isWifiEnabled()).thenReturn(false); 103 mController.mReceiver.onReceive(mContext, dummyIntent); 104 verify(mWifiDirectPreference).setEnabled(false); 105 } 106 107 @Test 108 public void testDisplayPreference_shouldToggleEnabledState() { 109 when(mWifiManager.isWifiEnabled()).thenReturn(true); 110 mController.displayPreference(mScreen); 111 verify(mWifiDirectPreference).setEnabled(true); 112 113 when(mWifiManager.isWifiEnabled()).thenReturn(false); 114 mController.displayPreference(mScreen); 115 verify(mWifiDirectPreference).setEnabled(false); 116 } 117 } 118