• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import logging
5import time
6
7from autotest_lib.client.common_lib.cros.network import interface
8from autotest_lib.client.cros.networking.chrome_testing \
9        import chrome_networking_test_api as cnta
10from autotest_lib.client.cros.networking.chrome_testing \
11        import chrome_networking_test_context as cntc
12from autotest_lib.client.cros.power import power_test
13
14class power_WifiIdle(power_test.power_Test):
15    """Class for power_WifiIdle test."""
16    version = 1
17
18
19    def initialize(self, pdash_note=''):
20        """Perform necessary initialization prior to test run."""
21        super(power_WifiIdle, self).initialize(seconds_period=10.,
22                                               pdash_note=pdash_note)
23
24    def _is_wifi_on(self):
25        """Return whether wifi is enabled."""
26        enabled_devices = self.chrome_net.get_enabled_devices()
27        return self.chrome_net.WIFI_DEVICE in enabled_devices
28
29    def _verify_connected_to_network(self):
30        """Raise error if not connected to network, else do nothing."""
31        networks = self.chrome_net.get_wifi_networks()
32        logging.info('Networks found: %s', networks)
33
34        for network in networks:
35            if network['ConnectionState'] == 'Connected':
36                logging.info('Connected to network: %s', network)
37                return
38
39        logging.info('Not connected to network.')
40        raise error.TestError('Not connected to network.')
41
42    def run_once(self, idle_time=120):
43        """Collect power stats when wifi is on or off.
44
45        Args:
46            idle_time: time in seconds to stay idle and measure power
47        """
48
49        # Find all wired ethernet interfaces.
50        # TODO(seankao): This block to check whether ethernet interface
51        # is active appears in several tests. Pull this into power_utils.
52        ifaces = [iface for iface in interface.get_interfaces()
53                if (not iface.is_wifi_device() and
54                iface.name.startswith('eth'))]
55        logging.debug('Ethernet interfaces include: ' +
56                str([iface.name for iface in ifaces]))
57        for iface in ifaces:
58            if iface.is_lower_up:
59                raise error.TestError('Ethernet interface is active. '
60                                      'Please remove Ethernet cable.')
61
62        with cntc.ChromeNetworkingTestContext() as testing_context:
63            self.chrome_net = cnta.ChromeNetworkProvider(testing_context)
64            # Ensure wifi is enabled.
65            if not self._is_wifi_on():
66                self.chrome_net.enable_network_device(
67                        self.chrome_net.WIFI_DEVICE)
68            if not self._is_wifi_on():
69                raise error.TestError('Failed to enable wifi.')
70
71            self.chrome_net.scan_for_networks()
72            self._verify_connected_to_network()
73
74            # Test system idle with wifi turned on.
75            self.start_measurements()
76            time.sleep(idle_time)
77            self.checkpoint_measurements('wifi_on')
78
79            # Disable wifi.
80            self.chrome_net.disable_network_device(self.chrome_net.WIFI_DEVICE)
81            if self._is_wifi_on():
82                raise error.TestError('Failed to disable wifi.')
83
84            # Test system idle with wifi turned off.
85            start_time = time.time()
86            time.sleep(idle_time)
87            self.checkpoint_measurements('wifi_off', start_time)
88
89            # Turn on wifi before leaving the test.
90            self.chrome_net.enable_network_device(self.chrome_net.WIFI_DEVICE)
91