• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging
7import os
8import shutil
9import time
10
11from autotest_lib.client.bin import test, utils
12from autotest_lib.client.common_lib import error
13from autotest_lib.client.common_lib.cros import chrome
14from autotest_lib.client.cros.power import sys_power
15
16class power_VideoSuspend(test.test):
17    """Suspend the system with a video playing."""
18    version = 1
19    tmp_path = '/tmp'
20
21    def run_once(self, video_urls=None):
22        # https://crbug.com/1288417, b/215442780
23        # Copy file to tmpdir to avoid the need of setting up local http server.
24        file_path = os.path.join(self.bindir, 'play.html')
25        self.dest_path = os.path.join(self.tmp_path, 'play.html')
26        shutil.copy(file_path, self.dest_path)
27        http_path = 'file://' + self.dest_path
28
29        if video_urls is None:
30            raise error.TestError('no videos to play')
31
32        with chrome.Chrome(init_network_controller=True) as cr:
33            tab = cr.browser.tabs[0]
34            tab.Navigate(http_path)
35            tab.WaitForDocumentReadyStateToBeComplete()
36
37            for url in video_urls:
38                self._suspend_with_video(cr.browser, tab, url)
39
40
41    def _check_video_is_playing(self, tab):
42        def _get_current_time():
43            return tab.EvaluateJavaScript('player.currentTime')
44
45        old_time = _get_current_time()
46        utils.poll_for_condition(
47            condition=lambda: _get_current_time() > old_time,
48            exception=error.TestError('Player stuck until timeout.'))
49
50
51    def _suspend_with_video(self, browser, tab, video_url):
52        logging.info('testing %s', video_url)
53        tab.EvaluateJavaScript('play("%s")' % video_url)
54
55        self._check_video_is_playing(tab)
56
57        time.sleep(2)
58        sys_power.kernel_suspend(10)
59        time.sleep(2)
60
61        self._check_video_is_playing(tab)
62
63    def cleanup(self):
64        """
65        Cleanup video file.
66        """
67        if hasattr(self, 'dest_path'):
68            os.remove(self.dest_path)
69