• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17import time
18from acts.test_utils.tel.tel_defines import CALL_PROPERTY_HIGH_DEF_AUDIO
19from acts.test_utils.tel.tel_defines import CALL_STATE_ACTIVE
20from acts.test_utils.tel.tel_defines import CALL_STATE_HOLDING
21from acts.test_utils.tel.tel_defines import GEN_2G
22from acts.test_utils.tel.tel_defines import GEN_3G
23from acts.test_utils.tel.tel_defines import GEN_4G
24from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
25from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_VOLTE_ENABLED
26from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_ENABLED
27from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
28from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE
29from acts.test_utils.tel.tel_defines import RAT_FAMILY_CDMA2000
30from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
31from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM
32from acts.test_utils.tel.tel_defines import RAT_FAMILY_WCDMA
33from acts.test_utils.tel.tel_defines import RAT_FAMILY_WLAN
34from acts.test_utils.tel.tel_defines import RAT_1XRTT
35from acts.test_utils.tel.tel_defines import RAT_IWLAN
36from acts.test_utils.tel.tel_defines import RAT_LTE
37from acts.test_utils.tel.tel_defines import RAT_UMTS
38from acts.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_REG_AND_CALL
39from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL
40from acts.test_utils.tel.tel_defines import WAIT_TIME_LEAVE_VOICE_MAIL
41from acts.test_utils.tel.tel_defines import WFC_MODE_DISABLED
42from acts.test_utils.tel.tel_defines import WFC_MODE_CELLULAR_PREFERRED
43from acts.test_utils.tel.tel_defines import NETWORK_MODE_CDMA
44from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_ONLY
45from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_UMTS
46from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_CDMA_EVDO
47from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_GSM_WCDMA
48from acts.test_utils.tel.tel_subscription_utils import get_outgoing_voice_sub_id
49from acts.test_utils.tel.tel_subscription_utils import get_default_data_sub_id
50from acts.test_utils.tel.tel_test_utils import call_reject_leave_message
51from acts.test_utils.tel.tel_test_utils import call_setup_teardown
52from acts.test_utils.tel.tel_test_utils import ensure_network_generation
53from acts.test_utils.tel.tel_test_utils import \
54    ensure_network_generation_for_subscription
55from acts.test_utils.tel.tel_test_utils import \
56    ensure_network_rat_for_subscription
57from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
58from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
59from acts.test_utils.tel.tel_test_utils import get_network_gen_for_subscription
60from acts.test_utils.tel.tel_test_utils import get_network_rat
61from acts.test_utils.tel.tel_test_utils import get_network_rat_for_subscription
62from acts.test_utils.tel.tel_test_utils import is_wfc_enabled
63from acts.test_utils.tel.tel_test_utils import \
64    reset_preferred_network_type_to_allowable_range
65from acts.test_utils.tel.tel_test_utils import set_wfc_mode
66from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
67from acts.test_utils.tel.tel_test_utils import toggle_volte
68from acts.test_utils.tel.tel_test_utils import toggle_volte_for_subscription
69from acts.test_utils.tel.tel_test_utils import verify_incall_state
70from acts.test_utils.tel.tel_test_utils import \
71    wait_for_data_attach_for_subscription
72from acts.test_utils.tel.tel_test_utils import \
73    wait_for_network_generation_for_subscription
74from acts.test_utils.tel.tel_test_utils import wait_for_not_network_rat
75from acts.test_utils.tel.tel_test_utils import wait_for_network_rat
76from acts.test_utils.tel.tel_test_utils import \
77    wait_for_network_rat_for_subscription
78from acts.test_utils.tel.tel_test_utils import wait_for_volte_enabled
79from acts.test_utils.tel.tel_test_utils import \
80    wait_for_voice_attach_for_subscription
81from acts.test_utils.tel.tel_test_utils import wait_for_wfc_enabled
82from acts.test_utils.tel.tel_test_utils import wait_for_wfc_disabled
83
84
85def two_phone_call_leave_voice_mail(
86        log,
87        caller,
88        caller_idle_func,
89        caller_in_call_check_func,
90        callee,
91        callee_idle_func,
92        wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL):
93    """Call from caller to callee, reject on callee, caller leave a voice mail.
94
95    1. Caller call Callee.
96    2. Callee reject incoming call.
97    3. Caller leave a voice mail.
98    4. Verify callee received the voice mail notification.
99
100    Args:
101        caller: caller android device object.
102        caller_idle_func: function to check caller's idle state.
103        caller_in_call_check_func: function to check caller's in-call state.
104        callee: callee android device object.
105        callee_idle_func: function to check callee's idle state.
106        wait_time_in_call: time to wait when leaving a voice mail.
107            This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL
108
109    Returns:
110        True: if voice message is received on callee successfully.
111        False: for errors
112    """
113
114    ads = [caller, callee]
115
116    # Make sure phones are idle.
117    ensure_phones_idle(log, ads)
118    if caller_idle_func and not caller_idle_func(log, caller):
119        log.error("Caller Failed to Reselect")
120        return False
121    if callee_idle_func and not callee_idle_func(log, callee):
122        log.error("Callee Failed to Reselect")
123        return False
124
125    # TODO: b/26337871 Need to use proper API to check phone registered.
126    time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
127
128    # Make call and leave a message.
129    if not call_reject_leave_message(
130            log, caller, callee, caller_in_call_check_func, wait_time_in_call):
131        log.error("make a call and leave a message failed.")
132        return False
133    return True
134
135
136def two_phone_call_short_seq(log,
137                             phone_a,
138                             phone_a_idle_func,
139                             phone_a_in_call_check_func,
140                             phone_b,
141                             phone_b_idle_func,
142                             phone_b_in_call_check_func,
143                             call_sequence_func=None,
144                             wait_time_in_call=WAIT_TIME_IN_CALL):
145    """Call process short sequence.
146    1. Ensure phone idle and in idle_func check return True.
147    2. Call from PhoneA to PhoneB, accept on PhoneB.
148    3. Check phone state, hangup on PhoneA.
149    4. Ensure phone idle and in idle_func check return True.
150    5. Call from PhoneA to PhoneB, accept on PhoneB.
151    6. Check phone state, hangup on PhoneB.
152
153    Args:
154        phone_a: PhoneA's android device object.
155        phone_a_idle_func: function to check PhoneA's idle state.
156        phone_a_in_call_check_func: function to check PhoneA's in-call state.
157        phone_b: PhoneB's android device object.
158        phone_b_idle_func: function to check PhoneB's idle state.
159        phone_b_in_call_check_func: function to check PhoneB's in-call state.
160        call_sequence_func: default parameter, not implemented.
161        wait_time_in_call: time to wait in call.
162            This is optional, default is WAIT_TIME_IN_CALL
163
164    Returns:
165        True: if call sequence succeed.
166        False: for errors
167    """
168    ads = [phone_a, phone_b]
169
170    call_params = [(ads[0], ads[1], ads[0], phone_a_in_call_check_func,
171                    phone_b_in_call_check_func),
172                   (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
173                    phone_b_in_call_check_func), ]
174
175    for param in call_params:
176        # Make sure phones are idle.
177        ensure_phones_idle(log, ads)
178        if phone_a_idle_func and not phone_a_idle_func(log, phone_a):
179            log.error("Phone A Failed to Reselect")
180            return False
181        if phone_b_idle_func and not phone_b_idle_func(log, phone_b):
182            log.error("Phone B Failed to Reselect")
183            return False
184
185        # TODO: b/26337871 Need to use proper API to check phone registered.
186        time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
187
188        # Make call.
189        log.info("---> Call test: {} to {} <---".format(param[0].serial, param[
190            1].serial))
191        if not call_setup_teardown(log,
192                                   *param,
193                                   wait_time_in_call=wait_time_in_call):
194            log.error("Call Iteration Failed")
195            return False
196
197    return True
198
199
200def two_phone_call_long_seq(log,
201                            phone_a,
202                            phone_a_idle_func,
203                            phone_a_in_call_check_func,
204                            phone_b,
205                            phone_b_idle_func,
206                            phone_b_in_call_check_func,
207                            call_sequence_func=None,
208                            wait_time_in_call=WAIT_TIME_IN_CALL):
209    """Call process long sequence.
210    1. Ensure phone idle and in idle_func check return True.
211    2. Call from PhoneA to PhoneB, accept on PhoneB.
212    3. Check phone state, hangup on PhoneA.
213    4. Ensure phone idle and in idle_func check return True.
214    5. Call from PhoneA to PhoneB, accept on PhoneB.
215    6. Check phone state, hangup on PhoneB.
216    7. Ensure phone idle and in idle_func check return True.
217    8. Call from PhoneB to PhoneA, accept on PhoneA.
218    9. Check phone state, hangup on PhoneA.
219    10. Ensure phone idle and in idle_func check return True.
220    11. Call from PhoneB to PhoneA, accept on PhoneA.
221    12. Check phone state, hangup on PhoneB.
222
223    Args:
224        phone_a: PhoneA's android device object.
225        phone_a_idle_func: function to check PhoneA's idle state.
226        phone_a_in_call_check_func: function to check PhoneA's in-call state.
227        phone_b: PhoneB's android device object.
228        phone_b_idle_func: function to check PhoneB's idle state.
229        phone_b_in_call_check_func: function to check PhoneB's in-call state.
230        call_sequence_func: default parameter, not implemented.
231        wait_time_in_call: time to wait in call.
232            This is optional, default is WAIT_TIME_IN_CALL
233
234    Returns:
235        True: if call sequence succeed.
236        False: for errors
237    """
238    ads = [phone_a, phone_b]
239
240    call_params = [(ads[0], ads[1], ads[0], phone_a_in_call_check_func,
241                    phone_b_in_call_check_func),
242                   (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
243                    phone_b_in_call_check_func),
244                   (ads[1], ads[0], ads[0], phone_b_in_call_check_func,
245                    phone_a_in_call_check_func),
246                   (ads[1], ads[0], ads[1], phone_b_in_call_check_func,
247                    phone_a_in_call_check_func), ]
248
249    for param in call_params:
250        # Make sure phones are idle.
251        ensure_phones_idle(log, ads)
252        if phone_a_idle_func and not phone_a_idle_func(log, phone_a):
253            log.error("Phone A Failed to Reselect")
254            return False
255        if phone_b_idle_func and not phone_b_idle_func(log, phone_b):
256            log.error("Phone B Failed to Reselect")
257            return False
258
259        # TODO: b/26337871 Need to use proper API to check phone registered.
260        time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
261
262        # Make call.
263        log.info("---> Call test: {} to {} <---".format(param[0].serial, param[
264            1].serial))
265        if not call_setup_teardown(log,
266                                   *param,
267                                   wait_time_in_call=wait_time_in_call):
268            log.error("Call Iteration Failed")
269            return False
270
271    return True
272
273def phone_setup_iwlan(log,
274                      ad,
275                      is_airplane_mode,
276                      wfc_mode,
277                      wifi_ssid=None,
278                      wifi_pwd=None):
279    """Phone setup function for epdg call test.
280    Set WFC mode according to wfc_mode.
281    Set airplane mode according to is_airplane_mode.
282    Make sure phone connect to WiFi. (If wifi_ssid is not None.)
283    Wait for phone to be in iwlan data network type.
284    Wait for phone to report wfc enabled flag to be true.
285
286    Args:
287        log: Log object.
288        ad: Android device object.
289        is_airplane_mode: True to turn on airplane mode. False to turn off airplane mode.
290        wfc_mode: WFC mode to set to.
291        wifi_ssid: WiFi network SSID. This is optional.
292            If wifi_ssid is None, then phone_setup_iwlan will not attempt to connect to wifi.
293        wifi_pwd: WiFi network password. This is optional.
294
295    Returns:
296        True if success. False if fail.
297    """
298    return phone_setup_iwlan_for_subscription(
299        log, ad, get_outgoing_voice_sub_id(ad), is_airplane_mode, wfc_mode,
300        wifi_ssid, wifi_pwd)
301
302
303def phone_setup_iwlan_for_subscription(log,
304                                       ad,
305                                       sub_id,
306                                       is_airplane_mode,
307                                       wfc_mode,
308                                       wifi_ssid=None,
309                                       wifi_pwd=None):
310    """Phone setup function for epdg call test for subscription id.
311    Set WFC mode according to wfc_mode.
312    Set airplane mode according to is_airplane_mode.
313    Make sure phone connect to WiFi. (If wifi_ssid is not None.)
314    Wait for phone to be in iwlan data network type.
315    Wait for phone to report wfc enabled flag to be true.
316
317    Args:
318        log: Log object.
319        ad: Android device object.
320        sub_id: subscription id.
321        is_airplane_mode: True to turn on airplane mode. False to turn off airplane mode.
322        wfc_mode: WFC mode to set to.
323        wifi_ssid: WiFi network SSID. This is optional.
324            If wifi_ssid is None, then phone_setup_iwlan will not attempt to connect to wifi.
325        wifi_pwd: WiFi network password. This is optional.
326
327    Returns:
328        True if success. False if fail.
329    """
330
331    toggle_airplane_mode(log, ad, False)
332
333    if ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform():
334        toggle_volte(log, ad, True)
335
336    if not is_airplane_mode and not ensure_network_generation_for_subscription(
337            log,
338            ad,
339            sub_id,
340            GEN_4G,
341            voice_or_data=NETWORK_SERVICE_DATA):
342        return False
343
344    if not set_wfc_mode(log, ad, wfc_mode):
345        log.error("{} set WFC mode failed.".format(ad.serial))
346        return False
347
348    toggle_airplane_mode(log, ad, is_airplane_mode)
349
350    if wifi_ssid is not None:
351        if not ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd):
352            log.error("{} connect to WiFi failed.".format(ad.serial))
353            return False
354
355    return phone_idle_iwlan_for_subscription(log, ad, sub_id)
356
357
358def phone_setup_iwlan_cellular_preferred(log,
359                                         ad,
360                                         wifi_ssid=None,
361                                         wifi_pwd=None):
362    """Phone setup function for iwlan Non-APM CELLULAR_PREFERRED test.
363    Set WFC mode according to CELLULAR_PREFERRED.
364    Set airplane mode according to False.
365    Make sure phone connect to WiFi. (If wifi_ssid is not None.)
366    Make sure phone don't report iwlan data network type.
367    Make sure phone don't report wfc enabled flag to be true.
368
369    Args:
370        log: Log object.
371        ad: Android device object.
372        wifi_ssid: WiFi network SSID. This is optional.
373            If wifi_ssid is None, then phone_setup_iwlan will not attempt to connect to wifi.
374        wifi_pwd: WiFi network password. This is optional.
375
376    Returns:
377        True if success. False if fail.
378    """
379    toggle_airplane_mode(log, ad, False)
380    toggle_volte(log, ad, True)
381    if not ensure_network_generation(log,
382                                     ad,
383                                     GEN_4G,
384                                     voice_or_data=NETWORK_SERVICE_DATA):
385        return False
386    if not set_wfc_mode(log, ad, WFC_MODE_CELLULAR_PREFERRED):
387        log.error("{} set WFC mode failed.".format(ad.serial))
388        return False
389    if wifi_ssid is not None:
390        if not ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd):
391            log.error("{} connect to WiFi failed.".format(ad.serial))
392            return False
393    if not wait_for_not_network_rat(log,
394                                    ad,
395                                    RAT_FAMILY_WLAN,
396                                    voice_or_data=NETWORK_SERVICE_DATA):
397        log.error("{} data rat in iwlan mode.".format(ad.serial))
398        return False
399    elif not wait_for_wfc_disabled(log, ad, MAX_WAIT_TIME_WFC_ENABLED):
400        log.error("{} should report wifi calling disabled within {}s.".format(
401            ad.serial, MAX_WAIT_TIME_WFC_ENABLED))
402        return False
403    return True
404
405def phone_setup_data_for_subscription(log, ad, sub_id, network_generation):
406    """Setup Phone <sub_id> Data to <network_generation>
407
408    Args:
409        log: log object
410        ad: android device object
411        sub_id: subscription id
412        network_generation: network generation, e.g. GEN_2G, GEN_3G, GEN_4G
413
414    Returns:
415        True if success, False if fail.
416    """
417    toggle_airplane_mode(log, ad, False)
418    if not set_wfc_mode(log, ad, WFC_MODE_DISABLED):
419        log.error("{} Disable WFC failed.".format(ad.serial))
420        return False
421    if not ensure_network_generation_for_subscription(
422            log,
423            ad,
424            sub_id,
425            network_generation,
426            voice_or_data=NETWORK_SERVICE_DATA):
427        return False
428    return True
429
430def phone_setup_4g(log, ad):
431    """Setup Phone default data sub_id data to 4G.
432
433    Args:
434        log: log object
435        ad: android device object
436
437    Returns:
438        True if success, False if fail.
439    """
440    return phone_setup_4g_for_subscription(
441        log, ad, get_default_data_sub_id(ad))
442
443def phone_setup_4g_for_subscription(log, ad, sub_id):
444    """Setup Phone <sub_id> Data to 4G.
445
446    Args:
447        log: log object
448        ad: android device object
449        sub_id: subscription id
450
451    Returns:
452        True if success, False if fail.
453    """
454    return phone_setup_data_for_subscription(log, ad, sub_id, GEN_4G)
455
456def phone_setup_3g(log, ad):
457    """Setup Phone default data sub_id data to 3G.
458
459    Args:
460        log: log object
461        ad: android device object
462
463    Returns:
464        True if success, False if fail.
465    """
466    return phone_setup_3g_for_subscription(
467        log, ad, get_default_data_sub_id(ad))
468
469def phone_setup_3g_for_subscription(log, ad, sub_id):
470    """Setup Phone <sub_id> Data to 3G.
471
472    Args:
473        log: log object
474        ad: android device object
475        sub_id: subscription id
476
477    Returns:
478        True if success, False if fail.
479    """
480    return phone_setup_data_for_subscription(log, ad, sub_id, GEN_3G)
481
482def phone_setup_2g(log, ad):
483    """Setup Phone default data sub_id data to 2G.
484
485    Args:
486        log: log object
487        ad: android device object
488
489    Returns:
490        True if success, False if fail.
491    """
492    return phone_setup_2g_for_subscription(
493        log, ad, get_default_data_sub_id(ad))
494
495def phone_setup_2g_for_subscription(log, ad, sub_id):
496    """Setup Phone <sub_id> Data to 3G.
497
498    Args:
499        log: log object
500        ad: android device object
501        sub_id: subscription id
502
503    Returns:
504        True if success, False if fail.
505    """
506    return phone_setup_data_for_subscription(log, ad, sub_id, GEN_2G)
507
508def phone_setup_csfb(log, ad):
509    """Setup phone for CSFB call test.
510
511    Setup Phone to be in 4G mode.
512    Disabled VoLTE.
513
514    Args:
515        log: log object
516        ad: Android device object.
517
518    Returns:
519        True if setup successfully.
520        False for errors.
521    """
522    return phone_setup_csfb_for_subscription(log, ad,
523                                             get_outgoing_voice_sub_id(ad))
524
525
526def phone_setup_csfb_for_subscription(log, ad, sub_id):
527    """Setup phone for CSFB call test for subscription id.
528
529    Setup Phone to be in 4G mode.
530    Disabled VoLTE.
531
532    Args:
533        log: log object
534        ad: Android device object.
535        sub_id: subscription id.
536
537    Returns:
538        True if setup successfully.
539        False for errors.
540    """
541    if not phone_setup_4g_for_subscription(log, ad, sub_id):
542        log.error("{} failed to set to 4G data.".format(ad.serial))
543        return False
544    if ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform():
545        toggle_volte(log, ad, False)
546    if not ensure_network_generation_for_subscription(
547            log,
548            ad,
549            sub_id,
550            GEN_4G,
551            voice_or_data=NETWORK_SERVICE_DATA):
552        return False
553
554    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
555                                                  MAX_WAIT_TIME_NW_SELECTION):
556        return False
557
558    return phone_idle_csfb_for_subscription(log, ad, sub_id)
559
560def phone_setup_volte(log, ad):
561    """Setup VoLTE enable.
562
563    Args:
564        log: log object
565        ad: android device object.
566
567    Returns:
568        True: if VoLTE is enabled successfully.
569        False: for errors
570    """
571    return phone_setup_volte_for_subscription(
572        log, ad, get_outgoing_voice_sub_id(ad))
573
574
575def phone_setup_volte_for_subscription(log, ad, sub_id):
576    """Setup VoLTE enable for subscription id.
577
578    Args:
579        log: log object
580        ad: android device object.
581        sub_id: subscription id.
582
583    Returns:
584        True: if VoLTE is enabled successfully.
585        False: for errors
586    """
587    if not phone_setup_4g_for_subscription(log, ad, sub_id):
588        log.error("{} failed to set to 4G data.".format(ad.serial))
589        return False
590    toggle_volte_for_subscription(log, ad, sub_id, True)
591    return phone_idle_volte_for_subscription(log, ad, sub_id)
592
593def phone_setup_voice_3g(log, ad):
594    """Setup phone voice to 3G.
595
596    Args:
597        log: log object
598        ad: Android device object.
599
600    Returns:
601        True if setup successfully.
602        False for errors.
603    """
604    return phone_setup_voice_3g_for_subscription(log, ad,
605                                                 get_outgoing_voice_sub_id(ad))
606
607def phone_setup_voice_3g_for_subscription(log, ad, sub_id):
608    """Setup phone voice to 3G for subscription id.
609
610    Args:
611        log: log object
612        ad: Android device object.
613        sub_id: subscription id.
614
615    Returns:
616        True if setup successfully.
617        False for errors.
618    """
619    if not phone_setup_3g_for_subscription(log, ad, sub_id):
620        log.error("{} failed to set to 3G data.".format(ad.serial))
621        return False
622    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
623                                                  MAX_WAIT_TIME_NW_SELECTION):
624        return False
625    return phone_idle_3g_for_subscription(log, ad, sub_id)
626
627def phone_setup_voice_2g(log, ad):
628    """Setup phone voice to 2G.
629
630    Args:
631        log: log object
632        ad: Android device object.
633
634    Returns:
635        True if setup successfully.
636        False for errors.
637    """
638    return phone_setup_voice_2g_for_subscription(
639        log, ad, get_outgoing_voice_sub_id(ad))
640
641
642def phone_setup_voice_2g_for_subscription(log, ad, sub_id):
643    """Setup phone voice to 2G for subscription id.
644
645    Args:
646        log: log object
647        ad: Android device object.
648        sub_id: subscription id.
649
650    Returns:
651        True if setup successfully.
652        False for errors.
653    """
654    if not phone_setup_2g_for_subscription(log, ad, sub_id):
655        log.error("{} failed to set to 2G data.".format(ad.serial))
656        return False
657    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
658                                                  MAX_WAIT_TIME_NW_SELECTION):
659        return False
660    return phone_idle_2g_for_subscription(log, ad, sub_id)
661
662def phone_setup_voice_general(log, ad):
663    """Setup phone for voice general call test.
664
665    Make sure phone attached to voice.
666    Make necessary delay.
667
668    Args:
669        ad: Android device object.
670
671    Returns:
672        True if setup successfully.
673        False for errors.
674    """
675    return phone_setup_voice_general_for_subscription(
676        log, ad, get_outgoing_voice_sub_id(ad))
677
678
679def phone_setup_voice_general_for_subscription(log, ad, sub_id):
680    """Setup phone for voice general call test for subscription id.
681
682    Make sure phone attached to voice.
683    Make necessary delay.
684
685    Args:
686        ad: Android device object.
687        sub_id: subscription id.
688
689    Returns:
690        True if setup successfully.
691        False for errors.
692    """
693    toggle_airplane_mode(log, ad, False)
694    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
695                                                  MAX_WAIT_TIME_NW_SELECTION):
696        # if phone can not attach voice, try phone_setup_voice_3g
697        return phone_setup_voice_3g_for_subscription(log, ad, sub_id)
698    return True
699
700def phone_setup_data_general(log, ad):
701    """Setup phone for data general test.
702
703    Make sure phone attached to data.
704    Make necessary delay.
705
706    Args:
707        ad: Android device object.
708
709    Returns:
710        True if setup successfully.
711        False for errors.
712    """
713    return phone_setup_data_general_for_subscription(
714        log, ad, ad.droid.subscriptionGetDefaultDataSubId())
715
716def phone_setup_data_general_for_subscription(log, ad, sub_id):
717    """Setup phone for data general test for subscription id.
718
719    Make sure phone attached to data.
720    Make necessary delay.
721
722    Args:
723        ad: Android device object.
724        sub_id: subscription id.
725
726    Returns:
727        True if setup successfully.
728        False for errors.
729    """
730    toggle_airplane_mode(log, ad, False)
731    if not wait_for_data_attach_for_subscription(log, ad, sub_id,
732                                                 MAX_WAIT_TIME_NW_SELECTION):
733        # if phone can not attach data, try reset network preference settings
734        reset_preferred_network_type_to_allowable_range(log, ad)
735
736    return wait_for_data_attach_for_subscription(log, ad, sub_id,
737                                                 MAX_WAIT_TIME_NW_SELECTION)
738
739def phone_setup_rat_for_subscription(log, ad, sub_id, network_preference,
740                                     rat_family):
741    toggle_airplane_mode(log, ad, False)
742    if not set_wfc_mode(log, ad, WFC_MODE_DISABLED):
743        log.error("{} Disable WFC failed.".format(ad.serial))
744        return False
745    return ensure_network_rat_for_subscription(log, ad, sub_id,
746                                               network_preference, rat_family)
747
748
749def phone_setup_lte_gsm_wcdma(log, ad):
750    return phone_setup_lte_gsm_wcdma_for_subscription(
751        log, ad, ad.droid.subscriptionGetDefaultSubId())
752
753
754def phone_setup_lte_gsm_wcdma_for_subscription(log, ad, sub_id):
755    return phone_setup_rat_for_subscription(
756        log, ad, sub_id, NETWORK_MODE_LTE_GSM_WCDMA, RAT_FAMILY_LTE)
757
758
759def phone_setup_gsm_umts(log, ad):
760    return phone_setup_gsm_umts_for_subscription(
761        log, ad, ad.droid.subscriptionGetDefaultSubId())
762
763
764def phone_setup_gsm_umts_for_subscription(log, ad, sub_id):
765    return phone_setup_rat_for_subscription(
766        log, ad, sub_id, NETWORK_MODE_GSM_UMTS, RAT_FAMILY_WCDMA)
767
768
769def phone_setup_gsm_only(log, ad):
770    return phone_setup_gsm_only_for_subscription(
771        log, ad, ad.droid.subscriptionGetDefaultSubId())
772
773
774def phone_setup_gsm_only_for_subscription(log, ad, sub_id):
775    return phone_setup_rat_for_subscription(
776        log, ad, sub_id, NETWORK_MODE_GSM_ONLY, RAT_FAMILY_GSM)
777
778
779def phone_setup_lte_cdma_evdo(log, ad):
780    return phone_setup_lte_cdma_evdo_for_subscription(
781        log, ad, ad.droid.subscriptionGetDefaultSubId())
782
783
784def phone_setup_lte_cdma_evdo_for_subscription(log, ad, sub_id):
785    return phone_setup_rat_for_subscription(
786        log, ad, sub_id, NETWORK_MODE_LTE_CDMA_EVDO, RAT_FAMILY_LTE)
787
788
789def phone_setup_cdma(log, ad):
790    return phone_setup_cdma_for_subscription(
791        log, ad, ad.droid.subscriptionGetDefaultSubId())
792
793
794def phone_setup_cdma_for_subscription(log, ad, sub_id):
795    return phone_setup_rat_for_subscription(log, ad, sub_id, NETWORK_MODE_CDMA,
796                                            RAT_FAMILY_CDMA2000)
797
798
799def phone_idle_volte(log, ad):
800    """Return if phone is idle for VoLTE call test.
801
802    Args:
803        ad: Android device object.
804    """
805    return phone_idle_volte_for_subscription(log, ad,
806                                             get_outgoing_voice_sub_id(ad))
807
808
809def phone_idle_volte_for_subscription(log, ad, sub_id):
810    """Return if phone is idle for VoLTE call test for subscription id.
811
812    Args:
813        ad: Android device object.
814        sub_id: subscription id.
815    """
816    if not wait_for_network_rat_for_subscription(
817            log,
818            ad,
819            sub_id,
820            RAT_FAMILY_LTE,
821            voice_or_data=NETWORK_SERVICE_VOICE):
822        log.error("{} voice rat not in LTE mode.".format(ad.serial))
823        return False
824    if not wait_for_volte_enabled(log, ad, MAX_WAIT_TIME_VOLTE_ENABLED):
825        log.error(
826            "{} failed to <report volte enabled true> within {}s.".format(
827                ad.serial, MAX_WAIT_TIME_VOLTE_ENABLED))
828        return False
829    return True
830
831
832def phone_idle_iwlan(log, ad):
833    """Return if phone is idle for WiFi calling call test.
834
835    Args:
836        ad: Android device object.
837    """
838    return phone_idle_iwlan_for_subscription(log, ad,
839                                             get_outgoing_voice_sub_id(ad))
840
841
842def phone_idle_iwlan_for_subscription(log, ad, sub_id):
843    """Return if phone is idle for WiFi calling call test for subscription id.
844
845    Args:
846        ad: Android device object.
847        sub_id: subscription id.
848    """
849    if not wait_for_network_rat_for_subscription(
850            log,
851            ad,
852            sub_id,
853            RAT_FAMILY_WLAN,
854            voice_or_data=NETWORK_SERVICE_DATA):
855        log.error("{} data rat not in iwlan mode.".format(ad.serial))
856        return False
857    if not wait_for_wfc_enabled(log, ad, MAX_WAIT_TIME_WFC_ENABLED):
858        log.error("{} failed to <report wfc enabled true> within {}s.".format(
859            ad.serial, MAX_WAIT_TIME_WFC_ENABLED))
860        return False
861    return True
862
863
864def phone_idle_csfb(log, ad):
865    """Return if phone is idle for CSFB call test.
866
867    Args:
868        ad: Android device object.
869    """
870    return phone_idle_csfb_for_subscription(log, ad,
871                                            get_outgoing_voice_sub_id(ad))
872
873
874def phone_idle_csfb_for_subscription(log, ad, sub_id):
875    """Return if phone is idle for CSFB call test for subscription id.
876
877    Args:
878        ad: Android device object.
879        sub_id: subscription id.
880    """
881    if not wait_for_network_rat_for_subscription(
882            log,
883            ad,
884            sub_id,
885            RAT_FAMILY_LTE,
886            voice_or_data=NETWORK_SERVICE_DATA):
887        log.error("{} data rat not in lte mode.".format(ad.serial))
888        return False
889    return True
890
891
892def phone_idle_3g(log, ad):
893    """Return if phone is idle for 3G call test.
894
895    Args:
896        ad: Android device object.
897    """
898    return phone_idle_3g_for_subscription(log, ad,
899                                          get_outgoing_voice_sub_id(ad))
900
901
902def phone_idle_3g_for_subscription(log, ad, sub_id):
903    """Return if phone is idle for 3G call test for subscription id.
904
905    Args:
906        ad: Android device object.
907        sub_id: subscription id.
908    """
909    return wait_for_network_generation_for_subscription(
910        log,
911        ad,
912        sub_id,
913        GEN_3G,
914        voice_or_data=NETWORK_SERVICE_VOICE)
915
916
917def phone_idle_2g(log, ad):
918    """Return if phone is idle for 2G call test.
919
920    Args:
921        ad: Android device object.
922    """
923    return phone_idle_2g_for_subscription(log, ad,
924                                          get_outgoing_voice_sub_id(ad))
925
926
927def phone_idle_2g_for_subscription(log, ad, sub_id):
928    """Return if phone is idle for 2G call test for subscription id.
929
930    Args:
931        ad: Android device object.
932        sub_id: subscription id.
933    """
934    return wait_for_network_generation_for_subscription(
935        log,
936        ad,
937        sub_id,
938        GEN_2G,
939        voice_or_data=NETWORK_SERVICE_VOICE)
940
941
942def is_phone_in_call_volte(log, ad):
943    """Return if phone is in VoLTE call.
944
945    Args:
946        ad: Android device object.
947    """
948    return is_phone_in_call_volte_for_subscription(
949        log, ad, get_outgoing_voice_sub_id(ad))
950
951
952def is_phone_in_call_volte_for_subscription(log, ad, sub_id):
953    """Return if phone is in VoLTE call for subscription id.
954
955    Args:
956        ad: Android device object.
957        sub_id: subscription id.
958    """
959    if not ad.droid.telecomIsInCall():
960        log.error("{} not in call.".format(ad.serial))
961        return False
962    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
963                                               NETWORK_SERVICE_VOICE)
964    if nw_type != RAT_LTE:
965        log.error("{} voice rat on: {}. Expected: LTE".format(ad.serial,
966                                                              nw_type))
967        return False
968    return True
969
970
971def is_phone_in_call_csfb(log, ad):
972    """Return if phone is in CSFB call.
973
974    Args:
975        ad: Android device object.
976    """
977    return is_phone_in_call_csfb_for_subscription(
978        log, ad, get_outgoing_voice_sub_id(ad))
979
980
981def is_phone_in_call_csfb_for_subscription(log, ad, sub_id):
982    """Return if phone is in CSFB call for subscription id.
983
984    Args:
985        ad: Android device object.
986        sub_id: subscription id.
987    """
988    if not ad.droid.telecomIsInCall():
989        log.error("{} not in call.".format(ad.serial))
990        return False
991    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
992                                               NETWORK_SERVICE_VOICE)
993    if nw_type == RAT_LTE:
994        log.error("{} voice rat on: {}. Expected: not LTE".format(ad.serial,
995                                                                  nw_type))
996        return False
997    return True
998
999
1000def is_phone_in_call_3g(log, ad):
1001    """Return if phone is in 3G call.
1002
1003    Args:
1004        ad: Android device object.
1005    """
1006    return is_phone_in_call_3g_for_subscription(log, ad,
1007                                                get_outgoing_voice_sub_id(ad))
1008
1009
1010def is_phone_in_call_3g_for_subscription(log, ad, sub_id):
1011    """Return if phone is in 3G call for subscription id.
1012
1013    Args:
1014        ad: Android device object.
1015        sub_id: subscription id.
1016    """
1017    if not ad.droid.telecomIsInCall():
1018        log.error("{} not in call.".format(ad.serial))
1019        return False
1020    nw_gen = get_network_gen_for_subscription(log, ad, sub_id,
1021                                              NETWORK_SERVICE_VOICE)
1022    if nw_gen != GEN_3G:
1023        log.error("{} voice rat on: {}. Expected: 3g".format(ad.serial,
1024                                                             nw_gen))
1025        return False
1026    return True
1027
1028
1029def is_phone_in_call_2g(log, ad):
1030    """Return if phone is in 2G call.
1031
1032    Args:
1033        ad: Android device object.
1034    """
1035    return is_phone_in_call_2g_for_subscription(log, ad,
1036                                                get_outgoing_voice_sub_id(ad))
1037
1038
1039def is_phone_in_call_2g_for_subscription(log, ad, sub_id):
1040    """Return if phone is in 2G call for subscription id.
1041
1042    Args:
1043        ad: Android device object.
1044        sub_id: subscription id.
1045    """
1046    if not ad.droid.telecomIsInCall():
1047        log.error("{} not in call.".format(ad.serial))
1048        return False
1049    nw_gen = get_network_gen_for_subscription(log, ad, sub_id,
1050                                              NETWORK_SERVICE_VOICE)
1051    if nw_gen != GEN_2G:
1052        log.error("{} voice rat on: {}. Expected: 2g".format(ad.serial,
1053                                                             nw_gen))
1054        return False
1055    return True
1056
1057
1058def is_phone_in_call_1x(log, ad):
1059    """Return if phone is in 1x call.
1060
1061    Args:
1062        ad: Android device object.
1063    """
1064    return is_phone_in_call_1x_for_subscription(log, ad,
1065                                                get_outgoing_voice_sub_id(ad))
1066
1067
1068def is_phone_in_call_1x_for_subscription(log, ad, sub_id):
1069    """Return if phone is in 1x call for subscription id.
1070
1071    Args:
1072        ad: Android device object.
1073        sub_id: subscription id.
1074    """
1075    if not ad.droid.telecomIsInCall():
1076        log.error("{} not in call.".format(ad.serial))
1077        return False
1078    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
1079                                               NETWORK_SERVICE_VOICE)
1080    if nw_type != RAT_1XRTT:
1081        log.error("{} voice rat on: {}. Expected: 1xrtt".format(ad.serial,
1082                                                                nw_type))
1083        return False
1084    return True
1085
1086
1087def is_phone_in_call_wcdma(log, ad):
1088    """Return if phone is in WCDMA call.
1089
1090    Args:
1091        ad: Android device object.
1092    """
1093    return is_phone_in_call_wcdma_for_subscription(
1094        log, ad, get_outgoing_voice_sub_id(ad))
1095
1096
1097def is_phone_in_call_wcdma_for_subscription(log, ad, sub_id):
1098    """Return if phone is in WCDMA call for subscription id.
1099
1100    Args:
1101        ad: Android device object.
1102        sub_id: subscription id.
1103    """
1104    # Currently checking 'umts'.
1105    # Changes may needed in the future.
1106    if not ad.droid.telecomIsInCall():
1107        log.error("{} not in call.".format(ad.serial))
1108        return False
1109    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
1110                                               NETWORK_SERVICE_VOICE)
1111    if nw_type != RAT_UMTS:
1112        log.error("{} voice rat on: {}. Expected: umts".format(ad.serial,
1113                                                               nw_type))
1114        return False
1115    return True
1116
1117
1118def is_phone_in_call_iwlan(log, ad):
1119    """Return if phone is in WiFi call.
1120
1121    Args:
1122        ad: Android device object.
1123    """
1124    if not ad.droid.telecomIsInCall():
1125        log.error("{} not in call.".format(ad.serial))
1126        return False
1127    nw_type = get_network_rat(log, ad, NETWORK_SERVICE_DATA)
1128    if nw_type != RAT_IWLAN:
1129        log.error("{} data rat on: {}. Expected: iwlan".format(ad.serial,
1130                                                               nw_type))
1131        return False
1132    if not is_wfc_enabled(log, ad):
1133        log.error("{} WiFi Calling feature bit is False.".format(ad.serial))
1134        return False
1135    return True
1136
1137
1138def is_phone_in_call_not_iwlan(log, ad):
1139    """Return if phone is in WiFi call for subscription id.
1140
1141    Args:
1142        ad: Android device object.
1143        sub_id: subscription id.
1144    """
1145    if not ad.droid.telecomIsInCall():
1146        log.error("{} not in call.".format(ad.serial))
1147        return False
1148    nw_type = get_network_rat(log, ad, NETWORK_SERVICE_DATA)
1149    if nw_type == RAT_IWLAN:
1150        log.error("{} data rat on: {}. Expected: not iwlan".format(ad.serial,
1151                                                                   nw_type))
1152        return False
1153    if is_wfc_enabled(log, ad):
1154        log.error("{} WiFi Calling feature bit is True.".format(ad.serial))
1155        return False
1156    return True
1157
1158
1159def swap_calls(log,
1160               ads,
1161               call_hold_id,
1162               call_active_id,
1163               num_swaps=1,
1164               check_call_status=True):
1165    """PhoneA in call with B and C. Swap active/holding call on PhoneA.
1166
1167    Swap call and check status on PhoneA.
1168        (This step may have multiple times according to 'num_swaps'.)
1169    Check if all 3 phones are 'in-call'.
1170
1171    Args:
1172        ads: list of ad object, at least three need to pass in.
1173            Swap operation will happen on ads[0].
1174            ads[1] and ads[2] are call participants.
1175        call_hold_id: id for the holding call in ads[0].
1176            call_hold_id should be 'STATE_HOLDING' when calling this function.
1177        call_active_id: id for the active call in ads[0].
1178            call_active_id should be 'STATE_ACTIVE' when calling this function.
1179        num_swaps: how many swap/check operations will be done before return.
1180        check_call_status: This is optional. Default value is True.
1181            If this value is True, then call status (active/hold) will be
1182            be checked after each swap operation.
1183
1184    Returns:
1185        If no error happened, return True, otherwise, return False.
1186    """
1187    if check_call_status:
1188        # Check status before swap.
1189        if ads[0].droid.telecomCallGetCallState(
1190                call_active_id) != CALL_STATE_ACTIVE:
1191            log.error("Call_id:{}, state:{}, expected: STATE_ACTIVE".format(
1192                call_active_id, ads[0].droid.telecomCallGetCallState(
1193                    call_active_id)))
1194            return False
1195        if ads[0].droid.telecomCallGetCallState(
1196                call_hold_id) != CALL_STATE_HOLDING:
1197            log.error("Call_id:{}, state:{}, expected: STATE_HOLDING".format(
1198                call_hold_id, ads[0].droid.telecomCallGetCallState(
1199                    call_hold_id)))
1200            return False
1201
1202    i = 1
1203    while (i <= num_swaps):
1204        log.info("swap_test: {}. {} swap and check call status.".format(i, ads[
1205            0].serial))
1206        ads[0].droid.telecomCallHold(call_active_id)
1207        time.sleep(WAIT_TIME_IN_CALL)
1208        # Swap object reference
1209        call_active_id, call_hold_id = call_hold_id, call_active_id
1210        if check_call_status:
1211            # Check status
1212            if ads[0].droid.telecomCallGetCallState(
1213                    call_active_id) != CALL_STATE_ACTIVE:
1214                log.error(
1215                    "Call_id:{}, state:{}, expected: STATE_ACTIVE".format(
1216                        call_active_id, ads[0].droid.telecomCallGetCallState(
1217                            call_active_id)))
1218                return False
1219            if ads[0].droid.telecomCallGetCallState(
1220                    call_hold_id) != CALL_STATE_HOLDING:
1221                log.error(
1222                    "Call_id:{}, state:{}, expected: STATE_HOLDING".format(
1223                        call_hold_id, ads[0].droid.telecomCallGetCallState(
1224                            call_hold_id)))
1225                return False
1226        # TODO: b/26296375 add voice check.
1227
1228        i += 1
1229
1230    #In the end, check all three phones are 'in-call'.
1231    if not verify_incall_state(log, [ads[0], ads[1], ads[2]], True):
1232        return False
1233
1234    return True
1235
1236
1237def get_audio_route(log, ad):
1238    """Gets the audio route for the active call
1239
1240    Args:
1241        log: logger object
1242        ad: android_device object
1243
1244    Returns:
1245        Audio route string ["BLUETOOTH", "EARPIECE", "SPEAKER", "WIRED_HEADSET"
1246            "WIRED_OR_EARPIECE"]
1247    """
1248
1249    audio_state = ad.droid.telecomCallGetAudioState()
1250    return audio_state["AudioRoute"]
1251
1252
1253def set_audio_route(log, ad, route):
1254    """Sets the audio route for the active call
1255
1256    Args:
1257        log: logger object
1258        ad: android_device object
1259        route: string ["BLUETOOTH", "EARPIECE", "SPEAKER", "WIRED_HEADSET"
1260            "WIRED_OR_EARPIECE"]
1261
1262    Returns:
1263        If no error happened, return True, otherwise, return False.
1264    """
1265    ad.droid.telecomCallSetAudioRoute(route)
1266    return True
1267
1268
1269def is_property_in_call_properties(log, ad, call_id, expected_property):
1270    """Return if the call_id has the expected property
1271
1272    Args:
1273        log: logger object
1274        ad: android_device object
1275        call_id: call id.
1276        expected_property: expected property.
1277
1278    Returns:
1279        True if call_id has expected_property. False if not.
1280    """
1281    properties = ad.droid.telecomCallGetProperties(call_id)
1282    return (expected_property in properties)
1283
1284
1285def is_call_hd(log, ad, call_id):
1286    """Return if the call_id is HD call.
1287
1288    Args:
1289        log: logger object
1290        ad: android_device object
1291        call_id: call id.
1292
1293    Returns:
1294        True if call_id is HD call. False if not.
1295    """
1296    return is_property_in_call_properties(log, ad, call_id,
1297                                          CALL_PROPERTY_HIGH_DEF_AUDIO)
1298
1299
1300def get_cep_conference_call_id(ad):
1301    """Get CEP conference call id if there is an ongoing CEP conference call.
1302
1303    Args:
1304        ad: android device object.
1305
1306    Returns:
1307        call id for CEP conference call if there is an ongoing CEP conference call.
1308        None otherwise.
1309    """
1310    for call in ad.droid.telecomCallGetCallIds():
1311        if len(ad.droid.telecomCallGetCallChildren(call)) != 0:
1312            return call
1313    return None
1314