summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/browser_ProcessPriorityManager.js
blob: f32306e17443e2d880f013bdc484b051ae47438f (plain)
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/* 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;

// A convenience function for getting the child ID from a browsing context.
function browsingContextChildID(bc) {
  return bc.currentWindowGlobal?.domProcess.childID;
}

/**
 * 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 maps from childIDs to process priorities.
    this.priorityMap = new Map();

    // The keys in this map are childIDs we're not expecting to change.
    // Each value is either null (if no change has been seen) or the
    // priority that the process changed to.
    this.noChangeChildIDs = new Map();

    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 returns a Promise that resolves when the process with
   * the given childID reaches the given priority.
   * This will eventually time out if that priority is never reached.
   *
   * @param childID
   *   The childID of the process to wait on.
   * @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(childID, expectedPriority) {
    await TestUtils.waitForCondition(() => {
      let currentPriority = this.priorityMap.get(childID);
      if (currentPriority == expectedPriority) {
        Assert.ok(
          true,
          `Process with child ID ${childID} reached expected ` +
            `priority: ${currentPriority}`
        );
        return true;
      }
      return false;
    }, `Waiting for process with child ID ${childID} to reach priority ${expectedPriority}`);
  }

  /**
   * Returns a Promise that resolves after a duration of
   * WAIT_FOR_CHANGE_TIME_MS. During that time, if the process
   * with the passed in child ID changes priority, a test
   * failure will be registered.
   *
   * @param childID
   *   The childID of the process that we expect to not change priority.
   * @return Promise
   * @resolves undefined
   *   Once the WAIT_FOR_CHANGE_TIME_MS duration has passed.
   */
  async ensureNoPriorityChange(childID) {
    this.noChangeChildIDs.set(childID, null);
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(resolve => setTimeout(resolve, WAIT_FOR_CHANGE_TIME_MS));
    let priority = this.noChangeChildIDs.get(childID);
    Assert.equal(
      priority,
      null,
      `Should have seen no process priority change for child ID ${childID}`
    );
    this.noChangeChildIDs.delete(childID);
  }

  /**
   * This returns a Promise that resolves when all of the processes
   * of the browsing contexts in the browsing context tree
   * of a particular <browser> have reached a particular priority.
   * This will eventually time out if that priority is never reached.
   *
   * @param browser (<browser>)
   *   The <browser> to get the BC tree from.
   * @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 waitForBrowserTreePriority(browser, expectedPriority) {
    let childIDs = new Set(
      browser.browsingContext
        .getAllBrowsingContextsInSubtree()
        .map(browsingContextChildID)
    );
    let promises = [];
    for (let childID of childIDs) {
      let currentPriority = this.priorityMap.get(childID);

      promises.push(
        currentPriority == expectedPriority
          ? this.ensureNoPriorityChange(childID)
          : this.waitForPriorityChange(childID, expectedPriority)
      );
    }

    await Promise.all(promises);
  }

  /**
   * Synchronously returns the priority of a particular child ID.
   *
   * @param childID
   *   The childID to get the content process priority for.
   * @return String
   *   The priority of the child ID's process.
   */
  currentPriority(childID) {
    return this.priorityMap.get(childID);
  }

  /**
   * 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);
    if (this.noChangeChildIDs.has(childID)) {
      this.noChangeChildIDs.set(childID, priority);
    }
    this.priorityMap.set(childID, priority);
  }
}

let gTabPriorityWatcher;

add_setup(async function () {
  // 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."
  );

  let fromPromise = gTabPriorityWatcher.waitForBrowserTreePriority(
    fromBrowser,
    fromTabExpectedPriority
  );
  let toPromise = gTabPriorityWatcher.waitForBrowserTreePriority(
    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.
 * Additionally, test priorityHint flag sets the process priority
 * appropriately to PROCESS_PRIORITY_BACKGROUND and PROCESS_PRIORITY_FOREGROUND.
 */
