[Payment Request] Implement PaymentRequest.canMakePayment()
[WebKit-https.git] / LayoutTests / http / tests / ssl / applepay / PaymentRequest.https.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <script src="../../resources/js-test-pre.js"></script>
6 </head>
7 <body>
8 <script>
9
10 description("Test basic creation of a PaymentRequest object with an Apple Pay payment method.");
11
12 window.jsTestIsAsync = true;
13
14 function validPaymentMethod() {
15     return {
16         supportedMethods: 'https://apple.com/apple-pay',
17         data: {
18             version: 2,
19             merchantIdentifier: '',
20             countryCode: 'US',
21             currencyCode: 'USD',
22             supportedNetworks: ['visa', 'masterCard'],
23             merchantCapabilities: ['supports3DS'],
24         },
25     }
26 }
27
28 function validPaymentDetails() {
29     return {
30         total: { label: 'Your label', amount: { currency: 'USD', value: '10.00' } },
31     }
32 }
33
34 function logAndShouldThrow(setup, test) {
35     debug("SETUP: " + setup)
36     eval(setup)
37     shouldThrow(test)
38     debug("")
39 }
40
41 function logAndShouldNotThrow(setup, test) {
42     debug("SETUP: " + setup)
43     eval(setup)
44     shouldNotThrow(test)
45     debug("")
46 }
47
48 function logAndShouldReject(setup, test) {
49     debug("SETUP: " + setup)
50     eval(setup)
51     return shouldReject(test)
52 }
53
54 async function go() {
55     debug("Testing PaymentRequest")
56     debug("")
57     shouldThrow("new PaymentRequest(null, null)")
58     shouldNotThrow("new PaymentRequest([validPaymentMethod()], validPaymentDetails())")
59     debug("")
60
61     debug("Testing ApplePayRequest.countryCode")
62     debug("")
63     await logAndShouldReject("paymentMethod = validPaymentMethod(); delete paymentMethod.data.countryCode;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
64     debug("")
65     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.countryCode = 'invalid';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
66     debug("")
67     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.countryCode = '';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
68     debug("")
69     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.countryCode = null;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
70     debug("")
71     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.countryCode = undefined;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
72     debug("")
73     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.countryCode = 7;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
74     debug("")
75
76     debug("Testing ApplePayRequest.currencyCode")
77     debug("")
78     await logAndShouldReject("paymentMethod = validPaymentMethod(); delete paymentMethod.data.currencyCode;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
79     debug("")
80     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.currencyCode = 'invalid';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
81     debug("")
82     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.currencyCode = '';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
83     debug("")
84     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.currencyCode = null;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
85     debug("")
86     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.currencyCode = undefined;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
87     debug("")
88     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.currencyCode = 7;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
89     debug("")
90
91     debug("Testing ApplePayRequest.supportedNetworks")
92     debug("")
93     await logAndShouldReject("paymentMethod = validPaymentMethod(); delete paymentMethod.data.supportedNetworks;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
94     debug("")
95     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = '';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
96     debug("")
97     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = null;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
98     debug("")
99     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = undefined;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
100     debug("")
101     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = 7;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
102     debug("")
103     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = [];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
104     debug("")
105     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = ['invalidNetwork'];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
106     debug("")
107     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.supportedNetworks = ['invalidNetwork', 'visa'];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
108     debug("")
109
110     debug("Testing ApplePayRequest.merchantCapabilities")
111     debug("");
112     await logAndShouldReject("paymentMethod = validPaymentMethod(); delete paymentMethod.data.merchantCapabilities;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
113     debug("")
114     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = '';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
115     debug("")
116     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = null;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
117     debug("")
118     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = undefined;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
119     debug("")
120     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = 7;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
121     debug("")
122     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = [];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
123     debug("")
124     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = ['invalidCapability'];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
125     debug("")
126     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.merchantCapabilities = ['invalidCapability', 'supports3DS'];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
127     debug("")
128
129     debug("Testing ApplePayRequest.requiredBillingContactFields")
130     debug("")
131     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = '';", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
132     debug("")
133     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = null;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
134     debug("")
135     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = 7;", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
136     debug("")
137     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = { };", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
138     debug("")
139     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = [''];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
140     debug("")
141     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = [null];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
142     debug("")
143     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = [undefined];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
144     debug("")
145     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = [{}];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
146     debug("")
147     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.requiredBillingContactFields = ['invalid'];", "request = new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
148     debug("")
149
150     debug("Testing ApplePayRequest.billingContact")
151     debug("")
152     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.billingContact = '';", "new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
153     debug("")
154     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.billingContact = 7;", "new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
155     debug("")
156     
157     debug("Testing ApplePayRequest.applicationData")
158     debug("")
159     await logAndShouldReject("paymentMethod = validPaymentMethod(); paymentMethod.data.applicationData = { toString: function() { throw '\"Error in toString\"'; } };", "new PaymentRequest([paymentMethod], validPaymentDetails()); request.show()")
160     debug("")
161
162     debug("Testing PaymentDetails.total")
163     debug("")
164     logAndShouldThrow("paymentDetails = validPaymentDetails(); delete paymentDetails.total;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
165     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = '';", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
166     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = null;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
167     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = undefined;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
168     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = 7;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
169     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = [];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
170     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = { };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
171     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = { label: 'label' };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
172     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = { label: 'label', amount: 'amount' };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
173     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = { label: 'label', amount: { currency: '', value: '0' } };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
174     await logAndShouldReject("paymentDetails = validPaymentDetails(); paymentDetails.total = { label: 'label', amount: { currency: 'USD', value: '0' } };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
175     debug("")
176     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.total = { label: 'label', amount: { currency: 'USD', value:'-10.00'} };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
177     await logAndShouldReject("paymentDetails = validPaymentDetails(); paymentDetails.total = { label: 'label', amount: { currency: 'USD', value: '10000000000.00' } };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
178     debug("")
179
180     debug("Testing PaymentDetails.displayItems")
181     debug("");
182     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = '';", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
183     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = null;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
184     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = 7;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
185     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = { };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
186     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [''];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
187     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [null];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
188     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [undefined];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
189     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [{}];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
190     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [{ label: 'label' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
191     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [{ label: 'label', amount: '' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
192     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.displayItems = [{ label: 'label', amount: '10.00', type: 'invalid' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
193     
194     debug("Testing PaymentDetails.shippingOptions")
195     debug("");
196     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = '';", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
197     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = 'invalid';", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
198     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = null;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
199     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = 7;", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
200     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = { };", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
201     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = [{ }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
202     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = [{ amount: '', }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
203     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = [{ amount: '', detail: '' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
204     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = [{ amount: '', detail: '', identifier: '' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
205     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = [{ amount: '', detail: '', identifier: '', label: '' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
206     logAndShouldThrow("paymentDetails = validPaymentDetails(); paymentDetails.shippingOptions = [{ amount: '-1', detail: '', identifier: '', label: '' }];", "request = new PaymentRequest([validPaymentMethod()], paymentDetails); request.show()")
207
208     debug("Testing PaymentOptions")
209     debug("");
210     logAndShouldThrow("paymentOptions = {}; paymentOptions.shippingType = '';", "new PaymentRequest([validPaymentMethod()], validPaymentDetails(), paymentOptions); request.show()")
211     logAndShouldThrow("paymentOptions = {}; paymentOptions.shippingType = 'invalid';", "new PaymentRequest([validPaymentMethod()], validPaymentDetails(), paymentOptions); request.show()")
212     logAndShouldThrow("paymentOptions = {}; paymentOptions.shippingType = null;", "new PaymentRequest([validPaymentMethod()], validPaymentDetails(), paymentOptions); request.show()")
213     logAndShouldThrow("paymentOptions = {}; paymentOptions.shippingType = 7;", "new PaymentRequest([validPaymentMethod()], validPaymentDetails(), paymentOptions); request.show()")
214     logAndShouldThrow("paymentOptions = {}; paymentOptions.shippingType = { };", "new PaymentRequest([validPaymentMethod()], validPaymentDetails(), paymentOptions); request.show()")
215
216     document.querySelector("button").remove();
217
218     finishJSTest();
219 }
220
221 function clickElement(element) {
222     let x = element.offsetLeft + 2;
223     let y = element.offsetTop + 2;
224
225     var supportsTouchEvents = "TouchEvent" in window;
226     if (supportsTouchEvents && window.testRunner && testRunner.runUIScript) {
227         testRunner.runUIScript(`(function() { uiController.singleTapAtPoint(${x}, ${y}, function() { }); })();`, function () { });
228     } else if (window.eventSender) {
229         eventSender.mouseMoveTo(x, y);
230         eventSender.mouseDown();
231         eventSender.mouseUp();
232     }
233 }
234
235 window.onload = function() {
236     clickElement(document.querySelector("button"));
237 }
238
239 </script>
240 <button onclick='go()'>Go</button>
241 <script src="../../resources/js-test-post.js"></script>
242 </body>
243 </html>