• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17import itertools
18import json
19import pprint
20import queue
21import time
22
23import acts.base_test
24import acts.signals as signals
25import acts_contrib.test_utils.wifi.wifi_test_utils as wutils
26import acts.utils
27
28from acts import asserts
29from acts.test_decorators import test_tracker_info
30from acts_contrib.test_utils.bt.bt_test_utils import enable_bluetooth
31from acts_contrib.test_utils.bt.bt_test_utils import disable_bluetooth
32from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
33from acts_contrib.test_utils.wifi.wifi_constants import\
34    COEX_BAND, COEX_CHANNEL, COEX_POWER_CAP_DBM, KEY_COEX_UNSAFE_CHANNELS, KEY_COEX_RESTRICTIONS
35
36WifiEnums = wutils.WifiEnums
37# Default timeout used for reboot, toggle WiFi and Airplane mode,
38# for the system to settle down after the operation.
39DEFAULT_TIMEOUT = 10
40BAND_2GHZ = 0
41BAND_5GHZ = 1
42
43
44class WifiManagerTest(WifiBaseTest):
45    """Tests for APIs in Android's WifiManager class.
46
47    Test Bed Requirement:
48    * Two Android device
49    * Several Wi-Fi networks visible to the device, including an open Wi-Fi
50      network.
51    """
52
53    def __init__(self, configs):
54        super().__init__(configs)
55        self.enable_packet_log = True
56
57    def setup_class(self):
58        super().setup_class()
59
60        self.dut = self.android_devices[0]
61        wutils.wifi_test_device_init(self.dut)
62        wutils.wifi_toggle_state(self.dut, True)
63
64        self.dut_client = None
65        if len(self.android_devices) > 1:
66            self.dut_client = self.android_devices[1]
67            wutils.wifi_test_device_init(self.dut_client)
68            wutils.wifi_toggle_state(self.dut_client, True)
69
70        req_params = []
71        opt_param = [
72            "open_network", "reference_networks", "iperf_server_address",
73            "wpa_networks", "wep_networks", "iperf_server_port",
74            "coex_unsafe_channels", "coex_restrictions", "wifi6_models"
75        ]
76        self.unpack_userparams(
77            req_param_names=req_params, opt_param_names=opt_param)
78
79        if "AccessPoint" in self.user_params:
80            self.legacy_configure_ap_and_start(wpa_network=True, wep_network=True)
81        elif "OpenWrtAP" in self.user_params:
82            self.configure_openwrt_ap_and_start(open_network=True,
83                                                wpa_network=True,
84                                                wep_network=True)
85
86        asserts.assert_true(
87            len(self.reference_networks) > 0,
88            "Need at least one reference network with psk.")
89        self.wpapsk_2g = self.reference_networks[0]["2g"]
90        self.wpapsk_5g = self.reference_networks[0]["5g"]
91        self.open_network_2g = self.open_network[0]["2g"]
92        self.open_network_5g = self.open_network[0]["5g"]
93
94    def setup_test(self):
95        super().setup_test()
96        for ad in self.android_devices:
97            ad.droid.wakeLockAcquireBright()
98            ad.droid.wakeUpNow()
99        wutils.wifi_toggle_state(self.dut, True)
100
101    def teardown_test(self):
102        super().teardown_test()
103        for ad in self.android_devices:
104            ad.droid.wakeLockRelease()
105            ad.droid.goToSleepNow()
106        self.turn_location_off_and_scan_toggle_off()
107        if self.dut.droid.wifiIsApEnabled():
108            wutils.stop_wifi_tethering(self.dut)
109        for ad in self.android_devices:
110            wutils.reset_wifi(ad)
111        self.log.debug("Toggling Airplane mode OFF")
112        asserts.assert_true(
113            acts.utils.force_airplane_mode(self.dut, False),
114            "Can not turn airplane mode off: %s" % self.dut.serial)
115
116        if self.dut.model in self.user_params["google_pixel_watch_models"]:
117            if wutils.get_wear_wifimediator_disable_status(self.dut):
118                wutils.disable_wear_wifimediator(self.dut, False)
119
120    def teardown_class(self):
121        if "AccessPoint" in self.user_params:
122            del self.user_params["reference_networks"]
123            del self.user_params["open_network"]
124
125    """Helper Functions"""
126
127    def connect_to_wifi_network(self, params):
128        """Connection logic for open and psk wifi networks.
129
130        Args:
131            params: A tuple of network info and AndroidDevice object.
132        """
133        network, ad = params
134        droid = ad.droid
135        ed = ad.ed
136        SSID = network[WifiEnums.SSID_KEY]
137        wutils.start_wifi_connection_scan_and_ensure_network_found(
138            ad, SSID);
139        wutils.wifi_connect(ad, network, num_of_tries=3)
140
141    def get_connection_data(self, dut, network):
142        """Get network id and ssid info from connection data.
143
144        Args:
145            dut: The Android device object under test.
146            network: dict representing the network to connect to.
147
148        Returns:
149            A convenience dict with the connected network's ID and SSID.
150
151        """
152        params = (network, dut)
153        self.connect_to_wifi_network(params)
154        connect_data = dut.droid.wifiGetConnectionInfo()
155        ssid_id_dict = dict()
156        ssid_id_dict[WifiEnums.NETID_KEY] = connect_data[WifiEnums.NETID_KEY]
157        ssid_id_dict[WifiEnums.SSID_KEY] = connect_data[WifiEnums.SSID_KEY]
158        return ssid_id_dict
159
160    def connect_multiple_networks(self, dut):
161        """Connect to one 2.4GHz and one 5Ghz network.
162
163        Args:
164            dut: The Android device object under test.
165
166        Returns:
167            A list with the connection details for the 2GHz and 5GHz networks.
168
169        """
170        network_list = list()
171        connect_2g_data = self.get_connection_data(dut, self.wpapsk_2g)
172        network_list.append(connect_2g_data)
173        connect_5g_data = self.get_connection_data(dut, self.wpapsk_5g)
174        network_list.append(connect_5g_data)
175        return network_list
176
177    def get_enabled_network(self, network1, network2):
178        """Check network status and return currently unconnected network.
179
180        Args:
181            network1: dict representing a network.
182            network2: dict representing a network.
183
184        Return:
185            Network dict of the unconnected network.
186
187        """
188        wifi_info = self.dut.droid.wifiGetConnectionInfo()
189        enabled = network1
190        if wifi_info[WifiEnums.SSID_KEY] == network1[WifiEnums.SSID_KEY]:
191            enabled = network2
192        return enabled
193
194    def check_configstore_networks(self, networks):
195        """Verify that all previously configured networks are presistent after
196           reboot.
197
198        Args:
199            networks: List of network dicts.
200
201        Return:
202            None. Raises TestFailure.
203
204        """
205        network_info = self.dut.droid.wifiGetConfiguredNetworks()
206        if len(network_info) != len(networks):
207            msg = (
208                "Length of configured networks before and after reboot don't"
209                " match. \nBefore reboot = %s \n After reboot = %s" %
210                (networks, network_info))
211            raise signals.TestFailure(msg)
212        # For each network, check if it exists in configured list after reboot
213        current_ssids = set()
214        for network in networks:
215            exists = wutils.match_networks({
216                WifiEnums.SSID_KEY: network[WifiEnums.SSID_KEY]
217            }, network_info)
218            if not len(exists):
219                raise signals.TestFailure("%s network is not present in the"
220                                          " configured list after reboot" %
221                                          network[WifiEnums.SSID_KEY])
222            # Get the new network id for each network after reboot.
223            network[WifiEnums.NETID_KEY] = exists[0]['networkId']
224            if exists[0]['status'] == 'CURRENT':
225                current_ssids.add(network[WifiEnums.SSID_KEY])
226                # At any given point, there can only be one currently active
227                # network, defined with 'status':'CURRENT'
228                if len(current_ssids) > 1:
229                    raise signals.TestFailure("More than one network showing"
230                                              "as 'CURRENT' after reboot")
231
232    def connect_to_wifi_network_with_id(self, network_id, network_ssid):
233        """Connect to the given network using network id and verify SSID.
234
235        Args:
236            network_id: int Network Id of the network.
237            network_ssid: string SSID of the network.
238
239        Returns: True if connect using network id was successful;
240                 False otherwise.
241
242        """
243        wutils.start_wifi_connection_scan_and_ensure_network_found(
244            self.dut, network_ssid);
245        wutils.wifi_connect_by_id(self.dut, network_id)
246        connect_data = self.dut.droid.wifiGetConnectionInfo()
247        connect_ssid = connect_data[WifiEnums.SSID_KEY]
248        self.log.debug("Expected SSID = %s Connected SSID = %s" %
249                       (network_ssid, connect_ssid))
250        if connect_ssid != network_ssid:
251            return False
252        wutils.verify_11ax_wifi_connection(
253            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
254        return True
255
256    def run_iperf_client(self, params):
257        """Run iperf traffic after connection.
258
259        Args:
260            params: A tuple of network info and AndroidDevice object.
261        """
262        if "iperf_server_address" in self.user_params:
263            wait_time = 5
264            network, ad = params
265            SSID = network[WifiEnums.SSID_KEY]
266            self.log.info("Starting iperf traffic through {}".format(SSID))
267            time.sleep(wait_time)
268            port_arg = "-p {}".format(self.iperf_server_port)
269            success, data = ad.run_iperf_client(self.iperf_server_address,
270                                                port_arg)
271            self.log.debug(pprint.pformat(data))
272            asserts.assert_true(success, "Error occurred in iPerf traffic.")
273
274    def connect_to_wifi_network_toggle_wifi_and_run_iperf(self, params):
275        """ Connect to the provided network and then toggle wifi mode and wait
276        for reconnection to the provided network.
277
278        Logic steps are
279        1. Connect to the network.
280        2. Turn wifi off.
281        3. Turn wifi on.
282        4. Wait for connection to the network.
283        5. Run iperf traffic.
284
285        Args:
286            params: A tuple of network info and AndroidDevice object.
287       """
288        network, ad = params
289        self.connect_to_wifi_network(params)
290        wutils.toggle_wifi_and_wait_for_reconnection(
291            ad, network, num_of_tries=5)
292        self.run_iperf_client(params)
293
294    def run_iperf(self, iperf_args):
295        if "iperf_server_address" not in self.user_params:
296            self.log.error(("Missing iperf_server_address. "
297                            "Provide one in config."))
298        else:
299            iperf_addr = self.user_params["iperf_server_address"]
300            self.log.info("Running iperf client.")
301            result, data = self.dut.run_iperf_client(iperf_addr, iperf_args)
302            self.log.debug(data)
303
304    def run_iperf_rx_tx(self, time, omit=10):
305        args = "-p {} -t {} -O 10".format(self.iperf_server_port, time, omit)
306        self.log.info("Running iperf client {}".format(args))
307        self.run_iperf(args)
308        args = "-p {} -t {} -O 10 -R".format(self.iperf_server_port, time,
309                                             omit)
310        self.log.info("Running iperf client {}".format(args))
311        self.run_iperf(args)
312
313    def get_energy_info(self):
314        """ Steps:
315            1. Check that the WiFi energy info reporting support on this device
316               is as expected (support or not).
317            2. If the device does not support energy info reporting as
318               expected, skip the test.
319            3. Call API to get WiFi energy info.
320            4. Verify the values of "ControllerEnergyUsed" and
321               "ControllerIdleTimeMillis" in energy info don't decrease.
322            5. Repeat from Step 3 for 10 times.
323        """
324        # Check if dut supports energy info reporting.
325        actual_support = self.dut.droid.wifiIsEnhancedPowerReportingSupported()
326        model = self.dut.model
327        if not actual_support:
328            asserts.skip(
329                ("Device %s does not support energy info reporting as "
330                 "expected.") % model)
331        # Verify reported values don't decrease.
332        self.log.info(("Device %s supports energy info reporting, verify that "
333                       "the reported values don't decrease.") % model)
334        energy = 0
335        idle_time = 0
336        for i in range(10):
337            info = self.dut.droid.wifiGetControllerActivityEnergyInfo()
338            self.log.debug("Iteration %d, got energy info: %s" % (i, info))
339            new_energy = info["ControllerEnergyUsed"]
340            new_idle_time = info["ControllerIdleTimeMillis"]
341            asserts.assert_true(new_energy >= energy,
342                                "Energy value decreased: previous %d, now %d" %
343                                (energy, new_energy))
344            energy = new_energy
345            asserts.assert_true(new_idle_time >= idle_time,
346                                "Idle time decreased: previous %d, now %d" % (
347                                    idle_time, new_idle_time))
348            idle_time = new_idle_time
349            wutils.start_wifi_connection_scan(self.dut)
350
351    def turn_location_on_and_scan_toggle_on(self):
352        """ Turns on wifi location scans.
353        """
354        acts.utils.set_location_service(self.dut, True)
355        self.dut.droid.wifiScannerToggleAlwaysAvailable(True)
356        msg = "Failed to turn on location service's scan."
357        asserts.assert_true(self.dut.droid.wifiScannerIsAlwaysAvailable(), msg)
358
359    def turn_location_off_and_scan_toggle_off(self):
360        """ Turns off wifi location scans.
361        """
362        acts.utils.set_location_service(self.dut, False)
363        self.dut.droid.wifiScannerToggleAlwaysAvailable(False)
364        msg = "Failed to turn off location service's scan."
365        asserts.assert_true(not self.dut.droid.wifiScannerIsAlwaysAvailable(), msg)
366
367    def turn_location_on_and_scan_toggle_off(self):
368        """ Turns off wifi location scans, but keeps location on.
369        """
370        acts.utils.set_location_service(self.dut, True)
371        self.dut.droid.wifiScannerToggleAlwaysAvailable(False)
372        msg = "Failed to turn off location service's scan."
373        asserts.assert_true(not self.dut.droid.wifiScannerIsAlwaysAvailable(), msg)
374
375    def helper_reconnect_toggle_wifi(self):
376        """Connect to multiple networks, turn off/on wifi, then reconnect to
377           a previously connected network.
378
379        Steps:
380        1. Connect to a 2GHz network.
381        2. Connect to a 5GHz network.
382        3. Turn WiFi OFF/ON.
383        4. Reconnect to the non-current network.
384
385        """
386        connect_2g_data = self.get_connection_data(self.dut, self.wpapsk_2g)
387        connect_5g_data = self.get_connection_data(self.dut, self.wpapsk_5g)
388        wutils.toggle_wifi_off_and_on(self.dut)
389        reconnect_to = self.get_enabled_network(connect_2g_data,
390                                                connect_5g_data)
391        reconnect = self.connect_to_wifi_network_with_id(
392            reconnect_to[WifiEnums.NETID_KEY],
393            reconnect_to[WifiEnums.SSID_KEY])
394        if not reconnect:
395            raise signals.TestFailure("Device did not connect to the correct"
396                                      " network after toggling WiFi.")
397        wutils.verify_11ax_wifi_connection(
398            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
399
400    def helper_reconnect_toggle_airplane(self):
401        """Connect to multiple networks, turn on/off Airplane moce, then
402           reconnect a previously connected network.
403
404        Steps:
405        1. Connect to a 2GHz network.
406        2. Connect to a 5GHz network.
407        3. Turn ON/OFF Airplane mode.
408        4. Reconnect to the non-current network.
409
410        """
411        connect_2g_data = self.get_connection_data(self.dut, self.wpapsk_2g)
412        connect_5g_data = self.get_connection_data(self.dut, self.wpapsk_5g)
413        wutils.toggle_airplane_mode_on_and_off(self.dut)
414        reconnect_to = self.get_enabled_network(connect_2g_data,
415                                                connect_5g_data)
416        reconnect = self.connect_to_wifi_network_with_id(
417            reconnect_to[WifiEnums.NETID_KEY],
418            reconnect_to[WifiEnums.SSID_KEY])
419        if not reconnect:
420            raise signals.TestFailure("Device did not connect to the correct"
421                                      " network after toggling Airplane mode.")
422        wutils.verify_11ax_wifi_connection(
423            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
424
425    def helper_reboot_configstore_reconnect(self, lock_screen=False):
426        """Connect to multiple networks, reboot then reconnect to previously
427           connected network.
428
429        Steps:
430        1. Connect to a 2GHz network.
431        2. Connect to a 5GHz network.
432        3. Reboot device.
433        4. Verify all networks are persistent after reboot.
434        5. Reconnect to the non-current network.
435
436        """
437        network_list = self.connect_multiple_networks(self.dut)
438        network_list = self.dut.droid.wifiGetConfiguredNetworks()
439        self.dut.reboot()
440        time.sleep(DEFAULT_TIMEOUT)
441        self.check_configstore_networks(network_list)
442
443        reconnect_to = self.get_enabled_network(network_list[BAND_2GHZ],
444                                                network_list[BAND_5GHZ])
445
446        if lock_screen:
447            self.dut.droid.wakeLockRelease()
448            self.dut.droid.goToSleepNow()
449        reconnect = self.connect_to_wifi_network_with_id(
450            reconnect_to[WifiEnums.NETID_KEY],
451            reconnect_to[WifiEnums.SSID_KEY])
452        if not reconnect:
453            raise signals.TestFailure(
454                "Device failed to reconnect to the correct"
455                " network after reboot.")
456        wutils.verify_11ax_wifi_connection(
457            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
458
459    def helper_toggle_wifi_reboot_configstore_reconnect(self):
460        """Connect to multiple networks, disable WiFi, reboot, then
461           reconnect to previously connected network.
462
463        Steps:
464        1. Connect to a 2GHz network.
465        2. Connect to a 5GHz network.
466        3. Turn WiFi OFF.
467        4. Reboot device.
468        5. Turn WiFi ON.
469        4. Verify all networks are persistent after reboot.
470        5. Reconnect to the non-current network.
471
472        """
473        network_list = self.connect_multiple_networks(self.dut)
474        self.log.debug("Toggling wifi OFF")
475        wutils.wifi_toggle_state(self.dut, False)
476        time.sleep(DEFAULT_TIMEOUT)
477        network_list = self.dut.droid.wifiGetConfiguredNetworks()
478        self.dut.reboot()
479        time.sleep(DEFAULT_TIMEOUT)
480        self.log.debug("Toggling wifi ON")
481        wutils.wifi_toggle_state(self.dut, True)
482        time.sleep(DEFAULT_TIMEOUT)
483        self.check_configstore_networks(network_list)
484        reconnect_to = self.get_enabled_network(network_list[BAND_2GHZ],
485                                                network_list[BAND_5GHZ])
486        reconnect = self.connect_to_wifi_network_with_id(
487            reconnect_to[WifiEnums.NETID_KEY],
488            reconnect_to[WifiEnums.SSID_KEY])
489        if not reconnect:
490            msg = ("Device failed to reconnect to the correct network after"
491                   " toggling WiFi and rebooting.")
492            raise signals.TestFailure(msg)
493        wutils.verify_11ax_wifi_connection(
494            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
495
496    def helper_toggle_airplane_reboot_configstore_reconnect(self):
497        """Connect to multiple networks, enable Airplane mode, reboot, then
498           reconnect to previously connected network.
499
500        Steps:
501        1. Connect to a 2GHz network.
502        2. Connect to a 5GHz network.
503        3. Toggle Airplane mode ON.
504        4. Reboot device.
505        5. Toggle Airplane mode OFF.
506        4. Verify all networks are persistent after reboot.
507        5. Reconnect to the non-current network.
508
509        """
510        network_list = self.connect_multiple_networks(self.dut)
511        self.log.debug("Toggling Airplane mode ON")
512        asserts.assert_true(
513            acts.utils.force_airplane_mode(self.dut, True),
514            "Can not turn on airplane mode on: %s" % self.dut.serial)
515        time.sleep(DEFAULT_TIMEOUT)
516        network_list = self.dut.droid.wifiGetConfiguredNetworks()
517        self.dut.reboot()
518        time.sleep(DEFAULT_TIMEOUT)
519        self.log.debug("Toggling Airplane mode OFF")
520        asserts.assert_true(
521            acts.utils.force_airplane_mode(self.dut, False),
522            "Can not turn on airplane mode on: %s" % self.dut.serial)
523        time.sleep(DEFAULT_TIMEOUT)
524        self.check_configstore_networks(network_list)
525        reconnect_to = self.get_enabled_network(network_list[BAND_2GHZ],
526                                                network_list[BAND_5GHZ])
527        reconnect = self.connect_to_wifi_network_with_id(
528            reconnect_to[WifiEnums.NETID_KEY],
529            reconnect_to[WifiEnums.SSID_KEY])
530        if not reconnect:
531            msg = ("Device failed to reconnect to the correct network after"
532                   " toggling Airplane mode and rebooting.")
533            raise signals.TestFailure(msg)
534        wutils.verify_11ax_wifi_connection(
535            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
536
537    def verify_traffic_between_devices(self,dest_device,src_device,num_of_tries=2):
538        """Test the clients and DUT can ping each other.
539
540        Args:
541            num_of_tries: the retry times of ping test.
542            dest_device:Test device.
543            src_device:Second DUT access same AP
544        """
545        dest_device = dest_device.droid.connectivityGetIPv4Addresses('wlan0')[0]
546        for _ in range(num_of_tries):
547            if acts.utils.adb_shell_ping(src_device, count=10, dest_ip=dest_device, timeout=20):
548                break
549        else:
550            asserts.fail("Ping to %s from %s failed" % (src_device.serial, dest_device))
551
552    def ping_public_gateway_ip(self):
553        """Ping 8.8.8.8"""
554        try:
555            ping_result = self.dut.adb.shell('ping -w 5 8.8.8.8')
556            if '0%' in ping_result:
557                self.dut.log.info('Ping success')
558            return True
559        except:
560            self.dut.log.error('Faild to ping public gateway 8.8.8.8')
561            return False
562
563    """Tests"""
564
565    @test_tracker_info(uuid="525fc5e3-afba-4bfd-9a02-5834119e3c66")
566    def test_toggle_wifi_state_and_get_startupTime(self):
567        """Test toggling wifi"""
568        self.log.debug("Going from on to off.")
569        wutils.wifi_toggle_state(self.dut, False)
570        self.log.debug("Going from off to on.")
571        startTime = time.time()
572        wutils.wifi_toggle_state(self.dut, True)
573        startup_time = time.time() - startTime
574        self.log.debug("WiFi was enabled on the device in %s s." % startup_time)
575
576    @test_tracker_info(uuid="e9d11563-2bbe-4c96-87eb-ec919b51435b")
577    def test_toggle_with_screen(self):
578        """Test toggling wifi with screen on/off"""
579        wait_time = 5
580        self.log.debug("Screen from off to on.")
581        self.dut.droid.wakeLockAcquireBright()
582        self.dut.droid.wakeUpNow()
583        time.sleep(wait_time)
584        self.log.debug("Going from on to off.")
585        try:
586            wutils.wifi_toggle_state(self.dut, False)
587            time.sleep(wait_time)
588            self.log.debug("Going from off to on.")
589            wutils.wifi_toggle_state(self.dut, True)
590        finally:
591            self.dut.droid.wakeLockRelease()
592            time.sleep(wait_time)
593            self.dut.droid.goToSleepNow()
594
595    @test_tracker_info(uuid="71556e06-7fb1-4e2b-9338-b01f1f8e286e")
596    def test_scan(self):
597        """Test wifi connection scan can start and find expected networks."""
598        ssid = self.open_network_2g[WifiEnums.SSID_KEY]
599        wutils.start_wifi_connection_scan_and_ensure_network_found(
600            self.dut, ssid)
601        ssid = self.open_network_5g[WifiEnums.SSID_KEY]
602        wutils.start_wifi_connection_scan_and_ensure_network_found(
603            self.dut, ssid)
604
605    @test_tracker_info(uuid="3ea09efb-6921-429e-afb1-705ef5a09afa")
606    def test_scan_with_wifi_off_and_location_scan_on(self):
607        """Put wifi in scan only mode"""
608        self.turn_location_on_and_scan_toggle_on()
609        wutils.wifi_toggle_state(self.dut, False)
610
611        """Test wifi connection scan can start and find expected networks."""
612        ssid = self.open_network_2g[WifiEnums.SSID_KEY]
613        wutils.start_wifi_connection_scan_and_ensure_network_found(
614            self.dut, ssid)
615        ssid = self.open_network_5g[WifiEnums.SSID_KEY]
616        wutils.start_wifi_connection_scan_and_ensure_network_found(
617            self.dut, ssid)
618
619    @test_tracker_info(uuid="558652de-c802-405f-b9dc-b7fcc9237673")
620    def test_scan_after_reboot_with_wifi_off_and_location_scan_on(self):
621        """Put wifi in scan only mode"""
622        self.turn_location_on_and_scan_toggle_on()
623        wutils.wifi_toggle_state(self.dut, False)
624
625        # Reboot the device.
626        self.dut.reboot()
627        time.sleep(DEFAULT_TIMEOUT)
628
629        """Test wifi connection scan can start and find expected networks."""
630        ssid = self.open_network_2g[WifiEnums.SSID_KEY]
631        wutils.start_wifi_connection_scan_and_ensure_network_found(
632            self.dut, ssid)
633        ssid = self.open_network_5g[WifiEnums.SSID_KEY]
634        wutils.start_wifi_connection_scan_and_ensure_network_found(
635            self.dut, ssid)
636
637    @test_tracker_info(uuid="770caebe-bcb1-43ac-95b6-5dd52dd90e80")
638    def test_scan_with_wifi_off_and_location_scan_off(self):
639        """Turn off wifi and location scan"""
640        self.turn_location_on_and_scan_toggle_off()
641
642        if self.dut.model in self.user_params["google_pixel_watch_models"]:
643            wutils.disable_wear_wifimediator(self.dut, True)
644
645        wutils.wifi_toggle_state(self.dut, False)
646
647        """Test wifi connection scan should fail."""
648        self.dut.droid.wifiStartScan()
649        try:
650            self.dut.ed.pop_event("WifiManagerScanResultsAvailable", 60)
651        except queue.Empty:
652            self.log.debug("Wifi scan results not received.")
653        else:
654            asserts.fail("Wi-Fi scan results received")
655
656    @test_tracker_info(uuid="a4ad9930-a8fa-4868-81ed-a79c7483e502")
657    def test_add_network(self):
658        """Test wifi connection scan."""
659        ssid = self.open_network_2g[WifiEnums.SSID_KEY]
660        nId = self.dut.droid.wifiAddNetwork(self.open_network_2g)
661        asserts.assert_true(nId > -1, "Failed to add network.")
662        configured_networks = self.dut.droid.wifiGetConfiguredNetworks()
663        self.log.debug(
664            ("Configured networks after adding: %s" % configured_networks))
665        wutils.assert_network_in_list({
666            WifiEnums.SSID_KEY: ssid
667        }, configured_networks)
668
669    @test_tracker_info(uuid="aca85551-10ba-4007-90d9-08bcdeb16a60")
670    def test_forget_network(self):
671        ssid = self.open_network_2g[WifiEnums.SSID_KEY]
672        nId = self.dut.droid.wifiAddNetwork(self.open_network_2g)
673        asserts.assert_true(nId > -1, "Failed to add network.")
674        configured_networks = self.dut.droid.wifiGetConfiguredNetworks()
675        self.log.debug(
676            ("Configured networks after adding: %s" % configured_networks))
677        wutils.assert_network_in_list({
678            WifiEnums.SSID_KEY: ssid
679        }, configured_networks)
680        wutils.wifi_forget_network(self.dut, ssid)
681        configured_networks = self.dut.droid.wifiGetConfiguredNetworks()
682        for nw in configured_networks:
683            asserts.assert_true(
684                nw[WifiEnums.BSSID_KEY] != ssid,
685                "Found forgotten network %s in configured networks." % ssid)
686
687    @test_tracker_info(uuid="3cff17f6-b684-4a95-a438-8272c2ad441d")
688    def test_reconnect_to_previously_connected(self):
689        """Connect to multiple networks and reconnect to the previous network.
690
691        Steps:
692        1. Connect to a 2GHz network.
693        2. Connect to a 5GHz network.
694        3. Reconnect to the 2GHz network using its network id.
695        4. Reconnect to the 5GHz network using its network id.
696
697        """
698        connect_2g_data = self.get_connection_data(self.dut, self.wpapsk_2g)
699        connect_5g_data = self.get_connection_data(self.dut, self.wpapsk_5g)
700        reconnect_2g = self.connect_to_wifi_network_with_id(
701            connect_2g_data[WifiEnums.NETID_KEY],
702            connect_2g_data[WifiEnums.SSID_KEY])
703        if not reconnect_2g:
704            raise signals.TestFailure("Device did not connect to the correct"
705                                      " 2GHz network.")
706        reconnect_5g = self.connect_to_wifi_network_with_id(
707            connect_5g_data[WifiEnums.NETID_KEY],
708            connect_5g_data[WifiEnums.SSID_KEY])
709        if not reconnect_5g:
710            raise signals.TestFailure("Device did not connect to the correct"
711                                      " 5GHz network.")
712
713    @test_tracker_info(uuid="334175c3-d26a-4c87-a8ab-8eb220b2d80f")
714    def test_reconnect_toggle_wifi(self):
715        """Connect to multiple networks, turn off/on wifi, then reconnect to
716           a previously connected network.
717
718        Steps:
719        1. Connect to a 2GHz network.
720        2. Connect to a 5GHz network.
721        3. Turn WiFi OFF/ON.
722        4. Reconnect to the non-current network.
723
724        """
725        self.helper_reconnect_toggle_wifi()
726
727    @test_tracker_info(uuid="bd2cec9e-7f17-44ef-8a0c-4da92a9b55ae")
728    def test_reconnect_toggle_wifi_with_location_scan_on(self):
729        """Connect to multiple networks, turn off/on wifi, then reconnect to
730           a previously connected network.
731
732        Steps:
733        1. Turn on location scans.
734        2. Connect to a 2GHz network.
735        3. Connect to a 5GHz network.
736        4. Turn WiFi OFF/ON.
737        5. Reconnect to the non-current network.
738
739        """
740        self.turn_location_on_and_scan_toggle_on()
741        self.helper_reconnect_toggle_wifi()
742
743    @test_tracker_info(uuid="8e6e6c21-fefb-4fe8-9fb1-f09b1182b76d")
744    def test_reconnect_toggle_airplane(self):
745        """Connect to multiple networks, turn on/off Airplane moce, then
746           reconnect a previously connected network.
747
748        Steps:
749        1. Connect to a 2GHz network.
750        2. Connect to a 5GHz network.
751        3. Turn ON/OFF Airplane mode.
752        4. Reconnect to the non-current network.
753
754        """
755        self.helper_reconnect_toggle_airplane()
756
757    @test_tracker_info(uuid="28562f13-8a0a-492e-932c-e587560db5f2")
758    def test_reconnect_toggle_airplane_with_location_scan_on(self):
759        """Connect to multiple networks, turn on/off Airplane moce, then
760           reconnect a previously connected network.
761
762        Steps:
763        1. Turn on location scans.
764        2. Connect to a 2GHz network.
765        3. Connect to a 5GHz network.
766        4. Turn ON/OFF Airplane mode.
767        5. Reconnect to the non-current network.
768
769        """
770        self.turn_location_on_and_scan_toggle_on()
771        self.helper_reconnect_toggle_airplane()
772
773    @test_tracker_info(uuid="52b89a47-f260-4343-922d-fbeb4d8d2b63")
774    def test_reconnect_toggle_wifi_on_with_airplane_on(self):
775        """Connect to multiple networks, turn on airplane mode, turn on wifi,
776        then reconnect a previously connected network.
777
778        Steps:
779        1. Connect to a 2GHz network.
780        2. Connect to a 5GHz network.
781        3. Turn ON Airplane mode.
782        4. Turn ON WiFi.
783        5. Reconnect to the a previously connected network.
784        """
785        connect_2g_data = self.get_connection_data(self.dut, self.wpapsk_2g)
786        connect_5g_data = self.get_connection_data(self.dut, self.wpapsk_5g)
787        self.log.debug("Toggling Airplane mode ON")
788        asserts.assert_true(
789            acts.utils.force_airplane_mode(self.dut, True),
790            "Can not turn on airplane mode on: %s" % self.dut.serial)
791        self.log.debug("Toggling wifi ON")
792        wutils.wifi_toggle_state(self.dut, True)
793        time.sleep(DEFAULT_TIMEOUT)
794        reconnect_to = self.get_enabled_network(connect_2g_data,
795                                                connect_5g_data)
796        reconnect = self.connect_to_wifi_network_with_id(
797            reconnect_to[WifiEnums.NETID_KEY],
798            reconnect_to[WifiEnums.SSID_KEY])
799        if not reconnect:
800            raise signals.TestFailure("Device did not connect to the correct"
801                                      " network after toggling WiFi.")
802
803    @test_tracker_info(uuid="2dddc734-e9f6-4d30-9c2d-4368e721a350")
804    def test_verify_airplane_mode_on_with_wifi_disabled(self):
805        """Connect to multiple networks, turn on airplane mode, turn off Wi-Fi,
806        then make sure there is no internet.
807
808        Steps:
809        1. Connect to a 2GHz network.
810        2. Connect to a 5GHz network.
811        3. Turn ON Airplane mode.
812        4. Turn OFF WiFi.
813        5. Ping to make sure there is no internet
814        """
815        connect_2g_data = self.get_connection_data(self.dut, self.wpapsk_2g)
816        connect_5g_data = self.get_connection_data(self.dut, self.wpapsk_5g)
817        self.log.debug("Toggling Airplane mode ON")
818        asserts.assert_true(
819            acts.utils.force_airplane_mode(self.dut, True),
820            "Can not turn on airplane mode on: %s" % self.dut.serial)
821        self.log.debug("Toggling Wi-Fi OFF")
822        wutils.wifi_toggle_state(self.dut, False)
823        time.sleep(DEFAULT_TIMEOUT)
824        if self.ping_public_gateway_ip():
825            raise signals.TestFailure("Device has internet after"
826                                             " toggling WiFi off.")
827
828    @test_tracker_info(uuid="3d041c12-05e2-46a7-ab9b-e3f60cc735db")
829    def test_reboot_configstore_reconnect(self):
830        """Connect to multiple networks, reboot then reconnect to previously
831           connected network.
832
833        Steps:
834        1. Connect to a 2GHz network.
835        2. Connect to a 5GHz network.
836        3. Reboot device.
837        4. Verify all networks are persistent after reboot.
838        5. Reconnect to the non-current network.
839
840        """
841        self.helper_reboot_configstore_reconnect()
842
843    @test_tracker_info(uuid="a70d5853-67b5-4d48-bdf7-08ee51fafd21")
844    def test_reboot_configstore_reconnect_with_location_scan_on(self):
845        """Connect to multiple networks, reboot then reconnect to previously
846           connected network.
847
848        Steps:
849        1. Turn on location scans.
850        2. Connect to a 2GHz network.
851        3. Connect to a 5GHz network.
852        4. Reboot device.
853        5. Verify all networks are persistent after reboot.
854        6. Reconnect to the non-current network.
855
856        """
857        self.turn_location_on_and_scan_toggle_on()
858        self.helper_reboot_configstore_reconnect()
859
860    @test_tracker_info(uuid="26d94dfa-1349-4c8b-aea0-475eb73bb521")
861    def test_toggle_wifi_reboot_configstore_reconnect(self):
862        """Connect to multiple networks, disable WiFi, reboot, then
863           reconnect to previously connected network.
864
865        Steps:
866        1. Connect to a 2GHz network.
867        2. Connect to a 5GHz network.
868        3. Turn WiFi OFF.
869        4. Reboot device.
870        5. Turn WiFi ON.
871        4. Verify all networks are persistent after reboot.
872        5. Reconnect to the non-current network.
873
874        """
875        self.helper_toggle_wifi_reboot_configstore_reconnect()
876
877    @test_tracker_info(uuid="7c004a3b-c1c6-4371-9124-0f34650be915")
878    def test_toggle_wifi_reboot_configstore_reconnect_with_location_scan_on(self):
879        """Connect to multiple networks, disable WiFi, reboot, then
880           reconnect to previously connected network.
881
882        Steps:
883        1. Turn on location scans.
884        2. Connect to a 2GHz network.
885        3. Connect to a 5GHz network.
886        4. Turn WiFi OFF.
887        5. Reboot device.
888        6. Turn WiFi ON.
889        7. Verify all networks are persistent after reboot.
890        8. Reconnect to the non-current network.
891
892        """
893        self.turn_location_on_and_scan_toggle_on()
894        self.helper_toggle_wifi_reboot_configstore_reconnect()
895
896    @test_tracker_info(uuid="4fce017b-b443-40dc-a598-51d59d3bb38f")
897    def test_toggle_airplane_reboot_configstore_reconnect(self):
898        """Connect to multiple networks, enable Airplane mode, reboot, then
899           reconnect to previously connected network.
900
901        Steps:
902        1. Connect to a 2GHz network.
903        2. Connect to a 5GHz network.
904        3. Toggle Airplane mode ON.
905        4. Reboot device.
906        5. Toggle Airplane mode OFF.
907        4. Verify all networks are persistent after reboot.
908        5. Reconnect to the non-current network.
909
910        """
911        self.helper_toggle_airplane_reboot_configstore_reconnect()
912
913    @test_tracker_info(uuid="7f0810f9-2338-4158-95f5-057f5a1905b6")
914    def test_toggle_airplane_reboot_configstore_reconnect_with_location_scan_on(self):
915        """Connect to multiple networks, enable Airplane mode, reboot, then
916           reconnect to previously connected network.
917
918        Steps:
919        1. Turn on location scans.
920        2. Connect to a 2GHz network.
921        3. Connect to a 5GHz network.
922        4. Toggle Airplane mode ON.
923        5. Reboot device.
924        6. Toggle Airplane mode OFF.
925        7. Verify all networks are persistent after reboot.
926        8. Reconnect to the non-current network.
927
928        """
929        self.turn_location_on_and_scan_toggle_on()
930        self.helper_toggle_airplane_reboot_configstore_reconnect()
931
932    @test_tracker_info(uuid="342c13cb-6508-4942-bee3-07c5d20d92a5")
933    def test_reboot_configstore_reconnect_with_screen_lock(self):
934        """Verify device can re-connect to configured networks after reboot.
935
936        Steps:
937        1. Connect to 2G and 5G networks.
938        2. Reboot device
939        3. Verify device connects to 1 network automatically.
940        4. Lock screen and verify device can connect to the other network.
941        """
942        self.helper_reboot_configstore_reconnect(lock_screen=True)
943
944    @test_tracker_info(uuid="7e6050d9-79b1-4726-80cf-686bb99b8945")
945    def test_connect_to_5g_after_reboot_without_unlock(self):
946        """Connect to 5g network afer reboot without unlock.
947
948        Steps:
949        1. Reboot device and lock screen
950        2. Connect to 5G network and verify it works.
951        """
952        self.dut.reboot()
953        time.sleep(DEFAULT_TIMEOUT)
954        self.dut.droid.wakeLockRelease()
955        self.dut.droid.goToSleepNow()
956        wutils.connect_to_wifi_network(self.dut, self.wpapsk_5g)
957        wutils.verify_11ax_wifi_connection(
958            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
959
960    @test_tracker_info(uuid="81eb7527-4c92-4422-897a-6b5f6445e84a")
961    def test_config_store_with_wpapsk_2g(self):
962        self.connect_to_wifi_network_toggle_wifi_and_run_iperf(
963            (self.wpapsk_2g, self.dut))
964        wutils.verify_11ax_wifi_connection(
965            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
966
967    @test_tracker_info(uuid="8457903d-cb7e-4c89-bcea-7f59585ea6e0")
968    def test_config_store_with_wpapsk_5g(self):
969        self.connect_to_wifi_network_toggle_wifi_and_run_iperf(
970            (self.wpapsk_5g, self.dut))
971        wutils.verify_11ax_wifi_connection(
972            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
973
974    @test_tracker_info(uuid="b9fbc13a-47b4-4f64-bd2c-e5a3cb24ab2f")
975    def test_tdls_supported(self):
976        model = self.dut.model
977        self.log.debug("Model is %s" % model)
978        if not model.startswith("volantis"):
979            asserts.assert_true(self.dut.droid.wifiIsTdlsSupported(), (
980                "TDLS should be supported on %s, but device is "
981                "reporting not supported.") % model)
982        else:
983            asserts.assert_false(self.dut.droid.wifiIsTdlsSupported(), (
984                "TDLS should not be supported on %s, but device "
985                "is reporting supported.") % model)
986
987    @test_tracker_info(uuid="50637d40-ea59-4f4b-9fc1-e6641d64074c")
988    def test_energy_info(self):
989        """Verify the WiFi energy info reporting feature """
990        self.get_energy_info()
991
992    @test_tracker_info(uuid="1f1cf549-53eb-4f36-9f33-ce06c9158efc")
993    def test_energy_info_connected(self):
994        """Verify the WiFi energy info reporting feature when connected.
995
996        Connect to a wifi network, then the same as test_energy_info.
997        """
998        wutils.wifi_connect(self.dut, self.open_network_2g)
999        self.get_energy_info()
1000        wutils.verify_11ax_wifi_connection(
1001            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1002
1003    @test_tracker_info(uuid="2622c253-defc-4a35-93a6-ca9d29a8238c")
1004    def test_connect_to_wep_2g(self):
1005        """Verify DUT can connect to 2GHz WEP network
1006
1007        Steps:
1008        1. Ensure the 2GHz WEP network is visible in scan result.
1009        2. Connect to the network and validate internet connection.
1010        """
1011        wutils.connect_to_wifi_network(self.dut, self.wep_networks[0]["2g"])
1012
1013    @test_tracker_info(uuid="1f2d17a2-e92d-43af-966b-3421c0db8620")
1014    def test_connect_to_wep_5g(self):
1015        """Verify DUT can connect to 5GHz WEP network
1016
1017        Steps:
1018        1. Ensure the 5GHz WEP network is visible in scan result.
1019        2. Connect to the network and validate internet connection.
1020        """
1021        wutils.connect_to_wifi_network(self.dut, self.wep_networks[0]["5g"])
1022
1023    @test_tracker_info(uuid="4a957952-289d-4657-9882-e1475274a7ff")
1024    def test_connect_to_wpa_2g(self):
1025        """Verify DUT can connect to 2GHz WPA-PSK network
1026
1027        Steps:
1028        1. Ensure the 2GHz WPA-PSK network is visible in scan result.
1029        2. Connect to the network and validate internet connection.
1030        """
1031        wutils.connect_to_wifi_network(self.dut, self.wpa_networks[0]["2g"])
1032        wutils.verify_11ax_wifi_connection(
1033            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1034
1035    @test_tracker_info(uuid="612c3c31-a4c5-4014-9a2d-3f4bcc20c0d7")
1036    def test_connect_to_wpa_5g(self):
1037        """Verify DUT can connect to 5GHz WPA-PSK network
1038
1039        Steps:
1040        1. Ensure the 5GHz WPA-PSK network is visible in scan result.
1041        2. Connect to the network and validate internet connection.
1042        """
1043        wutils.connect_to_wifi_network(self.dut, self.wpa_networks[0]["5g"])
1044        wutils.verify_11ax_wifi_connection(
1045            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1046
1047    @test_tracker_info(uuid="2a617fb4-1d8e-44e9-a500-a5456e1df83f")
1048    def test_connect_to_2g_can_be_pinged(self):
1049        """Verify DUT can be pinged by another device when it connects to 2GHz AP
1050
1051        Steps:
1052        1. Ensure the 2GHz WPA-PSK network is visible in scan result.
1053        2. Connect to the network and validate internet connection.
1054        3. Check DUT can be pinged by another device
1055        """
1056        wutils.connect_to_wifi_network(self.dut, self.wpa_networks[0]["2g"])
1057        wutils.verify_11ax_wifi_connection(
1058            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1059        wutils.connect_to_wifi_network(self.dut_client, self.wpa_networks[0]["2g"])
1060        wutils.verify_11ax_wifi_connection(
1061            self.dut_client, self.wifi6_models, "wifi6_ap" in self.user_params)
1062        self.verify_traffic_between_devices(self.dut,self.dut_client)
1063
1064    @test_tracker_info(uuid="94bdd657-649b-4a2c-89c3-3ec6ba18e08e")
1065    def test_connect_to_5g_can_be_pinged(self):
1066        """Verify DUT can be pinged by another device when it connects to 5GHz AP
1067
1068        Steps:
1069        1. Ensure the 5GHz WPA-PSK network is visible in scan result.
1070        2. Connect to the network and validate internet connection.
1071        3. Check DUT can be pinged by another device
1072        """
1073        wutils.connect_to_wifi_network(self.dut, self.wpa_networks[0]["5g"])
1074        wutils.verify_11ax_wifi_connection(
1075            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1076        wutils.connect_to_wifi_network(self.dut_client, self.wpa_networks[0]["5g"])
1077        wutils.verify_11ax_wifi_connection(
1078            self.dut_client, self.wifi6_models, "wifi6_ap" in self.user_params)
1079        self.verify_traffic_between_devices(self.dut,self.dut_client)
1080
1081    @test_tracker_info(uuid="d87359aa-c4da-4554-b5de-8e3fa852a6b0")
1082    def test_sta_turn_off_screen_can_be_pinged(self):
1083        """Verify DUT can be pinged by another device after idle for a while
1084
1085        Steps:
1086        1. Ensure the 2GHz WPA-PSK network is visible in scan result.
1087        2. DUT and DUT_Client connect to the network and validate internet connection.
1088        3. Let DUT sleep for 5 minutes
1089        4. Check DUT can be pinged by DUT_Client
1090        """
1091        asserts.skip_if(len(self.android_devices) < 3, "Need 3 devices")
1092        self.dut_client_a = self.android_devices[1]
1093        self.dut_client_b = self.android_devices[2]
1094
1095        # enable hotspot on dut and connect client devices to it
1096        ap_ssid = "softap_" + acts.utils.rand_ascii_str(8)
1097        ap_password = acts.utils.rand_ascii_str(8)
1098        self.dut.log.info("softap setup: %s %s", ap_ssid, ap_password)
1099        config = {wutils.WifiEnums.SSID_KEY: ap_ssid}
1100        config[wutils.WifiEnums.PWD_KEY] = ap_password
1101        wutils.start_wifi_tethering(
1102            self.dut,
1103            config[wutils.WifiEnums.SSID_KEY],
1104            config[wutils.WifiEnums.PWD_KEY],
1105            wutils.WifiEnums.WIFI_CONFIG_APBAND_AUTO)
1106
1107        # DUT connect to AP
1108        wutils.connect_to_wifi_network(
1109            self.dut_client_a, config, check_connectivity=False)
1110        wutils.connect_to_wifi_network(
1111            self.dut_client_b, config, check_connectivity=False)
1112        # Check DUT and DUT_Client can ping each other successfully
1113        self.verify_traffic_between_devices(self.dut_client_a,
1114                                            self.dut_client_b)
1115        self.verify_traffic_between_devices(self.dut_client_a,
1116                                            self.dut_client_b)
1117
1118        # DUT turn off screen and go sleep for 5 mins
1119        self.dut.droid.wakeLockRelease()
1120        self.dut.droid.goToSleepNow()
1121        # TODO(hsiuchangchen): find a way to check system already suspended
1122        #                      instead of waiting 5 mins
1123        self.log.info("Sleep for 5 minutes")
1124        time.sleep(300)
1125        # Verify DUT_Client can ping DUT when DUT sleeps
1126        self.verify_traffic_between_devices(self.dut_client_a,
1127                                            self.dut_client_b)
1128        self.dut.droid.wakeLockAcquireBright()
1129        self.dut.droid.wakeUpNow()
1130
1131    @test_tracker_info(uuid="25e8dd62-ae9f-46f7-96aa-030fee95dfda")
1132    def test_wifi_saved_network_reset(self):
1133        """Verify DUT can reset Wi-Fi saved network list after add a network.
1134
1135        Steps:
1136        1. Connect to a 2GHz network
1137        2. Reset the Wi-Fi saved network
1138        3. Verify the saved network has been clear
1139        """
1140        ssid = self.open_network_2g[WifiEnums.SSID_KEY]
1141        nId = self.dut.droid.wifiAddNetwork(self.open_network_2g)
1142        asserts.assert_true(nId > -1, "Failed to add network.")
1143        configured_networks = self.dut.droid.wifiGetConfiguredNetworks()
1144        self.log.debug(
1145            ("Configured networks after adding: %s" % configured_networks))
1146        wutils.assert_network_in_list({
1147            WifiEnums.SSID_KEY: ssid
1148        }, configured_networks)
1149        self.dut.droid.wifiFactoryReset()
1150        configured_networks = self.dut.droid.wifiGetConfiguredNetworks()
1151        for nw in configured_networks:
1152            asserts.assert_true(
1153                nw[WifiEnums.BSSID_KEY] != ssid,
1154                "Found saved network %s in configured networks." % ssid)
1155
1156    @test_tracker_info(uuid="402cfaa8-297f-4865-9e27-6bab6adca756")
1157    def test_reboot_wifi_and_bluetooth_on(self):
1158        """Toggle WiFi and bluetooth ON then reboot """
1159        wutils.wifi_toggle_state(self.dut, True)
1160        enable_bluetooth(self.dut.droid, self.dut.ed)
1161
1162        self.dut.reboot()
1163        time.sleep(DEFAULT_TIMEOUT)
1164
1165        asserts.assert_true(self.dut.droid.bluetoothCheckState(),
1166                "bluetooth state changed after reboot")
1167        asserts.assert_true(self.dut.droid.wifiCheckState(),
1168                "wifi state changed after reboot")
1169
1170        disable_bluetooth(self.dut.droid)
1171
1172    @test_tracker_info(uuid="d0e14a2d-a28f-4551-8988-1e15d9d8bb1a")
1173    def test_scan_result_api(self):
1174        """Register scan result callback, start scan and wait for event"""
1175        self.dut.ed.clear_all_events()
1176        self.dut.droid.wifiStartScanWithListener()
1177        try:
1178            events = self.dut.ed.pop_events(
1179                "WifiManagerScanResultsCallbackOnSuccess", 60)
1180        except queue.Empty:
1181            asserts.fail(
1182                "Wi-Fi scan results did not become available within 60s.")
1183
1184    @test_tracker_info(uuid="03cfbc86-7fcc-48d8-ab0f-1f6f3523e596")
1185    def test_enable_disable_auto_join_saved_network(self):
1186        """
1187        Add a saved network, simulate user change the auto join to false, ensure the device doesn't
1188        auto connect to this network
1189
1190        Steps:
1191        1. Create a saved network.
1192        2. Add this saved network, and ensure we connect to this network
1193        3. Simulate user change the auto join to false.
1194        4. Toggle the Wifi off and on
1195        4. Ensure device doesn't connect to his network
1196        """
1197        network = self.open_network_5g
1198        wutils.connect_to_wifi_network(self.dut, network)
1199        wutils.verify_11ax_wifi_connection(
1200            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1201        info = self.dut.droid.wifiGetConnectionInfo()
1202        network_id = info[WifiEnums.NETID_KEY]
1203        self.dut.log.info("Disable auto join on network")
1204        self.dut.droid.wifiEnableAutojoin(network_id, False)
1205        wutils.wifi_toggle_state(self.dut, False)
1206        wutils.wifi_toggle_state(self.dut, True)
1207        asserts.assert_false(
1208            wutils.wait_for_connect(self.dut, network[WifiEnums.SSID_KEY],
1209                                    assert_on_fail=False), "Device should not connect.")
1210        self.dut.droid.wifiEnableAutojoin(network_id, True)
1211        wutils.wait_for_connect(self.dut, network[WifiEnums.SSID_KEY], assert_on_fail=False)
1212        wutils.verify_11ax_wifi_connection(
1213            self.dut, self.wifi6_models, "wifi6_ap" in self.user_params)
1214
1215    def coex_unsafe_channel_key(self, unsafe_channel):
1216        if COEX_POWER_CAP_DBM in unsafe_channel:
1217            return (unsafe_channel[COEX_BAND], unsafe_channel[COEX_CHANNEL],
1218                    unsafe_channel[COEX_POWER_CAP_DBM])
1219        return (unsafe_channel[COEX_BAND], unsafe_channel[COEX_CHANNEL])
1220
1221    @test_tracker_info(uuid="78558b30-3792-4a1f-bb56-34bbbbce6ac8")
1222    def test_set_get_coex_unsafe_channels(self):
1223        """
1224        Set the unsafe channels to avoid for coex, then retrieve the active values and compare to
1225        values set. If the default algorithm is enabled, then ensure that the active values are
1226        unchanged.
1227
1228        Steps:
1229        1. Register a coex callback and listen for the update event to get the current coex values.
1230        2. Create list of coex unsafe channels and restrictions
1231
1232            coex_unsafe_channels format:
1233                [
1234                    {
1235                        "band": <"24_GHZ" or "5_GHZ">
1236                        "channel" : <Channel number>
1237                        (Optional) "powerCapDbm" : <Power Cap in Dbm>
1238                    }
1239                    ...
1240                ]
1241
1242            coex_restrictions format:
1243                [
1244                    (Optional) "WIFI_DIRECT",
1245                    (Optional) "SOFTAP",
1246                    (Optional) "WIFI_AWARE"
1247                ]
1248        3. Set these values as the active values and listen for the update event.
1249        4. If the default algorithm is enabled, expect to not get the update event. If it is
1250           disabled, compare the updated values and see if they are the same as the provided values.
1251        5. Restore the previous values if the test values were set.
1252        """
1253        asserts.skip_if(not self.dut.droid.isSdkAtLeastS(),
1254                        "Require SDK at least S to use wifi coex apis.")
1255        self.dut.ed.clear_all_events()
1256
1257        # Register a coex callback to start receiving coex events
1258        self.dut.droid.wifiRegisterCoexCallback()
1259        try:
1260            # Wait for the immediate callback from registering and store the current values
1261            event = self.dut.ed.pop_event("WifiManagerCoexCallback#onCoexUnsafeChannelsChanged", 5)
1262        except queue.Empty:
1263            asserts.fail("Coex callback event not received after registering.")
1264        prev_unsafe_channels = sorted(json.loads(event["data"][KEY_COEX_UNSAFE_CHANNELS]),
1265                                      key=self.coex_unsafe_channel_key)
1266        prev_restrictions = sorted(json.loads(event["data"][KEY_COEX_RESTRICTIONS]))
1267
1268        # Set new values for coex unsafe channels
1269        test_unsafe_channels = sorted(self.coex_unsafe_channels,
1270                                      key=self.coex_unsafe_channel_key)
1271        test_restrictions = sorted(self.coex_restrictions)
1272        self.dut.droid.wifiSetCoexUnsafeChannels(test_unsafe_channels, test_restrictions)
1273        try:
1274            # Wait for the callback from setting the coex unsafe channels
1275            event = self.dut.ed.pop_event("WifiManagerCoexCallback#onCoexUnsafeChannelsChanged", 5)
1276            # Callback received. This should be expected only if default algo is disabled.
1277            asserts.assert_false(self.dut.droid.wifiIsDefaultCoexAlgorithmEnabled(),
1278                                "Default algo was enabled but Coex callback received after"
1279                                " setCoexUnsafeChannels")
1280            curr_unsafe_channels = sorted(json.loads(event["data"][KEY_COEX_UNSAFE_CHANNELS]),
1281                                          key=self.coex_unsafe_channel_key)
1282            curr_restrictions = sorted(json.loads(event["data"][KEY_COEX_RESTRICTIONS]))
1283            # Compare the current values with the set values
1284            asserts.assert_true(curr_unsafe_channels == test_unsafe_channels,
1285                                "default coex algorithm disabled but current unsafe channels "
1286                                + str(curr_unsafe_channels)
1287                                + " do not match the set values " + str(test_unsafe_channels))
1288            asserts.assert_true(curr_restrictions == test_restrictions,
1289                                "default coex algorithm disabled but current restrictions "
1290                                + str(curr_restrictions)
1291                                + " do not match the set values " + str(test_restrictions))
1292            # Restore the previous values
1293            self.dut.droid.wifiSetCoexUnsafeChannels(prev_unsafe_channels, prev_restrictions)
1294        except queue.Empty:
1295            # Callback not received. This should be expected only if the default algo is enabled.
1296            asserts.assert_true(self.dut.droid.wifiIsDefaultCoexAlgorithmEnabled(),
1297                                "Default algo was disabled but Coex callback not received after"
1298                                " setCoexUnsafeChannels")
1299
1300        self.dut.droid.wifiUnregisterCoexCallback()
1301