[MediaStream] 'devicechange' event should not fire in frames that can't access captur...
[WebKit-https.git] / LayoutTests / http / tests / media / media-stream / device-change-event-in-iframe.html
1 <!doctype html>
2 <html>
3 <script src="../../../../resources/js-test-pre.js"></script>
4 <body onload="start()">
5     <iframe id="none"" src="http://localhost:8000/media/media-stream/resources/device-change-iframe.html?1"></iframe>
6     <iframe id="camera" allow="camera" src="http://localhost:8000/media/media-stream/resources/device-change-iframe.html?2"></iframe>
7     <iframe id="microphone" allow="camera" src="http://localhost:8000/media/media-stream/resources/device-change-iframe.html?2"></iframe>
8     <iframe id="same-origin" src="http://127.0.0.1:8000/media/media-stream/resources/device-change-iframe.html?3"></iframe>
9
10     <script>
11
12         if (window.testRunner) {
13             testRunner.dumpAsText();
14             testRunner.waitUntilDone();
15             testRunner.setUserMediaPermission(true);
16         }
17
18         let count = new Set();
19         let success = true;
20         let results = [];
21         function countEvent(id)
22         {
23             results.push(`&LT;iframe id=${id}> got a 'devicechange' event`);
24             if (id == "none") {
25                 result.innerHTML += `FAIL: &LT;iframe id=${id}> got 'devicechange' event<br>`;
26                 success = false;
27             } else
28                 count.add(id);
29
30             if (count.size == 4) {
31                 setTimeout(() => {
32                     result.innerHTML = results.sort().join('<br>');
33                     if (success)
34                         result.innerHTML += "<br><br>PASS: 'devicechange' only fired on frames with access to devices<br>";
35                     if (window.testRunner) {
36                         testRunner.notifyDone();
37                         testRunner.resetMockMediaDevices();
38                     }
39                 }, 500);
40             }
41         }
42         
43         async function start()
44         {
45             await window.navigator.mediaDevices.getUserMedia({audio:true});
46             if (window.testRunner)
47                 testRunner.addMockMicrophoneDevice("id4", "microphone 3");
48         }
49
50         window.onmessage = (event) => { countEvent(event.data); };
51         navigator.mediaDevices.ondevicechange = (event) => { countEvent('main'); };
52
53         Array.from(document.getElementsByTagName('iframe')).forEach(element => {
54             element.onload = (evt) => { 
55                 evt.target.contentWindow.postMessage(evt.target.id, '*'); 
56             }
57         });
58
59     </script>
60
61     <div id="result"></div>
62 </body>
63 </html>