Skip to content

Commit

Permalink
Bug 1678357 - part3 : modify test. r=chunmin
Browse files Browse the repository at this point in the history
Because of part1, now we would activate controller in different momemt, so we have to modify our test to make them work or reduce the possible intermittent failure.

Depends on D98830

Differential Revision: https://phabricator.services.mozilla.com/D98831
  • Loading branch information
alastor0325 committed Dec 12, 2020
1 parent f981a2b commit 95cf843
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ add_task(async function triggerDefaultActionHandler() {
add_task(async function triggerNonDefaultHandlerWhenSetCustomizedHandler() {
info(`open page and start media`);
const tab = await createLoadedTabWrapper(PAGE_URL);
await startMedia(tab, { videoId });
await Promise.all([
new Promise(r => (tab.controller.onactivated = r)),
startMedia(tab, { videoId }),
]);

const kActions = ["play", "pause", "stop"];
for (const action of kActions) {
Expand Down Expand Up @@ -101,7 +104,10 @@ add_task(
await loadIframe(tab, frameId, url);

info(`start media from iframe would make it become active session`);
await startMedia(tab, { frameId });
await Promise.all([
new Promise(r => (tab.controller.onactivated = r)),
startMedia(tab, { frameId }),
]);

info(`press '${action}' should trigger iframe's action handler`);
await setActionHandler(tab, action, frameId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ function playMediaFrom(tab, frameId = undefined) {
}

function playAndWaitUntilMetadataChanged(tab, frameId = undefined) {
const metadataChanged = waitUntilDisplayedMetadataChanged();
const metadataChanged = frameId
? new Promise(r => (tab.controller.onmetadatachange = r))
: waitUntilDisplayedMetadataChanged();
return Promise.all([metadataChanged, playMediaFrom(tab, frameId)]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ add_task(async function testDeterminingMainController() {
await setMediaMetadataForTabs([tab0, tab1, tab2]);

info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(tab0);
await makeTabBecomeMainControllerAndWaitForMetadataChange(tab0);

info(`currrent metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);

info(`start media for tab1, main controller should become tab1`);
await makeTabBecomeMainController(tab1);
await makeTabBecomeMainControllerAndWaitForMetadataChange(tab1);

info(`currrent metadata should be equal to tab1's metadata`);
await isCurrentMetadataEqualTo(tab1.metadata);

info(`start media for tab2, main controller should become tab2`);
await makeTabBecomeMainController(tab2);
await makeTabBecomeMainControllerAndWaitForMetadataChange(tab2);

info(`currrent metadata should be equal to tab2's metadata`);
await isCurrentMetadataEqualTo(tab2.metadata);
Expand Down Expand Up @@ -114,7 +114,7 @@ add_task(async function testPIPControllerWontBeReplacedByNormalController() {
await setMediaMetadataForTabs([tab0, tab1]);

info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(tab0);
await makeTabBecomeMainControllerAndWaitForMetadataChange(tab0);

info(`currrent metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);
Expand Down Expand Up @@ -150,7 +150,7 @@ add_task(
await setMediaMetadataForTabs([tab0, tab1]);

info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(tab0);
await makeTabBecomeMainControllerAndWaitForMetadataChange(tab0);

info(`current metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);
Expand Down Expand Up @@ -185,7 +185,7 @@ add_task(async function testFullscreenAndPIPControllers() {
* Current controller list : [tab0 (fullscreen)]
*/
info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(tab0);
await makeTabBecomeMainControllerAndWaitForMetadataChange(tab0);

info(`currrent metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);
Expand Down Expand Up @@ -299,6 +299,13 @@ function makeTabBecomeMainController(tab) {
return Promise.all([playPromise, waitUntilMainMediaControllerChanged()]);
}

function makeTabBecomeMainControllerAndWaitForMetadataChange(tab) {
return Promise.all([
new Promise(r => (tab.controller.onmetadatachange = r)),
makeTabBecomeMainController(tab),
]);
}

function playMediaAndWaitUntilRegisteringController(tab, elementId) {
const playPromise = SpecialPowers.spawn(
tab.linkedBrowser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ add_task(async function setupTestingPref() {
add_task(async function testActiveSessionWhenClosingTab() {
info(`open tab1 and load media session test page`);
const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
await startMediaPlayback(tab1);
await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab1);

info(`pressing '${ACTION}' key`);
MediaControlService.generateMediaControlKey(ACTION);
Expand All @@ -34,7 +34,7 @@ add_task(async function testActiveSessionWhenClosingTab() {

info(`open tab2 and load media session test page`);
const tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
await startMediaPlayback(tab2);
await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab2);

info(`pressing '${ACTION}' key`);
MediaControlService.generateMediaControlKey(ACTION);
Expand Down Expand Up @@ -66,7 +66,7 @@ add_task(async function testActiveSessionWhenClosingTab() {
add_task(async function testActiveSessionWhenNavigatingTab() {
info(`open tab1 and load media session test page`);
const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
await startMediaPlayback(tab1);
await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab1);

info(`pressing '${ACTION}' key`);
MediaControlService.generateMediaControlKey(ACTION);
Expand All @@ -76,7 +76,7 @@ add_task(async function testActiveSessionWhenNavigatingTab() {

info(`open tab2 and load media session test page`);
const tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
await startMediaPlayback(tab2);
await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab2);

info(`pressing '${ACTION}' key`);
MediaControlService.generateMediaControlKey(ACTION);
Expand Down Expand Up @@ -109,7 +109,7 @@ add_task(async function testActiveSessionWhenNavigatingTab() {
add_task(async function testCreatingSessionWithoutPlayingMedia() {
info(`open tab1 and load media session test page`);
const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
await startMediaPlayback(tab1);
await startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab1);

info(`pressing '${ACTION}' key`);
MediaControlService.generateMediaControlKey(ACTION);
Expand Down Expand Up @@ -137,16 +137,17 @@ add_task(async function testCreatingSessionWithoutPlayingMedia() {
/**
* The following are helper functions
*/
async function startMediaPlayback(tab) {
const controllerChanged = waitUntilMainMediaControllerChanged();
await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
const video = content.document.getElementById("testVideo");
if (!video) {
ok(false, `can't get the media element!`);
}
video.play();
});
await controllerChanged;
async function startMediaPlaybackAndWaitMedisSessionBecomeActiveSession(tab) {
await Promise.all([
BrowserUtils.promiseObserved("active-media-session-changed"),
SpecialPowers.spawn(tab.linkedBrowser, [], () => {
const video = content.document.getElementById("testVideo");
if (!video) {
ok(false, `can't get the media element!`);
}
video.play();
}),
]);
}

async function checkIfActionReceived(tab, action) {
Expand Down

0 comments on commit 95cf843

Please sign in to comment.