• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright 2018 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.
5import logging
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.networking.chrome_testing \
10        import chrome_networking_test_api as cnta
11from autotest_lib.client.cros.networking.chrome_testing \
12        import chrome_networking_test_context as cntc
13from autotest_lib.client.cros.power import power_test
14
15class power_WifiIdle(power_test.power_Test):
16    """Class for power_WifiIdle test."""
17    version = 1
18
19
20    def initialize(self, pdash_note='', force_discharge=False):
21        """Perform necessary initialization prior to test run."""
22        super(power_WifiIdle, self).initialize(seconds_period=10.,
23                                               pdash_note=pdash_note,
24                                               force_discharge=force_discharge)
25
26    def _is_wifi_on(self):
27        """Return whether wifi is enabled."""
28        enabled_devices = self.chrome_net.get_enabled_devices()
29        return self.chrome_net.WIFI_DEVICE in enabled_devices
30
31    def _verify_connected_to_network(self):
32        """Raise error if not connected to network, else do nothing."""
33        networks = self.chrome_net.get_wifi_networks()
34        logging.info('Networks found: %s', networks)
35
36        for network in networks:
37            if network['ConnectionState'] == 'Connected':
38                logging.info('Connected to network: %s', network)
39                return
40
41        logging.info('Not connected to network.')
42        raise error.TestError('Not connected to network.')
43
44    def run_once(self, idle_time=120):
45        """Collect power stats when wifi is on or off.
46
47        Args:
48            idle_time: time in seconds to stay idle and measure power
49        """
50
51        with cntc.ChromeNetworkingTestContext() as testing_context:
52            self.chrome_net = cnta.ChromeNetworkProvider(testing_context)
53            # Ensure wifi is enabled.
54            if not self._is_wifi_on():
55                self.chrome_net.enable_network_device(
56                        self.chrome_net.WIFI_DEVICE)
57            if not self._is_wifi_on():
58                raise error.TestError('Failed to enable wifi.')
59
60            self.chrome_net.scan_for_networks()
61            self._verify_connected_to_network()
62
63            # Test system idle with wifi turned on.
64            self.start_measurements()
65            time.sleep(idle_time)
66            self.checkpoint_measurements('wifi_on')
67
68            # Disable wifi.
69            self.chrome_net.disable_network_device(self.chrome_net.WIFI_DEVICE)
70            if self._is_wifi_on():
71                raise error.TestError('Failed to disable wifi.')
72
73            # Test system idle with wifi turned off.
74            start_time = time.time()
75            time.sleep(idle_time)
76            self.checkpoint_measurements('wifi_off', start_time)
77
78            # Turn on wifi before leaving the test.
79            self.chrome_net.enable_network_device(self.chrome_net.WIFI_DEVICE)
80