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.
4
5import time
6from autotest_lib.client.bin import test, utils
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros import power_status, power_utils
9from autotest_lib.client.cros import service_stopper
10from autotest_lib.client.cros.graphics import graphics_utils
11
12
13class power_Backlight(test.test):
14    version = 1
15
16
17    def initialize(self):
18        """Perform necessary initialization prior to test run.
19
20        Private Attributes:
21          _backlight: power_utils.Backlight object
22          _services: service_stopper.ServiceStopper object
23        """
24        super(power_Backlight, self).initialize()
25        self._backlight = None
26        self._services = service_stopper.ServiceStopper(
27            service_stopper.ServiceStopper.POWER_DRAW_SERVICES)
28        self._services.stop_services()
29
30
31    def run_once(self, delay=60, seconds=10, tries=20):
32        self._backlight = power_utils.Backlight()
33
34
35        # disable screen blanking. Stopping screen-locker isn't
36        # synchronous :(. Add a sleep for now, till powerd comes around
37        # and fixes all this for us.
38        # TODO(davidjames): Power manager should support this feature directly
39        time.sleep(5)
40        graphics_utils.screen_disable_blanking()
41
42        status = power_status.get_status()
43        status.assert_battery_state(5)
44
45        max_brightness = self._backlight.get_max_level()
46        if max_brightness < 4:
47            raise error.TestFail('Must have at least 5 backlight levels')
48        sysfs_max = self._get_highest_sysfs_max_brightness()
49        if max_brightness != sysfs_max:
50            raise error.TestFail(('Max brightness %d is not the highest ' +
51                                  'possible |max_brightness|, which is %d') %
52                                 (max_brightness, sysfs_max))
53        keyvals = {}
54        rates = []
55
56        levels = [0, 50, 100]
57        for i in levels:
58            self._backlight.set_percent(i)
59            time.sleep(delay)
60            this_rate = []
61            for _ in range(tries):
62                time.sleep(seconds)
63                status.refresh()
64                this_rate.append(status.battery[0].energy_rate)
65            rate = min(this_rate)
66            keyvals['w_bl_%d_rate' % i] = rate
67            rates.append(rate)
68        self.write_perf_keyval(keyvals)
69        for i in range(1, len(levels)):
70            if rates[i] <= rates[i-1]:
71                raise error.TestFail('Turning up the backlight ' \
72                                     'should increase energy consumption')
73
74
75    def cleanup(self):
76        if self._backlight:
77            self._backlight.restore()
78        self._services.restore_services()
79        super(power_Backlight, self).cleanup()
80
81
82    def _get_highest_sysfs_max_brightness(self):
83        # Print |max_brightness| for all backlight sysfs directories, and return
84        # the highest of these max_brightness values.
85        cmd = 'cat /sys/class/backlight/*/max_brightness'
86        output = utils.system_output(cmd)
87        return max(map(int, output.split()))
88