1 package com.android.systemui.qs.tiles.dialog;
2 
3 import static android.net.wifi.sharedconnectivity.app.NetworkProviderInfo.DEVICE_TYPE_PHONE;
4 import static android.provider.Settings.Global.AIRPLANE_MODE_ON;
5 import static android.telephony.SignalStrength.NUM_SIGNAL_STRENGTH_BINS;
6 import static android.telephony.SignalStrength.SIGNAL_STRENGTH_GREAT;
7 import static android.telephony.SignalStrength.SIGNAL_STRENGTH_POOR;
8 import static android.telephony.SubscriptionManager.PROFILE_CLASS_PROVISIONING;
9 
10 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
11 import static com.android.settingslib.wifi.WifiUtils.getHotspotIconResource;
12 import static com.android.systemui.qs.tiles.dialog.InternetDialogController.TOAST_PARAMS_HORIZONTAL_WEIGHT;
13 import static com.android.systemui.qs.tiles.dialog.InternetDialogController.TOAST_PARAMS_VERTICAL_WEIGHT;
14 import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MAX;
15 import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MIN;
16 import static com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyBoolean;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.reset;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 
35 import android.animation.Animator;
36 import android.content.Intent;
37 import android.content.res.Configuration;
38 import android.content.res.Resources;
39 import android.graphics.PixelFormat;
40 import android.graphics.drawable.Drawable;
41 import android.net.ConnectivityManager;
42 import android.net.Network;
43 import android.net.NetworkCapabilities;
44 import android.net.wifi.WifiConfiguration;
45 import android.net.wifi.WifiManager;
46 import android.os.Handler;
47 import android.telephony.ServiceState;
48 import android.telephony.SignalStrength;
49 import android.telephony.SubscriptionInfo;
50 import android.telephony.SubscriptionManager;
51 import android.telephony.TelephonyCallback;
52 import android.telephony.TelephonyDisplayInfo;
53 import android.telephony.TelephonyManager;
54 import android.testing.TestableLooper;
55 import android.testing.TestableResources;
56 import android.text.TextUtils;
57 import android.view.Gravity;
58 import android.view.View;
59 import android.view.WindowManager;
60 
61 import androidx.test.ext.junit.runners.AndroidJUnit4;
62 import androidx.test.filters.SmallTest;
63 
64 import com.android.internal.logging.UiEventLogger;
65 import com.android.keyguard.KeyguardUpdateMonitor;
66 import com.android.settingslib.wifi.WifiUtils;
67 import com.android.settingslib.wifi.dpp.WifiDppIntentHelper;
68 import com.android.systemui.SysuiTestCase;
69 import com.android.systemui.animation.DialogTransitionAnimator;
70 import com.android.systemui.broadcast.BroadcastDispatcher;
71 import com.android.systemui.flags.FakeFeatureFlags;
72 import com.android.systemui.flags.Flags;
73 import com.android.systemui.plugins.ActivityStarter;
74 import com.android.systemui.res.R;
75 import com.android.systemui.statusbar.connectivity.AccessPointController;
76 import com.android.systemui.statusbar.policy.KeyguardStateController;
77 import com.android.systemui.statusbar.policy.LocationController;
78 import com.android.systemui.toast.SystemUIToast;
79 import com.android.systemui.toast.ToastFactory;
80 import com.android.systemui.util.CarrierConfigTracker;
81 import com.android.systemui.util.concurrency.FakeExecutor;
82 import com.android.systemui.util.settings.GlobalSettings;
83 import com.android.systemui.util.time.FakeSystemClock;
84 import com.android.wifitrackerlib.HotspotNetworkEntry;
85 import com.android.wifitrackerlib.MergedCarrierEntry;
86 import com.android.wifitrackerlib.WifiEntry;
87 
88 import org.junit.After;
89 import org.junit.Before;
90 import org.junit.Test;
91 import org.junit.runner.RunWith;
92 import org.mockito.ArgumentCaptor;
93 import org.mockito.Mock;
94 import org.mockito.MockitoAnnotations;
95 import org.mockito.MockitoSession;
96 import org.mockito.quality.Strictness;
97 
98 import java.util.ArrayList;
99 import java.util.List;
100 import java.util.Locale;
101 import java.util.Map;
102 
103 @SmallTest
104 @RunWith(AndroidJUnit4.class)
105 @TestableLooper.RunWithLooper(setAsMainLooper = true)
106 public class InternetDialogDelegateControllerTest extends SysuiTestCase {
107 
108     private static final int SUB_ID = 1;
109     private static final int SUB_ID2 = 2;
110 
111     private MockitoSession mStaticMockSession;
112 
113     //SystemUIToast
114     private static final int GRAVITY_FLAGS = Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL;
115     private static final int TOAST_MESSAGE_STRING_ID = 1;
116     private static final String TOAST_MESSAGE_STRING = "toast message";
117 
118     @Mock
119     private WifiManager mWifiManager;
120     @Mock
121     private ConnectivityManager mConnectivityManager;
122     @Mock
123     private Network mNetwork;
124     @Mock
125     private NetworkCapabilities mNetworkCapabilities;
126     @Mock
127     private TelephonyManager mTelephonyManager;
128     @Mock
129     private SubscriptionManager mSubscriptionManager;
130     @Mock
131     private Handler mHandler;
132     @Mock
133     private Handler mWorkerHandler;
134     @Mock
135     private ActivityStarter mActivityStarter;
136     @Mock
137     private GlobalSettings mGlobalSettings;
138     @Mock
139     private KeyguardStateController mKeyguardStateController;
140     @Mock
141     private AccessPointController mAccessPointController;
142     @Mock
143     private WifiEntry mConnectedEntry;
144     @Mock
145     private WifiEntry mWifiEntry1;
146     @Mock
147     private WifiEntry mWifiEntry2;
148     @Mock
149     private WifiEntry mWifiEntry3;
150     @Mock
151     private WifiEntry mWifiEntry4;
152     @Mock
153     private MergedCarrierEntry mMergedCarrierEntry;
154     @Mock
155     private ServiceState mServiceState;
156     @Mock
157     private BroadcastDispatcher mBroadcastDispatcher;
158     @Mock
159     private WifiUtils.InternetIconInjector mWifiIconInjector;
160     @Mock
161     InternetDialogController.InternetDialogCallback mInternetDialogCallback;
162     @Mock
163     private WindowManager mWindowManager;
164     @Mock
165     private ToastFactory mToastFactory;
166     @Mock
167     private SystemUIToast mSystemUIToast;
168     @Mock
169     private View mToastView;
170     @Mock
171     private Animator mAnimator;
172     @Mock
173     private CarrierConfigTracker mCarrierConfigTracker;
174     @Mock
175     private LocationController mLocationController;
176     @Mock
177     private DialogTransitionAnimator mDialogTransitionAnimator;
178     @Mock
179     private View mDialogLaunchView;
180     @Mock
181     private WifiStateWorker mWifiStateWorker;
182     @Mock
183     private SignalStrength mSignalStrength;
184     @Mock
185     private WifiConfiguration mWifiConfiguration;
186 
187     private FakeFeatureFlags mFlags = new FakeFeatureFlags();
188 
189     private TestableResources mTestableResources;
190     private InternetDialogController mInternetDialogController;
191     private FakeExecutor mExecutor = new FakeExecutor(new FakeSystemClock());
192     private List<WifiEntry> mAccessPoints = new ArrayList<>();
193     private List<WifiEntry> mWifiEntries = new ArrayList<>();
194 
195     private Configuration mConfig;
196 
197     @Before
setUp()198     public void setUp() {
199         mStaticMockSession = mockitoSession()
200                 .mockStatic(SubscriptionManager.class)
201                 .mockStatic(WifiDppIntentHelper.class)
202                 .strictness(Strictness.LENIENT)
203                 .startMocking();
204         MockitoAnnotations.initMocks(this);
205         mTestableResources = mContext.getOrCreateTestableResources();
206         doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
207         when(mTelephonyManager.getSignalStrength()).thenReturn(mSignalStrength);
208         when(mSignalStrength.getLevel()).thenReturn(SIGNAL_STRENGTH_GREAT);
209         when(mKeyguardStateController.isUnlocked()).thenReturn(true);
210         when(mConnectedEntry.isDefaultNetwork()).thenReturn(true);
211         when(mConnectedEntry.hasInternetAccess()).thenReturn(true);
212         when(mWifiEntry1.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_DISCONNECTED);
213         when(mWifiEntry2.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_DISCONNECTED);
214         when(mWifiEntry3.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_DISCONNECTED);
215         when(mWifiEntry4.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_DISCONNECTED);
216         mAccessPoints.add(mConnectedEntry);
217         mAccessPoints.add(mWifiEntry1);
218         when(mAccessPointController.getMergedCarrierEntry()).thenReturn(mMergedCarrierEntry);
219         when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{SUB_ID});
220         when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(SUB_ID);
221         SubscriptionInfo info = mock(SubscriptionInfo.class);
222         when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(info);
223         when(mToastFactory.createToast(any(), anyString(), anyString(), anyInt(), anyInt()))
224             .thenReturn(mSystemUIToast);
225         when(mSystemUIToast.getView()).thenReturn(mToastView);
226         when(mSystemUIToast.getGravity()).thenReturn(GRAVITY_FLAGS);
227         when(mSystemUIToast.getInAnimation()).thenReturn(mAnimator);
228         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
229 
230         mInternetDialogController = new InternetDialogController(mContext,
231                 mock(UiEventLogger.class), mock(ActivityStarter.class), mAccessPointController,
232                 mSubscriptionManager, mTelephonyManager, mWifiManager,
233                 mConnectivityManager, mHandler, mExecutor, mBroadcastDispatcher,
234                 mock(KeyguardUpdateMonitor.class), mGlobalSettings, mKeyguardStateController,
235                 mWindowManager, mToastFactory, mWorkerHandler, mCarrierConfigTracker,
236                 mLocationController, mDialogTransitionAnimator, mWifiStateWorker, mFlags);
237         mSubscriptionManager.addOnSubscriptionsChangedListener(mExecutor,
238                 mInternetDialogController.mOnSubscriptionsChangedListener);
239         mInternetDialogController.onStart(mInternetDialogCallback, true);
240         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
241         mInternetDialogController.mActivityStarter = mActivityStarter;
242         mInternetDialogController.mWifiIconInjector = mWifiIconInjector;
243         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, false);
244         mFlags.set(Flags.SHARE_WIFI_QS_BUTTON, false);
245 
246         mConfig = new Configuration(mContext.getResources().getConfiguration());
247         Configuration c2 = new Configuration(mConfig);
248         c2.setLocale(Locale.US);
249         mContext.getResources().updateConfiguration(c2, null);
250     }
251 
252     @After
tearDown()253     public void tearDown() {
254         mStaticMockSession.finishMocking();
255         mContext.getResources().updateConfiguration(mConfig, null);
256     }
257 
258     @Test
connectCarrierNetwork_mergedCarrierEntryCanConnect_connectAndCreateSysUiToast()259     public void connectCarrierNetwork_mergedCarrierEntryCanConnect_connectAndCreateSysUiToast() {
260         when(mTelephonyManager.isDataEnabled()).thenReturn(true);
261         when(mKeyguardStateController.isUnlocked()).thenReturn(true);
262         when(mConnectivityManager.getActiveNetwork()).thenReturn(mNetwork);
263         when(mConnectivityManager.getNetworkCapabilities(mNetwork))
264                 .thenReturn(mNetworkCapabilities);
265         when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR))
266                 .thenReturn(false);
267 
268         when(mMergedCarrierEntry.canConnect()).thenReturn(true);
269         mTestableResources.addOverride(R.string.wifi_wont_autoconnect_for_now,
270             TOAST_MESSAGE_STRING);
271 
272         mInternetDialogController.connectCarrierNetwork();
273 
274         verify(mMergedCarrierEntry).connect(null /* callback */, false /* showToast */);
275         verify(mToastFactory).createToast(any(), eq(TOAST_MESSAGE_STRING), anyString(), anyInt(),
276             anyInt());
277     }
278 
279     @Test
connectCarrierNetwork_mergedCarrierEntryCanConnect_doNothingWhenSettingsOff()280     public void connectCarrierNetwork_mergedCarrierEntryCanConnect_doNothingWhenSettingsOff() {
281         when(mTelephonyManager.isDataEnabled()).thenReturn(false);
282 
283         mTestableResources.addOverride(R.string.wifi_wont_autoconnect_for_now,
284             TOAST_MESSAGE_STRING);
285         mInternetDialogController.connectCarrierNetwork();
286 
287         verify(mMergedCarrierEntry, never()).connect(null /* callback */, false /* showToast */);
288         verify(mToastFactory, never()).createToast(any(), anyString(), anyString(), anyInt(),
289             anyInt());
290     }
291 
292     @Test
connectCarrierNetwork_mergedCarrierEntryCanConnect_doNothingWhenKeyguardLocked()293     public void connectCarrierNetwork_mergedCarrierEntryCanConnect_doNothingWhenKeyguardLocked() {
294         when(mTelephonyManager.isDataEnabled()).thenReturn(true);
295         when(mKeyguardStateController.isUnlocked()).thenReturn(false);
296 
297         mTestableResources.addOverride(R.string.wifi_wont_autoconnect_for_now,
298             TOAST_MESSAGE_STRING);
299         mInternetDialogController.connectCarrierNetwork();
300 
301         verify(mMergedCarrierEntry, never()).connect(null /* callback */, false /* showToast */);
302         verify(mToastFactory, never()).createToast(any(), anyString(), anyString(), anyInt(),
303             anyInt());
304     }
305 
306     @Test
connectCarrierNetwork_mergedCarrierEntryCanConnect_doNothingWhenMobileIsPrimary()307     public void connectCarrierNetwork_mergedCarrierEntryCanConnect_doNothingWhenMobileIsPrimary() {
308         when(mTelephonyManager.isDataEnabled()).thenReturn(true);
309         when(mKeyguardStateController.isUnlocked()).thenReturn(true);
310         when(mConnectivityManager.getActiveNetwork()).thenReturn(mNetwork);
311         when(mConnectivityManager.getNetworkCapabilities(mNetwork))
312                 .thenReturn(mNetworkCapabilities);
313         when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR))
314                 .thenReturn(true);
315 
316         mTestableResources.addOverride(R.string.wifi_wont_autoconnect_for_now,
317             TOAST_MESSAGE_STRING);
318         mInternetDialogController.connectCarrierNetwork();
319 
320         verify(mMergedCarrierEntry, never()).connect(null /* callback */, false /* showToast */);
321         verify(mToastFactory, never()).createToast(any(), anyString(), anyString(), anyInt(),
322             anyInt());
323     }
324 
325     @Test
makeOverlayToast_withGravityFlags_addViewWithLayoutParams()326     public void makeOverlayToast_withGravityFlags_addViewWithLayoutParams() {
327         mTestableResources.addOverride(TOAST_MESSAGE_STRING_ID, TOAST_MESSAGE_STRING);
328 
329         mInternetDialogController.makeOverlayToast(TOAST_MESSAGE_STRING_ID);
330 
331         ArgumentCaptor<WindowManager.LayoutParams> paramsCaptor = ArgumentCaptor.forClass(
332             WindowManager.LayoutParams.class);
333         verify(mWindowManager).addView(eq(mToastView), paramsCaptor.capture());
334         WindowManager.LayoutParams params = paramsCaptor.getValue();
335         assertThat(params.format).isEqualTo(PixelFormat.TRANSLUCENT);
336         assertThat(params.type).isEqualTo(WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL);
337         assertThat(params.horizontalWeight).isEqualTo(TOAST_PARAMS_HORIZONTAL_WEIGHT);
338         assertThat(params.verticalWeight).isEqualTo(TOAST_PARAMS_VERTICAL_WEIGHT);
339     }
340 
341     @Test
makeOverlayToast_withAnimation_verifyAnimatorStart()342     public void makeOverlayToast_withAnimation_verifyAnimatorStart() {
343         mTestableResources.addOverride(TOAST_MESSAGE_STRING_ID, TOAST_MESSAGE_STRING);
344 
345         mInternetDialogController.makeOverlayToast(TOAST_MESSAGE_STRING_ID);
346 
347         verify(mAnimator).start();
348     }
349 
350     @Test
getDialogTitleText_withAirplaneModeOn_returnAirplaneMode()351     public void getDialogTitleText_withAirplaneModeOn_returnAirplaneMode() {
352         fakeAirplaneModeEnabled(true);
353 
354         assertTrue(TextUtils.equals(mInternetDialogController.getDialogTitleText(),
355                 getResourcesString("airplane_mode")));
356     }
357 
358     @Test
getDialogTitleText_withAirplaneModeOff_returnInternet()359     public void getDialogTitleText_withAirplaneModeOff_returnInternet() {
360         fakeAirplaneModeEnabled(false);
361 
362         assertTrue(TextUtils.equals(mInternetDialogController.getDialogTitleText(),
363                 getResourcesString("quick_settings_internet_label")));
364     }
365 
366     @Test
getSubtitleText_withApmOnAndWifiOff_returnWifiIsOff()367     public void getSubtitleText_withApmOnAndWifiOff_returnWifiIsOff() {
368         fakeAirplaneModeEnabled(true);
369         when(mWifiStateWorker.isWifiEnabled()).thenReturn(false);
370 
371         assertThat(mInternetDialogController.getSubtitleText(false))
372                 .isEqualTo(getResourcesString("wifi_is_off"));
373 
374         // if the Wi-Fi disallow config, then don't return Wi-Fi related string.
375         mInternetDialogController.mCanConfigWifi = false;
376 
377         assertThat(mInternetDialogController.getSubtitleText(false))
378                 .isNotEqualTo(getResourcesString("wifi_is_off"));
379     }
380 
381     @Test
getSubtitleText_withWifiOff_returnWifiIsOff()382     public void getSubtitleText_withWifiOff_returnWifiIsOff() {
383         fakeAirplaneModeEnabled(false);
384         when(mWifiStateWorker.isWifiEnabled()).thenReturn(false);
385 
386         assertThat(mInternetDialogController.getSubtitleText(false))
387                 .isEqualTo(getResourcesString("wifi_is_off"));
388 
389         // if the Wi-Fi disallow config, then don't return Wi-Fi related string.
390         mInternetDialogController.mCanConfigWifi = false;
391 
392         assertThat(mInternetDialogController.getSubtitleText(false))
393                 .isNotEqualTo(getResourcesString("wifi_is_off"));
394     }
395 
396     @Test
getSubtitleText_withNoWifiEntry_returnSearchWifi()397     public void getSubtitleText_withNoWifiEntry_returnSearchWifi() {
398         fakeAirplaneModeEnabled(false);
399         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
400         mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);
401 
402         assertThat(mInternetDialogController.getSubtitleText(true))
403                 .isEqualTo(getResourcesString("wifi_empty_list_wifi_on"));
404 
405         // if the Wi-Fi disallow config, then don't return Wi-Fi related string.
406         mInternetDialogController.mCanConfigWifi = false;
407 
408         assertThat(mInternetDialogController.getSubtitleText(true))
409                 .isNotEqualTo(getResourcesString("wifi_empty_list_wifi_on"));
410     }
411 
412     @Test
getSubtitleText_withWifiEntry_returnTapToConnect()413     public void getSubtitleText_withWifiEntry_returnTapToConnect() {
414         // The preconditions WiFi Entries is already in setUp()
415         fakeAirplaneModeEnabled(false);
416         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
417 
418         assertThat(mInternetDialogController.getSubtitleText(false))
419                 .isEqualTo(getResourcesString("tap_a_network_to_connect"));
420 
421         // if the Wi-Fi disallow config, then don't return Wi-Fi related string.
422         mInternetDialogController.mCanConfigWifi = false;
423 
424         assertThat(mInternetDialogController.getSubtitleText(false))
425                 .isNotEqualTo(getResourcesString("tap_a_network_to_connect"));
426     }
427 
428     @Test
getSubtitleText_deviceLockedWithWifiOn_returnUnlockToViewNetworks()429     public void getSubtitleText_deviceLockedWithWifiOn_returnUnlockToViewNetworks() {
430         fakeAirplaneModeEnabled(false);
431         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
432         when(mKeyguardStateController.isUnlocked()).thenReturn(false);
433 
434         assertTrue(TextUtils.equals(mInternetDialogController.getSubtitleText(false),
435                 getResourcesString("unlock_to_view_networks")));
436     }
437 
438     @Test
getSubtitleText_withNoService_returnNoNetworksAvailable()439     public void getSubtitleText_withNoService_returnNoNetworksAvailable() {
440         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
441         InternetDialogController spyController = spy(mInternetDialogController);
442         fakeAirplaneModeEnabled(false);
443         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
444         spyController.onAccessPointsChanged(null /* accessPoints */);
445 
446         doReturn(SUB_ID2).when(spyController).getActiveAutoSwitchNonDdsSubId();
447         doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mServiceState).getState();
448         doReturn(mServiceState).when(mTelephonyManager).getServiceState();
449         doReturn(TelephonyManager.DATA_DISCONNECTED).when(mTelephonyManager).getDataState();
450 
451         assertFalse(TextUtils.equals(spyController.getSubtitleText(false),
452                 getResourcesString("all_network_unavailable")));
453 
454         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
455                 .when(spyController).getActiveAutoSwitchNonDdsSubId();
456         spyController.onAccessPointsChanged(null /* accessPoints */);
457         assertTrue(TextUtils.equals(spyController.getSubtitleText(false),
458                 getResourcesString("all_network_unavailable")));
459     }
460 
461     @Test
getSubtitleText_withNoService_returnNoNetworksAvailable_flagOff()462     public void getSubtitleText_withNoService_returnNoNetworksAvailable_flagOff() {
463         InternetDialogController spyController = spy(mInternetDialogController);
464         fakeAirplaneModeEnabled(false);
465         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
466         spyController.onAccessPointsChanged(null /* accessPoints */);
467 
468         doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mServiceState).getState();
469         doReturn(mServiceState).when(mTelephonyManager).getServiceState();
470         doReturn(TelephonyManager.DATA_DISCONNECTED).when(mTelephonyManager).getDataState();
471 
472         assertTrue(TextUtils.equals(spyController.getSubtitleText(false),
473                 getResourcesString("all_network_unavailable")));
474 
475         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
476                 .when(spyController).getActiveAutoSwitchNonDdsSubId();
477         spyController.onAccessPointsChanged(null /* accessPoints */);
478         assertTrue(TextUtils.equals(spyController.getSubtitleText(false),
479                 getResourcesString("all_network_unavailable")));
480     }
481 
482     @Test
getSubtitleText_withMobileDataDisabled_returnNoOtherAvailable()483     public void getSubtitleText_withMobileDataDisabled_returnNoOtherAvailable() {
484         fakeAirplaneModeEnabled(false);
485         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
486         mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);
487 
488         doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
489         doReturn(mServiceState).when(mTelephonyManager).getServiceState();
490 
491         when(mTelephonyManager.isDataEnabled()).thenReturn(false);
492 
493         assertThat(mInternetDialogController.getSubtitleText(false))
494                 .isEqualTo(getResourcesString("non_carrier_network_unavailable"));
495 
496         // if the Wi-Fi disallow config, then don't return Wi-Fi related string.
497         mInternetDialogController.mCanConfigWifi = false;
498 
499         assertThat(mInternetDialogController.getSubtitleText(false))
500                 .isNotEqualTo(getResourcesString("non_carrier_network_unavailable"));
501     }
502 
503     @Test
getSubtitleText_withCarrierNetworkActiveOnly_returnNoOtherAvailable()504     public void getSubtitleText_withCarrierNetworkActiveOnly_returnNoOtherAvailable() {
505         fakeAirplaneModeEnabled(false);
506         when(mWifiStateWorker.isWifiEnabled()).thenReturn(true);
507         mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);
508         when(mMergedCarrierEntry.isDefaultNetwork()).thenReturn(true);
509 
510         assertThat(mInternetDialogController.getSubtitleText(false))
511                 .isEqualTo(getResourcesString("non_carrier_network_unavailable"));
512     }
513 
514     @Test
getWifiDetailsSettingsIntent_withNoKey_returnNull()515     public void getWifiDetailsSettingsIntent_withNoKey_returnNull() {
516         assertThat(mInternetDialogController.getWifiDetailsSettingsIntent(null)).isNull();
517     }
518 
519     @Test
getWifiDetailsSettingsIntent_withKey_returnIntent()520     public void getWifiDetailsSettingsIntent_withKey_returnIntent() {
521         assertThat(mInternetDialogController.getWifiDetailsSettingsIntent("test_key")).isNotNull();
522     }
523 
524     @Test
getInternetWifiDrawable_withConnectedEntry_returnIntentIconWithCorrectColor()525     public void getInternetWifiDrawable_withConnectedEntry_returnIntentIconWithCorrectColor() {
526         final Drawable drawable = mock(Drawable.class);
527         when(mWifiIconInjector.getIcon(anyBoolean(), anyInt())).thenReturn(drawable);
528 
529         mInternetDialogController.getInternetWifiDrawable(mConnectedEntry);
530 
531         verify(mWifiIconInjector).getIcon(eq(false), anyInt());
532         verify(drawable).setTint(mContext.getColor(R.color.connected_network_primary_color));
533     }
534 
535     @Test
getWifiDrawable_withWifiLevelUnreachable_returnNull()536     public void getWifiDrawable_withWifiLevelUnreachable_returnNull() {
537         when(mConnectedEntry.getLevel()).thenReturn(WIFI_LEVEL_UNREACHABLE);
538 
539         assertThat(mInternetDialogController.getWifiDrawable(mConnectedEntry)).isNull();
540     }
541 
542     @Test
getWifiDrawable_withHotspotNetworkEntry_returnHotspotDrawable()543     public void getWifiDrawable_withHotspotNetworkEntry_returnHotspotDrawable() {
544         HotspotNetworkEntry entry = mock(HotspotNetworkEntry.class);
545         when(entry.getDeviceType()).thenReturn(DEVICE_TYPE_PHONE);
546         Drawable hotspotDrawable = mock(Drawable.class);
547         mTestableResources.addOverride(getHotspotIconResource(DEVICE_TYPE_PHONE), hotspotDrawable);
548 
549         assertThat(mInternetDialogController.getWifiDrawable(entry)).isEqualTo(hotspotDrawable);
550     }
551 
552     @Test
launchWifiDetailsSetting_withNoWifiEntryKey_doNothing()553     public void launchWifiDetailsSetting_withNoWifiEntryKey_doNothing() {
554         mInternetDialogController.launchWifiDetailsSetting(null /* key */, mDialogLaunchView);
555 
556         verify(mActivityStarter, never())
557                 .postStartActivityDismissingKeyguard(any(Intent.class), anyInt());
558     }
559 
560     @Test
launchWifiDetailsSetting_withWifiEntryKey_startActivity()561     public void launchWifiDetailsSetting_withWifiEntryKey_startActivity() {
562         mInternetDialogController.launchWifiDetailsSetting("wifi_entry_key", mDialogLaunchView);
563 
564         verify(mActivityStarter).postStartActivityDismissingKeyguard(any(Intent.class), anyInt(),
565                 any());
566     }
567 
568     @Test
isDeviceLocked_keyguardIsUnlocked_returnFalse()569     public void isDeviceLocked_keyguardIsUnlocked_returnFalse() {
570         when(mKeyguardStateController.isUnlocked()).thenReturn(true);
571 
572         assertThat(mInternetDialogController.isDeviceLocked()).isFalse();
573     }
574 
575     @Test
isDeviceLocked_keyguardIsLocked_returnTrue()576     public void isDeviceLocked_keyguardIsLocked_returnTrue() {
577         when(mKeyguardStateController.isUnlocked()).thenReturn(false);
578 
579         assertThat(mInternetDialogController.isDeviceLocked()).isTrue();
580     }
581 
582     @Test
onAccessPointsChanged_canNotConfigWifi_doNothing()583     public void onAccessPointsChanged_canNotConfigWifi_doNothing() {
584         reset(mInternetDialogCallback);
585         mInternetDialogController.mCanConfigWifi = false;
586 
587         mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);
588 
589         verify(mInternetDialogCallback, never()).onAccessPointsChanged(any(), any(), anyBoolean());
590     }
591 
592     @Test
onAccessPointsChanged_nullAccessPoints_callbackBothNull()593     public void onAccessPointsChanged_nullAccessPoints_callbackBothNull() {
594         reset(mInternetDialogCallback);
595 
596         mInternetDialogController.onAccessPointsChanged(null /* accessPoints */);
597 
598         verify(mInternetDialogCallback).onAccessPointsChanged(null /* wifiEntries */,
599                 null /* connectedEntry */, false /* hasMoreEntry */);
600     }
601 
602     @Test
onAccessPointsChanged_oneConnectedEntry_callbackConnectedEntryOnly()603     public void onAccessPointsChanged_oneConnectedEntry_callbackConnectedEntryOnly() {
604         reset(mInternetDialogCallback);
605         mAccessPoints.clear();
606         mAccessPoints.add(mConnectedEntry);
607 
608         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
609 
610         mWifiEntries.clear();
611         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries, mConnectedEntry,
612                 false /* hasMoreEntry */);
613     }
614 
615     @Test
onAccessPointsChanged_noConnectedEntryAndOneOther_callbackWifiEntriesOnly()616     public void onAccessPointsChanged_noConnectedEntryAndOneOther_callbackWifiEntriesOnly() {
617         reset(mInternetDialogCallback);
618         mAccessPoints.clear();
619         mAccessPoints.add(mWifiEntry1);
620 
621         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
622 
623         mWifiEntries.clear();
624         mWifiEntries.add(mWifiEntry1);
625         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries,
626                 null /* connectedEntry */, false /* hasMoreEntry */);
627     }
628 
629     @Test
onAccessPointsChanged_oneConnectedEntryAndOneOther_callbackCorrectly()630     public void onAccessPointsChanged_oneConnectedEntryAndOneOther_callbackCorrectly() {
631         reset(mInternetDialogCallback);
632         mAccessPoints.clear();
633         mAccessPoints.add(mConnectedEntry);
634         mAccessPoints.add(mWifiEntry1);
635 
636         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
637 
638         mWifiEntries.clear();
639         mWifiEntries.add(mWifiEntry1);
640         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries, mConnectedEntry,
641                 false /* hasMoreEntry */);
642     }
643 
644     @Test
onAccessPointsChanged_oneConnectedEntryAndTwoOthers_callbackCorrectly()645     public void onAccessPointsChanged_oneConnectedEntryAndTwoOthers_callbackCorrectly() {
646         reset(mInternetDialogCallback);
647         mAccessPoints.clear();
648         mAccessPoints.add(mConnectedEntry);
649         mAccessPoints.add(mWifiEntry1);
650         mAccessPoints.add(mWifiEntry2);
651 
652         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
653 
654         mWifiEntries.clear();
655         mWifiEntries.add(mWifiEntry1);
656         mWifiEntries.add(mWifiEntry2);
657         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries, mConnectedEntry,
658                 false /* hasMoreEntry */);
659     }
660 
661     @Test
onAccessPointsChanged_oneConnectedEntryAndThreeOthers_callbackCutMore()662     public void onAccessPointsChanged_oneConnectedEntryAndThreeOthers_callbackCutMore() {
663         reset(mInternetDialogCallback);
664         mAccessPoints.clear();
665         mAccessPoints.add(mConnectedEntry);
666         mAccessPoints.add(mWifiEntry1);
667         mAccessPoints.add(mWifiEntry2);
668         mAccessPoints.add(mWifiEntry3);
669 
670         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
671 
672         mWifiEntries.clear();
673         mWifiEntries.add(mWifiEntry1);
674         mWifiEntries.add(mWifiEntry2);
675         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries, mConnectedEntry,
676                 true /* hasMoreEntry */);
677     }
678 
679     @Test
onAccessPointsChanged_fourWifiEntries_callbackCutMore()680     public void onAccessPointsChanged_fourWifiEntries_callbackCutMore() {
681         reset(mInternetDialogCallback);
682         mAccessPoints.clear();
683         mAccessPoints.add(mWifiEntry1);
684         mAccessPoints.add(mWifiEntry2);
685         mAccessPoints.add(mWifiEntry3);
686         mAccessPoints.add(mWifiEntry4);
687 
688         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
689 
690         mWifiEntries.clear();
691         mWifiEntries.add(mWifiEntry1);
692         mWifiEntries.add(mWifiEntry2);
693         mWifiEntries.add(mWifiEntry3);
694         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries,
695                 null /* connectedEntry */, true /* hasMoreEntry */);
696     }
697 
698     @Test
onAccessPointsChanged_wifiIsDefaultButNoInternetAccess_putIntoWifiEntries()699     public void onAccessPointsChanged_wifiIsDefaultButNoInternetAccess_putIntoWifiEntries() {
700         reset(mInternetDialogCallback);
701         mAccessPoints.clear();
702         when(mWifiEntry1.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_CONNECTED);
703         when(mWifiEntry1.isDefaultNetwork()).thenReturn(true);
704         when(mWifiEntry1.hasInternetAccess()).thenReturn(false);
705         mAccessPoints.add(mWifiEntry1);
706 
707         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
708 
709         mWifiEntries.clear();
710         mWifiEntries.add(mWifiEntry1);
711         verify(mInternetDialogCallback).onAccessPointsChanged(mWifiEntries,
712                 null /* connectedEntry */, false /* hasMoreEntry */);
713     }
714 
715     @Test
onAccessPointsChanged_connectedWifiNoInternetAccess_shouldSetListener()716     public void onAccessPointsChanged_connectedWifiNoInternetAccess_shouldSetListener() {
717         reset(mWifiEntry1);
718         mAccessPoints.clear();
719         when(mWifiEntry1.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_CONNECTED);
720         when(mWifiEntry1.isDefaultNetwork()).thenReturn(true);
721         when(mWifiEntry1.hasInternetAccess()).thenReturn(false);
722         mAccessPoints.add(mWifiEntry1);
723 
724         mInternetDialogController.onAccessPointsChanged(mAccessPoints);
725 
726         verify(mWifiEntry1).setListener(mInternetDialogController.mConnectedWifiInternetMonitor);
727     }
728 
729     @Test
onUpdated_connectedWifiHasInternetAccess_shouldScanWifiAccessPoints()730     public void onUpdated_connectedWifiHasInternetAccess_shouldScanWifiAccessPoints() {
731         reset(mAccessPointController);
732         when(mWifiEntry1.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_CONNECTED);
733         when(mWifiEntry1.isDefaultNetwork()).thenReturn(true);
734         when(mWifiEntry1.hasInternetAccess()).thenReturn(false);
735         InternetDialogController.ConnectedWifiInternetMonitor mConnectedWifiInternetMonitor =
736                 mInternetDialogController.mConnectedWifiInternetMonitor;
737         mConnectedWifiInternetMonitor.registerCallbackIfNeed(mWifiEntry1);
738 
739         // When the hasInternetAccess() changed to true, and call back the onUpdated() function.
740         when(mWifiEntry1.hasInternetAccess()).thenReturn(true);
741         mConnectedWifiInternetMonitor.onUpdated();
742 
743         verify(mAccessPointController).scanForAccessPoints();
744     }
745 
746     @Test
onWifiScan_isWifiEnabledFalse_callbackOnWifiScanFalse()747     public void onWifiScan_isWifiEnabledFalse_callbackOnWifiScanFalse() {
748         reset(mInternetDialogCallback);
749         when(mWifiStateWorker.isWifiEnabled()).thenReturn(false);
750 
751         mInternetDialogController.onWifiScan(true);
752 
753         verify(mInternetDialogCallback).onWifiScan(false);
754     }
755 
756     @Test
onWifiScan_isDeviceLockedTrue_callbackOnWifiScanFalse()757     public void onWifiScan_isDeviceLockedTrue_callbackOnWifiScanFalse() {
758         reset(mInternetDialogCallback);
759         when(mKeyguardStateController.isUnlocked()).thenReturn(false);
760 
761         mInternetDialogController.onWifiScan(true);
762 
763         verify(mInternetDialogCallback).onWifiScan(false);
764     }
765 
766     @Test
onWifiScan_onWifiScanFalse_callbackOnWifiScanFalse()767     public void onWifiScan_onWifiScanFalse_callbackOnWifiScanFalse() {
768         reset(mInternetDialogCallback);
769 
770         mInternetDialogController.onWifiScan(false);
771 
772         verify(mInternetDialogCallback).onWifiScan(false);
773     }
774 
775     @Test
onWifiScan_onWifiScanTrue_callbackOnWifiScanTrue()776     public void onWifiScan_onWifiScanTrue_callbackOnWifiScanTrue() {
777         reset(mInternetDialogCallback);
778 
779         mInternetDialogController.onWifiScan(true);
780 
781         verify(mInternetDialogCallback).onWifiScan(true);
782     }
783 
784     @Test
setMergedCarrierWifiEnabledIfNeed_carrierProvisionsEnabled_doNothing()785     public void setMergedCarrierWifiEnabledIfNeed_carrierProvisionsEnabled_doNothing() {
786         when(mCarrierConfigTracker.getCarrierProvisionsWifiMergedNetworksBool(SUB_ID))
787                 .thenReturn(true);
788 
789         mInternetDialogController.setMergedCarrierWifiEnabledIfNeed(SUB_ID, true);
790 
791         verify(mMergedCarrierEntry, never()).setEnabled(anyBoolean());
792     }
793 
794     @Test
setMergedCarrierWifiEnabledIfNeed_mergedCarrierEntryEmpty_doesntCrash()795     public void setMergedCarrierWifiEnabledIfNeed_mergedCarrierEntryEmpty_doesntCrash() {
796         when(mCarrierConfigTracker.getCarrierProvisionsWifiMergedNetworksBool(SUB_ID))
797                 .thenReturn(false);
798         when(mAccessPointController.getMergedCarrierEntry()).thenReturn(null);
799 
800         mInternetDialogController.setMergedCarrierWifiEnabledIfNeed(SUB_ID, true);
801     }
802 
803     @Test
setMergedCarrierWifiEnabledIfNeed_neededSetMergedCarrierEntry_setTogether()804     public void setMergedCarrierWifiEnabledIfNeed_neededSetMergedCarrierEntry_setTogether() {
805         when(mCarrierConfigTracker.getCarrierProvisionsWifiMergedNetworksBool(SUB_ID))
806                 .thenReturn(false);
807 
808         mInternetDialogController.setMergedCarrierWifiEnabledIfNeed(SUB_ID, true);
809 
810         verify(mMergedCarrierEntry).setEnabled(true);
811 
812         mInternetDialogController.setMergedCarrierWifiEnabledIfNeed(SUB_ID, false);
813 
814         verify(mMergedCarrierEntry).setEnabled(false);
815     }
816 
817     @Test
isWifiScanEnabled_locationOff_returnFalse()818     public void isWifiScanEnabled_locationOff_returnFalse() {
819         when(mLocationController.isLocationEnabled()).thenReturn(false);
820         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false);
821 
822         assertThat(mInternetDialogController.isWifiScanEnabled()).isFalse();
823 
824         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true);
825 
826         assertThat(mInternetDialogController.isWifiScanEnabled()).isFalse();
827     }
828 
829     @Test
isWifiScanEnabled_locationOn_returnIsScanAlwaysAvailable()830     public void isWifiScanEnabled_locationOn_returnIsScanAlwaysAvailable() {
831         when(mLocationController.isLocationEnabled()).thenReturn(true);
832         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false);
833 
834         assertThat(mInternetDialogController.isWifiScanEnabled()).isFalse();
835 
836         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true);
837 
838         assertThat(mInternetDialogController.isWifiScanEnabled()).isTrue();
839     }
840 
841     @Test
getSignalStrengthIcon_differentSubId()842     public void getSignalStrengthIcon_differentSubId() {
843         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
844         InternetDialogController spyController = spy(mInternetDialogController);
845         Drawable icons = spyController.getSignalStrengthIcon(SUB_ID, mContext, 1, 1, 0, false);
846         Drawable icons2 = spyController.getSignalStrengthIcon(SUB_ID2, mContext, 1, 1, 0, false);
847 
848         assertThat(icons).isNotEqualTo(icons2);
849     }
850 
851     @Test
getActiveAutoSwitchNonDdsSubId()852     public void getActiveAutoSwitchNonDdsSubId() {
853         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
854         // active on non-DDS
855         SubscriptionInfo info = mock(SubscriptionInfo.class);
856         doReturn(SUB_ID2).when(info).getSubscriptionId();
857         when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info);
858 
859         int subId = mInternetDialogController.getActiveAutoSwitchNonDdsSubId();
860         assertThat(subId).isEqualTo(SUB_ID2);
861 
862         // active on CBRS
863         doReturn(true).when(info).isOpportunistic();
864         subId = mInternetDialogController.getActiveAutoSwitchNonDdsSubId();
865         assertThat(subId).isEqualTo(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
866 
867         // active on DDS
868         doReturn(false).when(info).isOpportunistic();
869         doReturn(SUB_ID).when(info).getSubscriptionId();
870         when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info);
871 
872         subId = mInternetDialogController.getActiveAutoSwitchNonDdsSubId();
873         assertThat(subId).isEqualTo(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
874     }
875 
876     @Test
getActiveAutoSwitchNonDdsSubId_flagOff()877     public void getActiveAutoSwitchNonDdsSubId_flagOff() {
878         // active on non-DDS
879         SubscriptionInfo info = mock(SubscriptionInfo.class);
880         doReturn(SUB_ID2).when(info).getSubscriptionId();
881         when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info);
882 
883         int subId = mInternetDialogController.getActiveAutoSwitchNonDdsSubId();
884         assertThat(subId).isEqualTo(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
885     }
886 
887     @Test
getMobileNetworkSummary()888     public void getMobileNetworkSummary() {
889         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
890         Resources res1 = mock(Resources.class);
891         doReturn("EDGE").when(res1).getString(anyInt());
892         Resources res2 = mock(Resources.class);
893         doReturn("LTE").when(res2).getString(anyInt());
894         when(SubscriptionManager.getResourcesForSubId(any(), eq(SUB_ID))).thenReturn(res1);
895         when(SubscriptionManager.getResourcesForSubId(any(), eq(SUB_ID2))).thenReturn(res2);
896 
897         InternetDialogController spyController = spy(mInternetDialogController);
898         Map<Integer, TelephonyDisplayInfo> mSubIdTelephonyDisplayInfoMap =
899                 spyController.mSubIdTelephonyDisplayInfoMap;
900         TelephonyDisplayInfo info1 = new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_EDGE,
901                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
902         TelephonyDisplayInfo info2 = new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
903                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
904 
905         mSubIdTelephonyDisplayInfoMap.put(SUB_ID, info1);
906         mSubIdTelephonyDisplayInfoMap.put(SUB_ID2, info2);
907 
908         doReturn(SUB_ID2).when(spyController).getActiveAutoSwitchNonDdsSubId();
909         doReturn(true).when(spyController).isMobileDataEnabled();
910         doReturn(true).when(spyController).activeNetworkIsCellular();
911         String dds = spyController.getMobileNetworkSummary(SUB_ID);
912         String nonDds = spyController.getMobileNetworkSummary(SUB_ID2);
913 
914         String ddsNetworkType = dds.split("/")[1];
915         String nonDdsNetworkType = nonDds.split("/")[1];
916         assertThat(dds).contains(mContext.getString(R.string.mobile_data_poor_connection));
917         assertThat(ddsNetworkType).isNotEqualTo(nonDdsNetworkType);
918     }
919 
920     @Test
getMobileNetworkSummary_flagOff()921     public void getMobileNetworkSummary_flagOff() {
922         InternetDialogController spyController = spy(mInternetDialogController);
923         doReturn(true).when(spyController).isMobileDataEnabled();
924         doReturn(true).when(spyController).activeNetworkIsCellular();
925         String dds = spyController.getMobileNetworkSummary(SUB_ID);
926 
927         assertThat(dds).contains(mContext.getString(R.string.mobile_data_connection_active));
928     }
929 
930     @Test
launchMobileNetworkSettings_validSubId()931     public void launchMobileNetworkSettings_validSubId() {
932         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
933         InternetDialogController spyController = spy(mInternetDialogController);
934         doReturn(SUB_ID2).when(spyController).getActiveAutoSwitchNonDdsSubId();
935         spyController.launchMobileNetworkSettings(mDialogLaunchView);
936 
937         verify(mActivityStarter).postStartActivityDismissingKeyguard(any(Intent.class), anyInt(),
938                 any());
939     }
940 
941     @Test
launchMobileNetworkSettings_invalidSubId()942     public void launchMobileNetworkSettings_invalidSubId() {
943         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
944         InternetDialogController spyController = spy(mInternetDialogController);
945         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
946                 .when(spyController).getActiveAutoSwitchNonDdsSubId();
947         spyController.launchMobileNetworkSettings(mDialogLaunchView);
948 
949         verify(mActivityStarter, never())
950                 .postStartActivityDismissingKeyguard(any(Intent.class), anyInt());
951     }
952 
953     @Test
setAutoDataSwitchMobileDataPolicy()954     public void setAutoDataSwitchMobileDataPolicy() {
955         mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true);
956         mInternetDialogController.setAutoDataSwitchMobileDataPolicy(SUB_ID, true);
957 
958         verify(mTelephonyManager).setMobileDataPolicyEnabled(eq(
959                 TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH), eq(true));
960     }
961 
962     @Test
getSignalStrengthDrawableWithLevel_carrierNetworkIsNotActive_useMobileDataLevel()963     public void getSignalStrengthDrawableWithLevel_carrierNetworkIsNotActive_useMobileDataLevel() {
964         // Fake mobile data level as SIGNAL_STRENGTH_POOR(1)
965         when(mSignalStrength.getLevel()).thenReturn(SIGNAL_STRENGTH_POOR);
966         // Fake carrier network level as WIFI_LEVEL_MAX(4)
967         when(mInternetDialogController.getCarrierNetworkLevel()).thenReturn(WIFI_LEVEL_MAX);
968 
969         InternetDialogController spyController = spy(mInternetDialogController);
970         spyController.getSignalStrengthDrawableWithLevel(false /* isCarrierNetworkActive */, 0);
971 
972         verify(spyController).getSignalStrengthIcon(eq(0), any(), eq(SIGNAL_STRENGTH_POOR),
973                 eq(NUM_SIGNAL_STRENGTH_BINS), anyInt(), anyBoolean());
974     }
975 
976     @Test
getSignalStrengthDrawableWithLevel_carrierNetworkIsActive_useCarrierNetworkLevel()977     public void getSignalStrengthDrawableWithLevel_carrierNetworkIsActive_useCarrierNetworkLevel() {
978         // Fake mobile data level as SIGNAL_STRENGTH_POOR(1)
979         when(mSignalStrength.getLevel()).thenReturn(SIGNAL_STRENGTH_POOR);
980         // Fake carrier network level as WIFI_LEVEL_MAX(4)
981         when(mInternetDialogController.getCarrierNetworkLevel()).thenReturn(WIFI_LEVEL_MAX);
982 
983         InternetDialogController spyController = spy(mInternetDialogController);
984         spyController.getSignalStrengthDrawableWithLevel(true /* isCarrierNetworkActive */, 0);
985 
986         verify(spyController).getSignalStrengthIcon(eq(0), any(), eq(WIFI_LEVEL_MAX),
987                 eq(WIFI_LEVEL_MAX + 1), anyInt(), anyBoolean());
988     }
989 
990     @Test
getCarrierNetworkLevel_mergedCarrierEntryIsNull_returnMinLevel()991     public void getCarrierNetworkLevel_mergedCarrierEntryIsNull_returnMinLevel() {
992         when(mAccessPointController.getMergedCarrierEntry()).thenReturn(null);
993 
994         assertThat(mInternetDialogController.getCarrierNetworkLevel()).isEqualTo(WIFI_LEVEL_MIN);
995     }
996 
997     @Test
getCarrierNetworkLevel_getUnreachableLevel_returnMinLevel()998     public void getCarrierNetworkLevel_getUnreachableLevel_returnMinLevel() {
999         when(mMergedCarrierEntry.getLevel()).thenReturn(WIFI_LEVEL_UNREACHABLE);
1000 
1001         assertThat(mInternetDialogController.getCarrierNetworkLevel()).isEqualTo(WIFI_LEVEL_MIN);
1002     }
1003 
1004     @Test
getCarrierNetworkLevel_getAvailableLevel_returnSameLevel()1005     public void getCarrierNetworkLevel_getAvailableLevel_returnSameLevel() {
1006         for (int level = WIFI_LEVEL_MIN; level <= WIFI_LEVEL_MAX; level++) {
1007             when(mMergedCarrierEntry.getLevel()).thenReturn(level);
1008 
1009             assertThat(mInternetDialogController.getCarrierNetworkLevel()).isEqualTo(level);
1010         }
1011     }
1012 
1013     @Test
getMobileNetworkSummary_withCarrierNetworkChange()1014     public void getMobileNetworkSummary_withCarrierNetworkChange() {
1015         Resources res = mock(Resources.class);
1016         doReturn("Carrier network changing").when(res).getString(anyInt());
1017         when(SubscriptionManager.getResourcesForSubId(any(), eq(SUB_ID))).thenReturn(res);
1018         InternetDialogController spyController = spy(mInternetDialogController);
1019         Map<Integer, TelephonyDisplayInfo> mSubIdTelephonyDisplayInfoMap =
1020                 spyController.mSubIdTelephonyDisplayInfoMap;
1021         TelephonyDisplayInfo info = new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1022                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
1023 
1024         mSubIdTelephonyDisplayInfoMap.put(SUB_ID, info);
1025         doReturn(true).when(spyController).isMobileDataEnabled();
1026         doReturn(true).when(spyController).activeNetworkIsCellular();
1027         spyController.mCarrierNetworkChangeMode = true;
1028         String dds = spyController.getMobileNetworkSummary(SUB_ID);
1029 
1030         assertThat(dds).contains(mContext.getString(com.android.settingslib.R.string.carrier_network_change_mode));
1031     }
1032 
1033     @Test
getConfiguratorQrCodeGeneratorIntentOrNull_wifiNotShareable_returnNull()1034     public void getConfiguratorQrCodeGeneratorIntentOrNull_wifiNotShareable_returnNull() {
1035         mFlags.set(Flags.SHARE_WIFI_QS_BUTTON, true);
1036         when(mConnectedEntry.canShare()).thenReturn(false);
1037         assertThat(mInternetDialogController.getConfiguratorQrCodeGeneratorIntentOrNull(
1038                 mConnectedEntry)).isNull();
1039     }
1040     @Test
getConfiguratorQrCodeGeneratorIntentOrNull_flagOff_returnNull()1041     public void getConfiguratorQrCodeGeneratorIntentOrNull_flagOff_returnNull() {
1042         mFlags.set(Flags.SHARE_WIFI_QS_BUTTON, false);
1043         when(mConnectedEntry.canShare()).thenReturn(true);
1044         assertThat(mInternetDialogController.getConfiguratorQrCodeGeneratorIntentOrNull(
1045                 mConnectedEntry)).isNull();
1046     }
1047 
1048     @Test
getConfiguratorQrCodeGeneratorIntentOrNull_configurationNull_returnNull()1049     public void getConfiguratorQrCodeGeneratorIntentOrNull_configurationNull_returnNull() {
1050         mFlags.set(Flags.SHARE_WIFI_QS_BUTTON, true);
1051         when(mConnectedEntry.canShare()).thenReturn(true);
1052         when(mConnectedEntry.getWifiConfiguration()).thenReturn(null);
1053         assertThat(mInternetDialogController.getConfiguratorQrCodeGeneratorIntentOrNull(
1054                 mConnectedEntry)).isNull();
1055     }
1056 
1057     @Test
getConfiguratorQrCodeGeneratorIntentOrNull_wifiShareable()1058     public void getConfiguratorQrCodeGeneratorIntentOrNull_wifiShareable() {
1059         mFlags.set(Flags.SHARE_WIFI_QS_BUTTON, true);
1060         when(mConnectedEntry.canShare()).thenReturn(true);
1061         when(mConnectedEntry.getWifiConfiguration()).thenReturn(mWifiConfiguration);
1062         assertThat(mInternetDialogController.getConfiguratorQrCodeGeneratorIntentOrNull(
1063                 mConnectedEntry)).isNotNull();
1064     }
1065 
1066     @Test
onStop_cleanUp()1067     public void onStop_cleanUp() {
1068         doReturn(SUB_ID).when(mTelephonyManager).getSubscriptionId();
1069         assertThat(mInternetDialogController.mSubIdTelephonyManagerMap.get(SUB_ID)).isEqualTo(
1070                 mTelephonyManager);
1071         assertThat(mInternetDialogController.mSubIdTelephonyCallbackMap.get(SUB_ID)).isNotNull();
1072         assertThat(mInternetDialogController.mCallback).isNotNull();
1073 
1074         mInternetDialogController.onStop();
1075 
1076         verify(mTelephonyManager).unregisterTelephonyCallback(any(TelephonyCallback.class));
1077         assertThat(mInternetDialogController.mSubIdTelephonyDisplayInfoMap.isEmpty()).isTrue();
1078         assertThat(mInternetDialogController.mSubIdTelephonyManagerMap.isEmpty()).isTrue();
1079         assertThat(mInternetDialogController.mSubIdTelephonyCallbackMap.isEmpty()).isTrue();
1080         verify(mSubscriptionManager).removeOnSubscriptionsChangedListener(mInternetDialogController
1081                 .mOnSubscriptionsChangedListener);
1082         verify(mAccessPointController).removeAccessPointCallback(mInternetDialogController);
1083         verify(mConnectivityManager).unregisterNetworkCallback(
1084                 any(ConnectivityManager.NetworkCallback.class));
1085         assertThat(mInternetDialogController.mCallback).isNull();
1086     }
1087 
1088     @Test
hasActiveSubIdOnDds_noDds_returnFalse()1089     public void hasActiveSubIdOnDds_noDds_returnFalse() {
1090         when(SubscriptionManager.getDefaultDataSubscriptionId())
1091                 .thenReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
1092 
1093         mInternetDialogController.mOnSubscriptionsChangedListener.onSubscriptionsChanged();
1094 
1095         assertThat(mInternetDialogController.hasActiveSubIdOnDds()).isFalse();
1096     }
1097 
1098     @Test
hasActiveSubIdOnDds_activeDds_returnTrue()1099     public void hasActiveSubIdOnDds_activeDds_returnTrue() {
1100         mInternetDialogController.mOnSubscriptionsChangedListener.onSubscriptionsChanged();
1101 
1102         assertThat(mInternetDialogController.hasActiveSubIdOnDds()).isTrue();
1103     }
1104 
1105     @Test
hasActiveSubIdOnDds_activeDdsAndHasProvisioning_returnFalse()1106     public void hasActiveSubIdOnDds_activeDdsAndHasProvisioning_returnFalse() {
1107         when(SubscriptionManager.getDefaultDataSubscriptionId())
1108                 .thenReturn(SUB_ID);
1109         SubscriptionInfo info = mock(SubscriptionInfo.class);
1110         when(info.isEmbedded()).thenReturn(true);
1111         when(info.getProfileClass()).thenReturn(PROFILE_CLASS_PROVISIONING);
1112         when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(info);
1113 
1114         mInternetDialogController.mOnSubscriptionsChangedListener.onSubscriptionsChanged();
1115 
1116         assertThat(mInternetDialogController.hasActiveSubIdOnDds()).isFalse();
1117     }
1118 
1119     @Test
hasActiveSubIdOnDds_activeDdsAndIsOnlyNonTerrestrialNetwork_returnFalse()1120     public void hasActiveSubIdOnDds_activeDdsAndIsOnlyNonTerrestrialNetwork_returnFalse() {
1121         when(SubscriptionManager.getDefaultDataSubscriptionId())
1122                 .thenReturn(SUB_ID);
1123         SubscriptionInfo info = mock(SubscriptionInfo.class);
1124         when(info.isEmbedded()).thenReturn(true);
1125         when(info.isOnlyNonTerrestrialNetwork()).thenReturn(true);
1126         when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(info);
1127 
1128         mInternetDialogController.mOnSubscriptionsChangedListener.onSubscriptionsChanged();
1129 
1130         assertFalse(mInternetDialogController.hasActiveSubIdOnDds());
1131     }
1132 
1133     @Test
hasActiveSubIdOnDds_activeDdsAndIsNotOnlyNonTerrestrialNetwork_returnTrue()1134     public void hasActiveSubIdOnDds_activeDdsAndIsNotOnlyNonTerrestrialNetwork_returnTrue() {
1135         when(SubscriptionManager.getDefaultDataSubscriptionId())
1136                 .thenReturn(SUB_ID);
1137         SubscriptionInfo info = mock(SubscriptionInfo.class);
1138         when(info.isEmbedded()).thenReturn(true);
1139         when(info.isOnlyNonTerrestrialNetwork()).thenReturn(false);
1140         when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(info);
1141 
1142         mInternetDialogController.mOnSubscriptionsChangedListener.onSubscriptionsChanged();
1143 
1144         assertTrue(mInternetDialogController.hasActiveSubIdOnDds());
1145     }
1146 
getResourcesString(String name)1147     private String getResourcesString(String name) {
1148         return mContext.getResources().getString(getResourcesId(name));
1149     }
1150 
getResourcesId(String name)1151     private int getResourcesId(String name) {
1152         return mContext.getResources().getIdentifier(name, "string",
1153                 mContext.getPackageName());
1154     }
1155 
fakeAirplaneModeEnabled(boolean enabled)1156     private void fakeAirplaneModeEnabled(boolean enabled) {
1157         when(mGlobalSettings.getInt(eq(AIRPLANE_MODE_ON), anyInt())).thenReturn(enabled ? 1 : 0);
1158     }
1159 }
1160