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 fnmatch
6import logging
7import os
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.video import device_capability
13from autotest_lib.client.cros.video import helper_logger
14
15WAIT_TIMEOUT_S = 60
16
17class video_VideoSeek(test.test):
18    """This test verifies video seek works in Chrome."""
19    version = 1
20
21    def is_skipping_test(self, codec, is_switchres):
22        """Determine whether this test should skip.
23
24        @param codec: the codec to be tested, ex. 'vp8', 'vp9', 'h264'.
25        @param is_switchres: bool, True if using switch resolution video.
26        """
27        blacklist = [
28                # (board, codec, is_switchres); None if don't care.
29
30                # "board" supports Unix shell-type wildcards
31
32                # Disable vp8 switchres for nyan devices temporarily due to:
33                # crbug/699260
34                ('nyan', 'vp8', True), ('nyan_*', 'vp8', True)
35        ]
36
37        board = utils.get_current_board()
38
39        for entry in blacklist:
40            if ((entry[0] is None or fnmatch.fnmatch(board, entry[0])) and
41                (entry[1] is None or codec == entry[1]) and
42                (entry[2] is None or is_switchres == entry[2])):
43                return True
44
45        return False
46
47
48    @helper_logger.video_log_wrapper
49    def run_once(self, codec, is_switchres, video, capability):
50        """Tests whether video seek works by random seeks forward and backward.
51
52        @param codec: the codec to be tested, ex. 'vp8', 'vp9', 'h264'.
53        @param is_switchres: bool, True if using switch resolution video.
54        @param video: Sample video file to be seeked in Chrome.
55        @param capability: The capability required for executing the test.
56        """
57        if self.is_skipping_test(codec, is_switchres):
58            logging.info('Skipping test run on this board.')
59            return  # return immediately to pass this test
60
61        device_capability.DeviceCapability().ensure_capability(capability)
62
63        with chrome.Chrome(
64                extra_browser_args=helper_logger.chrome_vmodule_flag(),
65                init_network_controller=True) as cr:
66            cr.browser.platform.SetHTTPServerDirectories(self.bindir)
67            tab = cr.browser.tabs[0]
68            tab.Navigate(cr.browser.platform.http_server.UrlOf(
69                    os.path.join(self.bindir, 'video.html')))
70            tab.WaitForDocumentReadyStateToBeComplete()
71
72            tab.EvaluateJavaScript('loadSourceAndRunSeekTest("%s")' % video)
73
74            def get_seek_test_status():
75                seek_test_status = tab.EvaluateJavaScript('getSeekTestStatus()')
76                logging.info('Seeking: %s', seek_test_status)
77                return seek_test_status
78
79            # Wait until we get the 'pass' status, meaning the test has been
80            # successful. Also timeout and fail the test if we stay on the same
81            # seek for more than WAIT_TIMEOUT_S.
82            cur_status = get_seek_test_status()
83            while True:
84              utils.poll_for_condition(
85                      lambda: get_seek_test_status() != cur_status,
86                      exception=error.TestError('Seek test is stuck and timeout'),
87                      timeout=WAIT_TIMEOUT_S,
88                      sleep_interval=1)
89              cur_status = get_seek_test_status()
90              if cur_status == 'pass': break
91