power_VideoDetector.py revision 4cf56297789ce77db15cbd189e23d9426763776e
1# Copyright (c) 2013 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 utils
7from autotest_lib.client.common_lib import base_utils, error
8from autotest_lib.client.cros import cros_ui_test, power_utils
9
10class power_VideoDetector(cros_ui_test.UITest):
11    version = 1
12    _pref_path = '/var/lib/power_manager'
13
14    def initialize(self):
15        super(power_VideoDetector, self).initialize()
16        self._video_url = \
17            'file://%s/tests/power_VideoDetector/fade.html' % self.autodir
18
19
20    def run_once(self, run_time_sec=60):
21        if run_time_sec < 30:
22            raise error.TestError('Must run for at least 30 seconds')
23
24        self.login()
25
26        # Start powerd if not started.  Set timeouts for quick idle events.
27        run_time_ms = run_time_sec * 1000
28        # At the time of writing this test, the video detector gets a status
29        # update from Chrome every ten seconds.
30        react_ms = 5000
31        dim_ms = 10000
32        off_ms = max(3600000, run_time_ms * 10)
33        prefs = { 'disable_als'          : 1,
34                  'react_ms'             : react_ms,
35                  'plugged_dim_ms'       : dim_ms,
36                  'plugged_off_ms'       : off_ms,
37                  'unplugged_dim_ms'     : dim_ms,
38                  'unplugged_off_ms'     : off_ms, }
39        self._pref_change = power_utils.PowerPrefChanger(prefs)
40
41        keyvals = {}
42
43        # Start with max brightness, so we can easily detect dimming.
44        power_utils.BacklightController().set_brightness_to_max()
45        backlight = power_utils.Backlight()
46        initial_brightness = base_utils.wait_for_value(backlight.get_max_level)
47
48        # Open a tab to play video.
49        self.pyauto.AppendTab(self._video_url)
50
51        # Sleep until the runtime is up.
52        time.sleep(run_time_sec)
53
54        # Stop powerd to avoid dimming when the video stops.
55        utils.system_output('stop powerd')
56
57        final_brightness = backlight.get_level()
58
59        # Check that the backlight stayed the same.
60        if initial_brightness != final_brightness:
61            raise error.TestFail(
62                ('Backlight level changed from %d to %d when it should ' + \
63                 'have stayed the same.') %
64                (initial_brightness, final_brightness))
65
66        keyvals['initial_brightness'] = initial_brightness
67        keyvals['final_brightness'] = final_brightness
68        self.write_perf_keyval(keyvals)
69
70
71    def cleanup(self):
72        if self.logged_in():
73            self.logout()
74        utils.restart_job('powerd')
75