Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / media / media-continues-playing-after-replace-source.html
blob8fef6fb166b77aa22526c6a529072bc4ef215b77
1 <html>
2 <title>Test media source replacement</title>
3 <body>
5 <p>Test that media keeps playing when the source element is replaced.</p>
7 <script src=video-test.js></script>
8 <script src=media-file.js></script>
9 <script src=../resources/gc.js></script>
10 <script>
11 var timeupdateEventCount = 0;
12 var skippedCount = 0;
13 var sourceReplaced = false;
15 function swapAudio() {
16 v = document.getElementsByTagName('audio')[0];
17 v.removeChild(v.childNodes[0]);
18 var s = document.createElement('source');
19 s.src = findMediaFile("audio", "content/test");
20 v.appendChild(s);
23 function errorListener(event)
25 logResult(false, "Caught 'error' event, audio.error.code = " + this.error.code);
26 endTest();
29 function canplaythroughListener(event)
31 consoleWrite("EVENT(canplaythrough)");
32 testElement = this;
33 testExpected("testElement.currentTime", 0);
34 this.play();
37 function playingListener(event)
39 consoleWrite("EVENT(playing)");
42 function timeupdateListener(event)
44 ++timeupdateEventCount;
46 if (timeupdateEventCount-skippedCount == 1) {
47 // If the media play has started it should continue even if
48 // source was replaced. Wait for two timeupdate events to
49 // make sure the same source keeps playing.
50 if (sourceReplaced) {
51 if (skippedCount >= 1 || this.currentTime > 0) {
52 testElement = this;
53 testExpected("testElement.currentTime", 0, '>');
54 } else {
55 // The time is not 0 as expected. Make sure same
56 // source keeps playing by waiting for one more event.
57 ++skippedCount;
60 } else if (timeupdateEventCount-skippedCount >= 2) {
61 // We wait 2 timeupdate events so we are sure the media engine
62 // is playing the media, and make sure time is advancing.
63 testElement = this;
64 testExpected("testElement.currentTime", 0, '>');
65 if (!sourceReplaced) {
66 consoleWrite("Replacing the media's source element:");
67 sourceReplaced = true;
68 timeupdateEventCount = 0;
69 skippedCount = 0;
70 // The ports are not consistent in regards to whether
71 // the canplaythrough and playing events are triggered
72 // a second time, so stop listening for them. This was
73 // done to help the cr-linux test pass, and does not
74 // necessarily indicate a problem.
75 this.removeEventListener('playing', playingListener);
76 this.removeEventListener('canplaythrough', canplaythroughListener);
77 swapAudio();
78 } else {
79 this.removeEventListener('timeupdate', timeupdateListener);
80 this.pause();
81 endTest();
86 function testAudioElement(count)
88 timeupdateEventCount = 0;
89 skippedCount = 0;
90 var audioElement = document.getElementsByTagName('audio')[count];
91 //audioElement.removeChild(audioElement.childNodes[0]);
92 audioElement.addEventListener('error', errorListener);
93 audioElement.addEventListener('canplaythrough', canplaythroughListener);
94 audioElement.addEventListener('timeupdate', timeupdateListener);
95 audioElement.addEventListener('playing', playingListener);
97 var s = document.createElement('source');
98 s.src = findMediaFile("audio", "content/silence");
99 audioElement.appendChild(s);
103 document.write("<audio controls></audio>");
104 testAudioElement(0);
105 gc();
106 </script>
108 </body>
109 </html>