author | Alastor Wu <alwu@mozilla.com> |
Tue, 09 Jan 2018 17:14:40 +0800 | |
changeset 719364 | 0de609b654daa2868547887b2974dfb7d6df2aa6 |
parent 717028 | 739484451a6399c7f156a0d960335606aa6c1221 |
child 719421 | c0019a13a06aeb65719855c162ef981326f65a77 |
push id | 95239 |
push user | bmo:alwu@mozilla.com |
push date | Fri, 12 Jan 2018 03:23:04 +0000 |
bugs | 1424113 |
milestone | 59.0a1 |
--- a/toolkit/content/tests/browser/browser.ini +++ b/toolkit/content/tests/browser/browser.ini @@ -14,35 +14,40 @@ support-files = file_multipleAudio.html file_multiplePlayingAudio.html file_nonAutoplayAudio.html file_plugIn.html file_redirect.html file_redirect_to.html file_silentAudioTrack.html file_webAudio.html - gizmo.mp4 head.js image.jpg image_page.html silentAudioTrack.webm doggy.png firebird.png [browser_audioCompeting.js] tags = audiochannel [browser_audioCompeting_onlyForActiveAgent.js] tags = audiochannel [browser_autoplay_policy_play_twice.js] +support-files = + gizmo.mp4 + file_video.html [browser_autoplay_policy_user_gestures.js] +support-files = + gizmo.mp4 + file_video.html [browser_autoscroll_disabled.js] [browser_block_autoplay_media.js] tags = audiochannel [browser_block_autoplay_media_pausedAfterPlay.js] - tags = audiochannel +tags = audiochannel [browser_block_autoplay_playAfterTabVisible.js] tags = audiochannel [browser_block_multipleMedia.js] tags = audiochannel [browser_block_notInTreeAudio.js] tags = audiochannel [browser_block_playMediaInMuteTab.js] tags = audiochannel
--- a/toolkit/content/tests/browser/browser_autoplay_policy_play_twice.js +++ b/toolkit/content/tests/browser/browser_autoplay_policy_play_twice.js @@ -1,44 +1,43 @@ -const FILE = "https://example.com/browser/toolkit/content/tests/browser/gizmo.mp4"; +const VIDEO_PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_video.html"; function setup_test_preference(enableUserGesture) { let state = enableUserGesture ? "enable" : "disable"; info(`- set pref : ${state} user gesture -`); return SpecialPowers.pushPrefEnv({"set": [ ["media.autoplay.enabled", false], ["media.autoplay.enabled.user-gestures-needed", enableUserGesture] ]}); } async function allow_play_for_played_video() { info("- open new tab -"); let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, "about:blank"); - info("- create video -"); - let document = tab.linkedBrowser.contentDocumentAsCPOW; - let video = document.createElement("video"); - video.src = FILE; - video.controls = true; - document.body.appendChild(video); + tab.linkedBrowser.loadURI(VIDEO_PAGE); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); info("- simulate user-click to start video -"); - let waitForPlayEvent = once(video, "play"); - await BrowserTestUtils.synthesizeMouseAtCenter(video, {button: 0}, + await BrowserTestUtils.synthesizeMouseAtCenter("#v", {button: 0}, tab.linkedBrowser); - info("- video starts playing -"); - await waitForPlayEvent; + + async function play_video_again() { + let video = content.document.getElementById("v"); + ok(!video.paused, "video is playing"); - info("- call video play() again -"); - try { - await video.play(); - ok(true, "success to resolve play promise"); - } catch (e) { - ok(false, "promise should not be rejected"); + info("- call video play() again -"); + try { + await video.play(); + ok(true, "success to resolve play promise"); + } catch (e) { + ok(false, "promise should not be rejected"); + } } + await ContentTask.spawn(tab.linkedBrowser, null, play_video_again); info("- remove tab -"); await BrowserTestUtils.removeTab(tab); } add_task(async function start_test() { await setup_test_preference(true); await allow_play_for_played_video();
--- a/toolkit/content/tests/browser/browser_autoplay_policy_user_gestures.js +++ b/toolkit/content/tests/browser/browser_autoplay_policy_user_gestures.js @@ -1,9 +1,9 @@ -const FILE = "https://example.com/browser/toolkit/content/tests/browser/gizmo.mp4"; +const VIDEO_PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_video.html"; var UserGestures = { MOUSE_CLICK: "mouse-click", MOUSE_MOVE: "mouse-move", KEYBOARD_PRESS: "keyboard-press" }; var UserGestureTests = [ @@ -15,83 +15,93 @@ var UserGestureTests = [ function setup_test_preference() { return SpecialPowers.pushPrefEnv({"set": [ ["media.autoplay.enabled", false], ["media.autoplay.enabled.user-gestures-needed", true] ]}); } function simulateUserGesture(gesture, targetBrowser) { - // eslint-disable-next-line mozilla/no-cpows-in-tests - let targetElement = targetBrowser.contentDocumentAsCPOW.documentElement; info(`- simulate ${gesture.type} event -`); switch (gesture.type) { case UserGestures.MOUSE_CLICK: - return BrowserTestUtils.synthesizeMouseAtCenter(targetElement, {button: 0}, + return BrowserTestUtils.synthesizeMouseAtCenter("body", {button: 0}, targetBrowser); case UserGestures.MOUSE_MOVE: - return BrowserTestUtils.synthesizeMouseAtCenter(targetElement, {type: "mousemove"}, + return BrowserTestUtils.synthesizeMouseAtCenter("body", {type: "mousemove"}, targetBrowser); case UserGestures.KEYBOARD_PRESS: return BrowserTestUtils.sendChar("a", targetBrowser); default: ok(false, "undefined user gesture"); return false; } } async function test_play_without_user_gesture() { info("- open new tab -"); let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, "about:blank"); - info("- create autoplay video -"); - // eslint-disable-next-line mozilla/no-cpows-in-tests - let document = tab.linkedBrowser.contentDocumentAsCPOW; - let video = document.createElement("video"); - video.src = FILE; - video.autoplay = true; - let canplayPromise = once(video, "canplaythrough"); - document.body.appendChild(video); + tab.linkedBrowser.loadURI(VIDEO_PAGE); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + async function check_autoplay_keyword() { + info("- create an new autoplay video -"); + let video = content.document.createElement("video"); + video.src = "gizmo.mp4"; + video.autoplay = true; + let canplayPromise = new Promise(function(resolve) { + video.addEventListener("canplaythrough", function() { + resolve(); + }, {once: true}); + }); + content.document.body.appendChild(video); - info("- can't autoplay without user activation -"); - await canplayPromise; - ok(video.paused, "video can't start without user input."); + info("- can't autoplay without user activation -"); + await canplayPromise; + ok(video.paused, "video can't start without user input."); + } + await ContentTask.spawn(tab.linkedBrowser, null, check_autoplay_keyword); - info("- call play() without user activation -"); - await video.play().catch(function() { + async function play_video() { + let video = content.document.getElementById("v"); + info("- call play() without user activation -"); + await video.play().catch(function() { ok(video.paused, "video can't start play without user input."); - }); + }); + } + await ContentTask.spawn(tab.linkedBrowser, null, play_video); info("- remove tab -"); await BrowserTestUtils.removeTab(tab); } async function test_play_with_user_gesture(gesture) { info("- open new tab -"); let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, "about:blank"); - info("- create autoplay video -"); - // eslint-disable-next-line mozilla/no-cpows-in-tests - let document = tab.linkedBrowser.contentDocumentAsCPOW; - let video = document.createElement("video"); - video.src = FILE; - document.body.appendChild(video); + tab.linkedBrowser.loadURI(VIDEO_PAGE); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); info("- simulate user gesture -"); await simulateUserGesture(gesture, tab.linkedBrowser); info("- call play() -"); - try { - await video.play(); - ok(gesture.isActivationGesture, "user gesture can activate the page"); - ok(!video.paused, "video starts playing."); - } catch (e) { - ok(!gesture.isActivationGesture, "user gesture can not activate the page"); - ok(video.paused, "video can not start playing."); + async function play_video(gesture) { + let video = content.document.getElementById("v"); + try { + await video.play(); + ok(gesture.isActivationGesture, "user gesture can activate the page"); + ok(!video.paused, "video starts playing."); + } catch (e) { + ok(!gesture.isActivationGesture, "user gesture can not activate the page"); + ok(video.paused, "video can not start playing."); + } } + await ContentTask.spawn(tab.linkedBrowser, gesture, play_video); info("- remove tab -"); await BrowserTestUtils.removeTab(tab); } add_task(async function start_test() { info("- setup test preference -"); await setup_test_preference();