summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_timeout_throttling_with_audio_playback.js
blob: 683d1325ce06b63bf33f7e58aa4a28108a2a8867 (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
// The tab closing code leaves an uncaught rejection. This test has been
// whitelisted until the issue is fixed.
if (!gMultiProcessBrowser) {
  const { PromiseTestUtils } = ChromeUtils.importESModule(
    "resource://testing-common/PromiseTestUtils.sys.mjs"
  );
  PromiseTestUtils.expectUncaughtRejection(/is no longer, usable/);
}

const kBaseURI = "http://mochi.test:8888/browser/dom/base/test/empty.html";
var testURLs = [
  "http://mochi.test:8888/browser/dom/base/test/file_audioLoop.html",
  "http://mochi.test:8888/browser/dom/base/test/file_audioLoopInIframe.html",
  "http://mochi.test:8888/browser/dom/base/test/file_webaudio_startstop.html",
];

// We want to ensure that while audio is being played back, a background tab is
// treated the same as a foreground tab as far as timeout throttling is concerned.
// So we use a 100,000 second minimum timeout value for background tabs.  This
// means that in case the test fails, it will time out in practice, but just for
// sanity the test condition ensures that the observed timeout delay falls in
// this range.
const kMinTimeoutBackground = 100 * 1000 * 1000;

const kDelay = 10;

async function runTest(url) {
  let currentTab = gBrowser.selectedTab;
  let newTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, kBaseURI);
  let newBrowser = gBrowser.getBrowserForTab(newTab);

  // Wait for the UI to indicate that audio is being played back.
  let promise = BrowserTestUtils.waitForAttribute(
    "soundplaying",
    newTab,
    "true"
  );
  BrowserTestUtils.loadURI(newBrowser, url);
  await promise;

  // Put the tab in the background.
  await BrowserTestUtils.switchTab(gBrowser, currentTab);

  let timeout = await SpecialPowers.spawn(newBrowser, [kDelay], function(
    delay
  ) {
    return new Promise(resolve => {
      let before = new Date();
      content.window.setTimeout(function() {
        let after = new Date();
        resolve(after - before);
      }, delay);
    });
  });
  ok(timeout <= kMinTimeoutBackground, `Got the correct timeout (${timeout})`);

  // All done.
  BrowserTestUtils.removeTab(newTab);
}

add_setup(async function() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.min_background_timeout_value", kMinTimeoutBackground]],
  });
});

add_task(async function test() {
  for (var url of testURLs) {
    await runTest(url);
  }
});