• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 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.common_lib import error
8from autotest_lib.server import autotest, test
9
10
11# TODO(b/163372205): Some models before unibuild have ambient light sensors
12# only on certain skus. However, has_ambient_light_sensor powerd pref has to
13# be set for all skus. For these models, skip checking the existence of ambient
14# light sensors against has_ambient_light_sensor powerd pref.
15IGNORE_ALS_PREF_MODELS = ['caroline']
16
17class power_BrightnessResetAfterReboot(test.test):
18    """Verifies that the panel brightness level resets after device reboots.
19    """
20    version = 2
21
22    def run_once(self, host, client_autotest):
23        """This test verifies that the panel brightness level resets after
24           device reboots.
25        """
26        if host.has_internal_display() is None:
27            raise error.TestNAError('Device has no internal display.')
28        autotest_client = autotest.Autotest(host)
29        host.reboot()
30        autotest_client.run_test(client_autotest,
31                                 exit_without_logout=True)
32
33        cmd = 'check_powerd_config --ambient_light_sensor'
34        num_als_pref = int(host.run_output(cmd, ignore_status=True))
35
36        cmd = 'backlight_tool --get_ambient_light_lux'
37        result = host.run(cmd, ignore_status=True)
38        als_exists = not result.exit_status # 0 is cmd success.
39
40        if num_als_pref and not als_exists:
41            model = host.get_platform()
42            msg = ('Powerd pref indicates %d ambient light sensor(s) but device '
43                   'is unable to find it (them).' % num_als_pref)
44            if model in IGNORE_ALS_PREF_MODELS:
45                logging.info('%s However, skip this check for model: %s.',
46                             msg, model)
47            else:
48                raise error.TestFail(msg)
49
50        initial_lux = -1
51        if als_exists:
52            initial_lux = int(result.stdout.rstrip())
53            lux_domain = [initial_lux / 2, initial_lux * 2]
54            brightness_range = \
55                    [get_backlight(host, lux) for lux in lux_domain]
56        else:
57            brightness_range = [10.0, 90.0]
58
59        initial_brightness = get_backlight(host)
60        if (initial_brightness < brightness_range[0] or
61                initial_brightness > brightness_range[1]):
62            raise error.TestFail('Default brightness level is out of scope '
63                                 '(%d%% - %d%%): %f' % (brightness_range[0],
64                                                        brightness_range[1],
65                                                        initial_brightness))
66
67        brightness_min = 0.0
68        if (not set_backlight(host, brightness_min) or
69                get_backlight(host) != brightness_min):
70            raise error.TestFail('Unable to change the brightness to minimum '
71                                 '(%f%%) level.' % brightness_min)
72
73        brightness_max = 100.0
74        if (not set_backlight(host, brightness_max) or
75                get_backlight(host) != brightness_max):
76            raise error.TestFail('Unable to change the brightness to maximum '
77                                 '(%f%%) level.' % brightness_max)
78
79        host.reboot()
80        autotest_client.run_test(client_autotest,
81                                 exit_without_logout=True)
82        brightness_after_reboot = get_backlight(host)
83        if not als_exists and initial_brightness == brightness_after_reboot:
84            return
85        # If there is an ambient light sensor, allow a small change in internal
86        # display brightness, in case that the ambient light changes slightly.
87        if als_exists:
88            cushion = 0.2
89            lux_domain_after_reboot = [(1.0 - cushion) * initial_lux,
90                                       (1.0 + cushion) * initial_lux]
91            brightness_range_after_reboot = [get_backlight(host, lux) for lux
92                                             in lux_domain_after_reboot]
93            if (brightness_range_after_reboot[0] <=
94                    brightness_after_reboot <=
95                    brightness_range_after_reboot[1]):
96                return
97
98        raise error.TestFail('Unable to reset internal display brightness back '
99                             'to default after reboot.\n'
100                             'Previous boot default brightness: %f\n'
101                             'Current boot default brightness: %f' %
102                             (initial_brightness, brightness_after_reboot))
103
104
105def set_backlight(host, percentage):
106    """Executes backlight_tool to set internal display backlight.
107       @param host: host object representing the DUT.
108       @param percentage: linear percentage to set internal display
109                          backlight to.
110    """
111    cmd = 'backlight_tool --set_brightness_percent=%f' % percentage
112    try:
113        exit_status = host.run(cmd).exit_status
114    except error.CmdError:
115        raise error.TestFail(cmd)
116    return not exit_status # 0 is cmd success.
117
118def get_backlight(host, lux=-1):
119    """Executes backlight_tool to get internal display backlight.
120       @param host: host object representing the DUT.
121    """
122    cmd = 'backlight_tool --get_brightness_percent'
123    if lux >= 0:
124        cmd = '%s --lux=%d' % (cmd, lux)
125    try:
126        result = host.run_output(cmd)
127    except error.CmdError:
128        raise error.TestFail(cmd)
129    return float(result)
130