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).enableVerboseLogging( 63 WifiVerboseLoggingPreferenceController.SETTING_VALUE_ON); 64 } 65 66 @Test onPreferenceChange_settingDisabled_shouldDisablVerboseLogging()67 public void onPreferenceChange_settingDisabled_shouldDisablVerboseLogging() { 68 mController.onPreferenceChange(mPreference, false /* new value */); 69 70 verify(mWifiManager).enableVerboseLogging( 71 WifiVerboseLoggingPreferenceController.SETTING_VALUE_OFF); 72 } 73 74 @Test updateState_settingEnabled_shouldEnablePreference()75 public void updateState_settingEnabled_shouldEnablePreference() { 76 when(mWifiManager.getVerboseLoggingLevel()).thenReturn(1); 77 mController.updateState(mPreference); 78 79 verify(mPreference).setChecked(true); 80 } 81 82 @Test updateState_settingDisabled_shouldDisablePreference()83 public void updateState_settingDisabled_shouldDisablePreference() { 84 when(mWifiManager.getVerboseLoggingLevel()).thenReturn(0); 85 mController.updateState(mPreference); 86 87 verify(mPreference).setChecked(false); 88 } 89 90 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()91 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 92 mController.onDeveloperOptionsSwitchDisabled(); 93 94 verify(mWifiManager) 95 .enableVerboseLogging(WifiVerboseLoggingPreferenceController.SETTING_VALUE_OFF); 96 verify(mPreference).setEnabled(false); 97 verify(mPreference).setChecked(false); 98 } 99 } 100