From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- dom/payments/test/BasicCardErrorsChromeScript.js | 133 ++++++ dom/payments/test/BasiccardChromeScript.js | 372 +++++++++++++++ dom/payments/test/Bug1478740ChromeScript.js | 90 ++++ dom/payments/test/Bug1490698ChromeScript.js | 226 +++++++++ dom/payments/test/ClosePaymentChromeScript.js | 160 +++++++ dom/payments/test/ConstructorChromeScript.js | 490 ++++++++++++++++++++ .../test/CurrencyAmountValidationChromeScript.js | 67 +++ dom/payments/test/DefaultData.js | 59 +++ dom/payments/test/GeneralChromeScript.js | 20 + dom/payments/test/PMIValidationChromeScript.js | 82 ++++ dom/payments/test/PayerDetailsChromeScript.js | 83 ++++ dom/payments/test/RequestShippingChromeScript.js | 116 +++++ dom/payments/test/RetryPaymentChromeScript.js | 238 ++++++++++ dom/payments/test/ShippingOptionsChromeScript.js | 120 +++++ dom/payments/test/ShowPaymentChromeScript.js | 393 ++++++++++++++++ dom/payments/test/UpdateErrorsChromeScript.js | 214 +++++++++ dom/payments/test/blank_page.html | 16 + dom/payments/test/browser.ini | 10 + .../test/browser_payment_in_different_tabs.js | 37 ++ dom/payments/test/bug1478740.html | 44 ++ dom/payments/test/echo_payment_request.html | 37 ++ dom/payments/test/head.js | 127 ++++++ dom/payments/test/mochitest.ini | 53 +++ dom/payments/test/simple_payment_request.html | 81 ++++ dom/payments/test/test_abortPayment.html | 95 ++++ dom/payments/test/test_basiccard.html | 371 +++++++++++++++ dom/payments/test/test_basiccarderrors.html | 85 ++++ dom/payments/test/test_block_none10s.html | 58 +++ dom/payments/test/test_bug1478740.html | 140 ++++++ dom/payments/test/test_bug1490698.html | 119 +++++ dom/payments/test/test_canMakePayment.html | 164 +++++++ dom/payments/test/test_closePayment.html | 284 ++++++++++++ dom/payments/test/test_constructor.html | 351 ++++++++++++++ .../test/test_currency_amount_validation.html | 353 +++++++++++++++ dom/payments/test/test_payerDetails.html | 107 +++++ .../test/test_payment-request-in-iframe.html | 168 +++++++ dom/payments/test/test_pmi_validation.html | 245 ++++++++++ dom/payments/test/test_requestShipping.html | 180 ++++++++ dom/payments/test/test_retryPayment.html | 354 +++++++++++++++ dom/payments/test/test_shippingOptions.html | 208 +++++++++ dom/payments/test/test_showPayment.html | 504 +++++++++++++++++++++ dom/payments/test/test_update_errors.html | 121 +++++ 42 files changed, 7175 insertions(+) create mode 100644 dom/payments/test/BasicCardErrorsChromeScript.js create mode 100644 dom/payments/test/BasiccardChromeScript.js create mode 100644 dom/payments/test/Bug1478740ChromeScript.js create mode 100644 dom/payments/test/Bug1490698ChromeScript.js create mode 100644 dom/payments/test/ClosePaymentChromeScript.js create mode 100644 dom/payments/test/ConstructorChromeScript.js create mode 100644 dom/payments/test/CurrencyAmountValidationChromeScript.js create mode 100644 dom/payments/test/DefaultData.js create mode 100644 dom/payments/test/GeneralChromeScript.js create mode 100644 dom/payments/test/PMIValidationChromeScript.js create mode 100644 dom/payments/test/PayerDetailsChromeScript.js create mode 100644 dom/payments/test/RequestShippingChromeScript.js create mode 100644 dom/payments/test/RetryPaymentChromeScript.js create mode 100644 dom/payments/test/ShippingOptionsChromeScript.js create mode 100644 dom/payments/test/ShowPaymentChromeScript.js create mode 100644 dom/payments/test/UpdateErrorsChromeScript.js create mode 100644 dom/payments/test/blank_page.html create mode 100644 dom/payments/test/browser.ini create mode 100644 dom/payments/test/browser_payment_in_different_tabs.js create mode 100644 dom/payments/test/bug1478740.html create mode 100644 dom/payments/test/echo_payment_request.html create mode 100644 dom/payments/test/head.js create mode 100644 dom/payments/test/mochitest.ini create mode 100644 dom/payments/test/simple_payment_request.html create mode 100644 dom/payments/test/test_abortPayment.html create mode 100644 dom/payments/test/test_basiccard.html create mode 100644 dom/payments/test/test_basiccarderrors.html create mode 100644 dom/payments/test/test_block_none10s.html create mode 100644 dom/payments/test/test_bug1478740.html create mode 100644 dom/payments/test/test_bug1490698.html create mode 100644 dom/payments/test/test_canMakePayment.html create mode 100644 dom/payments/test/test_closePayment.html create mode 100644 dom/payments/test/test_constructor.html create mode 100644 dom/payments/test/test_currency_amount_validation.html create mode 100644 dom/payments/test/test_payerDetails.html create mode 100644 dom/payments/test/test_payment-request-in-iframe.html create mode 100644 dom/payments/test/test_pmi_validation.html create mode 100644 dom/payments/test/test_requestShipping.html create mode 100644 dom/payments/test/test_retryPayment.html create mode 100644 dom/payments/test/test_shippingOptions.html create mode 100644 dom/payments/test/test_showPayment.html create mode 100644 dom/payments/test/test_update_errors.html (limited to 'dom/payments/test') diff --git a/dom/payments/test/BasicCardErrorsChromeScript.js b/dom/payments/test/BasicCardErrorsChromeScript.js new file mode 100644 index 0000000000..f92e5eef5c --- /dev/null +++ b/dom/payments/test/BasicCardErrorsChromeScript.js @@ -0,0 +1,133 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +const defaultCard = { + cardholderName: "", + cardNumber: "4111111111111111", + expiryMonth: "", + expiryYear: "", + cardSecurityCode: "", + billingAddress: null, +}; + +function makeBillingAddress() { + const billingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" + ].createInstance(Ci.nsIPaymentAddress); + const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray + ); + const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString + ); + address.data = "Easton Ave"; + addressLine.appendElement(address); + const addressArgs = [ + "USA", // country + addressLine, // address line + "CA", // region + "CA", // regionCode + "San Bruno", // city + "", // dependent locality + "94066", // postal code + "123456", // sorting code + "", // organization + "Bill A. Pacheco", // recipient + "+14344413879", // phone + ]; + billingAddress.init(...addressArgs); + return billingAddress; +} + +function makeBasicCardResponse(details) { + const basicCardResponseData = Cc[ + "@mozilla.org/dom/payments/basiccard-response-data;1" + ].createInstance(Ci.nsIBasicCardResponseData); + const { + cardholderName, + cardNumber, + expiryMonth, + expiryYear, + cardSecurityCode, + billingAddress, + } = details; + + const address = + billingAddress !== undefined ? billingAddress : makeBillingAddress(); + + basicCardResponseData.initData( + cardholderName, + cardNumber, + expiryMonth, + expiryYear, + cardSecurityCode, + address + ); + + return basicCardResponseData; +} + +const TestingUIService = { + showPayment(requestId, details = { ...defaultCard }) { + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + makeBasicCardResponse(details), + "Person name", + "Person email", + "Person phone" + ); + + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + // Handles response.retry({ paymentMethod }): + updatePayment(requestId) { + // Let's echo what was sent in by the error... + const request = paymentSrv.getPaymentRequestById(requestId); + this.showPayment(requestId, request.paymentDetails.paymentMethodErrors); + }, + // Handles response.complete() + completePayment(requestId) { + const request = paymentSrv.getPaymentRequestById(requestId); + const completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + get QueryInterface() { + return ChromeUtils.generateQI(["nsIPaymentUIService"]); + }, +}; + +paymentSrv.setTestingUIService( + TestingUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("teardown", () => { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/BasiccardChromeScript.js b/dom/payments/test/BasiccardChromeScript.js new file mode 100644 index 0000000000..670f41ab48 --- /dev/null +++ b/dom/payments/test/BasiccardChromeScript.js @@ -0,0 +1,372 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", `${DummyUIService.testName}: ${message}`); +} + +const billingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +address.data = "Easton Ave"; +addressLine.appendElement(address); +billingAddress.init( + "USA", // country + addressLine, // address line + "CA", // region + "CA", // region code + "San Bruno", // city + "", // dependent locality + "94066", // postal code + "123456", // sorting code + "", // organization + "Bill A. Pacheco", // recipient + "+14344413879" +); // phone + +const specialAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const specialAddressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const specialData = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +specialData.data = ":$%@&*"; +specialAddressLine.appendElement(specialData); +specialAddress.init( + "USA", // country + specialAddressLine, // address line + "CA", // region + "CA", // region code + "San Bruno", // city + "", // dependent locality + "94066", // postal code + "123456", // sorting code + "", // organization + "Bill A. Pacheco", // recipient + "+14344413879" +); // phone + +const basiccardResponseData = Cc[ + "@mozilla.org/dom/payments/basiccard-response-data;1" +].createInstance(Ci.nsIBasicCardResponseData); + +const basiccardChangeDetails = Cc[ + "@mozilla.org/dom/payments/basiccard-change-details;1" +].createInstance(Ci.nsIBasicCardChangeDetails); + +const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" +].createInstance(Ci.nsIPaymentShowActionResponse); + +function abortPaymentResponse(requestId) { + let abortResponse = Cc[ + "@mozilla.org/dom/payments/payment-abort-action-response;1" + ].createInstance(Ci.nsIPaymentAbortActionResponse); + abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED); + paymentSrv.respondPayment( + abortResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function completePaymentResponse(requestId) { + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function showRequest(requestId) { + if (DummyUIService.showAction === "payment-method-change") { + basiccardChangeDetails.initData(billingAddress); + try { + paymentSrv.changePaymentMethod( + requestId, + "basic-card", + basiccardChangeDetails.QueryInterface(Ci.nsIMethodChangeDetails) + ); + } catch (error) { + emitTestFail( + `Unexpected error (${error.name}) when calling PaymentRequestService::changePaymentMethod` + ); + } + return; + } + if (DummyUIService.showAction === "detailBasicCardResponse") { + try { + basiccardResponseData.initData( + "Bill A. Pacheco", // cardholderName + "4916855166538720", // cardNumber + "01", // expiryMonth + "2024", // expiryYear + "180", // cardSecurityCode + billingAddress + ); // billingAddress + } catch (e) { + emitTestFail("Fail to initialize basic card response data."); + } + } + if (DummyUIService.showAction === "simpleBasicCardResponse") { + try { + basiccardResponseData.initData( + "", // cardholderName + "4916855166538720", // cardNumber + "", // expiryMonth + "", // expiryYear + "", // cardSecurityCode + null + ); // billingAddress + } catch (e) { + emitTestFail("Fail to initialize basic card response data."); + } + } + if (DummyUIService.showAction === "specialAddressResponse") { + try { + basiccardResponseData.initData( + "Bill A. Pacheco", // cardholderName + "4916855166538720", // cardNumber + "01", // expiryMonth + "2024", // expiryYear + "180", // cardSecurityCode + specialAddress + ); // billingAddress + } catch (e) { + emitTestFail("Fail to initialize basic card response data."); + } + } + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + basiccardResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +const DummyUIService = { + testName: "", + showAction: "", + showPayment: showRequest, + abortPayment: abortPaymentResponse, + completePayment: completePaymentResponse, + updatePayment: requestId => { + try { + basiccardResponseData.initData( + "Bill A. Pacheco", // cardholderName + "4916855166538720", // cardNumber + "01", // expiryMonth + "2024", // expiryYear + "180", // cardSecurityCode + billingAddress + ); // billingAddress + } catch (e) { + emitTestFail("Fail to initialize basic card response data."); + } + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + basiccardResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + closePayment: requestId => {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("set-detailed-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.showAction = "detailBasicCardResponse"; + sendAsyncMessage("set-detailed-ui-service-complete"); +}); + +addMessageListener("set-simple-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.showAction = "simpleBasicCardResponse"; + sendAsyncMessage("set-simple-ui-service-complete"); +}); + +addMessageListener("set-special-address-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.showAction = "specialAddressResponse"; + sendAsyncMessage("set-special-address-ui-service-complete"); +}); + +addMessageListener("method-change-to-basic-card", function(testName) { + DummyUIService.testName = testName; + DummyUIService.showAction = "payment-method-change"; + sendAsyncMessage("method-change-to-basic-card-complete"); +}); + +addMessageListener("error-response-test", function(testName) { + // test empty cardNumber + try { + basiccardResponseData.initData("", "", "", "", "", null); + emitTestFail( + "BasicCardResponse should not be initialized with empty cardNumber." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "Empty cardNumber expected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + + // test invalid expiryMonth 123 + try { + basiccardResponseData.initData("", "4916855166538720", "123", "", "", null); + emitTestFail( + "BasicCardResponse should not be initialized with invalid expiryMonth '123'." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "expiryMonth 123 expected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + // test invalid expiryMonth 99 + try { + basiccardResponseData.initData("", "4916855166538720", "99", "", "", null); + emitTestFail( + "BasicCardResponse should not be initialized with invalid expiryMonth '99'." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "expiryMonth 99 xpected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + // test invalid expiryMonth ab + try { + basiccardResponseData.initData("", "4916855166538720", "ab", "", "", null); + emitTestFail( + "BasicCardResponse should not be initialized with invalid expiryMonth 'ab'." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "expiryMonth ab expected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + // test invalid expiryYear abcd + try { + basiccardResponseData.initData( + "", + "4916855166538720", + "", + "abcd", + "", + null + ); + emitTestFail( + "BasicCardResponse should not be initialized with invalid expiryYear 'abcd'." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "expiryYear abcd expected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + // test invalid expiryYear 11111 + try { + basiccardResponseData.initData( + "", + "4916855166538720", + "", + "11111", + "", + null + ); + emitTestFail( + "BasicCardResponse should not be initialized with invalid expiryYear '11111'." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "expiryYear 11111 expected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + try { + responseData.initData({}); + } catch (e) { + emitTestFail("Fail to initialize response data with empty object."); + } + + try { + showResponse.init( + "testid", + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + responseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + emitTestFail( + "nsIPaymentShowActionResponse should not be initialized with basic-card method and nsIGeneralResponseData." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "ShowResponse init expected 'NS_ERROR_FAILURE', but got " + e.name + "." + ); + } + } + sendAsyncMessage("error-response-test-complete"); +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/Bug1478740ChromeScript.js b/dom/payments/test/Bug1478740ChromeScript.js new file mode 100644 index 0000000000..e36905abdc --- /dev/null +++ b/dom/payments/test/Bug1478740ChromeScript.js @@ -0,0 +1,90 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", message); +} + +function rejectPayment(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "", // payment method + responseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +const DummyUIService = { + testName: "", + requestId: "", + showPayment(requestId) { + this.requestId = requestId; + sendAsyncMessage("showing-payment", { data: "successful" }); + }, + abortPayment(requestId) { + this.requestId = requestId; + }, + completePayment(requestId) { + this.requestId = requestId; + }, + updatePayment(requestId) { + this.requestId = requestId; + }, + closePayment(requestId) { + this.requestId = requestId; + }, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("reject-payment", function() { + rejectPayment(DummyUIService.requestId); + sendAsyncMessage("reject-payment-complete"); +}); + +addMessageListener("start-test", function(testName) { + DummyUIService.testName = testName; + sendAsyncMessage("start-test-complete"); +}); + +addMessageListener("finish-test", function() { + DummyUIService.testName = ""; + sendAsyncMessage("finish-test-complete"); +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/Bug1490698ChromeScript.js b/dom/payments/test/Bug1490698ChromeScript.js new file mode 100644 index 0000000000..d0f64fd97e --- /dev/null +++ b/dom/payments/test/Bug1490698ChromeScript.js @@ -0,0 +1,226 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", message); +} + +const billingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +address.data = "Easton Ave"; +addressLine.appendElement(address); +billingAddress.init( + "USA", // country + addressLine, // address line + "CA", // region + "CA", // region code + "San Bruno", // city + "", // dependent locality + "94066", // postal code + "123456", // sorting code + "", // organization + "Bill A. Pacheco", // recipient + "+14344413879" +); // phone + +function acceptPayment(requestId) { + const basiccardResponseData = Cc[ + "@mozilla.org/dom/payments/basiccard-response-data;1" + ].createInstance(Ci.nsIBasicCardResponseData); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + basiccardResponseData.initData( + "Bill A. Pacheco", // cardholderName + "4916855166538720", // cardNumber + "01", // expiryMonth + "2024", // expiryYear + "180", // cardSecurityCode + billingAddress + ); // billingAddress + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + basiccardResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function rejectPayment(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "", // payment method + responseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +const DummyUIService = { + testName: "", + requestId: "", + showPayment(requestId) { + this.requestId = requestId; + acceptPayment(requestId); + }, + abortPaymen(requestId) { + this.requestId = requestId; + }, + completePayment(requestId) { + this.requestId = requestId; + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + updatePayment(requestId) { + this.requestId = requestId; + }, + closePayment(requestId) { + this.requestId = requestId; + }, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("start-test", function(testName) { + DummyUIService.testName = testName; + sendAsyncMessage("start-test-complete"); +}); + +addMessageListener("finish-test", function() { + DummyUIService.testName = ""; + sendAsyncMessage("finish-test-complete"); +}); + +addMessageListener("interact-with-payment", function() { + if (DummyUIService.requestId === "") { + emitTestFail(`${DummyUIService.testName}: Unexpected empty requestId`); + } + try { + acceptPayment(DummyUIService.requestId); + emitTestFail( + `${DummyUIService.testName}: Got unexpected success when accepting PaymentRequest.` + ); + } catch (err) { + if (err.name !== "NS_ERROR_FAILURE") { + emitTestFail( + `${DummyUIService.testName}: Got unexpected '${err.name}' when accepting PaymentRequest.` + ); + } else { + emitTestPass( + `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when accepting PaymentRequest.` + ); + } + } + + try { + rejectPayment(DummyUIService.requestId); + emitTestFail( + `${DummyUIService.testName}: Got unexpected success when rejecting PaymentRequest.` + ); + } catch (err) { + if (err.name !== "NS_ERROR_FAILURE") { + emitTestFail( + `${DummyUIService.testName}: Got unexpected '${err.name}' when rejecting PaymentRequest.` + ); + } else { + emitTestPass( + `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when rejecting PaymentRequest.` + ); + } + } + + try { + paymentSrv.changeShippingOption( + DummyUIService.requestId, + "error shippping option" + ); + emitTestFail( + `${DummyUIService.testName}: Got unexpected success when changing shippingOption.` + ); + } catch (err) { + if (err.name !== "NS_ERROR_FAILURE") { + emitTestFail( + `${DummyUIService.testName}: Got unexpected '${err.name}' when changin shippingOption.` + ); + } else { + emitTestPass( + `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when changing shippingOption.` + ); + } + } + + try { + paymentSrv.changeShippingOption(DummyUIService.requestId, billingAddress); + emitTestFail( + `${DummyUIService.testName}: Got unexpected success when changing shippingAddress.` + ); + } catch (err) { + if (err.name !== "NS_ERROR_FAILURE") { + emitTestFail( + `${DummyUIService.testName}: Got unexpected '${err.name}' when changing shippingAddress.` + ); + } else { + emitTestPass( + `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when changing shippingAddress.` + ); + } + } + sendAsyncMessage("interact-with-payment-complete"); +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/ClosePaymentChromeScript.js b/dom/payments/test/ClosePaymentChromeScript.js new file mode 100644 index 0000000000..58e26fab75 --- /dev/null +++ b/dom/payments/test/ClosePaymentChromeScript.js @@ -0,0 +1,160 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", `${DummyUIService.testName}: ${message}`); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", `${DummyUIService.testName}: ${message}`); +} + +addMessageListener("close-check", function() { + const paymentEnum = paymentSrv.enumerate(); + if (paymentEnum.hasMoreElements()) { + emitTestFail("Non-empty PaymentRequest queue in PaymentRequestService."); + } else { + emitTestPass("Got empty PaymentRequest queue in PaymentRequestService."); + } + sendAsyncMessage("close-check-complete"); +}); + +var setPaymentNums = 0; + +addMessageListener("payment-num-set", function() { + setPaymentNums = 0; + const paymentEnum = paymentSrv.enumerate(); + while (paymentEnum.hasMoreElements()) { + setPaymentNums = setPaymentNums + 1; + paymentEnum.getNext(); + } + sendAsyncMessage("payment-num-set-complete"); +}); + +addMessageListener("payment-num-check", function(expectedNumPayments) { + const paymentEnum = paymentSrv.enumerate(); + let numPayments = 0; + while (paymentEnum.hasMoreElements()) { + numPayments = numPayments + 1; + paymentEnum.getNext(); + } + if (numPayments !== expectedNumPayments + setPaymentNums) { + emitTestFail( + "Expected '" + + expectedNumPayments + + "' PaymentRequests in PaymentRequestService" + + ", but got '" + + numPayments + + "'." + ); + } else { + emitTestPass( + "Got expected '" + + numPayments + + "' PaymentRequests in PaymentRequestService." + ); + } + // force cleanup PaymentRequests for clear environment to next testcase. + paymentSrv.cleanup(); + sendAsyncMessage("payment-num-check-complete"); +}); + +addMessageListener("test-setup", testName => { + DummyUIService.testName = testName; + sendAsyncMessage("test-setup-complete"); +}); + +addMessageListener("reject-payment", expectedError => { + try { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + DummyUIService.respondRequestId, + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "", // payment method + responseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + emitTestPass("Reject PaymentRequest successfully"); + } catch (error) { + if (expectedError) { + if (error.name === "NS_ERROR_FAILURE") { + emitTestPass( + "Got expected NS_ERROR_FAILURE when responding a closed PaymentRequest" + ); + sendAsyncMessage("reject-payment-complete"); + return; + } + } + emitTestFail( + "Unexpected error '" + + error.name + + "' when reponding a closed PaymentRequest" + ); + } + sendAsyncMessage("reject-payment-complete"); +}); + +addMessageListener("update-payment", () => { + try { + paymentSrv.changeShippingOption(DummyUIService.respondRequestId, ""); + emitTestPass("Change shippingOption succefully"); + } catch (error) { + emitTestFail( + "Unexpected error '" + error.name + "' when changing the shipping option" + ); + } + sendAsyncMessage("update-payment-complete"); +}); + +const DummyUIService = { + testName: "", + respondRequestId: "", + showPayment: requestId => { + DummyUIService.respondRequestId = requestId; + }, + abortPayment: requestId => { + DummyUIService.respondRequestId = requestId; + }, + completePayment: requestId => { + DummyUIService.respondRequestId = requestId; + }, + updatePayment: requestId => { + DummyUIService.respondRequestId = requestId; + }, + closePayment: requestId => { + this.respondRequestId = requestId; + }, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/ConstructorChromeScript.js b/dom/payments/test/ConstructorChromeScript.js new file mode 100644 index 0000000000..2c48470350 --- /dev/null +++ b/dom/payments/test/ConstructorChromeScript.js @@ -0,0 +1,490 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} + +function checkSimplestRequest(payRequest) { + if (payRequest.topLevelPrincipal.origin != "https://example.com") { + emitTestFail( + "Top level principal's Origin should be 'https://example.com', but got '" + + payRequest.topLevelPrincipal.origin + + "'." + ); + } + + if (payRequest.paymentMethods.length != 1) { + emitTestFail("paymentMethods' length should be 1."); + } + + const methodData = payRequest.paymentMethods.queryElementAt( + 0, + Ci.nsIPaymentMethodData + ); + if (!methodData) { + emitTestFail("Fail to get payment methodData."); + } + const supportedMethod = methodData.supportedMethods; + if (supportedMethod != "basic-card") { + emitTestFail("supported method should be 'basic-card'."); + } + if (methodData.data) { + emitTestFail("methodData.data should not exist."); + } + + // checking the passed PaymentDetails parameter + const details = payRequest.paymentDetails; + if (details.totalItem.label != "Total") { + emitTestFail("total item's label should be 'Total'."); + } + if (details.totalItem.amount.currency != "USD") { + emitTestFail("total item's currency should be 'USD'."); + } + if (details.totalItem.amount.value != "1.00") { + emitTestFail("total item's value should be '1.00'."); + } + + if (details.displayItems.length !== 0) { + emitTestFail("details.displayItems should be an empty array."); + } + if (details.modifiers.length !== 0) { + emitTestFail("details.modifiers should be an empty array."); + } + if (details.shippingOptions.length !== 0) { + emitTestFail("details.shippingOptions should be an empty array."); + } + + // checking the default generated PaymentOptions parameter + const paymentOptions = payRequest.paymentOptions; + if (paymentOptions.requestPayerName) { + emitTestFail("requestPayerName option should be false."); + } + if (paymentOptions.requestPayerEmail) { + emitTestFail("requestPayerEmail option should be false."); + } + if (paymentOptions.requestPayerPhone) { + emitTestFail("requestPayerPhone option should be false."); + } + if (paymentOptions.requestShipping) { + emitTestFail("requestShipping option should be false."); + } + if (paymentOptions.shippingType != "shipping") { + emitTestFail("shippingType option should be 'shipping'."); + } +} + +// eslint-disable-next-line complexity +function checkComplexRequest(payRequest) { + if (payRequest.topLevelPrincipal.origin != "https://example.com") { + emitTestFail( + "Top level principal's origin should be 'https://example.com', but got '" + + payRequest.topLevelPrincipal.origin + + "'." + ); + } + + if (payRequest.paymentMethods.length != 1) { + emitTestFail("paymentMethods' length should be 1."); + } + + const methodData = payRequest.paymentMethods.queryElementAt( + 0, + Ci.nsIPaymentMethodData + ); + if (!methodData) { + emitTestFail("Fail to get payment methodData."); + } + let supportedMethod = methodData.supportedMethods; + if (supportedMethod != "basic-card") { + emitTestFail("supported method should be 'basic-card'."); + } + const data = methodData.data; + const supportedNetworks = data.supportedNetworks; + const expectedSupportedNetworks = [ + "unionpay", + "visa", + "mastercard", + "amex", + "discover", + "diners", + "jcb", + "mir", + ]; + if (supportedNetworks.length != expectedSupportedNetworks.length) { + emitTestFail( + "supportedNetworks.length should be " + + expectedSupportedNetworks.length + + ", but got " + + supportedNetworks.length + + "." + ); + } + for (let idx = 0; idx < supportedNetworks.length; idx++) { + if (supportedNetworks[idx] != expectedSupportedNetworks[idx]) { + emitTestFail( + "supportedNetworks[" + + idx + + "] should be '" + + expectedSupportedNetworks[idx] + + "', but got '" + + supportedNetworks[idx] + + "'." + ); + } + } + // checking the passed PaymentDetails parameter + const details = payRequest.paymentDetails; + if (details.id != "payment details") { + emitTestFail("details.id should be 'payment details'."); + } + if (details.totalItem.label != "Total") { + emitTestFail("total item's label should be 'Total'."); + } + if (details.totalItem.amount.currency != "USD") { + emitTestFail("total item's currency should be 'USD'."); + } + if (details.totalItem.amount.value != "100.00") { + emitTestFail("total item's value should be '100.00'."); + } + + const displayItems = details.displayItems; + if (!details.displayItems) { + emitTestFail("details.displayItems should not be undefined."); + } + if (displayItems.length != 2) { + emitTestFail("displayItems' length should be 2."); + } + let item = displayItems.queryElementAt(0, Ci.nsIPaymentItem); + if (item.label != "First item") { + emitTestFail("1st display item's label should be 'First item'."); + } + if (item.amount.currency != "USD") { + emitTestFail("1st display item's currency should be 'USD'."); + } + if (item.amount.value != "60.00") { + emitTestFail("1st display item's value should be '60.00'."); + } + item = displayItems.queryElementAt(1, Ci.nsIPaymentItem); + if (item.label != "Second item") { + emitTestFail("2nd display item's label should be 'Second item'."); + } + if (item.amount.currency != "USD") { + emitTestFail("2nd display item's currency should be 'USD'."); + } + if (item.amount.value != "40.00") { + emitTestFail("2nd display item's value should be '40.00'."); + } + + const modifiers = details.modifiers; + if (!modifiers) { + emitTestFail("details.displayItems should not be undefined."); + } + if (modifiers.length != 1) { + emitTestFail("modifiers' length should be 1."); + } + const modifier = modifiers.queryElementAt(0, Ci.nsIPaymentDetailsModifier); + const supportedMethods = modifier.supportedMethods; + if (supportedMethod != "basic-card") { + emitTestFail("modifier's supported method name should be 'basic-card'."); + } + if (modifier.total.label != "Discounted Total") { + emitTestFail("modifier's total label should be 'Discounted Total'."); + } + if (modifier.total.amount.currency != "USD") { + emitTestFail("modifier's total currency should be 'USD'."); + } + if (modifier.total.amount.value != "90.00") { + emitTestFail("modifier's total value should be '90.00'."); + } + + const additionalItems = modifier.additionalDisplayItems; + if (additionalItems.length != 1) { + emitTestFail("additionalDisplayItems' length should be 1."); + } + const additionalItem = additionalItems.queryElementAt(0, Ci.nsIPaymentItem); + if (additionalItem.label != "basic-card discount") { + emitTestFail("additional item's label should be 'basic-card discount'."); + } + if (additionalItem.amount.currency != "USD") { + emitTestFail("additional item's currency should be 'USD'."); + } + if (additionalItem.amount.value != "-10.00") { + emitTestFail("additional item's value should be '-10.00'."); + } + if (modifier.data.discountProgramParticipantId != "86328764873265") { + emitTestFail( + "modifier's data should be '86328764873265', but got '" + + modifier.data.discountProgramParticipantId + + "'." + ); + } + + const shippingOptions = details.shippingOptions; + if (!shippingOptions) { + emitTestFail("details.shippingOptions should not be undefined."); + } + if (shippingOptions.length != 2) { + emitTestFail("shippingOptions' length should be 2."); + } + let shippingOption = shippingOptions.queryElementAt( + 0, + Ci.nsIPaymentShippingOption + ); + if (shippingOption.id != "NormalShipping") { + emitTestFail("1st shippingOption's id should be 'NormalShipping'."); + } + if (shippingOption.label != "NormalShipping") { + emitTestFail("1st shippingOption's lable should be 'NormalShipping'."); + } + if (shippingOption.amount.currency != "USD") { + emitTestFail("1st shippingOption's amount currency should be 'USD'."); + } + if (shippingOption.amount.value != "10.00") { + emitTestFail("1st shippingOption's amount value should be '10.00'."); + } + if (!shippingOption.selected) { + emitTestFail("1st shippingOption should be selected."); + } + shippingOption = shippingOptions.queryElementAt( + 1, + Ci.nsIPaymentShippingOption + ); + if (shippingOption.id != "FastShipping") { + emitTestFail("2nd shippingOption's id should be 'FastShipping'."); + } + if (shippingOption.label != "FastShipping") { + emitTestFail("2nd shippingOption's lable should be 'FastShipping'."); + } + if (shippingOption.amount.currency != "USD") { + emitTestFail("2nd shippingOption's amount currency should be 'USD'."); + } + if (shippingOption.amount.value != "30.00") { + emitTestFail("2nd shippingOption's amount value should be '30.00'."); + } + if (shippingOption.selected) { + emitTestFail("2nd shippingOption should not be selected."); + } + + // checking the default generated PaymentOptions parameter + const paymentOptions = payRequest.paymentOptions; + if (!paymentOptions.requestPayerName) { + emitTestFail("requestPayerName option should be true."); + } + if (!paymentOptions.requestPayerEmail) { + emitTestFail("requestPayerEmail option should be true."); + } + if (!paymentOptions.requestPayerPhone) { + emitTestFail("requestPayerPhone option should be true."); + } + if (!paymentOptions.requestShipping) { + emitTestFail("requestShipping option should be true."); + } + if (paymentOptions.shippingType != "shipping") { + emitTestFail("shippingType option should be 'shipping'."); + } +} + +function checkNonBasicCardRequest(payRequest) { + if (payRequest.paymentMethods.length != 1) { + emitTestFail("paymentMethods' length should be 1."); + } + + const methodData = payRequest.paymentMethods.queryElementAt( + 0, + Ci.nsIPaymentMethodData + ); + if (!methodData) { + emitTestFail("Fail to get payment methodData."); + } + const supportedMethod = methodData.supportedMethods; + if (supportedMethod != "testing-payment-method") { + emitTestFail("supported method should be 'testing-payment-method'."); + } + + const paymentId = methodData.data.paymentId; + if (paymentId != "P3892940") { + emitTestFail( + "methodData.data.paymentId should be 'P3892940', but got " + + paymentId + + "." + ); + } + const paymentType = methodData.data.paymentType; + if (paymentType != "prepaid") { + emitTestFail( + "methodData.data.paymentType should be 'prepaid', but got " + + paymentType + + "." + ); + } + + // checking the passed PaymentDetails parameter + const details = payRequest.paymentDetails; + if (details.totalItem.label != "Total") { + emitTestFail("total item's label should be 'Total'."); + } + if (details.totalItem.amount.currency != "USD") { + emitTestFail("total item's currency should be 'USD'."); + } + if (details.totalItem.amount.value != "1.00") { + emitTestFail("total item's value should be '1.00'."); + } + + if (details.displayItems.length !== 0) { + emitTestFail("details.displayItems should be an zero length array."); + } + if (details.displayItems.length !== 0) { + emitTestFail("details.modifiers should be an zero length array."); + } + if (details.displayItems.length !== 0) { + emitTestFail("details.shippingOptions should be an zero length array."); + } + + // checking the default generated PaymentOptions parameter + const paymentOptions = payRequest.paymentOptions; + if (paymentOptions.requestPayerName) { + emitTestFail("requestPayerName option should be false."); + } + if (paymentOptions.requestPayerEmail) { + emitTestFail("requestPayerEmail option should be false."); + } + if (paymentOptions.requestPayerPhone) { + emitTestFail("requestPayerPhone option should be false."); + } + if (paymentOptions.requestShipping) { + emitTestFail("requestShipping option should be false."); + } + if (paymentOptions.shippingType != "shipping") { + emitTestFail("shippingType option should be 'shipping'."); + } +} + +function checkSimplestRequestHandler() { + const paymentEnum = paymentSrv.enumerate(); + if (!paymentEnum.hasMoreElements()) { + emitTestFail( + "PaymentRequestService should have at least one payment request." + ); + } + for (let payRequest of paymentEnum) { + if (!payRequest) { + emitTestFail("Fail to get existing payment request."); + break; + } + checkSimplestRequest(payRequest); + } + paymentSrv.cleanup(); + sendAsyncMessage("check-complete"); +} + +function checkComplexRequestHandler() { + const paymentEnum = paymentSrv.enumerate(); + if (!paymentEnum.hasMoreElements()) { + emitTestFail( + "PaymentRequestService should have at least one payment request." + ); + } + for (let payRequest of paymentEnum) { + if (!payRequest) { + emitTestFail("Fail to get existing payment request."); + break; + } + checkComplexRequest(payRequest); + } + paymentSrv.cleanup(); + sendAsyncMessage("check-complete"); +} + +function checkNonBasicCardRequestHandler() { + const paymentEnum = paymentSrv.enumerate(); + if (!paymentEnum.hasMoreElements()) { + emitTestFail( + "PaymentRequestService should have at least one payment request." + ); + } + for (let payRequest of paymentEnum) { + if (!payRequest) { + emitTestFail("Fail to get existing payment request."); + break; + } + checkNonBasicCardRequest(payRequest); + } + paymentSrv.cleanup(); + sendAsyncMessage("check-complete"); +} + +function checkMultipleRequestsHandler() { + const paymentEnum = paymentSrv.enumerate(); + if (!paymentEnum.hasMoreElements()) { + emitTestFail( + "PaymentRequestService should have at least one payment request." + ); + } + for (let payRequest of paymentEnum) { + if (!payRequest) { + emitTestFail("Fail to get existing payment request."); + break; + } + if (payRequest.paymentDetails.id == "payment details") { + checkComplexRequest(payRequest); + } else { + checkSimplestRequest(payRequest); + } + } + paymentSrv.cleanup(); + sendAsyncMessage("check-complete"); +} + +function checkCrossOriginTopLevelPrincipalHandler() { + const paymentEnum = paymentSrv.enumerate(); + if (!paymentEnum.hasMoreElements()) { + emitTestFail( + "PaymentRequestService should have at least one payment request." + ); + } + for (let payRequest of paymentEnum) { + if (!payRequest) { + emitTestFail("Fail to get existing payment request."); + break; + } + if (payRequest.topLevelPrincipal.origin != "https://example.com") { + emitTestFail( + "Top level principal's origin should be 'https://example.com', but got '" + + payRequest.topLevelPrincipal.origin + + "'." + ); + } + } + paymentSrv.cleanup(); + sendAsyncMessage("check-complete"); +} + +addMessageListener("check-simplest-request", checkSimplestRequestHandler); +addMessageListener("check-complex-request", checkComplexRequestHandler); +addMessageListener("check-multiple-requests", checkMultipleRequestsHandler); +addMessageListener( + "check-nonbasiccard-request", + checkNonBasicCardRequestHandler +); +addMessageListener( + "check-cross-origin-top-level-principal", + checkCrossOriginTopLevelPrincipalHandler +); + +addMessageListener("teardown", function() { + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/CurrencyAmountValidationChromeScript.js b/dom/payments/test/CurrencyAmountValidationChromeScript.js new file mode 100644 index 0000000000..a15e79be18 --- /dev/null +++ b/dom/payments/test/CurrencyAmountValidationChromeScript.js @@ -0,0 +1,67 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +const InvalidDetailsUIService = { + showPayment(requestId) { + paymentSrv.changeShippingOption(requestId, ""); + }, + abortPayment(requestId) { + const abortResponse = Cc[ + "@mozilla.org/dom/payments/payment-abort-action-response;1" + ].createInstance(Ci.nsIPaymentAbortActionResponse); + abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED); + paymentSrv.respondPayment( + abortResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + completePayment(requestId) {}, + updatePayment(requestId) {}, + closePayment(requestId) {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +function checkLowerCaseCurrency() { + const paymentEnum = paymentSrv.enumerate(); + if (!paymentEnum.hasMoreElements()) { + const msg = + "PaymentRequestService should have at least one payment request."; + sendAsyncMessage("test-fail", msg); + } + for (let payRequest of paymentEnum) { + if (!payRequest) { + sendAsyncMessage("test-fail", "Fail to get existing payment request."); + break; + } + const { currency } = payRequest.paymentDetails.totalItem.amount; + if (currency != "USD") { + const msg = + "Currency of PaymentItem total should be 'USD', but got ${currency}"; + sendAsyncMessage("check-complete"); + } + } + paymentSrv.cleanup(); + sendAsyncMessage("check-complete"); +} + +addMessageListener("check-lower-case-currency", checkLowerCaseCurrency); + +addMessageListener("set-update-with-invalid-details-ui-service", () => { + paymentSrv.setTestingUIService( + InvalidDetailsUIService.QueryInterface(Ci.nsIPaymentUIService) + ); +}); + +addMessageListener("teardown", () => sendAsyncMessage("teardown-complete")); diff --git a/dom/payments/test/DefaultData.js b/dom/payments/test/DefaultData.js new file mode 100644 index 0000000000..13723b5799 --- /dev/null +++ b/dom/payments/test/DefaultData.js @@ -0,0 +1,59 @@ +// testing data declation +const defaultMethods = [ + { + supportedMethods: "basic-card", + data: { + supportedNetworks: [ + "unionpay", + "visa", + "mastercard", + "amex", + "discover", + "diners", + "jcb", + "mir", + ], + }, + }, + { + supportedMethods: "testing-payment-method", + }, +]; + +const defaultDetails = { + total: { + label: "Total", + amount: { + currency: "USD", + value: "1.00", + }, + }, + shippingOptions: [ + { + id: "NormalShipping", + label: "NormalShipping", + amount: { + currency: "USD", + value: "10.00", + }, + selected: false, + }, + { + id: "FastShipping", + label: "FastShipping", + amount: { + currency: "USD", + value: "5.00", + }, + selected: false, + }, + ], +}; + +const defaultOptions = { + requestPayerName: true, + requestPayerEmail: false, + requestPayerPhone: false, + requestShipping: true, + shippingType: "shipping", +}; diff --git a/dom/payments/test/GeneralChromeScript.js b/dom/payments/test/GeneralChromeScript.js new file mode 100644 index 0000000000..6689c85b1f --- /dev/null +++ b/dom/payments/test/GeneralChromeScript.js @@ -0,0 +1,20 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/PMIValidationChromeScript.js b/dom/payments/test/PMIValidationChromeScript.js new file mode 100644 index 0000000000..cc4d35db77 --- /dev/null +++ b/dom/payments/test/PMIValidationChromeScript.js @@ -0,0 +1,82 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +const UIService = { + showPayment(requestId) { + paymentSrv.changeShippingOption(requestId, ""); + }, + abortPayment(requestId) { + let abortResponse = Cc[ + "@mozilla.org/dom/payments/payment-abort-action-response;1" + ].createInstance(Ci.nsIPaymentAbortActionResponse); + abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED); + paymentSrv.respondPayment( + abortResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + completePayment(requestId) { + const completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + updatePayment(requestId) { + const showResponseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + showResponseData.initData({ + paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1", + }); + + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "https://example.com", // payment method + showResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + closePayment(requestId) {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} + +addMessageListener("set-ui-service", function() { + paymentSrv.setTestingUIService( + UIService.QueryInterface(Ci.nsIPaymentUIService) + ); +}); + +addMessageListener("teardown", function() { + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/PayerDetailsChromeScript.js b/dom/payments/test/PayerDetailsChromeScript.js new file mode 100644 index 0000000000..77024cc754 --- /dev/null +++ b/dom/payments/test/PayerDetailsChromeScript.js @@ -0,0 +1,83 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +const TestingUIService = { + showPayment(requestId, name = "", email = "", phone = "") { + const showResponseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + showResponseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", // payment method + showResponseData, // payment method data + name, + email, + phone + ); + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + // .retry({ payer }) and .updateWith({payerErrors}) both get routed here: + updatePayment(requestId) { + // Let's echo what was sent in by the error... + const request = paymentSrv.getPaymentRequestById(requestId); + const { name, email, phone } = request.paymentDetails.payerErrors; + const { error } = request.paymentDetails; + // Let's use the .error as the switch + switch (error) { + case "retry-fire-payerdetaichangeevent": { + paymentSrv.changePayerDetail(requestId, name, email, phone); + break; + } + case "update-with": { + this.showPayment(requestId, name, email, phone); + break; + } + default: + const msg = `Expect details.error value: '${error}'`; + sendAsyncMessage("test-fail", msg); + } + }, + completePayment(requestId) { + const request = paymentSrv.getPaymentRequestById(requestId); + const completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + get QueryInterface() { + return ChromeUtils.generateQI(["nsIPaymentUIService"]); + }, +}; + +paymentSrv.setTestingUIService( + TestingUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("teardown", () => { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/RequestShippingChromeScript.js b/dom/payments/test/RequestShippingChromeScript.js new file mode 100644 index 0000000000..12c7f5477d --- /dev/null +++ b/dom/payments/test/RequestShippingChromeScript.js @@ -0,0 +1,116 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} + +const shippingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +address.data = "Easton Ave"; +addressLine.appendElement(address); +shippingAddress.init( + "", // country + addressLine, // address line + "", // region + "", // region code + "", // city + "", // dependent locality + "", // postal code + "", // sorting code + "", // organization + "", // recipient + "" +); // phone + +const NormalUIService = { + shippingOptionChanged: false, + showPayment(requestId) { + paymentSrv.changeShippingAddress(requestId, shippingAddress); + }, + abortPayment(requestId) {}, + completePayment(requestId) { + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + updatePayment(requestId) { + let showResponse = null; + let payRequest = paymentSrv.getPaymentRequestById(requestId); + + const shippingOptions = payRequest.paymentDetails.shippingOptions; + if (shippingOptions.length) { + emitTestFail("Wrong length for shippingOptions."); + } + + const showResponseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + + try { + showResponseData.initData({ + paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1", + }); + } catch (e) { + emitTestFail( + 'Fail to initialize response data with { paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1",}' + ); + } + + showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", // payment method + showResponseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + closePayment(requestId) {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +addMessageListener("set-normal-ui-service", function() { + paymentSrv.setTestingUIService( + NormalUIService.QueryInterface(Ci.nsIPaymentUIService) + ); +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/RetryPaymentChromeScript.js b/dom/payments/test/RetryPaymentChromeScript.js new file mode 100644 index 0000000000..aa944e779d --- /dev/null +++ b/dom/payments/test/RetryPaymentChromeScript.js @@ -0,0 +1,238 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", message); +} + +const billingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +address.data = "Easton Ave"; +addressLine.appendElement(address); +billingAddress.init( + "USA", // country + addressLine, // address line + "CA", // region + "CA", // region code + "San Bruno", // city + "", // dependent locality + "94066", // postal code + "123456", // sorting code + "", // organization + "Bill A. Pacheco", // recipient + "+14344413879" +); // phone + +function acceptPayment(requestId, mode) { + const basiccardResponseData = Cc[ + "@mozilla.org/dom/payments/basiccard-response-data;1" + ].createInstance(Ci.nsIBasicCardResponseData); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + basiccardResponseData.initData( + "Bill A. Pacheco", // cardholderName + "4916855166538720", // cardNumber + "01", // expiryMonth + "2024", // expiryYear + "180", // cardSecurityCode + billingAddress + ); // billingAddress + if (mode === "show") { + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + basiccardResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + } + if (mode == "retry") { + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "basic-card", // payment method + basiccardResponseData, // payment method data + "Bill A. Pacheco", // payer name + "bpacheco@test.org", // payer email + "+123456789" + ); // payer phone + } + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function rejectPayment(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "", // payment method + responseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function checkAddressErrors(testName, errors) { + if (!errors) { + emitTestFail( + `${testName}: Expect non-null shippingAddressErrors, but got null.` + ); + return; + } + for (const [key, msg] of Object.entries(errors)) { + const expected = `${key} error`; + if (msg !== expected) { + emitTestFail( + `${testName}: Expected '${expected}' on shippingAddressErrors.${key}, but got '${msg}'.` + ); + return; + } + } +} + +function checkPayerErrors(testName, errors) { + if (!errors) { + emitTestFail(`${testName}: Expect non-null payerErrors, but got null.`); + return; + } + for (const [key, msg] of Object.entries(errors)) { + const expected = `${key} error`; + if (msg !== expected) { + emitTestFail( + `${testName}: Expected '${expected}' on payerErrors.${key}, but got '${msg}'.` + ); + return; + } + } +} + +function checkPaymentMethodErrors(testName, errors) { + if (!errors) { + emitTestFail( + `${testName} :Expect non-null paymentMethodErrors, but got null.` + ); + return; + } + for (const [key, msg] of Object.entries(errors)) { + const expected = `method ${key} error`; + if (msg !== expected) { + emitTestFail( + `${testName}: Expected '${expected}' on paymentMethodErrors.${key}, but got '${msg}'.` + ); + return; + } + } +} + +const DummyUIService = { + testName: "", + rejectRetry: false, + showPayment(requestId) { + acceptPayment(requestId, "show"); + }, + abortPaymen(requestId) { + respondRequestId = requestId; + }, + completePayment(requestId) { + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + updatePayment(requestId) { + const payment = paymentSrv.getPaymentRequestById(requestId); + if (payment.paymentDetails.error !== "error") { + emitTestFail( + "Expect 'error' on details.error, but got '" + + payment.paymentDetails.error + + "'" + ); + } + checkAddressErrors( + this.testName, + payment.paymentDetails.shippingAddressErrors + ); + checkPayerErrors(this.testName, payment.paymentDetails.payerErrors); + checkPaymentMethodErrors( + this.testName, + payment.paymentDetails.paymentMethodErrors + ); + if (this.rejectRetry) { + rejectPayment(requestId); + } else { + acceptPayment(requestId, "retry"); + } + }, + closePayment: requestId => { + respondRequestId = requestId; + }, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("start-test", function(testName) { + DummyUIService.testName = testName; + sendAsyncMessage("start-test-complete"); +}); + +addMessageListener("finish-test", function() { + DummyUIService.testName = ""; + sendAsyncMessage("finish-test-complete"); +}); + +addMessageListener("reject-retry", function() { + DummyUIService.rejectRetry = true; + sendAsyncMessage("reject-retry-complete"); +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/ShippingOptionsChromeScript.js b/dom/payments/test/ShippingOptionsChromeScript.js new file mode 100644 index 0000000000..0d40cd1a16 --- /dev/null +++ b/dom/payments/test/ShippingOptionsChromeScript.js @@ -0,0 +1,120 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", message); +} + +let expectedRequestOption = null; +let expectedUpdatedOption = null; +let changeShippingOption = null; + +function showResponse(requestId) { + const showResponseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + showResponseData.initData({}); + const showActionResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showActionResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", // payment method + showResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showActionResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function showRequest(requestId) { + let request = paymentSrv.getPaymentRequestById(requestId); + const message = + "request.shippingOption should be " + + expectedRequestOption + + " when calling show(), but got " + + request.shippingOption + + "."; + if (request.shippingOption != expectedRequestOption) { + emitTestFail(message); + } else { + emitTestPass(message); + } + if (changeShippingOption) { + paymentSrv.changeShippingOption(requestId, changeShippingOption); + } else { + showResponse(requestId); + } +} + +function updateRequest(requestId) { + let request = paymentSrv.getPaymentRequestById(requestId); + const message = + "request.shippingOption should be " + + expectedUpdatedOption + + " when calling updateWith(), but got " + + request.shippingOption + + "."; + if (request.shippingOption != expectedUpdatedOption) { + emitTestFail(message); + } else { + emitTestPass(message); + } + showResponse(requestId); +} + +const TestingUIService = { + showPayment: showRequest, + abortPayment(requestId) {}, + completePayment(requestId) { + let request = paymentSrv.getPaymentRequestById(requestId); + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); + }, + updatePayment: updateRequest, + closePayment(requestId) {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + TestingUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("set-expected-results", function(results) { + expectedRequestOption = results.requestResult; + expectedUpdatedOption = results.responseResult; + changeShippingOption = results.changeOptionResult; +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/ShowPaymentChromeScript.js b/dom/payments/test/ShowPaymentChromeScript.js new file mode 100644 index 0000000000..757dd7e191 --- /dev/null +++ b/dom/payments/test/ShowPaymentChromeScript.js @@ -0,0 +1,393 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", `${DummyUIService.testName}: ${message}`); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", `${DummyUIService.testName}: ${message}`); +} + +const shippingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +address.data = "Easton Ave"; +addressLine.appendElement(address); +shippingAddress.init( + "USA", // country + addressLine, // address line + "CA", // region + "CA", // region code + "San Bruno", // city + "Test locality", // dependent locality + "94066", // postal code + "123456", // sorting code + "Testing Org", // organization + "Bill A. Pacheco", // recipient + "+1-434-441-3879" +); // phone + +function acceptShow(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({ + paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1", + }); + let showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", // payment method + responseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function rejectShow(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "", // payment method + responseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function updateShow(requestId) { + if (DummyUIService.expectedUpdateAction == "updateaddress") { + paymentSrv.changeShippingAddress(requestId, shippingAddress); + } else if ( + DummyUIService.expectedUpdateAction == "accept" || + DummyUIService.expectedUpdateAction == "error" + ) { + paymentSrv.changeShippingOption(requestId, "FastShipping"); + } else { + emitTestFail( + "Unknown expected update action: " + DummyUIService.expectedUpdateAction + ); + } +} + +function showRequest(requestId) { + const request = paymentSrv.getPaymentRequestById(requestId); + if (request.completeStatus == "initial") { + return; + } + if (DummyUIService.expectedShowAction == "accept") { + acceptShow(requestId); + } else if (DummyUIService.expectedShowAction == "reject") { + rejectShow(requestId); + } else if (DummyUIService.expectedShowAction == "update") { + updateShow(requestId); + } else { + emitTestFail( + "Unknown expected show action: " + DummyUIService.expectedShowAction + ); + } +} + +function abortRequest(requestId) { + let abortResponse = Cc[ + "@mozilla.org/dom/payments/payment-abort-action-response;1" + ].createInstance(Ci.nsIPaymentAbortActionResponse); + abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED); + paymentSrv.respondPayment(abortResponse); +} + +function completeRequest(requestId) { + let request = paymentSrv.getPaymentRequestById(requestId); + if (DummyUIService.expectedCompleteStatus) { + if (request.completeStatus == DummyUIService.expectedCompleteStatus) { + emitTestPass( + "request.completeStatus matches expectation of " + + DummyUIService.expectedCompleteStatus + ); + } else { + emitTestFail( + "request.completeStatus incorrect. Expected " + + DummyUIService.expectedCompleteStatus + + ", got " + + request.completeStatus + ); + } + } + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function updateRequest(requestId) { + let request = paymentSrv.getPaymentRequestById(requestId); + if (request.completeStatus !== "") { + emitTestFail( + "request.completeStatus should be empty, but got '" + + request.completeStatus + + "'." + ); + } + if (DummyUIService.expectedUpdateAction == "accept") { + if (request.paymentDetails.error != "") { + emitTestFail( + "updatedDetails should not have errors(" + + request.paymentDetails.error + + ")." + ); + } + const shippingOptions = request.paymentDetails.shippingOptions; + let shippingOption = shippingOptions.queryElementAt( + 0, + Ci.nsIPaymentShippingOption + ); + if (shippingOption.selected) { + emitTestFail(shippingOption.label + " should not be selected."); + } + shippingOption = shippingOptions.queryElementAt( + 1, + Ci.nsIPaymentShippingOption + ); + if (!shippingOption.selected) { + emitTestFail(shippingOption.label + " should be selected."); + } + acceptShow(requestId); + } else if (DummyUIService.expectedUpdateAction == "error") { + if (request.paymentDetails.error != "Update with Error") { + emitTestFail( + "details.error should be 'Update with Error', but got " + + request.paymentDetails.error + + "." + ); + } + rejectShow(requestId); + } else if (DummyUIService.expectedUpdateAction == "updateaddress") { + if (request.paymentDetails.error != "") { + emitTestFail( + "updatedDetails should not have errors(" + + request.paymentDetails.error + + ")." + ); + } + DummyUIService.expectedUpdateAction = "accept"; + paymentSrv.changeShippingOption(requestId, "FastShipping"); + } else { + emitTestFail( + "Unknown expected update aciton: " + DummyUIService.expectedUpdateAction + ); + } +} + +const DummyUIService = { + testName: "", + expectedCompleteStatus: null, + expectedShowAction: "accept", + expectedUpdateAction: "accept", + showPayment: showRequest, + abortPayment: abortRequest, + completePayment: completeRequest, + updatePayment: updateRequest, + closePayment(requestId) {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +function testShowResponseInit() { + const showResponseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + try { + showResponseData.initData(null); + emitTestFail( + "nsIGeneralResponseData can not be initialized with null object." + ); + } catch (e) { + if (e.name != "NS_ERROR_FAILURE") { + emitTestFail( + "Expected 'NS_ERROR_FAILURE' when initializing nsIGeneralResponseData with null object, but got " + + e.name + + "." + ); + } + emitTestPass( + "Get expected result for initializing nsIGeneralResponseData with null object" + ); + } + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + try { + showResponse.init( + "test request id", + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", // payment method + showResponseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + emitTestPass( + "Get expected result for initializing response with accepted and empty data." + ); + } catch (e) { + emitTestFail( + "Unexpected error " + + e.name + + " when initializing response with accepted and empty data." + ); + } + + try { + showResponse.init( + "test request id", + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "testing-payment-method", + null, + "Bill A. Pacheco", + "", + "" + ); + emitTestPass( + "Get expected result for initializing response with rejected and null data." + ); + } catch (e) { + emitTestFail( + "Unexpected error " + + e.name + + " when initializing response with rejected and null data." + ); + } + + try { + showResponse.init( + "test request id", + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", + null, + "Bill A. Pacheco", + "", + "" + ); + emitTestFail( + "nsIPaymentShowActionResponse can not be initialized with accpeted and null data." + ); + } catch (e) { + if (e.name != "NS_ERROR_ILLEGAL_VALUE") { + emitTestFail( + "Expected 'NS_ERROR_ILLEGAL_VALUE', but got " + e.name + "." + ); + } + emitTestPass( + "Get expected result for initializing response with accepted and null data." + ); + } + sendAsyncMessage("test-show-response-init-complete"); +} + +addMessageListener("set-simple-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.expectedCompleteStatus = null; + DummyUIService.expectedShowAction = "accept"; + DummyUIService.expectedUpdateAction = "accept"; + sendAsyncMessage("set-simple-ui-service-complete"); +}); + +addMessageListener("set-normal-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.expectedCompleteStatus = null; + DummyUIService.expectedShowAction = "update"; + DummyUIService.expectedUpdateAction = "updateaddress"; + sendAsyncMessage("set-normal-ui-service-complete"); +}); + +addMessageListener("set-reject-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.expectedCompleteStatus = null; + DummyUIService.expectedShowAction = "reject"; + DummyUIService.expectedUpdateAction = "error"; + sendAsyncMessage("set-reject-ui-service-complete"); +}); + +addMessageListener("set-update-with-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.expectedCompleteStatus = null; + DummyUIService.expectedShowAction = "update"; + DummyUIService.expectedUpdateAction = "accept"; + sendAsyncMessage("set-update-with-ui-service-complete"); +}); + +addMessageListener("set-update-with-error-ui-service", function(testName) { + DummyUIService.testName = testName; + DummyUIService.expectedCompleteStatus = null; + DummyUIService.expectedShowAction = "update"; + DummyUIService.expectedUpdateAction = "error"; + sendAsyncMessage("set-update-with-error-ui-service-complete"); +}); + +addMessageListener("test-show-response-init", testShowResponseInit); + +addMessageListener("set-complete-status-success", function() { + DummyUIService.expectedCompleteStatus = "success"; + sendAsyncMessage("set-complete-status-success-complete"); +}); + +addMessageListener("set-complete-status-fail", function() { + DummyUIService.expectedCompleteStatus = "fail"; + sendAsyncMessage("set-complete-status-fail-complete"); +}); + +addMessageListener("set-complete-status-unknown", function() { + DummyUIService.expectedCompleteStatus = "unknown"; + sendAsyncMessage("set-complete-status-unknown-complete"); +}); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/UpdateErrorsChromeScript.js b/dom/payments/test/UpdateErrorsChromeScript.js new file mode 100644 index 0000000000..fe802e3428 --- /dev/null +++ b/dom/payments/test/UpdateErrorsChromeScript.js @@ -0,0 +1,214 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env mozilla/chrome-script */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" +].getService(Ci.nsIPaymentRequestService); + +function emitTestFail(message) { + sendAsyncMessage("test-fail", message); +} +function emitTestPass(message) { + sendAsyncMessage("test-pass", message); +} + +const shippingAddress = Cc[ + "@mozilla.org/dom/payments/payment-address;1" +].createInstance(Ci.nsIPaymentAddress); +const addressLine = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray +); +const address = Cc["@mozilla.org/supports-string;1"].createInstance( + Ci.nsISupportsString +); +address.data = "Easton Ave"; +addressLine.appendElement(address); +shippingAddress.init( + "USA", // country + addressLine, // address line + "CA", // region + "CA", // region code + "San Bruno", // city + "Test locality", // dependent locality + "94066", // postal code + "123456", // sorting code + "Testing Org", // organization + "Bill A. Pacheco", // recipient + "+1-434-441-3879" +); // phone + +function acceptShow(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({ + paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1", + }); + let showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, + "testing-payment-method", // payment method + responseData, // payment method data + "Bill A. Pacheco", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function rejectShow(requestId) { + const responseData = Cc[ + "@mozilla.org/dom/payments/general-response-data;1" + ].createInstance(Ci.nsIGeneralResponseData); + responseData.initData({}); + const showResponse = Cc[ + "@mozilla.org/dom/payments/payment-show-action-response;1" + ].createInstance(Ci.nsIPaymentShowActionResponse); + showResponse.init( + requestId, + Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, + "", // payment method + responseData, // payment method data + "", // payer name + "", // payer email + "" + ); // payer phone + paymentSrv.respondPayment( + showResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function updateShow(requestId) { + paymentSrv.changeShippingAddress(requestId, shippingAddress); +} + +function showRequest(requestId) { + updateShow(requestId); +} + +function abortRequest(requestId) { + let abortResponse = Cc[ + "@mozilla.org/dom/payments/payment-abort-action-response;1" + ].createInstance(Ci.nsIPaymentAbortActionResponse); + abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED); + paymentSrv.respondPayment(abortResponse); +} + +function completeRequest(requestId) { + let payRequest = paymentSrv.getPaymentRequestById(requestId); + let completeResponse = Cc[ + "@mozilla.org/dom/payments/payment-complete-action-response;1" + ].createInstance(Ci.nsIPaymentCompleteActionResponse); + completeResponse.init( + requestId, + Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED + ); + paymentSrv.respondPayment( + completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) + ); +} + +function checkAddressErrors(errors) { + if (!errors) { + emitTestFail("Expect non-null shippingAddressErrors, but got null."); + } + if (errors.addressLine != "addressLine error") { + emitTestFail( + "Expect shippingAddressErrors.addressLine as 'addressLine error', but got" + + errors.addressLine + ); + } + if (errors.city != "city error") { + emitTestFail( + "Expect shippingAddressErrors.city as 'city error', but got" + errors.city + ); + } + if (errors.dependentLocality != "dependentLocality error") { + emitTestFail( + "Expect shippingAddressErrors.dependentLocality as 'dependentLocality error', but got" + + errors.dependentLocality + ); + } + if (errors.organization != "organization error") { + emitTestFail( + "Expect shippingAddressErrors.organization as 'organization error', but got" + + errors.organization + ); + } + if (errors.phone != "phone error") { + emitTestFail( + "Expect shippingAddressErrors.phone as 'phone error', but got" + + errors.phone + ); + } + if (errors.postalCode != "postalCode error") { + emitTestFail( + "Expect shippingAddressErrors.postalCode as 'postalCode error', but got" + + errors.postalCode + ); + } + if (errors.recipient != "recipient error") { + emitTestFail( + "Expect shippingAddressErrors.recipient as 'recipient error', but got" + + errors.recipient + ); + } + if (errors.region != "region error") { + emitTestFail( + "Expect shippingAddressErrors.region as 'region error', but got" + + errors.region + ); + } + if (errors.regionCode != "regionCode error") { + emitTestFail( + "Expect shippingAddressErrors.regionCode as 'regionCode error', but got" + + errors.region + ); + } + if (errors.sortingCode != "sortingCode error") { + emitTestFail( + "Expect shippingAddressErrors.sortingCode as 'sortingCode error', but got" + + errors.sortingCode + ); + } +} + +function updateRequest(requestId) { + let request = paymentSrv.getPaymentRequestById(requestId); + const addressErrors = request.paymentDetails.shippingAddressErrors; + const payerErrors = request.paymentDetails.payerErrors; + checkAddressErrors(addressErrors); + rejectShow(requestId); +} + +const DummyUIService = { + showPayment: showRequest, + abortPayment: abortRequest, + completePayment: completeRequest, + updatePayment: updateRequest, + closePayment(requestId) {}, + QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), +}; + +paymentSrv.setTestingUIService( + DummyUIService.QueryInterface(Ci.nsIPaymentUIService) +); + +addMessageListener("teardown", function() { + paymentSrv.setTestingUIService(null); + sendAsyncMessage("teardown-complete"); +}); diff --git a/dom/payments/test/blank_page.html b/dom/payments/test/blank_page.html new file mode 100644 index 0000000000..7323b00a28 --- /dev/null +++ b/dom/payments/test/blank_page.html @@ -0,0 +1,16 @@ + + + + Payment Request Testing + + + + +

blank page.html

+ + + diff --git a/dom/payments/test/browser.ini b/dom/payments/test/browser.ini new file mode 100644 index 0000000000..1fb3809b80 --- /dev/null +++ b/dom/payments/test/browser.ini @@ -0,0 +1,10 @@ +[DEFAULT] +prefs = + dom.payments.request.enabled=true +skip-if = + true # we don't ship webpayments right now bug 1514425 +support-files = + head.js + simple_payment_request.html + +[browser_payment_in_different_tabs.js] diff --git a/dom/payments/test/browser_payment_in_different_tabs.js b/dom/payments/test/browser_payment_in_different_tabs.js new file mode 100644 index 0000000000..c811d32dd2 --- /dev/null +++ b/dom/payments/test/browser_payment_in_different_tabs.js @@ -0,0 +1,37 @@ +"use strict"; + +// kTestRoot is from head.js +const kTestPage = kTestRoot + "simple_payment_request.html"; +const TABS_TO_OPEN = 5; +add_task(async () => { + Services.prefs.setBoolPref("dom.payments.request.enabled", true); + const tabs = []; + const options = { + gBrowser: Services.wm.getMostRecentWindow("navigator:browser").gBrowser, + url: kTestPage, + }; + for (let i = 0; i < TABS_TO_OPEN; i++) { + const tab = await BrowserTestUtils.openNewForegroundTab(options); + tabs.push(tab); + } + const paymentSrv = Cc[ + "@mozilla.org/dom/payments/payment-request-service;1" + ].getService(Ci.nsIPaymentRequestService); + const paymentEnum = paymentSrv.enumerate(); + ok( + paymentEnum.hasMoreElements(), + "PaymentRequestService should have at least one payment request." + ); + const payments = new Set(); + for (let payment of paymentEnum) { + ok(payment, "Fail to get existing payment request."); + checkSimplePayment(payment); + payments.add(payment); + } + is(payments.size, TABS_TO_OPEN, `Should be ${TABS_TO_OPEN} unique objects.`); + for (const tab of tabs) { + await TestUtils.waitForTick(); + BrowserTestUtils.removeTab(tab); + } + Services.prefs.setBoolPref("dom.payments.request.enabled", false); +}); diff --git a/dom/payments/test/bug1478740.html b/dom/payments/test/bug1478740.html new file mode 100644 index 0000000000..ddcc04bbb0 --- /dev/null +++ b/dom/payments/test/bug1478740.html @@ -0,0 +1,44 @@ + + +Payment Request Testing + diff --git a/dom/payments/test/echo_payment_request.html b/dom/payments/test/echo_payment_request.html new file mode 100644 index 0000000000..b1bf3da90c --- /dev/null +++ b/dom/payments/test/echo_payment_request.html @@ -0,0 +1,37 @@ + + + + Payment Request Testing + + + + + + + diff --git a/dom/payments/test/head.js b/dom/payments/test/head.js new file mode 100644 index 0000000000..3a377e09ae --- /dev/null +++ b/dom/payments/test/head.js @@ -0,0 +1,127 @@ +const kTestRoot = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "https://example.com" +); + +function checkSimplePayment(aSimplePayment) { + // checking the passed PaymentMethods parameter + is( + aSimplePayment.paymentMethods.length, + 1, + "paymentMethods' length should be 1." + ); + + const methodData = aSimplePayment.paymentMethods.queryElementAt( + 0, + Ci.nsIPaymentMethodData + ); + ok(methodData, "Fail to get payment methodData."); + is( + methodData.supportedMethods, + "basic-card", + "supported method should be 'basic-card'." + ); + ok(!methodData.data, "methodData.data should not exist."); + + // checking the passed PaymentDetails parameter + const details = aSimplePayment.paymentDetails; + is(details.id, "simple details", "details.id should be 'simple details'."); + is( + details.totalItem.label, + "Donation", + "total item's label should be 'Donation'." + ); + is( + details.totalItem.amount.currency, + "USD", + "total item's currency should be 'USD'." + ); + is( + details.totalItem.amount.value, + "55.00", + "total item's value should be '55.00'." + ); + + is( + details.displayItems.length, + 0, + "details.displayItems should be a zero length array." + ); + is( + details.modifiers.length, + 0, + "details.modifiers should be a zero length array." + ); + is( + details.shippingOptions.length, + 0, + "details.shippingOptions should be a zero length array." + ); + + // checking the default generated PaymentOptions parameter + const paymentOptions = aSimplePayment.paymentOptions; + ok(!paymentOptions.requestPayerName, "payerName option should be false"); + ok(!paymentOptions.requestPayerEmail, "payerEmail option should be false"); + ok(!paymentOptions.requestPayerPhone, "payerPhone option should be false"); + ok(!paymentOptions.requestShipping, "requestShipping option should be false"); + is( + paymentOptions.shippingType, + "shipping", + "shippingType option should be 'shipping'" + ); +} + +function checkDupShippingOptionsPayment(aPayment) { + // checking the passed PaymentMethods parameter + is(aPayment.paymentMethods.length, 1, "paymentMethods' length should be 1."); + + const methodData = aPayment.paymentMethods.queryElementAt( + 0, + Ci.nsIPaymentMethodData + ); + ok(methodData, "Fail to get payment methodData."); + is( + methodData.supportedMethods, + "basic-card", + "methodData.supportedMethod name should be 'basic-card'." + ); + ok(!methodData.data, "methodData.data should not exist."); + + // checking the passed PaymentDetails parameter + const details = aPayment.paymentDetails; + is( + details.id, + "duplicate shipping options details", + "details.id should be 'duplicate shipping options details'." + ); + is( + details.totalItem.label, + "Donation", + "total item's label should be 'Donation'." + ); + is( + details.totalItem.amount.currency, + "USD", + "total item's currency should be 'USD'." + ); + is( + details.totalItem.amount.value, + "55.00", + "total item's value should be '55.00'." + ); + + const shippingOptions = details.shippingOptions; + is(shippingOptions.length, 0, "shippingOptions' length should be 0."); + + // checking the passed PaymentOptions parameter + const paymentOptions = aPayment.paymentOptions; + ok(paymentOptions.requestPayerName, "payerName option should be true"); + ok(paymentOptions.requestPayerEmail, "payerEmail option should be true"); + ok(paymentOptions.requestPayerPhone, "payerPhone option should be true"); + ok(paymentOptions.requestShipping, "requestShipping option should be true"); + is( + paymentOptions.shippingType, + "shipping", + "shippingType option should be 'shipping'" + ); +} diff --git a/dom/payments/test/mochitest.ini b/dom/payments/test/mochitest.ini new file mode 100644 index 0000000000..2b302feed6 --- /dev/null +++ b/dom/payments/test/mochitest.ini @@ -0,0 +1,53 @@ +[DEFAULT] +prefs = + dom.payments.request.enabled=true +# Android crashes on nearly all tests, bug 1525959 +skip-if = + true # we don't ship webpayments right now bug 1514425 +scheme = https +support-files = + blank_page.html + bug1478740.html + simple_payment_request.html + echo_payment_request.html + BasiccardChromeScript.js + Bug1478740ChromeScript.js + BasicCardErrorsChromeScript.js + Bug1490698ChromeScript.js + ClosePaymentChromeScript.js + ConstructorChromeScript.js + CurrencyAmountValidationChromeScript.js + DefaultData.js + GeneralChromeScript.js + PayerDetailsChromeScript.js + PMIValidationChromeScript.js + RequestShippingChromeScript.js + RetryPaymentChromeScript.js + ShippingOptionsChromeScript.js + ShowPaymentChromeScript.js + UpdateErrorsChromeScript.js + +[test_abortPayment.html] +run-if = nightly_build # Bug 1390018: Depends on the Nightly-only UI service +skip-if = debug # Bug 1507251 - Leak +[test_basiccard.html] +[test_basiccarderrors.html] +[test_block_none10s.html] +skip-if = true # Bug 1408250: Don't expose PaymentRequest Constructor in non-e10s +[test_bug1478740.html] +[test_bug1490698.html] +[test_canMakePayment.html] +run-if = nightly_build # Bug 1390737: Depends on the Nightly-only UI service +skip-if = debug # Bug 1507251 - Leak +[test_closePayment.html] +[test_constructor.html] +skip-if = (os == "linux") || (os == "mac") || (os == "win" && os_version == "10.0") # Bug 1514425 +[test_currency_amount_validation.html] +[test_payerDetails.html] +[test_payment-request-in-iframe.html] +[test_pmi_validation.html] +[test_requestShipping.html] +[test_retryPayment.html] +[test_shippingOptions.html] +[test_showPayment.html] +[test_update_errors.html] diff --git a/dom/payments/test/simple_payment_request.html b/dom/payments/test/simple_payment_request.html new file mode 100644 index 0000000000..b532ba6101 --- /dev/null +++ b/dom/payments/test/simple_payment_request.html @@ -0,0 +1,81 @@ + + +Payment Request Testing + diff --git a/dom/payments/test/test_abortPayment.html b/dom/payments/test/test_abortPayment.html new file mode 100644 index 0000000000..64285914aa --- /dev/null +++ b/dom/payments/test/test_abortPayment.html @@ -0,0 +1,95 @@ + + + + + + Test for Bug 1345367 + + + + + +Mozilla Bug 1345367 + + + diff --git a/dom/payments/test/test_basiccard.html b/dom/payments/test/test_basiccard.html new file mode 100644 index 0000000000..e8d30fbd06 --- /dev/null +++ b/dom/payments/test/test_basiccard.html @@ -0,0 +1,371 @@ + + + + + + Test for Bug 1375345 + + + + + +Mozilla Bug 1375345 + + diff --git a/dom/payments/test/test_basiccarderrors.html b/dom/payments/test/test_basiccarderrors.html new file mode 100644 index 0000000000..f9ac76ae75 --- /dev/null +++ b/dom/payments/test/test_basiccarderrors.html @@ -0,0 +1,85 @@ + + + +Test for Bug 1489968 + + + + + +Mozilla Bug 1489968 diff --git a/dom/payments/test/test_block_none10s.html b/dom/payments/test/test_block_none10s.html new file mode 100644 index 0000000000..b1d654f38c --- /dev/null +++ b/dom/payments/test/test_block_none10s.html @@ -0,0 +1,58 @@ + + + + Test for Bug 1408250 + + + + + + + + Mozilla Bug 1408250 + + diff --git a/dom/payments/test/test_bug1478740.html b/dom/payments/test/test_bug1478740.html new file mode 100644 index 0000000000..e877face76 --- /dev/null +++ b/dom/payments/test/test_bug1478740.html @@ -0,0 +1,140 @@ + + + + + + Test for retry PaymentRequest + + + + + + +Mozilla Bug 1478740 + + diff --git a/dom/payments/test/test_bug1490698.html b/dom/payments/test/test_bug1490698.html new file mode 100644 index 0000000000..e1126af770 --- /dev/null +++ b/dom/payments/test/test_bug1490698.html @@ -0,0 +1,119 @@ + + + + + + Test for retry PaymentRequest + + + + + + +Mozilla Bug 1490698 + + diff --git a/dom/payments/test/test_canMakePayment.html b/dom/payments/test/test_canMakePayment.html new file mode 100644 index 0000000000..112fb8ce72 --- /dev/null +++ b/dom/payments/test/test_canMakePayment.html @@ -0,0 +1,164 @@ + + + + + + Test for PaymentRequest API canMakePayment() + + + + + +Mozilla Bug 1345365 + + diff --git a/dom/payments/test/test_closePayment.html b/dom/payments/test/test_closePayment.html new file mode 100644 index 0000000000..8f2ad7cd00 --- /dev/null +++ b/dom/payments/test/test_closePayment.html @@ -0,0 +1,284 @@ + + + + + + Test for closing PaymentRequest + + + + + + +Mozilla Bug 1408234 +Mozilla Bug 1483470 + + diff --git a/dom/payments/test/test_constructor.html b/dom/payments/test/test_constructor.html new file mode 100644 index 0000000000..4517a37028 --- /dev/null +++ b/dom/payments/test/test_constructor.html @@ -0,0 +1,351 @@ + + + + + + Test for Bug 1345361 + + + + + +Mozilla Bug 1345361 + + diff --git a/dom/payments/test/test_currency_amount_validation.html b/dom/payments/test/test_currency_amount_validation.html new file mode 100644 index 0000000000..bf8284f37a --- /dev/null +++ b/dom/payments/test/test_currency_amount_validation.html @@ -0,0 +1,353 @@ + + + +Test for PaymentRequest API currency amount validation + + + + +Mozilla Bug 1367669 +Mozilla Bug 1388661 diff --git a/dom/payments/test/test_payerDetails.html b/dom/payments/test/test_payerDetails.html new file mode 100644 index 0000000000..9a241803af --- /dev/null +++ b/dom/payments/test/test_payerDetails.html @@ -0,0 +1,107 @@ + + +Test for PaymentResponse.prototype.onpayerdetailchange + + + + + + + Mozilla Bug 1472026 diff --git a/dom/payments/test/test_payment-request-in-iframe.html b/dom/payments/test/test_payment-request-in-iframe.html new file mode 100644 index 0000000000..0a4b690f9b --- /dev/null +++ b/dom/payments/test/test_payment-request-in-iframe.html @@ -0,0 +1,168 @@ + + + + + + Test for Bug 1318988 + + + + + +Mozilla Bug 1318988 + + diff --git a/dom/payments/test/test_pmi_validation.html b/dom/payments/test/test_pmi_validation.html new file mode 100644 index 0000000000..00d5c0771c --- /dev/null +++ b/dom/payments/test/test_pmi_validation.html @@ -0,0 +1,245 @@ + + + + + + Test for PaymentRequest API payment method identifier validation + + + + + +Mozilla Bug 1389418 + + diff --git a/dom/payments/test/test_requestShipping.html b/dom/payments/test/test_requestShipping.html new file mode 100644 index 0000000000..b866588953 --- /dev/null +++ b/dom/payments/test/test_requestShipping.html @@ -0,0 +1,180 @@ + + + + + + Test for Bug 1436903 + + + + + +Mozilla Bug 1436903 + + diff --git a/dom/payments/test/test_retryPayment.html b/dom/payments/test/test_retryPayment.html new file mode 100644 index 0000000000..3ce389f475 --- /dev/null +++ b/dom/payments/test/test_retryPayment.html @@ -0,0 +1,354 @@ + + + + + + Test for retry PaymentRequest + + + + + + +Mozilla Bug 1435161 + + diff --git a/dom/payments/test/test_shippingOptions.html b/dom/payments/test/test_shippingOptions.html new file mode 100644 index 0000000000..887ec30de5 --- /dev/null +++ b/dom/payments/test/test_shippingOptions.html @@ -0,0 +1,208 @@ + + + + + + Test for shippingOptions related bugs + + + + + + +Mozilla Bug 1440041 +Mozilla Bug 1443914 + + diff --git a/dom/payments/test/test_showPayment.html b/dom/payments/test/test_showPayment.html new file mode 100644 index 0000000000..2a4a0bb4f7 --- /dev/null +++ b/dom/payments/test/test_showPayment.html @@ -0,0 +1,504 @@ + + + + + + Test for Bug 1345366 + + + + + +Mozilla Bug 1345366 + + diff --git a/dom/payments/test/test_update_errors.html b/dom/payments/test/test_update_errors.html new file mode 100644 index 0000000000..a473cf2706 --- /dev/null +++ b/dom/payments/test/test_update_errors.html @@ -0,0 +1,121 @@ + + + + + + Test for Bug 1435157 + + + + + + +Mozilla Bug 1435157 + + -- cgit v1.2.3