add_task(async function test_normal_background_tab() {
  let originalTab = gBrowser.selectedTab;

  await BrowserTestUtils.withNewTab(
    "https://example.com/browser/dom/ipc/tests/file_cross_frame.html",
    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,
      });

      let origtabID = browsingContextChildID(
        originalTab.linkedBrowser.browsingContext
      );

      Assert.equal(
        originalTab.linkedBrowser.frameLoader.remoteTab.priorityHint,
        false,
        "PriorityHint of the original tab should be false by default"
      );

      // Changing renderLayers doesn't change priority of the background tab.
      originalTab.linkedBrowser.preserveLayers(true);
      originalTab.linkedBrowser.renderLayers = true;
      await new Promise(resolve =>
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        setTimeout(resolve, WAIT_FOR_CHANGE_TIME_MS)
      );
      Assert.equal(
        gTabPriorityWatcher.currentPriority(origtabID),
        PROCESS_PRIORITY_BACKGROUND,
        "Tab didn't get prioritized only due to renderLayers"
      );

      // Test when priorityHint is true, the original tab priority
      // becomes PROCESS_PRIORITY_FOREGROUND.
      originalTab.linkedBrowser.frameLoader.remoteTab.priorityHint = true;
      Assert.equal(
        gTabPriorityWatcher.currentPriority(origtabID),
        PROCESS_PRIORITY_FOREGROUND,
        "Setting priorityHint to true should set the original tab to foreground priority"
      );

      // Test when priorityHint is false, the original tab priority
      // becomes PROCESS_PRIORITY_BACKGROUND.
      originalTab.linkedBrowser.frameLoader.remoteTab.priorityHint = false;
      await new Promise(resolve =>
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        setTimeout(resolve, WAIT_FOR_CHANGE_TIME_MS)
      );
      Assert.equal(
        gTabPriorityWatcher.currentPriority(origtabID),
        PROCESS_PRIORITY_BACKGROUND,
        "Setting priorityHint to false should set the original tab to background priority"
      );

      let tabID = browsingContextChildID(tab.linkedBrowser.browsingContext);

      // Test when priorityHint is true, the process priority of the
      // active tab remains PROCESS_PRIORITY_FOREGROUND.
      tab.linkedBrowser.frameLoader.remoteTab.priorityHint = true;
      Assert.equal(
        gTabPriorityWatcher.currentPriority(tabID),
        PROCESS_PRIORITY_FOREGROUND,
        "Setting priorityHint to true should maintain the new tab priority as foreground"
      );

      // Test when priorityHint is false, the process priority of the
      // active tab remains PROCESS_PRIORITY_FOREGROUND.
      tab.linkedBrowser.frameLoader.remoteTab.priorityHint = false;
      Assert.equal(
        gTabPriorityWatcher.currentPriority(tabID),
        PROCESS_PRIORITY_FOREGROUND,
        "Setting priorityHint to false should maintain the new tab priority as foreground"
      );

      originalTab.linkedBrowser.preserveLayers(false);
      originalTab.linkedBrowser.renderLayers = false;
    }
  );
});

// Load a simple page on the given host into a new tab.
async function loadKeepAliveTab(host) {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    host + "/browser/dom/ipc/tests/file_dummy.html"
  );
  let childID = browsingContextChildID(
    gBrowser.selectedBrowser.browsingContext
  );

  Assert.equal(
    gTabPriorityWatcher.currentPriority(childID),
    PROCESS_PRIORITY_FOREGROUND,
    "Loading a new tab should make it prioritized"
  );

  if (SpecialPowers.useRemoteSubframes) {
    // There must be only one process with a remote type for the tab we loaded
    // to ensure that when we load a new page into the iframe with that host
    // that it will end up in the same process as the initial tab.
    let remoteType = gBrowser.selectedBrowser.remoteType;
    await TestUtils.waitForCondition(() => {
      return (
        ChromeUtils.getAllDOMProcesses().filter(
          process => process.remoteType == remoteType
        ).length == 1
      );
    }, `Waiting for there to be only one process with remote type ${remoteType}`);
  }

  return { tab, childID };
}

/**
 * If an iframe in a foreground tab is navigated to a new page for
 * a different site, then the process of the new iframe page should
 * have priority PROCESS_PRIORITY_FOREGROUND. Additionally, if Fission
 * is enabled, then the old iframe page's process's priority should be
 * lowered to PROCESS_PRIORITY_BACKGROUND.
 */
