Commit 283e59c2 authored by Eden Chuang's avatar Eden Chuang
Browse files

Bug 1383293 - Mochitest for testing the new readonly attribute...

Bug 1383293 - Mochitest for testing the new readonly attribute topLevelPrincipal in nsIPaymentRequest. r=baku

1. Adding new checks in ConstructorChromeScript.js to checking the top level
   document's principal information.
2. Adding a new test testCrossOriginTopLevelPrincipal in
   test_constructor.html to testing the topLevelPrincipal attribute under
   cross origin situation.
3. Removing some useless mochitest browser-chrome testcase.
parent 34196fb8
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -14,6 +14,11 @@ function emitTestFail(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.");
  }
@@ -72,6 +77,11 @@ function checkSimplestRequest(payRequest) {
}

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.");
  }
@@ -473,11 +483,32 @@ function checkMultipleRequestsHandler () {
  sendAsyncMessage("check-complete");
}

function checkCrossOriginTopLevelPrincipalHandler() {
  const paymentEnum = paymentSrv.enumerate();
  if (!paymentEnum.hasMoreElements()) {
    emitTestFail("PaymentRequestService should have at least one payment request.");
  }
  while (paymentEnum.hasMoreElements()) {
    let payRequest = paymentEnum.getNext().QueryInterface(Ci.nsIPaymentRequest);
    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-duplicate-shipping-options-request", checkDuplicateShippingOptionsRequestHandler);
addMessageListener("check-multiple-requests", checkMultipleRequestsHandler);
addMessageListener("check-nonbasiccard-request", checkNonBasicCardRequestHandler);
addMessageListener("check-cross-origin-top-level-principal", checkCrossOriginTopLevelPrincipalHandler);

addMessageListener("teardown", function() {
  paymentSrv.cleanup();
+0 −3
Original line number Diff line number Diff line
@@ -4,8 +4,5 @@ skip-if = !e10s
support-files =
  head.js
  simple_payment_request.html
  multiple_payment_request.html

[browser_payment_construction.js]
[browser_multiple_construction.js]
[browser_payment_in_different_tabs.js]
+0 −34
Original line number Diff line number Diff line
"use strict";

// kTestRoot is from head.js
const kTestPage = kTestRoot + "multiple_payment_request.html";

registerCleanupFunction(cleanup);

add_task(async function() {
  Services.prefs.setBoolPref("dom.payments.request.enabled", true);
  await BrowserTestUtils.withNewTab(kTestPage,
    function(browser) {

      const paymentSrv = Cc["@mozilla.org/dom/payments/payment-request-service;1"].getService(Ci.nsIPaymentRequestService);
      ok(paymentSrv, "Fail to get PaymentRequestService.");

      const paymentEnum = paymentSrv.enumerate();
      ok(paymentEnum.hasMoreElements(), "PaymentRequestService should have at least one payment request.");
      while (paymentEnum.hasMoreElements()) {
        let payment = paymentEnum.getNext().QueryInterface(Ci.nsIPaymentRequest);
        ok(payment, "Fail to get existing payment request.");
        if (payment.paymentDetails.id == "complex details") {
          checkComplexPayment(payment);
        } else if (payment.paymentDetails.id == "simple details") {
          checkSimplePayment(payment);
        } else if (payment.paymentDetails.id == "duplicate shipping options details") {
          checkDupShippingOptionsPayment(payment);
        } else {
          ok(false, "Unknown payment.");
        }
      }
      Services.prefs.setBoolPref("dom.payments.request.enabled", false);
    }
  );
});
+0 −26
Original line number Diff line number Diff line
"use strict";

// kTestRoot is from head.js
const kTestPage = kTestRoot + "simple_payment_request.html";

registerCleanupFunction(cleanup);

add_task(async function() {
  Services.prefs.setBoolPref("dom.payments.request.enabled", true);
  await BrowserTestUtils.withNewTab(kTestPage,
    function(browser) {

      const paymentSrv = Cc["@mozilla.org/dom/payments/payment-request-service;1"].getService(Ci.nsIPaymentRequestService);
      ok(paymentSrv, "Fail to get PaymentRequestService.");

      const paymentEnum = paymentSrv.enumerate();
      ok(paymentEnum.hasMoreElements(), "PaymentRequestService should have at least one payment request.");
      while (paymentEnum.hasMoreElements()) {
        let payment = paymentEnum.getNext().QueryInterface(Ci.nsIPaymentRequest);
        ok(payment, "Fail to get existing payment request.");
        checkSimplePayment(payment);
      }
      Services.prefs.setBoolPref("dom.payments.request.enabled", false);
    }
  );
});
+0 −72
Original line number Diff line number Diff line
@@ -30,78 +30,6 @@ function checkSimplePayment(aSimplePayment) {
  is(paymentOptions.shippingType, "shipping", "shippingType option should be 'shipping'");
}

function checkComplexPayment(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, "MyPay", "supported method should be 'MyPay'.");
  ok(!methodData.data, "methodData.data should not exist.");

  // checking the passed PaymentDetails parameter
  const details = aPayment.paymentDetails;
  is(details.id, "complex details", "details.id should be 'complex 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 displayItems = details.displayItems;
  is(displayItems.length, 2, "displayItems' length should be 2.");
  let item = displayItems.queryElementAt(0, Ci.nsIPaymentItem);
  is(item.label, "Original donation amount", "1st display item's label should be 'Original donation amount'.");
  is(item.amount.currency, "USD", "1st display item's currency should be 'USD'.");
  is(item.amount.value, "-65.00", "1st display item's value should be '-65.00'.");
  item = displayItems.queryElementAt(1, Ci.nsIPaymentItem);
  is(item.label, "Friends and family discount", "2nd display item's label should be 'Friends and family discount'.");
  is(item.amount.currency, "USD", "2nd display item's currency should be 'USD'.");
  is(item.amount.value, "10.00", "2nd display item's value should be '10.00'.");

  const modifiers = details.modifiers;
  is(modifiers.length, 1, "modifiers' length should be 1.");

  const modifier = modifiers.queryElementAt(0, Ci.nsIPaymentDetailsModifier);
  is(modifier.supportedMethods, "MyPay", "modifier's supported method name should be 'MyPay'.");
  is(modifier.total.label, "Discounted donation", "modifier's total label should be 'Discounted donation'.");
  is(modifier.total.amount.currency, "USD", "modifier's total currency should be 'USD'.");
  is(modifier.total.amount.value, "45.00", "modifier's total value should be '45.00'.");

  const additionalItems = modifier.additionalDisplayItems;
  is(additionalItems.length, "1", "additionalDisplayItems' length should be 1.");
  const additionalItem = additionalItems.queryElementAt(0, Ci.nsIPaymentItem);
  is(additionalItem.label, "MyPay discount", "additional item's label should be 'MyPay discount'.");
  is(additionalItem.amount.currency, "USD", "additional item's currency should be 'USD'.");
  is(additionalItem.amount.value, "-10.00", "additional item's value should be '-10.00'.");
  ok(modifier.data, "moidifer.data should exist.");
  is(modifier.data.discountProgramParticipantId, "86328764873265",
     "modifier.data.discountProgramParticipantId should be '86328764873265'.");

  const shippingOptions = details.shippingOptions;
  is(shippingOptions.length, 2, "shippingOptions' length should be 2.");

  let shippingOption = shippingOptions.queryElementAt(0, Ci.nsIPaymentShippingOption);
  is(shippingOption.id, "NormalShipping", "1st shippingOption's id should be 'NoramlShpping'.");
  is(shippingOption.label, "NormalShipping", "1st shippingOption's lable should be 'NormalShipping'.");
  is(shippingOption.amount.currency, "USD", "1st shippingOption's amount currency should be 'USD'.");
  is(shippingOption.amount.value, "10.00", "1st shippingOption's amount value should be '10.00'.");
  ok(shippingOption.selected, "1st shippingOption should be selected.");

  shippingOption = shippingOptions.queryElementAt(1, Ci.nsIPaymentShippingOption);
  is(shippingOption.id, "FastShipping", "2nd shippingOption's id should be 'FastShpping'.");
  is(shippingOption.label, "FastShipping", "2nd shippingOption's lable should be 'FastShipping'.");
  is(shippingOption.amount.currency, "USD", "2nd shippingOption's amount currency should be 'USD'.");
  is(shippingOption.amount.value, "30.00", "2nd shippingOption's amount value should be '30.00'.");
  ok(!shippingOption.selected, "2nd shippingOption should not be selected.");

  // 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'");
}

function checkDupShippingOptionsPayment(aPayment) {
  // checking the passed PaymentMethods parameter
  is(aPayment.paymentMethods.length, 1, "paymentMethods' length should be 1.");
Loading