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 os
6import time
7
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import base_utils, error
10from autotest_lib.client.common_lib.cros import chrome
11from autotest_lib.client.cros import power_utils, upstart
12
13class power_VideoDetector(test.test):
14    """
15    Verify the backlight does not get dimmed while playing video.
16    """
17
18    version = 1
19
20    def run_once(self, run_time_sec=60):
21        """
22        @param run_time_sec: time to run the test
23        """
24        if run_time_sec < 30:
25            raise error.TestError('Must run for at least 30 seconds')
26
27        with chrome.Chrome(init_network_controller=True) as cr:
28            # Start powerd if not started.  Set timeouts for quick idle events.
29            run_time_ms = run_time_sec * 1000
30            # At the time of writing this test, the video detector gets a status
31            # update from Chrome every ten seconds.
32            dim_ms = 10000
33            off_ms = max(3600000, run_time_ms * 10)
34            prefs = { 'has_ambient_light_sensor' : 0,
35                      'ignore_external_policy'   : 1,
36                      'plugged_dim_ms'           : dim_ms,
37                      'plugged_off_ms'           : off_ms,
38                      'unplugged_dim_ms'         : dim_ms,
39                      'unplugged_off_ms'         : off_ms, }
40            self._pref_change = power_utils.PowerPrefChanger(prefs)
41
42            keyvals = {}
43
44            # Start with max brightness, so we can easily detect dimming.
45            power_utils.BacklightController().set_brightness_to_max()
46            backlight = power_utils.Backlight()
47            initial_brightness = \
48                base_utils.wait_for_value(backlight.get_max_level)
49
50            # Open a tab to play video.
51            cr.browser.platform.SetHTTPServerDirectories(self.bindir)
52            tab = cr.browser.tabs[0]
53            tab.Navigate(cr.browser.platform.http_server.UrlOf(
54                os.path.join(self.bindir, 'fade.html')))
55            tab.WaitForDocumentReadyStateToBeComplete()
56
57            # Sleep until the runtime is up.
58            time.sleep(run_time_sec)
59
60            # Stop powerd to avoid dimming when the video stops.
61            utils.system_output('stop powerd')
62
63            final_brightness = backlight.get_level()
64
65            # Check that the backlight stayed the same.
66            if initial_brightness != final_brightness:
67                raise error.TestFail(
68                    ('Backlight level changed from %d to %d when it should ' + \
69                     'have stayed the same.') %
70                    (initial_brightness, final_brightness))
71
72            keyvals['initial_brightness'] = initial_brightness
73            keyvals['final_brightness'] = final_brightness
74            self.write_perf_keyval(keyvals)
75
76
77    def cleanup(self):
78        """
79        Cleanup powerd after test.
80        """
81        upstart.restart_job('powerd')
82