Add imsFactoryReset in ensure_phone_default_state.

Make check_subscription optional in ensure_phone_default_state.

Shorten the waiting time before pick up the phone call.

Test: run test in lab
Bug: None
Change-Id: I09085e44ab82e20c23eee3553b83bd5434080734
diff --git a/acts/framework/acts/test_utils/tel/tel_test_utils.py b/acts/framework/acts/test_utils/tel/tel_test_utils.py
index 62967d7..af019e1 100644
--- a/acts/framework/acts/test_utils/tel/tel_test_utils.py
+++ b/acts/framework/acts/test_utils/tel/tel_test_utils.py
@@ -816,7 +816,7 @@
         ad.log.info("Accept the ring call")
         ad.droid.telecomAcceptRingingCall()
 
-        if wait_for_call_offhook_event(
+        if ad.droid.telecomIsInCall() or wait_for_call_offhook_event(
                 log, ad, sub_id, event_tracking_started=True,
                 timeout=timeout) or ad.droid.telecomIsInCall():
             ad.log.info("Call answered successfully.")
@@ -1914,6 +1914,9 @@
         False: DATA_STATE_DISCONNECTED
     }[state]
 
+    data_state = ad.droid.telephonyGetDataConnectionState()
+    if not state and ad.droid.telephonyGetDataConnectionState() == state_str:
+        return True
     ad.ed.clear_all_events()
     ad.droid.telephonyStartTrackingDataConnectionStateChangeForSubscription(
         sub_id)
@@ -3597,7 +3600,7 @@
     return True
 
 
-def ensure_phone_default_state(log, ad):
+def ensure_phone_default_state(log, ad, check_subscription=True):
     """Ensure ad in default state.
     Phone not in call.
     Phone have no stored WiFi network and WiFi disconnected.
@@ -3607,6 +3610,7 @@
 
     try:
         ad.droid.telephonyFactoryReset()
+        ad.droid.imsFactoryReset()
         if ad.droid.telecomIsInCall():
             ad.droid.telecomEndCall()
         if not wait_for_droid_not_in_call(log, ad):
@@ -3615,11 +3619,11 @@
         ad.log.error("Failure %s, toggle APM instead", e)
         toggle_airplane_mode(log, ad, True, False)
         ad.droid.telephonyToggleDataConnection(True)
+        set_wfc_mode(log, ad, WFC_MODE_DISABLED)
 
     if not toggle_airplane_mode(log, ad, False, False):
         ad.log.error("Fail to turn off airplane mode")
         result = False
-    set_wfc_mode(log, ad, WFC_MODE_DISABLED)
     set_wifi_to_default(log, ad)
 
     if not wait_for_not_network_rat(
@@ -3630,14 +3634,14 @@
     if getattr(ad, 'data_roaming', False):
         ad.log.info("Enable cell data roaming")
         toggle_cell_data_roaming(ad, True)
-    if not ensure_phone_subscription(log, ad):
+    if check_subscription and not ensure_phone_subscription(log, ad):
         ad.log.error("Unable to find a valid subscription!")
         result = False
 
     return result
 
 
-def ensure_phones_default_state(log, ads):
+def ensure_phones_default_state(log, ads, check_subscription=True):
     """Ensure ads in default state.
     Phone not in call.
     Phone have no stored WiFi network and WiFi disconnected.
@@ -3649,7 +3653,8 @@
     """
     tasks = []
     for ad in ads:
-        tasks.append((ensure_phone_default_state, (log, ad)))
+        tasks.append((ensure_phone_default_state, (log, ad,
+                                                   check_subscription)))
     if not multithread_func(log, tasks):
         log.error("Ensure_phones_default_state Fail.")
         return False
@@ -3707,7 +3712,7 @@
             ad.droid.wifiConnectByConfig(network)
             time.sleep(20)
             if check_is_wifi_connected(log, ad, wifi_ssid):
-                ad.log.info("Coneected to Wifi %s", wifi_ssid)
+                ad.log.info("Connected to Wifi %s", wifi_ssid)
                 return True
     ad.log.info("Fail to connected to wifi %s", wifi_ssid)
     return False
diff --git a/acts/framework/acts/test_utils/tel/tel_voice_utils.py b/acts/framework/acts/test_utils/tel/tel_voice_utils.py
index 60762c7..4997873 100644
--- a/acts/framework/acts/test_utils/tel/tel_voice_utils.py
+++ b/acts/framework/acts/test_utils/tel/tel_voice_utils.py
@@ -170,10 +170,12 @@
     """
     ads = [phone_a, phone_b]
 
