tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ShowPaymentChromeScript.js (12122B)


      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", `${DummyUIService.testName}: ${message}`);
     19 }
     20 function emitTestPass(message) {
     21  sendAsyncMessage("test-pass", `${DummyUIService.testName}: ${message}`);
     22 }
     23 
     24 const shippingAddress = 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 shippingAddress.init(
     36  "USA", // country
     37  addressLine, // address line
     38  "CA", // region
     39  "CA", // region code
     40  "San Bruno", // city
     41  "Test locality", // dependent locality
     42  "94066", // postal code
     43  "123456", // sorting code
     44  "Testing Org", // organization
     45  "Bill A. Pacheco", // recipient
     46  "+1-434-441-3879"
     47 ); // phone
     48 
     49 function acceptShow(requestId) {
     50  const responseData = Cc[
     51    "@mozilla.org/dom/payments/general-response-data;1"
     52  ].createInstance(Ci.nsIGeneralResponseData);
     53  responseData.initData({
     54    paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1",
     55  });
     56  let showResponse = Cc[
     57    "@mozilla.org/dom/payments/payment-show-action-response;1"
     58  ].createInstance(Ci.nsIPaymentShowActionResponse);
     59  showResponse.init(
     60    requestId,
     61    Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
     62    "testing-payment-method", // payment method
     63    responseData, // payment method data
     64    "Bill A. Pacheco", // payer name
     65    "", // payer email
     66    ""
     67  ); // payer phone
     68  paymentSrv.respondPayment(
     69    showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
     70  );
     71 }
     72 
     73 function rejectShow(requestId) {
     74  const responseData = Cc[
     75    "@mozilla.org/dom/payments/general-response-data;1"
     76  ].createInstance(Ci.nsIGeneralResponseData);
     77  responseData.initData({});
     78  const showResponse = Cc[
     79    "@mozilla.org/dom/payments/payment-show-action-response;1"
     80  ].createInstance(Ci.nsIPaymentShowActionResponse);
     81  showResponse.init(
     82    requestId,
     83    Ci.nsIPaymentActionResponse.PAYMENT_REJECTED,
     84    "", // payment method
     85    responseData, // payment method data
     86    "", // payer name
     87    "", // payer email
     88    ""
     89  ); // payer phone
     90  paymentSrv.respondPayment(
     91    showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
     92  );
     93 }
     94 
     95 function updateShow(requestId) {
     96  if (DummyUIService.expectedUpdateAction == "updateaddress") {
     97    paymentSrv.changeShippingAddress(requestId, shippingAddress);
     98  } else if (
     99    DummyUIService.expectedUpdateAction == "accept" ||
    100    DummyUIService.expectedUpdateAction == "error"
    101  ) {
    102    paymentSrv.changeShippingOption(requestId, "FastShipping");
    103  } else {
    104    emitTestFail(
    105      "Unknown expected update action: " + DummyUIService.expectedUpdateAction
    106    );
    107  }
    108 }
    109 
    110 function showRequest(requestId) {
    111  const request = paymentSrv.getPaymentRequestById(requestId);
    112  if (request.completeStatus == "initial") {
    113    return;
    114  }
    115  if (DummyUIService.expectedShowAction == "accept") {
    116    acceptShow(requestId);
    117  } else if (DummyUIService.expectedShowAction == "reject") {
    118    rejectShow(requestId);
    119  } else if (DummyUIService.expectedShowAction == "update") {
    120    updateShow(requestId);
    121  } else {
    122    emitTestFail(
    123      "Unknown expected show action: " + DummyUIService.expectedShowAction
    124    );
    125  }
    126 }
    127 
    128 function abortRequest(requestId) {
    129  let abortResponse = Cc[
    130    "@mozilla.org/dom/payments/payment-abort-action-response;1"
    131  ].createInstance(Ci.nsIPaymentAbortActionResponse);
    132  abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED);
    133  paymentSrv.respondPayment(abortResponse);
    134 }
    135 
    136 function completeRequest(requestId) {
    137  let request = paymentSrv.getPaymentRequestById(requestId);
    138  if (DummyUIService.expectedCompleteStatus) {
    139    if (request.completeStatus == DummyUIService.expectedCompleteStatus) {
    140      emitTestPass(
    141        "request.completeStatus matches expectation of " +
    142          DummyUIService.expectedCompleteStatus
    143      );
    144    } else {
    145      emitTestFail(
    146        "request.completeStatus incorrect. Expected " +
    147          DummyUIService.expectedCompleteStatus +
    148          ", got " +
    149          request.completeStatus
    150      );
    151    }
    152  }
    153  let completeResponse = Cc[
    154    "@mozilla.org/dom/payments/payment-complete-action-response;1"
    155  ].createInstance(Ci.nsIPaymentCompleteActionResponse);
    156  completeResponse.init(
    157    requestId,
    158    Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
    159  );
    160  paymentSrv.respondPayment(
    161    completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
    162  );
    163 }
    164 
    165 function updateRequest(requestId) {
    166  let request = paymentSrv.getPaymentRequestById(requestId);
    167  if (request.completeStatus !== "") {
    168    emitTestFail(
    169      "request.completeStatus should be empty, but got '" +
    170        request.completeStatus +
    171        "'."
    172    );
    173  }
    174  if (DummyUIService.expectedUpdateAction == "accept") {
    175    if (request.paymentDetails.error != "") {
    176      emitTestFail(
    177        "updatedDetails should not have errors(" +
    178          request.paymentDetails.error +
    179          ")."
    180      );
    181    }
    182    const shippingOptions = request.paymentDetails.shippingOptions;
    183    let shippingOption = shippingOptions.queryElementAt(
    184      0,
    185      Ci.nsIPaymentShippingOption
    186    );
    187    if (shippingOption.selected) {
    188      emitTestFail(shippingOption.label + " should not be selected.");
    189    }
    190    shippingOption = shippingOptions.queryElementAt(
    191      1,
    192      Ci.nsIPaymentShippingOption
    193    );
    194    if (!shippingOption.selected) {
    195      emitTestFail(shippingOption.label + " should be selected.");
    196    }
    197    acceptShow(requestId);
    198  } else if (DummyUIService.expectedUpdateAction == "error") {
    199    if (request.paymentDetails.error != "Update with Error") {
    200      emitTestFail(
    201        "details.error should be 'Update with Error', but got " +
    202          request.paymentDetails.error +
    203          "."
    204      );
    205    }
    206    rejectShow(requestId);
    207  } else if (DummyUIService.expectedUpdateAction == "updateaddress") {
    208    if (request.paymentDetails.error != "") {
    209      emitTestFail(
    210        "updatedDetails should not have errors(" +
    211          request.paymentDetails.error +
    212          ")."
    213      );
    214    }
    215    DummyUIService.expectedUpdateAction = "accept";
    216    paymentSrv.changeShippingOption(requestId, "FastShipping");
    217  } else {
    218    emitTestFail(
    219      "Unknown expected update aciton: " + DummyUIService.expectedUpdateAction
    220    );
    221  }
    222 }
    223 
    224 const DummyUIService = {
    225  testName: "",
    226  expectedCompleteStatus: null,
    227  expectedShowAction: "accept",
    228  expectedUpdateAction: "accept",
    229  showPayment: showRequest,
    230  abortPayment: abortRequest,
    231  completePayment: completeRequest,
    232  updatePayment: updateRequest,
    233  closePayment(requestId) {},
    234  QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]),
    235 };
    236 
    237 paymentSrv.setTestingUIService(
    238  DummyUIService.QueryInterface(Ci.nsIPaymentUIService)
    239 );
    240 
    241 function testShowResponseInit() {
    242  const showResponseData = Cc[
    243    "@mozilla.org/dom/payments/general-response-data;1"
    244  ].createInstance(Ci.nsIGeneralResponseData);
    245  try {
    246    showResponseData.initData(null);
    247    emitTestFail(
    248      "nsIGeneralResponseData can not be initialized with null object."
    249    );
    250  } catch (e) {
    251    if (e.name != "NS_ERROR_FAILURE") {
    252      emitTestFail(
    253        "Expected 'NS_ERROR_FAILURE' when initializing nsIGeneralResponseData with null object, but got " +
    254          e.name +
    255          "."
    256      );
    257    }
    258    emitTestPass(
    259      "Get expected result for initializing nsIGeneralResponseData with null object"
    260    );
    261  }
    262  const showResponse = Cc[
    263    "@mozilla.org/dom/payments/payment-show-action-response;1"
    264  ].createInstance(Ci.nsIPaymentShowActionResponse);
    265  try {
    266    showResponse.init(
    267      "test request id",
    268      Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
    269      "testing-payment-method", // payment method
    270      showResponseData, // payment method data
    271      "Bill A. Pacheco", // payer name
    272      "", // payer email
    273      ""
    274    ); // payer phone
    275    emitTestPass(
    276      "Get expected result for initializing response with accepted and empty data."
    277    );
    278  } catch (e) {
    279    emitTestFail(
    280      "Unexpected error " +
    281        e.name +
    282        " when initializing response with accepted and empty data."
    283    );
    284  }
    285 
    286  try {
    287    showResponse.init(
    288      "test request id",
    289      Ci.nsIPaymentActionResponse.PAYMENT_REJECTED,
    290      "testing-payment-method",
    291      null,
    292      "Bill A. Pacheco",
    293      "",
    294      ""
    295    );
    296    emitTestPass(
    297      "Get expected result for initializing response with rejected and null data."
    298    );
    299  } catch (e) {
    300    emitTestFail(
    301      "Unexpected error " +
    302        e.name +
    303        " when initializing response with rejected and null data."
    304    );
    305  }
    306 
    307  try {
    308    showResponse.init(
    309      "test request id",
    310      Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
    311      "testing-payment-method",
    312      null,
    313      "Bill A. Pacheco",
    314      "",
    315      ""
    316    );
    317    emitTestFail(
    318      "nsIPaymentShowActionResponse can not be initialized with accpeted and null data."
    319    );
    320  } catch (e) {
    321    if (e.name != "NS_ERROR_ILLEGAL_VALUE") {
    322      emitTestFail(
    323        "Expected 'NS_ERROR_ILLEGAL_VALUE', but got " + e.name + "."
    324      );
    325    }
    326    emitTestPass(
    327      "Get expected result for initializing response with accepted and null data."
    328    );
    329  }
    330  sendAsyncMessage("test-show-response-init-complete");
    331 }
    332 
    333 addMessageListener("set-simple-ui-service", function (testName) {
    334  DummyUIService.testName = testName;
    335  DummyUIService.expectedCompleteStatus = null;
    336  DummyUIService.expectedShowAction = "accept";
    337  DummyUIService.expectedUpdateAction = "accept";
    338  sendAsyncMessage("set-simple-ui-service-complete");
    339 });
    340 
    341 addMessageListener("set-normal-ui-service", function (testName) {
    342  DummyUIService.testName = testName;
    343  DummyUIService.expectedCompleteStatus = null;
    344  DummyUIService.expectedShowAction = "update";
    345  DummyUIService.expectedUpdateAction = "updateaddress";
    346  sendAsyncMessage("set-normal-ui-service-complete");
    347 });
    348 
    349 addMessageListener("set-reject-ui-service", function (testName) {
    350  DummyUIService.testName = testName;
    351  DummyUIService.expectedCompleteStatus = null;
    352  DummyUIService.expectedShowAction = "reject";
    353  DummyUIService.expectedUpdateAction = "error";
    354  sendAsyncMessage("set-reject-ui-service-complete");
    355 });
    356 
    357 addMessageListener("set-update-with-ui-service", function (testName) {
    358  DummyUIService.testName = testName;
    359  DummyUIService.expectedCompleteStatus = null;
    360  DummyUIService.expectedShowAction = "update";
    361  DummyUIService.expectedUpdateAction = "accept";
    362  sendAsyncMessage("set-update-with-ui-service-complete");
    363 });
    364 
    365 addMessageListener("set-update-with-error-ui-service", function (testName) {
    366  DummyUIService.testName = testName;
    367  DummyUIService.expectedCompleteStatus = null;
    368  DummyUIService.expectedShowAction = "update";
    369  DummyUIService.expectedUpdateAction = "error";
    370  sendAsyncMessage("set-update-with-error-ui-service-complete");
    371 });
    372 
    373 addMessageListener("test-show-response-init", testShowResponseInit);
    374 
    375 addMessageListener("set-complete-status-success", function () {
    376  DummyUIService.expectedCompleteStatus = "success";
    377  sendAsyncMessage("set-complete-status-success-complete");
    378 });
    379 
    380 addMessageListener("set-complete-status-fail", function () {
    381  DummyUIService.expectedCompleteStatus = "fail";
    382  sendAsyncMessage("set-complete-status-fail-complete");
    383 });
    384 
    385 addMessageListener("set-complete-status-unknown", function () {
    386  DummyUIService.expectedCompleteStatus = "unknown";
    387  sendAsyncMessage("set-complete-status-unknown-complete");
    388 });
    389 
    390 addMessageListener("teardown", function () {
    391  paymentSrv.setTestingUIService(null);
    392  sendAsyncMessage("teardown-complete");
    393 });