• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 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.
4
5import json
6import logging
7
8from autotest_lib.client.bin import test
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import chrome
12from autotest_lib.client.common_lib.cros import policy
13from autotest_lib.client.cros import cryptohome
14from autotest_lib.client.cros.enterprise import enterprise_fake_dmserver
15from autotest_lib.client.cros.power import power_status
16
17
18class enterprise_PowerManagement(test.test):
19    """Verify the power management policy setting."""
20    version = 1
21
22    def initialize(self, percent_initial_charge_min=10):
23        """
24        Setup local variables and  init the fake DM server
25
26        @param percent_initial_charge_min: Minimum percentage of battery
27                                           required for the test to run.
28
29        """
30        # Username and password for the fake dm server can be anything
31        # they are not used to authenticate against GAIA.
32        self.username = 'fake-user@managedchrome.com'
33        self.password = 'fakepassword'
34
35        self._power_status = power_status.get_status()
36        if not self._power_status.on_ac():
37            # Ensure that the battery has some charge.
38            self._power_status.assert_battery_state(percent_initial_charge_min)
39        logging.info("Device power type is %s", self._power_type)
40
41        # Note: FakeDMServer requires policy protos to be installed.
42        policy.install_protobufs(self.autodir, self.job)
43        self.fake_dm_server = enterprise_fake_dmserver.FakeDMServer()
44        self.fake_dm_server.start(self.tmpdir, self.debugdir)
45
46    def cleanup(self):
47        """Close out anything used by this test."""
48        self.fake_dm_server.stop()
49
50    @property
51    def _power_type(self):
52        """
53        Returns appropriate power type based on whether DUT is on AC or not.
54
55        @returns string of power type.
56
57        """
58        if self._power_status.on_ac():
59            return "AC"
60
61        return "Battery"
62
63    def _setup_lock_policy(self):
64        """Setup policy to lock screen in 10 seconds of idle time."""
65        self._screen_lock_delay = 10
66        screen_lock_policy = '{ "%s": %d }' % (self._power_type,
67                                               self._screen_lock_delay * 1000)
68        policy_blob = """{
69            "google/chromeos/user": {
70                "mandatory": {
71                    "ScreenLockDelays": %s
72                }
73            },
74            "managed_users": [ "*" ],
75            "policy_user": "%s",
76            "current_key_index": 0,
77            "invalidation_source": 16,
78            "invalidation_name": "test_policy"
79        }""" % (json.dumps(screen_lock_policy), self.username)
80
81        self.fake_dm_server.setup_policy(policy_blob)
82
83    def _setup_logout_policy(self):
84        """Setup policy to logout in 10 seconds of idle time."""
85        self._screen_logout_delay = 10
86        idle_settings_policy = '''{
87            "%s": {
88                "Delays": {
89                    "ScreenDim": 2000,
90                    "ScreenOff": 3000,
91                    "IdleWarning": 4000,
92                    "Idle": %d
93                 },
94                 "IdleAction": "Logout"
95            }
96        }''' % (self._power_type, self._screen_logout_delay * 1000)
97
98        policy_blob = """{
99            "google/chromeos/user": {
100                "mandatory": {
101                    "PowerManagementIdleSettings": %s
102                }
103            },
104            "managed_users": [ "*" ],
105            "policy_user": "%s",
106            "current_key_index": 0,
107            "invalidation_source": 16,
108            "invalidation_name": "test_policy"
109        }""" % (json.dumps(idle_settings_policy), self.username)
110
111        self.fake_dm_server.setup_policy(policy_blob)
112
113    def _create_chrome(self):
114        """
115        Create an instance of chrome.
116
117        @returns a telemetry browser instance.
118
119        """
120        extra_browser_args = '--device-management-url=%s ' % (
121                self.fake_dm_server.server_url)
122        return chrome.Chrome(
123                extra_browser_args=extra_browser_args,
124                autotest_ext=True,
125                disable_gaia_services=False,
126                gaia_login=False,
127                username=self.username,
128                password=self.password,
129                expect_policy_fetch=True)
130
131    def run_once(self):
132        """Run the power management policy tests."""
133        self._setup_lock_policy()
134        with self._create_chrome() as cr:
135            utils.poll_for_condition(
136                    lambda: cr.login_status['isScreenLocked'],
137                    exception=error.TestFail('User is not locked'),
138                    timeout=self._screen_lock_delay * 2,
139                    sleep_interval=1,
140                    desc='Expects to find Chrome locked.')
141
142        self._setup_logout_policy()
143        with self._create_chrome() as cr:
144            utils.poll_for_condition(
145                    lambda: not cryptohome.is_vault_mounted(user=self.username,
146                            allow_fail=True),
147                            exception=error.TestFail('User is not logged out'),
148                            timeout=self._screen_logout_delay*2,
149                            sleep_interval=1,
150                            desc='Expects to find user logged out.')
151