delegate-request.https.sub.html (3358B)
1 <!DOCTYPE html> 2 <title>Payment 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 PaymentRequest.show() call 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="payment" width="300px" height="50px" 16 src="https://{{hosts[alt][www]}}:{{ports[https][0]}}/payment-request/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-payment-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 }, "Payment-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 }, "Payment-request from a subframe fails without delegation when the top frame has user activation"); 59 60 promise_test(async () => { 61 let result_promise = getMessageData("result", frames[0]); 62 await test_driver.bless(); 63 frames[0].postMessage(request, {targetOrigin: target_origin, 64 delegate: "payment"}); 65 let data = await result_promise; 66 67 assert_equals(data.result, "success"); 68 }, "Payment-request from a subframe succeeds with delegation when the top frame has user activation"); 69 70 // This test must follow the successful test case above so that the user activation state there 71 // gets consumed. 72 promise_test(async () => { 73 let result_promise = getMessageData("result", frames[0]); 74 frames[0].postMessage(request, {targetOrigin: target_origin, 75 delegate: "payment"}); 76 let data = await result_promise; 77 78 assert_equals(data.result, "failure"); 79 }, "Payment-request from a subframe fails with delegation when the top frame has no user activation"); 80 81 </script>