1# Copyright 2019 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 time 6 7from autotest_lib.client.common_lib import error 8from autotest_lib.client.cros.enterprise import enterprise_policy_base 9from autotest_lib.client.cros.power import power_utils 10 11 12class policy_ScreenBrightnessPercent( 13 enterprise_policy_base.EnterprisePolicyTest): 14 """ 15 Test effect of ScreenBrightnessPercent policy on Chrome OS. 16 17 This test will set the policy, then check the current screen brightness. 18 The brightness reported by the hardware is not the same as the UI bright- 19 ness, so a conversion must be done. 20 Additionally this policy is only checking the setting when the DUT is on AC 21 power. 22 23 """ 24 version = 1 25 26 POLICY_NAME = 'ScreenBrightnessPercent' 27 28 def _convert_power_percent(self, percent): 29 """ 30 Will convert the UI brightness setting to the hw brightness level. 31 See: 32 https://chromium.googlesource.com/chromiumos/platform2/+/master/power_manager/docs/screen_brightness.md 33 34 @param percent: int or float, UI brightness settings. 35 36 return: float, hardware brightness percentage (on 0-100 scale). 37 38 """ 39 max_level = self._backlight.get_max_level() 40 41 # Get the minimum value brightness value to compute the HW brightness 42 bl_contr = power_utils.BacklightController() 43 bl_contr.set_brightness_to_min() 44 45 # Give the hardware a tiny bit of time to settle 46 time.sleep(1) 47 min_level = self._backlight.get_level() 48 49 # Use the formula defined in the link above 50 fract = (percent - 6.25) / (100 - 6.25) 51 level = min_level + ((fract ** 2) * (max_level - min_level)) 52 finalv = (level / max_level) * 100 53 return finalv 54 55 def _test_backlight(self, backlight_level): 56 """ 57 Get the actual backlight percentage and compare it to the set policy. 58 59 Note: There is a slight difference between the computed on the reported 60 brightness. This is likely due floating point math differences between 61 the implemented formula on the DUT, and the one used here. Because 62 of this, a half percent tolerace is added. 63 64 @param backlight_level: int or float, UI brightness settings. 65 66 """ 67 self._backlight = power_utils.Backlight() 68 actual_percent = self._backlight.get_percent() 69 set_percent = self._convert_power_percent(backlight_level) 70 71 if abs(actual_percent - set_percent) > 0.5: 72 raise error.TestError( 73 "Screen brightness incorrect ({}) when it should be {}" 74 .format(set_percent, actual_percent)) 75 76 def run_once(self, case): 77 """ 78 Setup and run the test configured for the specified test case. 79 80 @param case: Name of the test case to run. 81 82 """ 83 self.setup_case(user_policies={ 84 self.POLICY_NAME: {"BrightnessAC": case}}) 85 self._test_backlight(case) 86