Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <html> | 1 <!DOCTYPE html> |
| 2 <title>Test media source replacement</title> | 2 <title>Test that media keeps playing when the source element is replaced.</title > |
| 3 <body> | 3 <script src="../resources/testharness.js"></script> |
| 4 <script src="../resources/testharnessreport.js"></script> | |
| 5 <script src="media-file.js"></script> | |
| 6 <audio></audio> | |
| 7 <script> | |
| 8 async_test(function(t) { | |
| 9 var timeupdateEventCount = 0; | |
| 10 var sourceReplaced = false; | |
| 11 | |
| 12 var audio = document.querySelector("audio"); | |
| 13 var source = document.createElement("source"); | |
| 14 source.src = findMediaFile("audio", "content/silence"); | |
| 15 audio.appendChild(source); | |
| 4 | 16 |
| 5 <p>Test that media keeps playing when the source element is replaced.</p> | 17 audio.onerror = t.unreached_func(); |
| 6 | 18 |
| 7 <!-- TODO(foolip): Convert test to testharness.js. crbug.com/588956 | 19 audio.ontimeupdate = t.step_func(function() { |
| 8 (Please avoid writing new tests using video-test.js) --> | 20 ++timeupdateEventCount; |
| 9 <script src=video-test.js></script> | |
| 10 <script src=media-file.js></script> | |
| 11 <script src=../resources/gc.js></script> | |
| 12 <script> | |
| 13 var timeupdateEventCount = 0; | |
| 14 var skippedCount = 0; | |
| 15 var sourceReplaced = false; | |
| 16 | 21 |
| 17 function swapAudio() { | 22 if (timeupdateEventCount == 1) { |
| 18 v = document.getElementsByTagName('audio')[0]; | 23 // If the media play has started it should continue even if |
| 19 v.removeChild(v.childNodes[0]); | 24 // source was replaced. Wait for two timeupdate events to |
| 20 var s = document.createElement('source'); | 25 // make sure the same source keeps playing. |
| 21 s.src = findMediaFile("audio", "content/test"); | 26 if (sourceReplaced) |
| 22 v.appendChild(s); | 27 assert_greater_than(audio.currentTime, 0); |
| 23 } | 28 } else if (timeupdateEventCount == 2) { |
| 24 | 29 // We wait 2 timeupdate events so we are sure the media engine |
| 25 function errorListener(event) | 30 // is playing the media, and make sure time is advancing. |
| 26 { | 31 assert_greater_than(audio.currentTime, 0); |
| 27 logResult(false, "Caught 'error' event, audio.error.code = " + this .error.code); | 32 if (!sourceReplaced) { |
| 28 endTest(); | 33 // Replacing the media's source element. |
| 29 } | 34 sourceReplaced = true; |
| 30 | 35 timeupdateEventCount = 0; |
|
foolip
2016/06/02 09:37:03
This line makes the structure of the test harder t
Srirama
2016/06/02 13:29:37
Acknowledged.
| |
| 31 function canplaythroughListener(event) | 36 |
| 32 { | 37 audio.removeChild(source); |
| 33 consoleWrite("EVENT(canplaythrough)"); | 38 var newSource = document.createElement("source"); |
| 34 testElement = this; | 39 newSource.src = findMediaFile("audio", "content/test"); |
| 35 testExpected("testElement.currentTime", 0); | 40 audio.appendChild(newSource); |
| 36 this.play(); | 41 } else { |
| 37 } | 42 t.done(); |
| 38 | |
| 39 function playingListener(event) | |
| 40 { | |
| 41 consoleWrite("EVENT(playing)"); | |
| 42 } | |
| 43 | |
| 44 function timeupdateListener(event) | |
| 45 { | |
| 46 ++timeupdateEventCount; | |
| 47 | |
| 48 if (timeupdateEventCount-skippedCount == 1) { | |
| 49 // If the media play has started it should continue even if | |
| 50 // source was replaced. Wait for two timeupdate events to | |
| 51 // make sure the same source keeps playing. | |
| 52 if (sourceReplaced) { | |
| 53 if (skippedCount >= 1 || this.currentTime > 0) { | |
| 54 testElement = this; | |
| 55 testExpected("testElement.currentTime", 0, '>'); | |
| 56 } else { | |
| 57 // The time is not 0 as expected. Make sure same | |
| 58 // source keeps playing by waiting for one more event. | |
| 59 ++skippedCount; | |
| 60 } | |
| 61 } | |
| 62 } else if (timeupdateEventCount-skippedCount >= 2) { | |
| 63 // We wait 2 timeupdate events so we are sure the media engine | |
| 64 // is playing the media, and make sure time is advancing. | |
| 65 testElement = this; | |
| 66 testExpected("testElement.currentTime", 0, '>'); | |
| 67 if (!sourceReplaced) { | |
| 68 consoleWrite("Replacing the media's source element:"); | |
| 69 sourceReplaced = true; | |
| 70 timeupdateEventCount = 0; | |
| 71 skippedCount = 0; | |
| 72 // The ports are not consistent in regards to whether | |
| 73 // the canplaythrough and playing events are triggered | |
| 74 // a second time, so stop listening for them. This was | |
| 75 // done to help the cr-linux test pass, and does not | |
| 76 // necessarily indicate a problem. | |
| 77 this.removeEventListener('playing', playingListener); | |
| 78 this.removeEventListener('canplaythrough', canplaythroughLis tener); | |
| 79 swapAudio(); | |
| 80 } else { | |
| 81 this.removeEventListener('timeupdate', timeupdateListener); | |
| 82 this.pause(); | |
| 83 endTest(); | |
| 84 } | |
| 85 } | 43 } |
| 86 } | 44 } |
| 45 }); | |
| 87 | 46 |
| 88 function testAudioElement(count) | 47 assert_equals(audio.currentTime, 0); |
| 89 { | 48 audio.play(); |
| 90 timeupdateEventCount = 0; | 49 }); |
| 91 skippedCount = 0; | 50 </script> |
| 92 var audioElement = document.getElementsByTagName('audio')[count]; | |
| 93 //audioElement.removeChild(audioElement.childNodes[0]); | |
| 94 audioElement.addEventListener('error', errorListener); | |
| 95 audioElement.addEventListener('canplaythrough', canplaythroughListen er); | |
| 96 audioElement.addEventListener('timeupdate', timeupdateListener); | |
| 97 audioElement.addEventListener('playing', playingListener); | |
| 98 | |
| 99 var s = document.createElement('source'); | |
| 100 s.src = findMediaFile("audio", "content/silence"); | |
| 101 audioElement.appendChild(s); | |
| 102 | |
| 103 } | |
| 104 | |
| 105 document.write("<audio controls></audio>"); | |
| 106 testAudioElement(0); | |
| 107 gc(); | |
| 108 </script> | |
| 109 | |
| 110 </body> | |
| 111 </html> | |
| OLD | NEW |