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