• 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.content.Context.NETWORK_SCORE_SERVICE;
20 import static android.provider.Settings.Global.USE_OPEN_WIFI_PACKAGE;
21 
22 import static com.android.settings.wifi.UseOpenWifiPreferenceController
23         .REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 
32 import android.annotation.NonNull;
33 import android.app.Activity;
34 import android.content.ComponentName;
35 import android.content.Context;
36 import android.content.Intent;
37 import android.net.NetworkScoreManager;
38 import android.net.NetworkScorerAppData;
39 import android.provider.Settings;
40 
41 import androidx.fragment.app.Fragment;
42 import androidx.preference.Preference;
43 import androidx.preference.SwitchPreference;
44 
45 import com.android.settings.R;
46 import com.android.settingslib.core.lifecycle.Lifecycle;
47 
48 import com.google.common.collect.Lists;
49 
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.ArgumentCaptor;
55 import org.mockito.Captor;
56 import org.mockito.Mock;
57 import org.mockito.MockitoAnnotations;
58 import org.robolectric.RobolectricTestRunner;
59 import org.robolectric.RuntimeEnvironment;
60 import org.robolectric.shadows.ShadowApplication;
61 
62 import java.util.ArrayList;
63 import java.util.List;
64 
65 @RunWith(RobolectricTestRunner.class)
66 public class UseOpenWifiPreferenceControllerTest {
67 
68     private static ComponentName sEnableActivityComponent;
69     private static NetworkScorerAppData sAppData;
70     private static NetworkScorerAppData sAppDataNoActivity;
71 
72     @BeforeClass
beforeClass()73     public static void beforeClass() {
74         sEnableActivityComponent = new ComponentName("package", "activityClass");
75         sAppData = new NetworkScorerAppData(0, null, null, sEnableActivityComponent, null);
76         sAppDataNoActivity = new NetworkScorerAppData(0, null, null, null, null);
77     }
78 
79     @Mock
80     private Lifecycle mLifecycle;
81     @Mock
82     private Fragment mFragment;
83     @Mock
84     private NetworkScoreManager mNetworkScoreManager;
85     @Captor
86     private ArgumentCaptor<Intent> mIntentCaptor;
87     private Context mContext;
88     private UseOpenWifiPreferenceController mController;
89 
90     @Before
setUp()91     public void setUp() {
92         MockitoAnnotations.initMocks(this);
93 
94         mContext = RuntimeEnvironment.application;
95         ShadowApplication.getInstance()
96                 .setSystemService(NETWORK_SCORE_SERVICE, mNetworkScoreManager);
97     }
98 
createController()99     private void createController() {
100         mController = new UseOpenWifiPreferenceController(mContext, mFragment, mLifecycle);
101     }
102 
103     /**
104      * Sets the scorers.
105      *
106      * @param scorers list of scorers returned by {@link NetworkScoreManager#getAllValidScorers()}.
107      *                First scorer in the list is the active scorer.
108      */
setupScorers(@onNull List<NetworkScorerAppData> scorers)109     private void setupScorers(@NonNull List<NetworkScorerAppData> scorers) {
110         when(mNetworkScoreManager.getActiveScorerPackage())
111                 .thenReturn(sEnableActivityComponent.getPackageName());
112         when(mNetworkScoreManager.getAllValidScorers()).thenReturn(scorers);
113         when(mNetworkScoreManager.getActiveScorer()).thenReturn(scorers.get(0));
114     }
115 
116     @Test
testIsAvailable_returnsFalseWhenNoScorerSet()117     public void testIsAvailable_returnsFalseWhenNoScorerSet() {
118         createController();
119 
120         assertThat(mController.isAvailable()).isFalse();
121     }
122 
123     @Test
testIsAvailable_returnsFalseWhenScorersNotSupported()124     public void testIsAvailable_returnsFalseWhenScorersNotSupported() {
125         setupScorers(Lists.newArrayList(sAppDataNoActivity));
126         createController();
127 
128         assertThat(mController.isAvailable()).isFalse();
129     }
130 
131     @Test
testIsAvailable_returnsTrueIfActiveScorerSupported()132     public void testIsAvailable_returnsTrueIfActiveScorerSupported() {
133         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
134         createController();
135 
136         assertThat(mController.isAvailable()).isTrue();
137     }
138 
139     @Test
testIsAvailable_returnsTrueIfNonActiveScorerSupported()140     public void testIsAvailable_returnsTrueIfNonActiveScorerSupported() {
141         setupScorers(Lists.newArrayList(sAppDataNoActivity, sAppData));
142         when(mNetworkScoreManager.getActiveScorer()).thenReturn(sAppDataNoActivity);
143         createController();
144 
145         assertThat(mController.isAvailable()).isTrue();
146     }
147 
148     @Test
onPreferenceChange_nonMatchingKey_shouldDoNothing()149     public void onPreferenceChange_nonMatchingKey_shouldDoNothing() {
150         createController();
151 
152         final SwitchPreference pref = new SwitchPreference(mContext);
153 
154         assertThat(mController.onPreferenceChange(pref, null)).isFalse();
155     }
156 
157     @Test
onPreferenceChange_notAvailable_shouldDoNothing()158     public void onPreferenceChange_notAvailable_shouldDoNothing() {
159         createController();
160 
161         final Preference pref = new Preference(mContext);
162         pref.setKey(mController.getPreferenceKey());
163 
164         assertThat(mController.onPreferenceChange(pref, null)).isFalse();
165     }
166 
167     @Test
onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity()168     public void onPreferenceChange_matchingKeyAndAvailable_enableShouldStartEnableActivity() {
169         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
170         createController();
171 
172         final SwitchPreference pref = new SwitchPreference(mContext);
173         pref.setKey(mController.getPreferenceKey());
174 
175         assertThat(mController.onPreferenceChange(pref, null)).isFalse();
176         verify(mFragment).startActivityForResult(mIntentCaptor.capture(),
177                 eq(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY));
178         Intent activityIntent = mIntentCaptor.getValue();
179         assertThat(activityIntent.getComponent()).isEqualTo(sEnableActivityComponent);
180         assertThat(activityIntent.getAction()).isEqualTo(NetworkScoreManager.ACTION_CUSTOM_ENABLE);
181     }
182 
183     @Test
onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting()184     public void onPreferenceChange_matchingKeyAndAvailable_disableShouldUpdateSetting() {
185         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
186         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
187                 sEnableActivityComponent.getPackageName());
188 
189         createController();
190 
191         final SwitchPreference pref = new SwitchPreference(mContext);
192         pref.setKey(mController.getPreferenceKey());
193 
194         assertThat(mController.onPreferenceChange(pref, null)).isTrue();
195         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
196                 .isEqualTo("");
197     }
198 
199     @Test
onActivityResult_nonmatchingRequestCode_shouldDoNothing()200     public void onActivityResult_nonmatchingRequestCode_shouldDoNothing() {
201         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
202         createController();
203 
204         assertThat(mController.onActivityResult(234 /* requestCode */, Activity.RESULT_OK))
205                 .isEqualTo(false);
206         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
207                 .isNull();
208     }
209 
210     @Test
onActivityResult_matchingRequestCode_nonOkResult_shouldDoNothing()211     public void onActivityResult_matchingRequestCode_nonOkResult_shouldDoNothing() {
212         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
213         createController();
214 
215         assertThat(mController
216                 .onActivityResult(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY, Activity.RESULT_CANCELED))
217                 .isEqualTo(true);
218         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
219                 .isNull();
220     }
221 
222     @Test
onActivityResult_matchingRequestCode_okResult_updatesSetting()223     public void onActivityResult_matchingRequestCode_okResult_updatesSetting() {
224         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
225         createController();
226 
227         assertThat(mController
228                 .onActivityResult(REQUEST_CODE_OPEN_WIFI_AUTOMATICALLY, Activity.RESULT_OK))
229                 .isEqualTo(true);
230         assertThat(Settings.Global.getString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE))
231                 .isEqualTo(sEnableActivityComponent.getPackageName());
232     }
233 
234     @Test
updateState_noEnableActivity_preferenceDisabled_summaryChanged()235     public void updateState_noEnableActivity_preferenceDisabled_summaryChanged() {
236         setupScorers(Lists.newArrayList(sAppDataNoActivity));
237         createController();
238 
239         final SwitchPreference preference = mock(SwitchPreference.class);
240         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
241                 sEnableActivityComponent.getPackageName());
242 
243         mController.updateState(preference);
244 
245         verify(preference).setChecked(false);
246         verify(preference).setSummary(
247                 R.string.use_open_wifi_automatically_summary_scorer_unsupported_disabled);
248     }
249 
250     @Test
updateState_noScorer_preferenceDisabled_summaryChanged()251     public void updateState_noScorer_preferenceDisabled_summaryChanged() {
252         when(mNetworkScoreManager.getAllValidScorers()).thenReturn(new ArrayList<>());
253         createController();
254 
255         final SwitchPreference preference = mock(SwitchPreference.class);
256         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
257                 sEnableActivityComponent.getPackageName());
258 
259         mController.updateState(preference);
260 
261         verify(preference).setChecked(false);
262         verify(preference).setSummary(
263                 R.string.use_open_wifi_automatically_summary_scoring_disabled);
264     }
265 
266     @Test
updateState_enableActivityExists_preferenceEnabled()267     public void updateState_enableActivityExists_preferenceEnabled() {
268         setupScorers(Lists.newArrayList(sAppData, sAppDataNoActivity));
269         createController();
270 
271         final SwitchPreference preference = mock(SwitchPreference.class);
272         Settings.Global.putString(mContext.getContentResolver(), USE_OPEN_WIFI_PACKAGE,
273                 sEnableActivityComponent.getPackageName());
274 
275         mController.updateState(preference);
276 
277         verify(preference).setChecked(true);
278         verify(preference).setSummary(R.string.use_open_wifi_automatically_summary);
279     }
280 }
281