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.development; 18 19 import static org.mockito.Mockito.verify; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 import android.net.wifi.WifiManager; 24 25 import androidx.preference.PreferenceScreen; 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 35 @RunWith(RobolectricTestRunner.class) 36 public class WifiVerboseLoggingPreferenceControllerTest { 37 @Mock 38 private Context mContext; 39 @Mock 40 private WifiManager mWifiManager; 41 @Mock 42 private SwitchPreference mPreference; 43 @Mock 44 private PreferenceScreen mPreferenceScreen; 45 46 private WifiVerboseLoggingPreferenceController mController; 47 48 @Before setup()49 public void setup() { 50 MockitoAnnotations.initMocks(this); 51 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 52 mController = new WifiVerboseLoggingPreferenceController(mContext); 53 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 54 .thenReturn(mPreference); 55 mController.displayPreference(mPreferenceScreen); 56 } 57 58 @Test onPreferenceChange_settingEnabled_shouldEnableVerboseLogging()59 public void onPreferenceChange_settingEnabled_shouldEnableVerboseLogging() { 60 mController.onPreferenceChange(mPreference, true /* new value */); 61 62 verify(mWifiManager).setVerboseLoggingEnabled(true); 63 } 64 65 @Test onPreferenceChange_settingDisabled_shouldDisablVerboseLogging()66 public void onPreferenceChange_settingDisabled_shouldDisablVerboseLogging() { 67 mController.onPreferenceChange(mPreference, false /* new value */); 68 69 verify(mWifiManager).setVerboseLoggingEnabled(false); 70 } 71 72 @Test updateState_settingEnabled_shouldEnablePreference()73 public void updateState_settingEnabled_shouldEnablePreference() { 74 when(mWifiManager.isVerboseLoggingEnabled()).thenReturn(true); 75 mController.updateState(mPreference); 76 77 verify(mPreference).setChecked(true); 78 } 79 80 @Test updateState_settingDisabled_shouldDisablePreference()81 public void updateState_settingDisabled_shouldDisablePreference() { 82 when(mWifiManager.isVerboseLoggingEnabled()).thenReturn(false); 83 mController.updateState(mPreference); 84 85 verify(mPreference).setChecked(false); 86 } 87 88 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()89 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 90 mController.onDeveloperOptionsSwitchDisabled(); 91 92 verify(mWifiManager).setVerboseLoggingEnabled(false); 93 verify(mPreference).setEnabled(false); 94 verify(mPreference).setChecked(false); 95 } 96 } 97