• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright 2014 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9
10import logging
11import time
12
13from six.moves import range
14
15from autotest_lib.client.common_lib import error
16from autotest_lib.client.cros.networking.chrome_testing import test_utils
17
18class ChromeNetworkProvider(object):
19    """
20    ChromeNetworkProvider handles all calls made to the connection
21    manager by internally calling the Networking Private Extension API.
22
23    For Example: Enable/Disable WiFi, Scan WiFi, Connect to WiFi that can
24    be used to develop other tests.
25    """
26
27    WIFI_DEVICE = 'WiFi'
28    CELLULAR = 'Cellular'
29    SHORT_TIMEOUT = 3
30
31
32    def __init__(self, cntc):
33        """Initialization function for this class.
34
35        @param cntc: Instance of type ChromeNetworkingTestContext.
36
37        """
38        self._chrome_testing = cntc
39
40
41    def get_wifi_networks(self):
42        """Get list of available wifi networks.
43
44        @raises error.TestFail if no wifi networks are found.
45        @return List of dictionaries containing wifi network information.
46
47        """
48        wifi_networks = self._chrome_testing.find_wifi_networks()
49        if not wifi_networks:
50            raise error.TestFail('No wifi networks found.')
51
52        return wifi_networks
53
54
55    def get_enabled_devices(self):
56        """Get list of enabled network devices on the device.
57
58        @return List of enabled network devices.
59
60        """
61        enabled_network_types = self._chrome_testing.call_test_function(
62                test_utils.LONG_TIMEOUT,
63                'getEnabledNetworkDevices')
64        for key, value in enabled_network_types.items():
65            if key == 'result':
66                logging.info('Enabled Network Devices: %s', value)
67                return value
68
69
70    def disable_network_device(self, network):
71        """Disable given network device.
72
73        @param network: string name of the network device to be disabled.
74                Options include 'WiFi', 'Cellular' and 'Ethernet'.
75
76        """
77        # Do ChromeOS browser session teardown/setup before disabling the
78        # network device because chrome.networkingPrivate.disableNetworkType API
79        # fails to disable the network device on subsequent calls if we do not
80        # teardown and setup the browser session.
81        self._chrome_testing.teardown()
82        self._chrome_testing.setup()
83
84        logging.info('Disabling: %s', network)
85        disable_network_result = self._chrome_testing.call_test_function_async(
86                'disableNetworkDevice',
87                '"' + network + '"')
88
89
90    def enable_network_device(self, network):
91        """Enable given network device.
92
93        @param network: string name of the network device to be enabled. Options
94                include 'WiFi', 'Cellular' and 'Ethernet'.
95
96        """
97        logging.info('Enabling: %s', network)
98        enable_network_result = self._chrome_testing.call_test_function_async(
99                'enableNetworkDevice',
100                '"' + network + '"')
101        # Allow enough time for the DUT to fully transition into enabled state.
102        time.sleep(self.SHORT_TIMEOUT)
103
104
105    def scan_for_networks(self, timeout=SHORT_TIMEOUT):
106        """Scan for all the available networks
107
108        @param timeout int seconds to sleep while scanning for networks
109
110        """
111        self._chrome_testing.call_test_function_async('requestNetworkScan')
112        # Allow enough time for Chrome to scan and get all the network SSIDs.
113        time.sleep(timeout)
114
115
116    def connect_to_network(self, service_list):
117       """Connects to the given network using networkingPrivate API.
118
119       @param service_list: service list for the network to connect to.
120
121       """
122       connect_status = self._chrome_testing.call_test_function(
123                            test_utils.LONG_TIMEOUT,
124                            'connectToNetwork',
125                            '"' + service_list['GUID'] +'"')
126
127       if connect_status['error'] == 'connected':
128           return
129       elif connect_status['error'] == 'connecting':
130           for retry in range(3):
131               logging.debug('Just hold on for 10 seconds')
132               time.sleep(10)
133               if connect_status['error'] == 'connected':
134                   return
135
136       if connect_status['status'] == 'chrome-test-call-status-failure':
137           raise error.TestFail(
138                     'Could not connect to %s network. Error returned by '
139                     'chrome.networkingPrivate.startConnect API: %s' %
140                     (service_list['Name'], connect_status['error']))
141