-
Notifications
You must be signed in to change notification settings - Fork 54
/
browser_media_control_main_controller.js
223 lines (189 loc) · 7.46 KB
/
browser_media_control_main_controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* eslint-disable no-undef */
"use strict";
// Import this in order to use `triggerPictureInPicture()`.
/* import-globals-from ../../../../toolkit/components/pictureinpicture/tests/head.js */
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/toolkit/components/pictureinpicture/tests/head.js",
this
);
const PAGE_NON_AUTOPLAY =
"https://example.com/browser/dom/media/mediacontrol/tests/file_non_autoplay.html";
const testVideoId = "video";
add_task(async function setupTestingPref() {
await SpecialPowers.pushPrefEnv({
set: [
["media.mediacontrol.testingevents.enabled", true],
["dom.media.mediasession.enabled", true],
],
});
});
/**
* This test is used to check in different situaition if we can determine the
* main controller correctly that is the controller which can receive media
* control keys and show its metadata on the virtual control interface.
*
* We will assign different metadata for each tab and know which tab is the main
* controller by checking main controller's metadata.
*
* We will always choose the last tab which plays media as the main controller,
* and maintain a list by the order of playing media. If the top element in the
* list has been removed, then we will use the last element in the list as the
* main controller.
*
* Eg. tab1 plays first, then tab2 plays, then tab3 plays, the list would be
* like [tab1, tab2, tab3] and the main controller would be tab3. If tab3 has
* been closed, then the list would become [tab1, tab2] and the tab2 would be
* the main controller.
*/
add_task(async function testDeterminingMainController() {
info(`open three different tabs`);
const tab0 = await createTabAndLoad(PAGE_NON_AUTOPLAY);
const tab1 = await createTabAndLoad(PAGE_NON_AUTOPLAY);
const tab2 = await createTabAndLoad(PAGE_NON_AUTOPLAY);
/**
* part1 : [] -> [tab0] -> [tab0, tab1] -> [tab0, tab1, tab2]
*/
info(`# [] -> [tab0] -> [tab0, tab1] -> [tab0, tab1, tab2] #`);
info(`set different metadata for each tab`);
await setMediaMetadataForTabs([tab0, tab1, tab2]);
info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(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);
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);
info(`currrent metadata should be equal to tab2's metadata`);
await isCurrentMetadataEqualTo(tab2.metadata);
/**
* part2 : [tab0, tab1, tab2] -> [tab0, tab2, tab1] -> [tab2, tab1, tab0]
*/
info(`# [tab0, tab1, tab2] -> [tab0, tab2, tab1] -> [tab2, tab1, tab0] #`);
info(`start media for tab1, main controller should become tab1`);
await makeTabBecomeMainController(tab1);
info(`currrent metadata should be equal to tab1's metadata`);
await isCurrentMetadataEqualTo(tab1.metadata);
info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(tab0);
info(`currrent metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);
/**
* part3 : [tab2, tab1, tab0] -> [tab2, tab1] -> [tab2] -> []
*/
info(`# [tab2, tab1, tab0] -> [tab2, tab1] -> [tab2] -> [] #`);
info(`remove tab0 and wait until main controller changes`);
await Promise.all([
waitUntilMainMediaControllerChanged(),
BrowserTestUtils.removeTab(tab0),
]);
info(`currrent metadata should be equal to tab1's metadata`);
await isCurrentMetadataEqualTo(tab1.metadata);
info(`remove tab1 and wait until main controller changes`);
await Promise.all([
waitUntilMainMediaControllerChanged(),
BrowserTestUtils.removeTab(tab1),
]);
info(`currrent metadata should be equal to tab2's metadata`);
await isCurrentMetadataEqualTo(tab2.metadata);
info(`remove tab2 and wait until main controller changes`);
await Promise.all([
waitUntilMainMediaControllerChanged(),
BrowserTestUtils.removeTab(tab2),
]);
isCurrentMetadataEmpty();
});
add_task(async function testPIPControllerIsAlwaysMainController() {
info(`open two different tabs`);
const tab0 = await createTabAndLoad(PAGE_NON_AUTOPLAY);
const tab1 = await createTabAndLoad(PAGE_NON_AUTOPLAY);
info(`set different metadata for each tab`);
await setMediaMetadataForTabs([tab0, tab1]);
info(`start media for tab0, main controller should become tab0`);
await makeTabBecomeMainController(tab0);
info(`currrent metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);
info(`trigger Picture-in-Picture mode for tab0`);
await triggerPictureInPicture(tab0.linkedBrowser, testVideoId);
info(`start media for tab1, main controller should still be tab0`);
await playMediaAndWaitUntilRegisteringController(tab1, testVideoId);
info(`currrent metadata should be equal to tab0's metadata`);
await isCurrentMetadataEqualTo(tab0.metadata);
info(`remove tab0 and wait until main controller changes`);
await Promise.all([
waitUntilMainMediaControllerChanged(),
BrowserTestUtils.removeTab(tab0),
]);
info(`currrent metadata should be equal to tab1's metadata`);
await isCurrentMetadataEqualTo(tab1.metadata);
info(`remove tab1 and wait until main controller changes`);
await Promise.all([
waitUntilMainMediaControllerChanged(),
BrowserTestUtils.removeTab(tab1),
]);
isCurrentMetadataEmpty();
});
/**
* The following are helper functions
*/
async function setMediaMetadataForTabs(tabs) {
for (let idx = 0; idx < tabs.length; idx++) {
const tabName = "tab" + idx;
info(`create metadata for ${tabName}`);
tabs[idx].metadata = {
title: tabName,
artist: tabName,
album: tabName,
artwork: [{ src: tabName, sizes: "128x128", type: "image/jpeg" }],
};
const spawn = SpecialPowers.spawn(
tabs[idx].linkedBrowser,
[tabs[idx].metadata],
data => {
content.navigator.mediaSession.metadata = new content.MediaMetadata(
data
);
}
);
await Promise.all([spawn, waitUntilControllerMetadataChanged()]);
}
}
function makeTabBecomeMainController(tab) {
const playPromise = SpecialPowers.spawn(
tab.linkedBrowser,
[testVideoId],
async Id => {
const video = content.document.getElementById(Id);
if (!video) {
ok(false, `can't get the media element!`);
}
// If media has been started, we would stop media first and then start it
// again, which would make controller's playback state change to `playing`
// again and result in updating new main controller.
if (!video.paused) {
video.pause();
info(`wait until media stops`);
await new Promise(r => (video.onpause = r));
}
info(`start media`);
return video.play();
}
);
return Promise.all([playPromise, waitUntilMainMediaControllerChanged()]);
}
function playMediaAndWaitUntilRegisteringController(tab, elementId) {
const playPromise = SpecialPowers.spawn(
tab.linkedBrowser,
[elementId],
Id => {
const video = content.document.getElementById(Id);
if (!video) {
ok(false, `can't get the media element!`);
}
return video.play();
}
);
return Promise.all([waitUntilMediaControllerAmountChanged(), playPromise]);
}