1#!/usr/bin/env python3 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 17import time 18 19from acts.utils import rand_ascii_str 20from acts.test_utils.tel.tel_subscription_utils import \ 21 get_subid_from_slot_index 22from acts.test_utils.tel.tel_subscription_utils import set_subid_for_data 23from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION 24from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA 25from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING 26from acts.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_STATE_CHECK 27from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_FOR_STATE_CHANGE 28from acts.test_utils.tel.tel_subscription_utils import get_default_data_sub_id 29from acts.test_utils.tel.tel_test_utils import start_youtube_video 30from acts.test_utils.tel.tel_test_utils import start_wifi_tethering 31from acts.test_utils.tel.tel_test_utils import stop_wifi_tethering 32from acts.test_utils.tel.tel_test_utils import ensure_network_generation_for_subscription 33from acts.test_utils.tel.tel_test_utils import ensure_phones_idle 34from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected 35from acts.test_utils.tel.tel_test_utils import get_network_rat_for_subscription 36from acts.test_utils.tel.tel_test_utils import is_droid_in_network_generation_for_subscription 37from acts.test_utils.tel.tel_test_utils import rat_generation_from_rat 38from acts.test_utils.tel.tel_test_utils import set_wifi_to_default 39from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode 40from acts.test_utils.tel.tel_test_utils import verify_internet_connection 41from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection 42from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection 43from acts.test_utils.tel.tel_test_utils import wait_for_data_attach_for_subscription 44from acts.test_utils.tel.tel_test_utils import wifi_toggle_state 45from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_2G 46from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G 47from acts.test_utils.tel.tel_test_utils import get_service_state_by_adb 48from acts.test_utils.tel.tel_test_utils import wait_for_state 49 50 51def wifi_tethering_cleanup(log, provider, client_list): 52 """Clean up steps for WiFi Tethering. 53 54 Make sure provider turn off tethering. 55 Make sure clients reset WiFi and turn on cellular data. 56 57 Args: 58 log: log object. 59 provider: android object provide WiFi tethering. 60 client_list: a list of clients using tethered WiFi. 61 62 Returns: 63 True if no error happened. False otherwise. 64 """ 65 for client in client_list: 66 client.droid.telephonyToggleDataConnection(True) 67 set_wifi_to_default(log, client) 68 if not stop_wifi_tethering(log, provider): 69 provider.log.error("Provider stop WiFi tethering failed.") 70 return False 71 if provider.droid.wifiIsApEnabled(): 72 provider.log.error("Provider WiFi tethering is still enabled.") 73 return False 74 return True 75 76 77def wifi_tethering_setup_teardown(log, 78 provider, 79 client_list, 80 ap_band=WIFI_CONFIG_APBAND_2G, 81 check_interval=30, 82 check_iteration=4, 83 do_cleanup=True, 84 ssid=None, 85 password=None): 86 """Test WiFi Tethering. 87 88 Turn off WiFi on clients. 89 Turn off data and reset WiFi on clients. 90 Verify no Internet access on clients. 91 Turn on WiFi tethering on provider. 92 Clients connect to provider's WiFI. 93 Verify Internet on provider and clients. 94 Tear down WiFi tethering setup and clean up. 95 96 Args: 97 log: log object. 98 provider: android object provide WiFi tethering. 99 client_list: a list of clients using tethered WiFi. 100 ap_band: setup WiFi tethering on 2G or 5G. 101 This is optional, default value is WIFI_CONFIG_APBAND_2G 102 check_interval: delay time between each around of Internet connection check. 103 This is optional, default value is 30 (seconds). 104 check_iteration: check Internet connection for how many times in total. 105 This is optional, default value is 4 (4 times). 106 do_cleanup: after WiFi tethering test, do clean up to tear down tethering 107 setup or not. This is optional, default value is True. 108 ssid: use this string as WiFi SSID to setup tethered WiFi network. 109 This is optional. Default value is None. 110 If it's None, a random string will be generated. 111 password: use this string as WiFi password to setup tethered WiFi network. 112 This is optional. Default value is None. 113 If it's None, a random string will be generated. 114 115 Returns: 116 True if no error happened. False otherwise. 117 """ 118 log.info("--->Start wifi_tethering_setup_teardown<---") 119 log.info("Provider: {}".format(provider.serial)) 120 if not provider.droid.connectivityIsTetheringSupported(): 121 provider.log.error( 122 "Provider does not support tethering. Stop tethering test.") 123 return False 124 125 if ssid is None: 126 ssid = rand_ascii_str(10) 127 if password is None: 128 password = rand_ascii_str(8) 129 130 # No password 131 if password == "": 132 password = None 133 134 try: 135 for client in client_list: 136 log.info("Client: {}".format(client.serial)) 137 wifi_toggle_state(log, client, False) 138 client.droid.telephonyToggleDataConnection(False) 139 log.info("WiFI Tethering: Verify client have no Internet access.") 140 for client in client_list: 141 if not verify_internet_connection( 142 log, client, expected_state=False): 143 client.log.error("Turn off Data on client fail") 144 return False 145 146 provider.log.info( 147 "Provider turn on WiFi tethering. SSID: %s, password: %s", ssid, 148 password) 149 150 if not start_wifi_tethering(log, provider, ssid, password, ap_band): 151 provider.log.error("Provider start WiFi tethering failed.") 152 return False 153 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) 154 155 provider.log.info("Provider check Internet connection.") 156 if not verify_internet_connection(log, provider): 157 return False 158 for client in client_list: 159 client.log.info( 160 "Client connect to WiFi and verify AP band correct.") 161 if not ensure_wifi_connected(log, client, ssid, password): 162 client.log.error("Client connect to WiFi failed.") 163 return False 164 165 wifi_info = client.droid.wifiGetConnectionInfo() 166 if ap_band == WIFI_CONFIG_APBAND_5G: 167 if wifi_info["is_24ghz"]: 168 client.log.error("Expected 5g network. WiFi Info: %s", 169 wifi_info) 170 return False 171 else: 172 if wifi_info["is_5ghz"]: 173 client.log.error("Expected 2g network. WiFi Info: %s", 174 wifi_info) 175 return False 176 177 client.log.info("Client check Internet connection.") 178 if (not wait_for_wifi_data_connection(log, client, True) 179 or not verify_internet_connection(log, client)): 180 client.log.error("No WiFi Data on client") 181 return False 182 183 if not tethering_check_internet_connection( 184 log, provider, client_list, check_interval, check_iteration): 185 return False 186 187 finally: 188 if (do_cleanup 189 and (not wifi_tethering_cleanup(log, provider, client_list))): 190 return False 191 return True 192 193 194def tethering_check_internet_connection(log, provider, client_list, 195 check_interval, check_iteration): 196 """During tethering test, check client(s) and provider Internet connection. 197 198 Do the following for <check_iteration> times: 199 Delay <check_interval> seconds. 200 Check Tethering provider's Internet connection. 201 Check each client's Internet connection. 202 203 Args: 204 log: log object. 205 provider: android object provide WiFi tethering. 206 client_list: a list of clients using tethered WiFi. 207 check_interval: delay time between each around of Internet connection check. 208 check_iteration: check Internet connection for how many times in total. 209 210 Returns: 211 True if no error happened. False otherwise. 212 """ 213 for i in range(1, check_iteration + 1): 214 result = True 215 time.sleep(check_interval) 216 provider.log.info( 217 "Provider check Internet connection after %s seconds.", 218 check_interval * i) 219 if not verify_internet_connection(log, provider): 220 result = False 221 continue 222 for client in client_list: 223 client.log.info( 224 "Client check Internet connection after %s seconds.", 225 check_interval * i) 226 if not verify_internet_connection(log, client): 227 result = False 228 break 229 if result: return result 230 return False 231 232 233def wifi_cell_switching(log, ad, wifi_network_ssid, wifi_network_pass, nw_gen): 234 """Test data connection network switching when phone on <nw_gen>. 235 236 Ensure phone is on <nw_gen> 237 Ensure WiFi can connect to live network, 238 Airplane mode is off, data connection is on, WiFi is on. 239 Turn off WiFi, verify data is on cell and browse to google.com is OK. 240 Turn on WiFi, verify data is on WiFi and browse to google.com is OK. 241 Turn off WiFi, verify data is on cell and browse to google.com is OK. 242 243 Args: 244 log: log object. 245 ad: android object. 246 wifi_network_ssid: ssid for live wifi network. 247 wifi_network_pass: password for live wifi network. 248 nw_gen: network generation the phone should be camped on. 249 250 Returns: 251 True if pass. 252 """ 253 try: 254 255 if not ensure_network_generation_for_subscription( 256 log, ad, get_default_data_sub_id(ad), nw_gen, 257 MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA): 258 ad.log.error("Device failed to register in %s", nw_gen) 259 return False 260 261 start_youtube_video(ad) 262 # Ensure WiFi can connect to live network 263 ad.log.info("Make sure phone can connect to live network by WIFI") 264 if not ensure_wifi_connected(log, ad, wifi_network_ssid, 265 wifi_network_pass): 266 ad.log.error("WiFi connect fail.") 267 return False 268 log.info("Phone connected to WIFI.") 269 270 log.info("Step1 Airplane Off, WiFi On, Data On.") 271 toggle_airplane_mode(log, ad, False) 272 wifi_toggle_state(log, ad, True) 273 ad.droid.telephonyToggleDataConnection(True) 274 if (not wait_for_wifi_data_connection(log, ad, True) 275 or not verify_internet_connection(log, ad)): 276 ad.log.error("Data is not on WiFi") 277 return False 278 279 log.info("Step2 WiFi is Off, Data is on Cell.") 280 wifi_toggle_state(log, ad, False) 281 if (not wait_for_cell_data_connection(log, ad, True) 282 or not verify_internet_connection(log, ad)): 283 ad.log.error("Data did not return to cell") 284 return False 285 286 log.info("Step3 WiFi is On, Data is on WiFi.") 287 wifi_toggle_state(log, ad, True) 288 if (not wait_for_wifi_data_connection(log, ad, True) 289 or not verify_internet_connection(log, ad)): 290 ad.log.error("Data did not return to WiFi") 291 return False 292 293 log.info("Step4 WiFi is Off, Data is on Cell.") 294 wifi_toggle_state(log, ad, False) 295 if (not wait_for_cell_data_connection(log, ad, True) 296 or not verify_internet_connection(log, ad)): 297 ad.log.error("Data did not return to cell") 298 return False 299 return True 300 301 finally: 302 ad.force_stop_apk("com.google.android.youtube") 303 wifi_toggle_state(log, ad, False) 304 305 306def airplane_mode_test(log, ad, retries=3): 307 """ Test airplane mode basic on Phone and Live SIM. 308 309 Ensure phone attach, data on, WiFi off and verify Internet. 310 Turn on airplane mode to make sure detach. 311 Turn off airplane mode to make sure attach. 312 Verify Internet connection. 313 314 Args: 315 log: log object. 316 ad: android object. 317 318 Returns: 319 True if pass; False if fail. 320 """ 321 if not ensure_phones_idle(log, [ad]): 322 log.error("Failed to return phones to idle.") 323 return False 324 325 try: 326 ad.droid.telephonyToggleDataConnection(True) 327 wifi_toggle_state(log, ad, False) 328 329 ad.log.info("Step1: disable airplane mode and ensure attach") 330 if not toggle_airplane_mode(log, ad, False): 331 ad.log.error("Failed initial attach") 332 return False 333 334 if not wait_for_state( 335 get_service_state_by_adb, 336 "IN_SERVICE", 337 MAX_WAIT_TIME_FOR_STATE_CHANGE, 338 WAIT_TIME_BETWEEN_STATE_CHECK, 339 log, 340 ad): 341 ad.log.error("Current service state is not 'IN_SERVICE'.") 342 return False 343 344 time.sleep(30) 345 if not ad.droid.connectivityNetworkIsConnected(): 346 ad.log.error("Network is NOT connected!") 347 return False 348 349 if not wait_for_cell_data_connection(log, ad, True): 350 ad.log.error("Failed to enable data connection.") 351 return False 352 353 if not verify_internet_connection(log, ad, retries=3): 354 ad.log.error("Data not available on cell.") 355 return False 356 357 log.info("Step2: enable airplane mode and ensure detach") 358 if not toggle_airplane_mode(log, ad, True): 359 ad.log.error("Failed to enable Airplane Mode") 360 return False 361 if not wait_for_cell_data_connection(log, ad, False): 362 ad.log.error("Failed to disable cell data connection") 363 return False 364 365 if not verify_internet_connection(log, ad, expected_state=False): 366 ad.log.error("Data available in airplane mode.") 367 return False 368 369 log.info("Step3: disable airplane mode and ensure attach") 370 if not toggle_airplane_mode(log, ad, False): 371 ad.log.error("Failed to disable Airplane Mode") 372 return False 373 374 if not wait_for_state( 375 get_service_state_by_adb, 376 "IN_SERVICE", 377 MAX_WAIT_TIME_FOR_STATE_CHANGE, 378 WAIT_TIME_BETWEEN_STATE_CHECK, 379 log, 380 ad): 381 ad.log.error("Current service state is not 'IN_SERVICE'.") 382 return False 383 384 time.sleep(30) 385 if not ad.droid.connectivityNetworkIsConnected(): 386 ad.log.warning("Network is NOT connected!") 387 388 if not wait_for_cell_data_connection(log, ad, True): 389 ad.log.error("Failed to enable cell data connection") 390 return False 391 392 if not verify_internet_connection(log, ad, retries=3): 393 ad.log.warning("Data not available on cell") 394 return False 395 return True 396 finally: 397 toggle_airplane_mode(log, ad, False) 398 399 400def data_connectivity_single_bearer(log, ad, nw_gen): 401 """Test data connection: single-bearer (no voice). 402 403 Turn off airplane mode, enable Cellular Data. 404 Ensure phone data generation is expected. 405 Verify Internet. 406 Disable Cellular Data, verify Internet is inaccessible. 407 Enable Cellular Data, verify Internet. 408 409 Args: 410 log: log object. 411 ad: android object. 412 nw_gen: network generation the phone should on. 413 414 Returns: 415 True if success. 416 False if failed. 417 """ 418 ensure_phones_idle(log, [ad]) 419 wait_time = MAX_WAIT_TIME_NW_SELECTION 420 if getattr(ad, 'roaming', False): 421 wait_time = 2 * wait_time 422 if not ensure_network_generation_for_subscription( 423 log, ad, get_default_data_sub_id(ad), nw_gen, 424 MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA): 425 ad.log.error("Device failed to connect to %s in %s seconds.", nw_gen, 426 wait_time) 427 return False 428 429 try: 430 log.info("Step1 Airplane Off, Data On.") 431 toggle_airplane_mode(log, ad, False) 432 ad.droid.telephonyToggleDataConnection(True) 433 if not wait_for_cell_data_connection(log, ad, True, timeout_value=wait_time): 434 ad.log.error("Failed to enable data connection.") 435 return False 436 437 log.info("Step2 Verify internet") 438 if not verify_internet_connection(log, ad, retries=3): 439 ad.log.error("Data not available on cell.") 440 return False 441 442 log.info("Step3 Turn off data and verify not connected.") 443 ad.droid.telephonyToggleDataConnection(False) 444 if not wait_for_cell_data_connection(log, ad, False): 445 ad.log.error("Step3 Failed to disable data connection.") 446 return False 447 448 if not verify_internet_connection(log, ad, expected_state=False): 449 ad.log.error("Step3 Data still available when disabled.") 450 return False 451 452 log.info("Step4 Re-enable data.") 453 ad.droid.telephonyToggleDataConnection(True) 454 if not wait_for_cell_data_connection(log, ad, True, timeout_value=wait_time): 455 ad.log.error("Step4 failed to re-enable data.") 456 return False 457 if not verify_internet_connection(log, ad, retries=3): 458 ad.log.error("Data not available on cell.") 459 return False 460 461 if not is_droid_in_network_generation_for_subscription( 462 log, ad, get_default_data_sub_id(ad), nw_gen, 463 NETWORK_SERVICE_DATA): 464 ad.log.error("Failed: droid is no longer on correct network") 465 ad.log.info("Expected:%s, Current:%s", nw_gen, 466 rat_generation_from_rat( 467 get_network_rat_for_subscription( 468 log, ad, get_default_data_sub_id(ad), 469 NETWORK_SERVICE_DATA))) 470 return False 471 return True 472 finally: 473 ad.droid.telephonyToggleDataConnection(True) 474 475 476def change_data_sim_and_verify_data(log, ad, sim_slot): 477 """Change Data SIM and verify Data attach and Internet access 478 479 Args: 480 log: log object. 481 ad: android device object. 482 sim_slot: SIM slot index. 483 484 Returns: 485 Data SIM changed successfully, data attached and Internet access is OK. 486 """ 487 sub_id = get_subid_from_slot_index(log, ad, sim_slot) 488 ad.log.info("Change Data to subId: %s, SIM slot: %s", sub_id, sim_slot) 489 set_subid_for_data(ad, sub_id) 490 if not wait_for_data_attach_for_subscription(log, ad, sub_id, 491 MAX_WAIT_TIME_NW_SELECTION): 492 ad.log.error("Failed to attach data on subId:%s", sub_id) 493 return False 494 if not verify_internet_connection(log, ad): 495 ad.log.error("No Internet access after changing Data SIM.") 496 return False 497 return True 498