74791c4f1d23f0ea38328b69aca33fe25dabd2bb
[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="/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>
12 <script>
13     const applePay = Object.freeze({
14     supportedMethods: "https://apple.com/apple-pay",
15     data: {
16         version: 2,
17         merchantIdentifier: "",
18         merchantCapabilities: ["supports3DS"],
19         supportedNetworks: ["visa", "masterCard"],
20         countryCode: "US",
21     }
22 });
23 const defaultMethods = Object.freeze([applePay]);
24 const defaultDetails = Object.freeze({
25   total: {
26     label: "Total",
27     amount: {
28       currency: "USD",
29       value: "1.00",
30     },
31   },
32 });
33
34 promise_test(async t => {
35   const request = new PaymentRequest(defaultMethods, defaultDetails);
36   try {
37     assert_true(
38       await request.canMakePayment(),
39       `canMakePaymentPromise should be true`
40     );
41     assert_true(
42       await request.canMakePayment(),
43       `canMakePaymentPromise should be true`
44     );
45   } catch (err) {
46     assert_equals(
47       err.name,
48       "NotAllowedError",
49       "if it throws, then it must be a NotAllowedError."
50     );
51   }
52 }, `If request.[[state]] is "created", then return a promise that resolves to true for known method.`);
53
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();
58   try {
59     const result = await canMakePaymentPromise;
60     assert_true(
61       false,
62       `canMakePaymentPromise should have thrown InvalidStateError`
63     );
64   } catch (err) {
65     await promise_rejects(t, "InvalidStateError", canMakePaymentPromise);
66   } finally {
67     await request.abort();
68     await promise_rejects(t, "AbortError", acceptPromise);
69   }
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.`);
73
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());
80   try {
81     const result = await request.canMakePayment();
82     assert_true(
83       false,
84       `should have thrown InvalidStateError, but instead returned "${result}"`
85     );
86   } catch (err) {
87     assert_equals(
88       err.name,
89       "InvalidStateError",
90       "must be an InvalidStateError."
91     );
92   }
93 }, `If request.[[state]] is "closed", then return a promise rejected with an "InvalidStateError" DOMException.`);
94
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.`);
99
100 promise_test(async t => {
101   const unsupportedMethods = [
102     "this-is-not-supported",
103     "https://not.supported",
104     "e",
105     "n6jzof05mk2g4lhxr-u-q-w1-c-i-pa-ty-bdvs9-ho-ae7-p-md8-s-wq3-h-qd-e-q-sa",
106     "a-b-q-n-s-pw0",
107     "m-u",
108     "s-l5",
109     "k9-f",
110     "m-l",
111     "u4-n-t",
112     "i488jh6-g18-fck-yb-v7-i",
113     "x-x-t-t-c34-o",
114     "https://wpt",
115     "https://wpt.fyi/",
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#👍",
124     " \thttps://wpt\n ",
125     "https://xn--c1yn36f",
126     "https://點看",
127   ];
128   for (const method of unsupportedMethods) {
129     try {
130       const request = new PaymentRequest(
131         [{ supportedMethods: method }],
132         defaultDetails
133       );
134       assert_false(
135         await request.canMakePayment(),
136         `method "${method}" must not be supported`
137       );
138     } catch (err) {
139       assert_true(
140         false,
141         `Unexpected exception testing method ${method}, expected false. See error console.`
142       );
143     }
144   }
145 }, `If payment method identifier is unknown, resolve promise with false.`);
146
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++) {
151     try {
152       await request.canMakePayment();
153     } catch (err) {
154       assert_equals(
155         err.name,
156         "NotAllowedError",
157         "if it throws, then it must be a NotAllowedError."
158       );
159       break;
160     }
161   }
162   for (let i = 0; i < 1000; i++) {
163     try {
164       await new PaymentRequest(defaultMethods, defaultDetails).canMakePayment();
165     } catch (err) {
166       assert_equals(
167         err.name,
168         "NotAllowedError",
169         "if it throws, then it must be a NotAllowedError."
170       );
171       break;
172     }
173   }
174 }, `Optionally, at the user agent's discretion, return a promise rejected with a "NotAllowedError" DOMException.`);
175 </script>