Home | History | Annotate | Download | only in live
      1 #!/usr/bin/env python3.4
      2 #
      3 #   Copyright 2016 - Google
      4 #
      5 #   Licensed under the Apache License, Version 2.0 (the "License");
      6 #   you may not use this file except in compliance with the License.
      7 #   You may obtain a copy of the License at
      8 #
      9 #       http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 #   Unless required by applicable law or agreed to in writing, software
     12 #   distributed under the License is distributed on an "AS IS" BASIS,
     13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 #   See the License for the specific language governing permissions and
     15 #   limitations under the License.
     16 """
     17     Test Script for Telephony Pre Check In Sanity
     18 """
     19 
     20 import collections
     21 import random
     22 import time
     23 import os
     24 from acts.test_decorators import test_tracker_info
     25 from acts.base_test import BaseTestClass
     26 from queue import Empty
     27 from acts.test_utils.tel.tel_subscription_utils import \
     28     get_subid_from_slot_index
     29 from acts.test_utils.bt.bt_test_utils import bluetooth_enabled_check
     30 from acts.test_utils.bt.bt_test_utils import disable_bluetooth
     31 from acts.test_utils.bt.bt_test_utils import pair_pri_to_sec
     32 from acts.test_utils.tel.tel_subscription_utils import set_subid_for_data
     33 from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
     34 from acts.test_utils.tel.tel_defines import DIRECTION_MOBILE_ORIGINATED
     35 from acts.test_utils.tel.tel_defines import DIRECTION_MOBILE_TERMINATED
     36 from acts.test_utils.tel.tel_defines import DATA_STATE_CONNECTED
     37 from acts.test_utils.tel.tel_defines import GEN_2G
     38 from acts.test_utils.tel.tel_defines import GEN_3G
     39 from acts.test_utils.tel.tel_defines import GEN_4G
     40 from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
     41 from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE
     42 from acts.test_utils.tel.tel_defines import RAT_2G
     43 from acts.test_utils.tel.tel_defines import RAT_3G
     44 from acts.test_utils.tel.tel_defines import RAT_4G
     45 from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
     46 from acts.test_utils.tel.tel_defines import SIM1_SLOT_INDEX
     47 from acts.test_utils.tel.tel_defines import SIM2_SLOT_INDEX
     48 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
     49 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_TETHERING_ENTITLEMENT_CHECK
     50 from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WIFI_CONNECTION
     51 from acts.test_utils.tel.tel_defines import TETHERING_MODE_WIFI
     52 from acts.test_utils.tel.tel_defines import WAIT_TIME_AFTER_REBOOT
     53 from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING
     54 from acts.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_REG_AND_CALL
     55 from acts.test_utils.tel.tel_defines import \
     56     WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING
     57 from acts.test_utils.tel.tel_defines import WAIT_TIME_TETHERING_AFTER_REBOOT
     58 from acts.test_utils.tel.tel_data_utils import airplane_mode_test
     59 from acts.test_utils.tel.tel_data_utils import change_data_sim_and_verify_data
     60 from acts.test_utils.tel.tel_data_utils import data_connectivity_single_bearer
     61 from acts.test_utils.tel.tel_data_utils import tethering_check_internet_connection
     62 from acts.test_utils.tel.tel_data_utils import wifi_cell_switching
     63 from acts.test_utils.tel.tel_data_utils import wifi_tethering_cleanup
     64 from acts.test_utils.tel.tel_data_utils import wifi_tethering_setup_teardown
     65 from acts.test_utils.tel.tel_test_utils import active_file_download_test
     66 from acts.test_utils.tel.tel_test_utils import start_adb_tcpdump
     67 from acts.test_utils.tel.tel_test_utils import stop_adb_tcpdump
     68 from acts.test_utils.tel.tel_test_utils import call_setup_teardown
     69 from acts.test_utils.tel.tel_test_utils import check_is_wifi_connected
     70 from acts.test_utils.tel.tel_test_utils import ensure_phones_default_state
     71 from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
     72 from acts.test_utils.tel.tel_test_utils import ensure_network_generation
     73 from acts.test_utils.tel.tel_test_utils import \
     74     ensure_network_generation_for_subscription
     75 from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
     76 from acts.test_utils.tel.tel_test_utils import get_mobile_data_usage
     77 from acts.test_utils.tel.tel_test_utils import get_slot_index_from_subid
     78 from acts.test_utils.tel.tel_test_utils import get_network_rat_for_subscription
     79 from acts.test_utils.tel.tel_test_utils import hangup_call
     80 from acts.test_utils.tel.tel_test_utils import multithread_func
     81 from acts.test_utils.tel.tel_test_utils import remove_mobile_data_usage_limit
     82 from acts.test_utils.tel.tel_test_utils import set_call_state_listen_level
     83 from acts.test_utils.tel.tel_test_utils import set_mobile_data_usage_limit
     84 from acts.test_utils.tel.tel_test_utils import setup_sim
     85 from acts.test_utils.tel.tel_test_utils import stop_wifi_tethering
     86 from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
     87 from acts.test_utils.tel.tel_test_utils import toggle_volte
     88 from acts.test_utils.tel.tel_test_utils import verify_http_connection
     89 from acts.test_utils.tel.tel_test_utils import verify_internet_connection
     90 from acts.test_utils.tel.tel_test_utils import verify_incall_state
     91 from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection
     92 from acts.test_utils.tel.tel_test_utils import wait_for_network_rat
     93 from acts.test_utils.tel.tel_test_utils import \
     94     wait_for_voice_attach_for_subscription
     95 from acts.test_utils.tel.tel_test_utils import \
     96     wait_for_data_attach_for_subscription
     97 from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection
     98 from acts.test_utils.tel.tel_test_utils import wifi_reset
     99 from acts.test_utils.tel.tel_test_utils import wait_for_state
    100 from acts.test_utils.tel.tel_test_utils import wifi_toggle_state
    101 from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_2G
    102 from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G
    103 from acts.test_utils.tel.tel_test_utils import WIFI_SSID_KEY
    104 from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_3g
    105 from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_csfb
    106 from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_volte
    107 from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_3g
    108 from acts.test_utils.tel.tel_voice_utils import phone_setup_csfb
    109 from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_general
    110 from acts.test_utils.tel.tel_voice_utils import phone_setup_volte
    111 from acts.utils import disable_doze
    112 from acts.utils import enable_doze
    113 from acts.utils import rand_ascii_str
    114 from acts.utils import adb_shell_ping
    115 
    116 
    117 class TelLiveDataTest(TelephonyBaseTest):
    118     def __init__(self, controllers):
    119         TelephonyBaseTest.__init__(self, controllers)
    120 
    121         self.stress_test_number = self.get_stress_test_number()
    122         self.wifi_network_ssid = self.user_params.get(
    123             "wifi_network_ssid") or self.user_params.get(
    124                 "wifi_network_ssid_2g")
    125         self.wifi_network_pass = self.user_params.get(
    126             "wifi_network_pass") or self.user_params.get(
    127                 "wifi_network_pass_2g")
    128         self.provider = self.android_devices[0]
    129         self.clients = self.android_devices[1:]
    130 
    131     def setup_test(self):
    132         TelephonyBaseTest.setup_test(self)
    133         self.number_of_devices = 1
    134         try:
    135             self.tcpdump_proc = [None, None]
    136             self.tcpdump_proc[0] = start_adb_tcpdump(
    137                 self.android_devices[0], self.test_name, mask="all")
    138         except Exception as e:
    139             self.log.warning("Failed to start tcpdump collection", e)
    140             pass
    141 
    142     def on_fail(self, test_name, begin_time):
    143         self.log.info("Inside Teardown Test")
    144         TelephonyBaseTest.on_fail(self, test_name, begin_time)
    145         try:
    146             if self.tcpdump_proc[0] is not None:
    147                 stop_adb_tcpdump(self.android_devices[0], self.tcpdump_proc[0],
    148                                  True, self.test_name)
    149         except Exception as e:
    150             self.log.warning("Failed to stop tcpdump collection", e)
    151             pass
    152 
    153     @test_tracker_info(uuid="1b0354f3-8668-4a28-90a5-3b3d2b2756d3")
    154     @TelephonyBaseTest.tel_test_wrap
    155     def test_airplane_mode(self):
    156         """ Test airplane mode basic on Phone and Live SIM.
    157 
    158         Ensure phone attach, data on, WiFi off and verify Internet.
    159         Turn on airplane mode to make sure detach.
    160         Turn off airplane mode to make sure attach.
    161         Verify Internet connection.
    162 
    163         Returns:
    164             True if pass; False if fail.
    165         """
    166         return airplane_mode_test(self.log, self.android_devices[0])
    167 
    168     @test_tracker_info(uuid="47430f01-583f-4efb-923a-285a51b75d50")
    169     @TelephonyBaseTest.tel_test_wrap
    170     def test_lte_wifi_switching(self):
    171         """Test data connection network switching when phone camped on LTE.
    172 
    173         Ensure phone is camped on LTE
    174         Ensure WiFi can connect to live network,
    175         Airplane mode is off, data connection is on, WiFi is on.
    176         Turn off WiFi, verify data is on cell and browse to google.com is OK.
    177         Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
    178         Turn off WiFi, verify data is on cell and browse to google.com is OK.
    179 
    180         Returns:
    181             True if pass.
    182         """
    183         return wifi_cell_switching(self.log, self.android_devices[0],
    184                                    self.wifi_network_ssid,
    185                                    self.wifi_network_pass, GEN_4G)
    186 
    187     @test_tracker_info(uuid="8a836cf1-600b-4cf3-abfe-2e3da5c11396")
    188     @TelephonyBaseTest.tel_test_wrap
    189     def test_wcdma_wifi_switching(self):
    190         """Test data connection network switching when phone camped on WCDMA.
    191 
    192         Ensure phone is camped on WCDMA
    193         Ensure WiFi can connect to live network,
    194         Airplane mode is off, data connection is on, WiFi is on.
    195         Turn off WiFi, verify data is on cell and browse to google.com is OK.
    196         Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
    197         Turn off WiFi, verify data is on cell and browse to google.com is OK.
    198 
    199         Returns:
    200             True if pass.
    201         """
    202         return wifi_cell_switching(self.log, self.android_devices[0],
    203                                    self.wifi_network_ssid,
    204                                    self.wifi_network_pass, GEN_3G)
    205 
    206     @test_tracker_info(uuid="c016f2e8-0af6-42e4-a3cb-a2b7d8b564d0")
    207     @TelephonyBaseTest.tel_test_wrap
    208     def test_gsm_wifi_switching(self):
    209         """Test data connection network switching when phone camped on GSM.
    210 
    211         Ensure phone is camped on GSM
    212         Ensure WiFi can connect to live network,,
    213         Airplane mode is off, data connection is on, WiFi is on.
    214         Turn off WiFi, verify data is on cell and browse to google.com is OK.
    215         Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
    216         Turn off WiFi, verify data is on cell and browse to google.com is OK.
    217 
    218         Returns:
    219             True if pass.
    220         """
    221         return wifi_cell_switching(self.log, self.android_devices[0],
    222                                    self.wifi_network_ssid,
    223                                    self.wifi_network_pass, GEN_2G)
    224 
    225     @test_tracker_info(uuid="78d6b258-82d4-47b4-8723-3b3a15412d2d")
    226     @TelephonyBaseTest.tel_test_wrap
    227     def test_lte_multi_bearer(self):
    228         """Test LTE data connection before call and in call. (VoLTE call)
    229 
    230 
    231         Turn off airplane mode, disable WiFi, enable Cellular Data.
    232         Make sure phone in LTE, verify Internet.
    233         Initiate a voice call. verify Internet.
    234         Disable Cellular Data, verify Internet is inaccessible.
    235         Enable Cellular Data, verify Internet.
    236         Hangup Voice Call, verify Internet.
    237 
    238         Returns:
    239             True if success.
    240             False if failed.
    241         """
    242         if not phone_setup_volte(self.log, self.android_devices[0]):
    243             self.log.error("Failed to setup VoLTE")
    244             return False
    245         return self._test_data_connectivity_multi_bearer(GEN_4G)
    246 
    247     @test_tracker_info(uuid="5c9cb076-0c26-4517-95dc-2ec4974e8ce3")
    248     @TelephonyBaseTest.tel_test_wrap
    249     def test_wcdma_multi_bearer(self):
    250         """Test WCDMA data connection before call and in call.
    251 
    252         Turn off airplane mode, disable WiFi, enable Cellular Data.
    253         Make sure phone in WCDMA, verify Internet.
    254         Initiate a voice call. verify Internet.
    255         Disable Cellular Data, verify Internet is inaccessible.
    256         Enable Cellular Data, verify Internet.
    257         Hangup Voice Call, verify Internet.
    258 
    259         Returns:
    260             True if success.
    261             False if failed.
    262         """
    263 
    264         return self._test_data_connectivity_multi_bearer(GEN_3G)
    265 
    266     @test_tracker_info(uuid="314bbf1c-073f-4d48-9817-a6e14f96f3c0")
    267     @TelephonyBaseTest.tel_test_wrap
    268     def test_gsm_multi_bearer_mo(self):
    269         """Test gsm data connection before call and in call.
    270 
    271         Turn off airplane mode, disable WiFi, enable Cellular Data.
    272         Make sure phone in GSM, verify Internet.
    273         Initiate a MO voice call. Verify there is no Internet during call.
    274         Hangup Voice Call, verify Internet.
    275 
    276         Returns:
    277             True if success.
    278             False if failed.
    279         """
    280 
    281         return self._test_data_connectivity_multi_bearer(
    282             GEN_2G, False, DIRECTION_MOBILE_ORIGINATED)
    283 
    284     @test_tracker_info(uuid="549271ff-1034-4d02-8d92-b9d1b2bb912e")
    285     @TelephonyBaseTest.tel_test_wrap
    286     def test_gsm_multi_bearer_mt(self):
    287         """Test gsm data connection before call and in call.
    288 
    289         Turn off airplane mode, disable WiFi, enable Cellular Data.
    290         Make sure phone in GSM, verify Internet.
    291         Initiate a MT voice call. Verify there is no Internet during call.
    292         Hangup Voice Call, verify Internet.
    293 
    294         Returns:
    295             True if success.
    296             False if failed.
    297         """
    298 
    299         return self._test_data_connectivity_multi_bearer(
    300             GEN_2G, False, DIRECTION_MOBILE_TERMINATED)
    301 
    302     @test_tracker_info(uuid="111de471-559a-4bc3-9d3e-de18f098c162")
    303     @TelephonyBaseTest.tel_test_wrap
    304     def test_wcdma_multi_bearer_stress(self):
    305         """Stress Test WCDMA data connection before call and in call.
    306 
    307         This is a stress test for "test_wcdma_multi_bearer".
    308         Default MINIMUM_SUCCESS_RATE is set to 95%.
    309 
    310         Returns:
    311             True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
    312             False otherwise.
    313         """
    314         MINIMUM_SUCCESS_RATE = .95
    315         success_count = 0
    316         fail_count = 0
    317         self.number_of_devices = 2
    318 
    319         for i in range(1, self.stress_test_number + 1):
    320 
    321             ensure_phones_default_state(
    322                 self.log, [self.android_devices[0], self.android_devices[1]])
    323 
    324             if self.test_wcdma_multi_bearer():
    325                 success_count += 1
    326                 result_str = "Succeeded"
    327             else:
    328                 fail_count += 1
    329                 result_str = "Failed"
    330             self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
    331                 i, result_str, success_count, self.stress_test_number))
    332 
    333         self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
    334             success_count, fail_count,
    335             str(100 * success_count / (success_count + fail_count))))
    336         if success_count / (
    337                 success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
    338             return True
    339         else:
    340             return False
    341 
    342     @test_tracker_info(uuid="c7f14ba7-7ac3-45d2-b391-5ed5c4b0e70b")
    343     @TelephonyBaseTest.tel_test_wrap
    344     def test_lte_multi_bearer_stress(self):
    345         """Stress Test LTE data connection before call and in call. (VoLTE call)
    346 
    347         This is a stress test for "test_lte_multi_bearer".
    348         Default MINIMUM_SUCCESS_RATE is set to 95%.
    349 
    350         Returns:
    351             True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
    352             False otherwise.
    353         """
    354         ads = self.android_devices
    355         MINIMUM_SUCCESS_RATE = .95
    356         success_count = 0
    357         fail_count = 0
    358 
    359         for i in range(1, self.stress_test_number + 1):
    360 
    361             ensure_phones_default_state(
    362                 self.log, [self.android_devices[0], self.android_devices[1]])
    363 
    364             if self.test_lte_multi_bearer():
    365                 success_count += 1
    366                 result_str = "Succeeded"
    367             else:
    368                 fail_count += 1
    369                 result_str = "Failed"
    370             self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
    371                 i, result_str, success_count, self.stress_test_number))
    372 
    373         self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
    374             success_count, fail_count,
    375             str(100 * success_count / (success_count + fail_count))))
    376         if success_count / (
    377                 success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
    378             return True
    379         else:
    380             return False
    381 
    382     def _test_data_connectivity_multi_bearer(
    383             self,
    384             nw_gen,
    385             simultaneous_voice_data=True,
    386             call_direction=DIRECTION_MOBILE_ORIGINATED):
    387         """Test data connection before call and in call.
    388 
    389         Turn off airplane mode, disable WiFi, enable Cellular Data.
    390         Make sure phone in <nw_gen>, verify Internet.
    391         Initiate a voice call.
    392         if simultaneous_voice_data is True, then:
    393             Verify Internet.
    394             Disable Cellular Data, verify Internet is inaccessible.
    395             Enable Cellular Data, verify Internet.
    396         if simultaneous_voice_data is False, then:
    397             Verify Internet is not available during voice call.
    398         Hangup Voice Call, verify Internet.
    399 
    400         Returns:
    401             True if success.
    402             False if failed.
    403         """
    404 
    405         class _LocalException(Exception):
    406             pass
    407 
    408         self.number_of_devices = 2
    409         ad_list = [self.android_devices[0], self.android_devices[1]]
    410         ensure_phones_idle(self.log, ad_list)
    411 
    412         if not ensure_network_generation_for_subscription(
    413                 self.log, self.android_devices[0], self.android_devices[0]
    414                 .droid.subscriptionGetDefaultDataSubId(), nw_gen,
    415                 MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
    416             self.log.error("Device failed to reselect in {}s.".format(
    417                 MAX_WAIT_TIME_NW_SELECTION))
    418             return False
    419 
    420         if not wait_for_voice_attach_for_subscription(
    421                 self.log, self.android_devices[0], self.android_devices[0]
    422                 .droid.subscriptionGetDefaultVoiceSubId(),
    423                 MAX_WAIT_TIME_NW_SELECTION):
    424             return False
    425 
    426         self.log.info("Step1 WiFi is Off, Data is on Cell.")
    427         toggle_airplane_mode(self.log, self.android_devices[0], False)
    428         wifi_toggle_state(self.log, self.android_devices[0], False)
    429         self.android_devices[0].droid.telephonyToggleDataConnection(True)
    430         if (not wait_for_cell_data_connection(self.log,
    431                                               self.android_devices[0], True)
    432                 or not verify_internet_connection(self.log,
    433                                                   self.android_devices[0])):
    434             self.log.error("Data not available on cell")
    435             return False
    436 
    437         self.log.info(
    438             "b/69431819, sending data to increase NW threshold limit")
    439         adb_shell_ping(
    440             self.android_devices[0], count=30, timeout=60, loss_tolerance=100)
    441 
    442         try:
    443             self.log.info("Step2 Initiate call and accept.")
    444             if call_direction == DIRECTION_MOBILE_ORIGINATED:
    445                 ad_caller = self.android_devices[0]
    446                 ad_callee = self.android_devices[1]
    447             else:
    448                 ad_caller = self.android_devices[1]
    449                 ad_callee = self.android_devices[0]
    450             if not call_setup_teardown(self.log, ad_caller, ad_callee, None,
    451                                        None, None):
    452                 self.log.error(
    453                     "Failed to Establish {} Voice Call".format(call_direction))
    454                 return False
    455             if simultaneous_voice_data:
    456                 self.log.info("Step3 Verify internet.")
    457                 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
    458                 if not verify_internet_connection(
    459                         self.log, self.android_devices[0], retries=3):
    460                     raise _LocalException("Internet Inaccessible when Enabled")
    461 
    462                 self.log.info("Step4 Turn off data and verify not connected.")
    463                 self.android_devices[0].droid.telephonyToggleDataConnection(
    464                     False)
    465                 if not wait_for_cell_data_connection(
    466                         self.log, self.android_devices[0], False):
    467                     raise _LocalException("Failed to Disable Cellular Data")
    468 
    469                 if verify_internet_connection(self.log,
    470                                               self.android_devices[0]):
    471                     raise _LocalException("Internet Accessible when Disabled")
    472 
    473                 self.log.info("Step5 Re-enable data.")
    474                 self.android_devices[0].droid.telephonyToggleDataConnection(
    475                     True)
    476                 if not wait_for_cell_data_connection(
    477                         self.log, self.android_devices[0], True):
    478                     raise _LocalException("Failed to Re-Enable Cellular Data")
    479                 if not verify_internet_connection(
    480                         self.log, self.android_devices[0], retries=3):
    481                     raise _LocalException("Internet Inaccessible when Enabled")
    482             else:
    483                 self.log.info("Step3 Verify no Internet and skip step 4-5.")
    484                 if verify_internet_connection(
    485                         self.log, self.android_devices[0], retry=0):
    486                     raise _LocalException("Internet Accessible.")
    487 
    488             self.log.info("Step6 Verify phones still in call and Hang up.")
    489             if not verify_incall_state(
    490                     self.log,
    491                 [self.android_devices[0], self.android_devices[1]], True):
    492                 return False
    493             if not hangup_call(self.log, self.android_devices[0]):
    494                 self.log.error("Failed to hang up call")
    495                 return False
    496             if not verify_internet_connection(
    497                     self.log, self.android_devices[0], retries=3):
    498                 raise _LocalException("Internet Inaccessible when Enabled")
    499 
    500         except _LocalException as e:
    501             self.log.error(str(e))
    502             try:
    503                 hangup_call(self.log, self.android_devices[0])
    504                 self.android_devices[0].droid.telephonyToggleDataConnection(
    505                     True)
    506             except Exception:
    507                 pass
    508             return False
    509 
    510         return True
    511 
    512     @test_tracker_info(uuid="dcb9bdc6-dbe2-47e1-9c2d-6f37c529d366")
    513     @TelephonyBaseTest.tel_test_wrap
    514     def test_2g(self):
    515         """Test data connection in 2G.
    516 
    517         Turn off airplane mode, disable WiFi, enable Cellular Data.
    518         Ensure phone data generation is 2G.
    519         Verify Internet.
    520         Disable Cellular Data, verify Internet is inaccessible.
    521         Enable Cellular Data, verify Internet.
    522 
    523         Returns:
    524             True if success.
    525             False if failed.
    526         """
    527         wifi_reset(self.log, self.android_devices[0])
    528         wifi_toggle_state(self.log, self.android_devices[0], False)
    529         return data_connectivity_single_bearer(self.log,
    530                                                self.android_devices[0], RAT_2G)
    531 
    532     @test_tracker_info(uuid="84197a49-d73f-44ce-8b9e-9479e5c4dfdc")
    533     @TelephonyBaseTest.tel_test_wrap
    534     def test_2g_wifi_not_associated(self):
    535         """Test data connection in 2G.
    536 
    537         Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
    538         Ensure phone data generation is 2G.
    539         Verify Internet.
    540         Disable Cellular Data, verify Internet is inaccessible.
    541         Enable Cellular Data, verify Internet.
    542 
    543         Returns:
    544             True if success.
    545             False if failed.
    546         """
    547         wifi_reset(self.log, self.android_devices[0])
    548         wifi_toggle_state(self.log, self.android_devices[0], False)
    549         wifi_toggle_state(self.log, self.android_devices[0], True)
    550         return data_connectivity_single_bearer(self.log,
    551                                                self.android_devices[0], RAT_2G)
    552 
    553     @test_tracker_info(uuid="97067ebb-130a-4fcb-8e6b-f4ec5874828f")
    554     @TelephonyBaseTest.tel_test_wrap
    555     def test_3g(self):
    556         """Test data connection in 3G.
    557 
    558         Turn off airplane mode, disable WiFi, enable Cellular Data.
    559         Ensure phone data generation is 3G.
    560         Verify Internet.
    561         Disable Cellular Data, verify Internet is inaccessible.
    562         Enable Cellular Data, verify Internet.
    563 
    564         Returns:
    565             True if success.
    566             False if failed.
    567         """
    568         wifi_reset(self.log, self.android_devices[0])
    569         wifi_toggle_state(self.log, self.android_devices[0], False)
    570         return data_connectivity_single_bearer(self.log,
    571                                                self.android_devices[0], RAT_3G)
    572 
    573     @test_tracker_info(uuid="ffe2a392-95b8-4a4d-8a6f-bfa846c3462f")
    574     @TelephonyBaseTest.tel_test_wrap
    575     def test_3g_wifi_not_associated(self):
    576         """Test data connection in 3G.
    577 
    578         Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
    579         Ensure phone data generation is 3G.
    580         Verify Internet.
    581         Disable Cellular Data, verify Internet is inaccessible.
    582         Enable Cellular Data, verify Internet.
    583 
    584         Returns:
    585             True if success.
    586             False if failed.
    587         """
    588         wifi_reset(self.log, self.android_devices[0])
    589         wifi_toggle_state(self.log, self.android_devices[0], False)
    590         wifi_toggle_state(self.log, self.android_devices[0], True)
    591         return data_connectivity_single_bearer(self.log,
    592                                                self.android_devices[0], RAT_3G)
    593 
    594     @test_tracker_info(uuid="9c2f459f-1aac-4c68-818b-8698e8124c8b")
    595     @TelephonyBaseTest.tel_test_wrap
    596     def test_4g(self):
    597         """Test data connection in 4g.
    598 
    599         Turn off airplane mode, disable WiFi, enable Cellular Data.
    600         Ensure phone data generation is 4g.
    601         Verify Internet.
    602         Disable Cellular Data, verify Internet is inaccessible.
    603         Enable Cellular Data, verify Internet.
    604 
    605         Returns:
    606             True if success.
    607             False if failed.
    608         """
    609         wifi_reset(self.log, self.android_devices[0])
    610         wifi_toggle_state(self.log, self.android_devices[0], False)
    611         return data_connectivity_single_bearer(self.log,
    612                                                self.android_devices[0], RAT_4G)
    613 
    614     @test_tracker_info(uuid="015a39a1-15ac-4b76-962b-d7d82d52d425")
    615     @TelephonyBaseTest.tel_test_wrap
    616     def test_4g_wifi_not_associated(self):
    617         """Test data connection in 4g.
    618 
    619         Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
    620         Ensure phone data generation is 4g.
    621         Verify Internet.
    622         Disable Cellular Data, verify Internet is inaccessible.
    623         Enable Cellular Data, verify Internet.
    624 
    625         Returns:
    626             True if success.
    627             False if failed.
    628         """
    629         wifi_reset(self.log, self.android_devices[0])
    630         wifi_toggle_state(self.log, self.android_devices[0], False)
    631         wifi_toggle_state(self.log, self.android_devices[0], True)
    632         return data_connectivity_single_bearer(self.log,
    633                                                self.android_devices[0], RAT_4G)
    634 
    635     @test_tracker_info(uuid="44f47b64-f8bc-4a17-9195-42dcca0806bb")
    636     @TelephonyBaseTest.tel_test_wrap
    637     def test_3g_stress(self):
    638         """Stress Test data connection in 3G.
    639 
    640         This is a stress test for "test_3g".
    641         Default MINIMUM_SUCCESS_RATE is set to 95%.
    642 
    643         Returns:
    644             True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
    645             False otherwise.
    646         """
    647         MINIMUM_SUCCESS_RATE = .95
    648         success_count = 0
    649         fail_count = 0
    650 
    651         for i in range(1, self.stress_test_number + 1):
    652 
    653             ensure_phones_default_state(
    654                 self.log, [self.android_devices[0]])
    655             wifi_reset(self.log, self.android_devices[0])
    656             wifi_toggle_state(self.log, self.android_devices[0], False)
    657 
    658             if data_connectivity_single_bearer(
    659                     self.log, self.android_devices[0], RAT_3G):
    660                 success_count += 1
    661                 result_str = "Succeeded"
    662             else:
    663                 fail_count += 1
    664                 result_str = "Failed"
    665             self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
    666                 i, result_str, success_count, self.stress_test_number))
    667 
    668         self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
    669             success_count, fail_count,
    670             str(100 * success_count / (success_count + fail_count))))
    671         if success_count / (
    672                 success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
    673             return True
    674         else:
    675             return False
    676 
    677     @test_tracker_info(uuid="c8876388-0441-4a51-81e6-ac2cb358a531")
    678     @TelephonyBaseTest.tel_test_wrap
    679     def test_4g_stress(self):
    680         """Stress Test data connection in 4g.
    681 
    682         This is a stress test for "test_4g".
    683         Default MINIMUM_SUCCESS_RATE is set to 95%.
    684 
    685         Returns:
    686             True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
    687             False otherwise.
    688         """
    689         MINIMUM_SUCCESS_RATE = .95
    690         success_count = 0
    691         fail_count = 0
    692         self.number_of_devices = 1
    693 
    694         for i in range(1, self.stress_test_number + 1):
    695 
    696             ensure_phones_default_state(
    697                 self.log, [self.android_devices[0]])
    698             wifi_reset(self.log, self.android_devices[0])
    699             wifi_toggle_state(self.log, self.android_devices[0], False)
    700 
    701             if data_connectivity_single_bearer(
    702                     self.log, self.android_devices[0], RAT_4G):
    703                 success_count += 1
    704                 result_str = "Succeeded"
    705             else:
    706                 fail_count += 1
    707                 result_str = "Failed"
    708             self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
    709                 i, result_str, success_count, self.stress_test_number))
    710 
    711         self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
    712             success_count, fail_count,
    713             str(100 * success_count / (success_count + fail_count))))
    714         if success_count / (
    715                 success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
    716             return True
    717         else:
    718             return False
    719 
    720     def _test_setup_tethering(self, network_generation=None):
    721         """Pre setup steps for WiFi tethering test.
    722 
    723         Ensure all ads are idle.
    724         Ensure tethering provider:
    725             turn off APM, turn off WiFI, turn on Data.
    726             have Internet connection, no active ongoing WiFi tethering.
    727 
    728         Returns:
    729             True if success.
    730             False if failed.
    731         """
    732         self.number_of_devices = None
    733         ensure_phones_idle(self.log, self.android_devices)
    734         wifi_toggle_state(self.log, self.provider, False)
    735         if network_generation:
    736             if not ensure_network_generation(
    737                     self.log, self.provider, network_generation,
    738                     MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
    739                 self.provider.log.error("Provider failed to connect to %s.",
    740                                         network_generation)
    741                 return False
    742 
    743         self.provider.log.info(
    744             "Set provider Airplane Off, Wifi Off, Bluetooth Off, Data On.")
    745         toggle_airplane_mode(self.log, self.provider, False)
    746         self.provider.droid.telephonyToggleDataConnection(True)
    747         self.provider.log.info("Provider disable wifi")
    748         wifi_toggle_state(self.log, self.provider, False)
    749         # Turn off active SoftAP if any.
    750         if self.provider.droid.wifiIsApEnabled():
    751             self.provider.log.info("Disable provider wifi tethering")
    752             stop_wifi_tethering(self.log, self.provider)
    753         self.provider.log.info("Provider disable bluetooth")
    754         disable_bluetooth(self.provider.droid)
    755 
    756         for ad in self.clients:
    757             ad.log.info(
    758                 "Set client Airplane Off, Wifi Off, Bluetooth Off, Data Off.")
    759             toggle_airplane_mode(self.log, ad, False)
    760             ad.log.info("Client disable data")
    761             ad.droid.telephonyToggleDataConnection(False)
    762             ad.log.info("Client disable bluetooth")
    763             disable_bluetooth(ad.droid)
    764             ad.log.info("Client disable wifi")
    765             wifi_toggle_state(self.log, ad, False)
    766         if not wait_for_cell_data_connection(self.log, self.provider, True):
    767             self.provider.log.error(
    768                 "Provider failed to enable data connection.")
    769             return False
    770 
    771         self.log.info("Verify internet")
    772         if not self._test_internet_connection(
    773                 client_status=False, provider_status=True):
    774             self.log.error("Internet connection check failed before tethering")
    775             return False
    776 
    777         return True
    778 
    779     def _enable_bluetooth_tethering_connection(self, provider, clients):
    780         for ad in [self.provider] + self.clients:
    781             if not bluetooth_enabled_check(ad):
    782                 ad.log.info("Bluetooth is not enabled")
    783                 return False
    784             else:
    785                 ad.log.info("Bluetooth is enabled")
    786 
    787         for client in self.clients:
    788             if not (pair_pri_to_sec(self.provider, client)):
    789                 client.log.error("Client failed to pair with provider")
    790                 return False
    791             else:
    792                 client.log.info("Client paired with provider")
    793         self.provider.log.info("Provider enabling bluetooth tethering")
    794         try:
    795             provider.droid.bluetoothPanSetBluetoothTethering(True)
    796         except Exception as e:
    797             provider.log.error(
    798                 "Faile to enable provider Bluetooth tethering with %s", e)
    799             return False
    800 
    801         if wait_for_state(provider.droid.bluetoothPanIsTetheringOn, True):
    802             provider.log.info("Provider Bluetooth tethering is enabled.")
    803         else:
    804             provider.log.error(
    805                 "Failed to enable provider Bluetooth tethering.")
    806             provider.log.error("bluetoothPanIsTetheringOn = %s",
    807                                provider.droid.bluetoothPanIsTetheringOn())
    808             return False
    809         time.sleep(5)
    810         for client in clients:
    811             client.droid.bluetoothConnectBonded(
    812                 provider.droid.bluetoothGetLocalAddress())
    813         time.sleep(20)
    814         return True
    815 
    816     def _test_internet_connection(self,
    817                                   client_status=True,
    818                                   provider_status=True):
    819         client_retry = 10 if client_status else 1
    820         for client in self.clients:
    821             if not verify_http_connection(
    822                     self.log,
    823                     client,
    824                     retry=client_retry,
    825                     expected_state=client_status):
    826                 client.log.error("client internet connection state is not %s",
    827                                  client_status)
    828                 return False
    829             else:
    830                 client.log.info("client internet connection state is %s",
    831                                 client_status)
    832         if not verify_http_connection(
    833                 self.log, self.provider, retry=3,
    834                 expected_state=provider_status):
    835             self.provider.log.error(
    836                 "provider internet connection is not %s" % provider_status)
    837             return False
    838         else:
    839             self.provider.log.info(
    840                 "provider internet connection is %s" % provider_status)
    841         return True
    842 
    843     def _verify_bluetooth_tethering_connection(self,
    844                                                change_rat=None,
    845                                                toggle_data=False,
    846                                                toggle_tethering=False,
    847                                                voice_call=False,
    848                                                toggle_bluetooth=True):
    849         """Setups up a bluetooth tethering conenction between two android devices.
    850 
    851         Returns:
    852             True if PAN connection and verification is successful,
    853             false if unsuccessful.
    854         """
    855         if not self._enable_bluetooth_tethering_connection(
    856                 self.provider, self.clients):
    857             return False
    858         if not self._test_internet_connection():
    859             self.log.error("Internet connection check failed")
    860             return False
    861         if voice_call:
    862             self.log.info("====== Voice call test =====")
    863             for caller, callee in [(self.provider, self.clients[0]),
    864                                    (self.clients[0], self.provider)]:
    865                 if not call_setup_teardown(
    866                         self.log, caller, callee, ad_hangup=None):
    867                     self.log.error("Setup Call Failed.")
    868                     hangup_call(self.log, caller)
    869                     return False
    870                 self.log.info("Verify data.")
    871                 if not verify_http_connection(
    872                         self.log, self.clients[0], retry=0):
    873                     self.clients[0].log.warning(
    874                         "client internet connection state is not on")
    875                 else:
    876                     self.clients[0].log.info(
    877                         "client internet connection state is on")
    878                 hangup_call(self.log, caller)
    879                 if not verify_http_connection(
    880                         self.log, self.clients[0], retry=0):
    881                     self.clients[0].log.warning(
    882                         "client internet connection state is not on")
    883                     return False
    884                 else:
    885                     self.clients[0].log.info(
    886                         "client internet connection state is on")
    887         if toggle_tethering:
    888             self.log.info("====== Toggling provider bluetooth tethering =====")
    889             self.provider.log.info("Disable bluetooth tethering")
    890             self.provider.droid.bluetoothPanSetBluetoothTethering(False)
    891             if not self._test_internet_connection(False, True):
    892                 self.log.error(
    893                     "Internet connection check failed after disable tethering")
    894                 return False
    895             self.provider.log.info("Enable bluetooth tethering")
    896             if not self._enable_bluetooth_tethering_connection(
    897                     self.provider, self.clients):
    898                 self.provider.log.error(
    899                     "Fail to re-enable bluetooth tethering")
    900                 return False
    901             if not self._test_internet_connection(True, True):
    902                 self.log.error(
    903                     "Internet connection check failed after enable tethering")
    904                 return False
    905         if toggle_bluetooth:
    906             self.log.info("====== Toggling provider bluetooth =====")
    907             self.provider.log.info("Disable provider bluetooth")
    908             disable_bluetooth(self.provider.droid)
    909             if not self._test_internet_connection(False, True):
    910                 self.log.error(
    911                     "Internet connection check failed after disable bluetooth")
    912                 return False
    913             if not self._enable_bluetooth_tethering_connection(
    914                     self.provider, self.clients):
    915                 self.provider.log.error(
    916                     "Fail to re-enable bluetooth tethering")
    917                 return False
    918             if not self._test_internet_connection(True, True):
    919                 self.log.error(
    920                     "Internet connection check failed after enable bluetooth")
    921                 return False
    922         if toggle_data:
    923             self.log.info("===== Toggling provider data connection =====")
    924             self.provider.log.info("Disable provider data connection")
    925             self.provider.droid.telephonyToggleDataConnection(False)
    926 
    927             if not self._test_internet_connection(False, False):
    928                 return False
    929             self.provider.log.info("Enable provider data connection")
    930             self.provider.droid.telephonyToggleDataConnection(True)
    931             if not wait_for_cell_data_connection(self.log, self.provider,
    932                                                  True):
    933                 self.provider.log.error(
    934                     "Provider failed to enable data connection.")
    935                 return False
    936             if not self._test_internet_connection(True, True):
    937                 self.log.error(
    938                     "Internet connection check failed after enable data")
    939                 return False
    940         if change_rat:
    941             self.log.info("===== Change provider RAT to %s =====", change_rat)
    942             if not ensure_network_generation(
    943                     self.log,
    944                     self.provider,
    945                     change_rat,
    946                     voice_or_data=NETWORK_SERVICE_DATA,
    947                     toggle_apm_after_setting=False):
    948                 self.provider.log.error("Provider failed to reselect to %s.",
    949                                         change_rat)
    950                 return False
    951             if not self._test_internet_connection(True, True):
    952                 self.log.error(
    953                     "Internet connection check failed after RAT change to %s",
    954                     change_rat)
    955                 return False
    956         return True
    957 
    958     @test_tracker_info(uuid="2d945656-22f7-4610-9a84-40ce04d603a4")
    959     @TelephonyBaseTest.tel_test_wrap
    960     def test_tethering_4g_to_bluetooth(self):
    961         """Bluetooth Tethering test: LTE to Bluetooth Tethering
    962 
    963         1. DUT in LTE mode, idle.
    964         2. DUT start Bluetooth Tethering
    965         3. PhoneB disable data, connect to DUT's softAP
    966         4. Verify Internet access on DUT and PhoneB
    967         5. Toggle provider bluetooth connection
    968         6. Verify Internet access on DUT and PhoneB
    969 
    970         Returns:
    971             True if success.
    972             False if failed.
    973         """
    974         if not self._test_setup_tethering(RAT_4G):
    975             self.log.error("Verify 4G Internet access failed.")
    976             return False
    977 
    978         return self._verify_bluetooth_tethering_connection()
    979 
    980     @test_tracker_info(uuid="8d2ae56b-c2c1-4c32-9b8e-5044007b5b90")
    981     @TelephonyBaseTest.tel_test_wrap
    982     def test_tethering_4g_to_bluetooth_with_voice_call(self):
    983         """Bluetooth Tethering test: LTE to Bluetooth Tethering
    984 
    985         1. DUT in LTE mode, idle.
    986         2. DUT start Bluetooth Tethering
    987         3. PhoneB disable data, connect to DUT's softAP
    988         4. Verify Internet access on DUT and PhoneB
    989         5. Verify provider and client are able to make or receive phone call
    990         6. Verify Internet access on DUT and PhoneB
    991 
    992         Returns:
    993             True if success.
    994             False if failed.
    995         """
    996         if not self._test_setup_tethering(RAT_4G):
    997             self.log.error("Verify 4G Internet access failed.")
    998             return False
    999 
   1000         return self._verify_bluetooth_tethering_connection(
   1001             toggle_tethering=False, toggle_bluetooth=False, voice_call=True)
   1002 
   1003     @test_tracker_info(uuid="b4617727-fa83-4451-89d7-7e574c0a0938")
   1004     @TelephonyBaseTest.tel_test_wrap
   1005     def test_tethering_4g_to_bluetooth_toggle_data(self):
   1006         """Bluetooth Tethering test: LTE to Bluetooth Tethering
   1007 
   1008         1. DUT in LTE mode, idle.
   1009         2. DUT start Bluetooth Tethering
   1010         3. PhoneB disable data, connect to DUT's softAP
   1011         4. Verify Internet access on DUT and PhoneB
   1012         5. Toggle provider data connection
   1013         6. Verify Internet access on DUT and PhoneB
   1014 
   1015         Returns:
   1016             True if success.
   1017             False if failed.
   1018         """
   1019         if not self._test_setup_tethering(RAT_4G):
   1020             self.log.error("Verify 4G Internet access failed.")
   1021             return False
   1022 
   1023         return self._verify_bluetooth_tethering_connection(
   1024             toggle_tethering=False, toggle_bluetooth=False, toggle_data=True)
   1025 
   1026     @test_tracker_info(uuid="6a0f6001-609d-41f2-ad09-c8ae19f73ac8")
   1027     @TelephonyBaseTest.tel_test_wrap
   1028     def test_tethering_4g_to_bluetooth_toggle_tethering(self):
   1029         """Bluetooth Tethering test: LTE to Bluetooth Tethering
   1030 
   1031         1. DUT in LTE mode, idle.
   1032         2. DUT start Bluetooth Tethering
   1033         3. PhoneB disable data, connect to DUT's softAP
   1034         4. Verify Internet access on DUT and PhoneB
   1035         5. Toggle provider bluetooth tethering
   1036         6. Verify Internet access on DUT and PhoneB
   1037 
   1038         Returns:
   1039             True if success.
   1040             False if failed.
   1041         """
   1042         if not self._test_setup_tethering(RAT_4G):
   1043             self.log.error("Verify 4G Internet access failed.")
   1044             return False
   1045 
   1046         return self._verify_bluetooth_tethering_connection(
   1047             toggle_tethering=True, toggle_bluetooth=False, toggle_data=False)
   1048 
   1049     @test_tracker_info(uuid="b1abc1ac-8018-4956-a17e-bf2ceaf264ea")
   1050     @TelephonyBaseTest.tel_test_wrap
   1051     def test_tethering_3g_to_bluetooth(self):
   1052         """Bluetooth Tethering test: 3G to Bluetoothing Tethering
   1053 
   1054         1. DUT in 3G mode, idle.
   1055         2. DUT start bluetooth Tethering
   1056         3. PhoneB disable data, connect to DUT's softAP
   1057         4. Verify Internet access on DUT and PhoneB
   1058         5. Toggle provider bluetooth connection
   1059         6. Verify Internet access on DUT and PhoneB
   1060 
   1061         Returns:
   1062             True if success.
   1063             False if failed.
   1064         """
   1065         if not self._test_setup_tethering(RAT_3G):
   1066             self.log.error("Verify 3G Internet access failed.")
   1067             return False
   1068 
   1069         return self._verify_bluetooth_tethering_connection()
   1070 
   1071     @test_tracker_info(uuid="69793745-0c49-4cef-9879-d372e3a3f4c7")
   1072     @TelephonyBaseTest.tel_test_wrap
   1073     def test_tethering_3g_to_bluetooth_with_voice_call(self):
   1074         """Bluetooth Tethering test: 3G to Bluetooth Tethering
   1075 
   1076         1. DUT in 3G mode, idle.
   1077         2. DUT start Bluetooth Tethering
   1078         3. PhoneB disable data, connect to DUT's softAP
   1079         4. Verify Internet access on DUT and PhoneB
   1080         5. Verify provider and client are able to make or receive phone call
   1081         6. Verify Internet access on DUT and PhoneB
   1082 
   1083         Returns:
   1084             True if success.
   1085             False if failed.
   1086         """
   1087         if not self._test_setup_tethering(RAT_3G):
   1088             self.log.error("Verify 3G Internet access failed.")
   1089             return False
   1090 
   1091         return self._verify_bluetooth_tethering_connection(
   1092             toggle_tethering=False, toggle_bluetooth=False, voice_call=True)
   1093 
   1094     @test_tracker_info(uuid="4275ee69-dfdf-4f47-82c5-4224fceee761")
   1095     @TelephonyBaseTest.tel_test_wrap
   1096     def test_tethering_3g_to_bluetooth_toggle_data(self):
   1097         """Bluetooth Tethering test: 3G to Bluetoothing Tethering
   1098 
   1099         1. DUT in 3G mode, idle.
   1100         2. DUT start bluetooth Tethering
   1101         3. PhoneB disable data, connect to DUT's softAP
   1102         4. Verify Internet access on DUT and PhoneB
   1103         5. Toggle provider data connection
   1104         6. Verify Internet access on DUT and PhoneB
   1105 
   1106         Returns:
   1107             True if success.
   1108             False if failed.
   1109         """
   1110         if not self._test_setup_tethering(RAT_3G):
   1111             self.log.error("Verify 3G Internet access failed.")
   1112             return False
   1113 
   1114         return self._verify_bluetooth_tethering_connection(
   1115             toggle_tethering=False, toggle_bluetooth=False, toggle_data=True)
   1116 
   1117     @test_tracker_info(uuid="db0e0f27-1a4f-4301-832d-b66415e289f3")
   1118     @TelephonyBaseTest.tel_test_wrap
   1119     def test_tethering_2g_to_bluetooth(self):
   1120         """Bluetooth Tethering test: 2G to Bluetooth Tethering
   1121 
   1122         1. DUT in 2G mode, idle.
   1123         2. DUT start bluetooth Tethering
   1124         3. PhoneB disable data, connect to DUT's softAP
   1125         4. Verify Internet access on DUT and PhoneB
   1126         5. Toggle provider bluetooth connection
   1127         6. Verify Internet access on DUT and PhoneB
   1128 
   1129         Returns:
   1130             True if success.
   1131             False if failed.
   1132         """
   1133         if not self._test_setup_tethering(RAT_2G):
   1134             self.log.error("Verify 3G Internet access failed.")
   1135             return False
   1136 
   1137         return self._verify_bluetooth_tethering_connection()
   1138 
   1139     @test_tracker_info(uuid="584e9fa5-a38e-47cd-aa33-fcf8d72c423e")
   1140     @TelephonyBaseTest.tel_test_wrap
   1141     def test_tethering_2g_to_bluetooth_with_voice_call(self):
   1142         """Bluetooth Tethering test: 2G to Bluetooth Tethering
   1143 
   1144         1. DUT in 2G mode, idle.
   1145         2. DUT start Bluetooth Tethering
   1146         3. PhoneB disable data, connect to DUT's softAP
   1147         4. Verify Internet access on DUT and PhoneB
   1148         5. Verify provider and client are able to make or receive phone call
   1149         6. Verify Internet access on DUT and PhoneB
   1150 
   1151         Returns:
   1152             True if success.
   1153             False if failed.
   1154         """
   1155         if not self._test_setup_tethering(RAT_2G):
   1156             self.log.error("Verify 2G Internet access failed.")
   1157             return False
   1158 
   1159         return self._verify_bluetooth_tethering_connection(
   1160             toggle_tethering=False, toggle_bluetooth=False, voice_call=True)
   1161 
   1162     @test_tracker_info(uuid="be3e74f9-3dc8-4b72-8a33-32bff0868a44")
   1163     @TelephonyBaseTest.tel_test_wrap
   1164     def test_tethering_2g_to_bluetooth_toggle_data(self):
   1165         """Bluetooth Tethering test: 2G to Bluetooth Tethering
   1166 
   1167         1. DUT in 2G mode, idle.
   1168         2. DUT start Bluetooth Tethering
   1169         3. PhoneB disable data, connect to DUT's softAP
   1170         4. Verify Internet access on DUT and PhoneB
   1171         5. Toggle provider data connection
   1172         6. Verify Internet access on DUT and PhoneB
   1173 
   1174         Returns:
   1175             True if success.
   1176             False if failed.
   1177         """
   1178         if not self._test_setup_tethering(RAT_2G):
   1179             self.log.error("Verify 4G Internet access failed.")
   1180             return False
   1181 
   1182         return self._verify_bluetooth_tethering_connection(
   1183             toggle_tethering=False, toggle_bluetooth=False, toggle_data=True)
   1184 
   1185     @test_tracker_info(uuid="4a106549-0bfa-4c8f-8e66-edec93fabadf")
   1186     @TelephonyBaseTest.tel_test_wrap
   1187     def test_tethering_rat_from_4g_to_3g_bluetooth(self):
   1188         """Bluetooth Tethering test: 2G to Bluetooth Tethering
   1189 
   1190         1. DUT in 4G mode, idle.
   1191         2. DUT start bluetooth Tethering
   1192         3. PhoneB disable data, connect to DUT's softAP
   1193         4. Verify Internet access on DUT and PhoneB
   1194         5. Change provider RAT to 3G
   1195         6. Verify Internet access on DUT and PhoneB
   1196 
   1197         Returns:
   1198             True if success.
   1199             False if failed.
   1200         """
   1201         if not self._test_setup_tethering(RAT_4G):
   1202             self.log.error("Verify 3G Internet access failed.")
   1203             return False
   1204 
   1205         return self._verify_bluetooth_tethering_connection(
   1206             toggle_tethering=False,
   1207             toggle_bluetooth=False,
   1208             toggle_data=False,
   1209             change_rat=RAT_3G)
   1210 
   1211     @test_tracker_info(uuid="eaa5b61b-f054-437f-ae82-8d80f6487785")
   1212     @TelephonyBaseTest.tel_test_wrap
   1213     def test_tethering_rat_from_4g_to_2g_bluetooth(self):
   1214         """Bluetooth Tethering test: 2G to Bluetooth Tethering
   1215 
   1216         1. DUT in 4G mode, idle.
   1217         2. DUT start bluetooth Tethering
   1218         3. PhoneB disable data, connect to DUT's softAP
   1219         4. Verify Internet access on DUT and PhoneB
   1220         5. Change provider RAT to 2G
   1221         6. Verify Internet access on DUT and PhoneB
   1222 
   1223         Returns:
   1224             True if success.
   1225             False if failed.
   1226         """
   1227         if not self._test_setup_tethering(RAT_4G):
   1228             self.log.error("Verify 3G Internet access failed.")
   1229             return False
   1230 
   1231         return self._verify_bluetooth_tethering_connection(
   1232             toggle_tethering=False,
   1233             toggle_bluetooth=False,
   1234             toggle_data=False,
   1235             change_rat=RAT_2G)
   1236 
   1237     @test_tracker_info(uuid="912a11a3-14b3-4928-885f-cea69f14a571")
   1238     @TelephonyBaseTest.tel_test_wrap
   1239     def test_tethering_4g_to_2gwifi(self):
   1240         """WiFi Tethering test: LTE to WiFI 2.4G Tethering
   1241 
   1242         1. DUT in LTE mode, idle.
   1243         2. DUT start 2.4G WiFi Tethering
   1244         3. PhoneB disable data, connect to DUT's softAP
   1245         4. Verify Internet access on DUT and PhoneB
   1246 
   1247         Returns:
   1248             True if success.
   1249             False if failed.
   1250         """
   1251         if not self._test_setup_tethering(RAT_4G):
   1252             self.log.error("Verify 4G Internet access failed.")
   1253             return False
   1254 
   1255         return wifi_tethering_setup_teardown(
   1256             self.log,
   1257             self.provider, self.clients,
   1258             ap_band=WIFI_CONFIG_APBAND_2G,
   1259             check_interval=10,
   1260             check_iteration=10)
   1261 
   1262     @test_tracker_info(uuid="743e3998-d39f-42b9-b11f-009dcee34f3f")
   1263     @TelephonyBaseTest.tel_test_wrap
   1264     def test_tethering_4g_to_5gwifi(self):
   1265         """WiFi Tethering test: LTE to WiFI 5G Tethering
   1266 
   1267         1. DUT in LTE mode, idle.
   1268         2. DUT start 5G WiFi Tethering
   1269         3. PhoneB disable data, connect to DUT's softAP
   1270         4. Verify Internet access on DUT and PhoneB
   1271 
   1272         Returns:
   1273             True if success.
   1274             False if failed.
   1275         """
   1276         num = len(self.android_devices)
   1277         for idx, ad in enumerate(self.android_devices):
   1278             self.provider = self.android_devices[idx]
   1279             self.clients = self.android_devices[:idx] + self.android_devices[
   1280                                                         idx+1:]
   1281             if not self._test_setup_tethering(RAT_4G):
   1282                 ad.log.error("Verify 4G Internet access failed.")
   1283                 continue
   1284 
   1285             if not self.provider.droid.carrierConfigIsTetheringModeAllowed(
   1286                 TETHERING_MODE_WIFI, MAX_WAIT_TIME_TETHERING_ENTITLEMENT_CHECK):
   1287                 ad.log.info("Tethering is not entitled")
   1288                 continue
   1289 
   1290             if wifi_tethering_setup_teardown(self.log, self.provider,
   1291                                              [self.clients[0]],
   1292                                              ap_band=WIFI_CONFIG_APBAND_5G,
   1293                                              check_interval=10,
   1294                                              check_iteration=10):
   1295                 self.android_devices = [self.provider] + self.clients
   1296                 return True
   1297             elif idx == num - 1:
   1298                 self.log.error("Tethering is not working on all devices")
   1299                 return False
   1300         self.log.error("Faile to enable tethering on all devices")
   1301         return False
   1302 
   1303     @test_tracker_info(uuid="59be8d68-f05b-4448-8584-de971174fd81")
   1304     @TelephonyBaseTest.tel_test_wrap
   1305     def test_tethering_3g_to_2gwifi(self):
   1306         """WiFi Tethering test: 3G to WiFI 2.4G Tethering
   1307 
   1308         1. DUT in 3G mode, idle.
   1309         2. DUT start 2.4G WiFi Tethering
   1310         3. PhoneB disable data, connect to DUT's softAP
   1311         4. Verify Internet access on DUT and PhoneB
   1312 
   1313         Returns:
   1314             True if success.
   1315             False if failed.
   1316         """
   1317         if not self._test_setup_tethering(RAT_3G):
   1318             self.log.error("Verify 3G Internet access failed.")
   1319             return False
   1320 
   1321         return wifi_tethering_setup_teardown(
   1322             self.log,
   1323             self.provider, self.clients,
   1324             ap_band=WIFI_CONFIG_APBAND_2G,
   1325             check_interval=10,
   1326             check_iteration=10)
   1327 
   1328     @test_tracker_info(uuid="1be6b741-92e8-4ee1-9f59-e7f9f369b065")
   1329     @TelephonyBaseTest.tel_test_wrap
   1330     def test_tethering_3g_to_5gwifi(self):
   1331         """WiFi Tethering test: 3G to WiFI 5G Tethering
   1332 
   1333         1. DUT in 3G mode, idle.
   1334         2. DUT start 5G WiFi Tethering
   1335         3. PhoneB disable data, connect to DUT's softAP
   1336         4. Verify Internet access on DUT and PhoneB
   1337 
   1338         Returns:
   1339             True if success.
   1340             False if failed.
   1341         """
   1342         if not self._test_setup_tethering(RAT_3G):
   1343             self.log.error("Verify 3G Internet access failed.")
   1344             return False
   1345 
   1346         return wifi_tethering_setup_teardown(
   1347             self.log,
   1348             self.provider, self.clients,
   1349             ap_band=WIFI_CONFIG_APBAND_5G,
   1350             check_interval=10,
   1351             check_iteration=10)
   1352 
   1353     @test_tracker_info(uuid="89fe6321-4c0d-40c0-89b2-54008ecca68f")
   1354     @TelephonyBaseTest.tel_test_wrap
   1355     def test_tethering_2g_to_2gwifi(self):
   1356         """WiFi Tethering test: 2G to WiFI 2.4G Tethering
   1357 
   1358         1. DUT in 2G mode, idle.
   1359         2. DUT start 2.4G WiFi Tethering
   1360         3. PhoneB disable data, connect to DUT's softAP
   1361         4. Verify Internet access on DUT and PhoneB
   1362 
   1363         Returns:
   1364             True if success.
   1365             False if failed.
   1366         """
   1367         if not self._test_setup_tethering(RAT_2G):
   1368             self.log.error("Verify 2G Internet access failed.")
   1369             return False
   1370 
   1371         return wifi_tethering_setup_teardown(
   1372             self.log,
   1373             self.provider, [self.clients[0]],
   1374             ap_band=WIFI_CONFIG_APBAND_2G,
   1375             check_interval=10,
   1376             check_iteration=10)
   1377 
   1378     @test_tracker_info(uuid="b8258d51-9581-4d52-80b6-501941ec1191")
   1379     @TelephonyBaseTest.tel_test_wrap
   1380     def test_tethering_2g_to_5gwifi(self):
   1381         """WiFi Tethering test: 2G to WiFI 5G Tethering
   1382 
   1383         1. DUT in 2G mode, idle.
   1384         2. DUT start 5G WiFi Tethering
   1385         3. PhoneB disable data, connect to DUT's softAP
   1386         4. Verify Internet access on DUT and PhoneB
   1387 
   1388         Returns:
   1389             True if success.
   1390             False if failed.
   1391         """
   1392         if not self._test_setup_tethering(RAT_2G):
   1393             self.log.error("Verify 2G Internet access failed.")
   1394             return False
   1395 
   1396         return wifi_tethering_setup_teardown(
   1397             self.log,
   1398             self.provider, [self.clients[0]],
   1399             ap_band=WIFI_CONFIG_APBAND_5G,
   1400             check_interval=10,
   1401             check_iteration=10)
   1402 
   1403     @test_tracker_info(uuid="8ed766a6-71c5-4b3b-8897-a4e796c75619")
   1404     @TelephonyBaseTest.tel_test_wrap
   1405     def test_disable_wifi_tethering_resume_connected_wifi(self):
   1406         """WiFi Tethering test: WiFI connected to 2.4G network,
   1407         start (LTE) 2.4G WiFi tethering, then stop tethering
   1408 
   1409         1. DUT in data connected, idle. WiFi connected to 2.4G Network
   1410         2. DUT start 2.4G WiFi Tethering
   1411         3. PhoneB disable data, connect to DUT's softAP
   1412         4. Verify Internet access on DUT and PhoneB
   1413         5. Disable WiFi Tethering on DUT.
   1414         6. Verify DUT automatically connect to previous WiFI network
   1415 
   1416         Returns:
   1417             True if success.
   1418             False if failed.
   1419         """
   1420         if not self._test_setup_tethering():
   1421             self.log.error("Verify provider Internet access failed.")
   1422             return False
   1423         self.log.info("Connect WiFi.")
   1424         if not ensure_wifi_connected(self.log, self.provider,
   1425                                      self.wifi_network_ssid,
   1426                                      self.wifi_network_pass):
   1427             self.log.error("WiFi connect fail.")
   1428             return False
   1429         self.log.info("Start WiFi Tethering.")
   1430         if not wifi_tethering_setup_teardown(
   1431                 self.log,
   1432                 self.provider, [self.clients[0]],
   1433                 check_interval=10,
   1434                 check_iteration=2):
   1435             self.log.error("WiFi Tethering failed.")
   1436             return False
   1437 
   1438         if (not wait_for_wifi_data_connection(self.log, self.provider, True)
   1439                 or not verify_internet_connection(self.log, self.provider)):
   1440             self.log.error("Provider data did not return to Wifi")
   1441             return False
   1442         return True
   1443 
   1444     @test_tracker_info(uuid="b879ceb2-1b80-4762-93f9-afef4d688c28")
   1445     @TelephonyBaseTest.tel_test_wrap
   1446     def test_toggle_data_during_active_wifi_tethering(self):
   1447         """WiFi Tethering test: Toggle Data during active WiFi Tethering
   1448 
   1449         1. DUT data connection is on and idle.
   1450         2. DUT start 2.4G WiFi Tethering
   1451         3. PhoneB disable data, connect to DUT's softAP
   1452         4. Verify Internet access on DUT and PhoneB
   1453         5. Disable Data on DUT, verify PhoneB still connected to WiFi, but no Internet access.
   1454         6. Enable Data on DUT, verify PhoneB still connected to WiFi and have Internet access.
   1455 
   1456         Returns:
   1457             True if success.
   1458             False if failed.
   1459         """
   1460         if not self._test_setup_tethering():
   1461             self.log.error("Provider Internet access check failed.")
   1462             return False
   1463         try:
   1464             ssid = rand_ascii_str(10)
   1465             if not wifi_tethering_setup_teardown(
   1466                     self.log,
   1467                     self.provider, [self.clients[0]],
   1468                     ap_band=WIFI_CONFIG_APBAND_2G,
   1469                     check_interval=10,
   1470                     check_iteration=2,
   1471                     do_cleanup=False,
   1472                     ssid=ssid):
   1473                 self.log.error("WiFi Tethering failed.")
   1474                 return False
   1475 
   1476             if not self.provider.droid.wifiIsApEnabled():
   1477                 self.provider.log.error("Provider WiFi tethering stopped.")
   1478                 return False
   1479 
   1480             self.provider.log.info(
   1481                 "Disable Data on Provider, verify no data on Client.")
   1482             self.provider.droid.telephonyToggleDataConnection(False)
   1483             time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
   1484             if verify_internet_connection(self.log, self.provider):
   1485                 self.provider.log.error("Disable data on provider failed.")
   1486                 return False
   1487             if not self.provider.droid.wifiIsApEnabled():
   1488                 self.provider.log.error("Provider WiFi tethering stopped.")
   1489                 return False
   1490             if not check_is_wifi_connected(self.log, self.clients[0], ssid):
   1491                 self.clients[0].log.error("Client WiFi is not connected")
   1492                 return False
   1493 
   1494             self.log.info(
   1495                 "Enable Data on Provider, verify data available on Client.")
   1496             self.provider.droid.telephonyToggleDataConnection(True)
   1497             if not wait_for_cell_data_connection(self.log, self.provider,
   1498                                                  True):
   1499                 self.provider.log.error(
   1500                     "Provider failed to enable data connection.")
   1501                 return False
   1502             if not verify_internet_connection(self.log, self.provider):
   1503                 self.provider.log.error(
   1504                     "Provider internet connection check failed.")
   1505                 return False
   1506             if not self.provider.droid.wifiIsApEnabled():
   1507                 self.provider.log.error("Provider WiFi tethering stopped.")
   1508                 return False
   1509 
   1510             if not check_is_wifi_connected(
   1511                     self.log, self.clients[0],
   1512                     ssid) or (not verify_internet_connection(
   1513                         self.log, self.clients[0])):
   1514                 self.clients[0].log.error(
   1515                     "Client wifi connection check failed!")
   1516                 return False
   1517         finally:
   1518             if not wifi_tethering_cleanup(self.log, self.provider,
   1519                                           self.clients):
   1520                 return False
   1521         return True
   1522 
   1523     # Invalid Live Test. Can't rely on the result of this test with live network.
   1524     # Network may decide not to change the RAT when data conenction is active.
   1525     @test_tracker_info(uuid="c92a961b-e85d-435c-8988-052928add591")
   1526     @TelephonyBaseTest.tel_test_wrap
   1527     def test_change_rat_during_active_wifi_tethering_lte_to_3g(self):
   1528         """WiFi Tethering test: Change Cellular Data RAT generation from LTE to 3G,
   1529             during active WiFi Tethering.
   1530 
   1531         1. DUT in LTE mode, idle.
   1532         2. DUT start 2.4G WiFi Tethering
   1533         3. PhoneB disable data, connect to DUT's softAP
   1534         4. Verily Internet access on DUT and PhoneB
   1535         5. Change DUT Cellular Data RAT generation from LTE to 3G.
   1536         6. Verify both DUT and PhoneB have Internet access.
   1537 
   1538         Returns:
   1539             True if success.
   1540             False if failed.
   1541         """
   1542         if not self._test_setup_tethering(RAT_4G):
   1543             self.log.error("Verify 4G Internet access failed.")
   1544             return False
   1545         try:
   1546             if not wifi_tethering_setup_teardown(
   1547                     self.log,
   1548                     self.provider, [self.clients[0]],
   1549                     ap_band=WIFI_CONFIG_APBAND_2G,
   1550                     check_interval=10,
   1551                     check_iteration=2,
   1552                     do_cleanup=False):
   1553                 self.log.error("WiFi Tethering failed.")
   1554                 return False
   1555 
   1556             if not self.provider.droid.wifiIsApEnabled():
   1557                 self.provider.log.error("Provider WiFi tethering stopped.")
   1558                 return False
   1559 
   1560             self.log.info("Provider change RAT from LTE to 3G.")
   1561             if not ensure_network_generation(
   1562                     self.log,
   1563                     self.provider,
   1564                     RAT_3G,
   1565                     voice_or_data=NETWORK_SERVICE_DATA,
   1566                     toggle_apm_after_setting=False):
   1567                 self.provider.log.error("Provider failed to reselect to 3G.")
   1568                 return False
   1569             time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
   1570             if not verify_internet_connection(self.log, self.provider):
   1571                 self.provider.log.error("Data not available on Provider.")
   1572                 return False
   1573             if not self.provider.droid.wifiIsApEnabled():
   1574                 self.provider.log.error("Provider WiFi tethering stopped.")
   1575                 return False
   1576             if not tethering_check_internet_connection(
   1577                     self.log, self.provider, [self.clients[0]], 10, 5):
   1578                 return False
   1579         finally:
   1580             if not wifi_tethering_cleanup(self.log, self.provider,
   1581                                           self.clients):
   1582                 return False
   1583         return True
   1584 
   1585     # Invalid Live Test. Can't rely on the result of this test with live network.
   1586     # Network may decide not to change the RAT when data conenction is active.
   1587     @test_tracker_info(uuid="eb5f0180-b70d-436f-8fcb-60c59307cc43")
   1588     @TelephonyBaseTest.tel_test_wrap
   1589     def test_change_rat_during_active_wifi_tethering_3g_to_lte(self):
   1590         """WiFi Tethering test: Change Cellular Data RAT generation from 3G to LTE,
   1591             during active WiFi Tethering.
   1592 
   1593         1. DUT in 3G mode, idle.
   1594         2. DUT start 2.4G WiFi Tethering
   1595         3. PhoneB disable data, connect to DUT's softAP
   1596         4. Verily Internet access on DUT and PhoneB
   1597         5. Change DUT Cellular Data RAT generation from 3G to LTE.
   1598         6. Verify both DUT and PhoneB have Internet access.
   1599 
   1600         Returns:
   1601             True if success.
   1602             False if failed.
   1603         """
   1604         if not self._test_setup_tethering(RAT_3G):
   1605             self.log.error("Verify 3G Internet access failed.")
   1606             return False
   1607         try:
   1608             if not wifi_tethering_setup_teardown(
   1609                     self.log,
   1610                     self.provider, [self.clients[0]],
   1611                     ap_band=WIFI_CONFIG_APBAND_2G,
   1612                     check_interval=10,
   1613                     check_iteration=2,
   1614                     do_cleanup=False):
   1615                 self.log.error("WiFi Tethering failed.")
   1616                 return False
   1617 
   1618             if not self.provider.droid.wifiIsApEnabled():
   1619                 self.log.error("Provider WiFi tethering stopped.")
   1620                 return False
   1621 
   1622             self.log.info("Provider change RAT from 3G to 4G.")
   1623             if not ensure_network_generation(
   1624                     self.log,
   1625                     self.provider,
   1626                     RAT_4G,
   1627                     voice_or_data=NETWORK_SERVICE_DATA,
   1628                     toggle_apm_after_setting=False):
   1629                 self.log.error("Provider failed to reselect to 4G.")
   1630                 return False
   1631             time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
   1632             if not verify_internet_connection(self.log, self.provider):
   1633                 self.provider.log.error("Data not available on Provider.")
   1634                 return False
   1635             if not self.provider.droid.wifiIsApEnabled():
   1636                 self.provider.log.error("Provider WiFi tethering stopped.")
   1637                 return False
   1638             if not tethering_check_internet_connection(
   1639                     self.log, self.provider, [self.clients[0]], 10, 5):
   1640                 return False
   1641         finally:
   1642             if not wifi_tethering_cleanup(self.log, ads[0], [ads[1]]):
   1643                 return False
   1644         return True
   1645 
   1646     @test_tracker_info(uuid="12a6c910-fa96-4d9b-99a5-8391fea33732")
   1647     @TelephonyBaseTest.tel_test_wrap
   1648     def test_toggle_apm_during_active_wifi_tethering(self):
   1649         """WiFi Tethering test: Toggle APM during active WiFi Tethering
   1650 
   1651         1. DUT in LTE mode, idle.
   1652         2. DUT start 2.4G WiFi Tethering
   1653         3. PhoneB disable data, connect to DUT's softAP
   1654         4. Verify Internet access on DUT and PhoneB
   1655         5. DUT toggle APM on, verify WiFi tethering stopped, PhoneB lost WiFi connection.
   1656         6. DUT toggle APM off, verify PhoneA have cellular data and Internet connection.
   1657 
   1658         Returns:
   1659             True if success.
   1660             False if failed.
   1661         """
   1662         if not self._test_setup_tethering(RAT_4G):
   1663             self.log.error("Verify 4G Internet access failed.")
   1664             return False
   1665         try:
   1666             ssid = rand_ascii_str(10)
   1667             if not wifi_tethering_setup_teardown(
   1668                     self.log,
   1669                     self.provider, [self.clients[0]],
   1670                     ap_band=WIFI_CONFIG_APBAND_2G,
   1671                     check_interval=10,
   1672                     check_iteration=2,
   1673                     do_cleanup=False,
   1674                     ssid=ssid):
   1675                 self.log.error("WiFi Tethering failed.")
   1676                 return False
   1677 
   1678             if not self.provider.droid.wifiIsApEnabled():
   1679                 self.log.error("Provider WiFi tethering stopped.")
   1680                 return False
   1681 
   1682             self.log.info(
   1683                 "Provider turn on APM, verify no wifi/data on Client.")
   1684             if not toggle_airplane_mode(self.log, self.provider, True):
   1685                 self.log.error("Provider turn on APM failed.")
   1686                 return False
   1687             time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
   1688             if self.provider.droid.wifiIsApEnabled():
   1689                 self.provider.log.error("Provider WiFi tethering not stopped.")
   1690                 return False
   1691             if verify_internet_connection(self.log, self.clients[0]):
   1692                 self.clients[0].log.error(
   1693                     "Client should not have Internet connection.")
   1694                 return False
   1695             wifi_info = self.clients[0].droid.wifiGetConnectionInfo()
   1696             self.clients[0].log.info("WiFi Info: {}".format(wifi_info))
   1697             if wifi_info[WIFI_SSID_KEY] == ssid:
   1698                 self.clients[0].log.error(
   1699                     "WiFi error. WiFi should not be connected.".format(
   1700                         wifi_info))
   1701                 return False
   1702 
   1703             self.log.info("Provider turn off APM.")
   1704             if not toggle_airplane_mode(self.log, self.provider, False):
   1705                 self.provider.log.error("Provider turn on APM failed.")
   1706                 return False
   1707             time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
   1708             if self.provider.droid.wifiIsApEnabled():
   1709                 self.provider.log.error(
   1710                     "Provider WiFi tethering should not on.")
   1711                 return False
   1712             if not verify_internet_connection(self.log, self.provider):
   1713                 self.provider.log.error(
   1714                     "Provider should have Internet connection.")
   1715                 return False
   1716         finally:
   1717             self.clients[0].droid.telephonyToggleDataConnection(True)
   1718             wifi_reset(self.log, self.clients[0])
   1719         return True
   1720 
   1721     @test_tracker_info(uuid="037e80fc-6eab-4cd1-846a-b9780a1d502d")
   1722     @TelephonyBaseTest.tel_test_wrap
   1723     def test_tethering_entitlement_check(self):
   1724         """Tethering Entitlement Check Test
   1725 
   1726         Get tethering entitlement check result.
   1727 
   1728         Returns:
   1729             True if entitlement check returns True.
   1730         """
   1731         if (not wait_for_cell_data_connection(self.log, self.provider, True)
   1732                 or not verify_internet_connection(self.log, self.provider)):
   1733             self.log.error("Failed cell data call for entitlement check.")
   1734             return False
   1735 
   1736         result = self.provider.droid.carrierConfigIsTetheringModeAllowed(
   1737             TETHERING_MODE_WIFI, MAX_WAIT_TIME_TETHERING_ENTITLEMENT_CHECK)
   1738         self.provider.log.info("Tethering entitlement check result: %s",
   1739                                result)
   1740         return result
   1741 
   1742     @test_tracker_info(uuid="4972826e-39ea-42f7-aae0-06fe3aa9ecc6")
   1743     @TelephonyBaseTest.tel_test_wrap
   1744     def test_tethering_4g_to_2gwifi_stress(self):
   1745         """Stress Test LTE to WiFI 2.4G Tethering
   1746 
   1747         This is a stress test for "test_tethering_4g_to_2gwifi".
   1748         Default MINIMUM_SUCCESS_RATE is set to 95%.
   1749 
   1750         Returns:
   1751             True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
   1752             False otherwise.
   1753         """
   1754         MINIMUM_SUCCESS_RATE = .95
   1755         success_count = 0
   1756         fail_count = 0
   1757 
   1758         for i in range(1, self.stress_test_number + 1):
   1759 
   1760             ensure_phones_default_state(self.log, self.android_devices)
   1761 
   1762             if self.test_tethering_4g_to_2gwifi():
   1763                 success_count += 1
   1764                 result_str = "Succeeded"
   1765             else:
   1766                 fail_count += 1
   1767                 result_str = "Failed"
   1768             self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
   1769                 i, result_str, success_count, self.stress_test_number))
   1770 
   1771         self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
   1772             success_count, fail_count,
   1773             str(100 * success_count / (success_count + fail_count))))
   1774         if success_count / (
   1775                 success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
   1776             return True
   1777         else:
   1778             return False
   1779 
   1780     @test_tracker_info(uuid="54e85aed-09e3-42e2-bb33-bca1005d93ab")
   1781     @TelephonyBaseTest.tel_test_wrap
   1782     def test_tethering_wifi_ssid_quotes(self):
   1783         """WiFi Tethering test: SSID name have quotes.
   1784         1. Set SSID name have double quotes.
   1785         2. Start LTE to WiFi (2.4G) tethering.
   1786         3. Verify tethering.
   1787 
   1788         Returns:
   1789             True if success.
   1790             False if failed.
   1791         """
   1792         if not self._test_setup_tethering():
   1793             self.log.error("Verify Internet access failed.")
   1794             return False
   1795         ssid = "\"" + rand_ascii_str(10) + "\""
   1796         self.log.info(
   1797             "Starting WiFi Tethering test with ssid: {}".format(ssid))
   1798 
   1799         return wifi_tethering_setup_teardown(
   1800             self.log,
   1801             self.provider, self.clients,
   1802             ap_band=WIFI_CONFIG_APBAND_2G,
   1803             check_interval=10,
   1804             check_iteration=10,
   1805             ssid=ssid)
   1806 
   1807     @test_tracker_info(uuid="320326da-bf32-444d-81f9-f781c55dbc99")
   1808     @TelephonyBaseTest.tel_test_wrap
   1809     def test_tethering_wifi_password_escaping_characters(self):
   1810         """WiFi Tethering test: password have escaping characters.
   1811         1. Set password have escaping characters.
   1812             e.g.: '"DQ=/{Yqq;M=(^_3HzRvhOiL8S%`]w&l<Qp8qH)bs<4E9v_q=HLr^)}w$blA0Kg'
   1813         2. Start LTE to WiFi (2.4G) tethering.
   1814         3. Verify tethering.
   1815 
   1816         Returns:
   1817             True if success.
   1818             False if failed.
   1819         """
   1820         if not self._test_setup_tethering():
   1821             self.log.error("Verify Internet access failed.")
   1822             return False
   1823 
   1824         password = '"DQ=/{Yqq;M=(^_3HzRvhOiL8S%`]w&l<Qp8qH)bs<4E9v_q=HLr^)}w$blA0Kg'
   1825         self.log.info(
   1826             "Starting WiFi Tethering test with password: {}".format(password))
   1827 
   1828         return wifi_tethering_setup_teardown(
   1829             self.log,
   1830             self.provider, self.clients,
   1831             ap_band=WIFI_CONFIG_APBAND_2G,
   1832             check_interval=10,
   1833             check_iteration=10,
   1834             password=password)
   1835 
   1836     def _test_start_wifi_tethering_connect_teardown(self, ad_host, ad_client,
   1837                                                     ssid, password):
   1838         """Private test util for WiFi Tethering.
   1839 
   1840         1. Host start WiFi tethering.
   1841         2. Client connect to tethered WiFi.
   1842         3. Host tear down WiFi tethering.
   1843 
   1844         Args:
   1845             ad_host: android device object for host
   1846             ad_client: android device object for client
   1847             ssid: WiFi tethering ssid
   1848             password: WiFi tethering password
   1849 
   1850         Returns:
   1851             True if no error happen, otherwise False.
   1852         """
   1853         result = True
   1854         self.number_of_devices = 2
   1855         # Turn off active SoftAP if any.
   1856         if ad_host.droid.wifiIsApEnabled():
   1857             stop_wifi_tethering(self.log, ad_host)
   1858 
   1859         time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
   1860         if not start_wifi_tethering(self.log, ad_host, ssid, password,
   1861                                     WIFI_CONFIG_APBAND_2G):
   1862             self.log.error("Start WiFi tethering failed.")
   1863             result = False
   1864         time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
   1865         if not ensure_wifi_connected(self.log, ad_client, ssid, password):
   1866             self.log.error("Client connect to WiFi failed.")
   1867             result = False
   1868         if not wifi_reset(self.log, ad_client):
   1869             self.log.error("Reset client WiFi failed. {}".format(
   1870                 ad_client.serial))
   1871             result = False
   1872         if not stop_wifi_tethering(self.log, ad_host):
   1873             self.log.error("Stop WiFi tethering failed.")
   1874             result = False
   1875         return result
   1876 
   1877     @test_tracker_info(uuid="617c7e71-f166-465f-bfd3-b5a3a40cc0d4")
   1878     @TelephonyBaseTest.tel_test_wrap
   1879     def test_tethering_wifi_ssid(self):
   1880         """WiFi Tethering test: start WiFi tethering with all kinds of SSIDs.
   1881 
   1882         For each listed SSID, start WiFi tethering on DUT, client connect WiFi,
   1883         then tear down WiFi tethering.
   1884 
   1885         Returns:
   1886             True if WiFi tethering succeed on all SSIDs.
   1887             False if failed.
   1888         """
   1889         if not self._test_setup_tethering(RAT_4G):
   1890             self.log.error("Setup Failed.")
   1891             return False
   1892         ssid_list = [
   1893             " !\"#$%&'()*+,-./0123456789:;<=>?",
   1894             "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_",
   1895             "`abcdefghijklmnopqrstuvwxyz{|}~", " a ", "!b!", "#c#", "$d$",
   1896             "%e%", "&f&", "'g'", "(h(", ")i)", "*j*", "+k+", "-l-", ".m.",
   1897             "/n/", "_", " !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}",
   1898             "\u0644\u062c\u0648\u062c", "\u8c37\u6b4c", "\uad6c\uae00"
   1899             "\u30b0\u30fc\u30eb",
   1900             "\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0443\u0439"
   1901         ]
   1902         fail_list = {}
   1903 
   1904         for ssid in ssid_list:
   1905             password = rand_ascii_str(8)
   1906             self.log.info("SSID: <{}>, Password: <{}>".format(ssid, password))
   1907             if not self._test_start_wifi_tethering_connect_teardown(
   1908                     self.provider, self.clients[0], ssid, password):
   1909                 fail_list[ssid] = password
   1910 
   1911         if (len(fail_list) > 0):
   1912             self.log.error("Failed cases: {}".format(fail_list))
   1913             return False
   1914         else:
   1915             return True
   1916 
   1917     @test_tracker_info(uuid="9a5b5a34-b5cf-451d-94c4-8a64d456dfe5")
   1918     @TelephonyBaseTest.tel_test_wrap
   1919     def test_tethering_wifi_password(self):
   1920         """WiFi Tethering test: start WiFi tethering with all kinds of passwords.
   1921 
   1922         For each listed password, start WiFi tethering on DUT, client connect WiFi,
   1923         then tear down WiFi tethering.
   1924 
   1925         Returns:
   1926             True if WiFi tethering succeed on all passwords.
   1927             False if failed.
   1928         """
   1929         if not self._test_setup_tethering(RAT_4G):
   1930             self.log.error("Setup Failed.")
   1931             return False
   1932         password_list = [
   1933             " !\"#$%&'()*+,-./0123456789:;<=>?",
   1934             "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_",
   1935             "`abcdefghijklmnopqrstuvwxyz{|}~",
   1936             " !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}", "abcdefgh",
   1937             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!",
   1938             " a12345 ", "!b12345!", "#c12345#", "$d12345$", "%e12345%",
   1939             "&f12345&", "'g12345'", "(h12345(", ")i12345)", "*j12345*",
   1940             "+k12345+", "-l12345-", ".m12345.", "/n12345/"
   1941         ]
   1942         fail_list = {}
   1943 
   1944         for password in password_list:
   1945             ssid = rand_ascii_str(8)
   1946             self.log.info("SSID: <{}>, Password: <{}>".format(ssid, password))
   1947             if not self._test_start_wifi_tethering_connect_teardown(
   1948                     self.provider, self.clients[0], ssid, password):
   1949                 fail_list[ssid] = password
   1950 
   1951         if (len(fail_list) > 0):
   1952             self.log.error("Failed cases: {}".format(fail_list))
   1953             return False
   1954         else:
   1955             return True
   1956 
   1957     def _test_tethering_wifi_and_voice_call(self, provider_data_rat,
   1958                                             provider_setup_func,
   1959                                             provider_in_call_check_func):
   1960         self.number_of_devices = 2
   1961         if not self._test_setup_tethering(provider_data_rat):
   1962             self.log.error("Verify 4G Internet access failed.")
   1963             return False
   1964 
   1965         tasks = [(provider_setup_func, (self.log, self.provider)),
   1966                  (phone_setup_voice_general, (self.log, self.clients[0]))]
   1967         if not multithread_func(self.log, tasks):
   1968             self.log.error("Phone Failed to Set Up VoLTE.")
   1969             return False
   1970 
   1971         try:
   1972             self.log.info("1. Setup WiFi Tethering.")
   1973             if not wifi_tethering_setup_teardown(
   1974                     self.log,
   1975                     self.provider, [self.clients[0]],
   1976                     ap_band=WIFI_CONFIG_APBAND_2G,
   1977                     check_interval=10,
   1978                     check_iteration=2,
   1979                     do_cleanup=False):
   1980                 self.log.error("WiFi Tethering failed.")
   1981                 return False
   1982             self.log.info("2. Make outgoing call.")
   1983             if not call_setup_teardown(
   1984                     self.log,
   1985                     self.provider,
   1986                     self.clients[0],
   1987                     ad_hangup=None,
   1988                     verify_caller_func=provider_in_call_check_func):
   1989                 self.log.error("Setup Call Failed.")
   1990                 return False
   1991             self.log.info("3. Verify data.")
   1992             if not verify_internet_connection(self.log, self.provider):
   1993                 self.provider.log.error("Provider have no Internet access.")
   1994             if not verify_internet_connection(self.log, self.clients[0]):
   1995                 self.clients[0].log.error("Client have no Internet access.")
   1996             hangup_call(self.log, self.provider)
   1997 
   1998             time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
   1999 
   2000             self.log.info("4. Make incoming call.")
   2001             if not call_setup_teardown(
   2002                     self.log,
   2003                     self.clients[0],
   2004                     provider,
   2005                     ad_hangup=None,
   2006                     verify_callee_func=provider_in_call_check_func):
   2007                 self.log.error("Setup Call Failed.")
   2008                 return False
   2009             self.log.info("5. Verify data.")
   2010             if not verify_internet_connection(self.log, self.provider):
   2011                 self.provider.log.error("Provider have no Internet access.")
   2012             if not verify_internet_connection(self.log, self.clients[0]):
   2013                 self.clients[0].log.error("Client have no Internet access.")
   2014             hangup_call(self.log, self.provider)
   2015 
   2016         finally:
   2017             if not wifi_tethering_cleanup(self.log, self.provider,
   2018                                           self.clients):
   2019                 return False
   2020         return True
   2021 
   2022     @test_tracker_info(uuid="216bdb8c-edbf-4ff8-8750-a0861ab44df6")
   2023     @TelephonyBaseTest.tel_test_wrap
   2024     def test_tethering_wifi_volte_call(self):
   2025         """WiFi Tethering test: VoLTE call during WiFi tethering
   2026         1. Start LTE to WiFi (2.4G) tethering.
   2027         2. Verify tethering.
   2028         3. Make outgoing VoLTE call on tethering provider.
   2029         4. Verify tethering still works.
   2030         5. Make incoming VoLTE call on tethering provider.
   2031         6. Verify tethering still works.
   2032 
   2033         Returns:
   2034             True if success.
   2035             False if failed.
   2036         """
   2037         return self._test_tethering_wifi_and_voice_call(
   2038             RAT_4G, phone_setup_volte, is_phone_in_call_volte)
   2039 
   2040     @test_tracker_info(uuid="bcd430cc-6d33-47d1-825d-aae9f248addc")
   2041     @TelephonyBaseTest.tel_test_wrap
   2042     def test_tethering_wifi_csfb_call(self):
   2043         """WiFi Tethering test: CSFB call during WiFi tethering
   2044         1. Start LTE to WiFi (2.4G) tethering.
   2045         2. Verify tethering.
   2046         3. Make outgoing CSFB call on tethering provider.
   2047         4. Verify tethering still works.
   2048         5. Make incoming CSFB call on tethering provider.
   2049         6. Verify tethering still works.
   2050 
   2051         Returns:
   2052             True if success.
   2053             False if failed.
   2054         """
   2055         return self._test_tethering_wifi_and_voice_call(
   2056             RAT_4G, phone_setup_csfb, is_phone_in_call_csfb)
   2057 
   2058     @test_tracker_info(uuid="19e0df23-6819-4c69-bfda-eea9cce802d8")
   2059     @TelephonyBaseTest.tel_test_wrap
   2060     def test_tethering_wifi_3g_call(self):
   2061         """WiFi Tethering test: 3G call during WiFi tethering
   2062         1. Start 3G to WiFi (2.4G) tethering.
   2063         2. Verify tethering.
   2064         3. Make outgoing CS call on tethering provider.
   2065         4. Verify tethering still works.
   2066         5. Make incoming CS call on tethering provider.
   2067         6. Verify tethering still works.
   2068 
   2069         Returns:
   2070             True if success.
   2071             False if failed.
   2072         """
   2073         return self._test_tethering_wifi_and_voice_call(
   2074             RAT_3G, phone_setup_voice_3g, is_phone_in_call_3g)
   2075 
   2076     @test_tracker_info(uuid="4acd98b5-fdef-4736-969f-3fa953990a58")
   2077     @TelephonyBaseTest.tel_test_wrap
   2078     def test_tethering_wifi_no_password(self):
   2079         """WiFi Tethering test: Start WiFi tethering with no password
   2080 
   2081         1. DUT is idle.
   2082         2. DUT start 2.4G WiFi Tethering, with no WiFi password.
   2083         3. PhoneB disable data, connect to DUT's softAP
   2084         4. Verify Internet access on DUT and PhoneB
   2085 
   2086         Returns:
   2087             True if success.
   2088             False if failed.
   2089         """
   2090         if not self._test_setup_tethering():
   2091             self.log.error("Verify Internet access failed.")
   2092             return False
   2093 
   2094         return wifi_tethering_setup_teardown(
   2095             self.log,
   2096             self.provider, [self.clients[0]],
   2097             ap_band=WIFI_CONFIG_APBAND_2G,
   2098             check_interval=10,
   2099             check_iteration=10,
   2100             password="")
   2101 
   2102     @test_tracker_info(uuid="86ad1680-bfb8-457e-8b4d-23321cb3f223")
   2103     @TelephonyBaseTest.tel_test_wrap
   2104     def test_tethering_wifi_reboot(self):
   2105         """WiFi Tethering test: Start WiFi tethering then Reboot device
   2106 
   2107         1. DUT is idle.
   2108         2. DUT start 2.4G WiFi Tethering.
   2109         3. PhoneB disable data, connect to DUT's softAP
   2110         4. Verify Internet access on DUT and PhoneB
   2111         5. Reboot DUT
   2112         6. After DUT reboot, verify tethering is stopped.
   2113 
   2114         Returns:
   2115             True if success.
   2116             False if failed.
   2117         """
   2118         if not self._test_setup_tethering():
   2119             self.log.error("Verify Internet access failed.")
   2120             return False
   2121         try:
   2122             if not wifi_tethering_setup_teardown(
   2123                     self.log,
   2124                     self.provider, [self.clients[0]],
   2125                     ap_band=WIFI_CONFIG_APBAND_2G,
   2126                     check_interval=10,
   2127                     check_iteration=2,
   2128                     do_cleanup=False):
   2129                 self.log.error("WiFi Tethering failed.")
   2130                 return False
   2131 
   2132             if not self.provider.droid.wifiIsApEnabled():
   2133                 self.log.error("Provider WiFi tethering stopped.")
   2134                 return False
   2135 
   2136             self.provider.log.info("Reboot provider")
   2137             self.provider.reboot()
   2138             time.sleep(
   2139                 WAIT_TIME_AFTER_REBOOT + WAIT_TIME_TETHERING_AFTER_REBOOT)
   2140 
   2141             self.log.info("After reboot check if tethering stopped.")
   2142             if self.provider.droid.wifiIsApEnabled():
   2143                 self.log.error("Provider WiFi tethering did NOT stopped.")
   2144                 return False
   2145         finally:
   2146             self.clients[0].droid.telephonyToggleDataConnection(True)
   2147             wifi_reset(self.log, self.clients[0])
   2148             if self.provider.droid.wifiIsApEnabled():
   2149                 stop_wifi_tethering(self.log, self.provider)
   2150         return True
   2151 
   2152     @test_tracker_info(uuid="5cf04ca2-dfde-43d6-be74-78b9abdf6c26")
   2153     @TelephonyBaseTest.tel_test_wrap
   2154     def test_connect_wifi_start_tethering_wifi_reboot(self):
   2155         """WiFi Tethering test: WiFI connected, then start WiFi tethering,
   2156             then reboot device.
   2157 
   2158         Initial Condition: DUT in 4G mode, idle, DUT connect to WiFi.
   2159         1. DUT start 2.4G WiFi Tethering.
   2160         2. PhoneB disable data, connect to DUT's softAP
   2161         3. Verify Internet access on DUT and PhoneB
   2162         4. Reboot DUT
   2163         5. After DUT reboot, verify tethering is stopped. DUT is able to connect
   2164             to previous WiFi AP.
   2165 
   2166         Returns:
   2167             True if success.
   2168             False if failed.
   2169         """
   2170         if not self._test_setup_tethering():
   2171             self.log.error("Verify Internet access failed.")
   2172             return False
   2173 
   2174         self.log.info("Make sure DUT can connect to live network by WIFI")
   2175         if ((not ensure_wifi_connected(self.log, self.provider,
   2176                                        self.wifi_network_ssid,
   2177                                        self.wifi_network_pass))
   2178                 or (not verify_internet_connection(self.log, self.provider))):
   2179             self.log.error("WiFi connect fail.")
   2180             return False
   2181 
   2182         try:
   2183             if not wifi_tethering_setup_teardown(
   2184                     self.log,
   2185                     self.provider, [self.clients[0]],
   2186                     ap_band=WIFI_CONFIG_APBAND_2G,
   2187                     check_interval=10,
   2188                     check_iteration=2,
   2189                     do_cleanup=False):
   2190                 self.log.error("WiFi Tethering failed.")
   2191                 return False
   2192 
   2193             if not ads[0].droid.wifiIsApEnabled():
   2194                 self.log.error("Provider WiFi tethering stopped.")
   2195                 return False
   2196 
   2197             self.provider.log.info("Reboot Provider")
   2198             self.provider.reboot()
   2199             time.sleep(WAIT_TIME_AFTER_REBOOT)
   2200             time.sleep(WAIT_TIME_TETHERING_AFTER_REBOOT)
   2201 
   2202             self.log.info("After reboot check if tethering stopped.")
   2203             if self.provider.droid.wifiIsApEnabled():
   2204                 self.provider.log.error(
   2205                     "Provider WiFi tethering did NOT stopped.")
   2206                 return False
   2207 
   2208             self.log.info("Make sure WiFi can connect automatically.")
   2209             if (not wait_for_wifi_data_connection(self.log, self.provider,
   2210                                                   True) or
   2211                     not verify_internet_connection(self.log, self.provider)):
   2212                 self.log.error("Data did not return to WiFi")
   2213                 return False
   2214 
   2215         finally:
   2216             self.clients[0].droid.telephonyToggleDataConnection(True)
   2217             wifi_reset(self.log, self.clients[0])
   2218             if self.provider.droid.wifiIsApEnabled():
   2219                 stop_wifi_tethering(self.log, self.provider)
   2220         return True
   2221 
   2222     @test_tracker_info(uuid="e0621997-c5bd-4137-afa6-b43406e9c713")
   2223     @TelephonyBaseTest.tel_test_wrap
   2224     def test_connect_wifi_reboot_start_tethering_wifi(self):
   2225         """WiFi Tethering test: DUT connected to WiFi, then reboot,
   2226         After reboot, start WiFi tethering, verify tethering actually works.
   2227 
   2228         Initial Condition: Device set to 4G mode, idle, DUT connect to WiFi.
   2229         1. Verify Internet is working on DUT (by WiFi).
   2230         2. Reboot DUT.
   2231         3. DUT start 2.4G WiFi Tethering.
   2232         4. PhoneB disable data, connect to DUT's softAP
   2233         5. Verify Internet access on DUT and PhoneB
   2234 
   2235         Returns:
   2236             True if success.
   2237             False if failed.
   2238         """
   2239         if not self._test_setup_tethering():
   2240             self.log.error("Verify Internet access failed.")
   2241             return False
   2242 
   2243         self.log.info("Make sure DUT can connect to live network by WIFI")
   2244         if ((not ensure_wifi_connected(self.log, self.provider,
   2245                                        self.wifi_network_ssid,
   2246                                        self.wifi_network_pass))
   2247                 or (not verify_internet_connection(self.log, self.provider))):
   2248             self.log.error("WiFi connect fail.")
   2249             return False
   2250 
   2251         self.provider.log.info("Reboot provider")
   2252         self.provider.reboot()
   2253         time.sleep(WAIT_TIME_AFTER_REBOOT)
   2254         time.sleep(WAIT_TIME_TETHERING_AFTER_REBOOT)
   2255 
   2256         return wifi_tethering_setup_teardown(
   2257             self.log,
   2258             self.provider, [self.clients[0]],
   2259             ap_band=WIFI_CONFIG_APBAND_2G,
   2260             check_interval=10,
   2261             check_iteration=10)
   2262 
   2263     @test_tracker_info(uuid="89a849ef-e2ed-4bf2-ac31-81d34aba672a")
   2264     @TelephonyBaseTest.tel_test_wrap
   2265     def test_tethering_wifi_screen_off_enable_doze_mode(self):
   2266         """WiFi Tethering test: Start WiFi tethering, then turn off DUT's screen,
   2267             then enable doze mode.
   2268 
   2269         1. Start WiFi tethering on DUT.
   2270         2. PhoneB disable data, and connect to DUT's softAP
   2271         3. Verify Internet access on DUT and PhoneB
   2272         4. Turn off DUT's screen. Wait for 1 minute and
   2273             verify Internet access on Client PhoneB.
   2274         5. Enable doze mode on DUT. Wait for 1 minute and
   2275             verify Internet access on Client PhoneB.
   2276 
   2277         Returns:
   2278             True if success.
   2279             False if failed.
   2280         """
   2281         if not self._test_setup_tethering():
   2282             self.log.error("Verify Internet access failed.")
   2283             return False
   2284         try:
   2285             if not wifi_tethering_setup_teardown(
   2286                     self.log,
   2287                     self.provider, [self.clients[0]],
   2288                     ap_band=WIFI_CONFIG_APBAND_2G,
   2289                     check_interval=10,
   2290                     check_iteration=2,
   2291                     do_cleanup=False):
   2292                 self.log.error("WiFi Tethering failed.")
   2293                 return False
   2294 
   2295             if not self.provider.droid.wifiIsApEnabled():
   2296                 self.provider.log.error("Provider WiFi tethering stopped.")
   2297                 return False
   2298 
   2299             self.provider.log.info("Turn off screen on provider")
   2300             self.provider.droid.goToSleepNow()
   2301             time.sleep(60)
   2302             if not verify_internet_connection(self.log, self.clients[0]):
   2303                 self.client.log.error("Client have no Internet access.")
   2304                 return False
   2305 
   2306             self.provider.log.info("Enable doze mode on provider")
   2307             if not enable_doze(self.provider):
   2308                 self.provider.log.error("Failed to enable doze mode.")
   2309                 return False
   2310             time.sleep(60)
   2311             if not verify_internet_connection(self.log, self.clients[0]):
   2312                 self.clients[0].log.error("Client have no Internet access.")
   2313                 return False
   2314         finally:
   2315             self.log.info("Disable doze mode.")
   2316             if not disable_doze(self.provider):
   2317                 self.log.error("Failed to disable doze mode.")
   2318                 return False
   2319             if not wifi_tethering_cleanup(self.log, self.provider,
   2320                                           [self.clients[0]]):
   2321                 return False
   2322         return True
   2323 
   2324     @test_tracker_info(uuid="695eef18-f759-4b41-8ad3-1fb329ee4b1b")
   2325     @TelephonyBaseTest.tel_test_wrap
   2326     def test_msim_switch_data_sim_2g(self):
   2327         """Switch Data SIM on 2G network.
   2328 
   2329         Steps:
   2330         1. Data on default Data SIM.
   2331         2. Switch Data to another SIM. Make sure data is still available.
   2332         3. Switch Data back to previous SIM. Make sure data is still available.
   2333 
   2334         Expected Results:
   2335         1. Verify Data on Cell
   2336         2. Verify Data on Wifi
   2337 
   2338         Returns:
   2339             True if success.
   2340             False if failed.
   2341         """
   2342         ad = self.android_devices[0]
   2343         current_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
   2344         current_sim_slot_index = get_slot_index_from_subid(
   2345             self.log, ad, current_data_sub_id)
   2346         if current_sim_slot_index == SIM1_SLOT_INDEX:
   2347             next_sim_slot_index = SIM2_SLOT_INDEX
   2348         else:
   2349             next_sim_slot_index = SIM1_SLOT_INDEX
   2350         next_data_sub_id = get_subid_from_slot_index(self.log, ad,
   2351                                                      next_sim_slot_index)
   2352         self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
   2353             current_data_sub_id, current_sim_slot_index))
   2354         if not ensure_network_generation_for_subscription(
   2355                 self.log,
   2356                 ad,
   2357                 ad.droid.subscriptionGetDefaultDataSubId(),
   2358                 GEN_2G,
   2359                 voice_or_data=NETWORK_SERVICE_DATA):
   2360             self.log.error("Device data does not attach to 2G.")
   2361             return False
   2362         if not verify_internet_connection(self.log, ad):
   2363             self.log.error("No Internet access on default Data SIM.")
   2364             return False
   2365 
   2366         self.log.info("Change Data to subId: {}, SIM slot: {}".format(
   2367             next_data_sub_id, next_sim_slot_index))
   2368         if not change_data_sim_and_verify_data(self.log, ad,
   2369                                                next_sim_slot_index):
   2370             self.log.error("Failed to change data SIM.")
   2371             return False
   2372 
   2373         next_data_sub_id = current_data_sub_id
   2374         next_sim_slot_index = current_sim_slot_index
   2375         self.log.info("Change Data back to subId: {}, SIM slot: {}".format(
   2376             next_data_sub_id, next_sim_slot_index))
   2377         if not change_data_sim_and_verify_data(self.log, ad,
   2378                                                next_sim_slot_index):
   2379             self.log.error("Failed to change data SIM.")
   2380             return False
   2381 
   2382         return True
   2383 
   2384     def _test_wifi_connect_disconnect(self):
   2385         """Perform multiple connects and disconnects from WiFi and verify that
   2386             data switches between WiFi and Cell.
   2387 
   2388         Steps:
   2389         1. Reset Wifi on DUT
   2390         2. Connect DUT to a WiFi AP
   2391         3. Repeat steps 1-2, alternately disconnecting and disabling wifi
   2392 
   2393         Expected Results:
   2394         1. Verify Data on Cell
   2395         2. Verify Data on Wifi
   2396 
   2397         Returns:
   2398             True if success.
   2399             False if failed.
   2400         """
   2401         ad = self.android_devices[0]
   2402 
   2403         wifi_toggles = [
   2404             True, False, True, False, False, True, False, False, False, False,
   2405             True, False, False, False, False, False, False, False, False
   2406         ]
   2407 
   2408         for toggle in wifi_toggles:
   2409 
   2410             wifi_reset(self.log, ad, toggle)
   2411 
   2412             if not wait_for_cell_data_connection(
   2413                     self.log, ad, True, MAX_WAIT_TIME_WIFI_CONNECTION):
   2414                 self.log.error("Failed wifi connection, aborting!")
   2415                 return False
   2416 
   2417             if not verify_internet_connection(
   2418                     self.log, ad, 'http://www.google.com', 100, .1):
   2419                 self.log.error("Failed to get user-plane traffic, aborting!")
   2420                 return False
   2421 
   2422             if toggle:
   2423                 wifi_toggle_state(self.log, ad, True)
   2424 
   2425             wifi_connect(self.log, ad, self.wifi_network_ssid,
   2426                          self.wifi_network_pass)
   2427 
   2428             if not wait_for_wifi_data_connection(
   2429                     self.log, ad, True, MAX_WAIT_TIME_WIFI_CONNECTION):
   2430                 self.log.error("Failed wifi connection, aborting!")
   2431                 return False
   2432 
   2433             if not verify_internet_connection(
   2434                     self.log, ad, 'http://www.google.com', 100, .1):
   2435                 self.log.error("Failed to get user-plane traffic, aborting!")
   2436                 return False
   2437         return True
   2438 
   2439     @test_tracker_info(uuid="9b8e92da-0ae1-472c-a72a-f6427e5405ce")
   2440     @TelephonyBaseTest.tel_test_wrap
   2441     def test_wifi_connect_disconnect_4g(self):
   2442         """Perform multiple connects and disconnects from WiFi and verify that
   2443             data switches between WiFi and Cell.
   2444 
   2445         Steps:
   2446         1. DUT Cellular Data is on 4G. Reset Wifi on DUT
   2447         2. Connect DUT to a WiFi AP
   2448         3. Repeat steps 1-2, alternately disconnecting and disabling wifi
   2449 
   2450         Expected Results:
   2451         1. Verify Data on Cell
   2452         2. Verify Data on Wifi
   2453 
   2454         Returns:
   2455             True if success.
   2456             False if failed.
   2457         """
   2458 
   2459         ad = self.android_devices[0]
   2460         if not ensure_network_generation_for_subscription(
   2461                 self.log, ad, ad.droid.subscriptionGetDefaultDataSubId(),
   2462                 GEN_4G, MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
   2463             self.log.error("Device {} failed to reselect in {}s.".format(
   2464                 ad.serial, MAX_WAIT_TIME_NW_SELECTION))
   2465             return False
   2466         return self._test_wifi_connect_disconnect()
   2467 
   2468     @test_tracker_info(uuid="09893b1f-a4a2-49d3-8027-c2c91cb8742e")
   2469     @TelephonyBaseTest.tel_test_wrap
   2470     def test_wifi_connect_disconnect_3g(self):
   2471         """Perform multiple connects and disconnects from WiFi and verify that
   2472             data switches between WiFi and Cell.
   2473 
   2474         Steps:
   2475         1. DUT Cellular Data is on 3G. Reset Wifi on DUT
   2476         2. Connect DUT to a WiFi AP
   2477         3. Repeat steps 1-2, alternately disconnecting and disabling wifi
   2478 
   2479         Expected Results:
   2480         1. Verify Data on Cell
   2481         2. Verify Data on Wifi
   2482 
   2483         Returns:
   2484             True if success.
   2485             False if failed.
   2486         """
   2487 
   2488         ad = self.android_devices[0]
   2489         if not ensure_network_generation_for_subscription(
   2490                 self.log, ad, ad.droid.subscriptionGetDefaultDataSubId(),
   2491                 GEN_3G, MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
   2492             self.log.error("Device {} failed to reselect in {}s.".format(
   2493                 ad.serial, MAX_WAIT_TIME_NW_SELECTION))
   2494             return False
   2495         return self._test_wifi_connect_disconnect()
   2496 
   2497     @test_tracker_info(uuid="0f095ca4-ce05-458f-9670-49a69f8c8270")
   2498     @TelephonyBaseTest.tel_test_wrap
   2499     def test_wifi_connect_disconnect_2g(self):
   2500         """Perform multiple connects and disconnects from WiFi and verify that
   2501             data switches between WiFi and Cell.
   2502 
   2503         Steps:
   2504         1. DUT Cellular Data is on 2G. Reset Wifi on DUT
   2505         2. Connect DUT to a WiFi AP
   2506         3. Repeat steps 1-2, alternately disconnecting and disabling wifi
   2507 
   2508         Expected Results:
   2509         1. Verify Data on Cell
   2510         2. Verify Data on Wifi
   2511 
   2512         Returns:
   2513             True if success.
   2514             False if failed.
   2515         """
   2516         ad = self.android_devices[0]
   2517         if not ensure_network_generation_for_subscription(
   2518                 self.log, ad, ad.droid.subscriptionGetDefaultDataSubId(),
   2519                 GEN_2G, MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
   2520             self.log.error("Device {} failed to reselect in {}s.".format(
   2521                 ad.serial, MAX_WAIT_TIME_NW_SELECTION))
   2522             return False
   2523         return self._test_wifi_connect_disconnect()
   2524 
   2525     def _test_wifi_tethering_enabled_add_voice_call(
   2526             self, network_generation, voice_call_direction,
   2527             is_data_available_during_call):
   2528         """Tethering enabled + voice call.
   2529 
   2530         Steps:
   2531         1. DUT data is on <network_generation>. Start WiFi Tethering.
   2532         2. PhoneB connect to DUT's softAP
   2533         3. DUT make a MO/MT (<voice_call_direction>) phone call.
   2534         4. DUT end phone call.
   2535 
   2536         Expected Results:
   2537         1. DUT is able to start WiFi tethering.
   2538         2. PhoneB connected to DUT's softAP and able to browse Internet.
   2539         3. DUT WiFi tethering is still on. Phone call works OK.
   2540             If is_data_available_during_call is True, then PhoneB still has
   2541             Internet access.
   2542             Else, then Data is suspend, PhoneB has no Internet access.
   2543         4. WiFi Tethering still on, voice call stopped, and PhoneB have Internet
   2544             access.
   2545 
   2546         Returns:
   2547             True if success.
   2548             False if failed.
   2549         """
   2550         if not self._test_setup_tethering(network_generation):
   2551             self.log.error("Verify Internet access failed.")
   2552             return False
   2553         try:
   2554             # Start WiFi Tethering
   2555             if not wifi_tethering_setup_teardown(
   2556                     self.log,
   2557                     self.provider, [self.clients[0]],
   2558                     ap_band=WIFI_CONFIG_APBAND_2G,
   2559                     check_interval=10,
   2560                     check_iteration=2,
   2561                     do_cleanup=False):
   2562                 self.log.error("WiFi Tethering failed.")
   2563                 return False
   2564 
   2565             if not self.provider.droid.wifiIsApEnabled():
   2566                 self.log.error("Provider WiFi tethering stopped.")
   2567                 return False
   2568 
   2569             # Make a voice call
   2570             if voice_call_direction == DIRECTION_MOBILE_ORIGINATED:
   2571                 ad_caller = self.provider
   2572                 ad_callee = self.clients[0]
   2573             else:
   2574                 ad_caller = self.clients[0]
   2575                 ad_callee = self.provider
   2576             if not call_setup_teardown(self.log, ad_caller, ad_callee, None,
   2577                                        None, None):
   2578                 self.log.error("Failed to Establish {} Voice Call".format(
   2579                     voice_call_direction))
   2580                 return False
   2581 
   2582             # Tethering should still be on.
   2583             if not self.provider.droid.wifiIsApEnabled():
   2584                 self.provider.log.error("Provider WiFi tethering stopped.")
   2585                 return False
   2586             if not is_data_available_during_call:
   2587                 if verify_internet_connection(
   2588                         self.log, self.clients[0], retry=0):
   2589                     self.clients[0].log.error(
   2590                         "Client should not have Internet Access.")
   2591                     return False
   2592             else:
   2593                 if not verify_internet_connection(self.log, self.clients[0]):
   2594                     self.clients[0].error(
   2595                         "Client should have Internet Access.")
   2596                     return False
   2597 
   2598             # Hangup call. Client should have data.
   2599             if not hangup_call(self.log, self.provider):
   2600                 self.provider.log.error("Failed to hang up call")
   2601                 return False
   2602             if not self.provider.droid.wifiIsApEnabled():
   2603                 self.provider.log.error("Provider WiFi tethering stopped.")
   2604                 return False
   2605             if not verify_internet_connection(self.log, self.clients[0]):
   2606                 self.clients[0].log.error(
   2607                     "Client should have Internet Access.")
   2608                 return False
   2609         finally:
   2610             self.clients[0].droid.telephonyToggleDataConnection(True)
   2611             wifi_reset(self.log, self.clients[0])
   2612             if self.provider.droid.wifiIsApEnabled():
   2613                 stop_wifi_tethering(self.log, self.provider)
   2614         return True
   2615 
   2616     @test_tracker_info(uuid="4d7a68c6-5eae-4242-a6e6-668f830caec3")
   2617     @TelephonyBaseTest.tel_test_wrap
   2618     def test_wifi_tethering_enabled_add_mo_voice_call_2g_dsds(self):
   2619         """Tethering enabled + voice call
   2620 
   2621         Steps:
   2622         1. DUT is DSDS device, Data on 2G. Start WiFi Tethering on <Data SIM>
   2623         2. PhoneB connect to DUT's softAP
   2624         3. DUT make a mo phone call on <Voice SIM>
   2625         4. DUT end phone call.
   2626 
   2627         Expected Results:
   2628         1. DUT is able to start WiFi tethering.
   2629         2. PhoneB connected to DUT's softAP and able to browse Internet.
   2630         3. DUT WiFi tethering is still on. Phone call works OK. Data is suspend,
   2631             PhoneB still connected to DUT's softAP, but no data available.
   2632         4. DUT data resumes, and PhoneB have Internet access.
   2633 
   2634         Returns:
   2635             True if success.
   2636             False if failed.
   2637         """
   2638 
   2639         return self._test_wifi_tethering_enabled_add_voice_call(
   2640             GEN_2G, DIRECTION_MOBILE_ORIGINATED, False)
   2641 
   2642     @test_tracker_info(uuid="de720069-a46c-4a6f-ae80-60b9349c8528")
   2643     @TelephonyBaseTest.tel_test_wrap
   2644     def test_wifi_tethering_enabled_add_mt_voice_call_2g_dsds(self):
   2645         """Tethering enabled + voice call
   2646 
   2647         Steps:
   2648         1. DUT is DSDS device, Data on 2G. Start WiFi Tethering on <Data SIM>
   2649         2. PhoneB connect to DUT's softAP
   2650         3. DUT make a mt phone call on <Voice SIM>
   2651         4. DUT end phone call.
   2652 
   2653         Expected Results:
   2654         1. DUT is able to start WiFi tethering.
   2655         2. PhoneB connected to DUT's softAP and able to browse Internet.
   2656         3. DUT WiFi tethering is still on. Phone call works OK. Data is suspend,
   2657             PhoneB still connected to DUT's softAP, but no data available.
   2658         4. DUT data resumes, and PhoneB have Internet access.
   2659 
   2660         Returns:
   2661             True if success.
   2662             False if failed.
   2663         """
   2664 
   2665         return self._test_wifi_tethering_enabled_add_voice_call(
   2666             GEN_2G, DIRECTION_MOBILE_TERMINATED, False)
   2667 
   2668     @test_tracker_info(uuid="fad169c0-8ae6-45d2-98ba-3fb60466ff0b")
   2669     @TelephonyBaseTest.tel_test_wrap
   2670     def test_wifi_tethering_msim_switch_data_sim(self):
   2671         """Tethering enabled + switch data SIM.
   2672 
   2673         Steps:
   2674         1. Start WiFi Tethering on <Default Data SIM>
   2675         2. PhoneB connect to DUT's softAP
   2676         3. DUT change Default Data SIM.
   2677 
   2678         Expected Results:
   2679         1. DUT is able to start WiFi tethering.
   2680         2. PhoneB connected to DUT's softAP and able to browse Internet.
   2681         3. DUT Data changed to 2nd SIM, WiFi tethering should continues,
   2682             PhoneB should have Internet access.
   2683 
   2684         Returns:
   2685             True if success.
   2686             False if failed.
   2687         """
   2688         current_data_sub_id = self.provider.droid.subscriptionGetDefaultDataSubId(
   2689         )
   2690         current_sim_slot_index = get_slot_index_from_subid(
   2691             self.log, self.provider, current_data_sub_id)
   2692         self.provider.log.info("Current Data is on subId: %s, SIM slot: %s",
   2693                                current_data_sub_id, current_sim_slot_index)
   2694         if not self._test_setup_tethering():
   2695             self.log.error("Verify Internet access failed.")
   2696             return False
   2697         try:
   2698             # Start WiFi Tethering
   2699             if not wifi_tethering_setup_teardown(
   2700                     self.log,
   2701                     self.provider, [self.clients[0]],
   2702                     ap_band=WIFI_CONFIG_APBAND_2G,
   2703                     check_interval=10,
   2704                     check_iteration=2,
   2705                     do_cleanup=False):
   2706                 self.log.error("WiFi Tethering failed.")
   2707                 return False
   2708             for i in range(0, 2):
   2709                 next_sim_slot_index = \
   2710                     {SIM1_SLOT_INDEX : SIM2_SLOT_INDEX,
   2711                      SIM2_SLOT_INDEX : SIM1_SLOT_INDEX}[current_sim_slot_index]
   2712                 self.log.info(
   2713                     "Change Data to SIM slot: {}".format(next_sim_slot_index))
   2714                 if not change_data_sim_and_verify_data(self.log, self.provider,
   2715                                                        next_sim_slot_index):
   2716                     self.provider.log.error("Failed to change data SIM.")
   2717                     return False
   2718                 current_sim_slot_index = next_sim_slot_index
   2719                 if not verify_internet_connection(self.log, self.clients[0]):
   2720                     self.clients[0].log.error(
   2721                         "Client should have Internet Access.")
   2722                     return False
   2723         finally:
   2724             self.cliets[0].droid.telephonyToggleDataConnection(True)
   2725             wifi_reset(self.log, self.clients[0])
   2726             if self.provider.droid.wifiIsApEnabled():
   2727                 stop_wifi_tethering(self.log, self.provider)
   2728         return True
   2729 
   2730     @test_tracker_info(uuid="8bb9383f-ddf9-400c-a831-c9462bae6b47")
   2731     @TelephonyBaseTest.tel_test_wrap
   2732     def test_msim_cell_data_switch_to_wifi_switch_data_sim_2g(self):
   2733         """Switch Data SIM on 2G network.
   2734 
   2735         Steps:
   2736         1. Data on default Data SIM.
   2737         2. Turn on WiFi, then data should be on WiFi.
   2738         3. Switch Data to another SIM. Disable WiFi.
   2739 
   2740         Expected Results:
   2741         1. Verify Data on Cell
   2742         2. Verify Data on WiFi
   2743         3. After WiFi disabled, Cell Data is available on 2nd SIM.
   2744 
   2745         Returns:
   2746             True if success.
   2747             False if failed.
   2748         """
   2749         ad = self.android_devices[0]
   2750         current_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
   2751         current_sim_slot_index = get_slot_index_from_subid(
   2752             self.log, ad, current_data_sub_id)
   2753         if current_sim_slot_index == SIM1_SLOT_INDEX:
   2754             next_sim_slot_index = SIM2_SLOT_INDEX
   2755         else:
   2756             next_sim_slot_index = SIM1_SLOT_INDEX
   2757         next_data_sub_id = get_subid_from_slot_index(self.log, ad,
   2758                                                      next_sim_slot_index)
   2759         self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
   2760             current_data_sub_id, current_sim_slot_index))
   2761         if not ensure_network_generation_for_subscription(
   2762                 self.log,
   2763                 ad,
   2764                 ad.droid.subscriptionGetDefaultDataSubId(),
   2765                 GEN_2G,
   2766                 voice_or_data=NETWORK_SERVICE_DATA):
   2767             self.log.error("Device data does not attach to 2G.")
   2768             return False
   2769         if not verify_internet_connection(self.log, ad):
   2770             self.log.error("No Internet access on default Data SIM.")
   2771             return False
   2772 
   2773         self.log.info("Connect to WiFi and verify Internet access.")
   2774         if not ensure_wifi_connected(self.log, ad, self.wifi_network_ssid,
   2775                                      self.wifi_network_pass):
   2776             self.log.error("WiFi connect fail.")
   2777             return False
   2778         if (not wait_for_wifi_data_connection(self.log, ad, True)
   2779                 or not verify_internet_connection(self.log, ad)):
   2780             self.log.error("Data is not on WiFi")
   2781             return False
   2782 
   2783         try:
   2784             self.log.info(
   2785                 "Change Data SIM, Disable WiFi and verify Internet access.")
   2786             set_subid_for_data(ad, next_data_sub_id)
   2787             wifi_toggle_state(self.log, ad, False)
   2788             if not wait_for_data_attach_for_subscription(
   2789                     self.log, ad, next_data_sub_id,
   2790                     MAX_WAIT_TIME_NW_SELECTION):
   2791                 self.log.error("Failed to attach data on subId:{}".format(
   2792                     next_data_sub_id))
   2793                 return False
   2794             if not verify_internet_connection(self.log, ad):
   2795                 self.log.error("No Internet access after changing Data SIM.")
   2796                 return False
   2797 
   2798         finally:
   2799             self.log.info("Change Data SIM back.")
   2800             set_subid_for_data(ad, current_data_sub_id)
   2801 
   2802         return True
   2803 
   2804     @test_tracker_info(uuid="ef03eff7-ddd3-48e9-8f67-5e271e14048b")
   2805     @TelephonyBaseTest.tel_test_wrap
   2806     def test_vzw_embms_services(self):
   2807         ad = self.android_devices[0]
   2808         # Install App and Push config
   2809         self.log.info("Pushing embms config and apk to the Android device.")
   2810         android_embms_path = "/sdcard/mobitv"
   2811         embms_path = self.user_params.get("embms_path", "embms_path")
   2812         if isinstance(embms_path, list):
   2813             embms_path = embms_path[0]
   2814         ad.adb.shell("mkdir /sdcard/mobitv")
   2815         dcafile = os.path.join(embms_path, "dca.config")
   2816         apkfile = os.path.join(embms_path, "VzwDCA.apk")
   2817         ad.adb.push("%s %s" % (dcafile, android_embms_path))
   2818         ad.adb.install("%s" % apkfile)
   2819 
   2820         # Co-ordinates Mapping
   2821         lcd_density = ad.adb.shell("getprop ro.sf.lcd_density")
   2822         ad.log.debug("lcd_density %s" % lcd_density)
   2823         if "420" in lcd_density:
   2824             agree_y_axis = 1000
   2825         else:
   2826             agree_y_axis = 1300
   2827 
   2828         # Screen ON needed to open the VZW App
   2829         if "ON" in \
   2830         ad.adb.shell(
   2831                "dumpsys power | grep 'Display Power: state' | cut -d '=' -f2"):
   2832             ad.log.info("Screen already ON")
   2833             ad.adb.shell("input keyevent 82")
   2834         else:
   2835             ad.log.info("Screen OFF, turning ON")
   2836             ad.adb.shell("input keyevent 26")
   2837             ad.adb.shell("input keyevent 82")
   2838 
   2839         try:
   2840             # Check if app is installed
   2841             if ad.is_apk_installed("com.mobitv.vzwdca"):
   2842                 ad.log.info("VZWDCA App is successfully installed")
   2843             else:
   2844                 ad.log.error("VZWDCA App is not installed")
   2845                 return False
   2846 
   2847             # Grant Permissions, Start, Agree, Register
   2848             for cmd in ("pm grant com.mobitv.vzwdca "
   2849                         "android.permission.READ_EXTERNAL_STORAGE",
   2850                         "pm grant com.mobitv.vzwdca "
   2851                         "android.permission.WRITE_EXTERNAL_STORAGE",
   2852                         "am start -a android.intent.action.VIEW -n "
   2853                         "com.mobitv.vzwdca/.DcaActivity",
   2854                         "input tap 500 %d" % agree_y_axis, "input keyevent 66",
   2855                         "input keyevent 66", "input keyevent 66"):
   2856                 time.sleep(1)
   2857                 ad.log.info(cmd)
   2858                 ad.adb.shell(cmd)
   2859 
   2860             # Check Reg-DeReg
   2861             time.sleep(5)
   2862             if ad.is_apk_running("com.qualcomm.ltebc_vzw"):
   2863                 ad.log.info("EMBMS Registered successfully")
   2864                 ad.adb.shell("input keyevent 61")
   2865                 time.sleep(1)
   2866                 ad.adb.shell("input keyevent 66")
   2867                 time.sleep(1)
   2868                 if not ad.is_apk_running("com.qualcomm.ltebc_vzw"):
   2869                     ad.log.info("EMBMS De-Registered successfully")
   2870                     return True
   2871                 else:
   2872                     ad.log.error("EMBMS De-Registeration Failed")
   2873                     return False
   2874             else:
   2875                 ad.log.error("EMBMS Registeration Failed")
   2876                 return False
   2877         finally:
   2878             ad.log.info("Force Close the VZW App")
   2879             ad.adb.shell("am force-stop com.mobitv.vzwdca")
   2880 
   2881     @test_tracker_info(uuid="8a8cd773-77f5-4802-85ac-1a654bb4743c")
   2882     @TelephonyBaseTest.tel_test_wrap
   2883     def test_disable_data_on_non_active_data_sim(self):
   2884         """Switch Data SIM on 2G network.
   2885 
   2886         Steps:
   2887         1. Data on default Data SIM.
   2888         2. Disable data on non-active Data SIM.
   2889 
   2890         Expected Results:
   2891         1. Verify Data Status on Default Data SIM and non-active Data SIM.
   2892         1. Verify Data Status on Default Data SIM and non-active Data SIM.
   2893 
   2894         Returns:
   2895             True if success.
   2896             False if failed.
   2897         """
   2898         ad = self.android_devices[0]
   2899         current_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
   2900         current_sim_slot_index = get_slot_index_from_subid(
   2901             self.log, ad, current_data_sub_id)
   2902         if current_sim_slot_index == SIM1_SLOT_INDEX:
   2903             non_active_sim_slot_index = SIM2_SLOT_INDEX
   2904         else:
   2905             non_active_sim_slot_index = SIM1_SLOT_INDEX
   2906         non_active_sub_id = get_subid_from_slot_index(
   2907             self.log, ad, non_active_sim_slot_index)
   2908         self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
   2909             current_data_sub_id, current_sim_slot_index))
   2910 
   2911         if not ensure_network_generation_for_subscription(
   2912                 self.log,
   2913                 ad,
   2914                 ad.droid.subscriptionGetDefaultDataSubId(),
   2915                 GEN_2G,
   2916                 voice_or_data=NETWORK_SERVICE_DATA):
   2917             self.log.error("Device data does not attach to 2G.")
   2918             return False
   2919         if not verify_internet_connection(self.log, ad):
   2920             self.log.error("No Internet access on default Data SIM.")
   2921             return False
   2922 
   2923         if ad.droid.telephonyGetDataConnectionState() != DATA_STATE_CONNECTED:
   2924             self.log.error("Data Connection State should be connected.")
   2925             return False
   2926         # TODO: Check Data state for non-active subId.
   2927 
   2928         try:
   2929             self.log.info("Disable Data on Non-Active Sub ID")
   2930             ad.droid.telephonyToggleDataConnectionForSubscription(
   2931                 non_active_sub_id, False)
   2932             # TODO: Check Data state for non-active subId.
   2933             if ad.droid.telephonyGetDataConnectionState(
   2934             ) != DATA_STATE_CONNECTED:
   2935                 self.log.error("Data Connection State should be connected.")
   2936                 return False
   2937         finally:
   2938             self.log.info("Enable Data on Non-Active Sub ID")
   2939             ad.droid.telephonyToggleDataConnectionForSubscription(
   2940                 non_active_sub_id, True)
   2941         return True
   2942 
   2943     def file_download_stress(self):
   2944         failure = 0
   2945         total_count = 0
   2946         self.result_info = collections.defaultdict(int)
   2947         dut = self.android_devices[0]
   2948         self.max_sleep_time = int(self.user_params.get("max_sleep_time", 1200))
   2949         #file_names = ["5MB", "10MB", "20MB", "50MB", "200MB", "512MB", "1GB"]
   2950         file_names = ["5MB", "10MB", "20MB", "50MB", "200MB", "512MB"]
   2951         while total_count < self.stress_test_number:
   2952             total_count += 1
   2953             try:
   2954                 dut.log.info(dict(self.result_info))
   2955                 selection = random.randrange(0, len(file_names))
   2956                 file_name = file_names[selection]
   2957                 self.result_info["Total %s file download" % file_name] += 1
   2958                 if not active_file_download_test(self.log, dut, file_name):
   2959                     self.result_info["%s file download failure" %
   2960                                      file_name] += 1
   2961                     failure += 1
   2962                     dut.take_bug_report("%s_failure_%s" % (self.test_name,
   2963                                                            failure),
   2964                                         time.strftime("%m-%d-%Y-%H-%M-%S"))
   2965                     self.dut.droid.goToSleepNow()
   2966                     time.sleep(random.randrange(0, self.max_sleep_time))
   2967             except Exception as e:
   2968                 self.log.error("Exception error %s", str(e))
   2969                 self.result_info["Exception Errors"] += 1
   2970             dut.log.info("File download test failure: %s/%s", failure,
   2971                          total_count)
   2972         if failure / total_count > 0.1:
   2973             dut.log.error("File download test failure: %s/%s", failure,
   2974                           total_count)
   2975             return False
   2976         return True
   2977 
   2978     @test_tracker_info(uuid="5381a6fa-6771-4b00-a0d6-4a3891a6dba8")
   2979     @TelephonyBaseTest.tel_test_wrap
   2980     def test_file_download_stress_default(self):
   2981         """File download stress test
   2982 
   2983         Steps:
   2984         1. Download a file random picked.
   2985         2. Device sleep for sometime and Repeat 1.
   2986 
   2987         Expected Results:
   2988         Total download failure rate is less than 10%.
   2989 
   2990         Returns:
   2991             True if success.
   2992             False if failed.
   2993         """
   2994         return self.file_download_stress()
   2995 
   2996     @test_tracker_info(uuid="c9970955-123b-467c-afbb-95ec8f99e9b7")
   2997     def test_file_download_with_mobile_data_usage_limit_set(self):
   2998         """ Steps:
   2999         1. Set the data usage limit to current data usage + 9MB
   3000         2. Download 5MB file from internet.
   3001         3. The first file download should succeed
   3002         4. The second file download should fail
   3003         """
   3004         dut = self.android_devices[0]
   3005         ensure_phones_default_state(self.log, [dut])
   3006         subscriber_id = dut.droid.telephonyGetSubscriberId()
   3007         old_data_usage = get_mobile_data_usage(dut, subscriber_id)
   3008 
   3009         # set data usage limit to current usage limit + 10MB
   3010         data_limit = old_data_usage + 9 * 1000 * 1000
   3011         set_mobile_data_usage_limit(dut, data_limit, subscriber_id)
   3012 
   3013         # download file - size 5MB twice
   3014         try:
   3015             for _ in range(2):
   3016                 if not active_file_download_test(self.log, dut, "5MB"):
   3017                     if get_mobile_data_usage(
   3018                             dut, subscriber_id) + 5 * 1000 * 1000 < data_limit:
   3019                         dut.log.error(
   3020                             "Fail to download file when mobile data usage is"
   3021                             " below data usage limit")
   3022                         return False
   3023                     else:
   3024                         dut.log.info(
   3025                             "Download fails as expected due to data limit reached"
   3026                         )
   3027                 else:
   3028                     if get_mobile_data_usage(dut, subscriber_id) < data_limit:
   3029                         dut.log.info(
   3030                             "Download file succeed when mobile data usage is"
   3031                             " below data usage limit")
   3032                     else:
   3033                         dut.log.error(
   3034                             "Download should fail due to data limit reached")
   3035                         return False
   3036             return True
   3037         finally:
   3038             remove_mobile_data_usage_limit(dut, subscriber_id)
   3039         """ Tests End """
   3040