Home | History | Annotate | Download | only in dataconnection
      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.doAnswer;
     33 import static org.mockito.Mockito.doReturn;
     34 import static org.mockito.Mockito.never;
     35 import static org.mockito.Mockito.times;
     36 import static org.mockito.Mockito.verify;
     37 
     38 import android.app.AlarmManager;
     39 import android.app.PendingIntent;
     40 import android.content.ContentResolver;
     41 import android.content.ContentValues;
     42 import android.content.Context;
     43 import android.content.Intent;
     44 import android.content.IntentFilter;
     45 import android.content.pm.ServiceInfo;
     46 import android.database.Cursor;
     47 import android.database.MatrixCursor;
     48 import android.hardware.radio.V1_0.SetupDataCallResult;
     49 import android.net.LinkProperties;
     50 import android.net.NetworkCapabilities;
     51 import android.net.NetworkRequest;
     52 import android.net.Uri;
     53 import android.os.AsyncResult;
     54 import android.os.HandlerThread;
     55 import android.os.IBinder;
     56 import android.os.Message;
     57 import android.os.PersistableBundle;
     58 import android.provider.Settings;
     59 import android.provider.Telephony;
     60 import android.support.test.filters.FlakyTest;
     61 import android.telephony.AccessNetworkConstants.TransportType;
     62 import android.telephony.CarrierConfigManager;
     63 import android.telephony.ServiceState;
     64 import android.telephony.SubscriptionInfo;
     65 import android.telephony.SubscriptionManager;
     66 import android.telephony.TelephonyManager;
     67 import android.telephony.data.DataProfile;
     68 import android.telephony.data.DataService;
     69 import android.test.mock.MockContentProvider;
     70 import android.test.mock.MockContentResolver;
     71 import android.test.suitebuilder.annotation.MediumTest;
     72 import android.test.suitebuilder.annotation.SmallTest;
     73 import android.util.LocalLog;
     74 
     75 import com.android.internal.R;
     76 import com.android.internal.telephony.DctConstants;
     77 import com.android.internal.telephony.ISub;
     78 import com.android.internal.telephony.Phone;
     79 import com.android.internal.telephony.PhoneConstants;
     80 import com.android.internal.telephony.TelephonyTest;
     81 import com.android.server.pm.PackageManagerService;
     82 
     83 import org.junit.After;
     84 import org.junit.Before;
     85 import org.junit.Ignore;
     86 import org.junit.Test;
     87 import org.mockito.ArgumentCaptor;
     88 import org.mockito.Mock;
     89 import org.mockito.invocation.InvocationOnMock;
     90 import org.mockito.stubbing.Answer;
     91 
     92 import java.lang.reflect.Method;
     93 import java.util.ArrayList;
     94 import java.util.Arrays;
     95 import java.util.List;
     96 import java.util.regex.Matcher;
     97 import java.util.regex.Pattern;
     98 
     99 public class DcTrackerTest extends TelephonyTest {
    100 
    101     private final static String[] sNetworkAttributes = new String[]{
    102             "mobile,0,0,0,-1,true", "mobile_mms,2,0,2,60000,true",
    103             "mobile_supl,3,0,2,60000,true", "mobile_dun,4,0,2,60000,true",
    104             "mobile_hipri,5,0,3,60000,true", "mobile_fota,10,0,2,60000,true",
    105             "mobile_ims,11,0,2,60000,true", "mobile_cbs,12,0,2,60000,true",
    106             "mobile_ia,14,0,2,-1,true", "mobile_emergency,15,0,2,-1,true"};
    107 
    108     private final static List<String> sApnTypes = Arrays.asList(
    109             "default", "mms", "cbs", "fota", "supl", "ia", "emergency", "dun", "hipri", "ims");
    110     private static final int LTE_BEARER_BITMASK = 1 << (ServiceState.RIL_RADIO_TECHNOLOGY_LTE - 1);
    111     private static final int EHRPD_BEARER_BITMASK =
    112             1 << (ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD - 1);
    113     public static final String FAKE_APN1 = "FAKE APN 1";
    114     public static final String FAKE_APN2 = "FAKE APN 2";
    115     public static final String FAKE_APN3 = "FAKE APN 3";
    116     public static final String FAKE_APN4 = "FAKE APN 4";
    117     public static final String FAKE_APN5 = "FAKE APN 5";
    118     public static final String FAKE_IFNAME = "FAKE IFNAME";
    119     public static final String FAKE_PCSCF_ADDRESS = "22.33.44.55";
    120     public static final String FAKE_GATEWAY = "11.22.33.44";
    121     public static final String FAKE_DNS = "55.66.77.88";
    122     public static final String FAKE_ADDRESS = "99.88.77.66";
    123     private static final int NETWORK_TYPE_LTE_BITMASK =
    124             1 << (TelephonyManager.NETWORK_TYPE_LTE - 1);
    125     private static final int NETWORK_TYPE_EHRPD_BITMASK =
    126             1 << (TelephonyManager.NETWORK_TYPE_EHRPD - 1);
    127     private static final Uri PREFERAPN_URI = Uri.parse(
    128             Telephony.Carriers.CONTENT_URI + "/preferapn");
    129 
    130 
    131     @Mock
    132     ISub mIsub;
    133     @Mock
    134     IBinder mBinder;
    135     @Mock
    136     NetworkRequest mNetworkRequest;
    137     @Mock
    138     SubscriptionInfo mSubscriptionInfo;
    139     @Mock
    140     ApnContext mApnContext;
    141     @Mock
    142     ApnSetting mApnSetting;
    143     @Mock
    144     DcAsyncChannel mDcac;
    145     @Mock
    146     PackageManagerService mMockPackageManagerInternal;
    147 
    148     private DcTracker mDct;
    149     private DcTrackerTestHandler mDcTrackerTestHandler;
    150 
    151     private AlarmManager mAlarmManager;
    152 
    153     private PersistableBundle mBundle;
    154 
    155     private SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangedListener;
    156 
    157     private final ApnSettingContentProvider mApnSettingContentProvider =
    158             new ApnSettingContentProvider();
    159 
    160     private void addDataService() {
    161         CellularDataService cellularDataService = new CellularDataService();
    162         ServiceInfo serviceInfo = new ServiceInfo();
    163         serviceInfo.packageName = "com.android.phone";
    164         serviceInfo.permission = "android.permission.BIND_DATA_SERVICE";
    165         IntentFilter filter = new IntentFilter();
    166         mContextFixture.addService(
    167                 DataService.DATA_SERVICE_INTERFACE,
    168                 null,
    169                 "com.android.phone",
    170                 cellularDataService.mBinder,
    171                 serviceInfo,
    172                 filter);
    173     }
    174 
    175     private class DcTrackerTestHandler extends HandlerThread {
    176 
    177         private DcTrackerTestHandler(String name) {
    178             super(name);
    179         }
    180 
    181         @Override
    182         public void onLooperPrepared() {
    183             mDct = new DcTracker(mPhone, TransportType.WWAN);
    184             setReady(true);
    185         }
    186     }
    187 
    188     private class ApnSettingContentProvider extends MockContentProvider {
    189         private int mPreferredApnSet = 0;
    190 
    191         @Override
    192         public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
    193                             String sortOrder) {
    194             logd("ApnSettingContentProvider: query");
    195             logd("   uri = " + uri);
    196             logd("   projection = " + Arrays.toString(projection));
    197             logd("   selection = " + selection);
    198             logd("   selectionArgs = " + Arrays.toString(selectionArgs));
    199             logd("   sortOrder = " + sortOrder);
    200 
    201             if (uri.compareTo(Telephony.Carriers.CONTENT_URI) == 0
    202                     || uri.compareTo(Uri.withAppendedPath(
    203                             Telephony.Carriers.CONTENT_URI, "filtered")) == 0) {
    204                 if (projection == null && selectionArgs == null && selection != null) {
    205 
    206                     Pattern pattern = Pattern.compile("^numeric = '([0-9]*)'");
    207                     Matcher matcher = pattern.matcher(selection);
    208                     if (!matcher.find()) {
    209                         logd("Cannot find MCC/MNC from " + selection);
    210                         return null;
    211                     }
    212 
    213                     String plmn = matcher.group(1);
    214 
    215                     logd("Query '" + plmn + "' APN settings");
    216                     MatrixCursor mc = new MatrixCursor(
    217                             new String[]{Telephony.Carriers._ID, Telephony.Carriers.NUMERIC,
    218                                     Telephony.Carriers.NAME, Telephony.Carriers.APN,
    219                                     Telephony.Carriers.PROXY, Telephony.Carriers.PORT,
    220                                     Telephony.Carriers.MMSC, Telephony.Carriers.MMSPROXY,
    221                                     Telephony.Carriers.MMSPORT, Telephony.Carriers.USER,
    222                                     Telephony.Carriers.PASSWORD, Telephony.Carriers.AUTH_TYPE,
    223                                     Telephony.Carriers.TYPE,
    224                                     Telephony.Carriers.PROTOCOL,
    225                                     Telephony.Carriers.ROAMING_PROTOCOL,
    226                                     Telephony.Carriers.CARRIER_ENABLED, Telephony.Carriers.BEARER,
    227                                     Telephony.Carriers.BEARER_BITMASK,
    228                                     Telephony.Carriers.PROFILE_ID,
    229                                     Telephony.Carriers.MODEM_COGNITIVE,
    230                                     Telephony.Carriers.MAX_CONNS, Telephony.Carriers.WAIT_TIME,
    231                                     Telephony.Carriers.MAX_CONNS_TIME, Telephony.Carriers.MTU,
    232                                     Telephony.Carriers.MVNO_TYPE,
    233                                     Telephony.Carriers.MVNO_MATCH_DATA,
    234                                     Telephony.Carriers.NETWORK_TYPE_BITMASK,
    235                                     Telephony.Carriers.APN_SET_ID});
    236 
    237                     mc.addRow(new Object[]{
    238                             2163,                   // id
    239                             plmn,                   // numeric
    240                             "sp-mode",              // name
    241                             FAKE_APN1,              // apn
    242                             "",                     // proxy
    243                             "",                     // port
    244                             "",                     // mmsc
    245                             "",                     // mmsproxy
    246                             "",                     // mmsport
    247                             "",                     // user
    248                             "",                     // password
    249                             -1,                     // authtype
    250                             "default,supl",         // types
    251                             "IP",                   // protocol
    252                             "IP",                   // roaming_protocol
    253                             1,                      // carrier_enabled
    254                             ServiceState.RIL_RADIO_TECHNOLOGY_LTE, // bearer
    255                             0,                      // bearer_bitmask
    256                             0,                      // profile_id
    257                             0,                      // modem_cognitive
    258                             0,                      // max_conns
    259                             0,                      // wait_time
    260                             0,                      // max_conns_time
    261                             0,                      // mtu
    262                             "",                     // mvno_type
    263                             "",                     // mnvo_match_data
    264                             NETWORK_TYPE_LTE_BITMASK, // network_type_bitmask
    265                             0                       // apn_set_id
    266                     });
    267 
    268                     mc.addRow(new Object[]{
    269                             2164,                   // id
    270                             plmn,                   // numeric
    271                             "mopera U",             // name
    272                             FAKE_APN2,              // apn
    273                             "",                     // proxy
    274                             "",                     // port
    275                             "",                     // mmsc
    276                             "",                     // mmsproxy
    277                             "",                     // mmsport
    278                             "",                     // user
    279                             "",                     // password
    280                             -1,                     // authtype
    281                             "default,supl",         // types
    282                             "IP",                   // protocol
    283                             "IP",                   // roaming_protocol
    284                             1,                      // carrier_enabled
    285                             ServiceState.RIL_RADIO_TECHNOLOGY_LTE, // bearer,
    286                             0,                      // bearer_bitmask
    287                             0,                      // profile_id
    288                             0,                      // modem_cognitive
    289                             0,                      // max_conns
    290                             0,                      // wait_time
    291                             0,                      // max_conns_time
    292                             0,                      // mtu
    293                             "",                     // mvno_type
    294                             "",                     // mnvo_match_data
    295                             NETWORK_TYPE_LTE_BITMASK, // network_type_bitmask
    296                             0                       // apn_set_id
    297                     });
    298 
    299                     mc.addRow(new Object[]{
    300                             2165,                   // id
    301                             plmn,                   // numeric
    302                             "b-mobile for Nexus",   // name
    303                             FAKE_APN3,              // apn
    304                             "",                     // proxy
    305                             "",                     // port
    306                             "",                     // mmsc
    307                             "",                     // mmsproxy
    308                             "",                     // mmsport
    309                             "",                     // user
    310                             "",                     // password
    311                             -1,                     // authtype
    312                             "ims",                  // types
    313                             "IP",                   // protocol
    314                             "IP",                   // roaming_protocol
    315                             1,                      // carrier_enabled
    316                             0,                      // bearer
    317                             0,                      // bearer_bitmask
    318                             0,                      // profile_id
    319                             0,                      // modem_cognitive
    320                             0,                      // max_conns
    321                             0,                      // wait_time
    322                             0,                      // max_conns_time
    323                             0,                      // mtu
    324                             "",                     // mvno_type
    325                             "",                     // mnvo_match_data
    326                             0,                      // network_type_bitmask
    327                             0                       // apn_set_id
    328                     });
    329 
    330                     mc.addRow(new Object[]{
    331                             2166,                   // id
    332                             plmn,                   // numeric
    333                             "sp-mode ehrpd",        // name
    334                             FAKE_APN4,              // apn
    335                             "",                     // proxy
    336                             "",                     // port
    337                             "",                     // mmsc
    338                             "",                     // mmsproxy
    339                             "",                     // mmsport
    340                             "",                     // user
    341                             "",                     // password
    342                             -1,                     // authtype
    343                             "default,supl",         // types
    344                             "IP",                   // protocol
    345                             "IP",                   // roaming_protocol
    346                             1,                      // carrier_enabled
    347                             ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD, // bearer
    348                             0,                      // bearer_bitmask
    349                             0,                      // profile_id
    350                             0,                      // modem_cognitive
    351                             0,                      // max_conns
    352                             0,                      // wait_time
    353                             0,                      // max_conns_time
    354                             0,                      // mtu
    355                             "",                     // mvno_type
    356                             "",                     // mnvo_match_data
    357                             NETWORK_TYPE_EHRPD_BITMASK, // network_type_bitmask
    358                             0                       // apn_set_id
    359                     });
    360 
    361                     mc.addRow(new Object[]{
    362                             2166,                   // id
    363                             plmn,                   // numeric
    364                             "b-mobile for Nexus",   // name
    365                             FAKE_APN5,              // apn
    366                             "",                     // proxy
    367                             "",                     // port
    368                             "",                     // mmsc
    369                             "",                     // mmsproxy
    370                             "",                     // mmsport
    371                             "",                     // user
    372                             "",                     // password
    373                             -1,                     // authtype
    374                             "dun",                  // types
    375                             "IP",                   // protocol
    376                             "IP",                   // roaming_protocol
    377                             1,                      // carrier_enabled
    378                             0,                      // bearer
    379                             0,                      // bearer_bitmask
    380                             0,                      // profile_id
    381                             0,                      // modem_cognitive
    382                             0,                      // max_conns
    383                             0,                      // wait_time
    384                             0,                      // max_conns_time
    385                             0,                      // mtu
    386                             "",                     // mvno_type
    387                             "",                     // mnvo_match_data
    388                             0,                      // network_type_bitmask
    389                             0                       // apn_set_id
    390                     });
    391 
    392                     return mc;
    393                 }
    394             } else if (uri.isPathPrefixMatch(
    395                     Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "preferapnset"))) {
    396                 MatrixCursor mc = new MatrixCursor(
    397                         new String[]{Telephony.Carriers.APN_SET_ID});
    398                 // apn_set_id is the only field used with this URL
    399                 mc.addRow(new Object[]{ mPreferredApnSet });
    400                 mc.addRow(new Object[]{ 0 });
    401                 return mc;
    402             }
    403 
    404             return null;
    405         }
    406 
    407         @Override
    408         public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
    409             mPreferredApnSet = values.getAsInteger(Telephony.Carriers.APN_SET_ID);
    410             return 1;
    411         }
    412     }
    413 
    414     @Before
    415     public void setUp() throws Exception {
    416         logd("DcTrackerTest +Setup!");
    417         super.setUp(getClass().getSimpleName());
    418 
    419         doReturn("fake.action_detached").when(mPhone).getActionDetached();
    420         doReturn("fake.action_attached").when(mPhone).getActionAttached();
    421         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_LTE).when(mServiceState)
    422                 .getRilDataRadioTechnology();
    423         doReturn("44010").when(mSimRecords).getOperatorNumeric();
    424 
    425         mContextFixture.putStringArrayResource(com.android.internal.R.array.networkAttributes,
    426                 sNetworkAttributes);
    427         mContextFixture.putStringArrayResource(com.android.internal.R.array.
    428                 config_mobile_tcp_buffers, new String[]{
    429                 "umts:131072,262144,1452032,4096,16384,399360",
    430                 "hspa:131072,262144,2441216,4096,16384,399360",
    431                 "hsupa:131072,262144,2441216,4096,16384,399360",
    432                 "hsdpa:131072,262144,2441216,4096,16384,399360",
    433                 "hspap:131072,262144,2441216,4096,16384,399360",
    434                 "edge:16384,32768,131072,4096,16384,65536",
    435                 "gprs:4096,8192,24576,4096,8192,24576",
    436                 "1xrtt:16384,32768,131070,4096,16384,102400",
    437                 "evdo:131072,262144,1048576,4096,16384,524288",
    438                 "lte:524288,1048576,8388608,262144,524288,4194304"});
    439 
    440         mContextFixture.putResource(R.string.config_wwan_data_service_package,
    441                 "com.android.phone");
    442 
    443         ((MockContentResolver) mContext.getContentResolver()).addProvider(
    444                 Telephony.Carriers.CONTENT_URI.getAuthority(), mApnSettingContentProvider);
    445 
    446         doReturn(true).when(mSimRecords).getRecordsLoaded();
    447         doReturn(PhoneConstants.State.IDLE).when(mCT).getState();
    448         doReturn(true).when(mSST).getDesiredPowerState();
    449         doReturn(true).when(mSST).getPowerStateFromCarrier();
    450         doAnswer(
    451                 new Answer<Void>() {
    452                     @Override
    453                     public Void answer(InvocationOnMock invocation) throws Throwable {
    454                         mOnSubscriptionsChangedListener =
    455                                 (SubscriptionManager.OnSubscriptionsChangedListener)
    456                                         invocation.getArguments()[0];
    457                         return null;
    458                     }
    459                 }
    460         ).when(mSubscriptionManager).addOnSubscriptionsChangedListener(any());
    461         doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(anyInt());
    462 
    463         doReturn(1).when(mIsub).getDefaultDataSubId();
    464         doReturn(mIsub).when(mBinder).queryLocalInterface(anyString());
    465         mServiceManagerMockedServices.put("isub", mBinder);
    466         mServiceManagerMockedServices.put("package", mMockPackageManagerInternal);
    467 
    468         mContextFixture.putStringArrayResource(
    469                 com.android.internal.R.array.config_cell_retries_per_error_code,
    470                 new String[]{"36,2"});
    471 
    472         mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    473         mBundle = mContextFixture.getCarrierConfigBundle();
    474 
    475         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
    476         addDataService();
    477 
    478         mDcTrackerTestHandler = new DcTrackerTestHandler(getClass().getSimpleName());
    479         mDcTrackerTestHandler.start();
    480         waitUntilReady();
    481         waitForMs(600);
    482         logd("DcTrackerTest -Setup!");
    483     }
    484 
    485     @After
    486     public void tearDown() throws Exception {
    487         logd("DcTrackerTest -tearDown");
    488         mDct.removeCallbacksAndMessages(null);
    489         mDct = null;
    490         mDcTrackerTestHandler.quit();
    491         super.tearDown();
    492     }
    493 
    494     // Create a successful data response
    495     private static SetupDataCallResult createSetupDataCallResult() throws Exception {
    496         SetupDataCallResult result = new SetupDataCallResult();
    497         result.status = 0;
    498         result.suggestedRetryTime = -1;
    499         result.cid = 1;
    500         result.active = 2;
    501         result.type = "IP";
    502         result.ifname = FAKE_IFNAME;
    503         result.addresses = FAKE_ADDRESS;
    504         result.dnses = FAKE_DNS;
    505         result.gateways = FAKE_GATEWAY;
    506         result.pcscf = FAKE_PCSCF_ADDRESS;
    507         result.mtu = 1440;
    508         return result;
    509     }
    510 
    511     private void verifyDataProfile(DataProfile dp, String apn, int profileId,
    512                                    int supportedApnTypesBitmap, int type, int bearerBitmask) {
    513         assertEquals(profileId, dp.getProfileId());
    514         assertEquals(apn, dp.getApn());
    515         assertEquals("IP", dp.getProtocol());
    516         assertEquals(0, dp.getAuthType());
    517         assertEquals("", dp.getUserName());
    518         assertEquals("", dp.getPassword());
    519         assertEquals(type, dp.getType());
    520         assertEquals(0, dp.getMaxConnsTime());
    521         assertEquals(0, dp.getMaxConns());
    522         assertEquals(0, dp.getWaitTime());
    523         assertTrue(dp.isEnabled());
    524         assertEquals(supportedApnTypesBitmap, dp.getSupportedApnTypesBitmap());
    525         assertEquals("IP", dp.getRoamingProtocol());
    526         assertEquals(bearerBitmask, dp.getBearerBitmap());
    527         assertEquals(0, dp.getMtu());
    528         assertEquals("", dp.getMvnoType());
    529         assertEquals("", dp.getMvnoMatchData());
    530         assertFalse(dp.isModemCognitive());
    531     }
    532 
    533     private void verifyDataConnected(final String apnSetting) {
    534         verify(mPhone, times(1)).notifyDataConnection(eq(Phone.REASON_CONNECTED),
    535                 eq(PhoneConstants.APN_TYPE_DEFAULT));
    536 
    537         verify(mAlarmManager, times(1)).set(eq(AlarmManager.ELAPSED_REALTIME), anyLong(),
    538                 any(PendingIntent.class));
    539 
    540         assertEquals(apnSetting, mDct.getActiveApnString(PhoneConstants.APN_TYPE_DEFAULT));
    541         assertArrayEquals(new String[]{PhoneConstants.APN_TYPE_DEFAULT}, mDct.getActiveApnTypes());
    542 
    543         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
    544         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
    545 
    546         LinkProperties linkProperties = mDct.getLinkProperties(PhoneConstants.APN_TYPE_DEFAULT);
    547         assertEquals(FAKE_IFNAME, linkProperties.getInterfaceName());
    548         assertEquals(1, linkProperties.getAddresses().size());
    549         assertEquals(FAKE_ADDRESS, linkProperties.getAddresses().get(0).getHostAddress());
    550         assertEquals(1, linkProperties.getDnsServers().size());
    551         assertEquals(FAKE_DNS, linkProperties.getDnsServers().get(0).getHostAddress());
    552         assertEquals(FAKE_GATEWAY, linkProperties.getRoutes().get(0).getGateway().getHostAddress());
    553     }
    554 
    555     private boolean isDataAllowed(DataConnectionReasons dataConnectionReasons) {
    556         try {
    557             Method method = DcTracker.class.getDeclaredMethod("isDataAllowed",
    558                     DataConnectionReasons.class);
    559             method.setAccessible(true);
    560             return (boolean) method.invoke(mDct, dataConnectionReasons);
    561         } catch (Exception e) {
    562             fail(e.toString());
    563             return false;
    564         }
    565     }
    566 
    567     // Test the normal data call setup scenario.
    568     @Test
    569     @MediumTest
    570     public void testDataSetup() throws Exception {
    571 
    572         mDct.setUserDataEnabled(true);
    573 
    574         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
    575 
    576         DataConnectionReasons dataConnectionReasons = new DataConnectionReasons();
    577         boolean allowed = isDataAllowed(dataConnectionReasons);
    578         assertFalse(dataConnectionReasons.toString(), allowed);
    579 
    580         logd("Sending EVENT_RECORDS_LOADED");
    581         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
    582         waitForMs(200);
    583 
    584         ArgumentCaptor<String> apnTypeArgumentCaptor = ArgumentCaptor.forClass(String.class);
    585         verify(mPhone, times(sNetworkAttributes.length)).notifyDataConnection(
    586                 eq(Phone.REASON_SIM_LOADED), apnTypeArgumentCaptor.capture(),
    587                 eq(PhoneConstants.DataState.DISCONNECTED));
    588 
    589         assertEquals(sApnTypes, apnTypeArgumentCaptor.getAllValues());
    590 
    591         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
    592         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
    593         waitForMs(200);
    594 
    595         apnTypeArgumentCaptor = ArgumentCaptor.forClass(String.class);
    596         verify(mPhone, times(sNetworkAttributes.length)).notifyDataConnection(
    597                 eq(Phone.REASON_DATA_ATTACHED), apnTypeArgumentCaptor.capture(),
    598                 eq(PhoneConstants.DataState.DISCONNECTED));
    599 
    600         assertEquals(sApnTypes, apnTypeArgumentCaptor.getAllValues());
    601 
    602         apnTypeArgumentCaptor = ArgumentCaptor.forClass(String.class);
    603         verify(mPhone, times(sNetworkAttributes.length)).notifyDataConnection(
    604                 eq(Phone.REASON_DATA_ENABLED), apnTypeArgumentCaptor.capture(),
    605                 eq(PhoneConstants.DataState.DISCONNECTED));
    606 
    607         assertEquals(sApnTypes, apnTypeArgumentCaptor.getAllValues());
    608 
    609         logd("Sending EVENT_ENABLE_NEW_APN");
    610         // APN id 0 is APN_TYPE_DEFAULT
    611         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
    612         waitForMs(200);
    613 
    614         dataConnectionReasons = new DataConnectionReasons();
    615         allowed = isDataAllowed(dataConnectionReasons);
    616         assertTrue(dataConnectionReasons.toString(), allowed);
    617 
    618         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    619         // Verify if RIL command was sent properly.
    620         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
    621                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    622                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    623                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    624                 any(Message.class));
    625         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, LTE_BEARER_BITMASK);
    626 
    627         verifyDataConnected(FAKE_APN1);
    628     }
    629 
    630     // Test the scenario where the first data call setup is failed, and then retry the setup later.
    631     @Test
    632     @MediumTest
    633     public void testDataRetry() throws Exception {
    634 
    635         mDct.setUserDataEnabled(true);
    636 
    637         // LOST_CONNECTION(0x10004) is a non-permanent failure, so we'll retry data setup later.
    638         /*DataCallResponse dcResponse = new DataCallResponse(0x10004, -1, 1, 2, "IP", FAKE_IFNAME,
    639                 Arrays.asList(new LinkAddress(NetworkUtils.numericToInetAddress(FAKE_ADDRESS), 0)),
    640                 Arrays.asList(NetworkUtils.numericToInetAddress(FAKE_DNS)),
    641                 Arrays.asList(NetworkUtils.numericToInetAddress(FAKE_GATEWAY)),
    642                 Arrays.asList(FAKE_PCSCF_ADDRESS),
    643                 1440);*/
    644         SetupDataCallResult result = createSetupDataCallResult();
    645         result.status = 0x10004;
    646 
    647         // Simulate RIL fails the data call setup
    648         mSimulatedCommands.setDataCallResult(false, result);
    649 
    650         DataConnectionReasons dataConnectionReasons = new DataConnectionReasons();
    651         boolean allowed = isDataAllowed(dataConnectionReasons);
    652         assertFalse(dataConnectionReasons.toString(), allowed);
    653 
    654         logd("Sending EVENT_RECORDS_LOADED");
    655         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
    656         waitForMs(200);
    657 
    658         ArgumentCaptor<String> apnTypeArgumentCaptor = ArgumentCaptor.forClass(String.class);
    659         verify(mPhone, times(sNetworkAttributes.length)).notifyDataConnection(
    660                 eq(Phone.REASON_SIM_LOADED), apnTypeArgumentCaptor.capture(),
    661                 eq(PhoneConstants.DataState.DISCONNECTED));
    662 
    663         assertEquals(sApnTypes, apnTypeArgumentCaptor.getAllValues());
    664 
    665         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
    666         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
    667         waitForMs(200);
    668 
    669         apnTypeArgumentCaptor = ArgumentCaptor.forClass(String.class);
    670         verify(mPhone, times(sNetworkAttributes.length)).notifyDataConnection(
    671                 eq(Phone.REASON_DATA_ATTACHED), apnTypeArgumentCaptor.capture(),
    672                 eq(PhoneConstants.DataState.DISCONNECTED));
    673 
    674         assertEquals(sApnTypes, apnTypeArgumentCaptor.getAllValues());
    675 
    676         apnTypeArgumentCaptor = ArgumentCaptor.forClass(String.class);
    677         verify(mPhone, times(sNetworkAttributes.length)).notifyDataConnection(
    678                 eq(Phone.REASON_DATA_ENABLED), apnTypeArgumentCaptor.capture(),
    679                 eq(PhoneConstants.DataState.DISCONNECTED));
    680 
    681         assertEquals(sApnTypes, apnTypeArgumentCaptor.getAllValues());
    682 
    683         logd("Sending EVENT_ENABLE_NEW_APN");
    684         // APN id 0 is APN_TYPE_DEFAULT
    685         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
    686         waitForMs(200);
    687 
    688 
    689         dataConnectionReasons = new DataConnectionReasons();
    690         allowed = isDataAllowed(dataConnectionReasons);
    691         assertTrue(dataConnectionReasons.toString(), allowed);
    692 
    693         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    694         // Verify if RIL command was sent properly.
    695         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
    696                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    697                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    698                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    699                 any(Message.class));
    700         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, LTE_BEARER_BITMASK);
    701 
    702         // Make sure we never notify connected because the data call setup is supposed to fail.
    703         verify(mPhone, never()).notifyDataConnection(eq(Phone.REASON_CONNECTED),
    704                 eq(PhoneConstants.APN_TYPE_DEFAULT));
    705 
    706         // Verify the retry manger schedule another data call setup.
    707         verify(mAlarmManager, times(1)).setExact(eq(AlarmManager.ELAPSED_REALTIME_WAKEUP),
    708                 anyLong(), any(PendingIntent.class));
    709 
    710         // This time we'll let RIL command succeed.
    711         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
    712 
    713         // Simulate the timer expires.
    714         Intent intent = new Intent("com.android.internal.telephony.data-reconnect.default");
    715         intent.putExtra("reconnect_alarm_extra_type", PhoneConstants.APN_TYPE_DEFAULT);
    716         intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, 0);
    717         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    718         mContext.sendBroadcast(intent);
    719         waitForMs(200);
    720 
    721         dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    722         // Verify if RIL command was sent properly.
    723         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
    724                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    725                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    726                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    727                 any(Message.class));
    728         verifyDataProfile(dpCaptor.getValue(), FAKE_APN2, 0, 5, 1, LTE_BEARER_BITMASK);
    729 
    730         // Verify connected with APN2 setting.
    731         verifyDataConnected(FAKE_APN2);
    732     }
    733 
    734     @Test
    735     @MediumTest
    736     @Ignore
    737     @FlakyTest
    738     public void testUserDisableData() throws Exception {
    739         //step 1: setup two DataCalls one for Metered: default, another one for Non-metered: IMS
    740         //set Default and MMS to be metered in the CarrierConfigManager
    741         boolean dataEnabled = mDct.isUserDataEnabled();
    742         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    743                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    744         mDct.setEnabled(DctConstants.APN_IMS_ID, true);
    745         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
    746 
    747         logd("Sending EVENT_RECORDS_LOADED");
    748         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
    749         waitForMs(200);
    750 
    751         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
    752         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
    753         waitForMs(200);
    754 
    755         logd("Sending DATA_ENABLED_CMD");
    756         mDct.setUserDataEnabled(true);
    757 
    758         waitForMs(200);
    759         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    760         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
    761                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    762                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    763                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    764                 any(Message.class));
    765         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, LTE_BEARER_BITMASK);
    766 
    767         logd("Sending DATA_DISABLED_CMD");
    768         mDct.setUserDataEnabled(false);
    769         waitForMs(200);
    770 
    771         // expected tear down all metered DataConnections
    772         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
    773                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
    774                 any(Message.class));
    775         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
    776         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
    777         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_IMS));
    778 
    779         // reset the setting at the end of this test
    780         mDct.setUserDataEnabled(dataEnabled);
    781         waitForMs(200);
    782     }
    783 
    784     @Test
    785     @MediumTest
    786     public void testUserDisableRoaming() throws Exception {
    787         //step 1: setup two DataCalls one for Metered: default, another one for Non-metered: IMS
    788         //step 2: set roaming disabled, data is enabled
    789         //step 3: under roaming service
    790         //step 4: only tear down metered data connections.
    791 
    792         //set Default and MMS to be metered in the CarrierConfigManager
    793         boolean roamingEnabled = mDct.getDataRoamingEnabled();
    794         boolean dataEnabled = mDct.isUserDataEnabled();
    795 
    796         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    797                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    798 
    799         mDct.setEnabled(DctConstants.APN_IMS_ID, true);
    800         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
    801 
    802         logd("Sending EVENT_RECORDS_LOADED");
    803         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
    804         waitForMs(200);
    805 
    806         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
    807         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
    808         waitForMs(200);
    809 
    810         logd("Sending DATA_ENABLED_CMD");
    811         mDct.setUserDataEnabled(true);
    812 
    813         waitForMs(300);
    814         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    815         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
    816                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    817                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    818                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    819                 any(Message.class));
    820         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, LTE_BEARER_BITMASK);
    821 
    822         //user is in roaming
    823         doReturn(true).when(mServiceState).getDataRoaming();
    824         logd("Sending DISABLE_ROAMING_CMD");
    825         mDct.setDataRoamingEnabledByUser(false);
    826         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_ROAMING_ON));
    827         waitForMs(200);
    828 
    829         // expected tear down all metered DataConnections
    830         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
    831                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
    832                 any(Message.class));
    833         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
    834         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
    835         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_IMS));
    836 
    837         // reset roaming settings / data enabled settings at end of this test
    838         mDct.setDataRoamingEnabledByUser(roamingEnabled);
    839         mDct.setUserDataEnabled(dataEnabled);
    840         waitForMs(200);
    841     }
    842 
    843     @Test
    844     @MediumTest
    845     public void testDataCallOnUserDisableRoaming() throws Exception {
    846         //step 1: mock under roaming service and user disabled roaming from settings.
    847         //step 2: user toggled data settings on
    848         //step 3: only non-metered data call is established
    849 
    850         boolean roamingEnabled = mDct.getDataRoamingEnabled();
    851         boolean dataEnabled = mDct.isUserDataEnabled();
    852         doReturn(true).when(mServiceState).getDataRoaming();
    853 
    854         //set Default and MMS to be metered in the CarrierConfigManager
    855         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
    856                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    857         mDct.setEnabled(DctConstants.APN_IMS_ID, true);
    858         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
    859 
    860         logd("Sending DATA_ENABLED_CMD");
    861         mDct.setUserDataEnabled(true);
    862 
    863         logd("Sending DISABLE_ROAMING_CMD");
    864         mDct.setDataRoamingEnabledByUser(false);
    865 
    866         logd("Sending EVENT_RECORDS_LOADED");
    867         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
    868         waitForMs(200);
    869 
    870         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
    871         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
    872         waitForMs(200);
    873 
    874         waitForMs(200);
    875         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    876         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
    877                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    878                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    879                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    880                 any(Message.class));
    881         verifyDataProfile(dpCaptor.getValue(), FAKE_APN3, 2, 64, 0, 0);
    882 
    883         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
    884         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
    885         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_IMS));
    886 
    887         // reset roaming settings / data enabled settings at end of this test
    888         mDct.setDataRoamingEnabledByUser(roamingEnabled);
    889         mDct.setUserDataEnabled(dataEnabled);
    890         waitForMs(200);
    891     }
    892 
    893     // Test the default data switch scenario.
    894     @Test
    895     @MediumTest
    896     public void testDDSResetAutoAttach() throws Exception {
    897 
    898         ContentResolver resolver = mContext.getContentResolver();
    899         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONED, 1);
    900 
    901         mDct.setUserDataEnabled(true);
    902 
    903         mContextFixture.putBooleanResource(
    904                 com.android.internal.R.bool.config_auto_attach_data_on_creation, true);
    905 
    906         mSimulatedCommands.setDataCallResult(true, createSetupDataCallResult());
    907 
    908         DataConnectionReasons dataConnectionReasons = new DataConnectionReasons();
    909         boolean allowed = isDataAllowed(dataConnectionReasons);
    910         assertFalse(dataConnectionReasons.toString(), allowed);
    911 
    912         ArgumentCaptor<Integer> intArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
    913         verify(mUiccController, times(1)).registerForIccChanged(eq(mDct),
    914                 intArgumentCaptor.capture(), eq(null));
    915         // Ideally this should send EVENT_ICC_CHANGED.
    916         mDct.sendMessage(mDct.obtainMessage(intArgumentCaptor.getValue(), null));
    917         waitForMs(100);
    918 
    919         verify(mSimRecords, times(1)).registerForRecordsLoaded(eq(mDct),
    920                 intArgumentCaptor.capture(), eq(null));
    921         // Ideally this should send EVENT_RECORDS_LOADED.
    922         mDct.sendMessage(mDct.obtainMessage(intArgumentCaptor.getValue(), null));
    923         waitForMs(100);
    924 
    925         verify(mSST, times(1)).registerForDataConnectionAttached(eq(mDct),
    926                 intArgumentCaptor.capture(), eq(null));
    927         // Ideally this should send EVENT_DATA_CONNECTION_ATTACHED");
    928         mDct.sendMessage(mDct.obtainMessage(intArgumentCaptor.getValue(), null));
    929         waitForMs(200);
    930 
    931         NetworkRequest nr = new NetworkRequest.Builder()
    932                 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
    933         LocalLog l = new LocalLog(100);
    934         mDct.requestNetwork(nr, l);
    935         waitForMs(200);
    936 
    937         verifyDataConnected(FAKE_APN1);
    938 
    939         assertTrue(mDct.getAutoAttachOnCreation());
    940         mDct.update();
    941         // The auto attach flag should be reset after update
    942         assertFalse(mDct.getAutoAttachOnCreation());
    943 
    944         verify(mSST, times(1)).registerForDataConnectionDetached(eq(mDct),
    945                 intArgumentCaptor.capture(), eq(null));
    946         // Ideally this should send EVENT_DATA_CONNECTION_DETACHED
    947         mDct.sendMessage(mDct.obtainMessage(intArgumentCaptor.getValue(), null));
    948         waitForMs(200);
    949 
    950         // Data should not be allowed since auto attach flag has been reset.
    951         dataConnectionReasons = new DataConnectionReasons();
    952         allowed = isDataAllowed(dataConnectionReasons);
    953         assertFalse(dataConnectionReasons.toString(), allowed);
    954     }
    955 
    956     // Test for API carrierActionSetMeteredApnsEnabled.
    957     @FlakyTest
    958     @Ignore
    959     @Test
    960     @MediumTest
    961     public void testCarrierActionSetMeteredApnsEnabled() throws Exception {
    962         //step 1: setup two DataCalls one for Internet and IMS
    963         //step 2: set data is enabled
    964         //step 3: cold sim is detected
    965         //step 4: all data connection is torn down
    966         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
    967                 new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
    968 
    969         boolean dataEnabled = mDct.isUserDataEnabled();
    970         mDct.setUserDataEnabled(true);
    971 
    972         mDct.setEnabled(DctConstants.APN_IMS_ID, true);
    973         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
    974 
    975         logd("Sending EVENT_RECORDS_LOADED");
    976         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
    977         waitForMs(200);
    978 
    979         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
    980         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
    981         waitForMs(200);
    982 
    983         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
    984         verify(mSimulatedCommandsVerifier, times(2)).setupDataCall(
    985                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
    986                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
    987                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
    988                 any(Message.class));
    989         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, LTE_BEARER_BITMASK);
    990         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
    991 
    992         Message msg = mDct.obtainMessage(DctConstants.EVENT_SET_CARRIER_DATA_ENABLED);
    993         AsyncResult.forMessage(msg).result = false;
    994         mDct.sendMessage(msg);
    995 
    996         waitForMs(100);
    997 
    998         // Validate all metered data connections have been torn down
    999         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
   1000                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
   1001                 any(Message.class));
   1002         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
   1003         assertEquals(DctConstants.State.IDLE, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
   1004 
   1005         // Reset settings at the end of test
   1006         mDct.setUserDataEnabled(dataEnabled);
   1007         waitForMs(200);
   1008     }
   1009 
   1010     private void initApns(String targetApn, String[] canHandleTypes) {
   1011         doReturn(targetApn).when(mApnContext).getApnType();
   1012         doReturn(true).when(mApnContext).isConnectable();
   1013         ApnSetting apnSetting = createApnSetting(canHandleTypes);
   1014         doReturn(apnSetting).when(mApnContext).getNextApnSetting();
   1015         doReturn(apnSetting).when(mApnContext).getApnSetting();
   1016         doReturn(mDcac).when(mApnContext).getDcAc();
   1017         doReturn(true).when(mApnContext).isEnabled();
   1018         doReturn(true).when(mApnContext).getDependencyMet();
   1019         doReturn(true).when(mApnContext).isReady();
   1020         doReturn(true).when(mApnContext).hasNoRestrictedRequests(eq(true));
   1021     }
   1022 
   1023     // Test the emergency APN setup.
   1024     @Test
   1025     @SmallTest
   1026     public void testTrySetupDataEmergencyApn() throws Exception {
   1027         initApns(PhoneConstants.APN_TYPE_EMERGENCY, new String[]{PhoneConstants.APN_TYPE_ALL});
   1028         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1029         waitForMs(200);
   1030 
   1031         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
   1032                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
   1033                         mServiceState.getRilDataRadioTechnology())), any(DataProfile.class),
   1034                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1035                 any(Message.class));
   1036     }
   1037 
   1038     @Test
   1039     @SmallTest
   1040     public void testGetDataConnectionState() throws Exception {
   1041         initApns(PhoneConstants.APN_TYPE_SUPL,
   1042                 new String[]{PhoneConstants.APN_TYPE_SUPL, PhoneConstants.APN_TYPE_DEFAULT});
   1043         mDct.setUserDataEnabled(false);
   1044 
   1045         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1046                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1047 
   1048         logd("Sending EVENT_RECORDS_LOADED");
   1049         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1050         waitForMs(200);
   1051 
   1052         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1053         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1054         waitForMs(200);
   1055 
   1056         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1057         waitForMs(200);
   1058 
   1059         // Assert that both APN_TYPE_SUPL & APN_TYPE_DEFAULT are connected even we only setup data
   1060         // for APN_TYPE_SUPL
   1061         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_SUPL));
   1062         assertEquals(DctConstants.State.CONNECTED, mDct.getState(PhoneConstants.APN_TYPE_DEFAULT));
   1063     }
   1064 
   1065     // Test the unmetered APN setup when data is disabled.
   1066     @Test
   1067     @SmallTest
   1068     public void testTrySetupDataUnmeteredDataDisabled() throws Exception {
   1069         initApns(PhoneConstants.APN_TYPE_FOTA, new String[]{PhoneConstants.APN_TYPE_ALL});
   1070         mDct.setUserDataEnabled(false);
   1071 
   1072         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1073                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1074 
   1075         logd("Sending EVENT_RECORDS_LOADED");
   1076         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1077         waitForMs(200);
   1078 
   1079         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1080         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1081         waitForMs(200);
   1082 
   1083         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1084         waitForMs(200);
   1085 
   1086         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(
   1087                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
   1088                         mServiceState.getRilDataRadioTechnology())), any(DataProfile.class),
   1089                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1090                 any(Message.class));
   1091     }
   1092 
   1093     // Test the metered APN setup when data is disabled.
   1094     @Test
   1095     @SmallTest
   1096     public void testTrySetupMeteredDataDisabled() throws Exception {
   1097         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1098         mDct.setUserDataEnabled(false);
   1099 
   1100         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1101                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1102 
   1103         logd("Sending EVENT_RECORDS_LOADED");
   1104         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1105         waitForMs(200);
   1106 
   1107         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1108         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1109         waitForMs(200);
   1110 
   1111         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1112         waitForMs(200);
   1113 
   1114         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
   1115                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1116                 any(Message.class));
   1117     }
   1118 
   1119     // Test the restricted data request when data is disabled.
   1120     @Test
   1121     @SmallTest
   1122     public void testTrySetupRestrictedDataDisabled() throws Exception {
   1123         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1124         doReturn(false).when(mApnContext).hasNoRestrictedRequests(eq(true));
   1125 
   1126         mDct.setUserDataEnabled(false);
   1127 
   1128         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1129                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1130 
   1131         logd("Sending EVENT_RECORDS_LOADED");
   1132         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1133         waitForMs(200);
   1134 
   1135         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1136         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1137         waitForMs(200);
   1138 
   1139         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1140         waitForMs(200);
   1141 
   1142         verify(mSimulatedCommandsVerifier, times(1)).setupDataCall(anyInt(), any(DataProfile.class),
   1143                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1144                 any(Message.class));
   1145     }
   1146 
   1147     // Test the restricted data request when roaming is disabled.
   1148     @Test
   1149     @SmallTest
   1150     public void testTrySetupRestrictedRoamingDisabled() throws Exception {
   1151         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1152         doReturn(false).when(mApnContext).hasNoRestrictedRequests(eq(true));
   1153 
   1154         mDct.setUserDataEnabled(true);
   1155         mDct.setDataRoamingEnabledByUser(false);
   1156         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1157                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1158         //user is in roaming
   1159         doReturn(true).when(mServiceState).getDataRoaming();
   1160 
   1161         logd("Sending EVENT_RECORDS_LOADED");
   1162         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1163         waitForMs(200);
   1164 
   1165         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1166         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1167         waitForMs(200);
   1168 
   1169         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1170         waitForMs(200);
   1171 
   1172         // expect no restricted data connection
   1173         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
   1174                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1175                 any(Message.class));
   1176     }
   1177 
   1178     // Test the default data when data is not connectable.
   1179     @Test
   1180     @SmallTest
   1181     public void testTrySetupNotConnectable() throws Exception {
   1182         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1183         doReturn(false).when(mApnContext).isConnectable();
   1184         mDct.setUserDataEnabled(true);
   1185 
   1186         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1187                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1188 
   1189         logd("Sending EVENT_RECORDS_LOADED");
   1190         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1191         waitForMs(200);
   1192 
   1193         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1194         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1195         waitForMs(200);
   1196 
   1197         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1198         waitForMs(200);
   1199 
   1200         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
   1201                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1202                 any(Message.class));
   1203     }
   1204 
   1205     // Test the default data on IWLAN.
   1206     @Test
   1207     @SmallTest
   1208     public void testTrySetupDefaultOnIWLAN() throws Exception {
   1209         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1210         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN).when(mServiceState)
   1211                 .getRilDataRadioTechnology();
   1212         mDct.setUserDataEnabled(true);
   1213 
   1214         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1215                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1216 
   1217         logd("Sending EVENT_RECORDS_LOADED");
   1218         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1219         waitForMs(200);
   1220 
   1221         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1222         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1223         waitForMs(200);
   1224 
   1225         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1226         waitForMs(200);
   1227 
   1228         verify(mSimulatedCommandsVerifier, times(0)).setupDataCall(anyInt(), any(DataProfile.class),
   1229                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1230                 any(Message.class));
   1231     }
   1232 
   1233     // Test the default data when the phone is in ECBM.
   1234     @Test
   1235     @SmallTest
   1236     public void testTrySetupDefaultInECBM() throws Exception {
   1237         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1238         doReturn(true).when(mPhone).isInEcm();
   1239         mDct.setUserDataEnabled(true);
   1240 
   1241         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1242                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1243 
   1244         logd("Sending EVENT_RECORDS_LOADED");
   1245         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1246         waitForMs(200);
   1247 
   1248         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1249         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1250         waitForMs(200);
   1251 
   1252         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, mApnContext));
   1253         waitForMs(200);
   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     @Test
   1262     @SmallTest
   1263     public void testUpdateWaitingApnListOnDataRatChange() throws Exception {
   1264         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD).when(mServiceState)
   1265                 .getRilDataRadioTechnology();
   1266         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1267                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1268         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
   1269         mDct.setUserDataEnabled(true);
   1270         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1271 
   1272         logd("Sending EVENT_RECORDS_LOADED");
   1273         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1274         waitForMs(200);
   1275 
   1276         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1277         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1278         waitForMs(200);
   1279 
   1280         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
   1281         // Verify if RIL command was sent properly.
   1282         verify(mSimulatedCommandsVerifier).setupDataCall(
   1283                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
   1284                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
   1285                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1286                 any(Message.class));
   1287         verifyDataProfile(dpCaptor.getValue(), FAKE_APN4, 0, 5, 2, EHRPD_BEARER_BITMASK);
   1288         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
   1289 
   1290         //data rat change from ehrpd to lte
   1291         logd("Sending EVENT_DATA_RAT_CHANGED");
   1292         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_LTE).when(mServiceState)
   1293                 .getRilDataRadioTechnology();
   1294         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
   1295         waitForMs(200);
   1296 
   1297         // Verify the disconnected data call due to rat change and retry manger schedule another
   1298         // data call setup
   1299         verify(mSimulatedCommandsVerifier, times(1)).deactivateDataCall(
   1300                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
   1301                 any(Message.class));
   1302         verify(mAlarmManager, times(1)).setExact(eq(AlarmManager.ELAPSED_REALTIME_WAKEUP),
   1303                 anyLong(), any(PendingIntent.class));
   1304 
   1305         // Simulate the timer expires.
   1306         Intent intent = new Intent("com.android.internal.telephony.data-reconnect.default");
   1307         intent.putExtra("reconnect_alarm_extra_type", PhoneConstants.APN_TYPE_DEFAULT);
   1308         intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, 0);
   1309         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
   1310         mContext.sendBroadcast(intent);
   1311         waitForMs(200);
   1312 
   1313         // Verify if RIL command was sent properly.
   1314         verify(mSimulatedCommandsVerifier).setupDataCall(
   1315                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
   1316                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
   1317                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1318                 any(Message.class));
   1319         verifyDataProfile(dpCaptor.getValue(), FAKE_APN1, 0, 5, 1, LTE_BEARER_BITMASK);
   1320         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
   1321     }
   1322 
   1323     // Test for fetchDunApns()
   1324     @Test
   1325     @SmallTest
   1326     public void testFetchDunApn() {
   1327         logd("Sending EVENT_RECORDS_LOADED");
   1328         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1329         waitForMs(200);
   1330 
   1331         String dunApnString = "[ApnSettingV3]HOT mobile PC,pc.hotm,,,,,,,,,440,10,,DUN,,,true,"
   1332                 + "0,,,,,,,,";
   1333         ApnSetting dunApnExpected = ApnSetting.fromString(dunApnString);
   1334 
   1335         Settings.Global.putString(mContext.getContentResolver(),
   1336                 Settings.Global.TETHER_DUN_APN, dunApnString);
   1337         // should return APN from Setting
   1338         ApnSetting dunApn = mDct.fetchDunApns().get(0);
   1339         assertTrue(dunApnExpected.equals(dunApn));
   1340 
   1341         Settings.Global.putString(mContext.getContentResolver(),
   1342                 Settings.Global.TETHER_DUN_APN, null);
   1343         // should return APN from db
   1344         dunApn = mDct.fetchDunApns().get(0);
   1345         assertEquals(FAKE_APN5, dunApn.apn);
   1346     }
   1347 
   1348     // Test for fetchDunApns() with apn set id
   1349     @Test
   1350     @SmallTest
   1351     public void testFetchDunApnWithPreferredApnSet() {
   1352         logd("Sending EVENT_RECORDS_LOADED");
   1353         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1354         waitForMs(200);
   1355 
   1356         // apnSetId=1
   1357         String dunApnString1 = "[ApnSettingV5]HOT mobile PC,pc.hotm,,,,,,,,,440,10,,DUN,,,true,"
   1358                 + "0,,,,,,,,,,1";
   1359         // apnSetId=0
   1360         String dunApnString2 = "[ApnSettingV5]HOT mobile PC,pc.coldm,,,,,,,,,440,10,,DUN,,,true,"
   1361                 + "0,,,,,,,,,,0";
   1362 
   1363         ApnSetting dunApnExpected = ApnSetting.fromString(dunApnString1);
   1364 
   1365         ContentResolver cr = mContext.getContentResolver();
   1366         Settings.Global.putString(cr, Settings.Global.TETHER_DUN_APN,
   1367                 dunApnString1 + ";" + dunApnString2);
   1368 
   1369         // set that we prefer apn set 1
   1370         ContentValues values = new ContentValues();
   1371         values.put(Telephony.Carriers.APN_SET_ID, 1);
   1372         cr.update(PREFERAPN_URI, values, null, null);
   1373 
   1374         // return APN from Setting with apnSetId=1
   1375         ArrayList<ApnSetting> dunApns = mDct.sortApnListByPreferred(mDct.fetchDunApns());
   1376         assertEquals(2, dunApns.size());
   1377         assertTrue(dunApnExpected.equals(dunApns.get(0)));
   1378     }
   1379 
   1380     // Test oos
   1381     @Test
   1382     @SmallTest
   1383     public void testDataRatChangeOOS() throws Exception {
   1384         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD).when(mServiceState)
   1385                 .getRilDataRadioTechnology();
   1386         mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
   1387                 new String[]{PhoneConstants.APN_TYPE_DEFAULT});
   1388         mDct.setEnabled(DctConstants.APN_DEFAULT_ID, true);
   1389         mDct.setUserDataEnabled(true);
   1390         initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});
   1391 
   1392         logd("Sending EVENT_RECORDS_LOADED");
   1393         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
   1394         waitForMs(200);
   1395 
   1396         logd("Sending EVENT_DATA_CONNECTION_ATTACHED");
   1397         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
   1398         waitForMs(200);
   1399 
   1400         ArgumentCaptor<DataProfile> dpCaptor = ArgumentCaptor.forClass(DataProfile.class);
   1401         // Verify if RIL command was sent properly.
   1402         verify(mSimulatedCommandsVerifier).setupDataCall(
   1403                 eq(ServiceState.rilRadioTechnologyToAccessNetworkType(
   1404                         mServiceState.getRilDataRadioTechnology())), dpCaptor.capture(),
   1405                 eq(false), eq(false), eq(DataService.REQUEST_REASON_NORMAL), any(),
   1406                 any(Message.class));
   1407         verifyDataProfile(dpCaptor.getValue(), FAKE_APN4, 0, 5, 2, EHRPD_BEARER_BITMASK);
   1408         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
   1409 
   1410         // Data rat change from ehrpd to unknown due to OOS
   1411         logd("Sending EVENT_DATA_RAT_CHANGED");
   1412         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN).when(mServiceState)
   1413                 .getRilDataRadioTechnology();
   1414         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
   1415         waitForMs(200);
   1416 
   1417         // Verify data connection is on
   1418         verify(mSimulatedCommandsVerifier, times(0)).deactivateDataCall(
   1419                 eq(DataService.REQUEST_REASON_NORMAL), anyInt(),
   1420                 any(Message.class));
   1421 
   1422         // Data rat resume from unknown to ehrpd
   1423         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD).when(mServiceState)
   1424                 .getRilDataRadioTechnology();
   1425         mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_RAT_CHANGED, null));
   1426         waitForMs(200);
   1427 
   1428         // Verify the same data connection
   1429         assertEquals(FAKE_APN4, mDct.getActiveApnString(PhoneConstants.APN_TYPE_DEFAULT));
   1430         assertEquals(DctConstants.State.CONNECTED, mDct.getOverallState());
   1431     }
   1432 
   1433     // Test provisioning
   1434     @Test
   1435     @SmallTest
   1436     public void testDataEnableInProvisioning() throws Exception {
   1437         ContentResolver resolver = mContext.getContentResolver();
   1438 
   1439         assertEquals(1, Settings.Global.getInt(resolver, Settings.Global.MOBILE_DATA));
   1440         assertTrue(mDct.isDataEnabled());
   1441         assertTrue(mDct.isUserDataEnabled());
   1442 
   1443 
   1444         mDct.setUserDataEnabled(false);
   1445         waitForMs(200);
   1446 
   1447         assertEquals(0, Settings.Global.getInt(resolver, Settings.Global.MOBILE_DATA));
   1448         assertFalse(mDct.isDataEnabled());
   1449         assertFalse(mDct.isUserDataEnabled());
   1450 
   1451         // Changing provisioned to 0.
   1452         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0);
   1453 
   1454         assertTrue(mDct.isDataEnabled());
   1455         assertTrue(mDct.isUserDataEnabled());
   1456 
   1457         // Enable user data during provisioning. It should write to
   1458         // Settings.Global.MOBILE_DATA and keep data enabled when provisioned.
   1459         mDct.setUserDataEnabled(true);
   1460         Settings.Global.putInt(resolver, Settings.Global.DEVICE_PROVISIONED, 1);
   1461         waitForMs(200);
   1462 
   1463         assertTrue(mDct.isDataEnabled());
   1464         assertTrue(mDct.isUserDataEnabled());
   1465         assertEquals(1, Settings.Global.getInt(resolver, Settings.Global.MOBILE_DATA));
   1466     }
   1467 }
   1468