add_task(async function test_iframe_navigate() {
  // This test (eventually) loads a page from the host topHost that has an
  // iframe from iframe1Host. It then navigates the iframe to iframe2Host.
  let topHost = "https://example.com";
  let iframe1Host = "https://example.org";
  let iframe2Host = "https://example.net";

  // Before we load the final test page into a tab, we need to load pages
  // from both iframe hosts into tabs. This is needed so that we are testing
  // the "load a new page" part of prioritization and not the "initial
  // process load" part. Additionally, it ensures that the process for the
  // initial iframe page doesn't shut down once we navigate away from it,
  // which will also affect its prioritization.
  let { tab: iframe1Tab, childID: iframe1TabChildID } = await loadKeepAliveTab(
    iframe1Host
  );
  let { tab: iframe2Tab, childID: iframe2TabChildID } = await loadKeepAliveTab(
    iframe2Host
  );

  await BrowserTestUtils.withNewTab(
    topHost + "/browser/dom/ipc/tests/file_cross_frame.html",
    async browser => {
      Assert.equal(
        gTabPriorityWatcher.currentPriority(iframe2TabChildID),
        PROCESS_PRIORITY_BACKGROUND,
        "Switching to another new tab should deprioritize the old one"
      );

      let topChildID = browsingContextChildID(browser.browsingContext);
      let iframe = browser.browsingContext.children[0];
      let iframe1ChildID = browsingContextChildID(iframe);

      Assert.equal(
        gTabPriorityWatcher.currentPriority(topChildID),
        PROCESS_PRIORITY_FOREGROUND,
        "The top level page in the new tab should be prioritized"
      );

      Assert.equal(
        gTabPriorityWatcher.currentPriority(iframe1ChildID),
        PROCESS_PRIORITY_FOREGROUND,
        "The iframe in the new tab should be prioritized"
      );

      if (SpecialPowers.useRemoteSubframes) {
        // Basic process uniqueness checks for the state after all three tabs
        // are initially loaded.
        Assert.notEqual(
          topChildID,
          iframe1ChildID,
          "file_cross_frame.html should be loaded into a different process " +
            "than its initial iframe"
        );

        Assert.notEqual(
          topChildID,
          iframe2TabChildID,
          "file_cross_frame.html should be loaded into a different process " +
            "than the tab containing iframe2Host"
        );

        Assert.notEqual(
          iframe1ChildID,
          iframe2TabChildID,
          "The initial iframe loaded by file_cross_frame.html should be " +
            "loaded into a different process than the tab containing " +
            "iframe2Host"
        );

        // Note: this assertion depends on our process selection logic.
        // Specifically, that we reuse an existing process for an iframe if
        // possible.
        Assert.equal(
          iframe1TabChildID,
          iframe1ChildID,
          "Both pages loaded in iframe1Host should be in the same process"
        );
      }

      // Do a cross-origin navigation in the iframe in the foreground tab.
      let iframe2URI = iframe2Host + "/browser/dom/ipc/tests/file_dummy.html";
      let loaded = BrowserTestUtils.browserLoaded(browser, true, iframe2URI);
      await SpecialPowers.spawn(
        iframe,
        [iframe2URI],
        async function (_iframe2URI) {
          content.location = _iframe2URI;
        }
      );
      await loaded;

      let iframe2ChildID = browsingContextChildID(iframe);
      let iframe1Priority = gTabPriorityWatcher.currentPriority(iframe1ChildID);
      let iframe2Priority = gTabPriorityWatcher.currentPriority(iframe2ChildID);

      if (SpecialPowers.useRemoteSubframes) {
        // Basic process uniqueness check for the state after navigating the
        // iframe. There's no need to check the top level pages because they
        // have not navigated.
        //
        // iframe1ChildID != iframe2ChildID is implied by:
        //   iframe1ChildID != iframe2TabChildID
        //   iframe2TabChildID == iframe2ChildID
        //
        // iframe2ChildID != topChildID is implied by:
        //   topChildID != iframe2TabChildID
        //   iframe2TabChildID == iframe2ChildID

        // Note: this assertion depends on our process selection logic.
        // Specifically, that we reuse an existing process for an iframe if
        // possible. If that changes, this test may need to be carefully
        // rewritten, as the whole point of the test is to check what happens
        // with the priority manager when an iframe shares a process with
        // a page in another tab.
        Assert.equal(
          iframe2TabChildID,
          iframe2ChildID,
          "Both pages loaded in iframe2Host should be in the same process"
        );

        // Now that we've established the relationship between the various
        // processes, we can finally check that the priority manager is doing
        // the right thing.
        Assert.equal(
          iframe1Priority,
          PROCESS_PRIORITY_BACKGROUND,
          "The old iframe process should have been deprioritized"
        );
      } else {
        Assert.equal(
          iframe1ChildID,
          iframe2ChildID,
          "Navigation should not have switched processes"
        );
      }

      Assert.equal(
        iframe2Priority,
        PROCESS_PRIORITY_FOREGROUND,
        "The new iframe process should be prioritized"
      );
    }
  );

  await BrowserTestUtils.removeTab(iframe2Tab);
  await BrowserTestUtils.removeTab(iframe1Tab);
});

