1 // Copyright (c) 2012 The Chromium 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.
5 // Common js for the HTML5_* tests. The following variables need to be defined
6 // before this js is included:
7 // - 'willPlay' - indicates if the media is expected to start playing during
9 // - 'testNetworkEvents' - if set to true, the test will test for the
10 // loadstart and stalled events. NOTE that since the loadstart event fires
11 // very early, to test for it reliably, the source of the media tag
12 // should be added after this script is included or add
13 // 'onLoadStart=mediEventHandler' as an attribute to the media element.
14 // Also to reliably test the stalled event, the the test should wait for the
15 // prerendered page's title to change to "READY" before calling
18 function assert(bool) {
20 throw new Error('Assert Failed.');
23 var canPlaySeen = false;
24 var playingSeen = false;
25 var canPlayThroughSeen = false;
26 var loadStartSeen = false;
27 var stalledSeen = false;
30 assert(typeof(willPlay) != 'undefined');
31 assert(typeof(testNetworkEvents) != 'undefined');
33 var mediaEl = document.getElementById("mediaEl");
35 function mediaEventHandler(e) {
44 case 'canplaythrough':
46 canPlayThroughSeen = true;
55 assert(loadStartSeen);
57 if (testNetworkEvents) {
58 document.title = 'READY';
63 var progressDone = (willPlay && canPlayThroughSeen && playingSeen) ||
64 (!willPlay && canPlayThroughSeen && !playingSeen);
67 document.title = 'PASS';
70 mediaEl.addEventListener('playing', mediaEventHandler, false);
71 mediaEl.addEventListener('canplay', mediaEventHandler, false);
72 mediaEl.addEventListener('canplaythrough', mediaEventHandler, false);
73 mediaEl.addEventListener('error', mediaEventHandler, false);
75 if (testNetworkEvents) {
76 mediaEl.addEventListener('stalled', mediaEventHandler, false);
77 mediaEl.addEventListener('loadstart', mediaEventHandler, false);
80 // TODO(shishir): Remove this once http://crbug.com/130788 is fixed.
81 function printDebugInfo() {
82 console.log("\ncanPlaySeen: " + canPlaySeen);
83 console.log("playingSeen: " + playingSeen);
84 console.log("canPlayThroughSeen: " + canPlayThroughSeen);
85 console.log("loadStartSeen: " + loadStartSeen);
86 console.log("stalledSeen: " + stalledSeen);
87 console.log("hasError: " + hasError + "\n");
89 setInterval(printDebugInfo, 5000);
91 function DidPrerenderPass() {
92 // The media should not have started at this point.
93 return !canPlaySeen && !playingSeen && !hasError &&
94 mediaEl.currentTime == 0 &&
95 mediaEl.readyState == mediaEl.HAVE_NOTHING &&
96 (!testNetworkEvents || stalledSeen);
99 function DidDisplayPass() {
100 // The actual test is done via the TitleWatcher.