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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const PRIORITY_SET_TOPIC =
"process-priority-manager:TEST-ONLY:process-priority-set";
// Copied from Hal.cpp
const PROCESS_PRIORITY_FOREGROUND = "FOREGROUND";
const PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE = "BACKGROUND_PERCEIVABLE";
const PROCESS_PRIORITY_BACKGROUND = "BACKGROUND";
// This is how many milliseconds we'll wait for a process priority
// change before we assume that it's just not happening.
const WAIT_FOR_CHANGE_TIME_MS = 2000;
/**
* This class is responsible for watching process priority changes, and
* mapping them to tabs in a single window.
*/
class TabPriorityWatcher {
/**
* Constructing a TabPriorityWatcher should happen before any tests
* start when there's only a single tab in the window.
*
* Callers must call `destroy()` on any instance that is constructed
* when the test is completed.
*
* @param tabbrowser (<tabbrowser>)
* The tabbrowser (gBrowser) for the window to be tested.
*/
constructor(tabbrowser) {
this.tabbrowser = tabbrowser;
Assert.equal(
tabbrowser.tabs.length,
1,
"TabPriorityWatcher must be constructed in a window " +
"with a single tab to start."
);
this.priorityMap = new WeakMap();
this.priorityMap.set(
this.tabbrowser.selectedBrowser,
PROCESS_PRIORITY_FOREGROUND
);
this.noChangeBrowsers = new WeakMap();
Services.obs.addObserver(this, PRIORITY_SET_TOPIC);
}
/**
* Cleans up lingering references for an instance of
* TabPriorityWatcher to avoid leaks. This should be called when
* finishing the test.
*/
destroy() {
Services.obs.removeObserver(this, PRIORITY_SET_TOPIC);
this.window = null;
}
/**
* Returns a Promise that resolves when a particular <browser>
* has its content process reach a particular priority. Will
* eventually time out if that priority is never reached.
*
* @param browser (<browser>)
* The <browser> that we expect to change priority.
* @param expectedPriority (String)
* One of the PROCESS_PRIORITY_ constants defined at the
* top of this file.
* @return Promise
* @resolves undefined
* Once the browser reaches the expected priority.
*/
async waitForPriorityChange(browser, expectedPriority) {
return TestUtils.waitForCondition(() => {
let currentPriority = this.priorityMap.get(browser);
if (currentPriority == expectedPriority) {
Assert.ok(
true,
`Browser at ${browser.currentURI.spec} reached expected ` +
`priority: ${currentPriority}`
);
return true;
}
return false;
}, `Waiting for browser at ${browser.currentURI.spec} to reach priority ` + expectedPriority);
}
/**
* Returns a Promise that resolves after a duration of
* WAIT_FOR_CHANGE_TIME_MS. During that time, if the passed browser
* changes priority, a test failure will be registered.
*
* @param browser (<browser>)
* The <browser> that we expect to change priority.
* @return Promise
* @resolves undefined
* Once the WAIT_FOR_CHANGE_TIME_MS duration has passed.
*/
async ensureNoPriorityChange(browser) {
this.noChangeBrowsers.set(browser, null);
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
await new Promise(resolve => setTimeout(resolve, WAIT_FOR_CHANGE_TIME_MS));
let priority = this.noChangeBrowsers.get(browser);
Assert.equal(
priority,
null,
`Should have seen no process priority change for a browser at ${browser.currentURI.spec}`
);
this.noChangeBrowsers.delete(browser);
}
/**
* Makes sure that a particular foreground browser has been
* registered in the priority map. This is needed because browsers are
* only registered when their priorities change - and if a browser's
* priority never changes during a test, then they wouldn't be registered.
*
* The passed browser must be a foreground browser, since it's assumed that
* the associated content process is running with foreground priority.
*
* @param browser (browser)
* A _foreground_ browser.
*/
ensureForegroundRegistered(browser) {
if (!this.priorityMap.has(browser)) {
this.priorityMap.set(browser, PROCESS_PRIORITY_FOREGROUND);
}
}
/**
* Synchronously returns the priority of a particular browser's
* content process.
*
* @param browser (browser)
* The browser to get the content process priority for.
* @return String
* The priority that the browser's content process is at.
*/
currentPriority(browser) {
return this.priorityMap.get(browser);
}
/**
* A utility function that takes a string passed via the
* PRIORITY_SET_TOPIC observer notification and extracts the
* childID and priority string.
*
* @param ppmDataString (String)
* The string data passed through the PRIORITY_SET_TOPIC observer
* notification.
* @return Object
* An object with the following properties:
*
* childID (Number)
* The ID of the content process that changed priority.
*
* priority (String)
* The priority that the content process was set to.
*/
parsePPMData(ppmDataString) {
let [childIDStr, priority] = ppmDataString.split(":");
return {
childID: parseInt(childIDStr, 10),
priority,
};
}
/** nsIObserver **/
observe(subject, topic, data) {
if (topic != PRIORITY_SET_TOPIC) {
Assert.ok(false, "TabPriorityWatcher is observing the wrong topic");
return;
}
let { childID, priority } = this.parsePPMData(data);
for (let browser of this.tabbrowser.browsers) {
if (browser.frameLoader.childID == childID) {
info(
`Browser at: ${browser.currentURI.spec} transitioning to ${priority}`
);
if (this.noChangeBrowsers.has(browser)) {
this.noChangeBrowsers.set(browser, priority);
}
this.priorityMap.set(browser, priority);
}
}
}
}
let gTabPriorityWatcher;
add_task(async function setup() {
// We need to turn on testMode for the process priority manager in
// order to receive the observer notifications that this test relies on.
await SpecialPowers.pushPrefEnv({
set: [
["dom.ipc.processPriorityManager.testMode", true],
["dom.ipc.processPriorityManager.enabled", true],
],
});
gTabPriorityWatcher = new TabPriorityWatcher(gBrowser);
});
registerCleanupFunction(() => {
gTabPriorityWatcher.destroy();
gTabPriorityWatcher = null;
});
/**
* Utility function that switches the current tabbrowser from one
* tab to another, and ensures that the tab that goes into the background
* has (or reaches) a particular content process priority.
*
* It is expected that the fromTab and toTab belong to two separate content
* processes.
*
* @param Object
* An object with the following properties:
*
* fromTab (<tab>)
* The tab that will be switched from to the toTab. The fromTab
* is the one that will be going into the background.
*
* toTab (<tab>)
* The tab that will be switched to from the fromTab. The toTab
* is presumed to start in the background, and will enter the
* foreground.
*
* fromTabExpectedPriority (String)
* The priority that the content process for the fromTab is
* expected to be (or reach) after the tab goes into the background.
* This should be one of the PROCESS_PRIORITY_ strings defined at the
* top of the file.
*
* @return Promise
* @resolves undefined
* Once the tab switch is complete, and the two content processes for the
* tabs have reached the expected priority levels.
*/
async function assertPriorityChangeOnBackground({
fromTab,
toTab,
fromTabExpectedPriority,
}) {
let fromBrowser = fromTab.linkedBrowser;
let toBrowser = toTab.linkedBrowser;
// If the tabs aren't running in separate processes, none of the
// rest of this is going to work.
Assert.notEqual(
toBrowser.frameLoader.remoteTab.osPid,
fromBrowser.frameLoader.remoteTab.osPid,
"Tabs should be running in separate processes."
);
gTabPriorityWatcher.ensureForegroundRegistered(fromBrowser);
let fromPromise;
if (
gTabPriorityWatcher.currentPriority(fromBrowser) == fromTabExpectedPriority
) {
fromPromise = gTabPriorityWatcher.ensureNoPriorityChange(fromBrowser);
} else {
fromPromise = gTabPriorityWatcher.waitForPriorityChange(
fromBrowser,
fromTabExpectedPriority
);
}
let toPromise;
if (
gTabPriorityWatcher.currentPriority(toBrowser) ==
PROCESS_PRIORITY_FOREGROUND
) {
toPromise = gTabPriorityWatcher.ensureNoPriorityChange(toBrowser);
} else {
toPromise = gTabPriorityWatcher.waitForPriorityChange(
toBrowser,
PROCESS_PRIORITY_FOREGROUND
);
}
await BrowserTestUtils.switchTab(gBrowser, toTab);
await Promise.all([fromPromise, toPromise]);
}
/**
* Test that if a normal tab goes into the background,
* it has its process priority lowered to
* PROCESS_PRIORITY_BACKGROUND.
*/
add_task(async function test_normal_background_tab() {
let originalTab = gBrowser.selectedTab;
await BrowserTestUtils.withNewTab("http://example.com", async browser => {
let tab = gBrowser.getTabForBrowser(browser);
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
});
});
/**
* Test that if a tab with video goes into the background,
* it has its process priority lowered to
* PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE if it has no audio,
* and that it has its priority remain at
* PROCESS_PRIORITY_FOREGROUND if it does have audio.
*/
add_task(async function test_video_background_tab() {
let originalTab = gBrowser.selectedTab;
await BrowserTestUtils.withNewTab("http://example.com", async browser => {
// Let's load up a video in the tab, but mute it, so that this tab should
// reach PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE.
await SpecialPowers.spawn(browser, [], async () => {
let video = content.document.createElement("video");
video.src = "http://mochi.test:8888/browser/dom/ipc/tests/short.mp4";
video.muted = true;
content.document.body.appendChild(video);
// We'll loop the video to avoid it ending before the test is done.
video.loop = true;
await video.play();
});
let tab = gBrowser.getTabForBrowser(browser);
// The tab with the muted video should reach
// PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE,
});
// Now switch back. The initial blank tab should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
// Let's unmute the video now.
await SpecialPowers.spawn(browser, [], async () => {
let video = content.document.querySelector("video");
video.muted = false;
});
// The tab with the unmuted video should stay at
// PROCESS_PRIORITY_FOREGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_FOREGROUND,
});
// Now switch back. The initial blank tab should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
});
});
/**
* Test that if a tab with a playing <audio> element goes into
* the background, the process priority does not change, unless
* that audio is muted (in which case, it reaches
* PROCESS_PRIORITY_BACKGROUND).
*/
add_task(async function test_audio_background_tab() {
let originalTab = gBrowser.selectedTab;
await BrowserTestUtils.withNewTab("http://example.com", async browser => {
// Let's load up some audio in the tab, but mute it, so that this tab should
// reach PROCESS_PRIORITY_BACKGROUND.
await SpecialPowers.spawn(browser, [], async () => {
let audio = content.document.createElement("audio");
audio.src = "http://mochi.test:8888/browser/dom/ipc/tests/owl.mp3";
audio.muted = true;
content.document.body.appendChild(audio);
// We'll loop the audio to avoid it ending before the test is done.
audio.loop = true;
await audio.play();
});
let tab = gBrowser.getTabForBrowser(browser);
// The tab with the muted audio should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
// Now switch back. The initial blank tab should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
// Now unmute the audio. Unfortuntely, there's a bit of a race here,
// since the wakelock on the audio element is released and then
// re-acquired if the audio reaches its end and loops around. This
// will cause an unexpected priority change on the content process.
//
// To avoid this race, we'll seek the audio back to the beginning,
// and lower its playback rate to the minimum to increase the
// likelihood that the check completes before the audio loops around.
await SpecialPowers.spawn(browser, [], async () => {
let audio = content.document.querySelector("audio");
let seeked = ContentTaskUtils.waitForEvent(audio, "seeked");
audio.muted = false;
// 0.25 is the minimum playback rate that still keeps the audio audible.
audio.playbackRate = 0.25;
audio.currentTime = 0;
await seeked;
});
// The tab with the unmuted audio should stay at
// PROCESS_PRIORITY_FOREGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_FOREGROUND,
});
// Now switch back. The initial blank tab should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
});
});
/**
* Test that if a tab with a WebAudio playing goes into the background,
* the process priority does not change, unless that WebAudio context is
* suspended.
*/
add_task(async function test_web_audio_background_tab() {
let originalTab = gBrowser.selectedTab;
await BrowserTestUtils.withNewTab("http://example.com", async browser => {
// Let's synthesize a basic square wave as WebAudio.
await SpecialPowers.spawn(browser, [], async () => {
let audioCtx = new content.AudioContext();
let oscillator = audioCtx.createOscillator();
oscillator.type = "square";
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime);
oscillator.connect(audioCtx.destination);
oscillator.start();
while (audioCtx.state != "running") {
info(`wait until AudioContext starts running`);
await new Promise(r => (audioCtx.onstatechange = r));
}
// we'll stash the AudioContext away so that it's easier to access
// in the next SpecialPowers.spawn.
content.audioCtx = audioCtx;
});
let tab = gBrowser.getTabForBrowser(browser);
// The tab with the WebAudio should stay at
// PROCESS_PRIORITY_FOREGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_FOREGROUND,
});
// Now switch back. The initial blank tab should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
// Now suspend the WebAudio. This will cause it to stop
// playing.
await SpecialPowers.spawn(browser, [], async () => {
content.audioCtx.suspend();
});
// The tab with the suspended WebAudio should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: tab,
toTab: originalTab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
// Now switch back. The initial blank tab should reach
// PROCESS_PRIORITY_BACKGROUND when backgrounded.
await assertPriorityChangeOnBackground({
fromTab: originalTab,
toTab: tab,
fromTabExpectedPriority: PROCESS_PRIORITY_BACKGROUND,
});
});
});
|