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 Settings
18"""
19
20import os
21import time
22
23from acts import signals
24from acts.keys import Config
25from acts.utils import create_dir
26from acts.utils import unzip_maintain_permissions
27from acts.utils import get_current_epoch_time
28from acts.utils import exe_cmd
29from acts.test_decorators import test_tracker_info
30from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
31from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WIFI_CONNECTION
32from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
33from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_IMS_REGISTRATION
34from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_VOLTE_ENABLED
35from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_ENABLED
36from acts.test_utils.tel.tel_defines import RAT_FAMILY_WLAN
37from acts.test_utils.tel.tel_defines import WFC_MODE_CELLULAR_PREFERRED
38from acts.test_utils.tel.tel_defines import WFC_MODE_DISABLED
39from acts.test_utils.tel.tel_defines import WFC_MODE_WIFI_ONLY
40from acts.test_utils.tel.tel_defines import WFC_MODE_WIFI_PREFERRED
41from acts.test_utils.tel.tel_test_utils import call_setup_teardown
42from acts.test_utils.tel.tel_test_utils import ensure_phone_subscription
43from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
44from acts.test_utils.tel.tel_test_utils import flash_radio
45from acts.test_utils.tel.tel_test_utils import get_outgoing_voice_sub_id
46from acts.test_utils.tel.tel_test_utils import get_slot_index_from_subid
47from acts.test_utils.tel.tel_test_utils import is_droid_in_rat_family
48from acts.test_utils.tel.tel_test_utils import is_sim_locked
49from acts.test_utils.tel.tel_test_utils import is_wfc_enabled
50from acts.test_utils.tel.tel_test_utils import multithread_func
51from acts.test_utils.tel.tel_test_utils import power_off_sim
52from acts.test_utils.tel.tel_test_utils import power_on_sim
53from acts.test_utils.tel.tel_test_utils import print_radio_info
54from acts.test_utils.tel.tel_test_utils import set_qxdm_logger_command
55from acts.test_utils.tel.tel_test_utils import set_wfc_mode
56from acts.test_utils.tel.tel_test_utils import system_file_push
57from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode_by_adb
58from acts.test_utils.tel.tel_test_utils import toggle_volte
59from acts.test_utils.tel.tel_test_utils import unlock_sim
60from acts.test_utils.tel.tel_test_utils import verify_http_connection
61from acts.test_utils.tel.tel_test_utils import wait_for_ims_registered
62from acts.test_utils.tel.tel_test_utils import wait_for_network_rat
63from acts.test_utils.tel.tel_test_utils import wait_for_not_network_rat
64from acts.test_utils.tel.tel_test_utils import wait_for_volte_enabled
65from acts.test_utils.tel.tel_test_utils import wait_for_wfc_disabled
66from acts.test_utils.tel.tel_test_utils import wait_for_wfc_enabled
67from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection
68from acts.test_utils.tel.tel_test_utils import wifi_reset
69from acts.test_utils.tel.tel_test_utils import wifi_toggle_state
70from acts.test_utils.tel.tel_test_utils import set_wifi_to_default
71from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_volte
72from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_3g
73from acts.test_utils.tel.tel_voice_utils import phone_setup_csfb
74from acts.test_utils.tel.tel_voice_utils import phone_setup_iwlan
75from acts.test_utils.tel.tel_voice_utils import phone_setup_volte
76from acts.test_utils.tel.tel_voice_utils import phone_idle_iwlan
77from acts.test_utils.tel.tel_voice_utils import phone_idle_volte
78from acts.utils import set_mobile_data_always_on
79
80
81class TelLiveSettingsTest(TelephonyBaseTest):
82
83    _TEAR_DOWN_OPERATION_DISCONNECT_WIFI = "disconnect_wifi"
84    _TEAR_DOWN_OPERATION_RESET_WIFI = "reset_wifi"
85    _TEAR_DOWN_OPERATION_DISABLE_WFC = "disable_wfc"
86
87    def __init__(self, controllers):
88        TelephonyBaseTest.__init__(self, controllers)
89
90        self.ad = self.android_devices[0]
91        self.wifi_network_ssid = self.user_params["wifi_network_ssid"]
92        try:
93            self.wifi_network_pass = self.user_params["wifi_network_pass"]
94        except KeyError:
95            self.wifi_network_pass = None
96        self.number_of_devices = 1
97        self.stress_test_number = self.get_stress_test_number()
98
99    def _wifi_connected_enable_wfc_teardown_wfc(
100            self,
101            tear_down_operation,
102            initial_setup_wifi=True,
103            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED,
104            check_volte_after_wfc_disabled=False):
105        if initial_setup_wifi and not ensure_wifi_connected(
106                self.log, self.ad, self.wifi_network_ssid,
107                self.wifi_network_pass):
108            self.log.error("Failed to connect WiFi")
109            return False
110        if initial_setup_wfc_mode and not set_wfc_mode(self.log, self.ad,
111                                                       initial_setup_wfc_mode):
112            self.log.error("Failed to set WFC mode.")
113            return False
114        if not phone_idle_iwlan(self.log, self.ad):
115            self.log.error("WFC is not available.")
116            return False
117
118        # Tear Down WFC based on tear_down_operation
119        if tear_down_operation == self._TEAR_DOWN_OPERATION_DISCONNECT_WIFI:
120            if not wifi_toggle_state(self.log, self.ad, False):
121                self.ad.log.error("Failed to turn off WiFi.")
122                return False
123        elif tear_down_operation == self._TEAR_DOWN_OPERATION_RESET_WIFI:
124            if not wifi_reset(self.log, self.ad, False):
125                self.ad.log.error("Failed to reset WiFi")
126                return False
127        elif tear_down_operation == self._TEAR_DOWN_OPERATION_DISABLE_WFC:
128            if not set_wfc_mode(self.log, self.ad, WFC_MODE_DISABLED):
129                self.ad.log.error("Failed to turn off WFC.")
130                return False
131        else:
132            self.log.info("No tear down operation")
133            return True
134
135        if not wait_for_wfc_disabled(self.log, self.ad):
136            self.log.error(
137                "WFC is still available after turn off WFC or WiFi.")
138            return False
139
140        #For SMS over Wifi, data will be in IWLAN with WFC off
141        if tear_down_operation != self._TEAR_DOWN_OPERATION_DISABLE_WFC and (
142                not wait_for_not_network_rat(
143                    self.log,
144                    self.ad,
145                    RAT_FAMILY_WLAN,
146                    voice_or_data=NETWORK_SERVICE_DATA)):
147            self.log.error("Data Rat is still iwlan.")
148            return False
149
150        # If VoLTE was previous available, after tear down WFC, DUT should have
151        # VoLTE service.
152        if check_volte_after_wfc_disabled and not wait_for_volte_enabled(
153                self.log, self.ad, MAX_WAIT_TIME_VOLTE_ENABLED):
154            self.log.error("Device failed to acquire VoLTE service")
155            return False
156        return True
157
158    def _wifi_connected_set_wfc_mode_change_wfc_mode(
159            self,
160            initial_wfc_mode,
161            new_wfc_mode,
162            is_wfc_available_in_initial_wfc_mode,
163            is_wfc_available_in_new_wfc_mode,
164            initial_setup_wifi=True,
165            check_volte_after_wfc_disabled=False):
166        if initial_setup_wifi and not ensure_wifi_connected(
167                self.log, self.ad, self.wifi_network_ssid,
168                self.wifi_network_pass):
169            self.log.error("Failed to connect WiFi")
170            return False
171        # Set to initial_wfc_mode first, then change to new_wfc_mode
172        for (wfc_mode, is_wfc_available) in \
173            [(initial_wfc_mode, is_wfc_available_in_initial_wfc_mode),
174             (new_wfc_mode, is_wfc_available_in_new_wfc_mode)]:
175            current_wfc_status = is_wfc_enabled(self.log, self.ad)
176            self.log.info("Current WFC: {}, Set WFC to {}".format(
177                current_wfc_status, wfc_mode))
178            if not set_wfc_mode(self.log, self.ad, wfc_mode):
179                self.log.error("Failed to set WFC mode.")
180                return False
181            if is_wfc_available:
182                if current_wfc_status:
183                    # Previous is True, after set it still need to be true
184                    # wait and check if DUT WFC got disabled.
185                    if wait_for_wfc_disabled(self.log, self.ad):
186                        self.log.error("WFC is not available.")
187                        return False
188                else:
189                    # Previous is False, after set it will be true,
190                    # wait and check if DUT WFC got enabled.
191                    if not wait_for_wfc_enabled(self.log, self.ad):
192                        self.log.error("WFC is not available.")
193                        return False
194            else:
195                if current_wfc_status:
196                    # Previous is True, after set it will be false,
197                    # wait and check if DUT WFC got disabled.
198                    if not wait_for_wfc_disabled(self.log, self.ad):
199                        self.log.error("WFC is available.")
200                        return False
201                else:
202                    # Previous is False, after set it still need to be false
203                    # Wait and check if DUT WFC got enabled.
204                    if wait_for_wfc_enabled(self.log, self.ad):
205                        self.log.error("WFC is available.")
206                        return False
207                if check_volte_after_wfc_disabled and not wait_for_volte_enabled(
208                        self.log, self.ad, MAX_WAIT_TIME_VOLTE_ENABLED):
209                    self.log.error("Device failed to acquire VoLTE service")
210                    return False
211        return True
212
213    def _wifi_connected_set_wfc_mode_turn_off_apm(
214            self, wfc_mode, is_wfc_available_after_turn_off_apm):
215        if not ensure_wifi_connected(self.log, self.ad, self.wifi_network_ssid,
216                                     self.wifi_network_pass):
217            self.log.error("Failed to connect WiFi")
218            return False
219        if not set_wfc_mode(self.log, self.ad, wfc_mode):
220            self.log.error("Failed to set WFC mode.")
221            return False
222        if not phone_idle_iwlan(self.log, self.ad):
223            self.log.error("WFC is not available.")
224            return False
225        if not toggle_airplane_mode_by_adb(self.log, self.ad, False):
226            self.log.error("Failed to turn off airplane mode")
227            return False
228        is_wfc_not_available = wait_for_wfc_disabled(self.log, self.ad)
229        if is_wfc_available_after_turn_off_apm and is_wfc_not_available:
230            self.log.error("WFC is not available.")
231            return False
232        elif (not is_wfc_available_after_turn_off_apm
233              and not is_wfc_not_available):
234            self.log.error("WFC is available.")
235            return False
236        return True
237
238    @TelephonyBaseTest.tel_test_wrap
239    @test_tracker_info(uuid="a3a680ba-d1e0-4770-a38c-4de8f15f9171")
240    def test_lte_volte_wifi_connected_toggle_wfc(self):
241        """Test for WiFi Calling settings:
242        LTE + VoLTE Enabled + WiFi Connected, Toggling WFC
243
244        Steps:
245        1. Setup DUT Idle, LTE network type, VoLTE enabled.
246        2. Make sure DUT WiFi connected, WFC disabled.
247        3. Set DUT WFC enabled (WiFi Preferred), verify DUT WFC available,
248            report iwlan rat.
249        4. Set DUT WFC disabled, verify DUT WFC unavailable,
250            not report iwlan rat.
251
252        Expected Results:
253        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
254        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
255        """
256
257        if not phone_setup_volte(self.log, self.ad):
258            self.log.error("Failed to setup VoLTE")
259            return False
260        return self._wifi_connected_enable_wfc_teardown_wfc(
261            tear_down_operation=self._TEAR_DOWN_OPERATION_DISABLE_WFC,
262            initial_setup_wifi=True,
263            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED,
264            check_volte_after_wfc_disabled=True)
265
266    @TelephonyBaseTest.tel_test_wrap
267    @test_tracker_info(uuid="d3ffae75-ae4a-4ed8-9337-9155c413311d")
268    def test_lte_wifi_connected_toggle_wfc(self):
269        """Test for WiFi Calling settings:
270        LTE + VoLTE Disabled + WiFi Connected, Toggling WFC
271
272        Steps:
273        1. Setup DUT Idle, LTE network type, VoLTE disabled.
274        2. Make sure DUT WiFi connected, WFC disabled.
275        3. Set DUT WFC enabled (WiFi Preferred), verify DUT WFC available,
276            report iwlan rat.
277        4. Set DUT WFC disabled, verify DUT WFC unavailable,
278            not report iwlan rat.
279
280        Expected Results:
281        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
282        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
283        """
284
285        if not phone_setup_csfb(self.log, self.ad):
286            self.log.error("Failed to setup LTE")
287            return False
288        return self._wifi_connected_enable_wfc_teardown_wfc(
289            tear_down_operation=self._TEAR_DOWN_OPERATION_DISABLE_WFC,
290            initial_setup_wifi=True,
291            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED)
292
293    @TelephonyBaseTest.tel_test_wrap
294    @test_tracker_info(uuid="29d2d7b7-1c31-4a2c-896a-3f6756c620ac")
295    def test_3g_wifi_connected_toggle_wfc(self):
296        """Test for WiFi Calling settings:
297        3G + WiFi Connected, Toggling WFC
298
299        Steps:
300        1. Setup DUT Idle, 3G network type.
301        2. Make sure DUT WiFi connected, WFC disabled.
302        3. Set DUT WFC enabled (WiFi Preferred), verify DUT WFC available,
303            report iwlan rat.
304        4. Set DUT WFC disabled, verify DUT WFC unavailable,
305            not report iwlan rat.
306
307        Expected Results:
308        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
309        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
310        """
311        set_wifi_to_default(self.log, self.ad)
312        if not phone_setup_voice_3g(self.log, self.ad):
313            self.log.error("Failed to setup 3G")
314            return False
315        return self._wifi_connected_enable_wfc_teardown_wfc(
316            tear_down_operation=self._TEAR_DOWN_OPERATION_DISABLE_WFC,
317            initial_setup_wifi=True,
318            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED)
319
320    @TelephonyBaseTest.tel_test_wrap
321    @test_tracker_info(uuid="ce2c0208-9ea0-4b31-91f4-d06a62cb927a")
322    def test_apm_wifi_connected_toggle_wfc(self):
323        """Test for WiFi Calling settings:
324        APM + WiFi Connected, Toggling WFC
325
326        Steps:
327        1. Setup DUT Idle, Airplane mode.
328        2. Make sure DUT WiFi connected, WFC disabled.
329        3. Set DUT WFC enabled (WiFi Preferred), verify DUT WFC available,
330            report iwlan rat.
331        4. Set DUT WFC disabled, verify DUT WFC unavailable,
332            not report iwlan rat.
333
334        Expected Results:
335        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
336        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
337        """
338
339        if not toggle_airplane_mode_by_adb(self.log, self.ad, True):
340            self.log.error("Failed to turn on airplane mode")
341            return False
342        return self._wifi_connected_enable_wfc_teardown_wfc(
343            tear_down_operation=self._TEAR_DOWN_OPERATION_DISABLE_WFC,
344            initial_setup_wifi=True,
345            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED)
346
347    @TelephonyBaseTest.tel_test_wrap
348    @test_tracker_info(uuid="681e2448-32a2-434d-abd6-0bc2ab5afd9c")
349    def test_lte_volte_wfc_enabled_toggle_wifi(self):
350        """Test for WiFi Calling settings:
351        LTE + VoLTE Enabled + WFC enabled, Toggling WiFi
352
353        Steps:
354        1. Setup DUT Idle, LTE network type, VoLTE enabled.
355        2. Make sure DUT WiFi disconnected, WFC enabled (WiFi Preferred).
356        3. DUT connect WiFi, verify DUT WFC available, report iwlan rat.
357        4. DUT disconnect WiFi,verify DUT WFC unavailable, not report iwlan rat.
358
359        Expected Results:
360        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
361        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
362        """
363
364        if not phone_setup_volte(self.log, self.ad):
365            self.log.error("Failed to setup VoLTE")
366            return False
367        return self._wifi_connected_enable_wfc_teardown_wfc(
368            tear_down_operation=self._TEAR_DOWN_OPERATION_DISCONNECT_WIFI,
369            initial_setup_wifi=True,
370            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED,
371            check_volte_after_wfc_disabled=True)
372
373    @TelephonyBaseTest.tel_test_wrap
374    @test_tracker_info(uuid="63922066-9caa-42e6-bc9f-49f5ac01cbe2")
375    def test_lte_wfc_enabled_toggle_wifi(self):
376        """Test for WiFi Calling settings:
377        LTE + VoLTE Disabled + WFC enabled, Toggling WiFi
378
379        Steps:
380        1. Setup DUT Idle, LTE network type, VoLTE disabled.
381        2. Make sure DUT WiFi disconnected, WFC enabled (WiFi Preferred).
382        3. DUT connect WiFi, verify DUT WFC available, report iwlan rat.
383        4. DUT disconnect WiFi,verify DUT WFC unavailable, not report iwlan rat.
384
385        Expected Results:
386        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
387        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
388        """
389
390        if not phone_setup_csfb(self.log, self.ad):
391            self.log.error("Failed to setup LTE")
392            return False
393        return self._wifi_connected_enable_wfc_teardown_wfc(
394            tear_down_operation=self._TEAR_DOWN_OPERATION_DISCONNECT_WIFI,
395            initial_setup_wifi=True,
396            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED)
397
398    @TelephonyBaseTest.tel_test_wrap
399    @test_tracker_info(uuid="8a80a446-2116-4b19-b0ef-f771f30a6d15")
400    def test_3g_wfc_enabled_toggle_wifi(self):
401        """Test for WiFi Calling settings:
402        3G + WFC enabled, Toggling WiFi
403
404        Steps:
405        1. Setup DUT Idle, 3G network type.
406        2. Make sure DUT WiFi disconnected, WFC enabled (WiFi Preferred).
407        3. DUT connect WiFi, verify DUT WFC available, report iwlan rat.
408        4. DUT disconnect WiFi,verify DUT WFC unavailable, not report iwlan rat.
409
410        Expected Results:
411        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
412        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
413        """
414        set_wifi_to_default(self.log, self.ad)
415        if not phone_setup_voice_3g(self.log, self.ad):
416            self.log.error("Failed to setup 3G")
417            return False
418        return self._wifi_connected_enable_wfc_teardown_wfc(
419            tear_down_operation=self._TEAR_DOWN_OPERATION_DISCONNECT_WIFI,
420            initial_setup_wifi=True,
421            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED)
422
423    @TelephonyBaseTest.tel_test_wrap
424    @test_tracker_info(uuid="9889eebf-cde6-4f47-aec0-9cb204fdf2e5")
425    def test_apm_wfc_enabled_toggle_wifi(self):
426        """Test for WiFi Calling settings:
427        APM + WFC enabled, Toggling WiFi
428
429        Steps:
430        1. Setup DUT Idle, Airplane mode.
431        2. Make sure DUT WiFi disconnected, WFC enabled (WiFi Preferred).
432        3. DUT connect WiFi, verify DUT WFC available, report iwlan rat.
433        4. DUT disconnect WiFi,verify DUT WFC unavailable, not report iwlan rat.
434
435        Expected Results:
436        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
437        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
438        """
439
440        if not toggle_airplane_mode_by_adb(self.log, self.ad, True):
441            self.log.error("Failed to turn on airplane mode")
442            return False
443        return self._wifi_connected_enable_wfc_teardown_wfc(
444            tear_down_operation=self._TEAR_DOWN_OPERATION_DISCONNECT_WIFI,
445            initial_setup_wifi=True,
446            initial_setup_wfc_mode=WFC_MODE_WIFI_PREFERRED)
447
448    @TelephonyBaseTest.tel_test_wrap
449    @test_tracker_info(uuid="9b23e04b-4f70-4e73-88e7-6376262c739d")
450    def test_lte_wfc_enabled_wifi_connected_toggle_volte(self):
451        """Test for WiFi Calling settings:
452        LTE + VoLTE Enabled + WiFi Connected + WFC enabled, toggle VoLTE setting
453
454        Steps:
455        1. Setup DUT Idle, LTE network type, VoLTE enabled.
456        2. Make sure DUT WiFi connected, WFC enabled (WiFi Preferred).
457            Verify DUT WFC available, report iwlan rat.
458        3. Disable VoLTE on DUT, verify in 2 minutes period,
459            DUT does not lost WiFi Calling, DUT still report WFC available,
460            rat iwlan.
461        4. Enable VoLTE on DUT, verify in 2 minutes period,
462            DUT does not lost WiFi Calling, DUT still report WFC available,
463            rat iwlan.
464
465        Expected Results:
466        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
467        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
468        4. DUT WiFi Calling feature bit return True, network rat is iwlan.
469        """
470        if not phone_setup_volte(self.log, self.ad):
471            self.log.error("Failed to setup VoLTE.")
472            return False
473        if not phone_setup_iwlan(
474                self.log, self.ad, False, WFC_MODE_WIFI_PREFERRED,
475                self.wifi_network_ssid, self.wifi_network_pass):
476            self.log.error("Failed to setup WFC.")
477            return False
478        # Turn Off VoLTE, then Turn On VoLTE
479        for i in range(2):
480            if not toggle_volte(self.log, self.ad):
481                self.log.error("Failed to toggle VoLTE.")
482                return False
483            if wait_for_wfc_disabled(self.log, self.ad):
484                self.log.error("WFC is not available.")
485                return False
486            if not is_droid_in_rat_family(self.log, self.ad, RAT_FAMILY_WLAN,
487                                          NETWORK_SERVICE_DATA):
488                self.log.error("Data Rat is not iwlan.")
489                return False
490        return True
491
492    @TelephonyBaseTest.tel_test_wrap
493    @test_tracker_info(uuid="04bdfda4-06f7-41df-9352-a8534bc2a67a")
494    def test_lte_volte_wfc_wifi_preferred_to_cellular_preferred(self):
495        """Test for WiFi Calling settings:
496        LTE + VoLTE Enabled + WiFi Connected + WiFi Preferred,
497        change WFC to Cellular Preferred
498
499        Steps:
500        1. Setup DUT Idle, LTE network type, VoLTE enabled.
501        2. Make sure DUT WiFi connected, WFC is set to WiFi Preferred.
502            Verify DUT WFC available, report iwlan rat.
503        3. Change WFC setting to Cellular Preferred.
504        4. Verify DUT report WFC not available.
505
506        Expected Results:
507        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
508        4. DUT WiFI Calling feature bit return False, network rat is not iwlan.
509        """
510        if not phone_setup_volte(self.log, self.ad):
511            self.log.error("Failed to setup VoLTE.")
512            return False
513        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
514            WFC_MODE_WIFI_PREFERRED,
515            WFC_MODE_CELLULAR_PREFERRED,
516            True,
517            False,
518            check_volte_after_wfc_disabled=True)
519
520    @TelephonyBaseTest.tel_test_wrap
521    @test_tracker_info(uuid="80d26bdb-992a-4b30-ad51-68308d5af168")
522    def test_lte_wfc_wifi_preferred_to_cellular_preferred(self):
523        """Test for WiFi Calling settings:
524        LTE + WiFi Connected + WiFi Preferred, change WFC to Cellular Preferred
525
526        Steps:
527        1. Setup DUT Idle, LTE network type, VoLTE disabled.
528        2. Make sure DUT WiFi connected, WFC is set to WiFi Preferred.
529            Verify DUT WFC available, report iwlan rat.
530        3. Change WFC setting to Cellular Preferred.
531        4. Verify DUT report WFC not available.
532
533        Expected Results:
534        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
535        4. DUT WiFI Calling feature bit return False, network rat is not iwlan.
536        """
537        if not phone_setup_csfb(self.log, self.ad):
538            self.log.error("Failed to setup LTE.")
539            return False
540        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
541            WFC_MODE_WIFI_PREFERRED,
542            WFC_MODE_CELLULAR_PREFERRED,
543            True,
544            False,
545            check_volte_after_wfc_disabled=False)
546
547    @TelephonyBaseTest.tel_test_wrap
548    @test_tracker_info(uuid="d486c7e3-3d2b-4552-8af8-7b19f6347427")
549    def test_3g_wfc_wifi_preferred_to_cellular_preferred(self):
550        """Test for WiFi Calling settings:
551        3G + WiFi Connected + WiFi Preferred, change WFC to Cellular Preferred
552
553        Steps:
554        1. Setup DUT Idle, 3G network type.
555        2. Make sure DUT WiFi connected, WFC is set to WiFi Preferred.
556            Verify DUT WFC available, report iwlan rat.
557        3. Change WFC setting to Cellular Preferred.
558        4. Verify DUT report WFC not available.
559
560        Expected Results:
561        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
562        4. DUT WiFI Calling feature bit return False, network rat is not iwlan.
563        """
564        set_wifi_to_default(self.log, self.ad)
565        if not phone_setup_voice_3g(self.log, self.ad):
566            self.log.error("Failed to setup 3G.")
567            return False
568        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
569            WFC_MODE_WIFI_PREFERRED, WFC_MODE_CELLULAR_PREFERRED, True, False)
570
571    @TelephonyBaseTest.tel_test_wrap
572    @test_tracker_info(uuid="0feb0add-8e22-4c86-b13e-be68659cdd87")
573    def test_apm_wfc_wifi_preferred_to_cellular_preferred(self):
574        """Test for WiFi Calling settings:
575        APM + WiFi Connected + WiFi Preferred, change WFC to Cellular Preferred
576
577        Steps:
578        1. Setup DUT Idle, airplane mode.
579        2. Make sure DUT WiFi connected, WFC is set to WiFi Preferred.
580            Verify DUT WFC available, report iwlan rat.
581        3. Change WFC setting to Cellular Preferred.
582        4. Verify DUT report WFC not available.
583
584        Expected Results:
585        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
586        4. DUT WiFI Calling feature bit return True, network rat is iwlan.
587        """
588        if not toggle_airplane_mode_by_adb(self.log, self.ad, True):
589            self.log.error("Failed to turn on airplane mode")
590            return False
591        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
592            WFC_MODE_WIFI_PREFERRED, WFC_MODE_CELLULAR_PREFERRED, True, True)
593
594    @TelephonyBaseTest.tel_test_wrap
595    @test_tracker_info(uuid="9c8f359f-a084-4413-b8a9-34771af166c5")
596    def test_lte_volte_wfc_cellular_preferred_to_wifi_preferred(self):
597        """Test for WiFi Calling settings:
598        LTE + VoLTE Enabled + WiFi Connected + Cellular Preferred,
599        change WFC to WiFi Preferred
600
601        Steps:
602        1. Setup DUT Idle, LTE network type, VoLTE enabled.
603        2. Make sure DUT WiFi connected, WFC is set to Cellular Preferred.
604            Verify DUT WFC not available.
605        3. Change WFC setting to WiFi Preferred.
606        4. Verify DUT report WFC available.
607
608        Expected Results:
609        2. DUT WiFi Calling feature bit return False, network rat is not iwlan.
610        4. DUT WiFI Calling feature bit return True, network rat is iwlan.
611        """
612        if not phone_setup_volte(self.log, self.ad):
613            self.log.error("Failed to setup VoLTE.")
614            return False
615        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
616            WFC_MODE_CELLULAR_PREFERRED,
617            WFC_MODE_WIFI_PREFERRED,
618            False,
619            True,
620            check_volte_after_wfc_disabled=True)
621
622    @TelephonyBaseTest.tel_test_wrap
623    @test_tracker_info(uuid="1894e685-63cf-43aa-91ed-938782ca35a9")
624    def test_lte_wfc_cellular_preferred_to_wifi_preferred(self):
625        """Test for WiFi Calling settings:
626        LTE + WiFi Connected + Cellular Preferred, change WFC to WiFi Preferred
627
628        Steps:
629        1. Setup DUT Idle, LTE network type, VoLTE disabled.
630        2. Make sure DUT WiFi connected, WFC is set to Cellular Preferred.
631            Verify DUT WFC not available.
632        3. Change WFC setting to WiFi Preferred.
633        4. Verify DUT report WFC available.
634
635        Expected Results:
636        2. DUT WiFi Calling feature bit return False, network rat is not iwlan.
637        4. DUT WiFI Calling feature bit return True, network rat is iwlan.
638        """
639        if not phone_setup_csfb(self.log, self.ad):
640            self.log.error("Failed to setup LTE.")
641            return False
642        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
643            WFC_MODE_CELLULAR_PREFERRED,
644            WFC_MODE_WIFI_PREFERRED,
645            False,
646            True,
647            check_volte_after_wfc_disabled=False)
648
649    @TelephonyBaseTest.tel_test_wrap
650    @test_tracker_info(uuid="e7fb6a6c-4672-44da-bca2-78b4d96dea9e")
651    def test_3g_wfc_cellular_preferred_to_wifi_preferred(self):
652        """Test for WiFi Calling settings:
653        3G + WiFi Connected + Cellular Preferred, change WFC to WiFi Preferred
654
655        Steps:
656        1. Setup DUT Idle, 3G network type.
657        2. Make sure DUT WiFi connected, WFC is set to Cellular Preferred.
658            Verify DUT WFC not available.
659        3. Change WFC setting to WiFi Preferred.
660        4. Verify DUT report WFC available.
661
662        Expected Results:
663        2. DUT WiFi Calling feature bit return False, network rat is not iwlan.
664        4. DUT WiFI Calling feature bit return True, network rat is iwlan.
665        """
666        set_wifi_to_default(self.log, self.ad)
667        if not phone_setup_voice_3g(self.log, self.ad):
668            self.log.error("Failed to setup 3G.")
669            return False
670        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
671            WFC_MODE_CELLULAR_PREFERRED, WFC_MODE_WIFI_PREFERRED, False, True)
672
673    @TelephonyBaseTest.tel_test_wrap
674    @test_tracker_info(uuid="46262b2d-5de9-4984-87e8-42f44469289e")
675    def test_apm_wfc_cellular_preferred_to_wifi_preferred(self):
676        """Test for WiFi Calling settings:
677        APM + WiFi Connected + Cellular Preferred, change WFC to WiFi Preferred
678
679        Steps:
680        1. Setup DUT Idle, airplane mode.
681        2. Make sure DUT WiFi connected, WFC is set to Cellular Preferred.
682            Verify DUT WFC not available.
683        3. Change WFC setting to WiFi Preferred.
684        4. Verify DUT report WFC available.
685
686        Expected Results:
687        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
688        4. DUT WiFI Calling feature bit return True, network rat is iwlan.
689        """
690        if not toggle_airplane_mode_by_adb(self.log, self.ad, True):
691            self.log.error("Failed to turn on airplane mode")
692            return False
693        return self._wifi_connected_set_wfc_mode_change_wfc_mode(
694            WFC_MODE_CELLULAR_PREFERRED, WFC_MODE_WIFI_PREFERRED, True, True)
695
696    @TelephonyBaseTest.tel_test_wrap
697    @test_tracker_info(uuid="5b514f51-fed9-475e-99d3-17d2165e11a1")
698    def test_apm_wfc_wifi_preferred_turn_off_apm(self):
699        """Test for WiFi Calling settings:
700        APM + WiFi Connected + WiFi Preferred + turn off APM
701
702        Steps:
703        1. Setup DUT Idle in Airplane mode.
704        2. Make sure DUT WiFi connected, set WFC mode to WiFi preferred.
705        3. verify DUT WFC available, report iwlan rat.
706        4. Turn off airplane mode.
707        5. Verify DUT WFC still available, report iwlan rat
708
709        Expected Results:
710        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
711        5. DUT WiFI Calling feature bit return True, network rat is iwlan.
712        """
713        if not toggle_airplane_mode_by_adb(self.log, self.ad, True):
714            self.log.error("Failed to turn on airplane mode")
715            return False
716        return self._wifi_connected_set_wfc_mode_turn_off_apm(
717            WFC_MODE_WIFI_PREFERRED, True)
718
719    @TelephonyBaseTest.tel_test_wrap
720    @test_tracker_info(uuid="f328cff2-9dec-44b3-ba74-a662b76fcf2a")
721    def test_apm_wfc_cellular_preferred_turn_off_apm(self):
722        """Test for WiFi Calling settings:
723        APM + WiFi Connected + Cellular Preferred + turn off APM
724
725        Steps:
726        1. Setup DUT Idle in Airplane mode.
727        2. Make sure DUT WiFi connected, set WFC mode to Cellular preferred.
728        3. verify DUT WFC available, report iwlan rat.
729        4. Turn off airplane mode.
730        5. Verify DUT WFC not available, not report iwlan rat
731
732        Expected Results:
733        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
734        5. DUT WiFI Calling feature bit return False, network rat is not iwlan.
735        """
736        if not toggle_airplane_mode_by_adb(self.log, self.ad, True):
737            self.log.error("Failed to turn on airplane mode")
738            return False
739        return self._wifi_connected_set_wfc_mode_turn_off_apm(
740            WFC_MODE_CELLULAR_PREFERRED, False)
741
742    @TelephonyBaseTest.tel_test_wrap
743    @test_tracker_info(uuid="7e30d219-42ee-4309-a95c-2b45b8831d26")
744    def test_wfc_setup_timing(self):
745        """ Measures the time delay in enabling WiFi calling
746
747        Steps:
748        1. Make sure DUT idle.
749        2. Turn on Airplane Mode, Set WiFi Calling to WiFi_Preferred.
750        3. Turn on WiFi, connect to WiFi AP and measure time delay.
751        4. Wait for WiFi connected, verify Internet and measure time delay.
752        5. Wait for rat to be reported as iwlan and measure time delay.
753        6. Wait for ims registered and measure time delay.
754        7. Wait for WiFi Calling feature bit to be True and measure time delay.
755
756        Expected results:
757        Time Delay in each step should be within pre-defined limit.
758
759        Returns:
760            Currently always return True.
761        """
762        # TODO: b/26338119 Set pass/fail criteria
763        ad = self.android_devices[0]
764
765        time_values = {
766            'start': 0,
767            'wifi_enabled': 0,
768            'wifi_connected': 0,
769            'wifi_data': 0,
770            'iwlan_rat': 0,
771            'ims_registered': 0,
772            'wfc_enabled': 0,
773            'mo_call_success': 0
774        }
775
776        wifi_reset(self.log, ad)
777        toggle_airplane_mode_by_adb(self.log, ad, True)
778
779        set_wfc_mode(self.log, ad, WFC_MODE_WIFI_PREFERRED)
780
781        time_values['start'] = time.time()
782
783        self.log.info("Start Time {}s".format(time_values['start']))
784
785        wifi_toggle_state(self.log, ad, True)
786        time_values['wifi_enabled'] = time.time()
787        self.log.info("WiFi Enabled After {}s".format(
788            time_values['wifi_enabled'] - time_values['start']))
789
790        ensure_wifi_connected(self.log, ad, self.wifi_network_ssid,
791                              self.wifi_network_pass)
792        ad.droid.wakeUpNow()
793
794        if not wait_for_wifi_data_connection(self.log, ad, True,
795                                             MAX_WAIT_TIME_WIFI_CONNECTION):
796            self.log.error("Failed WiFi connection, aborting!")
797            return False
798        time_values['wifi_connected'] = time.time()
799
800        self.log.info("WiFi Connected After {}s".format(
801            time_values['wifi_connected'] - time_values['wifi_enabled']))
802
803        if not verify_http_connection(self.log, ad, 'http://www.google.com',
804                                      100, .1):
805            self.log.error("Failed to get user-plane traffic, aborting!")
806            return False
807
808        time_values['wifi_data'] = time.time()
809        self.log.info("WifiData After {}s".format(
810            time_values['wifi_data'] - time_values['wifi_connected']))
811
812        if not wait_for_network_rat(
813                self.log, ad, RAT_FAMILY_WLAN,
814                voice_or_data=NETWORK_SERVICE_DATA):
815            self.log.error("Failed to set-up iwlan, aborting!")
816            if is_droid_in_rat_family(self.log, ad, RAT_FAMILY_WLAN,
817                                      NETWORK_SERVICE_DATA):
818                self.log.error("Never received the event, but droid in iwlan")
819            else:
820                return False
821        time_values['iwlan_rat'] = time.time()
822        self.log.info("iWLAN Reported After {}s".format(
823            time_values['iwlan_rat'] - time_values['wifi_data']))
824
825        if not wait_for_ims_registered(self.log, ad,
826                                       MAX_WAIT_TIME_IMS_REGISTRATION):
827            self.log.error("Never received IMS registered, aborting")
828            return False
829        time_values['ims_registered'] = time.time()
830        self.log.info("Ims Registered After {}s".format(
831            time_values['ims_registered'] - time_values['iwlan_rat']))
832
833        if not wait_for_wfc_enabled(self.log, ad, MAX_WAIT_TIME_WFC_ENABLED):
834            self.log.error("Never received WFC feature, aborting")
835            return False
836
837        time_values['wfc_enabled'] = time.time()
838        self.log.info("Wifi Calling Feature Enabled After {}s".format(
839            time_values['wfc_enabled'] - time_values['ims_registered']))
840
841        set_wfc_mode(self.log, ad, WFC_MODE_DISABLED)
842
843        wait_for_not_network_rat(
844            self.log, ad, RAT_FAMILY_WLAN, voice_or_data=NETWORK_SERVICE_DATA)
845
846        self.log.info("\n\n------------------summary-----------------")
847        self.log.info("WiFi Enabled After {0:.2f} s".format(
848            time_values['wifi_enabled'] - time_values['start']))
849        self.log.info("WiFi Connected After {0:.2f} s".format(
850            time_values['wifi_connected'] - time_values['wifi_enabled']))
851        self.log.info("WifiData After {0:.2f} s".format(
852            time_values['wifi_data'] - time_values['wifi_connected']))
853        self.log.info("iWLAN Reported After {0:.2f} s".format(
854            time_values['iwlan_rat'] - time_values['wifi_data']))
855        self.log.info("Ims Registered After {0:.2f} s".format(
856            time_values['ims_registered'] - time_values['iwlan_rat']))
857        self.log.info("Wifi Calling Feature Enabled After {0:.2f} s".format(
858            time_values['wfc_enabled'] - time_values['ims_registered']))
859        self.log.info("\n\n")
860        return True
861
862    @TelephonyBaseTest.tel_test_wrap
863    @test_tracker_info(uuid="4e0bf35f-b4e1-44f8-b657-e9c71878d1f6")
864    def test_lte_volte_wfc_enabled_toggle_wifi_stress(self):
865        """Test for WiFi Calling settings:
866        LTE + VoLTE Enabled + WFC enabled, Toggling WiFi Stress test
867
868        Steps:
869        1. Setup DUT Idle, LTE network type, VoLTE enabled.
870        2. Make sure DUT WiFi disconnected, WFC enabled (WiFi Preferred).
871        3. DUT connect WiFi, verify DUT WFC available, report iwlan rat.
872        4. DUT disconnect WiFi, verify DUT WFC unavailable, not report iwlan rat.
873        5. Verify DUT report VoLTE available.
874        6. Repeat steps 3~5 for N times.
875
876        Expected Results:
877        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
878        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
879        5. DUT report VoLTE available.
880        """
881
882        if not phone_setup_volte(self.log, self.ad):
883            self.log.error("Failed to setup VoLTE")
884            return False
885        set_wfc_mode(self.log, self.ad, WFC_MODE_WIFI_PREFERRED)
886
887        for i in range(1, self.stress_test_number + 1):
888            self.log.info("Start Iteration {}.".format(i))
889            result = self._wifi_connected_enable_wfc_teardown_wfc(
890                tear_down_operation=self._TEAR_DOWN_OPERATION_DISCONNECT_WIFI,
891                initial_setup_wifi=True,
892                initial_setup_wfc_mode=None,
893                check_volte_after_wfc_disabled=True)
894            if not result:
895                self.log.error("Test Failed in iteration: {}.".format(i))
896                return False
897        return True
898
899    @TelephonyBaseTest.tel_test_wrap
900    @test_tracker_info(uuid="c6ef1dfd-29d4-4fc8-9fc0-27e35bb377fb")
901    def test_lte_volte_wfc_enabled_reset_wifi_stress(self):
902        """Test for WiFi Calling settings:
903        LTE + VoLTE Enabled + WFC enabled, Reset WiFi Stress test
904
905        Steps:
906        1. Setup DUT Idle, LTE network type, VoLTE enabled.
907        2. Make sure DUT WiFi disconnected, WFC enabled (WiFi Preferred).
908        3. DUT connect WiFi, verify DUT WFC available, report iwlan rat.
909        4. DUT Reset WiFi, verify DUT WFC unavailable, not report iwlan rat.
910        5. Verify DUT report VoLTE available.
911        6. Repeat steps 3~5 for N times.
912
913        Expected Results:
914        3. DUT WiFi Calling feature bit return True, network rat is iwlan.
915        4. DUT WiFi Calling feature bit return False, network rat is not iwlan.
916        5. DUT report VoLTE available.
917        """
918
919        if not phone_setup_volte(self.log, self.ad):
920            self.log.error("Failed to setup VoLTE")
921            return False
922        set_wfc_mode(self.log, self.ad, WFC_MODE_WIFI_PREFERRED)
923
924        for i in range(1, self.stress_test_number + 1):
925            self.log.info("Start Iteration {}.".format(i))
926            result = self._wifi_connected_enable_wfc_teardown_wfc(
927                tear_down_operation=self._TEAR_DOWN_OPERATION_RESET_WIFI,
928                initial_setup_wifi=True,
929                initial_setup_wfc_mode=None,
930                check_volte_after_wfc_disabled=True)
931            if not result:
932                self.log.error("Test Failed in iteration: {}.".format(i))
933                return False
934        return True
935
936    @TelephonyBaseTest.tel_test_wrap
937    @test_tracker_info(uuid="a7d2b9fc-d676-4ada-84b9-08e99659da78")
938    def test_lte_volte_wfc_wifi_preferred_to_cellular_preferred_stress(self):
939        """Test for WiFi Calling settings:
940        LTE + VoLTE Enabled + WiFi Connected + WiFi Preferred,
941        change WFC to Cellular Preferred stress
942
943        Steps:
944        1. Setup DUT Idle, LTE network type, VoLTE enabled.
945        2. Make sure DUT WiFi connected, WFC is set to WiFi Preferred.
946            Verify DUT WFC available, report iwlan rat.
947        3. Change WFC setting to Cellular Preferred.
948        4. Verify DUT report WFC not available.
949        5. Verify DUT report VoLTE available.
950        6. Repeat steps 3~5 for N times.
951
952        Expected Results:
953        2. DUT WiFi Calling feature bit return True, network rat is iwlan.
954        4. DUT WiFI Calling feature bit return False, network rat is not iwlan.
955        5. DUT report VoLTE available.
956        """
957        if not phone_setup_volte(self.log, self.ad):
958            self.log.error("Failed to setup VoLTE.")
959            return False
960        if not ensure_wifi_connected(self.log, self.ad, self.wifi_network_ssid,
961                                     self.wifi_network_pass):
962            self.log.error("Failed to connect WiFi")
963            return False
964
965        for i in range(1, self.stress_test_number + 1):
966            self.log.info("Start Iteration {}.".format(i))
967            result = self._wifi_connected_set_wfc_mode_change_wfc_mode(
968                WFC_MODE_WIFI_PREFERRED,
969                WFC_MODE_CELLULAR_PREFERRED,
970                True,
971                False,
972                initial_setup_wifi=False,
973                check_volte_after_wfc_disabled=True)
974            if not result:
975                self.log.error("Test Failed in iteration: {}.".format(i))
976                return False
977        return True
978
979    def verify_volte_on_wfc_off(self):
980        result = True
981        if self.ad.droid.imsGetWfcMode() != WFC_MODE_DISABLED:
982            self.ad.log.error(
983                "WFC mode is not disabled after IMS factory reset")
984            result = False
985        else:
986            self.ad.log.info("WFC mode is disabled as expected")
987        if not self.ad.droid.imsIsEnhanced4gLteModeSettingEnabledByUser():
988            self.ad.log.error("VoLTE mode is not on")
989            result = False
990        else:
991            self.ad.log.info("VoLTE mode is turned on as expected")
992        if not phone_idle_volte(self.log, self.ad):
993            self.ad.log.error("Voice RAT is not in LTE")
994            result = False
995        if not call_setup_teardown(self.log, self.ad, self.android_devices[1],
996                                   self.ad, is_phone_in_call_volte):
997            self.ad.log.error("Voice call in VoLTE failed")
998            result = False
999        return result
1000
1001    def verify_volte_off_wfc_off(self):
1002        result = True
1003        if self.ad.droid.imsGetWfcMode() != WFC_MODE_DISABLED:
1004            self.ad.log.error(
1005                "WFC mode is not disabled after IMS factory reset")
1006            result = False
1007        else:
1008            self.ad.log.info("WFC mode is disabled as expected")
1009        if self.ad.droid.imsIsEnhanced4gLteModeSettingEnabledByUser():
1010            self.ad.log.error("VoLTE mode is on")
1011            result = False
1012        else:
1013            self.ad.log.info("VoLTE mode is turned off as expected")
1014        if not call_setup_teardown(self.log, self.ad, self.android_devices[1],
1015                                   self.ad, None):
1016            self.ad.log.error("Voice call failed")
1017            result = False
1018        return result
1019
1020    def revert_default_telephony_setting(self):
1021        toggle_airplane_mode_by_adb(self.log, self.ad, True)
1022        default_data_roaming = int(
1023            self.ad.adb.getprop("ro.com.android.dataroaming") == 'true')
1024        default_network_preference = int(
1025            self.ad.adb.getprop("ro.telephony.default_network"))
1026        self.ad.log.info("Default data roaming %s, network preference %s",
1027                         default_data_roaming, default_network_preference)
1028        new_data_roaming = abs(default_data_roaming - 1)
1029        new_network_preference = abs(default_network_preference - 1)
1030        self.ad.log.info(
1031            "Set data roaming = %s, mobile data = 0, network preference = %s",
1032            new_data_roaming, new_network_preference)
1033        self.ad.adb.shell("settings put global mobile_data 0")
1034        self.ad.adb.shell(
1035            "settings put global data_roaming %s" % new_data_roaming)
1036        self.ad.adb.shell("settings put global preferred_network_mode %s" %
1037                          new_network_preference)
1038
1039    def verify_default_telephony_setting(self):
1040        default_data_roaming = int(
1041            self.ad.adb.getprop("ro.com.android.dataroaming") == 'true')
1042        default_network_preference = int(
1043            self.ad.adb.getprop("ro.telephony.default_network"))
1044        self.ad.log.info("Default data roaming %s, network preference %s",
1045                         default_data_roaming, default_network_preference)
1046        data_roaming = int(
1047            self.ad.adb.shell("settings get global data_roaming"))
1048        mobile_data = int(self.ad.adb.shell("settings get global mobile_data"))
1049        network_preference = int(
1050            self.ad.adb.shell("settings get global preferred_network_mode"))
1051        airplane_mode = int(
1052            self.ad.adb.shell("settings get global airplane_mode_on"))
1053        result = True
1054        self.ad.log.info("data_roaming = %s, mobile_data = %s, "
1055                         "network_perference = %s, airplane_mode = %s",
1056                         data_roaming, mobile_data, network_preference,
1057                         airplane_mode)
1058        if airplane_mode:
1059            self.ad.log.error("Airplane mode is on")
1060            result = False
1061        if data_roaming != default_data_roaming:
1062            self.ad.log.error("Data roaming is %s, expecting %s", data_roaming,
1063                              default_data_roaming)
1064            result = False
1065        if not mobile_data:
1066            self.ad.log.error("Mobile data is off")
1067            result = False
1068        if network_preference != default_network_preference:
1069            self.ad.log.error("preferred_network_mode is %s, expecting %s",
1070                              network_preference, default_network_preference)
1071            result = False
1072        return result
1073
1074    @test_tracker_info(uuid="135301ea-6d00-4233-98fd-cda706d61eb2")
1075    @TelephonyBaseTest.tel_test_wrap
1076    def test_ims_factory_reset_to_volte_on_wfc_off(self):
1077        """Test VOLTE is enabled WFC is disabled after ims factory reset.
1078
1079        Steps:
1080        1. Setup VoLTE, WFC, APM is various mode.
1081        2. Call IMS factory reset.
1082        3. Verify VoLTE is on, WFC is off after IMS factory reset.
1083        4. Verify VoLTE Voice call can be made successful.
1084
1085        Expected Results: VoLTE is on, WFC is off after IMS factory reset.
1086        """
1087        result = True
1088        for airplane_mode in (True, False):
1089            for volte_mode in (True, False):
1090                for wfc_mode in (WFC_MODE_DISABLED,
1091                                 WFC_MODE_CELLULAR_PREFERRED,
1092                                 WFC_MODE_WIFI_PREFERRED):
1093                    self.ad.log.info("Set VoLTE %s, WFC %s, APM %s",
1094                                     volte_mode, wfc_mode, airplane_mode)
1095                    toggle_airplane_mode_by_adb(self.log, self.ad,
1096                                                airplane_mode)
1097                    toggle_volte(self.log, self.ad, volte_mode)
1098                    set_wfc_mode(self.log, self.ad, wfc_mode)
1099                    self.ad.log.info("Call IMS factory reset")
1100                    self.ad.droid.imsFactoryReset()
1101                    self.ad.log.info("Ensure airplane mode is off")
1102                    toggle_airplane_mode_by_adb(self.log, self.ad, False)
1103                    if not self.verify_volte_on_wfc_off(): result = False
1104        return result
1105
1106    @test_tracker_info(uuid="5318bf7a-4210-4b49-b361-9539d28f3e38")
1107    @TelephonyBaseTest.tel_test_wrap
1108    def test_ims_factory_reset_to_volte_off_wfc_off(self):
1109        """Test VOLTE is enabled WFC is disabled after ims factory reset.
1110
1111        Steps:
1112        1. Setup VoLTE, WFC, APM is various mode.
1113        2. Call IMS factory reset.
1114        3. Verify VoLTE is on, WFC is off after IMS factory reset.
1115        4. Verify VoLTE Voice call can be made successful.
1116
1117        Expected Results: VoLTE is on, WFC is off after IMS factory reset.
1118        """
1119        result = True
1120        for airplane_mode in (True, False):
1121            for volte_mode in (True, False):
1122                for wfc_mode in (WFC_MODE_DISABLED,
1123                                 WFC_MODE_CELLULAR_PREFERRED,
1124                                 WFC_MODE_WIFI_PREFERRED):
1125                    self.ad.log.info("Set VoLTE %s, WFC %s, APM %s",
1126                                     volte_mode, wfc_mode, airplane_mode)
1127                    toggle_airplane_mode_by_adb(self.log, self.ad,
1128                                                airplane_mode)
1129                    toggle_volte(self.log, self.ad, volte_mode)
1130                    set_wfc_mode(self.log, self.ad, wfc_mode)
1131                    self.ad.log.info("Call IMS factory reset")
1132                    self.ad.droid.imsFactoryReset()
1133                    self.ad.log.info("Ensure airplane mode is off")
1134                    toggle_airplane_mode_by_adb(self.log, self.ad, False)
1135                    if not self.verify_volte_off_wfc_off(): result = False
1136        return result
1137
1138    @test_tracker_info(uuid="c6149bd6-7080-453d-af37-1f9bd350a764")
1139    @TelephonyBaseTest.tel_test_wrap
1140    def test_telephony_factory_reset(self):
1141        """Test VOLTE is enabled WFC is disabled after telephony factory reset.
1142
1143        Steps:
1144        1. Setup DUT with various dataroaming, mobiledata, and default_network.
1145        2. Call telephony factory reset.
1146        3. Verify DUT back to factory default.
1147
1148        Expected Results: dataroaming is off, mobiledata is on, network
1149                          preference is back to default.
1150        """
1151        self.ad.log.info("Call telephony factory reset")
1152        self.revert_default_telephony_setting()
1153        self.ad.droid.telephonyFactoryReset()
1154        return self.verify_default_telephony_setting()
1155
1156    @test_tracker_info(uuid="ce60740f-4d8e-4013-a7cf-65589e8a0893")
1157    @TelephonyBaseTest.tel_test_wrap
1158    def test_factory_reset_by_wipe_to_volte_on_wfc_off(self):
1159        """Verify the network setting after factory reset by wipe.
1160
1161        Steps:
1162        1. Config VoLTE off, WFC on, APM on, data_roaming on, mobile_data on
1163           preferred_network_mode.
1164        2. Factory reset by .
1165        3. Verify VoLTE is on, WFC is off after IMS factory reset.
1166        4. Verify VoLTE Voice call can be made successful.
1167
1168        Expected Results: VoLTE is on, WFC is off after IMS factory reset.
1169        """
1170        self.ad.log.info("Set VoLTE off, WFC wifi preferred, APM on")
1171        toggle_airplane_mode_by_adb(self.log, self.ad, True)
1172        toggle_volte(self.log, self.ad, False)
1173        set_wfc_mode(self.log, self.ad, WFC_MODE_WIFI_PREFERRED)
1174        self.revert_default_telephony_setting()
1175        self.ad.log.info("Wipe in fastboot")
1176        fastboot_wipe(self.ad)
1177        result = self.verify_volte_on_wfc_off()
1178        if not self.verify_default_telephony_setting(): result = False
1179        return result
1180
1181    @test_tracker_info(uuid="44e9291e-949b-4db1-a209-c6d41552ec27")
1182    @TelephonyBaseTest.tel_test_wrap
1183    def test_factory_reset_by_wipe_to_volte_off_wfc_off(self):
1184        """Verify the network setting after factory reset by wipe.
1185
1186        Steps:
1187        1. Config VoLTE on, WFC on, APM on, data_roaming on, mobile_data on
1188           preferred_network_mode.
1189        2. Factory reset by .
1190        3. Verify VoLTE is on, WFC is off after IMS factory reset.
1191        4. Verify VoLTE Voice call can be made successful.
1192
1193        Expected Results: VoLTE is on, WFC is off after IMS factory reset.
1194        """
1195        self.ad.log.info("Set VoLTE on, WFC wifi preferred, APM on")
1196        toggle_airplane_mode_by_adb(self.log, self.ad, True)
1197        toggle_volte(self.log, self.ad, True)
1198        set_wfc_mode(self.log, self.ad, WFC_MODE_WIFI_PREFERRED)
1199        self.revert_default_telephony_setting()
1200        self.ad.log.info("Wipe in fastboot")
1201        fastboot_wipe(self.ad)
1202        result = self.verify_volte_off_wfc_off()
1203        if not self.verify_default_telephony_setting(): result = False
1204        return result
1205
1206    @test_tracker_info(uuid="64deba57-c1c2-422f-b771-639c95edfbc0")
1207    @TelephonyBaseTest.tel_test_wrap
1208    def test_disable_mobile_data_always_on(self):
1209        """Verify mobile_data_always_on can be disabled.
1210
1211        Steps:
1212        1. Disable mobile_data_always_on by adb.
1213        2. Verify the mobile data_always_on state.
1214
1215        Expected Results: mobile_data_always_on return 0.
1216        """
1217        self.ad.log.info("Disable mobile_data_always_on")
1218        set_mobile_data_always_on(self.ad, False)
1219        time.sleep(1)
1220        return self.ad.adb.shell(
1221            "settings get global mobile_data_always_on") == "0"
1222
1223    @test_tracker_info(uuid="56ddcd5a-92b0-46c7-9c2b-d743794efb7c")
1224    @TelephonyBaseTest.tel_test_wrap
1225    def test_enable_mobile_data_always_on(self):
1226        """Verify mobile_data_always_on can be enabled.
1227
1228        Steps:
1229        1. Enable mobile_data_always_on by adb.
1230        2. Verify the mobile data_always_on state.
1231
1232        Expected Results: mobile_data_always_on return 1.
1233        """
1234        self.ad.log.info("Enable mobile_data_always_on")
1235        set_mobile_data_always_on(self.ad, True)
1236        time.sleep(1)
1237        return "1" in self.ad.adb.shell(
1238            "settings get global mobile_data_always_on")
1239
1240    @test_tracker_info(uuid="c2cc5b66-40af-4ba6-81cb-6c44ae34cbbb")
1241    @TelephonyBaseTest.tel_test_wrap
1242    def test_push_new_radio_or_mbn(self):
1243        """Verify new mdn and radio can be push to device.
1244
1245        Steps:
1246        1. If new radio path is given, flash new radio on the device.
1247        2. Verify the radio version.
1248        3. If new mbn path is given, push new mbn to device.
1249        4. Verify the installed mbn version.
1250
1251        Expected Results:
1252        radio and mbn can be pushed to device and mbn.ver is available.
1253        """
1254        result = True
1255        paths = {}
1256        for path_key, dst_name in zip(["radio_image", "mbn_path"],
1257                                      ["radio.img", "mcfg_sw"]):
1258            path = self.user_params.get(path_key)
1259            if not path:
1260                continue
1261            elif isinstance(path, list):
1262                if not path[0]:
1263                    continue
1264                path = path[0]
1265            if "dev/null" in path:
1266                continue
1267            if not os.path.exists(path):
1268                self.log.error("path %s does not exist", path)
1269                self.log.info(self.user_params)
1270                path = os.path.join(self.user_params[Config.key_config_path],
1271                                    path)
1272                if not os.path.exists(path):
1273                    self.log.error("path %s does not exist", path)
1274                    continue
1275
1276            self.log.info("%s path = %s", path_key, path)
1277            if "zip" in path:
1278                self.log.info("Unzip %s", path)
1279                file_path, file_name = os.path.split(path)
1280                dest_path = os.path.join(file_path, dst_name)
1281                os.system("rm -rf %s" % dest_path)
1282                unzip_maintain_permissions(path, file_path)
1283                path = dest_path
1284            os.system("chmod -R 777 %s" % path)
1285            paths[path_key] = path
1286        if not paths:
1287            self.log.info("No radio_path or mbn_path is provided")
1288            raise signals.TestSkip("No radio_path or mbn_path is provided")
1289        self.log.info("paths = %s", paths)
1290        for ad in self.android_devices:
1291            if paths.get("radio_image"):
1292                print_radio_info(ad, "Before flash radio, ")
1293                flash_radio(ad, paths["radio_image"])
1294                print_radio_info(ad, "After flash radio, ")
1295            if not paths.get("mbn_path") or "mbn" not in ad.adb.shell(
1296                    "ls /vendor"):
1297                ad.log.info("No need to push mbn files")
1298                continue
1299            push_result = True
1300            try:
1301                mbn_ver = ad.adb.shell(
1302                    "cat /vendor/mbn/mcfg/configs/mcfg_sw/mbn.ver")
1303                if mbn_ver:
1304                    ad.log.info("Before push mbn, mbn.ver = %s", mbn_ver)
1305                else:
1306                    ad.log.info(
1307                        "There is no mbn.ver before push, unmatching device")
1308                    continue
1309            except:
1310                ad.log.info(
1311                    "There is no mbn.ver before push, unmatching device")
1312                continue
1313            print_radio_info(ad, "Before push mbn, ")
1314            for i in range(2):
1315                if not system_file_push(ad, paths["mbn_path"],
1316                                        "/vendor/mbn/mcfg/configs/"):
1317                    if i == 1:
1318                        ad.log.error("Failed to push mbn file")
1319                        push_result = False
1320                else:
1321                    ad.log.info("The mbn file is pushed to device")
1322                    break
1323            if not push_result:
1324                result = False
1325                continue
1326            print_radio_info(ad, "After push mbn, ")
1327            try:
1328                new_mbn_ver = ad.adb.shell(
1329                    "cat /vendor/mbn/mcfg/configs/mcfg_sw/mbn.ver")
1330                if new_mbn_ver:
1331                    ad.log.info("new mcfg_sw mbn.ver = %s", new_mbn_ver)
1332                    if new_mbn_ver == mbn_ver:
1333                        ad.log.error(
1334                            "mbn.ver is the same before and after push")
1335                        result = False
1336                else:
1337                    ad.log.error("Unable to get new mbn.ver")
1338                    result = False
1339            except Exception as e:
1340                ad.log.error("cat mbn.ver with error %s", e)
1341                result = False
1342        return result
1343
1344    @TelephonyBaseTest.tel_test_wrap
1345    def test_set_qxdm_log_mask_ims(self):
1346        """Set the QXDM Log mask to IMS_DS_CNE_LnX_Golden.cfg"""
1347        tasks = [(set_qxdm_logger_command, [ad, "IMS_DS_CNE_LnX_Golden.cfg"])
1348                 for ad in self.android_devices]
1349        return multithread_func(self.log, tasks)
1350
1351    @TelephonyBaseTest.tel_test_wrap
1352    def test_set_qxdm_log_mask_qc_default(self):
1353        """Set the QXDM Log mask to QC_Default.cfg"""
1354        tasks = [(set_qxdm_logger_command, [ad, " QC_Default.cfg"])
1355                 for ad in self.android_devices]
1356        return multithread_func(self.log, tasks)
1357
1358    @test_tracker_info(uuid="e2734d66-6111-4e76-aa7b-d3b4cbcde4f1")
1359    @TelephonyBaseTest.tel_test_wrap
1360    def test_check_carrier_id(self):
1361        """Verify mobile_data_always_on can be enabled.
1362
1363        Steps:
1364        1. Enable mobile_data_always_on by adb.
1365        2. Verify the mobile data_always_on state.
1366
1367        Expected Results: mobile_data_always_on return 1.
1368        """
1369        result = True
1370        if self.ad.adb.getprop("ro.build.version.release")[0] in ("8", "O",
1371                                                                  "7", "N"):
1372            raise signals.TestSkip("Not supported in this build")
1373        old_carrier_id = self.ad.droid.telephonyGetSubscriptionCarrierId()
1374        old_carrier_name = self.ad.droid.telephonyGetSubscriptionCarrierName()
1375        self.result_detail = "carrier_id = %s, carrier_name = %s" % (
1376            old_carrier_id, old_carrier_name)
1377        self.ad.log.info(self.result_detail)
1378        sub_id = get_outgoing_voice_sub_id(self.ad)
1379        slot_index = get_slot_index_from_subid(self.log, self.ad, sub_id)
1380        if power_off_sim(self.ad, slot_index):
1381            for i in range(3):
1382                carrier_id = self.ad.droid.telephonyGetSubscriptionCarrierId()
1383                carrier_name = self.ad.droid.telephonyGetSubscriptionCarrierName(
1384                )
1385                msg = "After SIM power down, carrier_id = %s(expecting -1), " \
1386                      "carrier_name = %s(expecting None)" % (carrier_id, carrier_name)
1387                if carrier_id != -1 or carrier_name:
1388                    if i == 2:
1389                        self.ad.log.error(msg)
1390                        self.result_detail = "%s %s" % (self.result_detail,
1391                                                        msg)
1392                        result = False
1393                    else:
1394                        time.sleep(5)
1395                else:
1396                    self.ad.log.info(msg)
1397                    break
1398        else:
1399            if self.ad.model in ("taimen", "walleye"):
1400                msg = "Power off SIM slot is not working"
1401                self.ad.log.error(msg)
1402                result = False
1403            else:
1404                msg = "Power off SIM slot is not supported"
1405                self.ad.log.warning(msg)
1406            self.result_detail = "%s %s" % (self.result_detail, msg)
1407
1408        if not power_on_sim(self.ad, slot_index):
1409            self.ad.log.error("Fail to power up SIM")
1410            result = False
1411            setattr(self.ad, "reboot_to_recover", True)
1412        else:
1413            if is_sim_locked(self.ad):
1414                self.ad.log.info("Sim is locked")
1415                carrier_id = self.ad.droid.telephonyGetSubscriptionCarrierId()
1416                carrier_name = self.ad.droid.telephonyGetSubscriptionCarrierName(
1417                )
1418                msg = "In locked SIM, carrier_id = %s(expecting -1), " \
1419                      "carrier_name = %s(expecting None)" % (carrier_id, carrier_name)
1420                if carrier_id != -1 or carrier_name:
1421                    self.ad.log.error(msg)
1422                    self.result_detail = "%s %s" % (self.result_detail, msg)
1423                    result = False
1424                else:
1425                    self.ad.log.info(msg)
1426                unlock_sim(self.ad)
1427            elif getattr(self.ad, "is_sim_locked", False):
1428                self.ad.log.error(
1429                    "After SIM slot power cycle, SIM in not in locked state")
1430                return False
1431
1432            if not ensure_phone_subscription(self.log, self.ad):
1433                self.ad.log.error("Unable to find a valid subscription!")
1434                result = False
1435            new_carrier_id = self.ad.droid.telephonyGetSubscriptionCarrierId()
1436            new_carrier_name = self.ad.droid.telephonyGetSubscriptionCarrierName(
1437            )
1438            msg = "After SIM power up, new_carrier_id = %s, " \
1439                  "new_carrier_name = %s" % (new_carrier_id, new_carrier_name)
1440            if old_carrier_id != new_carrier_id or (old_carrier_name !=
1441                                                    new_carrier_name):
1442                self.ad.log.error(msg)
1443                self.result_detail = "%s %s" % (self.result_detail, msg)
1444                result = False
1445            else:
1446                self.ad.log.info(msg)
1447        return result
1448
1449    @TelephonyBaseTest.tel_test_wrap
1450    def test_modem_power_anomaly_file_existence(self):
1451        """Verify if the power anomaly file exists
1452
1453        1. Collect Bugreport
1454        2. unzip bugreport
1455        3. remane the .bin file to .tar
1456        4. unzip dumpstate.tar
1457        5. Verify if the file exists
1458
1459        """
1460        ad = self.android_devices[0]
1461        begin_time = get_current_epoch_time()
1462        for i in range(3):
1463            try:
1464                bugreport_path = os.path.join(ad.log_path, self.test_name)
1465                create_dir(bugreport_path)
1466                ad.take_bug_report(self.test_name, begin_time)
1467                break
1468            except Exception as e:
1469                ad.log.error("bugreport attempt %s error: %s", i + 1, e)
1470        ad.log.info("Bugreport Path is %s" % bugreport_path)
1471        try:
1472            list_of_files = os.listdir(bugreport_path)
1473            ad.log.info(list_of_files)
1474            for filename in list_of_files:
1475                if ".zip" in filename:
1476                    ad.log.info(filename)
1477                    file_path = os.path.join(bugreport_path, filename)
1478                    ad.log.info(file_path)
1479                    unzip_maintain_permissions(file_path, bugreport_path)
1480            dumpstate_path = os.path.join(bugreport_path,
1481                                          "dumpstate_board.bin")
1482            if os.path.isfile(dumpstate_path):
1483                os.rename(dumpstate_path,
1484                          bugreport_path + "/dumpstate_board.tar")
1485                os.chmod(bugreport_path + "/dumpstate_board.tar", 0o777)
1486                current_dir = os.getcwd()
1487                os.chdir(bugreport_path)
1488                exe_cmd("tar -xvf %s" %
1489                        (bugreport_path + "/dumpstate_board.tar"))
1490                os.chdir(current_dir)
1491                if os.path.isfile(bugreport_path + "/power_anomaly_data.txt"):
1492                    ad.log.info("Modem Power Anomaly File Exists!!")
1493                    return True
1494            return False
1495        except Exception as e:
1496            ad.log.error(e)
1497            return False
1498