• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.USE_OPEN_WIFI_PACKAGE;
20 import static com.android.settings.wifi.UseOpenWifiPreferenceController.REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY;
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.Activity;
29 import android.app.Fragment;
30 import android.content.ComponentName;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.net.NetworkScoreManager;
34 import android.net.NetworkScorerAppData;
35 import android.provider.Settings;
36 import android.support.v14.preference.SwitchPreference;
37 import android.support.v7.preference.Preference;
38 
39 import com.android.settings.network.NetworkScoreManagerWrapper;
40 import com.android.settings.SettingsRobolectricTestRunner;
41 import com.android.settings.TestConfig;
42 import com.android.settings.core.lifecycle.Lifecycle;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.Captor;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.RuntimeEnvironment;
52 import org.robolectric.annotation.Config;
53 
54 @RunWith(SettingsRobolectricTestRunner.class)
55 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
56 public class UseOpenWifiPreferenceControllerTest {
57     private static ComponentName ENABLE_ACTIVITY_COMPONENT = new ComponentName("package", "activityClass");
58     private static NetworkScorerAppData APP_DATA =
59             new NetworkScorerAppData(0, null, null, ENABLE_ACTIVITY_COMPONENT, null);
60     private static NetworkScorerAppData APP_DATA_NO_ACTIVITY =
61             new NetworkScorerAppData(0, null, null, null, null);
62 
63     @Mock private Lifecycle mLifecycle;
64     @Mock private Fragment mFragment;
65     @Mock private NetworkScoreManagerWrapper mNetworkScoreManagerWrapper;
66     @Captor private ArgumentCaptor<Intent> mIntentCaptor;
67     private Context mContext;
68     private UseOpenWifiPreferenceController mController;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73 
74         mContext = RuntimeEnvironment.application;
75     }
76 
createController()77     private void createController() {
78         mController = new UseOpenWifiPreferenceController(
79                 mContext, mFragment, mNetworkScoreManagerWrapper, mLifecycle);
80     }
81 
82     @Test
testIsAvailable_noScorer()83     public void testIsAvailable_noScorer() {
84         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(null);
85 
86         createController();
87 
88         assertThat(mController.isAvailable()).isFalse();
89     }
90 
91     @Test
testIsAvailable_noEnableActivity()92     public void testIsAvailable_noEnableActivity() {
93         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);
94 
95         createController();
96 
97         assertThat(mController.isAvailable()).isFalse();
98     }
99 
100     @Test
testIsAvailable_enableActivityExists()101     public void testIsAvailable_enableActivityExists() {
102         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
103 
104         createController();
105 
106         assertThat(mController.isAvailable()).isTrue();
107     }
108 
109     @Test
onPreferenceChange_nonMatchingKey_shouldDoNothing()110     public void onPreferenceChange_nonMatchingKey_shouldDoNothing() {
111         createController();
112 
113         final SwitchPreference pref = new SwitchPreference(mContext);
114 
115         assertThat(mController.onPreferenceChange(pref, null)).isFalse();
116     }
117 
118     @Test
onPreferenceChange_notAvailable_shouldDoNothing()119     public void onPreferenceChange_notAvailable_shouldDoNothing() {
120         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA_NO_ACTIVITY);
121 
122         createController();
123 
124         final Preference pref = new Preference(mContext);
125         pref.setKey(mController.getPreferenceKey());
126 
127         assertThat(mController.onPreferenceChange(pref, null)).isFalse();
128     }
129 
130     @Test
onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity()131     public void onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity() {
132         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
133         createController();
134 
135         final SwitchPreference pref = new SwitchPreference(mContext);
136         pref.setKey(mController.getPreferenceKey());
137 
138         assertThat(mController.onPreferenceChange(pref, null)).isFalse();
139         verify(mFragment).startActivityForResult(mIntentCaptor.capture(),
140                 eq(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY));
141         Intent activityIntent = mIntentCaptor.getValue();
142         assertThat(activityIntent.getComponent()).isEqualTo(ENABLE_ACTIVITY_COMPONENT);
143         assertThat(activityIntent.getAction()).isEqualTo(NetworkScoreManager.ACTION_CUSTOM_ENABLE);
144     }
145 
146     @Test
onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting()147     public void onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting() {
148         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
149         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
150                 ENABLE_ACTIVITY_COMPONENT.getPackageName());
151 
152         createController();
153 
154         final SwitchPreference pref = new SwitchPreference(mContext);
155         pref.setKey(mController.getPreferenceKey());
156 
157         assertThat(mController.onPreferenceChange(pref, null)).isTrue();
158         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
159                 .isEqualTo("");
160     }
161 
162     @Test
onActivityResult_nonmatchingRequestCode_shouldDoNothing()163     public void onActivityResult_nonmatchingRequestCode_shouldDoNothing() {
164         createController();
165 
166         assertThat(mController.onActivityResult(234 /* requestCode */ , Activity.RESULT_OK))
167                 .isEqualTo(false);
168         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
169                 .isNull();
170     }
171 
172     @Test
onActivityResult_matchingRequestCode_nonOkResult_shouldDoNothing()173     public void onActivityResult_matchingRequestCode_nonOkResult_shouldDoNothing() {
174         createController();
175 
176         assertThat(mController
177                 .onActivityResult(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY, Activity.RESULT_CANCELED))
178                 .isEqualTo(true);
179         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
180                 .isNull();
181     }
182 
183     @Test
onActivityResult_matchingRequestCode_okResult_updatesSetting()184     public void onActivityResult_matchingRequestCode_okResult_updatesSetting() {
185         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
186         createController();
187 
188         assertThat(mController
189                 .onActivityResult(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY, Activity.RESULT_OK))
190                 .isEqualTo(true);
191         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
192                 .isEqualTo(ENABLE_ACTIVITY_COMPONENT.getPackageName());
193     }
194 
195     @Test
updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreEnabled()196     public void updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreEnabled() {
197         when(mNetworkScoreManagerWrapper.getActiveScorer()).thenReturn(APP_DATA);
198         createController();
199 
200         final SwitchPreference preference = mock(SwitchPreference.class);
201         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
202                 ENABLE_ACTIVITY_COMPONENT.getPackageName());
203 
204         mController.updateState(preference);
205 
206         verify(preference).setVisible(true);
207         verify(preference).setChecked(true);
208     }
209 
210     @Test
updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreDisabled()211     public void updateState_preferenceSetCheckedAndSetVisibleWhenSettingsAreDisabled() {
212         final SwitchPreference preference = mock(SwitchPreference.class);
213         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE, "");
214         createController();
215 
216         mController.updateState(preference);
217 
218         verify(preference).setVisible(false);
219         verify(preference).setChecked(false);
220     }
221 }
222