ConstructorChromeScript.js (15647B)
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 21 function checkSimplestRequest(payRequest) { 22 if (payRequest.topLevelPrincipal.origin != "https://example.com") { 23 emitTestFail( 24 "Top level principal's Origin should be 'https://example.com', but got '" + 25 payRequest.topLevelPrincipal.origin + 26 "'." 27 ); 28 } 29 30 if (payRequest.paymentMethods.length != 1) { 31 emitTestFail("paymentMethods' length should be 1."); 32 } 33 34 const methodData = payRequest.paymentMethods.queryElementAt( 35 0, 36 Ci.nsIPaymentMethodData 37 ); 38 if (!methodData) { 39 emitTestFail("Fail to get payment methodData."); 40 } 41 const supportedMethod = methodData.supportedMethods; 42 if (supportedMethod != "basic-card") { 43 emitTestFail("supported method should be 'basic-card'."); 44 } 45 if (methodData.data) { 46 emitTestFail("methodData.data should not exist."); 47 } 48 49 // checking the passed PaymentDetails parameter 50 const details = payRequest.paymentDetails; 51 if (details.totalItem.label != "Total") { 52 emitTestFail("total item's label should be 'Total'."); 53 } 54 if (details.totalItem.amount.currency != "USD") { 55 emitTestFail("total item's currency should be 'USD'."); 56 } 57 if (details.totalItem.amount.value != "1.00") { 58 emitTestFail("total item's value should be '1.00'."); 59 } 60 61 if (details.displayItems.length !== 0) { 62 emitTestFail("details.displayItems should be an empty array."); 63 } 64 if (details.modifiers.length !== 0) { 65 emitTestFail("details.modifiers should be an empty array."); 66 } 67 if (details.shippingOptions.length !== 0) { 68 emitTestFail("details.shippingOptions should be an empty array."); 69 } 70 71 // checking the default generated PaymentOptions parameter 72 const paymentOptions = payRequest.paymentOptions; 73 if (paymentOptions.requestPayerName) { 74 emitTestFail("requestPayerName option should be false."); 75 } 76 if (paymentOptions.requestPayerEmail) { 77 emitTestFail("requestPayerEmail option should be false."); 78 } 79 if (paymentOptions.requestPayerPhone) { 80 emitTestFail("requestPayerPhone option should be false."); 81 } 82 if (paymentOptions.requestShipping) { 83 emitTestFail("requestShipping option should be false."); 84 } 85 if (paymentOptions.shippingType != "shipping") { 86 emitTestFail("shippingType option should be 'shipping'."); 87 } 88 } 89 90 // eslint-disable-next-line complexity 91 function checkComplexRequest(payRequest) { 92 if (payRequest.topLevelPrincipal.origin != "https://example.com") { 93 emitTestFail( 94 "Top level principal's origin should be 'https://example.com', but got '" + 95 payRequest.topLevelPrincipal.origin + 96 "'." 97 ); 98 } 99 100 if (payRequest.paymentMethods.length != 1) { 101 emitTestFail("paymentMethods' length should be 1."); 102 } 103 104 const methodData = payRequest.paymentMethods.queryElementAt( 105 0, 106 Ci.nsIPaymentMethodData 107 ); 108 if (!methodData) { 109 emitTestFail("Fail to get payment methodData."); 110 } 111 let supportedMethod = methodData.supportedMethods; 112 if (supportedMethod != "basic-card") { 113 emitTestFail("supported method should be 'basic-card'."); 114 } 115 const data = methodData.data; 116 const supportedNetworks = data.supportedNetworks; 117 const expectedSupportedNetworks = [ 118 "unionpay", 119 "visa", 120 "mastercard", 121 "amex", 122 "discover", 123 "diners", 124 "jcb", 125 "mir", 126 ]; 127 if (supportedNetworks.length != expectedSupportedNetworks.length) { 128 emitTestFail( 129 "supportedNetworks.length should be " + 130 expectedSupportedNetworks.length + 131 ", but got " + 132 supportedNetworks.length + 133 "." 134 ); 135 } 136 for (let idx = 0; idx < supportedNetworks.length; idx++) { 137 if (supportedNetworks[idx] != expectedSupportedNetworks[idx]) { 138 emitTestFail( 139 "supportedNetworks[" + 140 idx + 141 "] should be '" + 142 expectedSupportedNetworks[idx] + 143 "', but got '" + 144 supportedNetworks[idx] + 145 "'." 146 ); 147 } 148 } 149 // checking the passed PaymentDetails parameter 150 const details = payRequest.paymentDetails; 151 if (details.id != "payment details") { 152 emitTestFail("details.id should be 'payment details'."); 153 } 154 if (details.totalItem.label != "Total") { 155 emitTestFail("total item's label should be 'Total'."); 156 } 157 if (details.totalItem.amount.currency != "USD") { 158 emitTestFail("total item's currency should be 'USD'."); 159 } 160 if (details.totalItem.amount.value != "100.00") { 161 emitTestFail("total item's value should be '100.00'."); 162 } 163 164 const displayItems = details.displayItems; 165 if (!details.displayItems) { 166 emitTestFail("details.displayItems should not be undefined."); 167 } 168 if (displayItems.length != 2) { 169 emitTestFail("displayItems' length should be 2."); 170 } 171 let item = displayItems.queryElementAt(0, Ci.nsIPaymentItem); 172 if (item.label != "First item") { 173 emitTestFail("1st display item's label should be 'First item'."); 174 } 175 if (item.amount.currency != "USD") { 176 emitTestFail("1st display item's currency should be 'USD'."); 177 } 178 if (item.amount.value != "60.00") { 179 emitTestFail("1st display item's value should be '60.00'."); 180 } 181 item = displayItems.queryElementAt(1, Ci.nsIPaymentItem); 182 if (item.label != "Second item") { 183 emitTestFail("2nd display item's label should be 'Second item'."); 184 } 185 if (item.amount.currency != "USD") { 186 emitTestFail("2nd display item's currency should be 'USD'."); 187 } 188 if (item.amount.value != "40.00") { 189 emitTestFail("2nd display item's value should be '40.00'."); 190 } 191 192 const modifiers = details.modifiers; 193 if (!modifiers) { 194 emitTestFail("details.displayItems should not be undefined."); 195 } 196 if (modifiers.length != 1) { 197 emitTestFail("modifiers' length should be 1."); 198 } 199 const modifier = modifiers.queryElementAt(0, Ci.nsIPaymentDetailsModifier); 200 const supportedMethods = modifier.supportedMethods; 201 if (supportedMethod != "basic-card") { 202 emitTestFail("modifier's supported method name should be 'basic-card'."); 203 } 204 if (modifier.total.label != "Discounted Total") { 205 emitTestFail("modifier's total label should be 'Discounted Total'."); 206 } 207 if (modifier.total.amount.currency != "USD") { 208 emitTestFail("modifier's total currency should be 'USD'."); 209 } 210 if (modifier.total.amount.value != "90.00") { 211 emitTestFail("modifier's total value should be '90.00'."); 212 } 213 214 const additionalItems = modifier.additionalDisplayItems; 215 if (additionalItems.length != 1) { 216 emitTestFail("additionalDisplayItems' length should be 1."); 217 } 218 const additionalItem = additionalItems.queryElementAt(0, Ci.nsIPaymentItem); 219 if (additionalItem.label != "basic-card discount") { 220 emitTestFail("additional item's label should be 'basic-card discount'."); 221 } 222 if (additionalItem.amount.currency != "USD") { 223 emitTestFail("additional item's currency should be 'USD'."); 224 } 225 if (additionalItem.amount.value != "-10.00") { 226 emitTestFail("additional item's value should be '-10.00'."); 227 } 228 if (modifier.data.discountProgramParticipantId != "86328764873265") { 229 emitTestFail( 230 "modifier's data should be '86328764873265', but got '" + 231 modifier.data.discountProgramParticipantId + 232 "'." 233 ); 234 } 235 236 const shippingOptions = details.shippingOptions; 237 if (!shippingOptions) { 238 emitTestFail("details.shippingOptions should not be undefined."); 239 } 240 if (shippingOptions.length != 2) { 241 emitTestFail("shippingOptions' length should be 2."); 242 } 243 let shippingOption = shippingOptions.queryElementAt( 244 0, 245 Ci.nsIPaymentShippingOption 246 ); 247 if (shippingOption.id != "NormalShipping") { 248 emitTestFail("1st shippingOption's id should be 'NormalShipping'."); 249 } 250 if (shippingOption.label != "NormalShipping") { 251 emitTestFail("1st shippingOption's lable should be 'NormalShipping'."); 252 } 253 if (shippingOption.amount.currency != "USD") { 254 emitTestFail("1st shippingOption's amount currency should be 'USD'."); 255 } 256 if (shippingOption.amount.value != "10.00") { 257 emitTestFail("1st shippingOption's amount value should be '10.00'."); 258 } 259 if (!shippingOption.selected) { 260 emitTestFail("1st shippingOption should be selected."); 261 } 262 shippingOption = shippingOptions.queryElementAt( 263 1, 264 Ci.nsIPaymentShippingOption 265 ); 266 if (shippingOption.id != "FastShipping") { 267 emitTestFail("2nd shippingOption's id should be 'FastShipping'."); 268 } 269 if (shippingOption.label != "FastShipping") { 270 emitTestFail("2nd shippingOption's lable should be 'FastShipping'."); 271 } 272 if (shippingOption.amount.currency != "USD") { 273 emitTestFail("2nd shippingOption's amount currency should be 'USD'."); 274 } 275 if (shippingOption.amount.value != "30.00") { 276 emitTestFail("2nd shippingOption's amount value should be '30.00'."); 277 } 278 if (shippingOption.selected) { 279 emitTestFail("2nd shippingOption should not be selected."); 280 } 281 282 // checking the default generated PaymentOptions parameter 283 const paymentOptions = payRequest.paymentOptions; 284 if (!paymentOptions.requestPayerName) { 285 emitTestFail("requestPayerName option should be true."); 286 } 287 if (!paymentOptions.requestPayerEmail) { 288 emitTestFail("requestPayerEmail option should be true."); 289 } 290 if (!paymentOptions.requestPayerPhone) { 291 emitTestFail("requestPayerPhone option should be true."); 292 } 293 if (!paymentOptions.requestShipping) { 294 emitTestFail("requestShipping option should be true."); 295 } 296 if (paymentOptions.shippingType != "shipping") { 297 emitTestFail("shippingType option should be 'shipping'."); 298 } 299 } 300 301 function checkNonBasicCardRequest(payRequest) { 302 if (payRequest.paymentMethods.length != 1) { 303 emitTestFail("paymentMethods' length should be 1."); 304 } 305 306 const methodData = payRequest.paymentMethods.queryElementAt( 307 0, 308 Ci.nsIPaymentMethodData 309 ); 310 if (!methodData) { 311 emitTestFail("Fail to get payment methodData."); 312 } 313 const supportedMethod = methodData.supportedMethods; 314 if (supportedMethod != "testing-payment-method") { 315 emitTestFail("supported method should be 'testing-payment-method'."); 316 } 317 318 const paymentId = methodData.data.paymentId; 319 if (paymentId != "P3892940") { 320 emitTestFail( 321 "methodData.data.paymentId should be 'P3892940', but got " + 322 paymentId + 323 "." 324 ); 325 } 326 const paymentType = methodData.data.paymentType; 327 if (paymentType != "prepaid") { 328 emitTestFail( 329 "methodData.data.paymentType should be 'prepaid', but got " + 330 paymentType + 331 "." 332 ); 333 } 334 335 // checking the passed PaymentDetails parameter 336 const details = payRequest.paymentDetails; 337 if (details.totalItem.label != "Total") { 338 emitTestFail("total item's label should be 'Total'."); 339 } 340 if (details.totalItem.amount.currency != "USD") { 341 emitTestFail("total item's currency should be 'USD'."); 342 } 343 if (details.totalItem.amount.value != "1.00") { 344 emitTestFail("total item's value should be '1.00'."); 345 } 346 347 if (details.displayItems.length !== 0) { 348 emitTestFail("details.displayItems should be an zero length array."); 349 } 350 if (details.displayItems.length !== 0) { 351 emitTestFail("details.modifiers should be an zero length array."); 352 } 353 if (details.displayItems.length !== 0) { 354 emitTestFail("details.shippingOptions should be an zero length array."); 355 } 356 357 // checking the default generated PaymentOptions parameter 358 const paymentOptions = payRequest.paymentOptions; 359 if (paymentOptions.requestPayerName) { 360 emitTestFail("requestPayerName option should be false."); 361 } 362 if (paymentOptions.requestPayerEmail) { 363 emitTestFail("requestPayerEmail option should be false."); 364 } 365 if (paymentOptions.requestPayerPhone) { 366 emitTestFail("requestPayerPhone option should be false."); 367 } 368 if (paymentOptions.requestShipping) { 369 emitTestFail("requestShipping option should be false."); 370 } 371 if (paymentOptions.shippingType != "shipping") { 372 emitTestFail("shippingType option should be 'shipping'."); 373 } 374 } 375 376 function checkSimplestRequestHandler() { 377 const paymentEnum = paymentSrv.enumerate(); 378 if (!paymentEnum.hasMoreElements()) { 379 emitTestFail( 380 "PaymentRequestService should have at least one payment request." 381 ); 382 } 383 for (let payRequest of paymentEnum) { 384 if (!payRequest) { 385 emitTestFail("Fail to get existing payment request."); 386 break; 387 } 388 checkSimplestRequest(payRequest); 389 } 390 paymentSrv.cleanup(); 391 sendAsyncMessage("check-complete"); 392 } 393 394 function checkComplexRequestHandler() { 395 const paymentEnum = paymentSrv.enumerate(); 396 if (!paymentEnum.hasMoreElements()) { 397 emitTestFail( 398 "PaymentRequestService should have at least one payment request." 399 ); 400 } 401 for (let payRequest of paymentEnum) { 402 if (!payRequest) { 403 emitTestFail("Fail to get existing payment request."); 404 break; 405 } 406 checkComplexRequest(payRequest); 407 } 408 paymentSrv.cleanup(); 409 sendAsyncMessage("check-complete"); 410 } 411 412 function checkNonBasicCardRequestHandler() { 413 const paymentEnum = paymentSrv.enumerate(); 414 if (!paymentEnum.hasMoreElements()) { 415 emitTestFail( 416 "PaymentRequestService should have at least one payment request." 417 ); 418 } 419 for (let payRequest of paymentEnum) { 420 if (!payRequest) { 421 emitTestFail("Fail to get existing payment request."); 422 break; 423 } 424 checkNonBasicCardRequest(payRequest); 425 } 426 paymentSrv.cleanup(); 427 sendAsyncMessage("check-complete"); 428 } 429 430 function checkMultipleRequestsHandler() { 431 const paymentEnum = paymentSrv.enumerate(); 432 if (!paymentEnum.hasMoreElements()) { 433 emitTestFail( 434 "PaymentRequestService should have at least one payment request." 435 ); 436 } 437 for (let payRequest of paymentEnum) { 438 if (!payRequest) { 439 emitTestFail("Fail to get existing payment request."); 440 break; 441 } 442 if (payRequest.paymentDetails.id == "payment details") { 443 checkComplexRequest(payRequest); 444 } else { 445 checkSimplestRequest(payRequest); 446 } 447 } 448 paymentSrv.cleanup(); 449 sendAsyncMessage("check-complete"); 450 } 451 452 function checkCrossOriginTopLevelPrincipalHandler() { 453 const paymentEnum = paymentSrv.enumerate(); 454 if (!paymentEnum.hasMoreElements()) { 455 emitTestFail( 456 "PaymentRequestService should have at least one payment request." 457 ); 458 } 459 for (let payRequest of paymentEnum) { 460 if (!payRequest) { 461 emitTestFail("Fail to get existing payment request."); 462 break; 463 } 464 if (payRequest.topLevelPrincipal.origin != "https://example.com") { 465 emitTestFail( 466 "Top level principal's origin should be 'https://example.com', but got '" + 467 payRequest.topLevelPrincipal.origin + 468 "'." 469 ); 470 } 471 } 472 paymentSrv.cleanup(); 473 sendAsyncMessage("check-complete"); 474 } 475 476 addMessageListener("check-simplest-request", checkSimplestRequestHandler); 477 addMessageListener("check-complex-request", checkComplexRequestHandler); 478 addMessageListener("check-multiple-requests", checkMultipleRequestsHandler); 479 addMessageListener( 480 "check-nonbasiccard-request", 481 checkNonBasicCardRequestHandler 482 ); 483 addMessageListener( 484 "check-cross-origin-top-level-principal", 485 checkCrossOriginTopLevelPrincipalHandler 486 ); 487 488 addMessageListener("teardown", function () { 489 sendAsyncMessage("teardown-complete"); 490 });