summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_devices_get_user_media_screen.js
blob: d09d7f2c5f777f26e90dafd21dc6594b6e2d778d (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/* 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/. */

// The rejection "The fetching process for the media resource was aborted by the
// user agent at the user's request." is left unhandled in some cases. This bug
// should be fixed, but for the moment this file allows a class of rejections.
//
// NOTE: Allowing a whole class of rejections should be avoided. Normally you
//       should use "expectUncaughtRejection" to flag individual failures.
const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(/aborted by the user agent/);

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

const notFoundError = "error: NotFoundError: The object can not be found here.";

const isHeadless = Services.env.get("MOZ_HEADLESS");

function verifyTabSharingPopup(expectedItems) {
  let event = new MouseEvent("popupshowing");
  let sharingMenu = document.getElementById("tabSharingMenuPopup");
  sharingMenu.dispatchEvent(event);

  is(
    sharingMenu.children.length,
    expectedItems.length,
    "correct number of items on tab sharing menu"
  );
  for (let i = 0; i < expectedItems.length; i++) {
    is(
      JSON.parse(sharingMenu.children[i].getAttribute("data-l10n-args"))
        .itemList,
      expectedItems[i],
      "label of item " + i + " + was correct"
    );
  }

  sharingMenu.dispatchEvent(new MouseEvent("popuphiding"));
}

var gTests = [
  {
    desc: "getUserMedia window/screen picking screen",
    run: async function checkWindowOrScreen() {
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;

      is(
        PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
        "webRTC-shareScreen-notification-icon",
        "anchored to device icon"
      );
      checkDeviceSelectors(["screen"]);
      let notification = PopupNotifications.panel.firstElementChild;

      let menulist = document.getElementById("webRTC-selectWindow-menulist");
      let count = menulist.itemCount;
      ok(
        count >= 4,
        "There should be the 'Select Window or Screen' item, a separator and at least one window and one screen"
      );

      let noWindowOrScreenItem = menulist.getItemAtIndex(0);
      ok(
        noWindowOrScreenItem.hasAttribute("selected"),
        "the 'Select Window or Screen' item is selected"
      );
      is(
        menulist.selectedItem,
        noWindowOrScreenItem,
        "'Select Window or Screen' is the selected item"
      );
      is(menulist.value, "-1", "no window or screen is selected by default");
      ok(
        noWindowOrScreenItem.disabled,
        "'Select Window or Screen' item is disabled"
      );
      ok(notification.button.disabled, "Allow button is disabled");
      ok(
        notification.hasAttribute("invalidselection"),
        "Notification is marked as invalid"
      );

      let separator = menulist.getItemAtIndex(1);
      is(
        separator.localName,
        "menuseparator",
        "the second item is a separator"
      );

      ok(
        document.getElementById("webRTC-all-windows-shared").hidden,
        "the 'all windows will be shared' warning should be hidden while there's no selection"
      );
      ok(
        document.getElementById("webRTC-preview").hidden,
        "the preview area is hidden"
      );

      let scaryScreenIndex;
      for (let i = 2; i < count; ++i) {
        let item = menulist.getItemAtIndex(i);
        is(
          parseInt(item.getAttribute("value")),
          i - 2,
          "the window/screen item has the correct index"
        );
        let type = item.getAttribute("devicetype");
        ok(
          ["window", "screen"].includes(type),
          "the devicetype attribute is set correctly"
        );
        if (type == "screen") {
          ok(item.scary, "the screen item is marked as scary");
          scaryScreenIndex = i;
        }
      }
      ok(
        typeof scaryScreenIndex == "number",
        "there's at least one scary screen, as as all screens are"
      );

      // Select a screen, a preview with a scary warning should appear.
      menulist.getItemAtIndex(scaryScreenIndex).doCommand();
      ok(
        !document.getElementById("webRTC-all-windows-shared").hidden,
        "the 'all windows will be shared' warning should now be visible"
      );
      await TestUtils.waitForCondition(
        () => !document.getElementById("webRTC-preview").hidden,
        "preview unhide",
        100,
        100
      );
      ok(
        !document.getElementById("webRTC-preview").hidden,
        "the preview area is visible"
      );
      ok(
        !document.getElementById("webRTC-previewWarningBox").hidden,
        "the scary warning is visible"
      );
      ok(!notification.button.disabled, "Allow button is enabled");

      // Select the 'Select Window or Screen' item again, the preview should be hidden.
      menulist.getItemAtIndex(0).doCommand();
      ok(
        document.getElementById("webRTC-all-windows-shared").hidden,
        "the 'all windows will be shared' warning should now be hidden"
      );
      ok(
        document.getElementById("webRTC-preview").hidden,
        "the preview area is hidden"
      );

      // Select the scary screen again so that we can have a stream.
      menulist.getItemAtIndex(scaryScreenIndex).doCommand();

      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(),
        { screen: "Screen" },
        "expected screen to be shared"
      );

      await indicator;
      await checkSharingUI({ screen: "Screen" });
      verifyTabSharingPopup(["screen"]);

      // we always show prompt for screen sharing.
      promise = promisePopupNotificationShown("webRTC-shareDevices");
      observerPromise = expectObserverCalled("getUserMedia:request");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;

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

      observerPromise = expectObserverCalled("getUserMedia:response:deny");
      await promiseMessage(permissionError, () => {
        activateSecondaryAction(kActionDeny);
      });

      await observerPromise;
      SitePermissions.removeFromPrincipal(
        null,
        "screen",
        gBrowser.selectedBrowser
      );
      await closeStream();
    },
  },

  {
    desc: "getUserMedia window/screen picking window",
    run: async function checkWindowOrScreen() {
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "window");
      await promise;
      await observerPromise;

      is(
        PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
        "webRTC-shareScreen-notification-icon",
        "anchored to device icon"
      );
      checkDeviceSelectors(["screen"]);
      let notification = PopupNotifications.panel.firstElementChild;

      let menulist = document.getElementById("webRTC-selectWindow-menulist");
      let count = menulist.itemCount;
      ok(
        count >= 4,
        "There should be the 'Select Window or Screen' item, a separator and at least one window and one screen"
      );

      let noWindowOrScreenItem = menulist.getItemAtIndex(0);
      ok(
        noWindowOrScreenItem.hasAttribute("selected"),
        "the 'Select Window or Screen' item is selected"
      );
      is(
        menulist.selectedItem,
        noWindowOrScreenItem,
        "'Select Window or Screen' is the selected item"
      );
      is(menulist.value, "-1", "no window or screen is selected by default");
      ok(
        noWindowOrScreenItem.disabled,
        "'Select Window or Screen' item is disabled"
      );
      ok(notification.button.disabled, "Allow button is disabled");
      ok(
        notification.hasAttribute("invalidselection"),
        "Notification is marked as invalid"
      );

      let separator = menulist.getItemAtIndex(1);
      is(
        separator.localName,
        "menuseparator",
        "the second item is a separator"
      );

      ok(
        document.getElementById("webRTC-all-windows-shared").hidden,
        "the 'all windows will be shared' warning should be hidden while there's no selection"
      );
      ok(
        document.getElementById("webRTC-preview").hidden,
        "the preview area is hidden"
      );

      let scaryWindowIndexes = [],
        nonScaryWindowIndex,
        scaryScreenIndex;
      for (let i = 2; i < count; ++i) {
        let item = menulist.getItemAtIndex(i);
        is(
          parseInt(item.getAttribute("value")),
          i - 2,
          "the window/screen item has the correct index"
        );
        let type = item.getAttribute("devicetype");
        ok(
          ["window", "screen"].includes(type),
          "the devicetype attribute is set correctly"
        );
        if (type == "screen") {
          ok(item.scary, "the screen item is marked as scary");
          scaryScreenIndex = i;
        } else if (item.scary) {
          scaryWindowIndexes.push(i);
        } else {
          nonScaryWindowIndex = i;
        }
      }
      if (isHeadless) {
        is(
          scaryWindowIndexes.length,
          0,
          "there are no scary Firefox windows in headless mode"
        );
      } else {
        ok(
          scaryWindowIndexes.length,
          "there's at least one scary window, as Firefox is running"
        );
      }
      ok(
        typeof scaryScreenIndex == "number",
        "there's at least one scary screen, as all screens are"
      );

      if (!isHeadless) {
        // Select one scary window, a preview with a scary warning should appear.
        let scaryWindowIndex;
        for (scaryWindowIndex of scaryWindowIndexes) {
          menulist.getItemAtIndex(scaryWindowIndex).doCommand();
          ok(
            document.getElementById("webRTC-all-windows-shared").hidden,
            "the 'all windows will be shared' warning should still be hidden"
          );
          try {
            await TestUtils.waitForCondition(
              () => !document.getElementById("webRTC-preview").hidden,
              "",
              100,
              100
            );
            break;
          } catch (e) {
            // A "scary window" is Firefox. Multiple Firefox windows have been
            // observed to come and go during try runs, so we won't know which one
            // is ours. To avoid intermittents, we ignore preview failing due to
            // these going away on us, provided it succeeds on one of them.
          }
        }
        ok(
          !document.getElementById("webRTC-preview").hidden,
          "the preview area is visible"
        );
        ok(
          !document.getElementById("webRTC-previewWarningBox").hidden,
          "the scary warning is visible"
        );
        // Select the 'Select Window' item again, the preview should be hidden.
        menulist.getItemAtIndex(0).doCommand();
        ok(
          document.getElementById("webRTC-preview").hidden,
          "the preview area is hidden"
        );

        // Select the first window again so that we can have a stream.
        menulist.getItemAtIndex(scaryWindowIndex).doCommand();
      }

      let sharingNonScaryWindow = typeof nonScaryWindowIndex == "number";

      // If we have a non-scary window, select it and verify the warning isn't displayed.
      // A non-scary window may not always exist on test machines.
      if (sharingNonScaryWindow) {
        menulist.getItemAtIndex(nonScaryWindowIndex).doCommand();
        ok(
          document.getElementById("webRTC-all-windows-shared").hidden,
          "the 'all windows will be shared' warning should still be hidden"
        );
        await TestUtils.waitForCondition(
          () => !document.getElementById("webRTC-preview").hidden,
          "preview unhide",
          100,
          100
        );
        ok(
          !document.getElementById("webRTC-preview").hidden,
          "the preview area is visible"
        );
        ok(
          document.getElementById("webRTC-previewWarningBox").hidden,
          "the scary warning is hidden"
        );
      } else {
        info("no non-scary window available on this test machine");
      }

      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(),
        { screen: "Window" },
        "expected screen to be shared"
      );

      await indicator;
      if (sharingNonScaryWindow) {
        await checkSharingUI({ screen: "Window" });
      } else {
        await checkSharingUI({ screen: "Window", browserwindow: true });
      }

      verifyTabSharingPopup(["window"]);

      await closeStream();
    },
  },

  {
    desc: "getUserMedia audio + window/screen",
    run: async function checkAudioVideo() {
      if (AppConstants.platform == "macosx") {
        todo(
          false,
          "Bug 1323481 - On Mac on treeherder, but not locally, requesting microphone + screen never makes the permission prompt appear, and so causes the test to timeout"
        );
        return;
      }

      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true, true, null, "window");
      await promise;
      await observerPromise;

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

      let menulist = document.getElementById("webRTC-selectWindow-menulist");
      let count = menulist.itemCount;
      ok(
        count >= 4,
        "There should be the 'Select Window or Screen' item, a separator and at least one window and one screen"
      );

      // Select a screen, a preview with a scary warning should appear.
      menulist.getItemAtIndex(count - 1).doCommand();
      ok(
        !document.getElementById("webRTC-all-windows-shared").hidden,
        "the 'all windows will be shared' warning should now be visible"
      );
      await TestUtils.waitForCondition(
        () => !document.getElementById("webRTC-preview").hidden,
        "preview unhide",
        100,
        100
      );
      ok(
        !document.getElementById("webRTC-preview").hidden,
        "the preview area is visible"
      );
      ok(
        !document.getElementById("webRTC-previewWarningBox").hidden,
        "the scary warning is visible"
      );

      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, screen: "Screen" },
        "expected screen and microphone to be shared"
      );

      await indicator;
      await checkSharingUI({ audio: true, screen: "Screen" });

      verifyTabSharingPopup(["microphone and screen"]);

      await closeStream();
    },
  },

  {
    desc: 'getUserMedia screen, user clicks "Don\'t Allow"',
    run: async function checkDontShare() {
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;
      checkDeviceSelectors(["screen"]);

      let observerPromise1 = expectObserverCalled("getUserMedia:response:deny");
      let observerPromise2 = expectObserverCalled("recording-window-ended");
      await promiseMessage(permissionError, () => {
        activateSecondaryAction(kActionDeny);
      });

      await observerPromise1;
      await observerPromise2;
      await checkNotSharing();
      SitePermissions.removeFromPrincipal(
        null,
        "screen",
        gBrowser.selectedBrowser
      );
      SitePermissions.removeFromPrincipal(
        null,
        "camera",
        gBrowser.selectedBrowser
      );
    },
  },

  {
    desc: "getUserMedia audio + window/screen: stop sharing",
    run: async function checkStopSharing() {
      if (AppConstants.platform == "macosx") {
        todo(
          false,
          "Bug 1323481 - On Mac on treeherder, but not locally, requesting microphone + screen never makes the permission prompt appear, and so causes the test to timeout"
        );
        return;
      }

      async function share(deviceTypes) {
        let promise = promisePopupNotificationShown("webRTC-shareDevices");
        let observerPromise = expectObserverCalled("getUserMedia:request");
        await promiseRequestDevice(
          /* audio */ deviceTypes.includes("microphone"),
          /* video */ deviceTypes.some(t => t == "screen" || t == "camera"),
          null,
          deviceTypes.includes("screen") && "window"
        );
        await promise;
        await observerPromise;
        checkDeviceSelectors(deviceTypes);
        if (screen) {
          let menulist = document.getElementById(
            "webRTC-selectWindow-menulist"
          );
          menulist.getItemAtIndex(menulist.itemCount - 1).doCommand();
        }
        let observerPromise1 = expectObserverCalled(
          "getUserMedia:response:allow"
        );
        let observerPromise2 = expectObserverCalled("recording-device-events");
        await promiseMessage("ok", () => {
          PopupNotifications.panel.firstElementChild.button.click();
        });
        await observerPromise1;
        await observerPromise2;
      }

      async function check(expected = {}, expectedSharingLabel) {
        let shared = Object.keys(expected).join(" and ");
        if (shared) {
          Assert.deepEqual(
            await getMediaCaptureState(),
            expected,
            "expected " + shared + " to be shared"
          );
          await checkSharingUI(expected);
          verifyTabSharingPopup([expectedSharingLabel]);
        } else {
          await checkNotSharing();
          verifyTabSharingPopup([""]);
        }
      }

      info("Share screen and microphone");
      let indicator = promiseIndicatorWindow();
      await share(["microphone", "screen"]);
      await indicator;
      await check({ audio: true, screen: "Screen" }, "microphone and screen");

      info("Share camera");
      await share(["camera"]);
      await check(
        { video: true, audio: true, screen: "Screen" },
        "microphone, screen, and camera"
      );

      info("Stop the screen share, mic+cam should continue");
      await stopSharing("screen", true);
      await check({ video: true, audio: true }, "microphone and camera");

      info("Stop the camera, everything should stop.");
      await stopSharing("camera");

      info("Now, share only the screen...");
      indicator = promiseIndicatorWindow();
      await share(["screen"]);
      await indicator;
      await check({ screen: "Screen" }, "screen");

      info("... and add camera and microphone in a second request.");
      await share(["microphone", "camera"]);
      await check(
        { video: true, audio: true, screen: "Screen" },
        "screen, microphone, and camera"
      );

      info("Stop the camera, this should stop everything.");
      await stopSharing("camera");
    },
  },

  {
    desc: "getUserMedia window/screen: reloading the page removes all gUM UI",
    run: async function checkReloading() {
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;
      checkDeviceSelectors(["screen"]);
      let menulist = document.getElementById("webRTC-selectWindow-menulist");
      menulist.getItemAtIndex(menulist.itemCount - 1).doCommand();

      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(),
        { screen: "Screen" },
        "expected screen to be shared"
      );

      await indicator;
      await checkSharingUI({ screen: "Screen" });
      verifyTabSharingPopup(["screen"]);

      await reloadAndAssertClosedStreams();
    },
  },

  {
    desc: "test showControlCenter from screen icon",
    run: async function checkShowControlCenter() {
      if (!USING_LEGACY_INDICATOR) {
        info(
          "Skipping since this test doesn't apply to the new global sharing " +
            "indicator."
        );
        return;
      }
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;
      checkDeviceSelectors(["screen"]);
      let menulist = document.getElementById("webRTC-selectWindow-menulist");
      menulist.getItemAtIndex(menulist.itemCount - 1).doCommand();

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

      ok(permissionPopupHidden(), "control center should be hidden");
      if (IS_MAC) {
        let activeStreams = webrtcUI.getActiveStreams(false, false, true);
        webrtcUI.showSharingDoorhanger(activeStreams[0]);
      } else {
        let win = Services.wm.getMostRecentWindow(
          "Browser:WebRTCGlobalIndicator"
        );
        let elt = win.document.getElementById("screenShareButton");
        EventUtils.synthesizeMouseAtCenter(elt, {}, win);
      }
      await TestUtils.waitForCondition(
        () => !permissionPopupHidden(),
        "wait for control center to open"
      );
      ok(!permissionPopupHidden(), "control center should be open");

      gPermissionPanel._permissionPopup.hidePopup();

      await closeStream();
    },
  },

  {
    desc: "Only persistent block is possible for screen sharing",
    run: async function checkPersistentPermissions() {
      // This test doesn't apply when the notification silencing
      // feature is enabled, since the "Remember this decision"
      // checkbox doesn't exist.
      if (ALLOW_SILENCING_NOTIFICATIONS) {
        return;
      }

      let browser = gBrowser.selectedBrowser;
      let devicePerms = SitePermissions.getForPrincipal(
        browser.contentPrincipal,
        "screen",
        browser
      );
      is(
        devicePerms.state,
        SitePermissions.UNKNOWN,
        "starting without screen persistent permissions"
      );

      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;
      checkDeviceSelectors(["screen"]);
      document
        .getElementById("webRTC-selectWindow-menulist")
        .getItemAtIndex(2)
        .doCommand();

      // Ensure that checking the 'Remember this decision' checkbox disables
      // 'Allow'.
      let notification = PopupNotifications.panel.firstElementChild;
      ok(
        notification.hasAttribute("warninghidden"),
        "warning message is hidden"
      );
      let checkbox = notification.checkbox;
      ok(!!checkbox, "checkbox is present");
      ok(!checkbox.checked, "checkbox is not checked");
      checkbox.click();
      ok(checkbox.checked, "checkbox now checked");
      ok(notification.button.disabled, "Allow button is disabled");
      ok(
        !notification.hasAttribute("warninghidden"),
        "warning message is shown"
      );

      // Click "Don't Allow" to save a persistent block permission.
      let observerPromise1 = expectObserverCalled("getUserMedia:response:deny");
      let observerPromise2 = expectObserverCalled("recording-window-ended");
      await promiseMessage(permissionError, () => {
        activateSecondaryAction(kActionDeny);
      });
      await observerPromise1;
      await observerPromise2;
      await checkNotSharing();

      let permission = SitePermissions.getForPrincipal(
        browser.contentPrincipal,
        "screen",
        browser
      );
      is(permission.state, SitePermissions.BLOCK, "screen sharing is blocked");
      is(
        permission.scope,
        SitePermissions.SCOPE_PERSISTENT,
        "screen sharing is persistently blocked"
      );

      // Request screensharing again, expect an immediate failure.
      await Promise.all([
        expectObserverCalled("getUserMedia:request"),
        expectObserverCalled("getUserMedia:response:deny"),
        expectObserverCalled("recording-window-ended"),
        promiseMessage(permissionError),
        promiseRequestDevice(false, true, null, "screen"),
      ]);

      // Now set the permission to allow and expect a prompt.
      SitePermissions.setForPrincipal(
        browser.contentPrincipal,
        "screen",
        SitePermissions.ALLOW
      );

      // Request devices and expect a prompt despite the saved 'Allow' permission.
      observerPromise = expectObserverCalled("getUserMedia:request");
      promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;

      // The 'remember' checkbox shouldn't be checked anymore.
      notification = PopupNotifications.panel.firstElementChild;
      ok(
        notification.hasAttribute("warninghidden"),
        "warning message is hidden"
      );
      checkbox = notification.checkbox;
      ok(!!checkbox, "checkbox is present");
      ok(!checkbox.checked, "checkbox is not checked");

      // Deny the request to cleanup...
      observerPromise1 = expectObserverCalled("getUserMedia:response:deny");
      observerPromise2 = expectObserverCalled("recording-window-ended");
      await promiseMessage(permissionError, () => {
        activateSecondaryAction(kActionDeny);
      });
      await observerPromise1;
      await observerPromise2;
      SitePermissions.removeFromPrincipal(
        browser.contentPrincipal,
        "screen",
        browser
      );
    },
  },

  {
    desc: "Switching between menu options maintains correct main action state while window sharing",
    skipObserverVerification: true,
    run: async function checkDoorhangerState() {
      await enableObserverVerification();

      let win = await BrowserTestUtils.openNewBrowserWindow();
      await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:newtab");
      BrowserWindowTracker.orderedWindows[1].focus();

      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "window");
      await promise;
      await observerPromise;

      let menulist = document.getElementById("webRTC-selectWindow-menulist");
      let notification = PopupNotifications.panel.firstElementChild;
      let checkbox = notification.checkbox;

      menulist.getItemAtIndex(2).doCommand();
      checkbox.click();
      ok(checkbox.checked, "checkbox now checked");

      if (ALLOW_SILENCING_NOTIFICATIONS) {
        // When the notification silencing feature is enabled, the checkbox
        // controls that feature, and its state should not disable the
        // "Allow" button.
        ok(!notification.button.disabled, "Allow button is not disabled");
      } else {
        ok(notification.button.disabled, "Allow button is disabled");
        ok(
          !notification.hasAttribute("warninghidden"),
          "warning message is shown"
        );
      }

      menulist.getItemAtIndex(3).doCommand();
      ok(checkbox.checked, "checkbox still checked");
      if (ALLOW_SILENCING_NOTIFICATIONS) {
        // When the notification silencing feature is enabled, the checkbox
        // controls that feature, and its state should not disable the
        // "Allow" button.
        ok(!notification.button.disabled, "Allow button remains not disabled");
      } else {
        ok(notification.button.disabled, "Allow button remains disabled");
        ok(
          !notification.hasAttribute("warninghidden"),
          "warning message is still shown"
        );
      }

      await disableObserverVerification();

      observerPromise = expectObserverCalled("recording-window-ended");

      gBrowser.removeCurrentTab();
      win.close();

      await observerPromise;

      await openNewTestTab();
    },
  },
  {
    desc: "Switching between tabs does not bleed state into other prompts",
    skipObserverVerification: true,
    run: async function checkSwitchingTabs() {
      // Open a new window in the background to have a choice in the menulist.
      let win = await BrowserTestUtils.openNewBrowserWindow();
      await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:newtab");
      await enableObserverVerification();
      BrowserWindowTracker.orderedWindows[1].focus();

      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "window");
      await promise;
      await observerPromise;

      let notification = PopupNotifications.panel.firstElementChild;
      ok(notification.button.disabled, "Allow button is disabled");
      await disableObserverVerification();

      await openNewTestTab("get_user_media_in_xorigin_frame.html");
      await enableObserverVerification();

      observerPromise = expectObserverCalled("getUserMedia:request");
      promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true, true, "frame1");
      await promise;
      await observerPromise;

      notification = PopupNotifications.panel.firstElementChild;
      ok(!notification.button.disabled, "Allow button is not disabled");

      await disableObserverVerification();

      gBrowser.removeCurrentTab();
      gBrowser.removeCurrentTab();
      win.close();

      await openNewTestTab();
    },
  },
];

add_task(async function test() {
  await runTests(gTests);
});