summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_global_mute_toggles.js
blob: 54713fa8c3f4f5bf5149651c98173c0c676bd8a2 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_ROOT = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  "https://example.com/"
);
const TEST_PAGE = TEST_ROOT + "get_user_media.html";
const MUTE_TOPICS = [
  "getUserMedia:muteVideo",
  "getUserMedia:unmuteVideo",
  "getUserMedia:muteAudio",
  "getUserMedia:unmuteAudio",
];

add_setup(async function () {
  let prefs = [
    [PREF_PERMISSION_FAKE, true],
    [PREF_AUDIO_LOOPBACK, ""],
    [PREF_VIDEO_LOOPBACK, ""],
    [PREF_FAKE_STREAMS, true],
    [PREF_FOCUS_SOURCE, false],
    ["privacy.webrtc.globalMuteToggles", true],
  ];
  await SpecialPowers.pushPrefEnv({ set: prefs });
});

/**
 * Returns a Promise that resolves when the content process for
 * <browser> fires the right observer notification based on the
 * value of isMuted for the camera.
 *
 * Note: Callers must ensure that they first call
 * BrowserTestUtils.startObservingTopics to monitor the mute and
 * unmute observer notifications for this to work properly.
 *
 * @param {<xul:browser>} browser - The browser running in the content process
 * to be monitored.
 * @param {Boolean} isMuted - True if the muted topic should be fired.
 * @return {Promise}
 * @resolves {undefined} When the notification fires.
 */
function waitForCameraMuteState(browser, isMuted) {
  let topic = isMuted ? "getUserMedia:muteVideo" : "getUserMedia:unmuteVideo";
  return BrowserTestUtils.contentTopicObserved(browser.browsingContext, topic);
}

/**
 * Returns a Promise that resolves when the content process for
 * <browser> fires the right observer notification based on the
 * value of isMuted for the microphone.
 *
 * Note: Callers must ensure that they first call
 * BrowserTestUtils.startObservingTopics to monitor the mute and
 * unmute observer notifications for this to work properly.
 *
 * @param {<xul:browser>} browser - The browser running in the content process
 * to be monitored.
 * @param {Boolean} isMuted - True if the muted topic should be fired.
 * @return {Promise}
 * @resolves {undefined} When the notification fires.
 */
function waitForMicrophoneMuteState(browser, isMuted) {
  let topic = isMuted ? "getUserMedia:muteAudio" : "getUserMedia:unmuteAudio";
  return BrowserTestUtils.contentTopicObserved(browser.browsingContext, topic);
}

/**
 * Tests that the global mute toggles fire the right observer
 * notifications in pre-existing content processes.
 */
add_task(async function test_notifications() {
  await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
    let indicatorPromise = promiseIndicatorWindow();

    await shareDevices(browser, true /* camera */, true /* microphone */);

    let indicator = await indicatorPromise;
    let doc = indicator.document;

    let microphoneMute = doc.getElementById("microphone-mute-toggle");
    let cameraMute = doc.getElementById("camera-mute-toggle");

    Assert.ok(
      !microphoneMute.checked,
      "Microphone toggle should not start checked."
    );
    Assert.ok(!cameraMute.checked, "Camera toggle should not start checked.");

    await BrowserTestUtils.startObservingTopics(
      browser.browsingContext,
      MUTE_TOPICS
    );

    info("Muting microphone...");
    let microphoneMuted = waitForMicrophoneMuteState(browser, true);
    microphoneMute.click();
    await microphoneMuted;
    info("Microphone successfully muted.");

    info("Muting camera...");
    let cameraMuted = waitForCameraMuteState(browser, true);
    cameraMute.click();
    await cameraMuted;
    info("Camera successfully muted.");

    Assert.ok(
      microphoneMute.checked,
      "Microphone toggle should now be checked."
    );
    Assert.ok(cameraMute.checked, "Camera toggle should now be checked.");

    info("Unmuting microphone...");
    let microphoneUnmuted = waitForMicrophoneMuteState(browser, false);
    microphoneMute.click();
    await microphoneUnmuted;
    info("Microphone successfully unmuted.");

    info("Unmuting camera...");
    let cameraUnmuted = waitForCameraMuteState(browser, false);
    cameraMute.click();
    await cameraUnmuted;
    info("Camera successfully unmuted.");

    await BrowserTestUtils.stopObservingTopics(
      browser.browsingContext,
      MUTE_TOPICS
    );
  });
});

/**
 * Tests that if sharing stops while muted, and the indicator closes,
 * then the mute state is reset.
 */
