1# Copyright 2016 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 logging 6 7from autotest_lib.client.bin import utils 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.cros.enterprise import enterprise_policy_base 10from autotest_lib.client.cros.power import power_status 11 12 13class policy_ChromeOsLockOnIdleSuspend( 14 enterprise_policy_base.EnterprisePolicyTest): 15 """ 16 Test effect of ChromeOsLockOnIdleSuspend policy on Chrome OS behavior. 17 18 This test verifies the behaviour and appearance of the 'Require password 19 to wake from sleep' check box setting in the Security: Idle Settings 20 section of the chrome://settings page for all valid values of the user 21 policy ChromeOsLockOnIdleSuspend: True, False, and Not set. The 22 corresponding test cases are: True_Lock, False_Unlock, NotSet_Unlock. 23 24 Note: True_Lock case is not run as part of the regression suite due to 25 bug crbug.com/666430. See control.true_lock for details. 26 27 """ 28 version = 1 29 30 POLICY_NAME = 'ChromeOsLockOnIdleSuspend' 31 TEST_CASES = { 32 'True_Lock': True, 33 'False_Unlock': False, 34 'NotSet_Unlock': None 35 } 36 IDLE_ACTION_DELAY = 5 37 POWER_MANAGEMENT_IDLE_SETTINGS = { 38 'AC': { 39 'Delays': { 40 'ScreenDim': 2000, 41 'ScreenOff': 3000, 42 'IdleWarning': 4000, 43 'Idle': (IDLE_ACTION_DELAY * 1000) 44 }, 45 'IdleAction': 'Suspend' 46 }, 47 'Battery': { 48 'Delays': { 49 'ScreenDim': 2000, 50 'ScreenOff': 3000, 51 'IdleWarning': 4000, 52 'Idle': (IDLE_ACTION_DELAY * 1000) 53 }, 54 'IdleAction': 'Suspend' 55 } 56 } 57 PERCENT_CHARGE_MIN = 10 58 STARTUP_URLS = ['chrome://policy', 'chrome://settings'] 59 SUPPORTING_POLICIES = { 60 'AllowScreenLock': True, 61 'LidCloseAction': 0, 62 'PowerManagementIdleSettings': POWER_MANAGEMENT_IDLE_SETTINGS, 63 'RestoreOnStartup': 4, 64 'RestoreOnStartupURLs': STARTUP_URLS 65 } 66 67 68 def initialize(self, **kwargs): 69 """Set up local variables and ensure sufficient battery charge.""" 70 self._power_status = power_status.get_status() 71 if not self._power_status.on_ac(): 72 # Ensure that the battery has sufficient minimum charge. 73 self._power_status.assert_battery_state(self.PERCENT_CHARGE_MIN) 74 75 logging.info('Device power type is "%s"', self._power_type) 76 super(policy_ChromeOsLockOnIdleSuspend, self).initialize(**kwargs) 77 78 79 @property 80 def _power_type(self): 81 """Return type of power used by DUT: AC or Battery.""" 82 return 'AC' if self._power_status.on_ac() else 'Battery' 83 84 85 def _is_screen_locked(self): 86 """Return true if login status indicates that screen is locked.""" 87 def _get_screen_locked(): 88 """Return isScreenLocked property, if defined.""" 89 login_status = self.cr.login_status 90 if (isinstance(login_status, dict) and 91 'isScreenLocked' in login_status): 92 return self.cr.login_status['isScreenLocked'] 93 else: 94 logging.debug('login_status: %s', login_status) 95 return None 96 97 return utils.wait_for_value(_get_screen_locked, expected_value=True, 98 timeout_sec=self.IDLE_ACTION_DELAY) 99 100 101 def _test_require_password_to_wake(self, policy_value): 102 """ 103 Verify CrOS enforces ChromeOsLockOnIdleSuspend policy value. 104 105 @param policy_value: policy value for this case. 106 @raises: TestFail if behavior is incorrect. 107 108 """ 109 screen_is_locked = self._is_screen_locked() 110 if screen_is_locked is None: 111 raise error.TestError('Could not determine screen state!') 112 113 # Screen shall be locked if the policy is True, else unlocked. 114 if policy_value: 115 if not screen_is_locked: 116 raise error.TestFail('Screen should be locked.') 117 else: 118 if screen_is_locked: 119 raise error.TestFail('Screen should be unlocked.') 120 121 122 def run_once(self, case): 123 """ 124 Setup and run the test configured for the specified test case. 125 126 @param case: Name of the test case to run. 127 128 """ 129 case_value = self.TEST_CASES[case] 130 self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value 131 self.setup_case(user_policies=self.SUPPORTING_POLICIES) 132 self._test_require_password_to_wake(case_value) 133