[MediaStream] Add "has capture device" bit to media state flags
[WebKit-https.git] / LayoutTests / fast / mediastream / MediaStream-page-muted.html
1 <!DOCTYPE HTML>
2 <html>
3     <head>
4         <script src="../../resources/js-test-pre.js"></script>
5         <script>
6             let mediaStream;
7             let eventCount = 0;
8             let muteChangedEvent;
9
10             function nextStep()
11             {
12                 if (muteChangedEvent.type == "unmute") {
13                     finishJSTest();
14                     return;
15                 }
16
17                 debug("<br>*** Unmuting capture devices");
18                 eventCount = 0;
19                 internals.setPageMuted("");
20             }
21
22             function muteChanged(ev)
23             {
24                 muteChangedEvent = ev;
25                 debug(`EVENT: ${muteChangedEvent.type}`);
26                 shouldBe('muteChangedEvent.target.muted', (muteChangedEvent.type == "mute").toString());
27                 shouldBe("window.internals.pageMediaState().includes('HasMediaCaptureDevice')", "true");
28
29                 if (++eventCount == 2) {
30                     debug("");
31                     let shouldBeActive = muteChangedEvent.type == "mute" ? "false" : "true";
32                     shouldBecomeEqual("window.internals.pageMediaState().includes('HasActiveMediaCaptureDevice')", shouldBeActive, nextStep);
33                 }
34             }
35             
36             function muteCaptureDevices()
37             {
38                 debug("<br>*** Muting capture devices");
39                 internals.setPageMuted("capturedevices");
40             }
41             
42             function testWhenEnabled()
43             {
44                 navigator.mediaDevices
45                     .getUserMedia({audio:{}, video:{}})
46                     .then(stream => {
47                         mediaStream = stream;
48                         shouldBeType("mediaStream", "Object");
49                         shouldBe("mediaStream.getTracks().length", "2");
50
51                         var tracks = mediaStream.getTracks();
52                         for (var i = 0; i < tracks.length; i++) {
53                             tracks[i].onmute = muteChanged;
54                             tracks[i].onunmute = muteChanged;
55                         }
56
57                         shouldBecomeEqual("window.internals.pageMediaState().includes('HasActiveMediaCaptureDevice')", "true", muteCaptureDevices);
58                     })
59                     .catch((err) => {
60                         testFailed(`mediaDevices.getUserMedia() failed with ${err.name}`);
61                         finishJSTest();
62                     });
63             }
64
65             function start()
66             {
67                 debug(`<br>*** Mock capture devices should be enabled by default`);
68                 if (window.testRunner)
69                     testRunner.setUserMediaPermission(true);
70
71                 testWhenEnabled();
72             }
73
74         </script>
75     </head>
76
77     <body onload="start()">
78         <p id="description"></p>
79         <div id="console"></div>
80         <script>
81             description("Test enabling/disabling mock media capture devices");
82             window.jsTestIsAsync = true;
83
84             window.successfullyParsed = true;
85         </script>
86         <script src="../../resources/js-test-post.js"></script>
87     </body>
88 </html>