[Modern Media Controls] Turn media/modern-media-controls/ios-inline-media-controls...
[WebKit-https.git] / LayoutTests / media / media-continues-playing-after-replace-source.html
1 <html>
2     <title>Test media source replacement</title>
3     <body>
4
5     <p>Test that media keeps playing when the source element is replaced.</p>
6
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;
14
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);
21         }
22
23         function errorListener(event)
24         {
25              logResult(false, "Caught 'error' event, audio.error.code = " + this.error.code);
26              endTest();
27         }
28
29         function canplaythroughListener(event)
30         {
31              consoleWrite("EVENT(canplaythrough)");
32              testElement = this;
33              testExpected("testElement.currentTime", 0);
34              this.play();
35         }
36
37         function playingListener(event)
38         {
39              consoleWrite("EVENT(playing)");
40         }
41
42         function timeupdateListener(event)
43         {
44             ++timeupdateEventCount;
45
46             if (timeupdateEventCount-skippedCount == 1) {
47                 // First time update after source replacement should be 0.
48                 // We allow one late time update to come in from the previous
49                 // source element. This was done to help the cr-linux test
50                 // pass, and does not necessarily indicate a problem.
51                 if (sourceReplaced) {
52                     if (skippedCount >= 1 || this.currentTime == 0) {
53                         testElement = this;
54                         testExpected("testElement.currentTime", 0);
55                     } else {
56                         // The time is not 0 as expected. Hope this is a
57                         // late update from the previous source.
58                         ++skippedCount;
59                     }
60                 }
61             } else if (timeupdateEventCount-skippedCount >= 2) {
62                 // We wait 2 timeupdate events so we are sure the media engine
63                 // is playing the media, and make sure time is advancing.
64                 testElement = this;
65                 testExpected("testElement.currentTime", 0, '>');
66                 if (!sourceReplaced) {
67                     consoleWrite("Replacing the media's source element:");
68                     sourceReplaced = true;
69                     timeupdateEventCount = 0;
70                     skippedCount = 0;
71                     // The ports are not consistent in regards to whether
72                     // the canplaythrough and playing events are triggered
73                     // a second time, so stop listening for them. This was
74                     // done to help the cr-linux test pass, and does not
75                     // necessarily indicate a problem.
76                     this.removeEventListener('playing', playingListener);
77                     this.removeEventListener('canplaythrough', canplaythroughListener);
78                     swapAudio();
79                 } else {
80                     this.removeEventListener('timeupdate', timeupdateListener);
81                     this.pause();
82                     endTest();
83                 }
84             }
85         }
86
87         function testAudioElement(count)
88         {
89             timeupdateEventCount = 0;
90             skippedCount = 0;
91             var audioElement = document.getElementsByTagName('audio')[count];
92             //audioElement.removeChild(audioElement.childNodes[0]);
93             audioElement.addEventListener('error', errorListener);
94             audioElement.addEventListener('canplaythrough', canplaythroughListener);
95             audioElement.addEventListener('timeupdate', timeupdateListener);
96             audioElement.addEventListener('playing', playingListener);
97
98             var s = document.createElement('source');
99             s.src = findMediaFile("audio", "content/silence");
100             audioElement.appendChild(s);
101
102         }
103
104         document.write("<audio controls></audio>");
105         testAudioElement(0);
106         gc();
107         gc();
108     </script>
109
110     </body>
111 </html>