• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, time
6
7from autotest_lib.client.bin import test, utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros import chrome
10from autotest_lib.client.cros import httpd
11
12
13WAIT_TIMEOUT_S = 5
14PLAYBACK_TEST_TIME_S = 5
15MEDIA_SUPPORT_AVAILABLE = 'maybe'
16
17
18class video_VideoSanity(test.test):
19    """This test verify the media elements and video sanity.
20
21    - verify support for mp4, ogg and webm media.
22    - verify html5 video playback.
23
24    """
25    version = 2
26
27
28    def initialize(self):
29        self._testServer = httpd.HTTPListener(8000, docroot=self.bindir)
30        self._testServer.run()
31
32
33    def cleanup(self):
34        if self._testServer:
35            self._testServer.stop()
36
37
38    def video_current_time(self):
39        """Returns video's current playback time.
40
41        Returns:
42            returns the current playback location in seconds (int).
43
44        """
45        return self.tab.EvaluateJavaScript('testvideo.currentTime')
46
47
48    def video_duration(self):
49        """Returns video total length.
50
51        Returns:
52            returns the total video length in seconds (int).
53
54        """
55        return self.tab.EvaluateJavaScript('testvideo.duration')
56
57
58    def run_video_sanity_test(self, browser):
59        """Run the video sanity test.
60
61        @param browser: The Browser object to run the test with.
62
63        """
64        self.tab = browser.tabs[0]
65        # Verifying <video> support.
66        video_containers = ('mp4', 'ogg', 'webm')
67        self.tab.Navigate('http://localhost:8000/video.html')
68        for container in video_containers:
69            logging.info('Verifying video support for %s.', container)
70            js_script = ("document.createElement('video').canPlayType"
71                         "('video/" + container + "')")
72            status = self.tab.EvaluateJavaScript(js_script)
73            if status != MEDIA_SUPPORT_AVAILABLE:
74                raise error.TestError('No media support available for %s.'
75                                       % container)
76        # Waiting for test video to load.
77        wait_time = 0 # seconds
78        current_time_js = ("typeof videoCurTime != 'undefined' ? "
79                           "videoCurTime.innerHTML : 0")
80        while float(self.tab.EvaluateJavaScript(current_time_js)) < 1.0:
81            time.sleep(1)
82            wait_time = wait_time + 1
83            if wait_time > WAIT_TIMEOUT_S:
84                raise error.TestError('Video failed to load.')
85        # Muting the video.
86        self.tab.EvaluateJavaScript('testvideo.volume=0')
87
88
89        playback_test_count = 0
90        prev_time_s = -1
91        duration = self.video_duration()
92
93        while True:
94            current_time_s = self.video_current_time()
95
96            if (current_time_s >= duration
97                or playback_test_count >= PLAYBACK_TEST_TIME_S):
98                break
99
100            if current_time_s <= prev_time_s:
101                msg = ("Current time is %.3fs while Previous time was %.3fs. "
102                       "Video is not playing" % (current_time_s, prev_time_s))
103                raise error.TestError(msg)
104
105            prev_time_s = current_time_s
106            playback_test_count += 1
107            time.sleep(1)
108
109
110    def run_once(self):
111        boards_to_skip = ['x86-mario', 'x86-zgb']
112        # TODO(scottz): Remove this when crbug.com/220147 is fixed.
113        dut_board = utils.get_current_board()
114        if dut_board in boards_to_skip:
115            logging.info("Skipping test run on this board.")
116            return
117        with chrome.Chrome() as cr:
118            self.run_video_sanity_test(cr.browser)
119