2 <!-- Copyright © 2017 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
3 <!-- Copyright (C) 2017 Apple Inc. All rights reserved. -->
4 <!-- FIXME: Upstream this test to web-platform-tests/payment-request/. -->
6 <title>Tests for PaymentRequest.canMakePayment() method</title>
7 <link rel="help" href="https://w3c.github.io/browser-payment-api/#show-method">
8 <script src="/js-test-resources/ui-helper.js"></script>
9 <script src="/resources/payment-request.js"></script>
10 <script src="/resources/testharness.js"></script>
11 <script src="/resources/testharnessreport.js"></script>
13 const applePay = Object.freeze({
14 supportedMethods: "https://apple.com/apple-pay",
17 merchantIdentifier: "",
18 merchantCapabilities: ["supports3DS"],
19 supportedNetworks: ["visa", "masterCard"],
23 const defaultMethods = Object.freeze([applePay]);
24 const defaultDetails = Object.freeze({
34 promise_test(async t => {
35 const request = new PaymentRequest(defaultMethods, defaultDetails);
38 await request.canMakePayment(),
39 `canMakePaymentPromise should be true`
42 await request.canMakePayment(),
43 `canMakePaymentPromise should be true`
49 "if it throws, then it must be a NotAllowedError."
52 }, `If request.[[state]] is "created", then return a promise that resolves to true for known method.`);
54 user_activation_test(async t => {
55 const request = new PaymentRequest(defaultMethods, defaultDetails);
56 const acceptPromise = request.show(); // Sets state to "interactive"
57 const canMakePaymentPromise = request.canMakePayment();
59 const result = await canMakePaymentPromise;
62 `canMakePaymentPromise should have thrown InvalidStateError`
65 await promise_rejects(t, "InvalidStateError", canMakePaymentPromise);
67 await request.abort();
68 await promise_rejects(t, "AbortError", acceptPromise);
70 // The state should be "closed"
71 await promise_rejects(t, "InvalidStateError", request.canMakePayment());
72 }, `If request.[[state]] is "interactive", then return a promise rejected with an "InvalidStateError" DOMException.`);
74 user_activation_test(async t => {
75 const request = new PaymentRequest(defaultMethods, defaultDetails);
76 const acceptPromise = request.show(); // The state is now "interactive"
77 acceptPromise.catch(() => {}); // no-op, just to silence unhandled rejection in devtools.
78 await request.abort(); // The state is now "closed"
79 await promise_rejects(t, "InvalidStateError", request.canMakePayment());
81 const result = await request.canMakePayment();
84 `should have thrown InvalidStateError, but instead returned "${result}"`
90 "must be an InvalidStateError."
93 }, `If request.[[state]] is "closed", then return a promise rejected with an "InvalidStateError" DOMException.`);
95 promise_test(async t => {
96 const request = new PaymentRequest(defaultMethods, defaultDetails);
97 assert_true(await request.canMakePayment(), "basic-card should be supported");
98 }, `If payment method identifier and serialized parts are supported, resolve promise with true.`);
100 promise_test(async t => {
101 const unsupportedMethods = [
102 "this-is-not-supported",
103 "https://not.supported",
105 "n6jzof05mk2g4lhxr-u-q-w1-c-i-pa-ty-bdvs9-ho-ae7-p-md8-s-wq3-h-qd-e-q-sa",
112 "i488jh6-g18-fck-yb-v7-i",
116 "https://wpt.fyi/payment",
117 "https://wpt.fyi/payment-request",
118 "https://wpt.fyi/payment-request?",
119 "https://wpt.fyi/payment-request?this=is",
120 "https://wpt.fyi/payment-request?this=is&totally",
121 "https://wpt.fyi:443/payment-request?this=is&totally",
122 "https://wpt.fyi:443/payment-request?this=is&totally#fine",
123 "https://:@wpt.fyi:443/payment-request?this=is&totally#👍",
125 "https://xn--c1yn36f",
128 for (const method of unsupportedMethods) {
130 const request = new PaymentRequest(
131 [{ supportedMethods: method }],
135 await request.canMakePayment(),
136 `method "${method}" must not be supported`
141 `Unexpected exception testing method ${method}, expected false. See error console.`
145 }, `If payment method identifier is unknown, resolve promise with false.`);
147 promise_test(async t => {
148 // This test might never actually hit its assertion, but that's allowed.
149 const request = new PaymentRequest(defaultMethods, defaultDetails);
150 for (let i = 0; i < 1000; i++) {
152 await request.canMakePayment();
157 "if it throws, then it must be a NotAllowedError."
162 for (let i = 0; i < 1000; i++) {
164 await new PaymentRequest(defaultMethods, defaultDetails).canMakePayment();
169 "if it throws, then it must be a NotAllowedError."
174 }, `Optionally, at the user agent's discretion, return a promise rejected with a "NotAllowedError" DOMException.`);