summaryrefslogtreecommitdiffstats
path: root/dom/media/mediacontrol/tests/browser/browser_audio_focus_management.js
blob: 980281243d55c2f5def9a8dacd0e4c1e06aceb26 (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
const PAGE_AUDIBLE =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_autoplay.html";
const PAGE_INAUDIBLE =
  "https://example.com/browser/dom/media/mediacontrol/tests/browser/file_muted_autoplay.html";

const testVideoId = "autoplay";

/**
 * These tests are used to ensure that the audio focus management works correctly
 * amongs different tabs no matter the pref is on or off. If the pref is on,
 * there is only one tab which is allowed to play audio at a time, the last tab
 * starting audio will immediately stop other tabs which own audio focus. But
 * notice that playing inaudible media won't gain audio focus. If the pref is
 * off, all audible tabs can own audio focus at the same time without
 * interfering each others.
 */
add_task(async function testDisableAudioFocusManagement() {
  await switchAudioFocusManagerment(false);

  info(`open audible autoplay media in tab1`);
  const tab1 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(`open same page on another tab, which shouldn't cause audio competing`);
  const tab2 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab2, testVideoId);

  info(`media in tab1 should be playing still`);
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(`remove tabs`);
  await clearTabsAndResetPref([tab1, tab2]);
});

add_task(async function testEnableAudioFocusManagement() {
  await switchAudioFocusManagerment(true);

  info(`open audible autoplay media in tab1`);
  const tab1 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(`open same page on another tab, which should cause audio competing`);
  const tab2 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab2, testVideoId);

  info(`media in tab1 should be stopped`);
  await checkOrWaitUntilMediaStoppedPlaying(tab1, testVideoId);

  info(`remove tabs`);
  await clearTabsAndResetPref([tab1, tab2]);
});

add_task(async function testCheckAudioCompetingMultipleTimes() {
  await switchAudioFocusManagerment(true);

  info(`open audible autoplay media in tab1`);
  const tab1 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(`open same page on another tab, which should cause audio competing`);
  const tab2 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab2, testVideoId);

  info(`media in tab1 should be stopped`);
  await checkOrWaitUntilMediaStoppedPlaying(tab1, testVideoId);

  info(`play media in tab1 again`);
  await playMedia(tab1);

  info(`media in tab2 should be stopped`);
  await checkOrWaitUntilMediaStoppedPlaying(tab2, testVideoId);

  info(`play media in tab2 again`);
  await playMedia(tab2);

  info(`media in tab1 should be stopped`);
  await checkOrWaitUntilMediaStoppedPlaying(tab1, testVideoId);

  info(`remove tabs`);
  await clearTabsAndResetPref([tab1, tab2]);
});

add_task(async function testMutedMediaWontInvolveAudioCompeting() {
  await switchAudioFocusManagerment(true);

  info(`open audible autoplay media in tab1`);
  const tab1 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(
    `open inaudible media page on another tab, which shouldn't cause audio competing`
  );
  const tab2 = await createLoadedTabWrapper(PAGE_INAUDIBLE, {
    needCheck: false,
  });
  await checkOrWaitUntilMediaStartedPlaying(tab2, testVideoId);

  info(`media in tab1 should be playing still`);
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(
    `open audible media page on the third tab, which should cause audio competing`
  );
  const tab3 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab3, testVideoId);

  info(`media in tab1 should be stopped`);
  await checkOrWaitUntilMediaStoppedPlaying(tab1, testVideoId);

  info(`media in tab2 should not be affected because it's inaudible.`);
  await checkOrWaitUntilMediaStartedPlaying(tab2, testVideoId);

  info(`remove tabs`);
  await clearTabsAndResetPref([tab1, tab2, tab3]);
});

add_task(async function testStopMultipleTabsWhenSwitchingPrefDynamically() {
  await switchAudioFocusManagerment(false);

  info(`open audible autoplay media in tab1`);
  const tab1 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab1, testVideoId);

  info(`open same page on another tab, which shouldn't cause audio competing`);
  const tab2 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab2, testVideoId);

  await switchAudioFocusManagerment(true);

  info(`open same page on the third tab, which should cause audio competing`);
  const tab3 = await createLoadedTabWrapper(PAGE_AUDIBLE, { needCheck: false });
  await checkOrWaitUntilMediaStartedPlaying(tab3, testVideoId);

  info(`media in tab1 and tab2 should be stopped`);
  await checkOrWaitUntilMediaStoppedPlaying(tab1, testVideoId);
  await checkOrWaitUntilMediaStoppedPlaying(tab2, testVideoId);

  info(`remove tabs`);
  await clearTabsAndResetPref([tab1, tab2, tab3]);
});

/**
 * The following are helper funcions.
 */
async function switchAudioFocusManagerment(enable) {
  const state = enable ? "Enable" : "Disable";
  info(`${state} audio focus management`);
  await SpecialPowers.pushPrefEnv({
    set: [["media.audioFocus.management", enable]],
  });
}

async function playMedia(tab) {
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    return new Promise(resolve => {
      const video = content.document.getElementById("autoplay");
      if (!video) {
        ok(false, `can't get the media element!`);
      }

      ok(video.paused, `media has not started yet`);
      info(`wait until media starts playing`);
      video.play();
      video.onplaying = () => {
        video.onplaying = null;
        ok(true, `media started playing`);
        resolve();
      };
    });
  });
}

async function clearTabsAndResetPref(tabs) {
  info(`clear tabs and reset pref`);
  for (let tab of tabs) {
    await tab.close();
  }
  await switchAudioFocusManagerment(false);
}