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