[macOS Debug WK2] Layout Test http/tests/ssl/applepay/ApplePayShippingAddressChangeEv...
[WebKit-https.git] / LayoutTests / http / tests / resources / payment-request.js
1 function activateThen(completion)
2 {
3     return new Promise(resolve => {
4         var button = document.createElement("button");
5         button.style["position"] = "absolute";
6         button.onclick = () => {
7             document.body.removeChild(button);
8             resolve(completion());
9         };
10         document.body.insertBefore(button, document.body.firstChild);
11         UIHelper.activateElement(button);
12     });
13 }
14
15 function user_activation_test(func, name)
16 {
17     promise_test(async t => {
18         await activateThen(() => func(t));
19     }, name);
20 }
21
22 function validPaymentMethod()
23 {
24     return {
25         supportedMethods: 'https://apple.com/apple-pay',
26         data: {
27             version: 2,
28             merchantIdentifier: '',
29             countryCode: 'US',
30             supportedNetworks: ['visa', 'masterCard'],
31             merchantCapabilities: ['supports3DS'],
32         },
33     }
34 }
35
36 function validPaymentDetails()
37 {
38     return {
39         total: {
40             label: 'Total',
41             amount: {
42                 currency: 'USD',
43                 value: '10.00',
44             },
45         },
46         displayItems: [{
47             label: 'Item',
48             amount: {
49                 currency: 'USD',
50                 value: '10.00',
51             },
52         }],
53     }
54 }
55
56 function updateDetailsOnShippingAddressChange(paymentDetailsInit, paymentOptions, detailsUpdate)
57 {
58     return new Promise((resolve, reject) => {
59         activateThen(() => {
60             var request = new PaymentRequest([validPaymentMethod()], paymentDetailsInit, paymentOptions);
61             request.onmerchantvalidation = (event) => {
62                 event.complete({ });
63             };
64             request.onshippingaddresschange = (event) => {
65                 var detailsUpdatePromise = new Promise((resolve, reject) => {
66                     resolve(detailsUpdate);
67                 });
68                 event.updateWith(detailsUpdatePromise);
69                 detailsUpdatePromise.then(() => {
70                     resolve();
71                     request.abort().catch(() => { });
72                 });
73             };
74             request.show().catch(error => error);
75         });
76     });
77 }