1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.telephony.dataconnection;
18 
19 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
20 import static com.android.internal.telephony.dataconnection.ApnSettingTest.createApnSetting;
21 
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.anyInt;
29 import static org.mockito.Matchers.anyLong;
30 import static org.mockito.Matchers.anyString;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.atLeastOnce;
33 import static org.mockito.Mockito.clearInvocations;
34 import static org.mockito.Mockito.doAnswer;
35 import static org.mockito.Mockito.doReturn;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.timeout;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 
41 import android.app.AlarmManager;
42 import android.app.PendingIntent;
43 import android.content.ContentResolver;
44 import android.content.ContentValues;
45 import android.content.Context;
46 import android.content.Intent;
47 import android.content.IntentFilter;
48 import android.content.pm.ServiceInfo;
49 import android.database.Cursor;
50 import android.database.MatrixCursor;
51 import android.hardware.radio.V1_0.SetupDataCallResult;
52 import android.net.LinkProperties;
53 import android.net.NetworkAgent;
54 import android.net.NetworkCapabilities;
55 import android.net.NetworkPolicyManager;
56 import android.net.NetworkRequest;
57 import android.net.Uri;
58 import android.os.AsyncResult;
59 import android.os.Handler;
60 import android.os.HandlerThread;
61 import android.os.IBinder;
62 import android.os.Message;
63 import android.os.PersistableBundle;
64 import android.provider.Settings;
65 import android.provider.Telephony;
66 import android.telephony.AccessNetworkConstants;
67 import android.telephony.AccessNetworkConstants.AccessNetworkType;
68 import android.telephony.CarrierConfigManager;
69 import android.telephony.NetworkRegistrationInfo;
70 import android.telephony.ServiceState;
71 import android.telephony.SignalStrength;
72 import android.telephony.SubscriptionInfo;
73 import android.telephony.SubscriptionManager;
74 import android.telephony.SubscriptionPlan;
75 import android.telephony.TelephonyDisplayInfo;
76 import android.telephony.TelephonyManager;
77 import android.telephony.data.ApnSetting;
78 import android.telephony.data.DataProfile;
79 import android.telephony.data.DataService;
80 import android.test.mock.MockContentProvider;
81 import android.test.mock.MockContentResolver;
82 import android.test.suitebuilder.annotation.MediumTest;
83 import android.test.suitebuilder.annotation.SmallTest;
84 import android.text.TextUtils;
85 import android.util.Pair;
86 
87 import androidx.test.filters.FlakyTest;
88 
89 import com.android.internal.R;
90 import com.android.internal.telephony.DctConstants;
91 import com.android.internal.telephony.ISub;
92 import com.android.internal.telephony.PhoneConstants;
93 import com.android.internal.telephony.TelephonyTest;
94 import com.android.server.pm.PackageManagerService;
95 
96 import org.junit.After;
97 import org.junit.Before;
98 import org.junit.Ignore;
99 import org.junit.Test;
100 import org.mockito.ArgumentCaptor;
101 import org.mockito.Mock;
102 import org.mockito.invocation.InvocationOnMock;
103 import org.mockito.stubbing.Answer;
104 
105 import java.lang.reflect.Field;
106 import java.lang.reflect.Method;
107 import java.time.Period;
108 import java.time.ZonedDateTime;
109 import java.util.ArrayList;
110 import java.util.Arrays;
111 import java.util.HashMap;
112 import java.util.List;
113 import java.util.Objects;
114 import java.util.Optional;
115 import java.util.concurrent.atomic.AtomicInteger;
116 
117 public class DcTrackerTest extends TelephonyTest {
118     public static final String FAKE_APN1 = "FAKE APN 1";
119     public static final String FAKE_APN2 = "FAKE APN 2";
120     public static final String FAKE_APN3 = "FAKE APN 3";
121     public static final String FAKE_APN4 = "FAKE APN 4";
122     public static final String FAKE_APN5 = "FAKE APN 5";
123     public static final String FAKE_APN6 = "FAKE APN 6";
124     public static final String FAKE_IFNAME = "FAKE IFNAME";
125     public static final String FAKE_PCSCF_ADDRESS = "22.33.44.55";
126     public static final String FAKE_GATEWAY = "11.22.33.44";
127     public static final String FAKE_DNS = "55.66.77.88";
128     public static final String FAKE_ADDRESS = "99.88.77.66";
129     private static final int NETWORK_TYPE_LTE_BITMASK =
130             1 << (TelephonyManager.NETWORK_TYPE_LTE - 1);
131     private static final int NETWORK_TYPE_EHRPD_BITMASK =
132             1 << (TelephonyManager.NETWORK_TYPE_EHRPD - 1);
133     private static final Uri PREFERAPN_URI = Uri.parse(
134             Telephony.Carriers.CONTENT_URI + "/preferapn");
135     private static final int DATA_ENABLED_CHANGED = 0;
136     private static final String FAKE_PLMN = "44010";
137     private static final long TEST_TIMEOUT = 1000;
138 
139     @Mock
140     ISub mIsub;
141     @Mock
142     IBinder mBinder;
143     @Mock
144     SubscriptionInfo mSubscriptionInfo;
145     @Mock
146     ApnContext mApnContext;
147     @Mock
148     DataConnection mDataConnection;
149     @Mock
150     PackageManagerService mMockPackageManagerInternal;
151     @Mock
152     Handler mHandler;
153     @Mock
154     NetworkPolicyManager mNetworkPolicyManager;
155 
156     private DcTracker mDct;
157     private DcTrackerTestHandler mDcTrackerTestHandler;
158 
159     private AlarmManager mAlarmManager;
160 
161     private PersistableBundle mBundle;
162 
163     private SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangedListener;
164 
165     private final ApnSettingContentProvider mApnSettingContentProvider =
166             new ApnSettingContentProvider();
167 
168     private Message mMessage;
169 
170     private CellularDataService mCellularDataService;
171 
addDataService()172     private void addDataService() {
173         mCellularDataService = new CellularDataService();
174         ServiceInfo serviceInfo = new ServiceInfo();
175         serviceInfo.packageName = "com.android.phone";
176         serviceInfo.permission = "android.permission.BIND_TELEPHONY_DATA_SERVICE";
177         IntentFilter filter = new IntentFilter();
178         mContextFixture.addService(
179                 DataService.SERVICE_INTERFACE,
180                 null,
181                 "com.android.phone",
182                 mCellularDataService.mBinder,
183                 serviceInfo,
184                 filter);
185     }
186 
187     private class DcTrackerTestHandler extends HandlerThread {
188 
DcTrackerTestHandler(String name)189         private DcTrackerTestHandler(String name) {
190             super(name);
191         }
192 
193         @Override
onLooperPrepared()194         public void onLooperPrepared() {
195             mDct = new DcTracker(mPhone, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
196             setReady(true);
197         }
198     }
199 
200     private class ApnSettingContentProvider extends MockContentProvider {
201         private int mPreferredApnSet = 0;
202 
203         @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)204         public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
205                             String sortOrder) {
206             logd("ApnSettingContentProvider: query");
207             logd("   uri = " + uri);
208             logd("   projection = " + Arrays.toString(projection));
209             logd("   selection = " + selection);
210             logd("   selectionArgs = " + Arrays.toString(selectionArgs));
211             logd("   sortOrder = " + sortOrder);
212 
213             if (uri.compareTo(Telephony.Carriers.CONTENT_URI) == 0
214                     || uri.toString().startsWith(Uri.withAppendedPath(
215                             Telephony.Carriers.CONTENT_URI, "filtered").toString())
216                     || uri.toString().startsWith(Uri.withAppendedPath(
217                             Telephony.Carriers.SIM_APN_URI, "filtered").toString())) {
218                 if (projection == null) {
219 
220                     logd("Query '" + FAKE_PLMN + "' APN settings");
221                     MatrixCursor mc = new MatrixCursor(
222                             new String[]{Telephony.Carriers._ID, Telephony.Carriers.NUMERIC,
223                                     Telephony.Carriers.NAME, Telephony.Carriers.APN,
224                                     Telephony.Carriers.PROXY, Telephony.Carriers.PORT,
225                                     Telephony.Carriers.MMSC, Telephony.Carriers.MMSPROXY,
226                                     Telephony.Carriers.MMSPORT, Telephony.Carriers.USER,
227                                     Telephony.Carriers.PASSWORD, Telephony.Carriers.AUTH_TYPE,
228                                     Telephony.Carriers.TYPE,
229                                     Telephony.Carriers.PROTOCOL,
230                                     Telephony.Carriers.ROAMING_PROTOCOL,
231                                     Telephony.Carriers.CARRIER_ENABLED, Telephony.Carriers.BEARER,
232                                     Telephony.Carriers.BEARER_BITMASK,
233                                     Telephony.Carriers.PROFILE_ID,
234                                     Telephony.Carriers.MODEM_PERSIST,
235                                     Telephony.Carriers.MAX_CONNECTIONS,
236                                     Telephony.Carriers.WAIT_TIME_RETRY,
237                                     Telephony.Carriers.TIME_LIMIT_FOR_MAX_CONNECTIONS,
238                                     Telephony.Carriers.MTU,
239                                     Telephony.Carriers.MVNO_TYPE,
240                                     Telephony.Carriers.MVNO_MATCH_DATA,
241                                     Telephony.Carriers.NETWORK_TYPE_BITMASK,
242                                     Telephony.Carriers.APN_SET_ID,
243                                     Telephony.Carriers.CARRIER_ID,
244                                     Telephony.Carriers.SKIP_464XLAT});
245 
246                     mc.addRow(new Object[]{
247                             2163,                   // id
248                             FAKE_PLMN,              // numeric
249                             "sp-mode",              // name
250                             FAKE_APN1,              // apn
251                             "",                     // proxy
252                             "",                     // port
253                             "",                     // mmsc
254                             "",                     // mmsproxy
255                             "",                     // mmsport
256                             "",                     // user
257                             "",                     // password
258                             -1,                     // authtype
259                             "default,supl",         // types
260                             "IP",                   // protocol
261                             "IP",                   // roaming_protocol
262                             1,                      // carrier_enabled
263                             ServiceState.RIL_RADIO_TECHNOLOGY_LTE, // bearer
264                             0,                      // bearer_bitmask
265                             0,                      // profile_id
266                             1,                      // modem_cognitive
267                             0,                      // max_conns
268                             0,                      // wait_time
269                             0,                      // max_conns_time
270                             0,                      // mtu
271                             "",                     // mvno_type
272                             "",                     // mnvo_match_data
273                             NETWORK_TYPE_LTE_BITMASK, // network_type_bitmask
274                             0,                      // apn_set_id
275                             -1,                     // carrier_id
276                             -1                      // skip_464xlat
277                     });
278 
279                     mc.addRow(new Object[]{
280                             2164,                   // id
281                             FAKE_PLMN,              // numeric
282                             "mopera U",             // name
283                             FAKE_APN2,              // apn
284                             "",                     // proxy
285                             "",                     // port
286                             "",                     // mmsc
287                             "",                     // mmsproxy
288                             "",                     // mmsport
289                             "",                     // user
290                             "",                     // password
291                             -1,                     // authtype
292                             "default,supl",         // types
293                             "IP",                   // protocol
294                             "IP",                   // roaming_protocol
295                             1,                      // carrier_enabled
296                             ServiceState.RIL_RADIO_TECHNOLOGY_LTE, // bearer,
297                             0,                      // bearer_bitmask
298                             0,                      // profile_id
299                             1,                      // modem_cognitive
300                             0,                      // max_conns
301                             0,                      // wait_time
302                             0,                      // max_conns_time
303                             0,                      // mtu
304                             "",                     // mvno_type
305                             "",                     // mnvo_match_data
306                             NETWORK_TYPE_LTE_BITMASK, // network_type_bitmask
307                             0,                      // apn_set_id
308                             -1,                     // carrier_id
309                             -1                      // skip_464xlat
310                     });
311 
312                     mc.addRow(new Object[]{
313                             2165,                   // id
314                             FAKE_PLMN,              // numeric
315                             "b-mobile for Nexus",   // name
316                             FAKE_APN3,              // apn
317                             "",                     // proxy
318                             "",                     // port
319                             "",                     // mmsc
320                             "",                     // mmsproxy
321                             "",                     // mmsport
322                             "",                     // user
323                             "",                     // password
324                             -1,                     // authtype
325                             "ims",                  // types
326                             "IP",                   // protocol
327                             "IP",                   // roaming_protocol
328                             1,                      // carrier_enabled
329                             0,                      // bearer
330                             0,                      // bearer_bitmask
331                             0,                      // profile_id
332                             1,                      // modem_cognitive
333                             0,                      // max_conns
334                             0,                      // wait_time
335                             0,                      // max_conns_time
336                             0,                      // mtu
337                             "",                     // mvno_type
338                             "",                     // mnvo_match_data
339                             0,                      // network_type_bitmask
340                             0,                      // apn_set_id
341                             -1,                     // carrier_id
342                             -1                      // skip_464xlat
343                     });
344 
345                     mc.addRow(new Object[]{
346                             2166,                   // id
347                             FAKE_PLMN,              // numeric
348                             "sp-mode ehrpd",        // name
349                             FAKE_APN4,              // apn
350                             "",                     // proxy
351                             "",                     // port
352                             "",                     // mmsc
353                             "",                     // mmsproxy
354                             "",                     // mmsport
355                             "",                     // user
356                             "",                     // password
357                             -1,                     // authtype
358                             "default,supl",         // types
359                             "IP",                   // protocol
360                             "IP",                   // roaming_protocol
361                             1,                      // carrier_enabled
362                             ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD, // bearer
363                             0,                      // bearer_bitmask
364                             0,                      // profile_id
365                             1,                      // modem_cognitive
366                             0,                      // max_conns
367                             0,                      // wait_time
368                             0,                      // max_conns_time
369                             0,                      // mtu
370                             "",                     // mvno_type
371                             "",                     // mnvo_match_data
372                             NETWORK_TYPE_EHRPD_BITMASK, // network_type_bitmask
373                             0,                      // apn_set_id
374                             -1,                     // carrier_id
375                             -1                      // skip_464xlat
376                     });
377 
378                     mc.addRow(new Object[]{
379                             2167,                   // id
380                             FAKE_PLMN,              // numeric
381                             "b-mobile for Nexus",   // name
382                             FAKE_APN5,              // apn
383                             "",                     // proxy
384                             "",                     // port
385                             "",                     // mmsc
386                             "",                     // mmsproxy
387                             "",                     // mmsport
388                             "",                     // user
389                             "",                     // password
390                             -1,                     // authtype
391                             "dun",                  // types
392                             "IP",                   // protocol
393                             "IP",                   // roaming_protocol
394                             1,                      // carrier_enabled
395                             0,                      // bearer
396                             0,                      // bearer_bitmask
397                             0,                      // profile_id
398                             1,                      // modem_cognitive
399                             0,                      // max_conns
400                             0,                      // wait_time
401                             0,                      // max_conns_time
402                             0,                      // mtu
403                             "",                     // mvno_type
404                             "",                     // mnvo_match_data
405                             0,                      // network_type_bitmask
406                             0,                      // apn_set_id
407                             -1,                     // carrier_id
408                             -1                      // skip_464xlat
409                     });
410 
411                     mc.addRow(new Object[]{
412                             2168,                   // id
413                             FAKE_PLMN,              // numeric
414                             "sp-mode",              // name
415                             FAKE_APN6,              // apn
416                             "",                     // proxy
417                             "",                     // port
418                             "",                     // mmsc
419                             "",                     // mmsproxy
420                             "",                     // mmsport
421                             "",                     // user
422                             "",                     // password
423                             -1,                     // authtype
424                             "mms,xcap",             // types
425                             "IP",                   // protocol
426                             "IP",                   // roaming_protocol
427                             1,                      // carrier_enabled
428                             ServiceState.RIL_RADIO_TECHNOLOGY_LTE, // bearer
429                             0,                      // bearer_bitmask
430                             0,                      // profile_id
431                             1,                      // modem_cognitive
432                             0,                      // max_conns
433                             0,                      // wait_time
434                             0,                      // max_conns_time
435                             0,                      // mtu
436                             "",                     // mvno_type
437                             "",                     // mnvo_match_data
438                             NETWORK_TYPE_LTE_BITMASK, // network_type_bitmask
439                             0,                      // apn_set_id
440                             -1,                     // carrier_id
441                             -1                      // skip_464xlat
442                     });
443 
444                     return mc;
445                 }
446             } else if (isPathPrefixMatch(uri,
447                     Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "preferapnset"))) {
448                 MatrixCursor mc = new MatrixCursor(
449                         new String[]{Telephony.Carriers.APN_SET_ID});
450                 // apn_set_id is the only field used with this URL
451                 mc.addRow(new Object[]{ mPreferredApnSet });
452                 mc.addRow(new Object[]{ 0 });
453                 return mc;
454             }
455 
456             return null;
457         }
458 
459         @Override
update(Uri url, ContentValues values, String where, String[] whereArgs)460         public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
461             mPreferredApnSet = values.getAsInteger(Telephony.Carriers.APN_SET_ID);
462             return 1;
463         }
464     }
465 
466     @Before
setUp()467     public void setUp() throws Exception {
468         logd("DcTrackerTest +Setup!");
469         super.setUp(getClass().getSimpleName());
470 
471         doReturn("fake.action_detached").when(mPhone).getActionDetached();
472         doReturn("fake.action_attached").when(mPhone).getActionAttached();
473         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_LTE).when(mServiceState)
474                 .getRilDataRadioTechnology();
475 
476         mContextFixture.putStringArrayResource(com.android.internal.R.array.
477                 config_mobile_tcp_buffers, new String[]{
478                 "umts:131072,262144,1452032,4096,16384,399360",
479                 "hspa:131072,262144,2441216,4096,16384,399360",
480                 "hsupa:131072,262144,2441216,4096,16384,399360",
481                 "hsdpa:131072,262144,2441216,4096,16384,399360",
482                 "hspap:131072,262144,2441216,4096,16384,399360",
483                 "edge:16384,32768,131072,4096,16384,65536",
484                 "gprs:4096,8192,24576,4096,8192,24576",
485                 "1xrtt:16384,32768,131070,4096,16384,102400",
486                 "evdo:131072,262144,1048576,4096,16384,524288",
487                 "lte:524288,1048576,8388608,262144,524288,4194304"});
488 
489         mContextFixture.putResource(R.string.config_wwan_data_service_package,
490                 "com.android.phone");
491 
492         ((MockContentResolver) mContext.getContentResolver()).addProvider(
493                 Telephony.Carriers.CONTENT_URI.getAuthority(), mApnSettingContentProvider);
494         Settings.Global.putInt(mContext.getContentResolver(),
495                 Settings.Global.DATA_STALL_RECOVERY_ON_BAD_NETWORK, 0);
496 
497         doReturn(PhoneConstants.State.IDLE).when(mCT).getState();
498         doReturn(true).when(mSST).getDesiredPowerState();
499         doReturn(true).when(mSST).getPowerStateFromCarrier();
500         doAnswer(
501                 new Answer<Void>() {
502                     @Override
503                     public Void answer(InvocationOnMock invocation) throws Throwable {
504                         mOnSubscriptionsChangedListener =
505                                 (SubscriptionManager.OnSubscriptionsChangedListener)
506                                         invocation.getArguments()[0];
507                         return null;
508                     }
509                 }
510         ).when(mSubscriptionManager).addOnSubscriptionsChangedListener(any());
511         doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(anyInt());
512         doReturn(mNetworkPolicyManager).when(mContext)
513                 .getSystemService(Context.NETWORK_POLICY_SERVICE);
514         doReturn(1).when(mIsub).getDefaultDataSubId();
515         doReturn(mIsub).when(mBinder).queryLocalInterface(anyString());
516         mServiceManagerMockedServices.put("isub", mBinder);
517         mServiceManagerMockedServices.put("package", mMockPackageManagerInternal);
518 
519         mContextFixture.putStringArrayResource(
520                 com.android.internal.R.array.config_cell_retries_per_error_code,
521                 new String[]{"36,2"});
522 
523         mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
524         mBundle = mContextFixture.getCarrierConfigBundle();
525 
526         mBundle.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
527 
528         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
529         addDataService();
530 
531         mDcTrackerTestHandler = new DcTrackerTestHandler(getClass().getSimpleName());
532         mDcTrackerTestHandler.start();
533         waitUntilReady();
534 
535         Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
536         intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, mPhone.getPhoneId());
537         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, mPhone.getSubId());
538         mContext.sendBroadcast(intent);
539 
540         waitForMs(600);
541         logd("DcTrackerTest -Setup!");
542     }
543 
544     @After
tearDown()545     public void tearDown() throws Exception {
546         logd("DcTrackerTest -tearDown");
547         mDct.removeCallbacksAndMessages(null);
548         mDct.stopHandlerThread();
549         mDct = null;
550         mDcTrackerTestHandler.quit();
551         mCellularDataService.onDestroy();
552         waitForMs(100);
553         super.tearDown();
554     }
555 
556     // Create a successful data response
createSetupDataCallResult()557     private static SetupDataCallResult createSetupDataCallResult() throws Exception {
558         SetupDataCallResult result = new SetupDataCallResult();
559         result.status = 0;
560         result.suggestedRetryTime = -1;
561         result.cid = 1;
562         result.active = 2;
563         result.type = "IP";
564         result.ifname = FAKE_IFNAME;
565         result.addresses = FAKE_ADDRESS;
566         result.dnses = FAKE_DNS;
567         result.gateways = FAKE_GATEWAY;
568         result.pcscf = FAKE_PCSCF_ADDRESS;
569         result.mtu = 1440;
570         return result;
571     }
572 
verifyDataProfile(DataProfile dp, String apn, int profileId, int supportedApnTypesBitmap, int type, int bearerBitmask)573     private void verifyDataProfile(DataProfile dp, String apn, int profileId,
574                                    int supportedApnTypesBitmap, int type, int bearerBitmask) {
575         assertEquals(profileId, dp.getProfileId());
576         assertEquals(apn, dp.getApn());
577         assertEquals(ApnSetting.PROTOCOL_IP, dp.getProtocolType());
578         assertEquals(0, dp.getAuthType());
579         assertEquals("", dp.getUserName());
580         assertEquals("", dp.getPassword());
581         assertEquals(type, dp.getType());
582         assertEquals(0, dp.getWaitTime());
583         assertTrue(dp.isEnabled());
584         assertEquals(supportedApnTypesBitmap, dp.getSupportedApnTypesBitmask());
585         assertEquals(ApnSetting.PROTOCOL_IP, dp.getRoamingProtocolType());
586         assertEquals(bearerBitmask, dp.getBearerBitmask());
587         assertEquals(0, dp.getMtu());
588         assertTrue(dp.isPersistent());
589         assertFalse(dp.isPreferred());
590     }
591 
verifyDataConnected(final String apnSetting)592     private void verifyDataConnected(final String apnSetting) {
593         verify(mPhone, atLeastOnce()).notifyDataConnection(
594                 eq(PhoneConstants.APN_TYPE_DEFAULT));
595 
596         verify(mAlarmManager, times(1)).set(eq(AlarmManager.ELAPSED_REALTIME), anyLong(),
597                 any(PendingIntent.class));
598 
599         assertEquals(apnSetting, mDct.getActiveApnString(PhoneConstants.APN_TYPE_DEFAULT));
600         assertArrayEquals(new String[]{PhoneConstants.APN_TYPE_DEFAULT}, mDct.getActiveApnTypes());
601 
602         assertTrue(mDct.isAnyDataConnected());
603         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
604 
605         LinkProperties linkProperties = mDct.getLinkProperties(PhoneConstants.APN_TYPE_DEFAULT);
606         assertEquals(FAKE_IFNAME, linkProperties.getInterfaceName());
607         assertEquals(1, linkProperties.getAddresses().size());
608         assertEquals(FAKE_ADDRESS, linkProperties.getAddresses().get(0).getHostAddress());
609         assertEquals(1, linkProperties.getDnsServers().size());
610         assertEquals(FAKE_DNS, linkProperties.getDnsServers().get(0).getHostAddress());
611         assertEquals(FAKE_GATEWAY, linkProperties.getRoutes().get(0).getGateway().getHostAddress());
612     }
613 
isDataAllowed(DataConnectionReasons dataConnectionReasons)614     private boolean isDataAllowed(DataConnectionReasons dataConnectionReasons) {
615         try {
616             Method method = DcTracker.class.getDeclaredMethod("isDataAllowed",
617                     DataConnectionReasons.class);
618             method.setAccessible(true);
619             return (boolean) method.invoke(mDct, dataConnectionReasons);
620         } catch (Exception e) {
621             fail(e.toString());
622             return false;
623         }
624     }
625 
sendInitializationEvents()626     private void sendInitializationEvents() {
627         logd("Sending EVENT_CARRIER_CONFIG_CHANGED");
628         mDct.sendEmptyMessage(DctConstants.EVENT_CARRIER_CONFIG_CHANGED);
629         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
630 
631         logd("Sending EVENT_SIM_STATE_UPDATED");
632         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_SIM_STATE_UPDATED,
633                 TelephonyManager.SIM_STATE_LOADED, 0));
634         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
635 
636         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
637         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
638         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
639 
640         waitForMs(200);
641     }
642 
643     // Test the unmetered APN setup when data is disabled.
644     @Test
645     @SmallTest
testTrySetupDataUnmeteredDefaultNotSelected()646     public void testTrySetupDataUnmeteredDefaultNotSelected() throws Exception {
647         initApns(PhoneConstants.APN_TYPE_XCAP, new String[]{PhoneConstants.APN_TYPE_XCAP});
648         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mIsub).getDefaultDataSubId();
649 
650         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
651                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
652 
653         sendInitializationEvents();
654 
655         mDct.enableApn(ApnSetting.TYPE_XCAP, DcTracker.REQUEST_TYPE_NORMAL, null);
656         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
657 
658         // Data connection is running on a different thread. Have to wait.
659         waitForMs(200);
660         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
661                 eq(AccessNetworkType.EUTRAN), any(DataProfile.class),
662                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
663                 any(Message.class));
664     }
665 
666     // Test the normal data call setup scenario.
667     @Test
668     @MediumTest
testDataSetup()669     public void testDataSetup() throws Exception {
670         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
671 
672         DataConnectionReasons dataConnectionReasons = new DataConnectionReasons();
673         boolean allowed = isDataAllowed(dataConnectionReasons);
674         assertFalse(dataConnectionReasons.toString(), allowed);
675 
676         logd("Sending EVENT_ENABLE_APN");
677         // APN id 0 is APN_TYPE_DEFAULT
678         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
679         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
680 
681         sendInitializationEvents();
682 
683         dataConnectionReasons = new DataConnectionReasons();
684         allowed = isDataAllowed(dataConnectionReasons);
685         assertTrue(dataConnectionReasons.toString(), allowed);
686 
687         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
688         // Verify if RIL command was sent properly.
689         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
690                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
691                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
692                 any(Message.class));
693         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
694 
695         verifyDataConnected(FAKE_APN1);
696     }
697 
698     // Test the scenario where the first data call setup is failed, and then retry the setup later.
699     @Test
700     @MediumTest
testDataRetry()701     public void testDataRetry() throws Exception {
702         AsyncResult ar = new AsyncResult(null,
703                 new Pair<>(true, DataEnabledSettings.REASON_USER_DATA_ENABLED), null);
704         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_ENABLED_CHANGED, ar));
705         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
706 
707         // LOST_CONNECTION(0x10004) is a non-permanent failure, so we'll retry data setup later.
708         SetupDataCallResult result = createSetupDataCallResult();
709         result.status = 0x10004;
710 
711         // Simulate RIL fails the data call setup
712         mSimulatedCommands.setDataCallResult(true, result);
713 
714         DataConnectionReasons dataConnectionReasons = new DataConnectionReasons();
715         boolean allowed = isDataAllowed(dataConnectionReasons);
716         assertFalse(dataConnectionReasons.toString(), allowed);
717 
718         logd("Sending EVENT_ENABLE_APN");
719         // APN id 0 is APN_TYPE_DEFAULT
720         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
721         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
722 
723         sendInitializationEvents();
724 
725         dataConnectionReasons = new DataConnectionReasons();
726         allowed = isDataAllowed(dataConnectionReasons);
727         assertTrue(dataConnectionReasons.toString(), allowed);
728 
729         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
730         // Verify if RIL command was sent properly.
731         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
732                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
733                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
734                 any(Message.class));
735         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
736 
737         // This time we'll let RIL command succeed.
738         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
739 
740         //Send event for reconnecting data
741         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
742         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RECONNECT,
743                         mPhone.getPhoneId(), AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
744                         mApnContext));
745         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
746 
747         // Data connection is running on a different thread. Have to wait.
748         waitForMs(200);
749         dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
750         // Verify if RIL command was sent properly.
751         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
752                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
753                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
754                 any(Message.class));
755         verifyDataProfile(dpCaptor.getValue(), FAKE_APN2, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
756 
757         // Verify connected with APN2 setting.
758         verifyDataConnected(FAKE_APN2);
759     }
760 
761     @Test
762     @MediumTest
763     @Ignore
764     @FlakyTest
testUserDisableData()765     public void testUserDisableData() throws Exception {
766         //step 1: setup two DataCalls one for Metered: default, another one for Non-metered: IMS
767         //set Default and MMS to be metered in the CarrierConfigManager
768         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
769                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
770         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
771         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
772 
773         sendInitializationEvents();
774 
775         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
776         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
777                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
778                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
779                 any(Message.class));
780         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, NETWORK_TYPE_LTE_BITMASK);
781 
782         logd("Sending DATA_DISABLED_CMD");
783         doReturn(false).when(mDataEnabledSettings).isDataEnabled();
784         doReturn(false).when(mDataEnabledSettings).isDataEnabled(anyInt());
785         AsyncResult ar = new AsyncResult(null,
786                 new Pair<>(false, DataEnabledSettings.REASON_USER_DATA_ENABLED), null);
787         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_ENABLED_CHANGED, ar));
788         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
789 
790         // Data connection is running on a different thread. Have to wait.
791         waitForMs(200);
792         // expected tear down all metered DataConnections
793         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
794                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
795                 any(Message.class));
796         assertTrue(mDct.isAnyDataConnected());
797         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
798         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_IMS));
799     }
800 
801     @Test
802     @MediumTest
testTrySetupDataMmsAllowedDataDisabled()803     public void testTrySetupDataMmsAllowedDataDisabled() throws Exception {
804         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
805                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
806         mDct.enableApn(ApnSetting.TYPE_MMS, DcTracker.REQUEST_TYPE_NORMAL, null);
807         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
808 
809         sendInitializationEvents();
810 
811         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
812         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
813                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
814                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
815                 any(Message.class));
816 
817 
818         List<DataProfile> dataProfiles = dpCaptor.getAllValues();
819         assertEquals(2, dataProfiles.size());
820 
821         //Verify FAKE_APN1
822         Optional<DataProfile> fakeApn1 = dataProfiles.stream()
823                 .filter(dp -> dp.getApn().equals(FAKE_APN1))
824                 .findFirst();
825         assertTrue(fakeApn1.isPresent());
826         verifyDataProfile(fakeApn1.get(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
827 
828         //Verify FAKE_APN6
829         Optional<DataProfile> fakeApn6 = dataProfiles.stream()
830                 .filter(dp -> dp.getApn().equals(FAKE_APN6))
831                 .findFirst();
832         assertTrue(fakeApn6.isPresent());
833         verifyDataProfile(fakeApn6.get(), FAKE_APN6, 0, ApnSetting.TYPE_MMS | ApnSetting.TYPE_XCAP,
834                 1, NETWORK_TYPE_LTE_BITMASK);
835 
836         logd("Sending DATA_DISABLED_CMD for default data");
837         doReturn(false).when(mDataEnabledSettings).isDataEnabled();
838         doReturn(false).when(mDataEnabledSettings).isDataEnabled(anyInt());
839         mDct.obtainMessage(DctConstants.EVENT_DATA_ENABLED_OVERRIDE_RULES_CHANGED).sendToTarget();
840         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
841 
842         // Data connection is running on a different thread. Have to wait.
843         waitForMs(200);
844         // expected tear down all metered DataConnections
845         verify(mSimulatedCommandsVerifier, times(2)).deactivateDataCall(
846                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
847                 any(Message.class));
848         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
849         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_MMS));
850 
851         clearInvocations(mSimulatedCommandsVerifier);
852         doReturn(true).when(mDataEnabledSettings).isDataEnabled(ApnSetting.TYPE_MMS);
853         mDct.obtainMessage(DctConstants.EVENT_DATA_ENABLED_OVERRIDE_RULES_CHANGED).sendToTarget();
854         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
855 
856         // Data connection is running on a different thread. Have to wait.
857         waitForMs(200);
858         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
859                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
860                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
861                 any(Message.class));
862         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
863         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_MMS));
864     }
865 
866     @Test
867     @MediumTest
testUserDisableRoaming()868     public void testUserDisableRoaming() throws Exception {
869         //step 1: setup two DataCalls one for Metered: default, another one for Non-metered: IMS
870         //step 2: set roaming disabled, data is enabled
871         //step 3: under roaming service
872         //step 4: only tear down metered data connections.
873 
874         //set Default and MMS to be metered in the CarrierConfigManager
875         boolean roamingEnabled = mDct.getDataRoamingEnabled();
876 
877         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
878                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
879 
880         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
881         waitForHandlerAction(mDct, 1000);
882         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
883         waitForHandlerAction(mDct, 1000);
884 
885         sendInitializationEvents();
886 
887         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
888         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
889                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
890                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
891                 any(Message.class));
892         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
893 
894         //user is in roaming
895         doReturn(true).when(mServiceState).getDataRoaming();
896         logd("Sending DISABLE_ROAMING_CMD");
897         mDct.setDataRoamingEnabledByUser(false);
898         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_ROAMING_ON));
899         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
900 
901         // Data connection is running on a different thread. Have to wait.
902         waitForMs(200);
903         // expected tear down all metered DataConnections
904         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
905                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
906                 any(Message.class));
907         assertTrue(mDct.isAnyDataConnected());
908         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
909         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_IMS));
910 
911         // reset roaming settings / data enabled settings at end of this test
912         mDct.setDataRoamingEnabledByUser(roamingEnabled);
913         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
914     }
915 
916     @Test
917     @MediumTest
testDataCallOnUserDisableRoaming()918     public void testDataCallOnUserDisableRoaming() throws Exception {
919         //step 1: mock under roaming service and user disabled roaming from settings.
920         //step 2: user toggled data settings on
921         //step 3: only non-metered data call is established
922 
923         boolean roamingEnabled = mDct.getDataRoamingEnabled();
924         doReturn(true).when(mServiceState).getDataRoaming();
925 
926         //set Default and MMS to be metered in the CarrierConfigManager
927         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
928                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
929         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
930         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
931 
932         logd("Sending DISABLE_ROAMING_CMD");
933         mDct.setDataRoamingEnabledByUser(false);
934 
935         sendInitializationEvents();
936 
937         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
938 
939         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
940                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
941                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
942                 any(Message.class));
943         verifyDataProfile(dpCaptor.getValue(), FAKE_APN3, 2, 64, 0, 0);
944 
945         assertTrue(mDct.isAnyDataConnected());
946         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
947         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_IMS));
948 
949         // reset roaming settings / data enabled settings at end of this test
950         mDct.setDataRoamingEnabledByUser(roamingEnabled);
951         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
952     }
953 
954     // Test the default data switch scenario.
955     @FlakyTest /* flakes 1.57% of the time */
956     @Test
957     @MediumTest
testDDSResetAutoAttach()958     public void testDDSResetAutoAttach() throws Exception {
959         mContextFixture.putBooleanResource(
960                 com.android.internal.R.bool.config_auto_attach_data_on_creation, true);
961         testDataSetup();
962         assertTrue(mDct.shouldAutoAttach());
963         mDct.sendEmptyMessage(DctConstants.EVENT_CARRIER_CONFIG_CHANGED);
964         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
965         // The auto attach flag should be reset after update
966         assertFalse(mDct.shouldAutoAttach());
967     }
968 
969     // Test for API carrierActionSetMeteredApnsEnabled.
970     @FlakyTest
971     @Ignore
972     @Test
973     @MediumTest
testCarrierActionSetMeteredApnsEnabled()974     public void testCarrierActionSetMeteredApnsEnabled() throws Exception {
975         //step 1: setup two DataCalls one for Internet and IMS
976         //step 2: set data is enabled
977         //step 3: cold sim is detected
978         //step 4: all data connection is torn down
979         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
980                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
981 
982         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
983         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
984 
985         sendInitializationEvents();
986 
987         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
988         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
989                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
990                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
991                 any(Message.class));
992         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, NETWORK_TYPE_LTE_BITMASK);
993         assertTrue(mDct.isAnyDataConnected());
994 
995         AsyncResult ar = new AsyncResult(null,
996                 new Pair<>(false, DataEnabledSettings.REASON_DATA_ENABLED_BY_CARRIER), null);
997         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_ENABLED_CHANGED, ar));
998         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
999 
1000         // Data connection is running on a different thread. Have to wait.
1001         waitForMs(200);
1002         // Validate all metered data connections have been torn down
1003         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
1004                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
1005                 any(Message.class));
1006         assertTrue(mDct.isAnyDataConnected());
1007         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
1008     }
1009 
initApns(String targetApn, String[] canHandleTypes)1010     private void initApns(String targetApn, String[] canHandleTypes) {
1011         doReturn(targetApn).when(mApnContext).getApnType();
1012         doReturn(ApnSetting.getApnTypesBitmaskFromString(mApnContext.getApnType()))
1013                 .when(mApnContext).getApnTypeBitmask();
1014         doReturn(true).when(mApnContext).isConnectable();
1015         ApnSetting apnSetting = createApnSetting(ApnSetting.getApnTypesBitmaskFromString(
1016                 TextUtils.join(",", canHandleTypes)));
1017         doReturn(apnSetting).when(mApnContext).getNextApnSetting();
1018         doReturn(apnSetting).when(mApnContext).getApnSetting();
1019         doReturn(mDataConnection).when(mApnContext).getDataConnection();
1020         doReturn(true).when(mApnContext).isEnabled();
1021         doReturn(true).when(mApnContext).isDependencyMet();
1022         doReturn(true).when(mApnContext).isReady();
1023         doReturn(false).when(mApnContext).hasRestrictedRequests(eq(true));
1024     }
1025 
1026     // Test the emergency APN setup.
1027     @Test
1028     @SmallTest
testTrySetupDataEmergencyApn()1029     public void testTrySetupDataEmergencyApn() throws Exception {
1030         initApns(PhoneConstants.APN_TYPE_EMERGENCY,
1031                 new String[]{PhoneConstants.APN_TYPE_EMERGENCY});
1032         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
1033         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1034 
1035         waitForMs(200);
1036 
1037         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
1038                 eq(AccessNetworkType.EUTRAN), any(DataProfile.class), eq(false), eq(false),
1039                 eq(DataService.REQUEST_REASON_NORMAL), any(), any(Message.class));
1040     }
1041 
1042     // Test the XCAP APN setup.
1043     @Test
1044     @SmallTest
testTrySetupDataXcapApn()1045     public void testTrySetupDataXcapApn() throws Exception {
1046         initApns(PhoneConstants.APN_TYPE_XCAP, new String[]{PhoneConstants.APN_TYPE_XCAP});
1047         mDct.enableApn(ApnSetting.TYPE_XCAP, DcTracker.REQUEST_TYPE_NORMAL, null);
1048 
1049         sendInitializationEvents();
1050 
1051         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
1052                 eq(AccessNetworkType.EUTRAN), any(DataProfile.class), eq(false), eq(false),
1053                 eq(DataService.REQUEST_REASON_NORMAL), any(), any(Message.class));
1054     }
1055 
1056     @Test
1057     @SmallTest
testGetDataConnectionState()1058     public void testGetDataConnectionState() throws Exception {
1059         initApns(PhoneConstants.APN_TYPE_SUPL,
1060                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_DEFAULT});
1061         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1062                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1063         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1064         mDct.enableApn(ApnSetting.TYPE_SUPL, DcTracker.REQUEST_TYPE_NORMAL, null);
1065 
1066         sendInitializationEvents();
1067 
1068         // Assert that both APN_TYPE_SUPL & APN_TYPE_DEFAULT are connected even we only setup data
1069         // for APN_TYPE_SUPL
1070         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_SUPL));
1071         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
1072     }
1073 
1074     // Test the unmetered APN setup when data is disabled.
1075     @Test
1076     @SmallTest
testTrySetupDataUnmeteredDataDisabled()1077     public void testTrySetupDataUnmeteredDataDisabled() throws Exception {
1078         initApns(PhoneConstants.APN_TYPE_SUPL, new String[]{PhoneConstants.APN_TYPE_SUPL});
1079         doReturn(false).when(mDataEnabledSettings).isDataEnabled();
1080         doReturn(false).when(mDataEnabledSettings).isDataEnabled(anyInt());
1081 
1082         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1083                 new String[]{PhoneConstants.APN_TYPE_FOTA});
1084 
1085         mDct.enableApn(ApnSetting.TYPE_SUPL, DcTracker.REQUEST_TYPE_NORMAL, null);
1086 
1087         sendInitializationEvents();
1088 
1089         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
1090                 eq(AccessNetworkType.EUTRAN), any(DataProfile.class),
1091                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1092                 any(Message.class));
1093     }
1094 
1095     // Test the unmetered default APN setup when data is disabled. Default APN should always honor
1096     // the users's setting.
1097     @Test
1098     @SmallTest
testTrySetupDataUnmeteredDefaultDataDisabled()1099     public void testTrySetupDataUnmeteredDefaultDataDisabled() throws Exception {
1100         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1101         //mDct.setUserDataEnabled(false);
1102         doReturn(false).when(mDataEnabledSettings).isDataEnabled();
1103         doReturn(false).when(mDataEnabledSettings).isDataEnabled(anyInt());
1104 
1105         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1106                 new String[]{PhoneConstants.APN_TYPE_MMS});
1107 
1108         logd("Sending EVENT_CARRIER_CONFIG_CHANGED");
1109         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_CARRIER_CONFIG_CHANGED, 1, 0));
1110         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1111 
1112         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
1113         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
1114         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1115 
1116         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
1117         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1118 
1119         waitForMs(200);
1120 
1121         verify(mSimulatedCommandsVerifier, never()).setupDataCall(
1122                 eq(AccessNetworkType.EUTRAN), any(DataProfile.class),
1123                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1124                 any(Message.class));
1125     }
1126 
1127 
1128     // Test the metered APN setup when data is disabled.
1129     @Test
1130     @SmallTest
testTrySetupMeteredDataDisabled()1131     public void testTrySetupMeteredDataDisabled() throws Exception {
1132         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1133         doReturn(false).when(mDataEnabledSettings).isDataEnabled();
1134         doReturn(false).when(mDataEnabledSettings).isDataEnabled(anyInt());
1135 
1136         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1137                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1138 
1139         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1140 
1141         sendInitializationEvents();
1142 
1143         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
1144                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1145                 any(Message.class));
1146     }
1147 
1148     // Test the restricted data request when data is disabled.
1149     @Test
1150     @SmallTest
testTrySetupRestrictedDataDisabled()1151     public void testTrySetupRestrictedDataDisabled() throws Exception {
1152         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1153         doReturn(false).when(mDataEnabledSettings).isDataEnabled();
1154         doReturn(false).when(mDataEnabledSettings).isDataEnabled(anyInt());
1155 
1156         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1157                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1158 
1159         sendInitializationEvents();
1160 
1161         NetworkRequest nr = new NetworkRequest.Builder()
1162                 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
1163                 .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
1164                 .build();
1165         mDct.requestNetwork(nr, DcTracker.REQUEST_TYPE_NORMAL, null);
1166         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1167 
1168         // Data connection is running on a different thread. Have to wait.
1169         waitForMs(200);
1170         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(anyInt(), any(DataProfile.class),
1171                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1172                 any(Message.class));
1173     }
1174 
1175     // Test the restricted data request when roaming is disabled.
1176     @Test
1177     @SmallTest
testTrySetupRestrictedRoamingDisabled()1178     public void testTrySetupRestrictedRoamingDisabled() throws Exception {
1179         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1180 
1181         mDct.setDataRoamingEnabledByUser(false);
1182         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1183                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1184         //user is in roaming
1185         doReturn(true).when(mServiceState).getDataRoaming();
1186 
1187         sendInitializationEvents();
1188 
1189         NetworkRequest nr = new NetworkRequest.Builder()
1190                 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
1191                 .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
1192                 .build();
1193         mDct.requestNetwork(nr, DcTracker.REQUEST_TYPE_NORMAL, null);
1194         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1195 
1196         // Data connection is running on a different thread. Have to wait.
1197         waitForMs(200);
1198         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(anyInt(), any(DataProfile.class),
1199                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1200                 any(Message.class));
1201     }
1202 
1203     // Test the default data when data is not connectable.
1204     @Test
1205     @SmallTest
testTrySetupNotConnectable()1206     public void testTrySetupNotConnectable() throws Exception {
1207         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1208         doReturn(false).when(mApnContext).isConnectable();
1209 
1210         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1211                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1212 
1213         sendInitializationEvents();
1214 
1215         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
1216                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1217                 any(Message.class));
1218     }
1219 
1220     // Test the default data on IWLAN.
1221     @Test
1222     @SmallTest
testTrySetupDefaultOnIWLAN()1223     public void testTrySetupDefaultOnIWLAN() throws Exception {
1224         doReturn(true).when(mTransportManager).isInLegacyMode();
1225         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1226         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1227                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_IWLAN)
1228                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1229                 .build();
1230         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1231                 anyInt(), anyInt());
1232 
1233         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1234                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1235 
1236         sendInitializationEvents();
1237 
1238         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
1239                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1240                 any(Message.class));
1241     }
1242 
1243     // Test the default data when the phone is in ECBM.
1244     @Test
1245     @SmallTest
testTrySetupDefaultInECBM()1246     public void testTrySetupDefaultInECBM() throws Exception {
1247         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1248         doReturn(true).when(mPhone).isInEcm();
1249 
1250         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1251                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1252 
1253         sendInitializationEvents();
1254 
1255         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
1256                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1257                 any(Message.class));
1258     }
1259 
1260     // Test update waiting apn list when on data rat change
1261     @FlakyTest /* flakes 0.86% of the time */
1262     @Ignore
1263     @Test
1264     @SmallTest
testUpdateWaitingApnListOnDataRatChange()1265     public void testUpdateWaitingApnListOnDataRatChange() throws Exception {
1266         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1267                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_EHRPD)
1268                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1269                 .build();
1270         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1271                 anyInt(), anyInt());
1272         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1273                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1274         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1275         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1276 
1277         sendInitializationEvents();
1278 
1279         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1280         // Verify if RIL command was sent properly.
1281         verify(mSimulatedCommandsVerifier).setupDataCall(
1282                 eq(AccessNetworkType.CDMA2000), dpCaptor.capture(),
1283                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1284                 any(Message.class));
1285         verifyDataProfile(dpCaptor.getValue(), FAKE_APN4, 0, 21, 2, NETWORK_TYPE_EHRPD_BITMASK);
1286         assertTrue(mDct.isAnyDataConnected());
1287 
1288         //data rat change from ehrpd to lte
1289         logd("Sending EVENT_DATA_RAT_CHANGED");
1290         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1291                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
1292                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1293                 .build();
1294         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1295                 anyInt(), anyInt());
1296         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
1297         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1298 
1299         // Data connection is running on a different thread. Have to wait.
1300         waitForMs(200);
1301         // Verify the disconnected data call due to rat change and retry manger schedule another
1302         // data call setup
1303         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
1304                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
1305                 any(Message.class));
1306         verify(mAlarmManager, times(1)).setExact(eq(AlarmManager.ELAPSED_REALTIME_WAKEUP),
1307                 anyLong(), any(PendingIntent.class));
1308 
1309         //Send event for reconnecting data
1310         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1311         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RECONNECT,
1312                         mPhone.getPhoneId(), AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
1313                         mApnContext));
1314         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1315 
1316         // Data connection is running on a different thread. Have to wait.
1317         waitForMs(200);
1318         // Verify if RIL command was sent properly.
1319         verify(mSimulatedCommandsVerifier).setupDataCall(
1320                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
1321                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1322                 any(Message.class));
1323         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
1324         assertTrue(mDct.isAnyDataConnected());
1325     }
1326 
1327     // Test for fetchDunApns()
1328     @Test
1329     @SmallTest
testFetchDunApn()1330     public void testFetchDunApn() {
1331 
1332         sendInitializationEvents();
1333 
1334         String dunApnString = "[ApnSettingV3]HOT mobile PC,pc.hotm,,,,,,,,,440,10,,DUN,,,true,"
1335                 + "0,,,,,,,,";
1336         ApnSetting dunApnExpected = ApnSetting.fromString(dunApnString);
1337 
1338         Settings.Global.putString(mContext.getContentResolver(),
1339                 Settings.Global.TETHER_DUN_APN, dunApnString);
1340         // should return APN from Setting
1341         ApnSetting dunApn = mDct.fetchDunApns().get(0);
1342         assertTrue(dunApnExpected.equals(dunApn));
1343 
1344         Settings.Global.putString(mContext.getContentResolver(),
1345                 Settings.Global.TETHER_DUN_APN, null);
1346         // should return APN from db
1347         dunApn = mDct.fetchDunApns().get(0);
1348         assertEquals(FAKE_APN5, dunApn.getApnName());
1349     }
1350 
1351     // Test for fetchDunApns() with apn set id
1352     @Test
1353     @SmallTest
testFetchDunApnWithPreferredApnSet()1354     public void testFetchDunApnWithPreferredApnSet() {
1355         logd("Sending EVENT_CARRIER_CONFIG_CHANGED");
1356         mDct.sendEmptyMessage(DctConstants.EVENT_CARRIER_CONFIG_CHANGED);
1357         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1358 
1359         // apnSetId=1
1360         String dunApnString1 = "[ApnSettingV5]HOT mobile PC,pc.hotm,,,,,,,,,440,10,,DUN,,,true,"
1361                 + "0,,,,,,,,,,1";
1362         // apnSetId=0
1363         String dunApnString2 = "[ApnSettingV5]HOT mobile PC,pc.coldm,,,,,,,,,440,10,,DUN,,,true,"
1364                 + "0,,,,,,,,,,0";
1365 
1366         ApnSetting dunApnExpected = ApnSetting.fromString(dunApnString1);
1367 
1368         ContentResolver cr = mContext.getContentResolver();
1369         Settings.Global.putString(cr, Settings.Global.TETHER_DUN_APN,
1370                 dunApnString1 + ";" + dunApnString2);
1371 
1372         // set that we prefer apn set 1
1373         ContentValues values = new ContentValues();
1374         values.put(Telephony.Carriers.APN_SET_ID, 1);
1375         cr.update(PREFERAPN_URI, values, null, null);
1376 
1377         // return APN from Setting with apnSetId=1
1378         ArrayList<ApnSetting> dunApns = mDct.sortApnListByPreferred(mDct.fetchDunApns());
1379         assertEquals(2, dunApns.size());
1380         assertTrue(dunApnExpected.equals(dunApns.get(0)));
1381     }
1382 
1383     @Test
1384     @SmallTest
testFetchDunApnWhileRoaming()1385     public void testFetchDunApnWhileRoaming() {
1386         doReturn(true).when(mServiceState).getRoaming();
1387         mBundle.putBoolean(CarrierConfigManager.KEY_DISABLE_DUN_APN_WHILE_ROAMING, true);
1388 
1389         sendInitializationEvents();
1390 
1391         String dunApnString = "[ApnSettingV3]HOT mobile PC,pc.hotm,,,,,,,,,440,10,,DUN,,,true,"
1392                 + "0,,,,,,,,";
1393 
1394         Settings.Global.putString(mContext.getContentResolver(),
1395                 Settings.Global.TETHER_DUN_APN, dunApnString);
1396         // Expect empty DUN APN list
1397         assertEquals(0, mDct.fetchDunApns().size());
1398 
1399         Settings.Global.putString(mContext.getContentResolver(),
1400                 Settings.Global.TETHER_DUN_APN, null);
1401     }
1402 
1403     // Test oos
1404     @Test
1405     @SmallTest
testDataRatChangeOOS()1406     public void testDataRatChangeOOS() throws Exception {
1407         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1408                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_EHRPD)
1409                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1410                 .build();
1411         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1412                 anyInt(), anyInt());
1413 
1414         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1415                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
1416         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1417         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1418 
1419         sendInitializationEvents();
1420 
1421         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1422         // Verify if RIL command was sent properly.
1423         verify(mSimulatedCommandsVerifier).setupDataCall(
1424                 eq(AccessNetworkType.CDMA2000), dpCaptor.capture(),
1425                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1426                 any(Message.class));
1427         verifyDataProfile(dpCaptor.getValue(), FAKE_APN4, 0, 21, 2, NETWORK_TYPE_EHRPD_BITMASK);
1428         assertTrue(mDct.isAnyDataConnected());
1429 
1430         // Data rat change from ehrpd to unknown due to OOS
1431         logd("Sending EVENT_DATA_RAT_CHANGED");
1432         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1433                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_UNKNOWN)
1434                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1435                 .build();
1436         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1437                 anyInt(), anyInt());
1438         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
1439         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1440 
1441         // Data connection is running on a different thread. Have to wait.
1442         waitForMs(200);
1443         // Verify data connection is on
1444         verify(mSimulatedCommandsVerifier, times(0)).deactivateDataCall(
1445                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
1446                 any(Message.class));
1447 
1448         // Data rat resume from unknown to ehrpd
1449         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1450                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_EHRPD)
1451                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1452                 .build();
1453         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1454                 anyInt(), anyInt());
1455         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
1456         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1457 
1458         // Verify the same data connection
1459         assertEquals(FAKE_APN4, mDct.getActiveApnString(PhoneConstants.APN_TYPE_DEFAULT));
1460         assertTrue(mDct.isAnyDataConnected());
1461     }
1462 
1463     // Test provisioning
1464     /*@Test
1465     @SmallTest
1466     public void testDataEnableInProvisioning() throws Exception {
1467         ContentResolver resolver = mContext.getContentResolver();
1468 
1469         assertEquals(1, Settings.Global.getInt(resolver, Settings.Global.MOBILE_DATA));
1470         assertTrue(mDct.isDataEnabled());
1471         assertTrue(mDct.isUserDataEnabled());
1472 
1473         mDct.setUserDataEnabled(false);
1474         waitForMs(200);
1475 
1476         assertEquals(0, Settings.Global.getInt(resolver, Settings.Global.MOBILE_DATA));
1477         assertFalse(mDct.isDataEnabled());
1478         assertFalse(mDct.isUserDataEnabled());
1479 
1480         // Changing provisioned to 0.
1481         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0);
1482         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DEVICE_PROVISIONED_CHANGE, null));
1483         waitForMs(200);
1484 
1485         assertTrue(mDct.isDataEnabled());
1486         assertTrue(mDct.isUserDataEnabled());
1487 
1488         // Enable user data during provisioning. It should write to
1489         // Settings.Global.MOBILE_DATA and keep data enabled when provisioned.
1490         mDct.setUserDataEnabled(true);
1491         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONED, 1);
1492         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DEVICE_PROVISIONED_CHANGE, null));
1493         waitForMs(200);
1494 
1495         assertTrue(mDct.isDataEnabled());
1496         assertTrue(mDct.isUserDataEnabled());
1497         assertEquals(1, Settings.Global.getInt(resolver, Settings.Global.MOBILE_DATA));
1498     }*/
1499 
1500     /*
1501     @Test
1502     @SmallTest
1503     public void testNotifyDataEnabledChanged() throws Exception {
1504         doAnswer(invocation -> {
1505             mMessage = (Message) invocation.getArguments()[0];
1506             return true;
1507         }).when(mHandler).sendMessageDelayed(any(), anyLong());
1508 
1509         // Test registration.
1510         mDct.registerForDataEnabledChanged(mHandler, DATA_ENABLED_CHANGED, null);
1511         verifyDataEnabledChangedMessage(true, DataEnabledSettings.REASON_REGISTERED);
1512 
1513         // Disable user data. Should receive data enabled change to false.
1514         mDct.setUserDataEnabled(false);
1515         waitForMs(200);
1516         verifyDataEnabledChangedMessage(false, DataEnabledSettings.REASON_USER_DATA_ENABLED);
1517 
1518         // Changing provisioned to 0. Shouldn't receive any message, as data enabled remains false.
1519         ContentResolver resolver = mContext.getContentResolver();
1520         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0);
1521         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED,
1522                 0);
1523         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DEVICE_PROVISIONED_CHANGE, null));
1524         waitForMs(200);
1525         assertFalse(mDct.isDataEnabled());
1526         verify(mHandler, never()).sendMessageDelayed(any(), anyLong());
1527 
1528         // Changing provisioningDataEnabled to 1. It should trigger data enabled change to true.
1529         Settings.Global.putInt(resolver,
1530                 Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, 1);
1531         mDct.sendMessage(mDct.obtainMessage(
1532                 DctConstants.EVENT_DEVICE_PROVISIONING_DATA_SETTING_CHANGE, null));
1533         waitForMs(200);
1534         verifyDataEnabledChangedMessage(
1535                 true, DataEnabledSettings.REASON_PROVISIONING_DATA_ENABLED_CHANGED);
1536     }*/
1537 
1538     @Test
1539     @SmallTest
testNetworkStatusChangedRecoveryOFF()1540     public void testNetworkStatusChangedRecoveryOFF() throws Exception {
1541         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1542                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
1543         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
1544         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1545 
1546         sendInitializationEvents();
1547 
1548         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1549         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
1550                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
1551                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1552                 any(Message.class));
1553         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
1554 
1555         logd("Sending EVENT_NETWORK_STATUS_CHANGED");
1556         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1557                 NetworkAgent.VALID_NETWORK, 1, null));
1558         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1559 
1560         logd("Sending EVENT_NETWORK_STATUS_CHANGED");
1561         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1562                 NetworkAgent.INVALID_NETWORK, 1, null));
1563         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1564 
1565         waitForMs(200);
1566 
1567         // Verify that its no-op when the new data stall detection feature is disabled
1568         verify(mSimulatedCommandsVerifier, times(0)).getDataCallList(any(Message.class));
1569     }
1570 
1571     @FlakyTest
1572     @Test
1573     @SmallTest
testNetworkStatusChangedRecoveryON()1574     public void testNetworkStatusChangedRecoveryON() throws Exception {
1575         ContentResolver resolver = mContext.getContentResolver();
1576         Settings.Global.putInt(resolver, Settings.Global.DATA_STALL_RECOVERY_ON_BAD_NETWORK, 1);
1577         Settings.System.putInt(resolver, "radio.data.stall.recovery.action", 0);
1578         doReturn(new SignalStrength()).when(mPhone).getSignalStrength();
1579 
1580         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1581                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
1582         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
1583         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1584 
1585         sendInitializationEvents();
1586 
1587         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1588         verify(mSimulatedCommandsVerifier, timeout(TEST_TIMEOUT).times(2)).setupDataCall(
1589                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
1590                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1591                 any(Message.class));
1592         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1593         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
1594 
1595         logd("Sending EVENT_NETWORK_STATUS_CHANGED");
1596         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1597                 NetworkAgent.VALID_NETWORK, 1, null));
1598         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1599 
1600         logd("Sending EVENT_NETWORK_STATUS_CHANGED");
1601         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1602                 NetworkAgent.INVALID_NETWORK, 1, null));
1603         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1604 
1605         // Data connection is running on a different thread. Have to wait.
1606         waitForMs(200);
1607         verify(mSimulatedCommandsVerifier, times(1)).getDataCallList(any(Message.class));
1608     }
1609 
1610     @FlakyTest
1611     @Test
1612     @SmallTest
testRecoveryStepPDPReset()1613     public void testRecoveryStepPDPReset() throws Exception {
1614         ContentResolver resolver = mContext.getContentResolver();
1615         Settings.Global.putInt(resolver, Settings.Global.DATA_STALL_RECOVERY_ON_BAD_NETWORK, 1);
1616         Settings.Global.putLong(resolver,
1617                 Settings.Global.MIN_DURATION_BETWEEN_RECOVERY_STEPS_IN_MS, 100);
1618         Settings.System.putInt(resolver, "radio.data.stall.recovery.action", 1);
1619         doReturn(new SignalStrength()).when(mPhone).getSignalStrength();
1620 
1621         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1622                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
1623         mDct.enableApn(ApnSetting.TYPE_IMS, DcTracker.REQUEST_TYPE_NORMAL, null);
1624         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1625 
1626         sendInitializationEvents();
1627 
1628         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1629         verify(mSimulatedCommandsVerifier, timeout(TEST_TIMEOUT).times(2)).setupDataCall(
1630                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
1631                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1632                 any(Message.class));
1633         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
1634 
1635         logd("Sending EVENT_NETWORK_STATUS_CHANGED false");
1636         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1637                 NetworkAgent.INVALID_NETWORK, 1, null));
1638         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1639 
1640         waitForMs(200);
1641 
1642         // expected tear down all DataConnections
1643         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
1644                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
1645                 any(Message.class));
1646     }
1647 
1648 
1649     @Test
1650     @SmallTest
testRecoveryStepReRegister()1651     public void testRecoveryStepReRegister() throws Exception {
1652         ContentResolver resolver = mContext.getContentResolver();
1653         Settings.Global.putInt(resolver, Settings.Global.DATA_STALL_RECOVERY_ON_BAD_NETWORK, 1);
1654         Settings.Global.putLong(resolver,
1655                 Settings.Global.MIN_DURATION_BETWEEN_RECOVERY_STEPS_IN_MS, 100);
1656         Settings.System.putInt(resolver, "radio.data.stall.recovery.action", 2);
1657         doReturn(new SignalStrength()).when(mPhone).getSignalStrength();
1658 
1659         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1660                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
1661         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1662 
1663         sendInitializationEvents();
1664 
1665         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1666         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
1667                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
1668                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1669                 any(Message.class));
1670         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
1671 
1672         logd("Sending EVENT_NETWORK_STATUS_CHANGED false");
1673         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1674                 NetworkAgent.INVALID_NETWORK, 1, null));
1675         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1676 
1677         // expected to get preferred network type
1678         verify(mSST, times(1)).reRegisterNetwork(eq(null));
1679     }
1680 
1681     @Test
1682     @SmallTest
testRecoveryStepRestartRadio()1683     public void testRecoveryStepRestartRadio() throws Exception {
1684         ContentResolver resolver = mContext.getContentResolver();
1685         Settings.Global.putInt(resolver, Settings.Global.DATA_STALL_RECOVERY_ON_BAD_NETWORK, 1);
1686         Settings.Global.putLong(resolver,
1687                 Settings.Global.MIN_DURATION_BETWEEN_RECOVERY_STEPS_IN_MS, 100);
1688         Settings.System.putInt(resolver, "radio.data.stall.recovery.action", 3);
1689         doReturn(new SignalStrength()).when(mPhone).getSignalStrength();
1690 
1691         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
1692                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
1693         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1694 
1695         sendInitializationEvents();
1696 
1697         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
1698         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
1699                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
1700                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
1701                 any(Message.class));
1702         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
1703 
1704         logd("Sending EVENT_NETWORK_STATUS_CHANGED false");
1705         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NETWORK_STATUS_CHANGED,
1706                 NetworkAgent.INVALID_NETWORK, 1, null));
1707         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1708 
1709         // expected to get preferred network type
1710         verify(mSST, times(1)).powerOffRadioSafely();
1711     }
1712 
verifyDataEnabledChangedMessage(boolean enabled, int reason)1713     private void verifyDataEnabledChangedMessage(boolean enabled, int reason) {
1714         verify(mHandler, times(1)).sendMessageDelayed(any(), anyLong());
1715         Pair<Boolean, Integer> result = (Pair) ((AsyncResult) mMessage.obj).result;
1716         assertEquals(DATA_ENABLED_CHANGED, mMessage.what);
1717         assertEquals(enabled, result.first);
1718         assertEquals(reason, (int) result.second);
1719         clearInvocations(mHandler);
1720     }
1721 
setUpSubscriptionPlans(boolean is5GUnmetered)1722     private void setUpSubscriptionPlans(boolean is5GUnmetered) throws Exception {
1723         List<SubscriptionPlan> plans = new ArrayList<>();
1724         if (is5GUnmetered) {
1725             plans.add(SubscriptionPlan.Builder
1726                     .createRecurring(ZonedDateTime.parse("2007-03-14T00:00:00.000Z"),
1727                             Period.ofMonths(1))
1728                     .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED,
1729                             SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED)
1730                     .setNetworkTypes(new int[] {TelephonyManager.NETWORK_TYPE_NR})
1731                     .build());
1732         }
1733         plans.add(SubscriptionPlan.Builder
1734                 .createRecurring(ZonedDateTime.parse("2007-03-14T00:00:00.000Z"),
1735                         Period.ofMonths(1))
1736                 .setDataLimit(1_000_000_000, SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED)
1737                 .setDataUsage(500_000_000, System.currentTimeMillis())
1738                 .build());
1739         replaceInstance(DcTracker.class, "mSubscriptionPlans", mDct, plans);
1740     }
1741 
isNetworkTypeUnmetered(int networkType)1742     private boolean isNetworkTypeUnmetered(int networkType) throws Exception {
1743         Method method = DcTracker.class.getDeclaredMethod(
1744                 "isNetworkTypeUnmetered", int.class);
1745         method.setAccessible(true);
1746         return (boolean) method.invoke(mDct, networkType);
1747     }
1748 
setUpDataConnection()1749     private int setUpDataConnection() throws Exception {
1750         Field dc = DcTracker.class.getDeclaredField("mDataConnections");
1751         dc.setAccessible(true);
1752         Field uig = DcTracker.class.getDeclaredField("mUniqueIdGenerator");
1753         uig.setAccessible(true);
1754         int id = ((AtomicInteger) uig.get(mDct)).getAndIncrement();
1755         ((HashMap<Integer, DataConnection>) dc.get(mDct)).put(id, mDataConnection);
1756         return id;
1757     }
1758 
resetDataConnection(int id)1759     private void resetDataConnection(int id) throws Exception {
1760         Field dc = DcTracker.class.getDeclaredField("mDataConnections");
1761         dc.setAccessible(true);
1762         ((HashMap<Integer, DataConnection>) dc.get(mDct)).remove(id);
1763     }
1764 
setUpWatchdogTimer()1765     private void setUpWatchdogTimer() {
1766         // Watchdog active for 10s
1767         mBundle.putLong(CarrierConfigManager.KEY_5G_WATCHDOG_TIME_MS_LONG, 10000);
1768         Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
1769         intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, mPhone.getPhoneId());
1770         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, mPhone.getSubId());
1771         mContext.sendBroadcast(intent);
1772         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1773     }
1774 
getWatchdogStatus()1775     private boolean getWatchdogStatus() throws Exception {
1776         Field field = DcTracker.class.getDeclaredField(("mWatchdog"));
1777         field.setAccessible(true);
1778         return (boolean) field.get(mDct);
1779     }
1780 
1781     @Test
testIsNetworkTypeUnmetered()1782     public void testIsNetworkTypeUnmetered() throws Exception {
1783         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1784 
1785         // only 5G unmetered
1786         setUpSubscriptionPlans(true);
1787 
1788         assertTrue(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_NR));
1789         assertFalse(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_LTE));
1790         assertFalse(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_UNKNOWN));
1791 
1792         // all network types metered
1793         setUpSubscriptionPlans(false);
1794 
1795         assertFalse(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_NR));
1796         assertFalse(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_LTE));
1797         assertFalse(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_UNKNOWN));
1798 
1799         // all network types unmetered
1800         List<SubscriptionPlan> plans = new ArrayList<>();
1801         plans.add(SubscriptionPlan.Builder
1802                 .createRecurring(ZonedDateTime.parse("2007-03-14T00:00:00.000Z"),
1803                         Period.ofMonths(1))
1804                 .setTitle("Some 5GB Plan")
1805                 .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED,
1806                         SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED)
1807                 .build());
1808         replaceInstance(DcTracker.class, "mSubscriptionPlans", mDct, plans);
1809 
1810         assertTrue(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_NR));
1811         assertTrue(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_LTE));
1812         assertTrue(isNetworkTypeUnmetered(TelephonyManager.NETWORK_TYPE_UNKNOWN));
1813     }
1814 
1815     @Test
testIsFrequencyRangeUnmetered()1816     public void testIsFrequencyRangeUnmetered() throws Exception {
1817         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1818         int id = setUpDataConnection();
1819         setUpSubscriptionPlans(false);
1820         setUpWatchdogTimer();
1821         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1822                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA))
1823                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1824         doReturn(1).when(mPhone).getSubId();
1825 
1826         // NetCapability should be metered when connected to 5G with no unmetered plan or frequency
1827         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1828         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1829         verify(mDataConnection, times(1)).onMeterednessChanged(false);
1830 
1831         // Set MMWAVE frequency to unmetered
1832         mBundle.putBoolean(CarrierConfigManager.KEY_UNMETERED_NR_NSA_MMWAVE_BOOL, true);
1833         Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
1834         intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, mPhone.getPhoneId());
1835         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, mPhone.getSubId());
1836         mContext.sendBroadcast(intent);
1837         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1838 
1839         // NetCapability should switch to unmetered when fr=MMWAVE and MMWAVE unmetered
1840         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1841                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE))
1842                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1843         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1844         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1845         verify(mDataConnection, times(1)).onMeterednessChanged(true);
1846 
1847         // NetCapability should switch to metered when fr=SUB6 and MMWAVE unmetered
1848         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1849                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA))
1850                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1851         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1852         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1853         verify(mDataConnection, times(2)).onMeterednessChanged(false);
1854 
1855         // Set SUB6 frequency to unmetered
1856         doReturn(2).when(mPhone).getSubId();
1857         mBundle.putBoolean(CarrierConfigManager.KEY_UNMETERED_NR_NSA_SUB6_BOOL, true);
1858         intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
1859         intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, mPhone.getPhoneId());
1860         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, mPhone.getSubId());
1861         mContext.sendBroadcast(intent);
1862         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1863 
1864         // NetCapability should switch to unmetered when fr=SUB6 and SUB6 unmetered
1865         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1866         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1867         // Data connection is running on a different thread. Have to wait.
1868         waitForMs(200);
1869         verify(mDataConnection, times(2)).onMeterednessChanged(true);
1870 
1871         resetDataConnection(id);
1872     }
1873 
1874     @Test
testReevaluateUnmeteredConnectionsOnNetworkChange()1875     public void testReevaluateUnmeteredConnectionsOnNetworkChange() throws Exception {
1876         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1877         int id = setUpDataConnection();
1878         setUpSubscriptionPlans(true);
1879         setUpWatchdogTimer();
1880 
1881         // NetCapability should be unmetered when connected to 5G
1882         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1883                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA))
1884                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1885         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1886         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1887         verify(mDataConnection, times(1)).onMeterednessChanged(true);
1888 
1889         // NetCapability should be metered when disconnected from 5G
1890         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1891                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE))
1892                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1893         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1894         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1895         // Data connection is running on a different thread. Have to wait.
1896         waitForMs(200);
1897         verify(mDataConnection, times(1)).onMeterednessChanged(false);
1898 
1899         resetDataConnection(id);
1900     }
1901 
1902     @Test
testReevaluateUnmeteredConnectionsOnWatchdog()1903     public void testReevaluateUnmeteredConnectionsOnWatchdog() throws Exception {
1904         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
1905         int id = setUpDataConnection();
1906         setUpSubscriptionPlans(true);
1907         setUpWatchdogTimer();
1908 
1909         // Watchdog inactive when unmetered and not connected to 5G
1910         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1911                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE))
1912                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1913         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_NR_TIMER_WATCHDOG));
1914         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1915         assertFalse(getWatchdogStatus());
1916 
1917         // Watchdog active when unmetered and connected to 5G
1918         doReturn(new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_LTE,
1919                 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA))
1920                 .when(mDisplayInfoController).getTelephonyDisplayInfo();
1921         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1922         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1923         assertTrue(getWatchdogStatus());
1924 
1925         // Watchdog inactive when metered
1926         setUpSubscriptionPlans(false);
1927         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TELEPHONY_DISPLAY_INFO_CHANGED));
1928         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
1929         assertFalse(getWatchdogStatus());
1930 
1931         resetDataConnection(id);
1932     }
1933 
1934     /**
1935      * Test if this is a path prefix match against the given Uri. Verifies that
1936      * scheme, authority, and atomic path segments match.
1937      *
1938      * Copied from frameworks/base/core/java/android/net/Uri.java
1939      */
isPathPrefixMatch(Uri uriA, Uri uriB)1940     private boolean isPathPrefixMatch(Uri uriA, Uri uriB) {
1941         if (!Objects.equals(uriA.getScheme(), uriB.getScheme())) return false;
1942         if (!Objects.equals(uriA.getAuthority(), uriB.getAuthority())) return false;
1943 
1944         List<String> segA = uriA.getPathSegments();
1945         List<String> segB = uriB.getPathSegments();
1946 
1947         final int size = segB.size();
1948         if (segA.size() < size) return false;
1949 
1950         for (int i = 0; i < size; i++) {
1951             if (!Objects.equals(segA.get(i), segB.get(i))) {
1952                 return false;
1953             }
1954         }
1955 
1956         return true;
1957     }
1958 
1959     /*@Test
1960     public void testNoApnContextsWhenDataIsDisabled() throws java.lang.InterruptedException {
1961         //Check that apn contexts are loaded.
1962         assertTrue(mDct.getApnContexts().size() > 0);
1963 
1964         //Do work normally done in teardown.
1965         mDct.removeCallbacksAndMessages(null);
1966         mDcTrackerTestHandler.quit();
1967         mDcTrackerTestHandler.join();
1968 
1969         //Set isDataCapable to false for the new DcTracker being created in DcTrackerTestHandler.
1970         doReturn(false).when(mTelephonyManager).isDataCapable();
1971         mDcTrackerTestHandler = new DcTrackerTestHandler(getClass().getSimpleName());
1972         setReady(false);
1973 
1974         mDcTrackerTestHandler.start();
1975         waitUntilReady();
1976         assertEquals(0, mDct.getApnContexts().size());
1977 
1978         //No need to clean up handler because that work is done in teardown.
1979     }*/
1980 
1981     @Test
testRatChanged()1982     public void testRatChanged() throws Exception {
1983         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
1984 
1985         DataConnectionReasons dataConnectionReasons = new DataConnectionReasons();
1986         boolean allowed = isDataAllowed(dataConnectionReasons);
1987         assertFalse(dataConnectionReasons.toString(), allowed);
1988 
1989         logd("Sending EVENT_ENABLE_APN");
1990         // APN id 0 is APN_TYPE_DEFAULT
1991         mDct.enableApn(ApnSetting.TYPE_DEFAULT, DcTracker.REQUEST_TYPE_NORMAL, null);
1992 
1993         sendInitializationEvents();
1994 
1995         dataConnectionReasons = new DataConnectionReasons();
1996         allowed = isDataAllowed(dataConnectionReasons);
1997         assertTrue(dataConnectionReasons.toString(), allowed);
1998 
1999         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
2000         // Verify if RIL command was sent properly.
2001         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
2002                 eq(AccessNetworkType.EUTRAN), dpCaptor.capture(),
2003                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
2004                 any(Message.class));
2005         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 21, 1, NETWORK_TYPE_LTE_BITMASK);
2006 
2007         verifyDataConnected(FAKE_APN1);
2008 
2009         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_UMTS).when(mServiceState)
2010                 .getRilDataRadioTechnology();
2011 
2012         logd("Sending EVENT_DATA_RAT_CHANGED");
2013         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
2014                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_UMTS)
2015                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
2016                 .build();
2017         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
2018                 anyInt(), anyInt());
2019         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
2020         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
2021 
2022         // Data connection is running on a different thread. Have to wait.
2023         waitForMs(200);
2024         // expected tear down all metered DataConnections
2025         verify(mSimulatedCommandsVerifier).deactivateDataCall(
2026                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
2027                 any(Message.class));
2028     }
2029 
2030     @Test
testApnConfigRepositoryUpdatedOnCarrierConfigChange()2031     public void testApnConfigRepositoryUpdatedOnCarrierConfigChange() throws Exception {
2032         assertPriority(ApnSetting.TYPE_CBS_STRING, 2);
2033         assertPriority(ApnSetting.TYPE_MMS_STRING, 2);
2034 
2035         mBundle.putStringArray(CarrierConfigManager.KEY_APN_PRIORITY_STRING_ARRAY,
2036                 new String[] {
2037                         ApnSetting.TYPE_CBS_STRING + ":11",
2038                         ApnSetting.TYPE_MMS_STRING + ":19",
2039                 });
2040 
2041         sendInitializationEvents();
2042 
2043         Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
2044         intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, mPhone.getPhoneId());
2045         intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, mPhone.getSubId());
2046         mContext.sendBroadcast(intent);
2047         waitForLastHandlerAction(mDcTrackerTestHandler.getThreadHandler());
2048 
2049         assertPriority(ApnSetting.TYPE_CBS_STRING, 11);
2050         assertPriority(ApnSetting.TYPE_MMS_STRING, 19);
2051 
2052         //Ensure apns are in sorted order.
2053         ApnContext lastApnContext = null;
2054         for (ApnContext apnContext : mDct.getApnContexts()) {
2055             if (lastApnContext != null) {
2056                 assertTrue(apnContext.getPriority() <= lastApnContext.getPriority());
2057             }
2058             lastApnContext = apnContext;
2059         }
2060     }
2061 
assertPriority(String type, int priority)2062     private void assertPriority(String type, int priority) {
2063         assertEquals(priority, mDct.getApnContexts().stream()
2064                 .filter(x -> x.getApnType().equals(type))
2065                 .findFirst().get().getPriority());
2066     }
2067 }
2068