Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 16 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 20 | import static junit.framework.Assert.assertNotNull; |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 21 | |
| 22 | import static org.mockito.Matchers.any; |
| 23 | import static org.mockito.Matchers.anyBoolean; |
| 24 | import static org.mockito.Matchers.anyInt; |
| 25 | import static org.mockito.Matchers.anyString; |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 26 | import static org.mockito.Matchers.eq; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 27 | import static org.mockito.Matchers.isA; |
| 28 | import static org.mockito.Mockito.atLeastOnce; |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 29 | import static org.mockito.Mockito.doAnswer; |
Jason Monk | f6a3cf9 | 2016-02-29 13:01:08 -0500 | [diff] [blame] | 30 | import static org.mockito.Mockito.mock; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 33 | import android.app.Instrumentation; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 34 | import android.content.Intent; |
| 35 | import android.net.ConnectivityManager; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 36 | import android.net.Network; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 37 | import android.net.NetworkCapabilities; |
| 38 | import android.net.wifi.WifiManager; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 39 | import android.os.Handler; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 40 | import android.provider.Settings; |
| 41 | import android.provider.Settings.Global; |
| 42 | import android.telephony.PhoneStateListener; |
| 43 | import android.telephony.ServiceState; |
| 44 | import android.telephony.SignalStrength; |
| 45 | import android.telephony.SubscriptionInfo; |
| 46 | import android.telephony.SubscriptionManager; |
| 47 | import android.telephony.TelephonyManager; |
| 48 | import android.testing.TestableLooper; |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 49 | import android.testing.TestableResources; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 50 | import android.util.Log; |
| 51 | |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 52 | import androidx.test.InstrumentationRegistry; |
| 53 | |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 54 | import com.android.internal.telephony.cdma.EriInfo; |
Evan Laird | 06e9fd8 | 2018-02-10 09:36:55 -0800 | [diff] [blame] | 55 | import com.android.settingslib.graph.SignalDrawable; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 56 | import com.android.settingslib.net.DataUsageController; |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 57 | import com.android.systemui.R; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 58 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 59 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 60 | import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener; |
| 61 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
| 62 | import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; |
| 63 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.Config; |
| 64 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.SubscriptionDefaults; |
| 65 | |
| 66 | import org.junit.Before; |
| 67 | import org.junit.Rule; |
| 68 | import org.junit.rules.TestWatcher; |
| 69 | import org.junit.runner.Description; |
| 70 | import org.mockito.ArgumentCaptor; |
| 71 | import org.mockito.Mockito; |
| 72 | |
| 73 | import java.io.PrintWriter; |
| 74 | import java.io.StringWriter; |
| 75 | import java.util.ArrayList; |
| 76 | import java.util.List; |
| 77 | |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 78 | public class NetworkControllerBaseTest extends SysuiTestCase { |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 79 | private static final String TAG = "NetworkControllerBaseTest"; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 80 | protected static final int DEFAULT_LEVEL = 2; |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 81 | protected static final int DEFAULT_SIGNAL_STRENGTH = DEFAULT_LEVEL; |
| 82 | protected static final int DEFAULT_QS_SIGNAL_STRENGTH = DEFAULT_LEVEL; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 83 | protected static final int DEFAULT_ICON = TelephonyIcons.ICON_3G; |
Amin Shaikh | d64e626 | 2018-03-08 10:08:13 -0500 | [diff] [blame] | 84 | protected static final int DEFAULT_QS_ICON = TelephonyIcons.ICON_3G; |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 85 | protected static final String NO_DATA_STRING = "Data disabled"; |
| 86 | protected static final String NOT_DEFAULT_DATA_STRING = "Not default data"; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 87 | |
| 88 | protected NetworkControllerImpl mNetworkController; |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 89 | protected MobileSignalController mMobileSignalController; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 90 | protected PhoneStateListener mPhoneStateListener; |
Jason Monk | 67b2c16 | 2016-01-12 10:24:09 -0500 | [diff] [blame] | 91 | protected SignalStrength mSignalStrength; |
| 92 | protected ServiceState mServiceState; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 93 | protected ConnectivityManager mMockCm; |
| 94 | protected WifiManager mMockWm; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 95 | protected SubscriptionManager mMockSm; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 96 | protected TelephonyManager mMockTm; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 97 | protected BroadcastDispatcher mMockBd; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 98 | protected Config mConfig; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 99 | protected CallbackHandler mCallbackHandler; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 100 | protected SubscriptionDefaults mMockSubDefaults; |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 101 | protected DeviceProvisionedController mMockProvisionController; |
| 102 | protected DeviceProvisionedListener mUserCallback; |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 103 | protected Instrumentation mInstrumentation; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 104 | |
Jason Monk | 18985e0 | 2015-01-07 12:07:06 -0500 | [diff] [blame] | 105 | protected int mSubId; |
| 106 | |
Jason Monk | d8e4bf4 | 2014-12-12 10:23:12 -0500 | [diff] [blame] | 107 | private NetworkCapabilities mNetCapabilities; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 108 | private ConnectivityManager.NetworkCallback mNetworkCallback; |
Jason Monk | d8e4bf4 | 2014-12-12 10:23:12 -0500 | [diff] [blame] | 109 | |
Geoffrey Pitsch | d80035a | 2016-09-14 09:34:17 -0400 | [diff] [blame] | 110 | @Rule |
| 111 | public TestWatcher failWatcher = new TestWatcher() { |
| 112 | @Override |
| 113 | protected void failed(Throwable e, Description description) { |
| 114 | // Print out mNetworkController state if the test fails. |
| 115 | StringWriter sw = new StringWriter(); |
| 116 | PrintWriter pw = new PrintWriter(sw); |
| 117 | mNetworkController.dump(null, pw, null); |
| 118 | pw.flush(); |
| 119 | Log.d(TAG, sw.toString()); |
| 120 | } |
| 121 | }; |
| 122 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 123 | @Before |
| 124 | public void setUp() throws Exception { |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 125 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
Jason Monk | a3c3d85 | 2017-08-14 11:04:42 -0400 | [diff] [blame] | 126 | Settings.Global.putInt(mContext.getContentResolver(), Global.AIRPLANE_MODE_ON, 0); |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 127 | TestableResources res = mContext.getOrCreateTestableResources(); |
| 128 | res.addOverride(R.string.cell_data_off_content_description, NO_DATA_STRING); |
| 129 | res.addOverride(R.string.not_default_data_content_description, NOT_DEFAULT_DATA_STRING); |
| 130 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 131 | mMockWm = mock(WifiManager.class); |
| 132 | mMockTm = mock(TelephonyManager.class); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 133 | mMockSm = mock(SubscriptionManager.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 134 | mMockCm = mock(ConnectivityManager.class); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 135 | mMockBd = mock(BroadcastDispatcher.class); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 136 | mMockSubDefaults = mock(SubscriptionDefaults.class); |
Jason Monk | d8e4bf4 | 2014-12-12 10:23:12 -0500 | [diff] [blame] | 137 | mNetCapabilities = new NetworkCapabilities(); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 138 | when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true); |
Jason Monk | d8e4bf4 | 2014-12-12 10:23:12 -0500 | [diff] [blame] | 139 | when(mMockCm.getDefaultNetworkCapabilitiesForUser(0)).thenReturn( |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 140 | new NetworkCapabilities[] { mNetCapabilities }); |
chen xu | 84a9087 | 2019-03-28 15:44:12 -0700 | [diff] [blame] | 141 | when(mMockTm.createForSubscriptionId(anyInt())).thenReturn(mMockTm); |
David Su | 5c52237 | 2019-11-19 16:20:20 -0800 | [diff] [blame] | 142 | doAnswer(invocation -> { |
| 143 | int rssi = invocation.getArgument(0); |
| 144 | if (rssi < -88) return 0; |
| 145 | if (rssi < -77) return 1; |
| 146 | if (rssi < -66) return 2; |
| 147 | if (rssi < -55) return 3; |
| 148 | return 4; |
| 149 | }).when(mMockWm).calculateSignalLevel(anyInt()); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 150 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 151 | mSignalStrength = mock(SignalStrength.class); |
| 152 | mServiceState = mock(ServiceState.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 153 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 154 | mConfig = new Config(); |
| 155 | mConfig.hspaDataDistinguishable = true; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 156 | mCallbackHandler = mock(CallbackHandler.class); |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 157 | |
| 158 | mMockProvisionController = mock(DeviceProvisionedController.class); |
| 159 | when(mMockProvisionController.isUserSetup(anyInt())).thenReturn(true); |
| 160 | doAnswer(invocation -> { |
| 161 | mUserCallback = (DeviceProvisionedListener) invocation.getArguments()[0]; |
| 162 | mUserCallback.onUserSetupChanged(); |
| 163 | mUserCallback.onDeviceProvisionedChanged(); |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 164 | TestableLooper.get(this).processAllMessages(); |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 165 | return null; |
| 166 | }).when(mMockProvisionController).addCallback(any()); |
| 167 | |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 168 | mNetworkController = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 169 | mConfig, TestableLooper.get(this).getLooper(), mCallbackHandler, |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 170 | mock(AccessPointControllerImpl.class), mock(DataUsageController.class), |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 171 | mMockSubDefaults, mMockProvisionController, mMockBd); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 172 | setupNetworkController(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 173 | |
| 174 | // Trigger blank callbacks to always get the current state (some tests don't trigger |
| 175 | // changes from default state). |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 176 | mNetworkController.addCallback(mock(SignalCallback.class)); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 177 | mNetworkController.addEmergencyListener(null); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | protected void setupNetworkController() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 181 | // For now just pretend to be the data sim, so we can test that too. |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 182 | mSubId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; |
Malcolm Chen | 04be9d1 | 2019-06-11 19:48:38 -0700 | [diff] [blame] | 183 | when(mMockTm.isDataCapable()).thenReturn(true); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 184 | setDefaultSubId(mSubId); |
| 185 | setSubscriptions(mSubId); |
Jason Monk | 18985e0 | 2015-01-07 12:07:06 -0500 | [diff] [blame] | 186 | mMobileSignalController = mNetworkController.mMobileSignalControllers.get(mSubId); |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 187 | mPhoneStateListener = mMobileSignalController.mPhoneStateListener; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 188 | |
| 189 | ArgumentCaptor<ConnectivityManager.NetworkCallback> callbackArg = |
| 190 | ArgumentCaptor.forClass(ConnectivityManager.NetworkCallback.class); |
| 191 | Mockito.verify(mMockCm, atLeastOnce()) |
| 192 | .registerDefaultNetworkCallback(callbackArg.capture(), isA(Handler.class)); |
| 193 | mNetworkCallback = callbackArg.getValue(); |
| 194 | assertNotNull(mNetworkCallback); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 195 | } |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 196 | |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 197 | protected void setDefaultSubId(int subId) { |
| 198 | when(mMockSubDefaults.getDefaultDataSubId()).thenReturn(subId); |
| 199 | when(mMockSubDefaults.getDefaultVoiceSubId()).thenReturn(subId); |
Fabian Kozynski | ccde55d | 2019-06-26 10:06:09 -0400 | [diff] [blame] | 200 | when(mMockSubDefaults.getActiveDataSubId()).thenReturn(subId); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | protected void setSubscriptions(int... subIds) { |
| 204 | List<SubscriptionInfo> subs = new ArrayList<SubscriptionInfo>(); |
| 205 | for (int subId : subIds) { |
| 206 | SubscriptionInfo subscription = mock(SubscriptionInfo.class); |
| 207 | when(subscription.getSubscriptionId()).thenReturn(subId); |
| 208 | subs.add(subscription); |
| 209 | } |
| 210 | when(mMockSm.getActiveSubscriptionInfoList()).thenReturn(subs); |
Malcolm Chen | d2d400cb | 2018-12-21 17:11:10 -0800 | [diff] [blame] | 211 | when(mMockSm.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(subs); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 212 | mNetworkController.doUpdateMobileControllers(); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 215 | protected NetworkControllerImpl setUpNoMobileData() { |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 216 | when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false); |
| 217 | NetworkControllerImpl networkControllerNoMobile |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 218 | = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, |
Jason Monk | 2515f47 | 2017-09-19 09:52:43 -0400 | [diff] [blame] | 219 | mConfig, TestableLooper.get(this).getLooper(), mCallbackHandler, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 220 | mock(AccessPointControllerImpl.class), |
| 221 | mock(DataUsageController.class), mMockSubDefaults, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 222 | mock(DeviceProvisionedController.class), mMockBd); |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 223 | |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 224 | setupNetworkController(); |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 225 | |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 226 | return networkControllerNoMobile; |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 227 | |
| 228 | } |
| 229 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 230 | // 2 Bars 3G GSM. |
| 231 | public void setupDefaultSignal() { |
| 232 | setIsGsm(true); |
| 233 | setVoiceRegState(ServiceState.STATE_IN_SERVICE); |
| 234 | setGsmRoaming(false); |
| 235 | setLevel(DEFAULT_LEVEL); |
| 236 | updateDataConnectionState(TelephonyManager.DATA_CONNECTED, |
| 237 | TelephonyManager.NETWORK_TYPE_UMTS); |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 238 | setConnectivityViaBroadcast( |
| 239 | NetworkCapabilities.TRANSPORT_CELLULAR, true, true); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 240 | } |
| 241 | |
Pengquan Meng | a62d32b | 2018-12-06 17:38:38 -0800 | [diff] [blame] | 242 | public void setupDefaultNr5GIconConfiguration() { |
| 243 | NetworkControllerImpl.Config.add5GIconMapping("connected_mmwave:5g_plus", mConfig); |
| 244 | NetworkControllerImpl.Config.add5GIconMapping("connected:5g", mConfig); |
| 245 | } |
| 246 | |
SongFerngWang | 52dada7 | 2019-08-14 16:59:29 +0800 | [diff] [blame] | 247 | public void setupNr5GIconConfigurationForNotRestrictedRrcCon() { |
| 248 | NetworkControllerImpl.Config.add5GIconMapping("connected_mmwave:5g_plus", mConfig); |
| 249 | NetworkControllerImpl.Config.add5GIconMapping("connected:5g_plus", mConfig); |
| 250 | NetworkControllerImpl.Config.add5GIconMapping("not_restricted_rrc_con:5g", mConfig); |
| 251 | } |
| 252 | |
| 253 | public void setupNr5GIconConfigurationForNotRestrictedRrcIdle() { |
| 254 | NetworkControllerImpl.Config.add5GIconMapping("connected_mmwave:5g_plus", mConfig); |
| 255 | NetworkControllerImpl.Config.add5GIconMapping("connected:5g_plus", mConfig); |
| 256 | NetworkControllerImpl.Config.add5GIconMapping("not_restricted_rrc_idle:5g", mConfig); |
| 257 | } |
| 258 | |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 259 | public void setupDefaultNr5GIconDisplayGracePeriodTime_enableThirtySeconds() { |
| 260 | final int enableDisplayGraceTimeSec = 30; |
| 261 | NetworkControllerImpl.Config.setDisplayGraceTime(enableDisplayGraceTimeSec, mConfig); |
| 262 | } |
| 263 | |
| 264 | public void setupDefaultNr5GIconDisplayGracePeriodTime_disabled() { |
| 265 | final int disableDisplayGraceTimeSec = 0; |
| 266 | NetworkControllerImpl.Config.setDisplayGraceTime(disableDisplayGraceTimeSec, mConfig); |
| 267 | } |
| 268 | |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 269 | public void setConnectivityViaBroadcast( |
| 270 | int networkType, boolean validated, boolean isConnected) { |
| 271 | setConnectivityCommon(networkType, validated, isConnected); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 272 | Intent i = new Intent(ConnectivityManager.INET_CONDITION_ACTION); |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 273 | mNetworkController.onReceive(mContext, i); |
| 274 | } |
| 275 | |
| 276 | public void setConnectivityViaCallback( |
| 277 | int networkType, boolean validated, boolean isConnected){ |
| 278 | setConnectivityCommon(networkType, validated, isConnected); |
| 279 | mNetworkCallback.onCapabilitiesChanged( |
| 280 | mock(Network.class), new NetworkCapabilities(mNetCapabilities)); |
| 281 | } |
| 282 | |
| 283 | private void setConnectivityCommon( |
| 284 | int networkType, boolean validated, boolean isConnected){ |
Jason Monk | d8e4bf4 | 2014-12-12 10:23:12 -0500 | [diff] [blame] | 285 | // TODO: Separate out into several NetworkCapabilities. |
| 286 | if (isConnected) { |
| 287 | mNetCapabilities.addTransportType(networkType); |
| 288 | } else { |
| 289 | mNetCapabilities.removeTransportType(networkType); |
| 290 | } |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 291 | if (validated) { |
Jason Monk | d8e4bf4 | 2014-12-12 10:23:12 -0500 | [diff] [blame] | 292 | mNetCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); |
| 293 | } else { |
| 294 | mNetCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); |
| 295 | } |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | public void setGsmRoaming(boolean isRoaming) { |
| 299 | when(mServiceState.getRoaming()).thenReturn(isRoaming); |
| 300 | updateServiceState(); |
| 301 | } |
| 302 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 303 | public void setCdmaRoaming(boolean isRoaming) { |
| 304 | when(mServiceState.getCdmaEriIconIndex()).thenReturn(isRoaming ? |
| 305 | EriInfo.ROAMING_INDICATOR_ON : EriInfo.ROAMING_INDICATOR_OFF); |
| 306 | when(mServiceState.getCdmaEriIconMode()).thenReturn(isRoaming ? |
| 307 | EriInfo.ROAMING_ICON_MODE_NORMAL : -1); |
| 308 | updateServiceState(); |
| 309 | } |
| 310 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 311 | public void setVoiceRegState(int voiceRegState) { |
| 312 | when(mServiceState.getVoiceRegState()).thenReturn(voiceRegState); |
| 313 | updateServiceState(); |
| 314 | } |
| 315 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 316 | public void setDataRegState(int dataRegState) { |
| 317 | when(mServiceState.getDataRegState()).thenReturn(dataRegState); |
| 318 | updateServiceState(); |
| 319 | } |
| 320 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 321 | public void setIsEmergencyOnly(boolean isEmergency) { |
| 322 | when(mServiceState.isEmergencyOnly()).thenReturn(isEmergency); |
| 323 | updateServiceState(); |
| 324 | } |
| 325 | |
| 326 | public void setCdmaLevel(int level) { |
| 327 | when(mSignalStrength.getCdmaLevel()).thenReturn(level); |
| 328 | updateSignalStrength(); |
| 329 | } |
| 330 | |
| 331 | public void setLevel(int level) { |
| 332 | when(mSignalStrength.getLevel()).thenReturn(level); |
| 333 | updateSignalStrength(); |
| 334 | } |
| 335 | |
| 336 | public void setIsGsm(boolean gsm) { |
| 337 | when(mSignalStrength.isGsm()).thenReturn(gsm); |
| 338 | updateSignalStrength(); |
| 339 | } |
| 340 | |
| 341 | public void setCdmaEri(int index, int mode) { |
| 342 | // TODO: Figure this out. |
| 343 | } |
| 344 | |
| 345 | private void updateSignalStrength() { |
| 346 | Log.d(TAG, "Sending Signal Strength: " + mSignalStrength); |
| 347 | mPhoneStateListener.onSignalStrengthsChanged(mSignalStrength); |
| 348 | } |
| 349 | |
Jason Monk | 67b2c16 | 2016-01-12 10:24:09 -0500 | [diff] [blame] | 350 | protected void updateServiceState() { |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 351 | Log.d(TAG, "Sending Service State: " + mServiceState); |
| 352 | mPhoneStateListener.onServiceStateChanged(mServiceState); |
| 353 | } |
| 354 | |
| 355 | public void updateCallState(int state) { |
| 356 | // Inputs not currently used in NetworkControllerImpl. |
| 357 | mPhoneStateListener.onCallStateChanged(state, "0123456789"); |
| 358 | } |
| 359 | |
| 360 | public void updateDataConnectionState(int dataState, int dataNetType) { |
Jason Monk | 67b2c16 | 2016-01-12 10:24:09 -0500 | [diff] [blame] | 361 | when(mServiceState.getDataNetworkType()).thenReturn(dataNetType); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 362 | mPhoneStateListener.onDataConnectionStateChanged(dataState, dataNetType); |
| 363 | } |
| 364 | |
| 365 | public void updateDataActivity(int dataActivity) { |
| 366 | mPhoneStateListener.onDataActivity(dataActivity); |
| 367 | } |
| 368 | |
Andrew Flynn | 5e75147 | 2015-05-01 11:50:07 -0400 | [diff] [blame] | 369 | public void setCarrierNetworkChange(boolean enable) { |
| 370 | Log.d(TAG, "setCarrierNetworkChange(" + enable + ")"); |
| 371 | mPhoneStateListener.onCarrierNetworkChange(enable); |
| 372 | } |
| 373 | |
Jason Monk | 21d05a0 | 2014-12-02 12:46:58 -0500 | [diff] [blame] | 374 | protected void verifyHasNoSims(boolean hasNoSimsVisible) { |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 375 | Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setNoSims( |
| 376 | eq(hasNoSimsVisible), eq(false)); |
Jason Monk | 21d05a0 | 2014-12-02 12:46:58 -0500 | [diff] [blame] | 377 | } |
| 378 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 379 | protected void verifyLastQsMobileDataIndicators(boolean visible, int icon, int typeIcon, |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 380 | boolean dataIn, boolean dataOut) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 381 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 382 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 383 | ArgumentCaptor<Boolean> dataInArg = ArgumentCaptor.forClass(Boolean.class); |
| 384 | ArgumentCaptor<Boolean> dataOutArg = ArgumentCaptor.forClass(Boolean.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 385 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 386 | Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setMobileDataIndicators( |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 387 | any(), |
| 388 | iconArg.capture(), |
| 389 | anyInt(), |
| 390 | typeIconArg.capture(), dataInArg.capture(), dataOutArg.capture(), |
| 391 | anyString(), anyString(), anyBoolean(), anyInt(), anyBoolean()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 392 | IconState iconState = iconArg.getValue(); |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 393 | int state = SignalDrawable.getState(icon, SignalStrength.NUM_SIGNAL_STRENGTH_BINS, |
| 394 | false); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 395 | assertEquals("Visibility in, quick settings", visible, iconState.visible); |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 396 | assertEquals("Signal icon in, quick settings", state, iconState.icon); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 397 | assertEquals("Data icon in, quick settings", typeIcon, (int) typeIconArg.getValue()); |
| 398 | assertEquals("Data direction in, in quick settings", dataIn, |
| 399 | (boolean) dataInArg.getValue()); |
| 400 | assertEquals("Data direction out, in quick settings", dataOut, |
| 401 | (boolean) dataOutArg.getValue()); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon) { |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 405 | verifyLastMobileDataIndicators(visible, icon, typeIcon, false); |
| 406 | } |
| 407 | |
| 408 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon, |
| 409 | boolean roaming) { |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 410 | verifyLastMobileDataIndicators(visible, icon, typeIcon, roaming, true); |
| 411 | } |
| 412 | |
| 413 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 414 | boolean roaming, boolean inet) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 415 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 416 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 417 | |
| 418 | // TODO: Verify all fields. |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 419 | Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setMobileDataIndicators( |
| 420 | iconArg.capture(), |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 421 | any(), |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 422 | typeIconArg.capture(), |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 423 | anyInt(), anyBoolean(), anyBoolean(), anyString(), anyString(), anyBoolean(), |
| 424 | anyInt(), eq(roaming)); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 425 | IconState iconState = iconArg.getValue(); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 426 | |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 427 | int state = icon == -1 ? 0 |
| 428 | : SignalDrawable.getState(icon, SignalStrength.NUM_SIGNAL_STRENGTH_BINS, !inet); |
| 429 | assertEquals("Signal icon in status bar", state, iconState.icon); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 430 | assertEquals("Data icon in status bar", typeIcon, (int) typeIconArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 431 | assertEquals("Visibility in status bar", visible, iconState.visible); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 432 | } |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame] | 433 | |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 434 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 435 | boolean qsVisible, int qsIcon, int qsTypeIcon, boolean dataIn, boolean dataOut) { |
Amin Shaikh | af4c193 | 2018-03-08 10:29:56 -0500 | [diff] [blame] | 436 | verifyLastMobileDataIndicators( |
| 437 | visible, icon, typeIcon, qsVisible, qsIcon, qsTypeIcon, dataIn, dataOut, false); |
| 438 | } |
| 439 | |
| 440 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon, |
| 441 | boolean qsVisible, int qsIcon, int qsTypeIcon, boolean dataIn, boolean dataOut, |
| 442 | boolean cutOut) { |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 443 | verifyLastMobileDataIndicators( |
| 444 | visible, icon, typeIcon, qsVisible, qsIcon, qsTypeIcon, dataIn, dataOut, cutOut, |
| 445 | null); |
| 446 | } |
| 447 | |
| 448 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon, |
| 449 | boolean qsVisible, int qsIcon, int qsTypeIcon, boolean dataIn, boolean dataOut, |
| 450 | boolean cutOut, String typeContentDescription) { |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 451 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
| 452 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 453 | ArgumentCaptor<IconState> qsIconArg = ArgumentCaptor.forClass(IconState.class); |
| 454 | ArgumentCaptor<Integer> qsTypeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 455 | ArgumentCaptor<Boolean> dataInArg = ArgumentCaptor.forClass(Boolean.class); |
| 456 | ArgumentCaptor<Boolean> dataOutArg = ArgumentCaptor.forClass(Boolean.class); |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 457 | ArgumentCaptor<String> typeContentDescriptionArg = ArgumentCaptor.forClass(String.class); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 458 | |
| 459 | Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setMobileDataIndicators( |
| 460 | iconArg.capture(), |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 461 | qsIconArg.capture(), |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 462 | typeIconArg.capture(), |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 463 | qsTypeIconArg.capture(), |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 464 | dataInArg.capture(), |
| 465 | dataOutArg.capture(), |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 466 | typeContentDescriptionArg.capture(), |
| 467 | anyString(), anyBoolean(), anyInt(), anyBoolean()); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 468 | |
| 469 | IconState iconState = iconArg.getValue(); |
| 470 | |
andychou | b935e68 | 2019-04-11 23:40:49 +0800 | [diff] [blame] | 471 | int numSignalStrengthBins = SignalStrength.NUM_SIGNAL_STRENGTH_BINS; |
| 472 | if (mMobileSignalController.mInflateSignalStrengths) { |
| 473 | numSignalStrengthBins++; |
| 474 | icon++; |
| 475 | } |
| 476 | int state = SignalDrawable.getState(icon, numSignalStrengthBins, cutOut); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 477 | assertEquals("Data icon in status bar", typeIcon, (int) typeIconArg.getValue()); |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 478 | assertEquals("Signal icon in status bar", state, iconState.icon); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 479 | assertEquals("Visibility in status bar", visible, iconState.visible); |
| 480 | |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 481 | iconState = qsIconArg.getValue(); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 482 | assertEquals("Visibility in quick settings", qsVisible, iconState.visible); |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 483 | assertEquals("Signal icon in quick settings", state, iconState.icon); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 484 | assertEquals("Data icon in quick settings", qsTypeIcon, (int) qsTypeIconArg.getValue()); |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 485 | assertEquals("Data direction in in quick settings", dataIn, |
| 486 | (boolean) dataInArg.getValue()); |
| 487 | assertEquals("Data direction out in quick settings", dataOut, |
| 488 | (boolean) dataOutArg.getValue()); |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 489 | if (typeContentDescription != null) { // Only check if it was provided |
| 490 | assertEquals("Type content description", typeContentDescription, |
| 491 | typeContentDescriptionArg.getValue()); |
| 492 | } |
Jason Monk | 26ad85a | 2016-12-08 14:45:42 -0500 | [diff] [blame] | 493 | } |
| 494 | |
Fabian Kozynski | a909199 | 2019-03-25 11:08:32 -0400 | [diff] [blame] | 495 | protected void assertNetworkNameEquals(String expected) { |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 496 | assertEquals("Network name", expected, mMobileSignalController.getState().networkName); |
Fabian Kozynski | a909199 | 2019-03-25 11:08:32 -0400 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | protected void assertDataNetworkNameEquals(String expected) { |
| 500 | assertEquals("Data network name", expected, mNetworkController.getMobileDataNetworkName()); |
| 501 | } |
| 502 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 503 | } |