1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium 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
6"""Basic playback test.  Checks playback, seek, and replay based on events.
7
8This test uses the bear videos from the test matrix in h264, vp8, and theora
9formats.
10"""
11import logging
12import os
13
14import pyauto_media
15import pyauto
16
17
18# HTML test path; relative to src/chrome/test/data.
19_TEST_HTML_PATH = os.path.join('media', 'html', 'media_basic_playback.html')
20
21# Test videos to play.  TODO(dalecurtis): Convert to text matrix parser when we
22# have more test videos in the matrix.  Code already written, see patch here:
23# https://chromiumcodereview.appspot.com/9290008/#ps12
24_TEST_VIDEOS = [
25    pyauto.PyUITest.GetFileURLForContentDataPath('media', name)
26    for name in ['bear.mp4', 'bear.ogv', 'bear.webm', 'bear_silent.mp4',
27                 'bear_silent.ogv', 'bear_silent.webm']]
28
29# Expected events for the first iteration and every iteration thereafter.
30_EXPECTED_EVENTS_0 = [('ended', 2), ('playing', 2), ('seeked', 1),
31                      ('suspend', 1)]
32_EXPECTED_EVENTS_n = [('abort', 1), ('emptied', 1)] + _EXPECTED_EVENTS_0
33
34
35class MediaBasicPlaybackTest(pyauto.PyUITest):
36  """PyAuto test container.  See file doc string for more information."""
37
38  def testBasicPlaybackMatrix(self):
39    """Launches HTML test which plays each video until end, seeks, and replays.
40
41    Specifically ensures that after the above sequence of events, the following
42    are true:
43
44        1. The first video has only 2x playing, 2x ended, and 1x seeked events.
45        2. Each subsequent video additionally has 1x abort and 1x emptied due to
46           switching of the src attribute.
47        3. video.currentTime == video.duration for each video.
48
49    See the HTML file at _TEST_HTML_PATH for more information.
50    """
51    self.NavigateToURL(self.GetFileURLForDataPath(_TEST_HTML_PATH))
52
53    for i, media in enumerate(_TEST_VIDEOS):
54      logging.debug('Running basic playback test for %s', media)
55
56      # Block until the test finishes and notifies us.  Upon return the value of
57      # video.currentTime == video.duration is provided.
58      try:
59        self.assertTrue(self.ExecuteJavascript("startTest('%s');" % media))
60
61        # PyAuto has trouble with arrays, so convert to string prior to request.
62        events = self.GetDOMValue("events.join(',')").split(',')
63        counts = [(item, events.count(item)) for item in sorted(set(events))]
64
65        # The first loop will not have the abort and emptied events triggered by
66        # changing the video src.
67        if (i == 0):
68          self.assertEqual(counts, _EXPECTED_EVENTS_0)
69        else:
70          self.assertEqual(counts, _EXPECTED_EVENTS_n)
71      except:
72        logging.debug(
73            'Test failed with events: %s', self.GetDOMValue("events.join(',')"))
74        raise
75
76
77if __name__ == '__main__':
78  pyauto_media.Main()
79