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 */
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(
27 const address = Cc["@mozilla.org/supports-string;1"].createInstance(
30 address.data = "Easton Ave";
31 addressLine.appendElement(address);
34 addressLine, // address line
38 "", // dependent locality
39 "94066", // postal code
40 "123456", // sorting code
42 "Bill A. Pacheco", // recipient
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(
52 const specialData = Cc["@mozilla.org/supports-string;1"].createInstance(
55 specialData.data = ":$%@&*";
56 specialAddressLine.appendElement(specialData);
59 specialAddressLine, // address line
63 "", // dependent locality
64 "94066", // postal code
65 "123456", // sorting code
67 "Bill A. Pacheco", // recipient
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)
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(
99 Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
101 paymentSrv.respondPayment(
102 completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
106 function showRequest(requestId) {
107 if (DummyUIService.showAction === "payment-method-change") {
108 basiccardChangeDetails.initData(billingAddress);
110 paymentSrv.changePaymentMethod(
113 basiccardChangeDetails.QueryInterface(Ci.nsIMethodChangeDetails)
117 `Unexpected error (${error.name}) when calling PaymentRequestService::changePaymentMethod`
122 if (DummyUIService.showAction === "detailBasicCardResponse") {
124 basiccardResponseData.initData(
125 "Bill A. Pacheco", // cardholderName
126 "4916855166538720", // cardNumber
128 "2024", // expiryYear
129 "180", // cardSecurityCode
133 emitTestFail("Fail to initialize basic card response data.");
136 if (DummyUIService.showAction === "simpleBasicCardResponse") {
138 basiccardResponseData.initData(
139 "", // cardholderName
140 "4916855166538720", // cardNumber
143 "", // cardSecurityCode
147 emitTestFail("Fail to initialize basic card response data.");
150 if (DummyUIService.showAction === "specialAddressResponse") {
152 basiccardResponseData.initData(
153 "Bill A. Pacheco", // cardholderName
154 "4916855166538720", // cardNumber
156 "2024", // expiryYear
157 "180", // cardSecurityCode
161 emitTestFail("Fail to initialize basic card response data.");
166 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
167 "basic-card", // payment method
168 basiccardResponseData, // payment method data
169 "Bill A. Pacheco", // payer name
173 paymentSrv.respondPayment(
174 showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
178 const DummyUIService = {
181 showPayment: showRequest,
182 abortPayment: abortPaymentResponse,
183 completePayment: completePaymentResponse,
184 updatePayment: requestId => {
186 basiccardResponseData.initData(
187 "Bill A. Pacheco", // cardholderName
188 "4916855166538720", // cardNumber
190 "2024", // expiryYear
191 "180", // cardSecurityCode
195 emitTestFail("Fail to initialize basic card response data.");
199 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
200 "basic-card", // payment method
201 basiccardResponseData, // payment method data
202 "Bill A. Pacheco", // payer name
206 paymentSrv.respondPayment(
207 showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
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
245 basiccardResponseData.initData("", "", "", "", "", null);
247 "BasicCardResponse should not be initialized with empty cardNumber."
250 if (e.name != "NS_ERROR_FAILURE") {
252 "Empty cardNumber expected 'NS_ERROR_FAILURE', but got " + e.name + "."
257 // test invalid expiryMonth 123
259 basiccardResponseData.initData("", "4916855166538720", "123", "", "", null);
261 "BasicCardResponse should not be initialized with invalid expiryMonth '123'."
264 if (e.name != "NS_ERROR_FAILURE") {
266 "expiryMonth 123 expected 'NS_ERROR_FAILURE', but got " + e.name + "."
270 // test invalid expiryMonth 99
272 basiccardResponseData.initData("", "4916855166538720", "99", "", "", null);
274 "BasicCardResponse should not be initialized with invalid expiryMonth '99'."
277 if (e.name != "NS_ERROR_FAILURE") {
279 "expiryMonth 99 xpected 'NS_ERROR_FAILURE', but got " + e.name + "."
283 // test invalid expiryMonth ab
285 basiccardResponseData.initData("", "4916855166538720", "ab", "", "", null);
287 "BasicCardResponse should not be initialized with invalid expiryMonth 'ab'."
290 if (e.name != "NS_ERROR_FAILURE") {
292 "expiryMonth ab expected 'NS_ERROR_FAILURE', but got " + e.name + "."
296 // test invalid expiryYear abcd
298 basiccardResponseData.initData(
307 "BasicCardResponse should not be initialized with invalid expiryYear 'abcd'."
310 if (e.name != "NS_ERROR_FAILURE") {
312 "expiryYear abcd expected 'NS_ERROR_FAILURE', but got " + e.name + "."
316 // test invalid expiryYear 11111
318 basiccardResponseData.initData(
327 "BasicCardResponse should not be initialized with invalid expiryYear '11111'."
330 if (e.name != "NS_ERROR_FAILURE") {
332 "expiryYear 11111 expected 'NS_ERROR_FAILURE', but got " + e.name + "."
337 const responseData = Cc[
338 "@mozilla.org/dom/payments/general-response-data;1"
339 ].createInstance(Ci.nsIGeneralResponseData);
341 responseData.initData({});
343 emitTestFail("Fail to initialize response data with empty object.");
349 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
350 "basic-card", // payment method
351 responseData, // payment method data
352 "Bill A. Pacheco", // payer name
357 "nsIPaymentShowActionResponse should not be initialized with basic-card method and nsIGeneralResponseData."
360 if (e.name != "NS_ERROR_FAILURE") {
362 "ShowResponse init expected 'NS_ERROR_FAILURE', but got " + e.name + "."
366 sendAsyncMessage("error-response-test-complete");
369 addMessageListener("teardown", function () {
370 paymentSrv.setTestingUIService(null);
371 sendAsyncMessage("teardown-complete");