/**
 * Test that a cross-group navigation properly preserves the process priority.
 * The goal of this test is to check that the code related to mPriorityActive in
 * CanonicalBrowsingContext::ReplacedBy works correctly, but in practice the
 * prioritization code in SetRenderLayers will also make this test pass, though
 * that prioritization happens slightly later.
 */
add_task(async function test_cross_group_navigate() {
  // This page is same-site with the page we're going to cross-group navigate to.
  let coopPage =
    "https://example.com/browser/dom/tests/browser/file_coop_coep.html";

  // Load it as a top level tab so that we don't accidentally get the initial
  // load prioritization.
  let backgroundTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    coopPage
  );
  let backgroundTabChildID = browsingContextChildID(
    gBrowser.selectedBrowser.browsingContext
  );

  Assert.equal(
    gTabPriorityWatcher.currentPriority(backgroundTabChildID),
    PROCESS_PRIORITY_FOREGROUND,
    "Loading a new tab should make it prioritized"
  );

  await BrowserTestUtils.withNewTab(
    "https://example.org/browser/dom/ipc/tests/file_cross_frame.html",
    async browser => {
      Assert.equal(
        gTabPriorityWatcher.currentPriority(backgroundTabChildID),
        PROCESS_PRIORITY_BACKGROUND,
        "Switching to a new tab should deprioritize the old one"
      );

      let dotOrgChildID = browsingContextChildID(browser.browsingContext);

      // Do a cross-group navigation.
      BrowserTestUtils.loadURIString(browser, coopPage);
      await BrowserTestUtils.browserLoaded(browser);

      let coopChildID = browsingContextChildID(browser.browsingContext);
      let coopPriority = gTabPriorityWatcher.currentPriority(coopChildID);
      let dotOrgPriority = gTabPriorityWatcher.currentPriority(dotOrgChildID);

      Assert.equal(
        backgroundTabChildID,
        coopChildID,
        "The same site should get loaded into the same process"
      );
      Assert.notEqual(
        dotOrgChildID,
        coopChildID,
        "Navigation should have switched processes"
      );
      Assert.equal(
        dotOrgPriority,
        PROCESS_PRIORITY_BACKGROUND,
        "The old page process should have been deprioritized"
      );
      Assert.equal(
        coopPriority,
        PROCESS_PRIORITY_FOREGROUND,
        "The new page process should be prioritized"
      );
    }
  );

  await BrowserTestUtils.removeTab(backgroundTab);
});

/**
 * 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("https://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 = "https://example.net/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("https://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 = "https://example.net/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("https://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,
    });
  });
});

/**
 * Test that foreground tab's process priority isn't changed when going back to
 * a bfcached session history entry.
 */
add_task(async function test_audio_background_tab() {
  let page1 = "https://example.com";
  let page2 = page1 + "/?2";

  await BrowserTestUtils.withNewTab(page1, async browser => {
    let childID = browsingContextChildID(browser.browsingContext);
    Assert.equal(
      gTabPriorityWatcher.currentPriority(childID),
      PROCESS_PRIORITY_FOREGROUND,
      "Loading a new tab should make it prioritized."
    );
    let loaded = BrowserTestUtils.browserLoaded(browser, false, page2);
    BrowserTestUtils.loadURIString(browser, page2);
    await loaded;

    childID = browsingContextChildID(browser.browsingContext);
    Assert.equal(
      gTabPriorityWatcher.currentPriority(childID),
      PROCESS_PRIORITY_FOREGROUND,
      "Loading a new page should keep the tab prioritized."
    );

    let pageShowPromise = BrowserTestUtils.waitForContentEvent(
      browser,
      "pageshow"
    );
    browser.goBack();
    await pageShowPromise;

    childID = browsingContextChildID(browser.browsingContext);
    Assert.equal(
      gTabPriorityWatcher.currentPriority(childID),
      PROCESS_PRIORITY_FOREGROUND,
      "Loading a page from the bfcache should keep the tab prioritized."
    );
  });
});