|
Line 0
a/LayoutTests/http/tests/paymentrequest/rejects_if_not_active.https.html_sec1
|
|
|
1 |
<!DOCTYPE html> |
| 2 |
<meta charset=utf-8> |
| 3 |
<link rel="help" href="https://w3c.github.io/payment-request/#show()-method"> |
| 4 |
<title>PaymentRequest show() rejects if doc is not fully active</title> |
| 5 |
<script src="/js-test-resources/ui-helper.js"></script> |
| 6 |
<script src="/resources/payment-request.js"></script> |
| 7 |
<script src="/resources/testharness.js"></script> |
| 8 |
<script src="/resources/testharnessreport.js"></script> |
| 9 |
<link rel="help" href="https://w3c.github.io/payment-request/#dom-paymentrequest-show()"> |
| 10 |
<body> |
| 11 |
<script> |
| 12 |
const basicCardMethod = Object.freeze({ |
| 13 |
supportedMethods: "basic-card", |
| 14 |
}); |
| 15 |
const applePayMethod = Object.freeze({ |
| 16 |
supportedMethods: "https://apple.com/apple-pay", |
| 17 |
data: { |
| 18 |
version: 2, |
| 19 |
merchantIdentifier: '', |
| 20 |
merchantCapabilities: ['supports3DS'], |
| 21 |
supportedNetworks: ['visa', 'masterCard'], |
| 22 |
countryCode: 'US', |
| 23 |
}, |
| 24 |
}); |
| 25 |
const validMethods = Object.freeze([basicCardMethod, applePayMethod]); |
| 26 |
const validAmount = Object.freeze({ |
| 27 |
currency: "USD", |
| 28 |
value: "5.00", |
| 29 |
}); |
| 30 |
const validTotal = Object.freeze({ |
| 31 |
label: "Total due", |
| 32 |
amount: validAmount, |
| 33 |
}); |
| 34 |
const validDetails = Object.freeze({ |
| 35 |
total: validTotal, |
| 36 |
}); |
| 37 |
|
| 38 |
function getLoadedPaymentRequest(iframe, url) { |
| 39 |
return new Promise(resolve => { |
| 40 |
iframe.addEventListener( |
| 41 |
"load", |
| 42 |
() => { |
| 43 |
const { PaymentRequest } = iframe.contentWindow; |
| 44 |
const request = new PaymentRequest(validMethods, validDetails); |
| 45 |
resolve(request); |
| 46 |
}, |
| 47 |
{ once: true } |
| 48 |
); |
| 49 |
iframe.src = url; |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
promise_test(async t => { |
| 54 |
// Check that PaymentRequests can be constructed. |
| 55 |
new PaymentRequest(validMethods, validDetails); |
| 56 |
const iframe = document.createElement("iframe"); |
| 57 |
iframe.allowPaymentRequest = true; |
| 58 |
document.body.appendChild(iframe); |
| 59 |
|
| 60 |
// We first got to page1.html, grab a PaymentRequest instance. |
| 61 |
const request1 = await getLoadedPaymentRequest( |
| 62 |
iframe, |
| 63 |
"resources/page1.html" |
| 64 |
); |
| 65 |
|
| 66 |
// We navigate the iframe again, putting request1's document into an inactive state. |
| 67 |
const request2 = await getLoadedPaymentRequest( |
| 68 |
iframe, |
| 69 |
"resources/page2.html" |
| 70 |
); |
| 71 |
|
| 72 |
// Now, request1's relevant global object's document is no longer active. |
| 73 |
// So, call .show(), and make sure it rejects appropriately. |
| 74 |
await promise_rejects( |
| 75 |
t, |
| 76 |
"AbortError", |
| 77 |
request1.show(), |
| 78 |
"Inactive document, so must throw AbortError" |
| 79 |
); |
| 80 |
|
| 81 |
// request2 has an active document tho, so confirm it's working as expected: |
| 82 |
await activateThen(async () => { |
| 83 |
request2.show().catch((error) => {}); |
| 84 |
await request2.abort(); |
| 85 |
}); |
| 86 |
|
| 87 |
await activateThen(async () => { |
| 88 |
await promise_rejects( |
| 89 |
t, |
| 90 |
"InvalidStateError", |
| 91 |
request2.show(), |
| 92 |
"Abort already called, so InvalidStateError" |
| 93 |
); |
| 94 |
}); |
| 95 |
|
| 96 |
// We are done, so clean up. |
| 97 |
iframe.remove(); |
| 98 |
}, "PaymentRequest.show() aborts if the document is not active"); |
| 99 |
|
| 100 |
promise_test(async t => { |
| 101 |
// check that PaymentRequests can be constructed (smoke test). |
| 102 |
new PaymentRequest(validMethods, validDetails); |
| 103 |
|
| 104 |
// We nest two iframes and wait for them to load. |
| 105 |
const outerIframe = document.createElement("iframe"); |
| 106 |
outerIframe.allowPaymentRequest = true; |
| 107 |
document.body.appendChild(outerIframe); |
| 108 |
// Load the outer iframe (we don't care about the awaited request) |
| 109 |
await getLoadedPaymentRequest( |
| 110 |
outerIframe, |
| 111 |
"resources/page1.html" |
| 112 |
); |
| 113 |
|
| 114 |
// Now we create the inner iframe |
| 115 |
const innerIframe = outerIframe.contentDocument.createElement("iframe"); |
| 116 |
innerIframe.allowPaymentRequest = true; |
| 117 |
|
| 118 |
// nest them |
| 119 |
outerIframe.contentDocument.body.appendChild(innerIframe); |
| 120 |
|
| 121 |
// load innerIframe, and get the PaymentRequest instance |
| 122 |
const request = await getLoadedPaymentRequest( |
| 123 |
innerIframe, |
| 124 |
"resources/page2.html" |
| 125 |
); |
| 126 |
|
| 127 |
// Navigate the outer iframe to a new location. |
| 128 |
// Wait for the load event to fire. |
| 129 |
await new Promise(resolve => { |
| 130 |
outerIframe.addEventListener("load", resolve); |
| 131 |
outerIframe.src = "resources/page2.html"; |
| 132 |
}); |
| 133 |
// Now, request's relevant global object's document is still active |
| 134 |
// (it is the active document of the inner iframe), but is not fully active |
| 135 |
// (since the parent of the inner iframe is itself no longer active). |
| 136 |
// So, call request.show() and make sure it rejects appropriately. |
| 137 |
await promise_rejects( |
| 138 |
t, |
| 139 |
"AbortError", |
| 140 |
request.show(), |
| 141 |
"Active, but not fully active, so must throw AbortError" |
| 142 |
); |
| 143 |
|
| 144 |
// We are done, so clean up. |
| 145 |
outerIframe.remove(); |
| 146 |
}, "PaymentRequest.show() aborts if the document is active, but not fully active"); |
| 147 |
|
| 148 |
promise_test(async t => { |
| 149 |
// Check that PaymentRequests can be constructed. |
| 150 |
new PaymentRequest(validMethods, validDetails); |
| 151 |
const iframe = document.createElement("iframe"); |
| 152 |
iframe.allowPaymentRequest = true; |
| 153 |
document.body.appendChild(iframe); |
| 154 |
// Make a request in the iframe. |
| 155 |
const request = await getLoadedPaymentRequest( |
| 156 |
iframe, |
| 157 |
"resources/page1.html" |
| 158 |
); |
| 159 |
// Present the payment sheet. |
| 160 |
await activateThen(async () => { |
| 161 |
const showPromise = request.show(); |
| 162 |
// Navigate the iframe to a new location. Wait for the load event to fire. |
| 163 |
await new Promise(resolve => { |
| 164 |
iframe.addEventListener("load", resolve); |
| 165 |
iframe.src = "resources/page2.html"; |
| 166 |
}); |
| 167 |
await promise_rejects( |
| 168 |
t, |
| 169 |
"AbortError", |
| 170 |
showPromise, |
| 171 |
"The iframe was navigated away, so showPromise must reject with AbortError" |
| 172 |
); |
| 173 |
}); |
| 174 |
|
| 175 |
// We are done, so clean up. |
| 176 |
iframe.remove(); |
| 177 |
}, "If a payment request is showing, but its document is navigated away (so no longer fully active), the payment request aborts."); |
| 178 |
</script> |