delegate-request.https.sub.html (3240B)
1 <!DOCTYPE html> 2 <title>Display-capture request delegation test</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/resources/testdriver.js"></script> 6 <script src="/resources/testdriver-actions.js"></script> 7 <script src="/resources/testdriver-vendor.js"></script> 8 9 <div> 10 Verifies that getDisplayMedia() calls from a cross-origin subframe without user activation 11 works if and only if the top frame has user activation and it delegates the capability to the 12 subframe. 13 </div> 14 15 <iframe allow="display-capture" width="300px" height="50px" 16 src="https://{{hosts[alt][www]}}:{{ports[https][0]}}/screen-capture/resources/delegate-request-subframe.sub.html"> 17 </iframe> 18 19 <script> 20 // Returns a |Promise| that gets resolved with |event.data| when |window| 21 // receives from |source| a "message" event whose |event.data.type| matches the string 22 // |message_data_type|. 23 function getMessageData(message_data_type, source) { 24 return new Promise(resolve => { 25 function waitAndRemove(e) { 26 if (e.source != source || !e.data || e.data.type != message_data_type) 27 return; 28 window.removeEventListener("message", waitAndRemove); 29 resolve(e.data); 30 } 31 window.addEventListener("message", waitAndRemove); 32 }); 33 } 34 35 promise_setup(async () => { 36 // Make sure the iframe has loaded. 37 await getMessageData("subframe-loaded", frames[0]); 38 }); 39 40 const target_origin = "https://{{hosts[alt][www]}}:{{ports[https][0]}}"; 41 const request = {"type": "make-display-capture-request"}; 42 43 promise_test(async () => { 44 let result_promise = getMessageData("result", frames[0]); 45 frames[0].postMessage(request, {targetOrigin: target_origin}); 46 let data = await result_promise; 47 48 assert_equals(data.result, "failure"); 49 }, "Display-capture request from a subframe fails without delegation when the top frame has no user activation"); 50 51 promise_test(async () => { 52 let result_promise = getMessageData("result", frames[0]); 53 await test_driver.bless(); 54 frames[0].postMessage(request, {targetOrigin: target_origin}); 55 let data = await result_promise; 56 57 assert_equals(data.result, "failure"); 58 }, "Display-capture request from a subframe fails without delegation when the top frame has user activation"); 59 60 promise_test(async () => { 61 { 62 let result_promise = getMessageData("result", frames[0]); 63 await test_driver.bless(); 64 frames[0].postMessage(request, {targetOrigin: target_origin, 65 delegate: "display-capture"}); 66 let data = await result_promise; 67 68 assert_equals(data.result, "success"); 69 } 70 { 71 // Check display-capture request can be consumed only once. 72 let result_promise = getMessageData("result", frames[0]); 73 frames[0].postMessage(request, {targetOrigin: target_origin}); 74 let data = await result_promise; 75 76 assert_equals(data.result, "failure"); 77 } 78 }, "Display-capture request from a subframe succeeds with delegation when the top frame has user activation"); 79 80 </script>