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_NETWORKS_AVAILABLE_NOTIFICATION_ON; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.provider.Settings; 26 import android.support.v14.preference.SwitchPreference; 27 import android.support.v7.preference.Preference; 28 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 import com.android.settingslib.core.lifecycle.Lifecycle; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class NotifyOpenNetworkPreferenceControllerTest { 40 41 private Context mContext; 42 private NotifyOpenNetworksPreferenceController mController; 43 44 @Before 45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 mContext = RuntimeEnvironment.application; 48 mController = new NotifyOpenNetworksPreferenceController(mContext, mock(Lifecycle.class)); 49 } 50 51 @Test 52 public void testIsAvailable_shouldAlwaysReturnTrue() { 53 assertThat(mController.isAvailable()).isTrue(); 54 } 55 56 @Test 57 public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() { 58 final SwitchPreference pref = new SwitchPreference(mContext); 59 60 assertThat(mController.handlePreferenceTreeClick(pref)).isFalse(); 61 } 62 63 @Test 64 public void handlePreferenceTreeClick_nonMatchingType_shouldDoNothing() { 65 final Preference pref = new Preference(mContext); 66 pref.setKey(mController.getPreferenceKey()); 67 68 assertThat(mController.handlePreferenceTreeClick(pref)).isFalse(); 69 } 70 71 @Test 72 public void handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting() { 73 final SwitchPreference pref = new SwitchPreference(mContext); 74 pref.setChecked(true); 75 pref.setKey(mController.getPreferenceKey()); 76 77 assertThat(mController.handlePreferenceTreeClick(pref)).isTrue(); 78 assertThat(Settings.Global.getInt(mContext.getContentResolver(), 79 WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0)) 80 .isEqualTo(1); 81 } 82 83 @Test 84 public void updateState_preferenceSetCheckedWhenSettingsAreEnabled() { 85 final SwitchPreference preference = mock(SwitchPreference.class); 86 Settings.System.putInt(mContext.getContentResolver(), 87 WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1); 88 89 mController.updateState(preference); 90 91 verify(preference).setChecked(true); 92 } 93 94 @Test 95 public void updateState_preferenceSetCheckedWhenSettingsAreDisabled() { 96 final SwitchPreference preference = mock(SwitchPreference.class); 97 Settings.System.putInt(mContext.getContentResolver(), 98 WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0); 99 100 mController.updateState(preference); 101 102 verify(preference).setChecked(false); 103 } 104 } 105