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 android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.IntentFilter; 22 import android.net.wifi.WifiManager; 23 import android.support.v7.preference.Preference.OnPreferenceChangeListener; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 29 import com.android.settings.testutils.FakeFeatureFactory; 30 import com.android.settings.widget.MasterSwitchPreference; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Answers; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RuntimeEnvironment; 39 import org.robolectric.annotation.Config; 40 41 import static com.google.common.truth.Truth.assertThat; 42 import static org.mockito.Matchers.any; 43 import static org.mockito.Mockito.spy; 44 import static org.mockito.Mockito.verify; 45 import static org.mockito.Mockito.when; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 49 public class WifiMasterSwitchPreferenceControllerTest { 50 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 Context mMockContext; 53 @Mock 54 private WifiManager mWifiManager; 55 @Mock 56 private PreferenceScreen mScreen; 57 @Mock 58 private MasterSwitchPreference mPreference; 59 60 private Context mContext; 61 private WifiMasterSwitchPreferenceController mController; 62 private FakeFeatureFactory mFakeFeatureFactory; 63 private MetricsFeatureProvider mMetricsFeatureProvider; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 FakeFeatureFactory.setupForTest(mMockContext); 69 mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext); 70 mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider(); 71 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 72 mController = new WifiMasterSwitchPreferenceController(mContext, mMetricsFeatureProvider); 73 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 74 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 75 when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED); 76 } 77 78 @Test isAvailable_shouldAlwaysReturnTrue()79 public void isAvailable_shouldAlwaysReturnTrue() { 80 assertThat(mController.isAvailable()).isTrue(); 81 } 82 83 @Test onResume_shouldRegisterCallback()84 public void onResume_shouldRegisterCallback() { 85 mController.onResume(); 86 87 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); 88 } 89 90 @Test onPause_shouldUnregisterCallback()91 public void onPause_shouldUnregisterCallback() { 92 mController.onResume(); 93 mController.onPause(); 94 95 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class)); 96 } 97 98 @Test onStart_shouldRegisterPreferenceChangeListener()99 public void onStart_shouldRegisterPreferenceChangeListener() { 100 mController.displayPreference(mScreen); 101 mController.onStart(); 102 103 verify(mPreference).setOnPreferenceChangeListener(any(OnPreferenceChangeListener.class)); 104 } 105 106 @Test onStop_shouldRegisterPreferenceChangeListener()107 public void onStop_shouldRegisterPreferenceChangeListener() { 108 mController.displayPreference(mScreen); 109 mController.onStart(); 110 111 mController.onStop(); 112 113 verify(mPreference).setOnPreferenceChangeListener(null); 114 } 115 116 @Test onSummaryChanged_shouldUpdatePreferenceSummary()117 public void onSummaryChanged_shouldUpdatePreferenceSummary() { 118 mController.displayPreference(mScreen); 119 120 mController.onSummaryChanged("test summary"); 121 122 verify(mPreference).setSummary("test summary"); 123 } 124 } 125