• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.network;
17 
18 import static androidx.lifecycle.Lifecycle.Event.ON_START;
19 import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
20 
21 import static com.android.settings.network.MobileNetworkPreferenceController.MOBILE_NETWORK_CLASS;
22 import static com.android.settings.network.MobileNetworkPreferenceController.MOBILE_NETWORK_PACKAGE;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import static org.robolectric.shadow.api.Shadow.extract;
32 
33 import android.app.Activity;
34 import android.content.ComponentName;
35 import android.content.Context;
36 import android.content.Intent;
37 import android.net.ConnectivityManager;
38 import android.os.UserManager;
39 import android.provider.Settings;
40 import android.provider.Settings.Global;
41 import android.telephony.PhoneStateListener;
42 import android.telephony.TelephonyManager;
43 import android.util.FeatureFlagUtils;
44 
45 import androidx.lifecycle.LifecycleOwner;
46 import androidx.preference.Preference;
47 import androidx.preference.PreferenceScreen;
48 
49 import com.android.settings.core.FeatureFlags;
50 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
51 import com.android.settings.testutils.shadow.ShadowUserManager;
52 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
53 import com.android.settingslib.RestrictedPreference;
54 import com.android.settingslib.core.lifecycle.Lifecycle;
55 
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.ArgumentCaptor;
60 import org.mockito.Mock;
61 import org.mockito.MockitoAnnotations;
62 import org.robolectric.Robolectric;
63 import org.robolectric.RobolectricTestRunner;
64 import org.robolectric.annotation.Config;
65 
66 @RunWith(RobolectricTestRunner.class)
67 @Config(shadows = {ShadowConnectivityManager.class, ShadowUserManager.class})
68 public class MobileNetworkPreferenceControllerTest {
69 
70     private Context mContext;
71     @Mock
72     private TelephonyManager mTelephonyManager;
73     @Mock
74     private PreferenceScreen mScreen;
75 
76     private Lifecycle mLifecycle;
77     private LifecycleOwner mLifecycleOwner;
78     private MobileNetworkPreferenceController mController;
79     private Preference mPreference;
80 
81     @Before
setUp()82     public void setUp() {
83         MockitoAnnotations.initMocks(this);
84         mContext = spy(Robolectric.setupActivity(Activity.class));
85         mLifecycleOwner = () -> mLifecycle;
86         mLifecycle = new Lifecycle(mLifecycleOwner);
87         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
88         mPreference = new Preference(mContext);
89         mPreference.setKey(MobileNetworkPreferenceController.KEY_MOBILE_NETWORK_SETTINGS);
90     }
91 
92     @Test
secondaryUser_prefIsNotAvailable()93     public void secondaryUser_prefIsNotAvailable() {
94         ShadowUserManager userManager = extract(mContext.getSystemService(UserManager.class));
95         userManager.setIsAdminUser(false);
96         ShadowConnectivityManager connectivityManager =
97                 extract(mContext.getSystemService(ConnectivityManager.class));
98         connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, true);
99 
100         mController = new MobileNetworkPreferenceController(mContext);
101         assertThat(mController.isAvailable()).isFalse();
102     }
103 
104     @Test
wifiOnly_prefIsNotAvailable()105     public void wifiOnly_prefIsNotAvailable() {
106         ShadowUserManager userManager = extract(mContext.getSystemService(UserManager.class));
107         userManager.setIsAdminUser(true);
108         ShadowConnectivityManager connectivityManager =
109                 extract(mContext.getSystemService(ConnectivityManager.class));
110         connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false);
111 
112         mController = new MobileNetworkPreferenceController(mContext);
113         assertThat(mController.isAvailable()).isFalse();
114     }
115 
116     @Test
goThroughLifecycle_isAvailable_shouldListenToServiceChange()117     public void goThroughLifecycle_isAvailable_shouldListenToServiceChange() {
118         mController = spy(new MobileNetworkPreferenceController(mContext));
119         mLifecycle.addObserver(mController);
120         doReturn(true).when(mController).isAvailable();
121 
122         mLifecycle.handleLifecycleEvent(ON_START);
123         verify(mTelephonyManager).listen(mController.mPhoneStateListener,
124                 PhoneStateListener.LISTEN_SERVICE_STATE);
125 
126         mLifecycle.handleLifecycleEvent(ON_STOP);
127         verify(mTelephonyManager).listen(mController.mPhoneStateListener,
128                 PhoneStateListener.LISTEN_NONE);
129     }
130 
131     @Test
serviceStateChange_shouldUpdatePrefSummary()132     public void serviceStateChange_shouldUpdatePrefSummary() {
133         final String testCarrierName = "test";
134         final Preference mPreference = mock(Preference.class);
135         mController = spy(new MobileNetworkPreferenceController(mContext));
136         mLifecycle.addObserver(mController);
137 
138         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
139         doReturn(true).when(mController).isAvailable();
140 
141         // Display pref and go through lifecycle to set up listener.
142         mController.displayPreference(mScreen);
143         mLifecycle.handleLifecycleEvent(ON_START);
144         verify(mController).onStart();
145         verify(mTelephonyManager).listen(mController.mPhoneStateListener,
146                 PhoneStateListener.LISTEN_SERVICE_STATE);
147 
148         // Trigger listener update
149         when(mTelephonyManager.getNetworkOperatorName()).thenReturn(testCarrierName);
150         mController.mPhoneStateListener.onServiceStateChanged(null);
151 
152         // Carrier name should be set.
153         verify(mPreference).setSummary(testCarrierName);
154     }
155 
156     @Test
airplaneModeTurnedOn_shouldDisablePreference()157     public void airplaneModeTurnedOn_shouldDisablePreference() {
158         Settings.Global.putInt(mContext.getContentResolver(),
159                 Global.AIRPLANE_MODE_ON, 1);
160         mController = spy(new MobileNetworkPreferenceController(mContext));
161         final RestrictedPreference mPreference = new RestrictedPreference(mContext);
162         mController.updateState(mPreference);
163         assertThat(mPreference.isEnabled()).isFalse();
164     }
165 
166     @Test
airplaneModeTurnedOffAndNoUserRestriction_shouldEnablePreference()167     public void airplaneModeTurnedOffAndNoUserRestriction_shouldEnablePreference() {
168         Settings.Global.putInt(mContext.getContentResolver(),
169                 Global.AIRPLANE_MODE_ON, 0);
170         mController = spy(new MobileNetworkPreferenceController(mContext));
171         final RestrictedPreference mPreference = new RestrictedPreference(mContext);
172         mPreference.setDisabledByAdmin(null);
173         mController.updateState(mPreference);
174         assertThat(mPreference.isEnabled()).isTrue();
175     }
176 
177     @Test
airplaneModeTurnedOffAndHasUserRestriction_shouldDisablePreference()178     public void airplaneModeTurnedOffAndHasUserRestriction_shouldDisablePreference() {
179         Settings.Global.putInt(mContext.getContentResolver(),
180                 Global.AIRPLANE_MODE_ON, 0);
181         mController = spy(new MobileNetworkPreferenceController(mContext));
182         final RestrictedPreference mPreference = new RestrictedPreference(mContext);
183         mPreference.setDisabledByAdmin(EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN);
184         mController.updateState(mPreference);
185         assertThat(mPreference.isEnabled()).isFalse();
186     }
187 
188     @Test
handlePreferenceTreeClick_mobileFeatureDisabled_sendIntent()189     public void handlePreferenceTreeClick_mobileFeatureDisabled_sendIntent() {
190         mController = new MobileNetworkPreferenceController(mContext);
191         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.MOBILE_NETWORK_V2, false);
192         ArgumentCaptor<Intent> argument = ArgumentCaptor.forClass(Intent.class);
193 
194         mController.handlePreferenceTreeClick(mPreference);
195 
196         verify(mContext).startActivity(argument.capture());
197         final ComponentName componentName = argument.getValue().getComponent();
198         assertThat(componentName.getPackageName()).isEqualTo(MOBILE_NETWORK_PACKAGE);
199         assertThat(componentName.getClassName()).isEqualTo(MOBILE_NETWORK_CLASS);
200     }
201 }
202