OLD | NEW |
(Empty) | |
| 1 <video autoplay controls></video> |
| 2 <script src=media-file.js></script> |
| 3 <script src=video-test.js></script> |
| 4 <body> |
| 5 <pre> |
| 6 Check if the autoplay gesture override experiment works. There are a lot |
| 7 of config options, so this test just runs all of them. |
| 8 |
| 9 The "results" table contains one row per config tested. |
| 10 == Test Inputs == |
| 11 # - config number, in case you'd like to run just one. |
| 12 Flags - autoplay experiment setting being tested. |
| 13 E - "enabled" in command line. |
| 14 V - "-ifvisible" |
| 15 M - "-ifmuted" |
| 16 p - "-playmuted" |
| 17 For example, EVM means "enabled-ifvisible-ifmuted". |
| 18 This test does not check -ifmobile since that check always |
| 19 fails outside of android. |
| 20 Play w/- how is play requested? |
| 21 none - play is not requested. |
| 22 attr - autoplay attribute is set on the element. |
| 23 play() - play() called after media is ready to play. |
| 24 Mute - how is media muted? |
| 25 none - media is not muted. |
| 26 attr - muted attribute is set on the element. |
| 27 js - muted property is set after media is ready to play. |
| 28 Vis - is media visible? |
| 29 onscreen - element starts out onscreen. |
| 30 scroll - element starts offscreen, scrolled into view once |
| 31 it is ready to play. |
| 32 offscreen - element starts out offscreen and stays offscreen. |
| 33 |
| 34 == Test Outputs == |
| 35 Early? - did playback start before element was scrolled onscreen? For |
| 36 tests in which Vis!=scroll, this is reported as "-". |
| 37 Played? - did playback start by the conclusion of the test? |
| 38 Muted? - was the media muted? If the media didn't play, then this is |
| 39 reported as "-". |
| 40 |
| 41 </pre> |
| 42 <table id="results"> |
| 43 <tr> |
| 44 <td>#</td> |
| 45 <td>Flags</td> |
| 46 <td>Play w/</td> |
| 47 <td>Mute</td> |
| 48 <td>Vis</td> |
| 49 <td>Early?</td> |
| 50 <td>Played?</td> |
| 51 <td>Muted?</td> |
| 52 </tr> |
| 53 </table> |
| 54 </body> |
| 55 |
| 56 <script> |
| 57 |
| 58 // Starting configuration number. This should be zero normally. |
| 59 var configNumber = 0; |
| 60 |
| 61 var mediaFile = findMediaFile("video", "content/test"); |
| 62 var onscreenParent = document.createElement("div"); |
| 63 // The onscreen parent's height is also used to make sure that the off-screen |
| 64 // parent is, in fact, off-screen. |
| 65 onscreenParent.style.height="1000px"; |
| 66 document.body.insertBefore(onscreenParent, document.body.firstChild); |
| 67 // Also create another root that's off the bottom of the window. |
| 68 var offscreenParent = document.createElement("div"); |
| 69 document.body.appendChild(offscreenParent); |
| 70 |
| 71 function didPlaybackStart(video) |
| 72 { |
| 73 // We say that the video started if it's not paused or if it played and |
| 74 // already ended. |
| 75 return !video.paused || video.ended; |
| 76 } |
| 77 |
| 78 function addResultsRow(spec) |
| 79 { |
| 80 // Add a row to the results table. |
| 81 var row = document.getElementById("results").insertRow(-1); |
| 82 var td = row.insertCell(0); |
| 83 |
| 84 // Add experiment number |
| 85 row.insertCell(-1).innerText = (""+spec["experimentNumber"]); |
| 86 |
| 87 // Process experiment type specially. |
| 88 var type = spec["experimentType"]; |
| 89 var smallType = ""; |
| 90 smallType += (type.indexOf("enabled")>-1)?"E":""; |
| 91 smallType += (type.indexOf("-ifvisible")>-1)?"V":""; |
| 92 smallType += (type.indexOf("-ifmuted")>-1)?"M":""; |
| 93 smallType += (type.indexOf("-playmuted")>-1)?"p":""; |
| 94 row.insertCell(-1).innerText = smallType; |
| 95 |
| 96 // Add remaining fields. |
| 97 var fields = [ "autoplayType", "mutedType", "visType", "playedEarly", |
| 98 "played", "muted"]; |
| 99 for(idx in fields) |
| 100 row.insertCell(-1).innerText = spec[fields[idx]].substring(0,7); |
| 101 } |
| 102 |
| 103 function configureVideoViaScript(video, spec) |
| 104 { |
| 105 if (spec.mutedType == "js") |
| 106 video.muted = true; |
| 107 if(spec.autoplayType == "play()") |
| 108 video.play(); |
| 109 } |
| 110 |
| 111 function queueNextExperiment() |
| 112 { |
| 113 // Start the next config, but let the event queue drain. |
| 114 setTimeout(runNextConfig, 0); |
| 115 } |
| 116 |
| 117 function checkVideoStatus(video, spec) |
| 118 { |
| 119 // Update the spec with the results. |
| 120 var didStart = didPlaybackStart(video); |
| 121 video.spec.played = didStart ? "played" : "no"; |
| 122 video.spec.muted = didStart ? (video.muted ? "muted" : "unmuted") : "-"; |
| 123 |
| 124 addResultsRow(video.spec); |
| 125 video.parentElement.removeChild(video); |
| 126 |
| 127 // Scroll back to the top, in case this was a scrolling test. |
| 128 onscreenParent.scrollIntoView(); |
| 129 |
| 130 queueNextExperiment(); |
| 131 } |
| 132 |
| 133 function runOneConfig(spec) |
| 134 { |
| 135 internals.settings.setAutoplayExperimentMode(spec.experimentType); |
| 136 |
| 137 // Create, configure, and attach a media element. |
| 138 var video = document.createElement("video"); |
| 139 |
| 140 // Pick whether the element will be visible when canPlayThrough. |
| 141 if (spec.visType == "onscreen") |
| 142 onscreenParent.appendChild(video); |
| 143 else |
| 144 offscreenParent.appendChild(video); |
| 145 |
| 146 // Set any attributes before canPlayThrough. |
| 147 if (spec.mutedType == "attr") |
| 148 video.setAttribute("muted", "true"); |
| 149 if (spec.autoplayType == "attr") |
| 150 video.setAttribute("autoplay", "true"); |
| 151 |
| 152 spec.playedEarly = "-"; |
| 153 |
| 154 // Record the spec in the video element, so that we can display the |
| 155 // results later. |
| 156 video.spec = spec; |
| 157 |
| 158 // Wait for canplaythrough before continuing, so that the media |
| 159 // might actually be playing. |
| 160 video.addEventListener("canplaythrough", function() |
| 161 { |
| 162 // Now that we can play, if we're supposed to play / mute via js do so. |
| 163 configureVideoViaScript(video, spec); |
| 164 |
| 165 // If we're supposed to scroll the item onscreen after it is ready to |
| 166 // play, then do so now. |
| 167 if(spec.visType == "scroll") { |
| 168 // Record the play state here, too, before we scroll. |
| 169 spec.playedEarly = didPlaybackStart(video) ? "yes" : "no"; |
| 170 |
| 171 // We are supposed to scroll the player into view. |
| 172 video.scrollIntoView(true); |
| 173 // We cause two visibility checks. The first notices the scroll, |
| 174 // while the second notices that scrolling has stopped. |
| 175 // It would be nicer to trigger location polling here, but that |
| 176 // can take ~second to notice that scrolling is stopped. |
| 177 window.internals.triggerAutoplayVisibilityCheck(video); |
| 178 window.internals.triggerAutoplayVisibilityCheck(video); |
| 179 // Once those are return, the state should be correct. |
| 180 checkVideoStatus(video, spec); |
| 181 } else { |
| 182 // Record the results immediately. |
| 183 checkVideoStatus(video, spec); |
| 184 } |
| 185 }); |
| 186 |
| 187 // Set the source, which will eventually lead to canPlayThrough. |
| 188 video.src=mediaFile; |
| 189 } |
| 190 |
| 191 var experimentTypes = [ |
| 192 "none", |
| 193 "enabled", |
| 194 "enabled-ifvisible", |
| 195 "enabled-ifvisible-ifmuted", |
| 196 "enabled-ifvisible-playmuted" |
| 197 ]; |
| 198 var autoplayTypes = ["none", "attr", "play()"]; |
| 199 var mutedTypes = ["none", "attr", "js"]; |
| 200 var visTypes = ["onscreen", "scroll", "offscreen"]; |
| 201 |
| 202 function runNextConfig() |
| 203 { |
| 204 // Convert configNumber into a spec, and run it. |
| 205 var exp = configNumber; |
| 206 |
| 207 // Convert this experiment number into settings. |
| 208 var spec = {}; |
| 209 spec.experimentType = experimentTypes[exp % experimentTypes.length]; |
| 210 exp = Math.floor(exp / experimentTypes.length); |
| 211 spec.autoplayType = autoplayTypes[exp % autoplayTypes.length]; |
| 212 exp = Math.floor(exp / autoplayTypes.length); |
| 213 spec.mutedType = mutedTypes[exp % mutedTypes.length]; |
| 214 exp = Math.floor(exp / mutedTypes.length); |
| 215 spec.visType = visTypes[exp % visTypes.length]; |
| 216 exp = Math.floor(exp / visTypes.length); |
| 217 spec.experimentNumber = configNumber; |
| 218 |
| 219 // exp>0 here indicates that we've wrapped around. |
| 220 if (exp > 0) { |
| 221 endTest(); |
| 222 } |
| 223 |
| 224 configNumber++; |
| 225 |
| 226 // To keep the test fast, skip a few combinations. |
| 227 var skip = false; |
| 228 if (spec.autoplayType == "none" && spec.visType != 'onscreen') |
| 229 skip = true; |
| 230 else if (spec.experimentType.indexOf("-ifmuted") > -1 && spec.visType != "on
screen") |
| 231 skip = true; |
| 232 else if (spec.visType == "offscreen" && spec.autoplayType != "attr") |
| 233 skip = true; |
| 234 else if (spec.experimentType.indexOf("-ifmuted") == -1 && spec.mutedType !=
"none") |
| 235 skip = true; |
| 236 |
| 237 if (skip) |
| 238 queueNextExperiment(); |
| 239 else |
| 240 runOneConfig(spec); |
| 241 } |
| 242 |
| 243 window.internals.settings.setMediaPlaybackRequiresUserGesture(true); |
| 244 runNextConfig(); |
| 245 |
| 246 </script> |
OLD | NEW |