1# Copyright (c) 2010 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 5 6from autotest_lib.client.bin import test 7from autotest_lib.client.common_lib import error 8from autotest_lib.client.cros import service_stopper 9from autotest_lib.client.cros.power import power_utils 10 11 12class hardware_Backlight(test.test): 13 version = 1 14 15 def initialize(self): 16 """Perform necessary initialization prior to test run. 17 18 Private Attributes: 19 _backlight: power_utils.Backlight object 20 _services: service_stopper.ServiceStopper object 21 """ 22 super(hardware_Backlight, self).initialize() 23 self._backlight = None 24 # Stop powerd to avoid it adjusting backlight levels 25 self._services = service_stopper.ServiceStopper(['powerd']) 26 self._services.stop_services() 27 28 29 def run_once(self): 30 # optionally test keyboard backlight 31 kblight = None 32 kblight_errs = 0 33 try: 34 kblight = power_utils.KbdBacklight() 35 except power_utils.KbdBacklightException as e: 36 logging.info("Assuming no keyboard backlight due to %s", str(e)) 37 38 if kblight: 39 init_percent = kblight.get_percent() 40 try: 41 for i in xrange(100, -1, -1): 42 kblight.set_percent(i) 43 result = int(kblight.get_percent()) 44 if i != result: 45 logging.error('keyboard backlight set %d != %d get', 46 i, result) 47 kblight_errs += 1 48 finally: 49 kblight.set_percent(init_percent) 50 51 if kblight_errs: 52 raise error.TestFail("%d errors testing keyboard backlight." % \ 53 kblight_errs) 54 55 self._backlight = power_utils.Backlight() 56 backlight_errs = 0 57 backlight_max = self._backlight.get_max_level() 58 for i in xrange(backlight_max + 1): 59 self._backlight.set_level(i) 60 result = self._backlight.get_level() 61 if i != result: 62 # The kernel Documentation/ABI/stable/sysfs-class-backlight 63 # states that the requested brightness may not be the 64 # actual_brightness. 65 # Although not specified in the docs, let's allow the difference 66 # between requested brightness and actual_brightness percent be 67 # within a tolerance of 1 of each other. 68 actual_percent = self._backlight.get_percent() 69 expected_percent = float(i) / float(backlight_max) * 100.0 70 diff_percent = abs(actual_percent - expected_percent) 71 log_level_func = logging.warn 72 if diff_percent > 1: 73 backlight_errs += 1 74 log_level_func = logging.error 75 log_level_func('backlight expected vs. actual exceeds error' 76 'tolerance') 77 log_level_func('backlight set %d != %d get', i, result) 78 log_level_func('backlight percent difference is %f%%', 79 diff_percent) 80 81 if backlight_errs: 82 raise error.TestFail("%d errors testing backlight." % \ 83 backlight_errs) 84 85 86 def cleanup(self): 87 if self._backlight: 88 self._backlight.restore() 89 self._services.restore_services() 90 super(hardware_Backlight, self).cleanup() 91