Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / payments / test / BasiccardChromeScript.js
blob6ce2ca024b0c63a4e5bbb90b119d99ec40a627f8
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* Any copyright is dedicated to the Public Domain.
3    http://creativecommons.org/publicdomain/zero/1.0/ */
5 /* eslint-env mozilla/chrome-script */
7 "use strict";
9 const { XPCOMUtils } = ChromeUtils.importESModule(
10   "resource://gre/modules/XPCOMUtils.sys.mjs"
13 const paymentSrv = Cc[
14   "@mozilla.org/dom/payments/payment-request-service;1"
15 ].getService(Ci.nsIPaymentRequestService);
17 function emitTestFail(message) {
18   sendAsyncMessage("test-fail", `${DummyUIService.testName}: ${message}`);
21 const billingAddress = Cc[
22   "@mozilla.org/dom/payments/payment-address;1"
23 ].createInstance(Ci.nsIPaymentAddress);
24 const addressLine = Cc["@mozilla.org/array;1"].createInstance(
25   Ci.nsIMutableArray
27 const address = Cc["@mozilla.org/supports-string;1"].createInstance(
28   Ci.nsISupportsString
30 address.data = "Easton Ave";
31 addressLine.appendElement(address);
32 billingAddress.init(
33   "USA", // country
34   addressLine, // address line
35   "CA", // region
36   "CA", // region code
37   "San Bruno", // city
38   "", // dependent locality
39   "94066", // postal code
40   "123456", // sorting code
41   "", // organization
42   "Bill A. Pacheco", // recipient
43   "+14344413879"
44 ); // phone
46 const specialAddress = Cc[
47   "@mozilla.org/dom/payments/payment-address;1"
48 ].createInstance(Ci.nsIPaymentAddress);
49 const specialAddressLine = Cc["@mozilla.org/array;1"].createInstance(
50   Ci.nsIMutableArray
52 const specialData = Cc["@mozilla.org/supports-string;1"].createInstance(
53   Ci.nsISupportsString
55 specialData.data = ":$%@&*";
56 specialAddressLine.appendElement(specialData);
57 specialAddress.init(
58   "USA", // country
59   specialAddressLine, // address line
60   "CA", // region
61   "CA", // region code
62   "San Bruno", // city
63   "", // dependent locality
64   "94066", // postal code
65   "123456", // sorting code
66   "", // organization
67   "Bill A. Pacheco", // recipient
68   "+14344413879"
69 ); // phone
71 const basiccardResponseData = Cc[
72   "@mozilla.org/dom/payments/basiccard-response-data;1"
73 ].createInstance(Ci.nsIBasicCardResponseData);
75 const basiccardChangeDetails = Cc[
76   "@mozilla.org/dom/payments/basiccard-change-details;1"
77 ].createInstance(Ci.nsIBasicCardChangeDetails);
79 const showResponse = Cc[
80   "@mozilla.org/dom/payments/payment-show-action-response;1"
81 ].createInstance(Ci.nsIPaymentShowActionResponse);
83 function abortPaymentResponse(requestId) {
84   let abortResponse = Cc[
85     "@mozilla.org/dom/payments/payment-abort-action-response;1"
86   ].createInstance(Ci.nsIPaymentAbortActionResponse);
87   abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED);
88   paymentSrv.respondPayment(
89     abortResponse.QueryInterface(Ci.nsIPaymentActionResponse)
90   );
93 function completePaymentResponse(requestId) {
94   let completeResponse = Cc[
95     "@mozilla.org/dom/payments/payment-complete-action-response;1"
96   ].createInstance(Ci.nsIPaymentCompleteActionResponse);
97   completeResponse.init(
98     requestId,
99     Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
100   );
101   paymentSrv.respondPayment(
102     completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
103   );
106 function showRequest(requestId) {
107   if (DummyUIService.showAction === "payment-method-change") {
108     basiccardChangeDetails.initData(billingAddress);
109     try {
110       paymentSrv.changePaymentMethod(
111         requestId,
112         "basic-card",
113         basiccardChangeDetails.QueryInterface(Ci.nsIMethodChangeDetails)
114       );
115     } catch (error) {
116       emitTestFail(
117         `Unexpected error (${error.name}) when calling PaymentRequestService::changePaymentMethod`
118       );
119     }
120     return;
121   }
122   if (DummyUIService.showAction === "detailBasicCardResponse") {
123     try {
124       basiccardResponseData.initData(
125         "Bill A. Pacheco", // cardholderName
126         "4916855166538720", // cardNumber
127         "01", // expiryMonth
128         "2024", // expiryYear
129         "180", // cardSecurityCode
130         billingAddress
131       ); // billingAddress
132     } catch (e) {
133       emitTestFail("Fail to initialize basic card response data.");
134     }
135   }
136   if (DummyUIService.showAction === "simpleBasicCardResponse") {
137     try {
138       basiccardResponseData.initData(
139         "", // cardholderName
140         "4916855166538720", // cardNumber
141         "", // expiryMonth
142         "", // expiryYear
143         "", // cardSecurityCode
144         null
145       ); // billingAddress
146     } catch (e) {
147       emitTestFail("Fail to initialize basic card response data.");
148     }
149   }
150   if (DummyUIService.showAction === "specialAddressResponse") {
151     try {
152       basiccardResponseData.initData(
153         "Bill A. Pacheco", // cardholderName
154         "4916855166538720", // cardNumber
155         "01", // expiryMonth
156         "2024", // expiryYear
157         "180", // cardSecurityCode
158         specialAddress
159       ); // billingAddress
160     } catch (e) {
161       emitTestFail("Fail to initialize basic card response data.");
162     }
163   }
164   showResponse.init(
165     requestId,
166     Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
167     "basic-card", // payment method
168     basiccardResponseData, // payment method data
169     "Bill A. Pacheco", // payer name
170     "", // payer email
171     ""
172   ); // payer phone
173   paymentSrv.respondPayment(
174     showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
175   );
178 const DummyUIService = {
179   testName: "",
180   showAction: "",
181   showPayment: showRequest,
182   abortPayment: abortPaymentResponse,
183   completePayment: completePaymentResponse,
184   updatePayment: requestId => {
185     try {
186       basiccardResponseData.initData(
187         "Bill A. Pacheco", // cardholderName
188         "4916855166538720", // cardNumber
189         "01", // expiryMonth
190         "2024", // expiryYear
191         "180", // cardSecurityCode
192         billingAddress
193       ); // billingAddress
194     } catch (e) {
195       emitTestFail("Fail to initialize basic card response data.");
196     }
197     showResponse.init(
198       requestId,
199       Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
200       "basic-card", // payment method
201       basiccardResponseData, // payment method data
202       "Bill A. Pacheco", // payer name
203       "", // payer email
204       ""
205     ); // payer phone
206     paymentSrv.respondPayment(
207       showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
208     );
209   },
210   closePayment: requestId => {},
211   QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]),
214 paymentSrv.setTestingUIService(
215   DummyUIService.QueryInterface(Ci.nsIPaymentUIService)
218 addMessageListener("set-detailed-ui-service", function (testName) {
219   DummyUIService.testName = testName;
220   DummyUIService.showAction = "detailBasicCardResponse";
221   sendAsyncMessage("set-detailed-ui-service-complete");
224 addMessageListener("set-simple-ui-service", function (testName) {
225   DummyUIService.testName = testName;
226   DummyUIService.showAction = "simpleBasicCardResponse";
227   sendAsyncMessage("set-simple-ui-service-complete");
230 addMessageListener("set-special-address-ui-service", function (testName) {
231   DummyUIService.testName = testName;
232   DummyUIService.showAction = "specialAddressResponse";
233   sendAsyncMessage("set-special-address-ui-service-complete");
236 addMessageListener("method-change-to-basic-card", function (testName) {
237   DummyUIService.testName = testName;
238   DummyUIService.showAction = "payment-method-change";
239   sendAsyncMessage("method-change-to-basic-card-complete");
242 addMessageListener("error-response-test", function (testName) {
243   // test empty cardNumber
244   try {
245     basiccardResponseData.initData("", "", "", "", "", null);
246     emitTestFail(
247       "BasicCardResponse should not be initialized with empty cardNumber."
248     );
249   } catch (e) {
250     if (e.name != "NS_ERROR_FAILURE") {
251       emitTestFail(
252         "Empty cardNumber expected 'NS_ERROR_FAILURE', but got " + e.name + "."
253       );
254     }
255   }
257   // test invalid expiryMonth 123
258   try {
259     basiccardResponseData.initData("", "4916855166538720", "123", "", "", null);
260     emitTestFail(
261       "BasicCardResponse should not be initialized with invalid expiryMonth '123'."
262     );
263   } catch (e) {
264     if (e.name != "NS_ERROR_FAILURE") {
265       emitTestFail(
266         "expiryMonth 123 expected 'NS_ERROR_FAILURE', but got " + e.name + "."
267       );
268     }
269   }
270   // test invalid expiryMonth 99
271   try {
272     basiccardResponseData.initData("", "4916855166538720", "99", "", "", null);
273     emitTestFail(
274       "BasicCardResponse should not be initialized with invalid expiryMonth '99'."
275     );
276   } catch (e) {
277     if (e.name != "NS_ERROR_FAILURE") {
278       emitTestFail(
279         "expiryMonth 99 xpected 'NS_ERROR_FAILURE', but got " + e.name + "."
280       );
281     }
282   }
283   // test invalid expiryMonth ab
284   try {
285     basiccardResponseData.initData("", "4916855166538720", "ab", "", "", null);
286     emitTestFail(
287       "BasicCardResponse should not be initialized with invalid expiryMonth 'ab'."
288     );
289   } catch (e) {
290     if (e.name != "NS_ERROR_FAILURE") {
291       emitTestFail(
292         "expiryMonth ab expected 'NS_ERROR_FAILURE', but got " + e.name + "."
293       );
294     }
295   }
296   // test invalid expiryYear abcd
297   try {
298     basiccardResponseData.initData(
299       "",
300       "4916855166538720",
301       "",
302       "abcd",
303       "",
304       null
305     );
306     emitTestFail(
307       "BasicCardResponse should not be initialized with invalid expiryYear 'abcd'."
308     );
309   } catch (e) {
310     if (e.name != "NS_ERROR_FAILURE") {
311       emitTestFail(
312         "expiryYear abcd expected 'NS_ERROR_FAILURE', but got " + e.name + "."
313       );
314     }
315   }
316   // test invalid expiryYear 11111
317   try {
318     basiccardResponseData.initData(
319       "",
320       "4916855166538720",
321       "",
322       "11111",
323       "",
324       null
325     );
326     emitTestFail(
327       "BasicCardResponse should not be initialized with invalid expiryYear '11111'."
328     );
329   } catch (e) {
330     if (e.name != "NS_ERROR_FAILURE") {
331       emitTestFail(
332         "expiryYear 11111 expected 'NS_ERROR_FAILURE', but got " + e.name + "."
333       );
334     }
335   }
337   const responseData = Cc[
338     "@mozilla.org/dom/payments/general-response-data;1"
339   ].createInstance(Ci.nsIGeneralResponseData);
340   try {
341     responseData.initData({});
342   } catch (e) {
343     emitTestFail("Fail to initialize response data with empty object.");
344   }
346   try {
347     showResponse.init(
348       "testid",
349       Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
350       "basic-card", // payment method
351       responseData, // payment method data
352       "Bill A. Pacheco", // payer name
353       "", // payer email
354       ""
355     ); // payer phone
356     emitTestFail(
357       "nsIPaymentShowActionResponse should not be initialized with basic-card method and nsIGeneralResponseData."
358     );
359   } catch (e) {
360     if (e.name != "NS_ERROR_FAILURE") {
361       emitTestFail(
362         "ShowResponse init expected 'NS_ERROR_FAILURE', but got " + e.name + "."
363       );
364     }
365   }
366   sendAsyncMessage("error-response-test-complete");
369 addMessageListener("teardown", function () {
370   paymentSrv.setTestingUIService(null);
371   sendAsyncMessage("teardown-complete");