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
14
15class power_FlashVideoSuspend(test.test):
16    """Suspend the system with a video playing."""
17    version = 2
18
19    def run_once(self, video_url=None):
20        utils.verify_flash_installed()
21        with chrome.Chrome() as cr:
22            cr.browser.platform.SetHTTPServerDirectories(self.bindir)
23            tab = cr.browser.tabs[0]
24            tab.Navigate(cr.browser.platform.http_server.UrlOf(
25                os.path.join(self.bindir, 'youtube.html')))
26            self.suspend_with_youtube(cr.browser.tabs[0], video_url)
27
28
29    def check_video_is_playing(self, tab):
30        """
31        Checks if video is playing or not.
32
33        @param tab: Object to the browser tab
34        """
35        def get_current_time():
36            """Get current time from the javascript."""
37            return tab.EvaluateJavaScript('player.getCurrentTime()')
38
39        old_time = get_current_time()
40        utils.poll_for_condition(
41            condition=lambda: get_current_time() > old_time,
42            exception=error.TestError('Player is stuck until timeout.'))
43
44
45    def suspend_with_youtube(self, tab, video_url):
46        """
47        Suspends kernel while video is running in browser.
48
49        @param tab: Object to the browser tab
50        @param video_url: Object to video url
51        """
52        tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()
53        logging.info('video url is %s', video_url)
54        tab.EvaluateJavaScript('play("%s")' % video_url)
55        tab.WaitForJavaScriptExpression('typeof player != "undefined"', 10)
56
57        self.check_video_is_playing(tab)
58
59        time.sleep(2)
60        try:
61            sys_power.do_suspend(10)
62        except Exception as e:
63            logging.error(e)
64            raise error.TestFail('====Kernel suspend failed====')
65        time.sleep(2)
66
67        self.check_video_is_playing(tab)
68