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 com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.BroadcastReceiver; 27 import android.content.Context; 28 import android.content.IntentFilter; 29 import android.net.ConnectivityManager; 30 import android.net.NetworkRequest; 31 import android.net.NetworkScoreManager; 32 import android.net.wifi.WifiManager; 33 import android.os.Handler; 34 35 import androidx.preference.Preference.OnPreferenceChangeListener; 36 import androidx.preference.PreferenceScreen; 37 38 import com.android.settings.testutils.FakeFeatureFactory; 39 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal; 40 import com.android.settings.widget.MasterSwitchPreference; 41 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.annotation.Config; 51 52 @RunWith(RobolectricTestRunner.class) 53 @Config(shadows = ShadowRestrictedLockUtilsInternal.class) 54 public class WifiMasterSwitchPreferenceControllerTest { 55 56 @Mock 57 private WifiManager mWifiManager; 58 @Mock 59 private PreferenceScreen mScreen; 60 @Mock 61 private MasterSwitchPreference mPreference; 62 @Mock 63 private ConnectivityManager mConnectivityManager; 64 @Mock 65 private NetworkScoreManager mNetworkScoreManager; 66 67 private Context mContext; 68 private WifiMasterSwitchPreferenceController mController; 69 private MetricsFeatureProvider mMetricsFeatureProvider; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mMetricsFeatureProvider = FakeFeatureFactory.setupForTest().getMetricsFeatureProvider(); 75 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 76 when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(mConnectivityManager); 77 when(mContext.getSystemService(NetworkScoreManager.class)).thenReturn(mNetworkScoreManager); 78 mController = new WifiMasterSwitchPreferenceController(mContext, mMetricsFeatureProvider); 79 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 80 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 81 when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED); 82 } 83 84 @Test testWifiMasterSwitch_byDefault_shouldBeShown()85 public void testWifiMasterSwitch_byDefault_shouldBeShown() { 86 assertThat(mController.isAvailable()).isTrue(); 87 } 88 89 @Test 90 @Config(qualifiers = "mcc999") testWifiMasterSwitch_ifDisabled_shouldNotBeShown()91 public void testWifiMasterSwitch_ifDisabled_shouldNotBeShown() { 92 assertThat(mController.isAvailable()).isFalse(); 93 } 94 95 @Test onResume_shouldRegisterCallback()96 public void onResume_shouldRegisterCallback() { 97 mController.onResume(); 98 99 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); 100 verify(mConnectivityManager).registerNetworkCallback( 101 any(NetworkRequest.class), 102 any(ConnectivityManager.NetworkCallback.class), 103 any(Handler.class)); 104 } 105 106 @Test onPause_shouldUnregisterCallback()107 public void onPause_shouldUnregisterCallback() { 108 mController.onResume(); 109 mController.onPause(); 110 111 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 112 verify(mConnectivityManager).unregisterNetworkCallback( 113 any(ConnectivityManager.NetworkCallback.class)); 114 } 115 116 @Test onStart_shouldRegisterPreferenceChangeListener()117 public void onStart_shouldRegisterPreferenceChangeListener() { 118 mController.displayPreference(mScreen); 119 mController.onStart(); 120 121 verify(mPreference).setOnPreferenceChangeListener(any(OnPreferenceChangeListener.class)); 122 } 123 124 @Test onStop_shouldRegisterPreferenceChangeListener()125 public void onStop_shouldRegisterPreferenceChangeListener() { 126 mController.displayPreference(mScreen); 127 mController.onStart(); 128 129 mController.onStop(); 130 131 verify(mPreference).setOnPreferenceChangeListener(null); 132 } 133 134 @Test onSummaryChanged_shouldUpdatePreferenceSummary()135 public void onSummaryChanged_shouldUpdatePreferenceSummary() { 136 mController.displayPreference(mScreen); 137 138 mController.onSummaryChanged("test summary"); 139 140 verify(mPreference).setSummary("test summary"); 141 } 142 } 143