Bug1490698ChromeScript.js (6557B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ */ 4 5 /* eslint-env mozilla/chrome-script */ 6 7 "use strict"; 8 9 const { XPCOMUtils } = ChromeUtils.importESModule( 10 "resource://gre/modules/XPCOMUtils.sys.mjs" 11 ); 12 13 const paymentSrv = Cc[ 14 "@mozilla.org/dom/payments/payment-request-service;1" 15 ].getService(Ci.nsIPaymentRequestService); 16 17 function emitTestFail(message) { 18 sendAsyncMessage("test-fail", message); 19 } 20 function emitTestPass(message) { 21 sendAsyncMessage("test-pass", message); 22 } 23 24 const billingAddress = Cc[ 25 "@mozilla.org/dom/payments/payment-address;1" 26 ].createInstance(Ci.nsIPaymentAddress); 27 const addressLine = Cc["@mozilla.org/array;1"].createInstance( 28 Ci.nsIMutableArray 29 ); 30 const address = Cc["@mozilla.org/supports-string;1"].createInstance( 31 Ci.nsISupportsString 32 ); 33 address.data = "Easton Ave"; 34 addressLine.appendElement(address); 35 billingAddress.init( 36 "USA", // country 37 addressLine, // address line 38 "CA", // region 39 "CA", // region code 40 "San Bruno", // city 41 "", // dependent locality 42 "94066", // postal code 43 "123456", // sorting code 44 "", // organization 45 "Bill A. Pacheco", // recipient 46 "+14344413879" 47 ); // phone 48 49 function acceptPayment(requestId) { 50 const basiccardResponseData = Cc[ 51 "@mozilla.org/dom/payments/basiccard-response-data;1" 52 ].createInstance(Ci.nsIBasicCardResponseData); 53 const showResponse = Cc[ 54 "@mozilla.org/dom/payments/payment-show-action-response;1" 55 ].createInstance(Ci.nsIPaymentShowActionResponse); 56 basiccardResponseData.initData( 57 "Bill A. Pacheco", // cardholderName 58 "4916855166538720", // cardNumber 59 "01", // expiryMonth 60 "2024", // expiryYear 61 "180", // cardSecurityCode 62 billingAddress 63 ); // billingAddress 64 showResponse.init( 65 requestId, 66 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, 67 "basic-card", // payment method 68 basiccardResponseData, // payment method data 69 "Bill A. Pacheco", // payer name 70 "", // payer email 71 "" 72 ); // payer phone 73 paymentSrv.respondPayment( 74 showResponse.QueryInterface(Ci.nsIPaymentActionResponse) 75 ); 76 } 77 78 function rejectPayment(requestId) { 79 const responseData = Cc[ 80 "@mozilla.org/dom/payments/general-response-data;1" 81 ].createInstance(Ci.nsIGeneralResponseData); 82 responseData.initData({}); 83 const showResponse = Cc[ 84 "@mozilla.org/dom/payments/payment-show-action-response;1" 85 ].createInstance(Ci.nsIPaymentShowActionResponse); 86 showResponse.init( 87 requestId, 88 Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, 89 "", // payment method 90 responseData, // payment method data 91 "", // payer name 92 "", // payer email 93 "" 94 ); // payer phone 95 paymentSrv.respondPayment( 96 showResponse.QueryInterface(Ci.nsIPaymentActionResponse) 97 ); 98 } 99 100 const DummyUIService = { 101 testName: "", 102 requestId: "", 103 showPayment(requestId) { 104 this.requestId = requestId; 105 acceptPayment(requestId); 106 }, 107 abortPaymen(requestId) { 108 this.requestId = requestId; 109 }, 110 completePayment(requestId) { 111 this.requestId = requestId; 112 let completeResponse = Cc[ 113 "@mozilla.org/dom/payments/payment-complete-action-response;1" 114 ].createInstance(Ci.nsIPaymentCompleteActionResponse); 115 completeResponse.init( 116 requestId, 117 Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED 118 ); 119 paymentSrv.respondPayment( 120 completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) 121 ); 122 }, 123 updatePayment(requestId) { 124 this.requestId = requestId; 125 }, 126 closePayment(requestId) { 127 this.requestId = requestId; 128 }, 129 QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), 130 }; 131 132 paymentSrv.setTestingUIService( 133 DummyUIService.QueryInterface(Ci.nsIPaymentUIService) 134 ); 135 136 addMessageListener("start-test", function (testName) { 137 DummyUIService.testName = testName; 138 sendAsyncMessage("start-test-complete"); 139 }); 140 141 addMessageListener("finish-test", function () { 142 DummyUIService.testName = ""; 143 sendAsyncMessage("finish-test-complete"); 144 }); 145 146 addMessageListener("interact-with-payment", function () { 147 if (DummyUIService.requestId === "") { 148 emitTestFail(`${DummyUIService.testName}: Unexpected empty requestId`); 149 } 150 try { 151 acceptPayment(DummyUIService.requestId); 152 emitTestFail( 153 `${DummyUIService.testName}: Got unexpected success when accepting PaymentRequest.` 154 ); 155 } catch (err) { 156 if (err.name !== "NS_ERROR_FAILURE") { 157 emitTestFail( 158 `${DummyUIService.testName}: Got unexpected '${err.name}' when accepting PaymentRequest.` 159 ); 160 } else { 161 emitTestPass( 162 `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when accepting PaymentRequest.` 163 ); 164 } 165 } 166 167 try { 168 rejectPayment(DummyUIService.requestId); 169 emitTestFail( 170 `${DummyUIService.testName}: Got unexpected success when rejecting PaymentRequest.` 171 ); 172 } catch (err) { 173 if (err.name !== "NS_ERROR_FAILURE") { 174 emitTestFail( 175 `${DummyUIService.testName}: Got unexpected '${err.name}' when rejecting PaymentRequest.` 176 ); 177 } else { 178 emitTestPass( 179 `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when rejecting PaymentRequest.` 180 ); 181 } 182 } 183 184 try { 185 paymentSrv.changeShippingOption( 186 DummyUIService.requestId, 187 "error shippping option" 188 ); 189 emitTestFail( 190 `${DummyUIService.testName}: Got unexpected success when changing shippingOption.` 191 ); 192 } catch (err) { 193 if (err.name !== "NS_ERROR_FAILURE") { 194 emitTestFail( 195 `${DummyUIService.testName}: Got unexpected '${err.name}' when changin shippingOption.` 196 ); 197 } else { 198 emitTestPass( 199 `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when changing shippingOption.` 200 ); 201 } 202 } 203 204 try { 205 paymentSrv.changeShippingOption(DummyUIService.requestId, billingAddress); 206 emitTestFail( 207 `${DummyUIService.testName}: Got unexpected success when changing shippingAddress.` 208 ); 209 } catch (err) { 210 if (err.name !== "NS_ERROR_FAILURE") { 211 emitTestFail( 212 `${DummyUIService.testName}: Got unexpected '${err.name}' when changing shippingAddress.` 213 ); 214 } else { 215 emitTestPass( 216 `${DummyUIService.testName}: Got expected 'NS_ERROR_FAILURE' when changing shippingAddress.` 217 ); 218 } 219 } 220 sendAsyncMessage("interact-with-payment-complete"); 221 }); 222 223 addMessageListener("teardown", function () { 224 paymentSrv.setTestingUIService(null); 225 sendAsyncMessage("teardown-complete"); 226 });