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