power_VideoDetector.py revision 73db85f34daeb727fc84ead3f2d81a25dc108c39
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._saved_prefs = power_utils.set_power_prefs(prefs)
40
41        keyvals = {}
42
43        # Start with max brightness, so we can easily detect dimming.
44        utils.restart_job('powerd')
45        power_utils.BacklightController().set_brightness_to_max()
46        backlight = power_utils.Backlight()
47        initial_brightness = base_utils.wait_for_value(backlight.get_max_level)
48
49        # Open a tab to play video.
50        self.pyauto.AppendTab(self._video_url)
51
52        # Sleep until the runtime is up.
53        time.sleep(run_time_sec)
54
55        # Stop powerd to avoid dimming when the video stops.
56        utils.system_output('stop powerd')
57
58        final_brightness = backlight.get_level()
59
60        # Check that the backlight stayed the same.
61        if initial_brightness != final_brightness:
62            raise error.TestFail(
63                ('Backlight level changed from %d to %d when it should ' + \
64                 'have stayed the same.') %
65                (initial_brightness, final_brightness))
66
67        keyvals['initial_brightness'] = initial_brightness
68        keyvals['final_brightness'] = final_brightness
69        self.write_perf_keyval(keyvals)
70
71
72    def cleanup(self):
73        if self.logged_in():
74            self.logout()
75
76        # Restore saved prefs and restart powerd.
77        power_utils.set_power_prefs(self._saved_prefs)
78        utils.restart_job('powerd')
79