summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_stop_sharing_button.js
blob: 17ab66abc4f6d81afc11852b3dbeb75ef9c0709d (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
/* 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 user chooses to "Stop Sharing" a display while
 * also sharing their microphone or camera, that only the display
 * stream is stopped.
 */
add_task(async function test_stop_sharing() {
  await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
    let indicatorPromise = promiseIndicatorWindow();

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

    let indicator = await indicatorPromise;

    let stopSharingButton = indicator.document.getElementById("stop-sharing");
    let stopSharingPromise = expectObserverCalled("recording-device-events");
    stopSharingButton.click();
    await stopSharingPromise;

    // Ensure that we're still sharing the other streams.
    await checkSharingUI({ audio: true, video: true });

    // Ensure that the "display-share" section of the indicator is now hidden
    Assert.ok(
      BrowserTestUtils.is_hidden(
        indicator.document.getElementById("display-share")
      ),
      "The display-share section of the indicator should now be hidden."
    );
  });
});

/**
 * Tests that if the user chooses to "Stop Sharing" a display while
 * sharing their display on multiple sites, all of those display sharing
 * streams are closed.
 */
add_task(async function test_stop_sharing_multiple() {
  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);

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

  let indicator = await indicatorPromise;

  let stopSharingButton = indicator.document.getElementById("stop-sharing");
  let stopSharingPromise = TestUtils.waitForCondition(() => {
    return !webrtcUI.showScreenSharingIndicator;
  });
  stopSharingButton.click();
  await stopSharingPromise;

  Assert.equal(gBrowser.selectedTab, tab2, "Should have tab2 selected.");
  await checkSharingUI({ audio: false, video: true }, window, {
    audio: true,
    video: true,
  });
  BrowserTestUtils.removeTab(tab2);

  Assert.equal(gBrowser.selectedTab, tab1, "Should have tab1 selected.");
  await checkSharingUI({ audio: true, video: true }, window, {
    audio: true,
    video: true,
  });

  // Ensure that the "display-share" section of the indicator is now hidden
  Assert.ok(
    BrowserTestUtils.is_hidden(
      indicator.document.getElementById("display-share")
    ),
    "The display-share section of the indicator should now be hidden."
  );

  BrowserTestUtils.removeTab(tab1);
});

/**
 * Tests that if the user chooses to "Stop Sharing" a display, persistent
 * permissions are not removed for camera or microphone devices.
 */
add_task(async function test_keep_permissions() {
  await BrowserTestUtils.withNewTab(TEST_PAGE, async browser => {
    let indicatorPromise = promiseIndicatorWindow();

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

    let indicator = await indicatorPromise;

    let stopSharingButton = indicator.document.getElementById("stop-sharing");
    let stopSharingPromise = expectObserverCalled("recording-device-events");
    stopSharingButton.click();
    await stopSharingPromise;

    // Ensure that we're still sharing the other streams.
    await checkSharingUI({ audio: true, video: true }, undefined, undefined, {
      audio: { scope: SitePermissions.SCOPE_PERSISTENT },
      video: { scope: SitePermissions.SCOPE_PERSISTENT },
    });

    // Ensure that the "display-share" section of the indicator is now hidden
    Assert.ok(
      BrowserTestUtils.is_hidden(
        indicator.document.getElementById("display-share")
      ),
      "The display-share section of the indicator should now be hidden."
    );

    let { state: micState, scope: micScope } = SitePermissions.getForPrincipal(
      browser.contentPrincipal,
      "microphone",
      browser
    );

    Assert.equal(micState, SitePermissions.ALLOW);
    Assert.equal(micScope, SitePermissions.SCOPE_PERSISTENT);

    let { state: camState, scope: camScope } = SitePermissions.getForPrincipal(
      browser.contentPrincipal,
      "camera",
      browser
    );
    Assert.equal(camState, SitePermissions.ALLOW);
    Assert.equal(camScope, SitePermissions.SCOPE_PERSISTENT);

    SitePermissions.removeFromPrincipal(
      browser.contentPrincipal,
      "camera",
      browser
    );
    SitePermissions.removeFromPrincipal(
      browser.contentPrincipal,
      "microphone",
      browser
    );
  });
});