-    call_params = [(ads[0], ads[1], ads[0], phone_a_in_call_check_func,
-                    phone_b_in_call_check_func),
-                   (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
-                    phone_b_in_call_check_func), ]
+    call_params = [
+        (ads[0], ads[1], ads[0], phone_a_in_call_check_func,
+         phone_b_in_call_check_func),
+        (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
+         phone_b_in_call_check_func),
+    ]
 
     for param in call_params:
         # Make sure phones are idle.
@@ -239,14 +241,16 @@
     """
     ads = [phone_a, phone_b]
 
-    call_params = [(ads[0], ads[1], ads[0], phone_a_in_call_check_func,
-                    phone_b_in_call_check_func),
-                   (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
-                    phone_b_in_call_check_func),
-                   (ads[1], ads[0], ads[0], phone_b_in_call_check_func,
-                    phone_a_in_call_check_func),
-                   (ads[1], ads[0], ads[1], phone_b_in_call_check_func,
-                    phone_a_in_call_check_func), ]
+    call_params = [
+        (ads[0], ads[1], ads[0], phone_a_in_call_check_func,
+         phone_b_in_call_check_func),
+        (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
+         phone_b_in_call_check_func),
+        (ads[1], ads[0], ads[0], phone_b_in_call_check_func,
+         phone_a_in_call_check_func),
+        (ads[1], ads[0], ads[1], phone_b_in_call_check_func,
+         phone_a_in_call_check_func),
+    ]
 
     for param in call_params:
         # Make sure phones are idle.
@@ -297,9 +301,10 @@
     Returns:
         True if success. False if fail.
     """
-    return phone_setup_iwlan_for_subscription(
-        log, ad, get_outgoing_voice_sub_id(ad), is_airplane_mode, wfc_mode,
-        wifi_ssid, wifi_pwd)
+    return phone_setup_iwlan_for_subscription(log, ad,
+                                              get_outgoing_voice_sub_id(ad),
+                                              is_airplane_mode, wfc_mode,
+                                              wifi_ssid, wifi_pwd)
 
 
 def phone_setup_iwlan_for_subscription(log,
@@ -387,13 +392,17 @@
         True if success. False if fail.
     """
     toggle_airplane_mode(log, ad, False, strict_checking=False)
-    toggle_volte(log, ad, True)
-    if not wait_for_network_generation(
-            log, ad, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA):
-        if not ensure_network_generation(
+    try:
+        toggle_volte(log, ad, True)
+        if not wait_for_network_generation(
                 log, ad, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA):
-            ad.log.error("Fail to ensure data in 4G")
-            return False
+            if not ensure_network_generation(
+                    log, ad, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA):
+                ad.log.error("Fail to ensure data in 4G")
+                return False
+    except Exception as e:
+        ad.log.error(e)
+        ad.droid.telephonyToggleDataConnection(True)
     if not set_wfc_mode(log, ad, WFC_MODE_CELLULAR_PREFERRED):
         ad.log.error("Set WFC mode failed.")
         return False
@@ -875,6 +884,7 @@
         return False
     return True
 
+
 def phone_idle_not_iwlan(log, ad):
     """Return if phone is idle for non WiFi calling call test.
 
@@ -882,7 +892,8 @@
         ad: Android device object.
     """
     return phone_idle_not_iwlan_for_subscription(log, ad,
-                                             get_outgoing_voice_sub_id(ad))
+                                                 get_outgoing_voice_sub_id(ad))
+
 
 def phone_idle_not_iwlan_for_subscription(log, ad, sub_id):
     """Return if phone is idle for non WiFi calling call test for sub id.
@@ -892,15 +903,13 @@
         sub_id: subscription id.
     """
     if not wait_for_not_network_rat_for_subscription(
-            log,
-            ad,
-            sub_id,
-            RAT_FAMILY_WLAN,
+            log, ad, sub_id, RAT_FAMILY_WLAN,
             voice_or_data=NETWORK_SERVICE_DATA):
         log.error("{} data rat in iwlan mode.".format(ad.serial))
         return False
     return True
 
+
 def phone_idle_csfb(log, ad):
     """Return if phone is idle for CSFB call test.