[Payment Request] Implement PaymentRequest.canMakePayment()
[WebKit-https.git] / LayoutTests / http / tests / paymentrequest / payment-request-canmakepayment-method.https.html
1 <!DOCTYPE html>
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/. -->
5 <meta charset="utf-8">
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="/resources/testharness.js"></script>
9 <script src="/resources/testharnessreport.js"></script>
10 <script>
11     const applePay = Object.freeze({
12     supportedMethods: "https://apple.com/apple-pay",
13     data: {
14         version: 2,
15         merchantIdentifier: "",
16         merchantCapabilities: ["supports3DS"],
17         supportedNetworks: ["visa", "masterCard"],
18         countryCode: "US",
19         currencyCode: "USD",
20     }
21 });
22 const defaultMethods = Object.freeze([applePay]);
23 const defaultDetails = Object.freeze({
24   total: {
25     label: "Total",
26     amount: {
27       currency: "USD",
28       value: "1.00",
29     },
30   },
31 });
32
33 promise_test(async t => {
34   const request = new PaymentRequest(defaultMethods, defaultDetails);
35   try {
36     assert_true(
37       await request.canMakePayment(),
38       `canMakePaymentPromise should be true`
39     );
40     assert_true(
41       await request.canMakePayment(),
42       `canMakePaymentPromise should be true`
43     );
44   } catch (err) {
45     assert_equals(
46       err.name,
47       "NotAllowedError",
48       "if it throws, then it must be a NotAllowedError."
49     );
50   }
51 }, `If request.[[state]] is "created", then return a promise that resolves to true for known method.`);
52
53 promise_test(async t => {
54   const request = new PaymentRequest(defaultMethods, defaultDetails);
55   const acceptPromise = request.show(); // Sets state to "interactive"
56   const canMakePaymentPromise = request.canMakePayment();
57   try {
58     const result = await canMakePaymentPromise;
59     assert_true(
60       false,
61       `canMakePaymentPromise should have thrown InvalidStateError`
62     );
63   } catch (err) {
64     await promise_rejects(t, "InvalidStateError", canMakePaymentPromise);
65   } finally {
66     await request.abort();
67     await promise_rejects(t, "AbortError", acceptPromise);
68   }
69   // The state should be "closed"
70   await promise_rejects(t, "InvalidStateError", request.canMakePayment());
71 }, `If request.[[state]] is "interactive", then return a promise rejected with an "InvalidStateError" DOMException.`);
72
73 promise_test(async t => {
74   const request = new PaymentRequest(defaultMethods, defaultDetails);
75   const acceptPromise = request.show(); // The state is now "interactive"
76   acceptPromise.catch(() => {}); // no-op, just to silence unhandled rejection in devtools.
77   await request.abort(); // The state is now "closed"
78   await promise_rejects(t, "InvalidStateError", request.canMakePayment());
79   try {
80     const result = await request.canMakePayment();
81     assert_true(
82       false,
83       `should have thrown InvalidStateError, but instead returned "${result}"`
84     );
85   } catch (err) {
86     assert_equals(
87       err.name,
88       "InvalidStateError",
89       "must be an InvalidStateError."
90     );
91   }
92 }, `If request.[[state]] is "closed", then return a promise rejected with an "InvalidStateError" DOMException.`);
93
94 promise_test(async t => {
95   const request = new PaymentRequest(defaultMethods, defaultDetails);
96   assert_true(await request.canMakePayment(), "basic-card should be supported");
97 }, `If payment method identifier and serialized parts are supported, resolve promise with true.`);
98
99 promise_test(async t => {
100   const unsupportedMethods = [
101     "this-is-not-supported",
102     "https://not.supported",
103     "e",
104     "n6jzof05mk2g4lhxr-u-q-w1-c-i-pa-ty-bdvs9-ho-ae7-p-md8-s-wq3-h-qd-e-q-sa",
105     "a-b-q-n-s-pw0",
106     "m-u",
107     "s-l5",
108     "k9-f",
109     "m-l",
110     "u4-n-t",
111     "i488jh6-g18-fck-yb-v7-i",
112     "x-x-t-t-c34-o",
113     "https://wpt",
114     "https://wpt.fyi/",
115     "https://wpt.fyi/payment",
116     "https://wpt.fyi/payment-request",
117     "https://wpt.fyi/payment-request?",
118     "https://wpt.fyi/payment-request?this=is",
119     "https://wpt.fyi/payment-request?this=is&totally",
120     "https://wpt.fyi:443/payment-request?this=is&totally",
121     "https://wpt.fyi:443/payment-request?this=is&totally#fine",
122     "https://:@wpt.fyi:443/payment-request?this=is&totally#👍",
123     " \thttps://wpt\n ",
124     "https://xn--c1yn36f",
125     "https://點看",
126   ];
127   for (const method of unsupportedMethods) {
128     try {
129       const request = new PaymentRequest(
130         [{ supportedMethods: method }],
131         defaultDetails
132       );
133       assert_false(
134         await request.canMakePayment(),
135         `method "${method}" must not be supported`
136       );
137     } catch (err) {
138       assert_true(
139         false,
140         `Unexpected exception testing method ${method}, expected false. See error console.`
141       );
142     }
143   }
144 }, `If payment method identifier is unknown, resolve promise with false.`);
145
146 promise_test(async t => {
147   // This test might never actually hit its assertion, but that's allowed.
148   const request = new PaymentRequest(defaultMethods, defaultDetails);
149   for (let i = 0; i < 1000; i++) {
150     try {
151       await request.canMakePayment();
152     } catch (err) {
153       assert_equals(
154         err.name,
155         "NotAllowedError",
156         "if it throws, then it must be a NotAllowedError."
157       );
158       break;
159     }
160   }
161   for (let i = 0; i < 1000; i++) {
162     try {
163       await new PaymentRequest(defaultMethods, defaultDetails).canMakePayment();
164     } catch (err) {
165       assert_equals(
166         err.name,
167         "NotAllowedError",
168         "if it throws, then it must be a NotAllowedError."
169       );
170       break;
171     }
172   }
173 }, `Optionally, at the user agent's discretion, return a promise rejected with a "NotAllowedError" DOMException.`);
174 </script>