• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2021 - The Android Open Source Project
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_contrib.test_utils.tel.gft_inout_defines import VOLTE_CALL
19from acts_contrib.test_utils.tel.gft_inout_defines import CSFB_CALL
20from acts_contrib.test_utils.tel.gft_inout_defines import WFC_CALL
21from acts_contrib.test_utils.tel.tel_defines  import DATA_STATE_CONNECTED
22from acts_contrib.test_utils.tel.tel_defines  import SERVICE_STATE_IN_SERVICE
23from acts_contrib.test_utils.tel.tel_defines  import SERVICE_STATE_OUT_OF_SERVICE
24from acts_contrib.test_utils.tel.tel_logging_utils import log_screen_shot
25from acts_contrib.test_utils.tel.tel_ims_utils import is_ims_registered
26from acts_contrib.test_utils.tel.tel_test_utils import get_telephony_signal_strength
27from acts_contrib.test_utils.tel.tel_test_utils import get_service_state_by_adb
28from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection
29from acts_contrib.test_utils.tel.tel_voice_utils import hangup_call
30from acts_contrib.test_utils.tel.tel_voice_utils import initiate_call
31from acts_contrib.test_utils.tel.tel_voice_utils import is_phone_in_call
32from acts_contrib.test_utils.tel.tel_voice_utils import is_phone_in_call_iwlan
33from acts_contrib.test_utils.tel.tel_voice_utils import is_phone_in_call_volte
34from acts_contrib.test_utils.tel.tel_voice_utils import is_phone_in_call_csfb
35from acts_contrib.test_utils.tel.tel_data_utils import browsing_test
36
37
38def check_no_service_time(ad, timeout=120):
39    """ check device is no service or not
40
41        Args:
42            ad: android device
43            timeout: timeout time for device back to service
44
45        Returns:
46            True if pass; False if fail.
47    """
48
49    for i in range (timeout):
50        service_state = get_service_state_by_adb(ad.log,ad)
51        if service_state != SERVICE_STATE_IN_SERVICE:
52            ad.log.info("device becomes no/limited service in %s sec and service_state=%s"
53                %(i+1, service_state))
54            get_telephony_signal_strength(ad)
55            return True
56        time.sleep(1)
57    get_telephony_signal_strength(ad)
58    check_network_service(ad)
59    ad.log.info("device does not become no/limited service in %s sec and service_state=%s"
60        %(timeout, service_state))
61    return False
62
63
64def check_back_to_service_time(ad, timeout=120):
65    """ check device is back to service or not
66
67        Args:
68            ad: android device
69            timeout: timeout time for device back to service
70
71        Returns:
72            True if pass; False if fail.
73    """
74    for i in range (timeout):
75        service_state = get_service_state_by_adb(ad.log,ad)
76        if service_state == SERVICE_STATE_IN_SERVICE:
77            if i==0:
78                check_network_service(ad)
79                ad.log.info("Skip check_back_to_service_time. Service_state=%s"
80                    %(service_state))
81                return True
82            else:
83                ad.log.info("device is back to service in %s sec and service_state=%s"
84                    %(i+1, service_state))
85                get_telephony_signal_strength(ad)
86                return True
87        time.sleep(1)
88    get_telephony_signal_strength(ad)
89    ad.log.info("device is not back in service in %s sec and service_state=%s"
90        %(timeout, service_state))
91    return False
92
93
94def check_network_service(ad):
95    """ check network service
96
97        Args:
98            ad: android device
99
100        Returns:
101            True if ad is in service; False if ad is not in service.
102    """
103    network_type_voice = ad.droid.telephonyGetCurrentVoiceNetworkType()
104    network_type_data = ad.droid.telephonyGetCurrentDataNetworkType()
105    service_state = get_service_state_by_adb(ad.log,ad)
106    sim_state = ad.droid.telephonyGetSimState()
107    ad.log.info("sim_state=%s" %(sim_state))
108    ad.log.info("networkType_voice=%s" %(network_type_voice))
109    ad.log.info("networkType_data=%s" %(network_type_data))
110    ad.log.info("service_state=%s" %(service_state))
111    if service_state == SERVICE_STATE_OUT_OF_SERVICE:
112        log_screen_shot(ad, "device_out_of_service")
113        return False
114    return True
115
116
117def mo_voice_call(log, ad, call_type, end_call=True, talk_time=15,
118    retries=1, retry_time=30):
119    """ MO voice call and check call type.
120        End call if necessary.
121
122        Args:
123            log: log
124            ad: android device
125            call_type: WFC call, VOLTE call. CSFB call, voice call
126            end_call: hangup call after voice call flag
127            talk_time: in call duration in sec
128            retries: retry times
129            retry_time: wait for how many sec before next retry
130
131        Returns:
132            True if pass; False if fail.
133    """
134    callee_number = ad.mt_phone_number
135    ad.log.info("MO voice call. call_type=%s" %(call_type))
136    if is_phone_in_call(log, ad):
137        ad.log.info("%s is in call. hangup_call before initiate call" %(callee_number))
138        hangup_call(log, ad)
139        time.sleep(1)
140
141    for i in range(retries):
142        ad.log.info("mo_voice_call attempt %d", i + 1)
143        if initiate_call(log, ad, callee_number):
144            time.sleep(5)
145            check_voice_call_type(ad,call_type)
146            get_voice_call_type(ad)
147            break
148        else:
149            ad.log.error("initiate_call fail attempt %d", i + 1)
150            time.sleep(retry_time)
151            if i+1 == retries:
152                ad.log.error("mo_voice_call retry failure")
153                return False
154
155    time.sleep(10)
156    if end_call:
157        time.sleep(talk_time)
158        if is_phone_in_call(log, ad):
159            ad.log.info("end voice call")
160            if not hangup_call(log, ad):
161                ad.log.error("end call fail")
162                ad.droid.telecomShowInCallScreen()
163                log_screen_shot(ad, "end_call_fail")
164                return False
165        else:
166            #Call drop is unexpected
167            ad.log.error("%s Unexpected call drop" %(call_type))
168            ad.droid.telecomShowInCallScreen()
169            log_screen_shot(ad, "call_drop")
170            return False
171        ad.log.info("%s successful" %(call_type))
172    return True
173
174
175def check_voice_call_type(ad, call_type):
176    """ check current voice call type
177
178        Args:
179            ad: android device
180            call_type: WFC call, VOLTE call. CSFB call, voice call
181
182        Returns:
183            True if pass; False if fail.
184    """
185    if is_phone_in_call(ad.log, ad):
186        ad.droid.telecomShowInCallScreen()
187        log_screen_shot(ad, "expected_call_type_%s" %call_type)
188        if call_type == CSFB_CALL:
189            if not is_phone_in_call_csfb(ad.log, ad):
190                ad.log.error("current call is not %s" %(call_type))
191                return False
192            else:
193                ad.log.info("current call is CSFB %s" %(call_type))
194        elif call_type == WFC_CALL:
195            if not is_phone_in_call_iwlan(ad.log, ad):
196                ad.log.error("current call is not %s" %(call_type))
197                return False
198            else:
199                ad.log.info("current call is VoWiFi %s" %(call_type))
200        elif call_type == VOLTE_CALL:
201            if not is_phone_in_call_volte(ad.log, ad):
202                ad.log.error("current call is not %s" %(call_type))
203                return False
204            else:
205                ad.log.info(" current call is VOLTE %s" %(call_type))
206    else:
207        ad.log.error("device is not in call")
208        return False
209    return True
210
211
212def get_voice_call_type(ad):
213    """ get current voice call type
214
215        Args:
216            ad: android device
217
218        Returns:
219            call type
220    """
221    if is_phone_in_call(ad.log, ad):
222        if is_phone_in_call_csfb(ad.log, ad):
223            ad.log.info("current call is CSFB")
224            return CSFB_CALL
225        elif is_phone_in_call_iwlan(ad.log, ad):
226            ad.log.info("current call is VoWiFi")
227            return WFC_CALL
228        elif is_phone_in_call_volte(ad.log, ad):
229            ad.log.info("current call is VOLTE")
230            return VOLTE_CALL
231    else:
232        ad.log.error("device is not in call")
233    return "UNKNOWN"
234
235
236def verify_data_connection(ad, retries=3, retry_time=30):
237    """ verify data connection
238
239        Args:
240            ad: android device
241            retries: retry times
242            retry_time: wait for how many sec before next retry
243
244        Returns:
245            True if pass; False if fail.
246    """
247    for i in range(retries):
248        data_state = ad.droid.telephonyGetDataConnectionState()
249        wifi_info = ad.droid.wifiGetConnectionInfo()
250        if wifi_info["supplicant_state"] == "completed":
251            ad.log.info("Wifi is connected=%s" %(wifi_info["SSID"]))
252        ad.log.info("verify_data_connection attempt %d", i + 1)
253        if not verify_internet_connection(ad.log, ad, retries=3):
254            data_state = ad.droid.telephonyGetDataConnectionState()
255            network_type_data = ad.droid.telephonyGetCurrentDataNetworkType()
256            ad.log.error("verify_internet fail. data_state=%s, network_type_data=%s"
257                %(data_state, network_type_data))
258            ad.log.info("verify_data_connection fail attempt %d", i + 1)
259            log_screen_shot(ad, "verify_internet")
260            time.sleep(retry_time)
261        else:
262            ad.log.info("verify_data_connection pass")
263            return True
264    return False
265
266
267def check_ims_state(ad):
268    """ check current ism state
269
270        Args:
271            ad: android device
272
273        Returns:
274            ims state
275    """
276    r1 = is_ims_registered(ad.log, ad)
277    r2 = ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform()
278    r3 = ad.droid.imsIsEnhanced4gLteModeSettingEnabledByUser()
279    r4 = ad.droid.telephonyIsVolteAvailable()
280    ad.log.info("telephonyIsImsRegistered=%s" %(r1))
281    ad.log.info("imsIsEnhanced4gLteModeSettingEnabledByPlatform=%s" %(r2))
282    ad.log.info("imsIsEnhanced4gLteModeSettingEnabledByUser=%s" %(r3))
283    ad.log.info("telephonyIsVolteAvailable=%s" %(r4))
284    return r1
285
286
287def browsing_test_ping_retry(ad):
288    """ If browse test fails, use ping to test data connection
289
290        Args:
291            ad: android device
292
293        Returns:
294            True if pass; False if fail.
295    """
296    if not browsing_test(ad.log, ad):
297        ad.log.error("Failed to browse websites!")
298        if verify_data_connection(ad):
299            ad.log.info("Ping success!")
300            return True
301        else:
302            ad.log.info("Ping fail!")
303            return False
304    else:
305        ad.log.info("Successful to browse websites!")