1 /*
2  * Copyright (C) 2020 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;
18 
19 import static com.android.settings.AllInOneTetherSettings.BLUETOOTH_TETHER_KEY;
20 import static com.android.settings.AllInOneTetherSettings.ETHERNET_TETHER_KEY;
21 import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_DEFAULT;
22 import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_MAX;
23 import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_WITH_SECURITY_NON;
24 import static com.android.settings.AllInOneTetherSettings.USB_TETHER_KEY;
25 import static com.android.settings.AllInOneTetherSettings.WIFI_TETHER_DISABLE_KEY;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.when;
33 
34 import android.content.Context;
35 import android.net.ConnectivityManager;
36 import android.net.TetheringManager;
37 import android.net.wifi.SoftApConfiguration;
38 import android.os.UserHandle;
39 import android.os.UserManager;
40 import android.util.FeatureFlagUtils;
41 
42 import androidx.preference.PreferenceGroup;
43 import androidx.preference.PreferenceScreen;
44 
45 import com.android.settings.core.FeatureFlags;
46 import com.android.settings.testutils.shadow.ShadowWifiManager;
47 import com.android.settings.wifi.tether.WifiTetherAutoOffPreferenceController;
48 import com.android.settings.wifi.tether.WifiTetherSecurityPreferenceController;
49 import com.android.settingslib.core.lifecycle.Lifecycle;
50 
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.Mock;
55 import org.mockito.MockitoAnnotations;
56 import org.robolectric.RobolectricTestRunner;
57 import org.robolectric.RuntimeEnvironment;
58 import org.robolectric.annotation.Config;
59 import org.robolectric.util.ReflectionHelpers;
60 
61 import java.util.ArrayList;
62 import java.util.List;
63 
64 @RunWith(RobolectricTestRunner.class)
65 @Config(shadows = {ShadowWifiManager.class})
66 public class AllInOneTetherSettingsTest {
67     private static final String[] WIFI_REGEXS = {"wifi_regexs"};
68     private static final String[] USB_REGEXS = {"usb_regexs"};
69     private static final String[] BT_REGEXS = {"bt_regexs"};
70     private static final String[] ETHERNET_REGEXS = {"ethernet_regexs"};
71 
72     private Context mContext;
73     private AllInOneTetherSettings mAllInOneTetherSettings;
74 
75     @Mock
76     private ConnectivityManager mConnectivityManager;
77     @Mock
78     private TetheringManager mTetheringManager;
79     @Mock
80     private UserManager mUserManager;
81     @Mock
82     private WifiTetherSecurityPreferenceController mSecurityPreferenceController;
83     @Mock
84     private PreferenceScreen mPreferenceScreen;
85     @Mock
86     private PreferenceGroup mWifiTetherGroup;
87 
88     @Before
setUp()89     public void setUp() {
90         mContext = spy(RuntimeEnvironment.application);
91 
92         MockitoAnnotations.initMocks(this);
93         doReturn(mConnectivityManager)
94                 .when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
95         doReturn(mTetheringManager)
96                 .when(mContext).getSystemService(Context.TETHERING_SERVICE);
97         doReturn(WIFI_REGEXS).when(mTetheringManager).getTetherableWifiRegexs();
98         doReturn(USB_REGEXS).when(mTetheringManager).getTetherableUsbRegexs();
99         doReturn(BT_REGEXS).when(mTetheringManager).getTetherableBluetoothRegexs();
100         doReturn(ETHERNET_REGEXS).when(mTetheringManager).getTetherableIfaces();
101         doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
102         // Assume the feature is enabled for most test cases.
103         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true);
104         mAllInOneTetherSettings = spy(new AllInOneTetherSettings());
105         doReturn(mPreferenceScreen).when(mAllInOneTetherSettings).getPreferenceScreen();
106         ReflectionHelpers.setField(mAllInOneTetherSettings, "mLifecycle", mock(Lifecycle.class));
107         ReflectionHelpers.setField(mAllInOneTetherSettings, "mSecurityPreferenceController",
108                 mSecurityPreferenceController);
109         ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTetherGroup", mWifiTetherGroup);
110     }
111 
112     @Test
getNonIndexableKeys_tetherAvailable_featureEnabled_keysReturnedCorrectly()113     public void getNonIndexableKeys_tetherAvailable_featureEnabled_keysReturnedCorrectly() {
114         // To let TetherUtil.isTetherAvailable return true, select one of the combinations
115         setupIsTetherAvailable(true);
116 
117         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true);
118         final List<String> niks =
119                 AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
120 
121         assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
122         assertThat(niks).doesNotContain(
123                 AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
124         assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
125         assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
126         assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
127         assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY);
128         assertThat(niks).doesNotContain(USB_TETHER_KEY);
129         assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY);
130 
131         // This key should be returned because it's not visible by default.
132         assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
133     }
134 
135     @Test
getNonIndexableKeys_tetherAvailable_featureDisabled_keysReturned()136     public void getNonIndexableKeys_tetherAvailable_featureDisabled_keysReturned() {
137         setupIsTetherAvailable(true);
138         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, false);
139 
140         final List<String> niks =
141                 AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
142 
143         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
144         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
145         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
146         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
147         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
148         assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
149         assertThat(niks).contains(BLUETOOTH_TETHER_KEY);
150         assertThat(niks).contains(USB_TETHER_KEY);
151         assertThat(niks).contains(ETHERNET_TETHER_KEY);
152     }
153 
154     @Test
getNonIndexableKeys_tetherNotAvailable_keysReturned()155     public void getNonIndexableKeys_tetherNotAvailable_keysReturned() {
156         // To let TetherUtil.isTetherAvailable return false, select one of the combinations
157         setupIsTetherAvailable(false);
158 
159         final List<String> niks =
160                 AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
161 
162         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
163         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
164         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
165         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
166         assertThat(niks).contains(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
167         assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
168         assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY);
169         assertThat(niks).doesNotContain(USB_TETHER_KEY);
170         assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY);
171     }
172 
173     @Test
getPreferenceControllers_notEmpty()174     public void getPreferenceControllers_notEmpty() {
175         assertThat(AllInOneTetherSettings.SEARCH_INDEX_DATA_PROVIDER
176                 .getPreferenceControllers(mContext)).isNotEmpty();
177     }
178 
179     @Test
createPreferenceControllers_hasAutoOffPreference()180     public void createPreferenceControllers_hasAutoOffPreference() {
181         assertThat(mAllInOneTetherSettings.createPreferenceControllers(mContext)
182                 .stream()
183                 .filter(controller -> controller instanceof WifiTetherAutoOffPreferenceController)
184                 .count())
185                 .isEqualTo(1);
186     }
187 
188     @Test
getInitialChildCount_withSecurity()189     public void getInitialChildCount_withSecurity() {
190         when(mSecurityPreferenceController.getSecurityType())
191                 .thenReturn(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
192         assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo(
193                 EXPANDED_CHILD_COUNT_DEFAULT);
194     }
195 
196     @Test
getInitialChildCount_withoutSecurity()197     public void getInitialChildCount_withoutSecurity() {
198         when(mSecurityPreferenceController.getSecurityType())
199                 .thenReturn(SoftApConfiguration.SECURITY_TYPE_OPEN);
200         assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo(
201                 EXPANDED_CHILD_COUNT_WITH_SECURITY_NON);
202     }
203 
204     @Test
getInitialExpandedChildCount_expandAllChild()205     public void getInitialExpandedChildCount_expandAllChild() {
206         assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
207                 .isNotEqualTo(EXPANDED_CHILD_COUNT_MAX);
208         ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", false);
209         assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
210                 .isEqualTo(EXPANDED_CHILD_COUNT_MAX);
211         ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", true);
212         assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
213                 .isEqualTo(EXPANDED_CHILD_COUNT_MAX);
214     }
215 
setupIsTetherAvailable(boolean returnValue)216     private void setupIsTetherAvailable(boolean returnValue) {
217         when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
218 
219         // For RestrictedLockUtils.checkIfRestrictionEnforced
220         final int userId = UserHandle.myUserId();
221         List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
222         when(mUserManager.getUserRestrictionSources(
223                 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
224                 .thenReturn(enforcingUsers);
225 
226         // For RestrictedLockUtils.hasBaseUserRestriction
227         when(mUserManager.hasBaseUserRestriction(
228                 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
229                 .thenReturn(!returnValue);
230     }
231 }
232