add_task(async function test_closing_indicator_resets_mute() {
  await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
    let indicatorPromise = promiseIndicatorWindow();

    await shareDevices(browser, true /* camera */, true /* microphone */);

    let indicator = await indicatorPromise;
    let doc = indicator.document;

    let microphoneMute = doc.getElementById("microphone-mute-toggle");
    let cameraMute = doc.getElementById("camera-mute-toggle");

    Assert.ok(
      !microphoneMute.checked,
      "Microphone toggle should not start checked."
    );
    Assert.ok(!cameraMute.checked, "Camera toggle should not start checked.");

    await BrowserTestUtils.startObservingTopics(
      browser.browsingContext,
      MUTE_TOPICS
    );

    info("Muting microphone...");
    let microphoneMuted = waitForMicrophoneMuteState(browser, true);
    microphoneMute.click();
    await microphoneMuted;
    info("Microphone successfully muted.");

    info("Muting camera...");
    let cameraMuted = waitForCameraMuteState(browser, true);
    cameraMute.click();
    await cameraMuted;
    info("Camera successfully muted.");

    Assert.ok(
      microphoneMute.checked,
      "Microphone toggle should now be checked."
    );
    Assert.ok(cameraMute.checked, "Camera toggle should now be checked.");

    let allUnmuted = Promise.all([
      waitForMicrophoneMuteState(browser, false),
      waitForCameraMuteState(browser, false),
    ]);

    await closeStream();
    await allUnmuted;

    await BrowserTestUtils.stopObservingTopics(
      browser.browsingContext,
      MUTE_TOPICS
    );
  });
});

/**
 * Test that if the global mute state is set, then newly created
 * content processes also have their tracks muted after sending
 * a getUserMedia request.
 */
add_task(async function test_new_processes() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_PAGE,
  });
  let browser1 = tab1.linkedBrowser;

  let indicatorPromise = promiseIndicatorWindow();

  await shareDevices(browser1, true /* camera */, true /* microphone */);

  let indicator = await indicatorPromise;
  let doc = indicator.document;

  let microphoneMute = doc.getElementById("microphone-mute-toggle");
  let cameraMute = doc.getElementById("camera-mute-toggle");

  Assert.ok(
    !microphoneMute.checked,
    "Microphone toggle should not start checked."
  );
  Assert.ok(!cameraMute.checked, "Camera toggle should not start checked.");

  await BrowserTestUtils.startObservingTopics(
    browser1.browsingContext,
    MUTE_TOPICS
  );

  info("Muting microphone...");
  let microphoneMuted = waitForMicrophoneMuteState(browser1, true);
  microphoneMute.click();
  await microphoneMuted;
  info("Microphone successfully muted.");

  info("Muting camera...");
  let cameraMuted = waitForCameraMuteState(browser1, true);
  cameraMute.click();
  await cameraMuted;
  info("Camera successfully muted.");

  // We'll make sure a new process is being launched by observing
  // for the ipc:content-created notification.
  let processLaunched = TestUtils.topicObserved("ipc:content-created");

  let tab2 = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_PAGE,
    forceNewProcess: true,
  });
  let browser2 = tab2.linkedBrowser;

  await processLaunched;

  await BrowserTestUtils.startObservingTopics(
    browser2.browsingContext,
    MUTE_TOPICS
  );

  let microphoneMuted2 = waitForMicrophoneMuteState(browser2, true);
  let cameraMuted2 = waitForCameraMuteState(browser2, true);
  info("Sharing the microphone and camera from a new process.");
  await shareDevices(browser2, true /* camera */, true /* microphone */);
  await Promise.all([microphoneMuted2, cameraMuted2]);

  info("Unmuting microphone...");
  let microphoneUnmuted = Promise.all([
    waitForMicrophoneMuteState(browser1, false),
    waitForMicrophoneMuteState(browser2, false),
  ]);
  microphoneMute.click();
  await microphoneUnmuted;
  info("Microphone successfully unmuted.");

  info("Unmuting camera...");
  let cameraUnmuted = Promise.all([
    waitForCameraMuteState(browser1, false),
    waitForCameraMuteState(browser2, false),
  ]);
  cameraMute.click();
  await cameraUnmuted;
  info("Camera successfully unmuted.");

  await BrowserTestUtils.stopObservingTopics(
    browser1.browsingContext,
    MUTE_TOPICS
  );

  await BrowserTestUtils.stopObservingTopics(
    browser2.browsingContext,
    MUTE_TOPICS
  );

  BrowserTestUtils.removeTab(tab2);
  BrowserTestUtils.removeTab(tab1);
});