| <!DOCTYPE html> |
| <title>Test autoplay muted behaviour in various system conditions.</title> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="media-file.js"></script> |
| <body></body> |
| <script> |
| var tests = [ |
| { |
| name: 'regular conditions', |
| conditions: { |
| dataSaver: false, |
| cellular: false, |
| lowEndDevice: false, |
| }, |
| expectations: { |
| autoplay: true |
| } |
| }, |
| { |
| name: 'cellular connections', |
| conditions: { |
| dataSaver: false, |
| cellular: true, |
| lowEndDevice: false, |
| }, |
| expectations: { |
| autoplay: true |
| } |
| }, |
| { |
| name: 'cellular connections with Data Saver', |
| conditions: { |
| dataSaver: true, |
| cellular: true, |
| lowEndDevice: false, |
| }, |
| expectations: { |
| autoplay: false |
| } |
| }, |
| { |
| name: 'Data Saver', |
| conditions: { |
| dataSaver: true, |
| cellular: false, |
| lowEndDevice: false, |
| }, |
| expectations: { |
| autoplay: false |
| } |
| }, |
| { |
| name: 'low end device', |
| conditions: { |
| dataSaver: false, |
| cellular: false, |
| lowEndDevice: true, |
| }, |
| expectations: { |
| autoplay: false |
| } |
| }, |
| ]; |
| |
| var asyncTests = tests.map(test => async_test('Testing autoplay behaviour for ' + test.name)); |
| |
| var currentTest = 0; |
| function runNextTest() { |
| asyncTests[currentTest].done(); |
| |
| ++currentTest; |
| if (currentTest == tests.length) |
| return; |
| |
| asyncTests[currentTest].step_func(runTest(asyncTests[currentTest], tests[currentTest])); |
| } |
| |
| function runTest(t, test) { |
| // Sanity checks for 'conditions'. |
| assert_true('dataSaver' in test.conditions); |
| assert_true('cellular' in test.conditions); |
| assert_true('lowEndDevice' in test.conditions); |
| |
| internals.setSaveDataEnabled(test.conditions.dataSaver); |
| if (test.conditions.cellular) |
| internals.setNetworkConnectionInfoOverride(true, 'cellular3g', 2.0); |
| else |
| internals.setNetworkConnectionInfoOverride(true, 'wifi', 2.0); |
| internals.settings.setForcePreloadNoneForMediaElements(test.conditions.lowEndDevice); |
| |
| // Generic required settings. |
| internals.settings.setAutoplayPolicy('user-gesture-required'); |
| internals.runtimeFlags.autoplayMutedVideosEnabled = true; |
| |
| t.add_cleanup(() => { |
| internals.setSaveDataEnabled(false); |
| internals.clearNetworkConnectionInfoOverride(); |
| internals.settings.setForcePreloadNoneForMediaElements(false); |
| internals.settings.setAutoplayPolicy('no-user-gesture-required'); |
| internals.runtimeFlags.autoplayMutedVideosEnabled = false; |
| }); |
| |
| var count = 0; |
| [ 'attribute', 'method' ].forEach(type => { |
| var media = document.createElement('video'); |
| document.body.appendChild(media); |
| |
| var expectedEvents = []; |
| if (test.expectations.autoplay) |
| expectedEvents = [ 'play', 'playing' ]; |
| else |
| expectedEvents = [ 'suspend' ]; |
| |
| var eventWatcher = new EventWatcher(t, media, expectedEvents); |
| eventWatcher.wait_for(expectedEvents).then(_ => { |
| if (test.expectations.autoplay) { |
| assert_equals(media.readyState, media.HAVE_ENOUGH_DATA); |
| assert_false(media.paused); |
| } else { |
| assert_true(media.paused); |
| } |
| |
| document.body.removeChild(media); |
| ++count; |
| if (count == 2) |
| runNextTest(); |
| }); |
| |
| media.muted = true; |
| media.src = findMediaFile('video', 'content/test'); |
| |
| if (type == 'attribute') |
| media.autoplay = true; |
| else if (type == 'method') |
| media.play().catch(e => {}); |
| }); |
| } |
| |
| asyncTests[currentTest].step_func(runTest(asyncTests[currentTest], tests[currentTest])); |
| |
| </script> |