Home | History | Annotate | Download | only in power_FlashVideoSuspend
      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 
      5 import logging
      6 import os
      7 import time
      8 
      9 from autotest_lib.client.bin import test, utils
     10 from autotest_lib.client.common_lib import error
     11 from autotest_lib.client.common_lib.cros import chrome
     12 from autotest_lib.client.cros import sys_power
     13 
     14 
     15 class 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(init_network_controller=True) 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.WaitForJavaScriptCondition('typeof player != "undefined"',
     56                                        timeout=10)
     57 
     58         self.check_video_is_playing(tab)
     59 
     60         time.sleep(2)
     61         try:
     62             sys_power.do_suspend(10)
     63         except Exception as e:
     64             logging.error(e)
     65             raise error.TestFail('====Kernel suspend failed====')
     66         time.sleep(2)
     67 
     68         self.check_video_is_playing(tab)
     69