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.NETWORK_RECOMMENDATIONS_ENABLED;
20 import static android.provider.Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE;
21 import static android.provider.Settings.Global.WIFI_WAKEUP_AVAILABLE;
22 import static android.provider.Settings.Global.WIFI_WAKEUP_ENABLED;
23 import static com.google.common.truth.Truth.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 import android.provider.Settings;
29 import android.support.v14.preference.SwitchPreference;
30 import android.support.v7.preference.Preference;
31 
32 import com.android.settings.R;
33 import com.android.settings.SettingsRobolectricTestRunner;
34 import com.android.settings.TestConfig;
35 import com.android.settings.core.lifecycle.Lifecycle;
36 import com.android.settings.testutils.shadow.SettingsShadowResources;
37 
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 
46 @RunWith(SettingsRobolectricTestRunner.class)
47 @Config(
48         manifest = TestConfig.MANIFEST_PATH,
49         sdk = TestConfig.SDK_VERSION,
50         shadows = { SettingsShadowResources.class })
51 public class WifiWakeupPreferenceControllerTest {
52 
53     private Context mContext;
54     private WifiWakeupPreferenceController mController;
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59         mContext = RuntimeEnvironment.application;
60         mController = new WifiWakeupPreferenceController(mContext, mock(Lifecycle.class));
61         Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
62         SettingsShadowResources.overrideResource(
63                 com.android.internal.R.integer.config_wifi_wakeup_available, 0);
64     }
65 
66     @After
tearDown()67     public void tearDown() {
68         SettingsShadowResources.reset();
69     }
70 
71     @Test
testIsAvailable_returnsFalseWhenSettingIsNotAvailable()72     public void testIsAvailable_returnsFalseWhenSettingIsNotAvailable() {
73         Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_AVAILABLE, 0);
74         assertThat(mController.isAvailable()).isFalse();
75     }
76 
77     @Test
testIsAvailable_returnsTrueWhenSettingIsAvailable()78     public void testIsAvailable_returnsTrueWhenSettingIsAvailable() {
79         Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_AVAILABLE, 1);
80         assertThat(mController.isAvailable()).isTrue();
81     }
82 
83     @Test
handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing()84     public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() {
85         final SwitchPreference pref = new SwitchPreference(mContext);
86 
87         assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
88     }
89 
90     @Test
handlePreferenceTreeClick_nonMatchingType_shouldDoNothing()91     public void handlePreferenceTreeClick_nonMatchingType_shouldDoNothing() {
92         final Preference pref = new Preference(mContext);
93         pref.setKey(mController.getPreferenceKey());
94 
95         assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
96     }
97 
98     @Test
handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting()99     public void handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting() {
100         final SwitchPreference pref = new SwitchPreference(mContext);
101         pref.setChecked(true);
102         pref.setKey(mController.getPreferenceKey());
103 
104         assertThat(mController.handlePreferenceTreeClick(pref)).isTrue();
105         assertThat(Settings.Global.getInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0))
106                 .isEqualTo(1);
107     }
108 
109     @Test
updateState_preferenceSetCheckedAndSetEnabledWhenSettingsAreEnabled()110     public void updateState_preferenceSetCheckedAndSetEnabledWhenSettingsAreEnabled() {
111         final SwitchPreference preference = mock(SwitchPreference.class);
112         Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
113         Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
114 
115         mController.updateState(preference);
116 
117         verify(preference).setChecked(true);
118         verify(preference).setEnabled(true);
119         verify(preference).setSummary(R.string.wifi_wakeup_summary);
120     }
121 
122     @Test
updateState_preferenceSetCheckedAndSetEnabledWhenSettingsAreDisabled()123     public void updateState_preferenceSetCheckedAndSetEnabledWhenSettingsAreDisabled() {
124         final SwitchPreference preference = mock(SwitchPreference.class);
125         Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 0);
126         Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
127 
128         mController.updateState(preference);
129 
130         verify(preference).setChecked(false);
131         verify(preference).setEnabled(false);
132         verify(preference).setSummary(R.string.wifi_wakeup_summary);
133     }
134 
135     @Test
updateState_preferenceSetUncheckedAndSetDisabledWhenWifiScanningDisabled()136     public void updateState_preferenceSetUncheckedAndSetDisabledWhenWifiScanningDisabled() {
137         final SwitchPreference preference = mock(SwitchPreference.class);
138         Settings.System.putInt(mContext.getContentResolver(), NETWORK_RECOMMENDATIONS_ENABLED, 1);
139         Settings.System.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
140         Settings.System.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
141 
142         mController.updateState(preference);
143 
144         verify(preference).setChecked(true);
145         verify(preference).setEnabled(false);
146         verify(preference).setSummary(R.string.wifi_wakeup_summary_scanning_disabled);
147     }
148 }
149