summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_stop_streams_on_indicator_close.js
blob: b5d4229fdcbb4841cc89613d3c7068001dbd685f (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
/* 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";

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

/**
 * Tests that if the indicator is closed somehow by the user when streams
 * still ongoing, that all of those streams it represents are stopped, and
 * the most recent tab that a stream was shared with is selected.
 *
 * This test makes sure the global mute toggles for camera and microphone
 * are disabled, so the indicator only represents display streams, and only
 * those streams should be stopped on close.
 */
add_task(async function test_close_indicator_no_global_toggles() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.webrtc.globalMuteToggles", false]],
  });

  let indicatorPromise = promiseIndicatorWindow();

  info("Opening first tab");
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE);
  info("Sharing camera, microphone and screen");
  await shareDevices(tab1.linkedBrowser, true, true, SHARE_SCREEN, false);

  info("Opening second tab");
  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE);
  info("Sharing camera, microphone and screen");
  await shareDevices(tab2.linkedBrowser, true, true, SHARE_SCREEN, true);

  info("Opening third tab");
  let tab3 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE);
  info("Sharing screen");
  await shareDevices(tab3.linkedBrowser, false, false, SHARE_SCREEN, false);

  info("Opening fourth tab");
  let tab4 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.com"
  );

  Assert.equal(
    gBrowser.selectedTab,
    tab4,
    "Most recently opened tab is selected"
  );

  let indicator = await indicatorPromise;

  indicator.close();

  // Wait a tick of the event loop to give the unload handler in the indicator
  // a chance to run.
  await new Promise(resolve => executeSoon(resolve));

  // Make sure the media capture state has a chance to flush up to the parent.
  await getMediaCaptureState();

  // The camera and microphone streams should still be active.
  let camStreams = webrtcUI.getActiveStreams(true, false);
  Assert.equal(camStreams.length, 2, "Should have found two camera streams");
  let micStreams = webrtcUI.getActiveStreams(false, true);
  Assert.equal(
    micStreams.length,
    2,
    "Should have found two microphone streams"
  );

  // The camera and microphone permission were remembered for tab2, so check to
  // make sure that the permissions remain.
  let { state: camState, scope: camScope } = SitePermissions.getForPrincipal(
    tab2.linkedBrowser.contentPrincipal,
    "camera",
    tab2.linkedBrowser
  );
  Assert.equal(camState, SitePermissions.ALLOW);
  Assert.equal(camScope, SitePermissions.SCOPE_PERSISTENT);

  let { state: micState, scope: micScope } = SitePermissions.getForPrincipal(
    tab2.linkedBrowser.contentPrincipal,
    "microphone",
    tab2.linkedBrowser
  );
  Assert.equal(micState, SitePermissions.ALLOW);
  Assert.equal(micScope, SitePermissions.SCOPE_PERSISTENT);

  Assert.equal(
    gBrowser.selectedTab,
    tab3,
    "Most recently tab that streams were shared with is selected"
  );

  SitePermissions.removeFromPrincipal(
    tab2.linkedBrowser.contentPrincipal,
    "camera",
    tab2.linkedBrowser
  );

  SitePermissions.removeFromPrincipal(
    tab2.linkedBrowser.contentPrincipal,
    "microphone",
    tab2.linkedBrowser
  );

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

/**
 * Tests that if the indicator is closed somehow by the user when streams
 * still ongoing, that all of those streams is represents are stopped, and
 * the most recent tab that a stream was shared with is selected.
 *
 * This test makes sure the global mute toggles are enabled. This means that
 * when the user manages to close the indicator, we should revoke camera
 * and microphone permissions too.
 */
add_task(async function test_close_indicator_with_global_toggles() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.webrtc.globalMuteToggles", true]],
  });

  let indicatorPromise = promiseIndicatorWindow();

  info("Opening first tab");
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE);
  info("Sharing camera, microphone and screen");
  await shareDevices(tab1.linkedBrowser, true, true, SHARE_SCREEN, false);

  info("Opening second tab");
  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE);
  info("Sharing camera, microphone and screen");
  await shareDevices(tab2.linkedBrowser, true, true, SHARE_SCREEN, true);

  info("Opening third tab");
  let tab3 = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PAGE);
  info("Sharing screen");
  await shareDevices(tab3.linkedBrowser, false, false, SHARE_SCREEN, false);

  info("Opening fourth tab");
  let tab4 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.com"
  );

  Assert.equal(
    gBrowser.selectedTab,
    tab4,
    "Most recently opened tab is selected"
  );

  let indicator = await indicatorPromise;

  indicator.close();

  // Wait a tick of the event loop to give the unload handler in the indicator
  // a chance to run.
  await new Promise(resolve => executeSoon(resolve));

  Assert.deepEqual(
    await getMediaCaptureState(),
    {},
    "expected nothing to be shared"
  );

  // Ensuring we no longer have any active streams.
  let streams = webrtcUI.getActiveStreams(true, true, true, true);
  Assert.equal(streams.length, 0, "Should have found no active streams");

  // The camera and microphone permissions should have been cleared.
  let { state: camState } = SitePermissions.getForPrincipal(
    tab2.linkedBrowser.contentPrincipal,
    "camera",
    tab2.linkedBrowser
  );
  Assert.equal(camState, SitePermissions.UNKNOWN);

  let { state: micState } = SitePermissions.getForPrincipal(
    tab2.linkedBrowser.contentPrincipal,
    "microphone",
    tab2.linkedBrowser
  );
  Assert.equal(micState, SitePermissions.UNKNOWN);

  Assert.equal(
    gBrowser.selectedTab,
    tab3,
    "Most recently tab that streams were shared with is selected"
  );

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