[Payment Request] Update web platform tests
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / payment-request / payment-request-canmakepayment-method.https.html
1 <!DOCTYPE html>
2 <meta charset="utf-8">
3 <title>Tests for PaymentRequest.canMakePayment() method</title>
4 <link rel="help" href="https://w3c.github.io/browser-payment-api/#show-method">
5 <script src="/resources/testharness.js"></script>
6 <script src="/resources/testharnessreport.js"></script>
7 <script src='/resources/testdriver-vendor.js'></script>
8 <script src="/resources/testdriver.js"></script>
9 <script>
10 const basicCard = Object.freeze({ supportedMethods: "basic-card" });
11 const applePay = Object.freeze({
12   supportedMethods: "https://apple.com/apple-pay",
13 });
14 const defaultMethods = Object.freeze([basicCard, applePay]);
15 const defaultDetails = Object.freeze({
16   total: {
17     label: "Total",
18     amount: {
19       currency: "USD",
20       value: "1.00",
21     },
22   },
23 });
24
25 const unsupportedMethods = [
26   { supportedMethods: "this-is-not-supported" },
27   { supportedMethods: "https://not.supported" },
28 ];
29
30 promise_test(async t => {
31   const request = new PaymentRequest(defaultMethods, defaultDetails);
32   assert_true(
33     await request.canMakePayment(),
34     "one of the methods should be supported"
35   );
36 }, `If payment method identifier are supported, resolve promise with true.`);
37
38 promise_test(async t => {
39   const request = new PaymentRequest(defaultMethods, defaultDetails);
40   const acceptPromise = test_driver.bless("show payment request", () => {
41     request.show(); // Sets state to "interactive"
42   });
43   const canMakePaymentPromise = request.canMakePayment();
44   try {
45     const result = await canMakePaymentPromise;
46     assert_true(
47       false,
48       `canMakePaymentPromise should have thrown InvalidStateError`
49     );
50   } catch (err) {
51     await promise_rejects(t, "InvalidStateError", canMakePaymentPromise);
52   } finally {
53     await request.abort();
54     await promise_rejects(t, "AbortError", acceptPromise);
55   }
56   // The state should be "closed"
57   await promise_rejects(t, "InvalidStateError", request.canMakePayment());
58 }, 'If request.[[state]] is "interactive", then return a promise rejected with an "InvalidStateError" DOMException.');
59
60 promise_test(async t => {
61   const request = new PaymentRequest(defaultMethods, defaultDetails);
62   const acceptPromise = test_driver.bless("show payment request", () => {
63     request.show(); // Sets state to "interactive"
64   });
65   acceptPromise.catch(() => {}); // no-op, just to silence unhandled rejection in devtools.
66   await request.abort(); // The state is now "closed"
67   await promise_rejects(t, "InvalidStateError", request.canMakePayment());
68   try {
69     const result = await request.canMakePayment();
70     assert_true(
71       false,
72       `should have thrown InvalidStateError, but instead returned "${result}"`
73     );
74   } catch (err) {
75     assert_equals(
76       err.name,
77       "InvalidStateError",
78       "must be an InvalidStateError."
79     );
80   }
81 }, 'If request.[[state]] is "closed", then return a promise rejected with an "InvalidStateError" DOMException.');
82
83 promise_test(async t => {
84   const request = new PaymentRequest(defaultMethods, defaultDetails);
85   try {
86     assert_true(
87       await request.canMakePayment(),
88       `canMakePaymentPromise should be true`
89     );
90     assert_true(
91       await request.canMakePayment(),
92       `canMakePaymentPromise should be true`
93     );
94   } catch (err) {
95     assert_equals(
96       err.name,
97       "NotAllowedError",
98       "if it throws, then it must be a NotAllowedError."
99     );
100   }
101 }, `If request.[[state]] is "created", then return a promise that resolves to true for known method.`);
102
103 promise_test(async t => {
104   const noneSupported = new PaymentRequest(
105     unsupportedMethods,
106     defaultDetails
107   ).canMakePayment();
108   assert_false(await noneSupported, `methods must not be supported`);
109 }, "All methods are unsupported");
110
111 promise_test(async t => {
112   const someSupported = new PaymentRequest(
113     [...unsupportedMethods, ...defaultMethods],
114     defaultDetails
115   ).canMakePayment();
116   assert_true(await someSupported, `At least one method is expected to be supported.`);
117 }, `Mix of supported and unsupported methods, at least one method is supported.`);
118 </script>
119
120 <small>
121   If you find a buggy test, please <a href="https://github.com/web-platform-tests/wpt/issues">file a bug</a>
122   and tag one of the <a href="https://github.com/web-platform-tests/wpt/blob/master/payment-request/META.yml">suggested reviewers</a>.
123 </small>