2 <video autoplay controls
></video>
3 <script src=media-file.js
></script>
4 <script src=video-test.js
></script>
7 Check if the autoplay gesture override experiment works. There are a lot
8 of config options, so this test just runs all of them.
10 The
"results" table contains one row per config tested.
12 # - config number, in case you'd like to run just one.
13 Flags - autoplay experiment setting being tested.
19 For example, EM means
"enabled-ifmuted".
20 Type - audio or video element?
23 Play w/- how is play requested?
24 none - play is not requested.
25 attr - autoplay attribute is set on the element.
26 play() - play() called after media is ready to play.
27 Mute - how is media muted?
28 no - media is not muted.
29 yes - muted attribute is set on the element.
30 Mobile - is page optimized for mobile?
31 no - page is not optimized for mobile.
32 yes - page is optimized for mobile.
35 Played? - did playback start by the conclusion of the test?
36 Muted? - was the media muted? If the media didn't play, then this is
56 // Starting configuration number. This should be zero normally.
59 var mediaFile
= findMediaFile("video", "content/test");
60 var onscreenParent
= document
.createElement("div");
61 document
.body
.insertBefore(onscreenParent
, document
.body
.firstChild
);
62 // When we remove the meta tag from the document, we put it here.
63 var isOptimizedForMobile
= false;
65 function didPlaybackStart(video
)
67 // We say that the video started if it's not paused or if it played and
69 return !video
.paused
|| video
.ended
;
72 function becomeOptimizedForMobile(enable
)
74 // If we're in the right state already, then return;
75 if (enable
== isOptimizedForMobile
)
79 // We can't transition out of optimized for mobile.
80 console
.log("becomeOptimizedForMobile: test is broken -- cannot un-enable mobile");
83 // This only works once.
84 mobileMetaTag
= document
.createElement('meta');
85 mobileMetaTag
.name
= "viewport";
86 mobileMetaTag
.content
= "width=device-width";
87 document
.head
.appendChild(mobileMetaTag
);
88 isOptimizedForMobile
= true;
92 function addResultsRow(spec
)
94 // Add a row to the results table.
95 var row
= document
.getElementById("results").insertRow();
96 var td
= row
.insertCell();
98 // Add experiment number
99 row
.insertCell().innerText
= (""+spec
.experimentNumber
);
101 // Process experiment type specially.
102 var type
= spec
.experimentType
;
104 smallType
+= (type
.indexOf("-forvideo")>-1)?"v":"";
105 smallType
+= (type
.indexOf("-foraudio")>-1)?"a":"";
106 smallType
+= (type
.indexOf("-ifmuted")>-1)?"M":"";
107 smallType
+= (type
.indexOf("-playmuted")>-1)?"p":"";
108 smallType
+= (type
.indexOf("-ifmobile")>-1)?"m":"";
109 row
.insertCell().innerText
= smallType
;
111 // Add remaining fields.
112 var fields
= [ "elementType", "autoplayType", "mutedType", "mobileType",
115 row
.insertCell().innerText
= spec
[fields
[idx
]].substring(0,7);
118 function configureElementViaScript(element
, spec
)
120 if(spec
.autoplayType
== "play()")
124 function queueNextExperiment()
126 // Start the next config, but let the event queue drain.
127 setTimeout(runNextConfig
, 0);
130 function checkElementStatus(element
)
132 // Update the spec with the results.
133 var didStart
= didPlaybackStart(element
);
134 element
.spec
.played
= didStart
? "played" : "no";
135 element
.spec
.muted
= didStart
? (element
.muted
? "muted" : "unmuted") : "-";
137 addResultsRow(element
.spec
);
140 queueNextExperiment();
143 function runOneConfig(spec
)
145 internals
.settings
.setAutoplayExperimentMode(spec
.experimentType
);
147 // Create, configure, and attach a media element.
148 var element
= document
.createElement(spec
.elementType
);
149 element
.controls
= true;
151 onscreenParent
.appendChild(element
);
153 // Set any attributes before canPlayThrough.
154 if (spec
.mutedType
== "yes")
155 element
.muted
= true;
156 if (spec
.autoplayType
== "attr")
157 element
.autoplay
= true;
158 becomeOptimizedForMobile(spec
.mobileType
== "yes");
160 // Record the spec in the element, so that we can display the
164 // Wait for canplaythrough before continuing, so that the media
165 // might actually be playing.
166 element
.addEventListener("canplaythrough", function()
168 // Now that we can play, if we're supposed to play / mute via js do so.
169 configureElementViaScript(element
, spec
);
171 // Record the results.
172 checkElementStatus(element
);
175 // Set the source, which will eventually lead to canPlayThrough.
176 element
.src
= mediaFile
;
179 var experimentTypes
= [
182 "enabled-forvideo-ifmuted",
183 "enabled-forvideo-playmuted",
185 "enabled-forvideo-ifmobile",
187 var elementTypes
= ["video", "audio"];
188 var autoplayTypes
= ["none", "attr", "play()"];
189 var mutedTypes
= ["no", "yes"];
190 // mobileTypes must always start with no, since we cannot un-optimize the page.
191 var mobileTypes
= ["no", "yes"];
193 function runNextConfig()
195 // Convert configNumber into a spec, and run it.
196 var exp
= configNumber
;
198 // Convert this experiment number into settings.
200 spec
.elementType
= elementTypes
[exp
% elementTypes
.length
];
201 exp
= Math
.floor(exp
/ elementTypes
.length
);
202 spec
.experimentType
= experimentTypes
[exp
% experimentTypes
.length
];
203 exp
= Math
.floor(exp
/ experimentTypes
.length
);
204 spec
.autoplayType
= autoplayTypes
[exp
% autoplayTypes
.length
];
205 exp
= Math
.floor(exp
/ autoplayTypes
.length
);
206 spec
.mutedType
= mutedTypes
[exp
% mutedTypes
.length
];
207 exp
= Math
.floor(exp
/ mutedTypes
.length
);
208 // Mobile must always change last, so that all the "no" cases precede
209 // all the "yes" cases, since we can't switch the doc back to "not
210 // optimized for mobile".
211 spec
.mobileType
= mobileTypes
[exp
% mobileTypes
.length
];
212 exp
= Math
.floor(exp
/ mobileTypes
.length
);
213 spec
.experimentNumber
= configNumber
;
215 // Return null if configNumber was larger than the highest experiment.
221 // To keep the test fast, skip a few combinations.
223 if (spec
.experimentType
.indexOf("-ifmuted") == -1 && spec
.mutedType
!= "no")
226 // Only allow basic combinations for the mobile case. We just want to
227 // test video with autoplay, no mute options when testing -ifmobile.
228 // Similarly, if we're setting the page to be optimied for mobile, then
229 // require that we're one of those tests.
230 if ((spec
.mobileType
== "yes" || spec
.experimentType
.indexOf("-ifmobile")>0)
231 && (spec
.elementType
!= "video" || spec
.autoplayType
!= "attr"
232 || spec
.mutedType
!= "no"
233 || (spec
.experimentType
!= "enabled-forvideo"
234 && spec
.experimentType
!= "enabled-forvideo-ifmobile")))
238 queueNextExperiment();
243 window
.internals
.settings
.setMediaPlaybackRequiresUserGesture(true);