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.location; 17 18 import static org.mockito.Mockito.spy; 19 import static org.mockito.Mockito.verify; 20 import static org.mockito.Mockito.when; 21 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.net.wifi.WifiManager; 25 26 import androidx.preference.SwitchPreference; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 36 @RunWith(RobolectricTestRunner.class) 37 public class WifiScanningPreferenceControllerTest { 38 39 @Mock 40 private SwitchPreference mPreference; 41 @Mock 42 private WifiManager mWifiManager; 43 44 private Context mContext; 45 private ContentResolver mContentResolver; 46 private WifiScanningPreferenceController mController; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 52 mContext = spy(RuntimeEnvironment.application); 53 when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); 54 55 mController = new WifiScanningPreferenceController(mContext); 56 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 57 } 58 59 @Test updateState_wifiScanningEnabled_shouldCheckedPreference()60 public void updateState_wifiScanningEnabled_shouldCheckedPreference() { 61 when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true); 62 63 mController.updateState(mPreference); 64 65 verify(mPreference).setChecked(true); 66 } 67 68 @Test updateState_wifiScanningDisabled_shouldUncheckedPreference()69 public void updateState_wifiScanningDisabled_shouldUncheckedPreference() { 70 when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false); 71 72 mController.updateState(mPreference); 73 74 verify(mPreference).setChecked(false); 75 } 76 77 @Test handlePreferenceTreeClick_checked_shouldEnableWifiScanning()78 public void handlePreferenceTreeClick_checked_shouldEnableWifiScanning() { 79 when(mPreference.isChecked()).thenReturn(true); 80 81 mController.handlePreferenceTreeClick(mPreference); 82 83 verify(mWifiManager).setScanAlwaysAvailable(true); 84 } 85 86 @Test handlePreferenceTreeClick_unchecked_shouldDisableWifiScanning()87 public void handlePreferenceTreeClick_unchecked_shouldDisableWifiScanning() { 88 when(mPreference.isChecked()).thenReturn(false); 89 90 mController.handlePreferenceTreeClick(mPreference); 91 92 verify(mWifiManager).setScanAlwaysAvailable(false); 93 } 94 } 95