summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_devices_get_user_media_default_permissions.js
blob: e6464fd4aa749dab8051da7eb905a1ac618fb8d4 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const permissionError =
  "error: NotAllowedError: The request is not allowed " +
  "by the user agent or the platform in the current context.";

const CAMERA_PREF = "permissions.default.camera";
const MICROPHONE_PREF = "permissions.default.microphone";

var gTests = [
  {
    desc: "getUserMedia audio+video: globally blocking camera",
    run: async function checkAudioVideo() {
      Services.prefs.setIntPref(CAMERA_PREF, SitePermissions.BLOCK);

      // Requesting audio+video shouldn't work.
      await Promise.all([
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:deny"),
        expectObserverCalled("recording-window-ended"),
        promiseMessage(permissionError),
        promiseRequestDevice(true, true),
      ]);
      await checkNotSharing();

      // Requesting only video shouldn't work.
      await Promise.all([
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:deny"),
        expectObserverCalled("recording-window-ended"),
        promiseMessage(permissionError),
        promiseRequestDevice(false, true),
      ]);
      await checkNotSharing();

      // Requesting audio should work.
      const observerPromise = expectObserverCalled("getUserMedia:request");
      const promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true);
      await promise;
      await observerPromise;

      is(
        PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
        "webRTC-shareMicrophone-notification-icon",
        "anchored to mic icon"
      );
      checkDeviceSelectors(["microphone"]);

      let indicator = promiseIndicatorWindow();
      let observerPromise1 = expectObserverCalled(
        "getUserMedia:response:allow"
      );
      let observerPromise2 = expectObserverCalled("recording-device-events");
      await promiseMessage("ok", () => {
        PopupNotifications.panel.firstElementChild.button.click();
      });
      await observerPromise1;
      await observerPromise2;
      Assert.deepEqual(
        await getMediaCaptureState(),
        { audio: true },
        "expected microphone to be shared"
      );

      await indicator;
      await checkSharingUI({ audio: true });
      await closeStream();
      Services.prefs.clearUserPref(CAMERA_PREF);
    },
  },

  {
    desc: "getUserMedia video: globally blocking camera + local exception",
    run: async function checkAudioVideo() {
      let browser = gBrowser.selectedBrowser;
      Services.prefs.setIntPref(CAMERA_PREF, SitePermissions.BLOCK);
      // Overwrite the permission for that URI, requesting video should work again.
      PermissionTestUtils.add(
        browser.currentURI,
        "camera",
        Services.perms.ALLOW_ACTION
      );

      // Requesting video should work.
      let indicator = promiseIndicatorWindow();
      let promises = [
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:allow"),
        expectObserverCalled("recording-device-events"),
      ];

      let promise = promiseMessage("ok");
      await promiseRequestDevice(false, true);
      await promise;

      await Promise.all(promises);
      await indicator;
      await checkSharingUI({ video: true }, undefined, undefined, {
        video: { scope: SitePermissions.SCOPE_PERSISTENT },
      });
      await closeStream();

      PermissionTestUtils.remove(browser.currentURI, "camera");
      Services.prefs.clearUserPref(CAMERA_PREF);
    },
  },

  {
    desc: "getUserMedia audio+video: globally blocking microphone",
    run: async function checkAudioVideo() {
      Services.prefs.setIntPref(MICROPHONE_PREF, SitePermissions.BLOCK);

      // Requesting audio+video shouldn't work.
      await Promise.all([
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:deny"),
        expectObserverCalled("recording-window-ended"),
        promiseMessage(permissionError),
        promiseRequestDevice(true, true),
      ]);
      await checkNotSharing();

      // Requesting only audio shouldn't work.
      await Promise.all([
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:deny"),
        expectObserverCalled("recording-window-ended"),
        promiseMessage(permissionError),
        promiseRequestDevice(true),
      ]);

      // Requesting video should work.
      const observerPromise = expectObserverCalled("getUserMedia:request");
      const promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true);
      await promise;
      await observerPromise;

      is(
        PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
        "webRTC-shareDevices-notification-icon",
        "anchored to device icon"
      );
      checkDeviceSelectors(["camera"]);

      let indicator = promiseIndicatorWindow();
      let observerPromise1 = expectObserverCalled(
        "getUserMedia:response:allow"
      );
      let observerPromise2 = expectObserverCalled("recording-device-events");
      await promiseMessage("ok", () => {
        PopupNotifications.panel.firstElementChild.button.click();
      });
      await observerPromise1;
      await observerPromise2;
      Assert.deepEqual(
        await getMediaCaptureState(),
        { video: true },
        "expected camera to be shared"
      );

      await indicator;
      await checkSharingUI({ video: true });
      await closeStream();
      Services.prefs.clearUserPref(MICROPHONE_PREF);
    },
  },

  {
    desc: "getUserMedia audio: globally blocking microphone + local exception",
    run: async function checkAudioVideo() {
      let browser = gBrowser.selectedBrowser;
      Services.prefs.setIntPref(MICROPHONE_PREF, SitePermissions.BLOCK);
      // Overwrite the permission for that URI, requesting video should work again.
      PermissionTestUtils.add(
        browser.currentURI,
        "microphone",
        Services.perms.ALLOW_ACTION
      );

      // Requesting audio should work.
      let indicator = promiseIndicatorWindow();
      let promises = [
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:allow"),
        expectObserverCalled("recording-device-events"),
      ];
      let promise = promiseMessage("ok");
      await promiseRequestDevice(true);
      await promise;

      await Promise.all(promises);
      await indicator;
      await checkSharingUI({ audio: true }, undefined, undefined, {
        audio: { scope: SitePermissions.SCOPE_PERSISTENT },
      });
      await closeStream();

      PermissionTestUtils.remove(browser.currentURI, "microphone");
      Services.prefs.clearUserPref(MICROPHONE_PREF);
    },
  },
];
add_task(async function test() {
  await runTests(gTests);
});