1# Lint as: python2, python3 2# Copyright 2016 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import logging 7import time 8 9from autotest_lib.client.bin import test 10from autotest_lib.client.bin import utils 11from autotest_lib.client.common_lib import error 12from autotest_lib.client.common_lib.cros import chrome 13from autotest_lib.client.cros.graphics import graphics_utils 14from autotest_lib.client.cros.input_playback import input_playback 15 16BRIGHTNESS_LEVELS = 16 17 18class platform_InputBrightness(test.test): 19 """Tests if device suspends using shortcut keys.""" 20 version = 1 21 BRIGHTNESS_CMD = "backlight_tool --get_brightness_percent" 22 RESET_BRIGHTNESS_CMD = "backlight_tool --set_brightness_percent=70.0" 23 WAIT_TIME = 1 24 25 def warmup(self): 26 """Test setup.""" 27 # Emulate keyboard. 28 # See input_playback. The keyboard is used to play back shortcuts. 29 self._player = input_playback.InputPlayback() 30 self._player.emulate(input_type='keyboard') 31 self._player.find_connected_inputs() 32 33 def test_brightness_down(self, verify_brightness=True): 34 """ 35 Use keyboard shortcut to test brightness Down (F6) key. 36 37 @raises: error.TestFail if system brightness did not decrease. 38 39 """ 40 for level in range(0, BRIGHTNESS_LEVELS): 41 logging.debug("Decreasing the brightness. Level %s", level) 42 sys_brightness = self.get_system_brightness() 43 self._player.blocking_playback_of_default_file( 44 input_type='keyboard', filename='keyboard_f6') 45 time.sleep(self.WAIT_TIME) 46 if verify_brightness: 47 if not sys_brightness > self.get_system_brightness(): 48 raise error.TestFail("Brightness did not decrease from: " 49 "%s" % sys_brightness) 50 51 def test_brightness_up(self, verify_brightness=True): 52 """ 53 Use keyboard shortcut to test brightness Up (F7) key. 54 55 @raises: error.TestFail if system brightness did not increase. 56 57 """ 58 for level in range(0, BRIGHTNESS_LEVELS): 59 logging.debug("Increasing the brightness. Level %s", level) 60 sys_brightness = self.get_system_brightness() 61 self._player.blocking_playback_of_default_file( 62 input_type='keyboard', filename='keyboard_f7') 63 time.sleep(self.WAIT_TIME) 64 if verify_brightness: 65 if not sys_brightness < self.get_system_brightness(): 66 raise error.TestFail("Brightness did not increase from: " 67 "%s" % sys_brightness) 68 69 def get_system_brightness(self): 70 """ 71 Get current system brightness percent (0.0-100.0). 72 73 @returns: current system brightness. 74 """ 75 sys_brightness = utils.system_output(self.BRIGHTNESS_CMD) 76 return float(sys_brightness) 77 78 79 def run_once(self): 80 """ 81 Open browser, and affect brightness using up and down functions. 82 83 @raises: error.TestFail if brightness keys (F6/F7) did not work. 84 85 """ 86 # Check for internal_display 87 if not graphics_utils.has_internal_display(): 88 raise error.TestNAError('Test can not proceed on ' 89 'devices without internal display.') 90 with chrome.Chrome(): 91 logging.info("Increasing the initial brightness to max without " 92 "verifying brightness.") 93 # Not using command to set max brightness because 94 # the brightness button stays at the current level 95 # only irrespective of the command 96 self.test_brightness_up(verify_brightness=False) 97 # Actual test starts from here. 98 self.test_brightness_down(verify_brightness=True) 99 self.test_brightness_up(verify_brightness=True) 100 101 102 def cleanup(self): 103 """Test cleanup.""" 104 self._player.close() 105 utils.system_output(self.RESET_BRIGHTNESS_CMD) 106