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.wifi; 18 19 import static android.provider.Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE; 20 import static android.provider.Settings.Global.WIFI_WAKEUP_ENABLED; 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.verify; 26 27 import android.app.Fragment; 28 import android.content.Context; 29 import android.location.LocationManager; 30 import android.provider.Settings; 31 import android.support.v14.preference.SwitchPreference; 32 import android.support.v7.preference.Preference; 33 34 import com.android.settings.R; 35 import com.android.settings.dashboard.DashboardFragment; 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 import com.android.settings.testutils.shadow.SettingsShadowResources; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RuntimeEnvironment; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 public class WifiWakeupPreferenceControllerTest { 49 50 private static final String NO_LOCATION_STRING = 51 "Unavailable because location is turned off. Turn on location."; 52 private Context mContext; 53 private WifiWakeupPreferenceController mController; 54 @Mock 55 DashboardFragment mFragment; 56 @Mock 57 LocationManager mLocationManager; 58 @Mock 59 SwitchPreference mPreference; 60 61 @Before setUp()62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 mContext = RuntimeEnvironment.application; 65 mController = new WifiWakeupPreferenceController(mContext, mFragment); 66 mController.mLocationManager = mLocationManager; 67 mController.mPreference = mPreference; 68 69 Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1); 70 doReturn(true).when(mLocationManager).isLocationEnabled(); 71 } 72 73 @After tearDown()74 public void tearDown() { 75 SettingsShadowResources.reset(); 76 } 77 78 @Test handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing()79 public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() { 80 final SwitchPreference pref = new SwitchPreference(mContext); 81 82 assertThat(mController.handlePreferenceTreeClick(pref)).isFalse(); 83 } 84 85 @Test handlePreferenceTreeClick_nonMatchingType_shouldDoNothing()86 public void handlePreferenceTreeClick_nonMatchingType_shouldDoNothing() { 87 final Preference pref = new Preference(mContext); 88 pref.setKey(mController.getPreferenceKey()); 89 90 assertThat(mController.handlePreferenceTreeClick(pref)).isFalse(); 91 } 92 93 @Test handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting()94 public void handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting() { 95 final SwitchPreference pref = new SwitchPreference(mContext); 96 pref.setChecked(true); 97 pref.setKey(mController.getPreferenceKey()); 98 99 assertThat(mController.handlePreferenceTreeClick(pref)).isTrue(); 100 assertThat(Settings.Global.getInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0)) 101 .isEqualTo(1); 102 } 103 104 @Test updateState_preferenceSetCheckedWhenWakeupSettingEnabled()105 public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() { 106 final SwitchPreference preference = mock(SwitchPreference.class); 107 Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); 108 109 mController.updateState(preference); 110 111 verify(preference).setChecked(true); 112 verify(preference).setSummary(R.string.wifi_wakeup_summary); 113 } 114 115 @Test updateState_preferenceSetUncheckedWhenWakeupSettingDisabled()116 public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabled() { 117 final SwitchPreference preference = mock(SwitchPreference.class); 118 Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0); 119 120 mController.updateState(preference); 121 122 verify(preference).setChecked(false); 123 verify(preference).setSummary(R.string.wifi_wakeup_summary); 124 } 125 126 @Test updateState_preferenceSetUncheckedWhenWifiScanningDisabled()127 public void updateState_preferenceSetUncheckedWhenWifiScanningDisabled() { 128 final SwitchPreference preference = mock(SwitchPreference.class); 129 Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); 130 Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0); 131 132 mController.updateState(preference); 133 134 verify(preference).setChecked(false); 135 } 136 137 @Test updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation()138 public void updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation() { 139 final SwitchPreference preference = mock(SwitchPreference.class); 140 Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); 141 doReturn(false).when(mLocationManager).isLocationEnabled(); 142 143 mController.updateState(preference); 144 145 verify(preference).setChecked(false); 146 verify(preference).setSummary(mController.getNoLocationSummary()); 147 } 148 149 @Test updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled()150 public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled() { 151 final SwitchPreference preference = mock(SwitchPreference.class); 152 Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0); 153 doReturn(false).when(mLocationManager).isLocationEnabled(); 154 155 mController.updateState(preference); 156 157 verify(preference).setChecked(false); 158 verify(preference).setSummary(mController.getNoLocationSummary()); 159 } 160 161 @Test updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled()162 public void updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled() { 163 final SwitchPreference preference = mock(SwitchPreference.class); 164 Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); 165 Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0); 166 doReturn(false).when(mLocationManager).isLocationEnabled(); 167 168 mController.updateState(preference); 169 170 verify(preference).setChecked(false); 171 verify(preference).setSummary(mController.getNoLocationSummary()); 172 } 173 } 174