1# Copyright (c) 2012 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
6import os
7import time
8
9from autotest_lib.client.bin import test, utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import chrome
12from autotest_lib.client.cros import sys_power
13
14class power_VideoSuspend(test.test):
15    """Suspend the system with a video playing."""
16    version = 1
17
18    def run_once(self, video_urls=None):
19        if video_urls is None:
20            raise error.TestError('no videos to play')
21
22        with chrome.Chrome() as cr:
23            cr.browser.platform.SetHTTPServerDirectories(self.bindir)
24            tab = cr.browser.tabs[0]
25            tab.Navigate(cr.browser.platform.http_server.UrlOf(
26                os.path.join(self.bindir, 'play.html')))
27            tab.WaitForDocumentReadyStateToBeComplete()
28
29            for url in video_urls:
30                self.suspend_with_video(cr.browser, tab, url)
31
32
33    def check_video_is_playing(self, tab):
34        def get_current_time():
35            return tab.EvaluateJavaScript('player.currentTime')
36
37        old_time = get_current_time()
38        utils.poll_for_condition(
39            condition=lambda: get_current_time() > old_time,
40            exception=error.TestError('Player stuck until timeout.'))
41
42
43    def suspend_with_video(self, browser, tab, video_url):
44        logging.info('testing %s', video_url)
45        tab.EvaluateJavaScript('play("%s")' % video_url)
46
47        self.check_video_is_playing(tab)
48
49        time.sleep(2)
50        sys_power.kernel_suspend(10)
51        time.sleep(2)
52
53        self.check_video_is_playing(tab)
54