1#!/usr/bin/env python3.4
2#
3# Copyright 2016 - 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
17from acts.test_decorators import test_tracker_info
18from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
19from acts.test_utils.tel.tel_atten_utils import set_rssi
20from acts.test_utils.tel.tel_defines import MAX_RSSI_RESERVED_VALUE
21from acts.test_utils.tel.tel_defines import MIN_RSSI_RESERVED_VALUE
22from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
23from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
24from acts.test_utils.tel.tel_defines import GEN_4G
25from acts.test_utils.tel.tel_test_utils import ensure_network_generation
26from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
27from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
28from acts.test_utils.tel.tel_test_utils import verify_http_connection
29from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection
30from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection
31from acts.test_utils.tel.tel_test_utils import run_multithread_func
32from acts.test_utils.tel.tel_test_utils import active_file_download_test
33from acts.test_utils.tel.tel_test_utils import get_telephony_signal_strength
34from acts.test_utils.tel.tel_test_utils import get_wifi_signal_strength
35from acts.utils import adb_shell_ping
36
37# Attenuator name
38ATTEN_NAME_FOR_WIFI_2G = 'wifi0'
39ATTEN_NAME_FOR_WIFI_5G = 'wifi1'
40ATTEN_NAME_FOR_CELL_3G = 'cell0'
41ATTEN_NAME_FOR_CELL_4G = 'cell1'
42
43DEFAULT_PING_DURATION = 120
44DEFAULT_IRAT_DURATION = 60
45
46
47class TelWifiDataTest(TelephonyBaseTest):
48    def __init__(self, controllers):
49        TelephonyBaseTest.__init__(self, controllers)
50
51        self.stress_test_number = self.get_stress_test_number()
52        self.live_network_ssid = self.user_params["wifi_network_ssid"]
53        self.live_network_pwd = self.user_params.get("wifi_network_pass")
54
55        self.attens = {}
56        for atten in self.attenuators:
57            self.attens[atten.path] = atten
58        attentuator_name_list = [
59            ATTEN_NAME_FOR_WIFI_2G, ATTEN_NAME_FOR_WIFI_5G,
60            ATTEN_NAME_FOR_CELL_3G, ATTEN_NAME_FOR_CELL_4G
61        ]
62        for atten_name in attentuator_name_list:
63            set_rssi(self.log, self.attens[atten_name], 0,
64                     MAX_RSSI_RESERVED_VALUE)
65
66    def teardown_test(self):
67        super().teardown_test()
68        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_2G], 0,
69                 MAX_RSSI_RESERVED_VALUE)
70        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_5G], 0,
71                 MAX_RSSI_RESERVED_VALUE)
72        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_3G], 0,
73                 MAX_RSSI_RESERVED_VALUE)
74        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_4G], 0,
75                 MAX_RSSI_RESERVED_VALUE)
76        return True
77
78    def _basic_connectivity_check(self):
79        """
80        Set Attenuator Value for WiFi and Cell to 0
81        Make sure DUT get Cell Data coverage (LTE)
82        Make sure DUT WiFi is connected
83        """
84        ad = self.android_devices[0]
85        toggle_airplane_mode(self.log, ad, False)
86        if not ensure_network_generation(self.log, ad,
87                                         GEN_4G, NETWORK_SERVICE_DATA):
88            return False
89
90        if not ensure_wifi_connected(self.log, ad,
91                                     self.live_network_ssid,
92                                     self.live_network_pwd):
93            ad.log.error("connect WiFi failed")
94            return False
95        return True
96
97    def _atten_setup_wifi_cell(self):
98        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_2G], 0,
99                 MAX_RSSI_RESERVED_VALUE)
100        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_5G], 0,
101                 MAX_RSSI_RESERVED_VALUE)
102        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_3G], 0,
103                 MAX_RSSI_RESERVED_VALUE)
104        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_4G], 0,
105                 MAX_RSSI_RESERVED_VALUE)
106
107    def _atten_setup_cell_only(self):
108        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_2G], 0,
109                 MIN_RSSI_RESERVED_VALUE)
110        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_5G], 0,
111                 MIN_RSSI_RESERVED_VALUE)
112        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_3G], 0,
113                 MAX_RSSI_RESERVED_VALUE)
114        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_4G], 0,
115                 MAX_RSSI_RESERVED_VALUE)
116
117    def _atten_setup_lte_only(self):
118        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_2G], 0,
119                 MIN_RSSI_RESERVED_VALUE)
120        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_5G], 0,
121                 MIN_RSSI_RESERVED_VALUE)
122        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_3G], 0,
123                 MIN_RSSI_RESERVED_VALUE)
124        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_4G], 0,
125                 MAX_RSSI_RESERVED_VALUE)
126
127    def _atten_setup_wcdma_only(self):
128        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_2G], 0,
129                 MIN_RSSI_RESERVED_VALUE)
130        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_5G], 0,
131                 MIN_RSSI_RESERVED_VALUE)
132        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_3G], 0,
133                 MAX_RSSI_RESERVED_VALUE)
134        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_4G], 0,
135                 MIN_RSSI_RESERVED_VALUE)
136
137    def _atten_setup_wifi_only(self):
138        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_2G], 0,
139                 MAX_RSSI_RESERVED_VALUE)
140        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_WIFI_5G], 0,
141                 MAX_RSSI_RESERVED_VALUE)
142        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_3G], 0,
143                 MIN_RSSI_RESERVED_VALUE)
144        set_rssi(self.log, self.attens[ATTEN_NAME_FOR_CELL_4G], 0,
145                 MIN_RSSI_RESERVED_VALUE)
146
147    @TelephonyBaseTest.tel_test_wrap
148    def _wifi_cell_irat_task(self, ad, irat_wait_time=60):
149        """
150        Atten only WiFi to MIN and MAX
151        WiFi --> Cellular
152        """
153        self._atten_setup_wifi_cell()
154        if (not wait_for_wifi_data_connection(self.log, ad, True,
155                                              irat_wait_time) or
156                not verify_http_connection(self.log, ad)):
157            ad.log.error("Data not on WiFi")
158            get_telephony_signal_strength(ad)
159            get_wifi_signal_strength(ad)
160            return False
161
162        ad.log.info("Triggering WiFi to Cellular IRAT")
163        self._atten_setup_cell_only()
164        if (not wait_for_cell_data_connection(self.log, ad, True,
165                                              irat_wait_time) or
166                not verify_http_connection(self.log, ad)):
167            ad.log.error("Data not on Cell")
168            get_telephony_signal_strength(ad)
169            get_wifi_signal_strength(ad)
170            return False
171        return True
172
173    @test_tracker_info(uuid="b223f74b-59f4-4eec-8785-67420bd96bd1")
174    @TelephonyBaseTest.tel_test_wrap
175    def test_wifi_cell_irat_stress_ping_continuous(self):
176        """Test for data switch between WiFi and Cell. DUT go in and out WiFi
177        coverage for multiple times.
178
179        Steps:
180        1. Set WiFi and Cellular signal to good (attenuation value to MIN).
181        2. Make sure DUT get Cell data coverage (LTE) and WiFi connected.
182        3. Set WiFi RSSI to MAX (WiFi attenuator value to MIN).
183        4. Verify DUT report WiFi connected and Internet access OK.
184        5. Set WiFi RSSI to MIN (WiFi attenuator value to MAX).
185        6. Verify DUT report Cellular Data connected and Internet access OK.
186        7. Repeat Step 3~6 for stress number.
187
188        Expected Results:
189        4. DUT report WiFi connected and Internet access OK.
190        6. DUT report Cellular Data connected and Internet access OK.
191        7. Stress test should pass.
192
193        Returns:
194        True if Pass. False if fail.
195        """
196        if not self._basic_connectivity_check():
197            self.log.error("Basic Connectivity Check Failed")
198            return False
199
200        total_iteration = self.stress_test_number
201        ad = self.android_devices[0]
202        ping_task = (adb_shell_ping, (ad, DEFAULT_PING_DURATION,
203                                      "www.google.com", 200, 40))
204        irat_task = (self._wifi_cell_irat_task, (ad, DEFAULT_IRAT_DURATION))
205        current_iteration = 1
206        while (current_iteration <= total_iteration):
207            self.log.info(">----Current iteration = %d/%d----<",
208                          current_iteration, total_iteration)
209            results = run_multithread_func(self.log, [ping_task, irat_task])
210            if not results[1]:
211                ad.log.error("Data IRAT failed in active ICMP transfer")
212                break
213            if results[0]:
214                ad.log.info("ICMP transfer succeeded with parallel IRAT")
215            else:
216                ad.log.error("ICMP transfer failed with parallel IRAT")
217                break
218            self.log.info(">----Iteration : %d/%d succeed.----<",
219                          current_iteration, total_iteration)
220            current_iteration += 1
221        if current_iteration <= total_iteration:
222            self.log.info(">----Iteration : %d/%d failed.----<",
223                          current_iteration, total_iteration)
224            return False
225        else:
226            return True
227
228    @test_tracker_info(uuid="72d2aa4d-c395-417e-99c5-12dc22ea90a1")
229    @TelephonyBaseTest.tel_test_wrap
230    def test_wifi_cell_irat_stress_http_dl(self):
231        """Test for data switch between WiFi and Cell. DUT go in and out WiFi
232        coverage for multiple times.
233
234        Steps:
235        1. Set WiFi and Cellular signal to good (attenuation value to MIN).
236        2. Make sure DUT get Cell data coverage (LTE) and WiFi connected.
237        3. Set WiFi RSSI to MAX (WiFi attenuator value to MIN).
238        4. Verify DUT report WiFi connected and able to download file
239        5. Set WiFi RSSI to MIN (WiFi attenuator value to MAX).
240        6. Verify DUT report Cellular Data connected and able to download file
241        7. Repeat Step 3~6 for stress number.
242
243        Expected Results:
244        4. DUT report WiFi connected and able to download file
245        6. DUT report Cellular Data connected and able to download file
246        7. Stress test should pass.
247
248        Returns:
249        True if Pass. False if fail.
250        """
251        ad = self.android_devices[0]
252        if not self._basic_connectivity_check():
253            self.log.error("Basic Connectivity Check Failed")
254            return False
255
256        total_iteration = self.stress_test_number
257        self.log.info("Stress test. Total iteration = %d.", total_iteration)
258        current_iteration = 1
259        while (current_iteration <= total_iteration):
260            self.log.info(">----Current iteration = %d/%d----<",
261                          current_iteration, total_iteration)
262
263            self._atten_setup_wifi_cell()
264            if (not wait_for_wifi_data_connection(self.log, ad, True)):
265                ad.log.error("Data not on WiFi")
266                get_telephony_signal_strength(ad)
267                get_wifi_signal_strength(ad)
268                break
269
270            ad.on_mobile_data = False
271            if not active_file_download_test(self.log, ad):
272                ad.log.error("HTTP file download failed on WiFi")
273                get_telephony_signal_strength(ad)
274                get_wifi_signal_strength(ad)
275                break
276
277            self._atten_setup_cell_only()
278            if (not wait_for_cell_data_connection(self.log, ad, True)):
279                ad.log.error("Data not on Cell")
280                get_telephony_signal_strength(ad)
281                get_wifi_signal_strength(ad)
282                break
283
284            ad.on_mobile_data = True
285            if not active_file_download_test(self.log, ad):
286                ad.log.error("HTTP file download failed on cell")
287                get_telephony_signal_strength(ad)
288                get_wifi_signal_strength(ad)
289                break
290
291            self.log.info(">----Iteration : %d/%d succeed.----<",
292                          current_iteration, total_iteration)
293            current_iteration += 1
294
295        if current_iteration <= total_iteration:
296            self.log.info(">----Iteration : %d/%d failed.----<",
297                          current_iteration, total_iteration)
298            return False
299        else:
300            return True
301
302    @test_tracker_info(uuid="bce71469-114c-489f-b9c4-26c53c29a553")
303    @TelephonyBaseTest.tel_test_wrap
304    def test_wifi_cell_irat_stress_ping(self):
305        """Test for data switch between WiFi and Cell. DUT go in and out WiFi
306        coverage for multiple times.
307
308        Steps:
309        1. Set WiFi and Cellular signal to good (attenuation value to MIN).
310        2. Make sure DUT get Cell data coverage (LTE) and WiFi connected.
311        3. Set WiFi RSSI to MAX (WiFi attenuator value to MIN).
312        4. Verify DUT report WiFi connected and Internet access OK.
313        5. Set WiFi RSSI to MIN (WiFi attenuator value to MAX).
314        6. Verify DUT report Cellular Data connected and Internet access OK.
315        7. Repeat Step 3~6 for stress number.
316
317        Expected Results:
318        4. DUT report WiFi connected and Internet access OK.
319        6. DUT report Cellular Data connected and Internet access OK.
320        7. Stress test should pass.
321
322        Returns:
323        True if Pass. False if fail.
324        """
325        ad = self.android_devices[0]
326        if not self._basic_connectivity_check():
327            self.log.error("Basic Connectivity Check Failed")
328            return False
329
330        total_iteration = self.stress_test_number
331        self.log.info("Stress test. Total iteration = %d.", total_iteration)
332        current_iteration = 1
333        while (current_iteration <= total_iteration):
334            self.log.info(">----Current iteration = %d/%d----<",
335                          current_iteration, total_iteration)
336
337            self._atten_setup_wifi_cell()
338            if (not wait_for_wifi_data_connection(self.log, ad, True) or
339                    not verify_http_connection(self.log, ad)):
340                ad.log.error("Data not on WiFi")
341                get_telephony_signal_strength(ad)
342                get_wifi_signal_strength(ad)
343                break
344
345            self._atten_setup_cell_only()
346            if (not wait_for_cell_data_connection(self.log, ad, True) or
347                    not verify_http_connection(self.log, ad)):
348                ad.log.error("Data not on Cell")
349                get_telephony_signal_strength(ad)
350                get_wifi_signal_strength(ad)
351                break
352
353            self.log.info(">----Iteration : %d/%d succeed.----<",
354                          current_iteration, total_iteration)
355            current_iteration += 1
356        if current_iteration <= total_iteration:
357            self.log.info(">----Iteration : %d/%d failed.----<",
358                          current_iteration, total_iteration)
359            return False
360        else:
361            return True
362
363    @test_tracker_info(uuid="696f22ef-39cd-4e15-bbb2-f836d2ee47f1")
364    @TelephonyBaseTest.tel_test_wrap
365    def test_wifi_only_http_dl(self):
366        """Test for 10MB file download on WiFi Only
367
368        Steps:
369        1. Set WiFi atten to MIN and Cellular to MAX
370        2. Start downloading 1GB file from net
371        3. Verify is the download is successfull
372
373        Expected Results:
374        1. File should download over WiFi
375
376        Returns:
377        True if Pass. False if fail.
378        """
379        ad = self.android_devices[0]
380        if not self._basic_connectivity_check():
381            self.log.error("Basic Connectivity Check Failed")
382            return False
383        self._atten_setup_wifi_only()
384        if (not wait_for_wifi_data_connection(self.log, ad, True) or
385                not verify_http_connection(self.log, ad)):
386            ad.log.error("Data not on WiFi")
387            get_telephony_signal_strength(ad)
388            get_wifi_signal_strength(ad)
389            return False
390        ad.on_mobile_data = False
391        if not active_file_download_test(self.log, ad, "10MB"):
392            ad.log.error("HTTP file download failed on WiFi")
393            get_telephony_signal_strength(ad)
394            get_wifi_signal_strength(ad)
395            return False
396        return True
397
398    @test_tracker_info(uuid="6c9bf89b-5469-4b08-acf4-0ef651b1a318")
399    @TelephonyBaseTest.tel_test_wrap
400    def test_lte_only_http_dl(self):
401        """Test for 1GB file download on WiFi Only
402
403        Steps:
404        1. Set WiFi atten to MIN and Cellular to MAX
405        2. Start downloading 1GB file from net
406        3. Verify is the download is successfull
407
408        Expected Results:
409        1. File should download over WiFi
410
411        Returns:
412        True if Pass. False if fail.
413        """
414        ad = self.android_devices[0]
415        if not self._basic_connectivity_check():
416            self.log.error("Basic Connectivity Check Failed")
417            return False
418        self._atten_setup_lte_only()
419        if (not wait_for_cell_data_connection(self.log, ad, True) or
420                not verify_http_connection(self.log, ad)):
421            ad.log.error("Data not on LTE")
422            get_telephony_signal_strength(ad)
423            get_wifi_signal_strength(ad)
424            return False
425        ad.on_mobile_data = True
426        if not active_file_download_test(self.log, ad, "512MB"):
427            ad.log.error("HTTP file download failed on LTE")
428            get_telephony_signal_strength(ad)
429            get_wifi_signal_strength(ad)
430            return False
431        return True
432
433
434if __name__ == "__main__":
435    raise Exception("Cannot run this class directly")
436