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 os
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import cryptohome
10from autotest_lib.client.cros import enterprise_base
11from autotest_lib.client.bin import utils
12
13
14class enterprise_PowerManagement(enterprise_base.EnterpriseTest):
15    """Verify the power management policy setting."""
16    version = 1
17
18
19    def setup(self):
20        os.chdir(self.srcdir)
21        utils.make('OUT_DIR=.')
22
23
24    def initialize(self):
25        self.import_dmserver(self.srcdir)
26        super(enterprise_PowerManagement, self).initialize()
27
28
29    def _setup_lock_policy(self):
30        """Setup policy to lock screen in 10 seconds of idle time."""
31        self._screen_lock_delay = 10
32        screen_lock_policy = '{ "AC": %d }' % (self._screen_lock_delay*1000)
33
34        policy_blob = """{
35            "google/chromeos/user": {
36                "mandatory": {
37                    "ScreenLockDelays": %s
38                }
39            },
40            "managed_users": [ "*" ],
41            "policy_user": "%s",
42            "current_key_index": 0,
43            "invalidation_source": 16,
44            "invalidation_name": "test_policy"
45        }""" % (json.dumps(screen_lock_policy), self.USERNAME)
46
47        self.setup_policy(policy_blob)
48
49
50    def _setup_logout_policy(self):
51        """Setup policy to logout in 10 seconds of idle time."""
52        self._screen_logout_delay = 10
53        idle_settings_policy = '''{
54            "AC": {
55                "Delays": {
56                    "ScreenDim": 2000,
57                    "ScreenOff": 3000,
58                    "IdleWarning": 4000,
59                    "Idle": %d
60                 },
61                 "IdleAction": "Logout"
62            }
63        }''' % (self._screen_logout_delay*1000)
64
65        policy_blob = """{
66            "google/chromeos/user": {
67                "mandatory": {
68                    "PowerManagementIdleSettings": %s
69                }
70            },
71            "managed_users": [ "*" ],
72            "policy_user": "%s",
73            "current_key_index": 0,
74            "invalidation_source": 16,
75            "invalidation_name": "test_policy"
76        }""" % (json.dumps(idle_settings_policy), self.USERNAME)
77
78        self.setup_policy(policy_blob)
79
80
81    def run_once(self):
82        """Run the power management policy tests."""
83        self._setup_lock_policy()
84        with self.create_chrome(autotest_ext=True) as cr:
85            utils.poll_for_condition(
86                lambda: cr.login_status['isScreenLocked'],
87                exception=error.TestFail('User is not locked'),
88                timeout=self._screen_lock_delay*2,
89                sleep_interval=1,
90                desc='Expects to find Chrome locked.')
91
92        self._setup_logout_policy()
93        with self.create_chrome() as cr:
94            utils.poll_for_condition(
95                lambda: not cryptohome.is_vault_mounted(user=self.USERNAME,
96                                                        allow_fail=True),
97                exception=error.TestFail('User is not logged out'),
98                timeout=self._screen_logout_delay*2,
99                sleep_interval=1,
100                desc='Expects to find user logged out.')
101
102