Index: LayoutTests/media/video-autoplay-experiment.html |
diff --git a/LayoutTests/media/video-autoplay-experiment.html b/LayoutTests/media/video-autoplay-experiment.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bf6340045f3c025ccafc5e6a5cd7e4f923fd856f |
--- /dev/null |
+++ b/LayoutTests/media/video-autoplay-experiment.html |
@@ -0,0 +1,192 @@ |
+<video autoplay controls></video> |
+<script src=media-file.js></script> |
+<script src=video-test.js></script> |
+<body> |
+<pre> |
+ Check if the autoplay gesture override experiment works. There are a lot |
+ of config options, so this test just runs all of them. |
+ |
+ The "results" table contains one row per config tested. |
+ == Test Inputs == |
+ # - config number, in case you'd like to run just one. |
+ Exp - autoplay experiment setting being tested. |
+ A-play - how is autoplay requested? |
+ none - autoplay is not requested. |
+ attr - autoplay attribute is set on the element. |
+ js - play() called after media is ready to play. |
+ Mute - how is media muted? |
+ none - media is not muted. |
+ attr - muted attribute is set on the element. |
+ js - muted property is set after media is ready to play. |
+ Vis - is media visible? |
+ onscreen - element starts out onscreen. |
+ scroll - element starts offscreen, scrolled into view once |
+ it is ready to play. |
+ offscreen - element starts out offscreen and stays offscreen. |
+ |
+ == Test Outputs == |
+ Early? - did playback start before element was scrolled onscreen? For |
+ tests in which Vis!=scroll, this is reported as "-". |
+ Played? - did playback start by the conclusion of the test? |
+ Muted? - was the media muted? If the media didn't play, then this is |
+ reported as "-". |
+ |
+</pre> |
+<table id="results"> |
+<tr> |
+<td>#</td> |
+<td>Exp</td> |
+<td>A-play</td> |
+<td>Mute</td> |
+<td>Vis</td> |
+<td>Early?</td> |
+<td>Played?</td> |
+<td>Muted?</td> |
+</tr> |
+</table> |
+</body> |
+ |
+<script> |
+ |
+// Starting configuration number. This should be zero normally. |
+var configNumber = 0; |
+ |
+var mediaFile = findMediaFile("video", "content/test"); |
+var onscreenParent = document.createElement("div"); |
+// The onscreen parent's height is also used to make sure that the off-screen |
+// parent is, in fact, off-screen. |
+onscreenParent.style.height="1000px"; |
+document.body.insertBefore(onscreenParent, document.body.firstChild); |
+// Also create another root that's off the bottom of the window. |
+var offscreenParent = document.createElement("div"); |
+document.body.appendChild(offscreenParent); |
+ |
+function didPlaybackStart(video) |
+{ |
+ // We say that the video started if it's not paused or if it played and |
+ // already ended. |
+ return !video.paused || video.ended; |
+} |
+ |
+function addResultsRow(spec) |
+{ |
+ // Add a row to the results table. |
+ var row = document.getElementById("results").insertRow(-1); |
+ var td = row.insertCell(0); |
+ |
+ var fields = [ "experimentNumber", "experimentType", "autoplayType", "mutedType", "visType", |
+ "playedEarly", "played", "muted"]; |
+ for(idx in fields) |
+ row.insertCell(-1).innerText = (""+spec[fields[idx]]).substring(0,7); |
+} |
+ |
+function checkVideoStatus(video, spec) |
+{ |
+ // Now that we can play, if we're supposed to play / mute via js do so. |
+ if (spec.mutedType == "js") |
+ video.muted = true; |
+ if(spec.autoplayType == "js") |
+ video.play(); |
+ |
+ // Update the spec with the results. |
+ var didStart = didPlaybackStart(video); |
+ video.spec.played = didStart ? "played" : "no"; |
+ video.spec.muted = didStart ? (video.muted ? "muted" : "unmuted") : "-"; |
+ |
+ addResultsRow(video.spec); |
+ video.parentElement.removeChild(video); |
+ |
+ // Scroll back to the top, in case this was a scrolling test. |
+ onscreenParent.scrollIntoView(); |
+ |
+ // Start the next config, but let the event queue drain. |
+ setTimeout(runNextConfig, 0); |
+} |
+ |
+function runOneConfig(spec) |
+{ |
+ internals.settings.setAutoplayExperimentMode(spec.experimentType); |
+ |
+ // Create, configure, and attach a media element. |
+ var video = document.createElement("video"); |
+ |
+ // Pick whether the element will be visible when canPlayThrough. |
+ if (spec.visType == "onscreen") |
+ onscreenParent.appendChild(video); |
+ else |
+ offscreenParent.appendChild(video); |
+ |
+ // Set any attributes before canPlayThrough. |
+ if (spec.mutedType == "attr") |
+ video.setAttribute("muted", "true"); |
+ if (spec.autoplayType == "attr") |
+ video.setAttribute("autoplay", "true"); |
+ |
+ spec.playedEarly = "-"; |
+ |
+ // Record the spec in the video element, so that we can display the |
+ // results later. |
+ video.spec = spec; |
+ |
+ // Wait for canplaythrough before continuing, so that the media |
+ // might actually be playing. |
+ video.addEventListener("canplaythrough", function() |
+ { |
+ // If we're supposed to scroll the item onscreen after it is ready to |
+ // play, then do so now. |
+ if(spec.visType == "scroll") { |
+ // Record the play state here, too, before we scroll. |
+ spec.playedEarly = didPlaybackStart(video) ? "yes" : "no"; |
+ |
+ // We are supposed to scroll the player into view. Doing so |
+ // earlier would work, but might not test the scroll listener. |
+ video.scrollIntoView(true); |
+ // Wait for HTMLMediaElement to get the scroll event before |
+ // recording the results. |
+ setTimeout(function() { checkVideoStatus(video, spec); }, 0); |
+ } else { |
+ // Record the results immediately. |
+ checkVideoStatus(video, spec); |
+ } |
+ }); |
+ |
+ // Set the source, which will eventually lead to canPlayThrough. |
+ video.src=mediaFile; |
+} |
+ |
+var experimentTypes = ["none", "always", "if-muted", "play-muted"]; |
+var autoplayTypes = ["none", "attr", "js"]; |
+var mutedTypes = ["none", "attr", "js"]; |
+var visTypes = ["onscreen", "scroll", "offscreen"]; |
+ |
+function runNextConfig() |
+{ |
+ // Convert configNumber into a spec, and run it. |
+ var exp = configNumber; |
+ |
+ // Convert this experiment number into settings. |
+ var spec = {}; |
+ spec.experimentType = experimentTypes[exp % experimentTypes.length]; |
+ exp = Math.floor(exp / experimentTypes.length); |
+ spec.autoplayType = autoplayTypes[exp % autoplayTypes.length]; |
+ exp = Math.floor(exp / autoplayTypes.length); |
+ spec.mutedType = mutedTypes[exp % mutedTypes.length]; |
+ exp = Math.floor(exp / mutedTypes.length); |
+ spec.visType = visTypes[exp % visTypes.length]; |
+ exp = Math.floor(exp / visTypes.length); |
+ spec.experimentNumber = configNumber; |
+ |
+ // exp>0 here indicates that we've wrapped around. |
+ if (exp > 0) { |
+ endTest(); |
+ } |
+ |
+ configNumber++; |
+ runOneConfig(spec); |
+} |
+ |
+window.internals.settings.setMediaPlaybackRequiresUserGesture(true); |
+window.internals.settings.setOverrideOptimizedForMobileCheck(true); |
+runNextConfig(); |
+ |
+</script> |