summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_mediaPlayback_mute.js
blob: 852fc56fb17b89595608f6363af69fcecf634509 (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
const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_mediaPlayback2.html";
const FRAME = "https://example.com/browser/toolkit/content/tests/browser/file_mediaPlaybackFrame2.html";

function wait_for_event(browser, event) {
  return BrowserTestUtils.waitForEvent(browser, event, false, (event) => {
    is(event.originalTarget, browser, "Event must be dispatched to correct browser.");
    return true;
  });
}

function* test_audio_in_browser() {
  function get_audio_element() {
    var doc = content.document;
    var list = doc.getElementsByTagName('audio');
    if (list.length == 1) {
      return list[0];
    }

    // iframe?
    list = doc.getElementsByTagName('iframe');

    var iframe = list[0];
    list = iframe.contentDocument.getElementsByTagName('audio');
    return list[0];
  }

  var audio = get_audio_element();
  return {
    computedVolume: audio.computedVolume,
    computedMuted: audio.computedMuted
  }
}

function* test_on_browser(url, browser) {
  browser.loadURI(url);
  yield wait_for_event(browser, "DOMAudioPlaybackStarted");

  var result = yield ContentTask.spawn(browser, null, test_audio_in_browser);
  is(result.computedVolume, 1, "Audio volume is 1");
  is(result.computedMuted, false, "Audio is not muted");

  ok(!browser.audioMuted, "Audio should not be muted by default");
  browser.mute();
  ok(browser.audioMuted, "Audio should be muted now");

  yield wait_for_event(browser, "DOMAudioPlaybackStopped");

  result = yield ContentTask.spawn(browser, null, test_audio_in_browser);
  is(result.computedVolume, 0, "Audio volume is 0 when muted");
  is(result.computedMuted, true, "Audio is muted");
}

function* test_visibility(url, browser) {
  browser.loadURI(url);
  yield wait_for_event(browser, "DOMAudioPlaybackStarted");

  var result = yield ContentTask.spawn(browser, null, test_audio_in_browser);
  is(result.computedVolume, 1, "Audio volume is 1");
  is(result.computedMuted, false, "Audio is not muted");

  yield BrowserTestUtils.withNewTab({
    gBrowser,
    url: "about:blank",
  }, function() {});

  ok(!browser.audioMuted, "Audio should not be muted by default");
  browser.mute();
  ok(browser.audioMuted, "Audio should be muted now");

  yield wait_for_event(browser, "DOMAudioPlaybackStopped");

  result = yield ContentTask.spawn(browser, null, test_audio_in_browser);
  is(result.computedVolume, 0, "Audio volume is 0 when muted");
  is(result.computedMuted, true, "Audio is muted");
}

add_task(function*() {
  yield new Promise((resolve) => {
    SpecialPowers.pushPrefEnv({"set": [
      ["media.useAudioChannelService.testing", true]
    ]}, resolve);
  });
});

add_task(function* test_page() {
  yield BrowserTestUtils.withNewTab({
    gBrowser,
    url: "about:blank",
  }, test_on_browser.bind(undefined, PAGE));
});

add_task(function* test_frame() {
  yield BrowserTestUtils.withNewTab({
    gBrowser,
    url: "about:blank",
  }, test_on_browser.bind(undefined, FRAME));
});

add_task(function* test_frame() {
  yield BrowserTestUtils.withNewTab({
    gBrowser,
    url: "about:blank",
  }, test_visibility.bind(undefined, PAGE));
});