blob: b9c1b7f7cc4fb568cc3f8cd63fc3b7973666076c [file] [log] [blame]
# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
from autotest_lib.client.cros.networking.chrome_testing \
import chrome_networking_test_context as cntc
from autotest_lib.client.cros.networking.chrome_testing \
import chrome_networking_test_api as cnta
from autotest_lib.client.cros.networking.chrome_testing import test_utils
from collections import namedtuple
NetworkInfo = namedtuple('NetworkInfo', 'name guid connectionState security')
class network_ChromeWifiEndToEnd(test.test):
Tests the following with chrome.networkingPrivate APIs:
1. Tests that the configured wifi networks are seen by Chrome.
2. Tests the transitioning between various available networks.
3. Tests that the enabling and disabling WiFi works.
4. Tests that the DUT autoconnects to previously connected WiFi network.
version = 1
def _extract_wifi_network_info(self, networks_found):
"""Extract the needed information from the list of networks found
via API.
@param networks_found: Networks found via getVisibleNetworks api.
@return Formated list of available wifi networks.
network_list = []
for network in networks_found:
network = NetworkInfo(name=network['Name'],
return network_list
def _wifi_network_comparison(
self, configured_service_name_list, wifi_name_list):
"""Compare the known wifi SSID's against the SSID's returned via API.
@param configured_service_name_list: Known SSID's that are configured
by the network_WiFi_ChromeEndToEnd test.
@param wifi_name_list: List of SSID's returned by the
getVisibleNetworks API.
@raises error.TestFail if network names do not match.
for name in configured_service_name_list:
if name not in wifi_name_list:
raise error.TestFail(
'Following network does not match: %s' % name)'Network names match!')
def _enable_disable_network_check(
self, original_enabled_networks, new_enabled_networks):
"""Tests enabling and disabling of WiFi.
@param original_enabled_networks: List of network devices that were
enabled when the test started.
@param new_enabled_networks: List of network devices that are now
now enabled.
@raises error.TestFail if WiFi state is not toggled.
# Make sure we leave the WiFi network device in enabled state before
# ending the test.
if (self.chrome_net.WIFI_DEVICE in original_enabled_networks and
self.chrome_net.WIFI_DEVICE in new_enabled_networks):
raise error.TestFail('WiFi was not disabled.')
if (self.chrome_net.WIFI_DEVICE not in original_enabled_networks
and self.chrome_net.WIFI_DEVICE not in
raise error.TestFail('WiFi was not enabled.')'Enabling / Disabling WiFi works!')
def _connect_to_network(self, network):
"""Connects to the given network using networkingPrivate API.
@param network: Namedtuple containing network attributes.
new_network_connect = self.chrome_net._chrome_testing.call_test_function(
'"' + network.guid +'"')
if (new_network_connect['status'] ==
raise error.TestFail(
'Could not connect to %s network. Error returned by '
'chrome.networkingPrivate.startConnect API: %s' %
(, new_network_connect['error']))
def _find_and_transition_wifi_networks_in_range(self):
"""Verify all WiFi networks in range are displayed."""
known_service_names_in_wifi_cell = [self.SSID_1, self.SSID_2]
networks_found_via_api = self.chrome_net.get_wifi_networks()
network_list = self._extract_wifi_network_info(networks_found_via_api)'Networks found via API: %s', networks_found_via_api)
wifi_names_found_via_api = []
known_wifi_network_details = []
for network in network_list:
if in known_service_names_in_wifi_cell:
if self.TEST in ('all', 'findVerifyWiFiNetworks'):
known_service_names_in_wifi_cell, wifi_names_found_via_api)
if self.TEST in ('all', 'transitionWiFiNetworks'):
def _enable_disable_wifi(self):
"""Verify that the test is able to enable and disable WiFi."""
original_enabled_networks = self.chrome_net.get_enabled_devices()
if self.chrome_net.WIFI_DEVICE in original_enabled_networks:
new_enabled_networks = self.chrome_net.get_enabled_devices()
original_enabled_networks, new_enabled_networks)
def _transition_wifi_networks(self, known_wifi_networks):
"""Verify that the test is able to transition between the two known
wifi networks.
@param known_wifi_networks: List of known wifi networks.
@raises error.TestFail if device is not able to transition to a
known wifi network.
if not known_wifi_networks:
raise error.TestFail('No pre-configured network available for '
for network in known_wifi_networks:
self._connect_to_network(network)'Successfully transitioned to: %s',
def _autoconnect_wifi(self):
"""Test and verify the device autoconnects to WiFi network.
@raises error.TestFail if device is not able to autoconnect to a
previously connected WiFi network.
networks = self._extract_wifi_network_info( \
self.chrome_net.get_wifi_networks())'Networks found before connection: %s', networks)
network_to_connect = networks.pop()
original_network_name =
if network_to_connect.connectionState == 'NotConnected':
self._connect_to_network(network_to_connect)'Connected to WiFi network: %s',
networks = self._extract_wifi_network_info( \
self.chrome_net.get_wifi_networks())'Networks found after connection: %s', networks)
network_to_connect = networks.pop()
while != original_network_name:
if not networks:
raise error.TestFail('Previously connected WiFi network not '
network_to_connect = networks.pop()
if network_to_connect.connectionState == 'NotConnected':
raise error.TestFail('Did not autoconnect to remembered network.')'Successfully autoconnected to remembered network.')
def run_once(self, ssid_1, ssid_2, test):
"""Run the test.
@param ssid_1: SSID of the first AP.
@param ssid_2: SSID of the second AP.
@param test: Set by the server test control file depending on the test
that is being run.
self.SSID_1 = ssid_1
self.SSID_2 = ssid_2
self.TEST = test
with cntc.ChromeNetworkingTestContext() as testing_context:
self.chrome_net = cnta.ChromeNetworkProvider(testing_context)
enabled_devices = self.chrome_net.get_enabled_devices()
if (self.chrome_net.WIFI_DEVICE not in enabled_devices):
if test == 'all':
elif test in ('findVerifyWiFiNetworks', 'transitionWiFiNetworks'):
elif test == 'enableDisableWiFi':
elif test == 'autoconnectWiFi':