dom/payments/test/test_requestShipping.html
author Manuel Rego Casasnovas <rego@igalia.com>
Mon, 19 Nov 2018 18:46:13 +0000
changeset 506778 359ee839f1a358175a0d4d537476d32a7f4a39a4
parent 504722 f1e380fe0a835cb90f83606feb8ac8f3933566d7
child 531525 ba6f655fd68963530c866d0d4a48c3db3d307777
permissions -rw-r--r--
Bug 1507805 [wpt PR 14092] - [css-grid] Some fixes on grid-container-scrollbar-* tests, a=testonly Automatic update from web-platform-tests[css-grid] Some fixes on grid-container-scrollbar-* tests * Use Ahem font to avoid weird small pixel differences on iOS. * Fix some typos reported by @fred-wang at https://bugs.webkit.org/show_bug.cgi?id=191656#c14 -- Merge pull request #14092 from mrego/grid-container-scrollbars [css-grid] Some fixes on grid-container-scrollbar-* tests -- wpt-commits: ef7824a8ff88c95d36b31b377fe252f7c56d2da0, 1b543a1083f48c13b25f23a5f23d14f6ade958d4 wpt-pr: 14092

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1436903
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1436903</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript">

  "use strict";
  SimpleTest.waitForExplicitFinish();

  var gUrl = SimpleTest.getTestFileURL('RequestShippingChromeScript.js');
  var gScript = SpecialPowers.loadChromeScript(gUrl);

  function testFailHandler(message) {
    ok(false, message);
  }
  gScript.addMessageListener("test-fail", testFailHandler);

  const defaultMethods = [{
    supportedMethods: "basic-card",
    data: {
      supportedNetworks: ['unionpay', 'visa', 'mastercard', 'amex', 'discover',
                          'diners', 'jcb', 'mir',
      ],
    },
  }, {
    supportedMethods: "testing-payment-method",
  }];
  const defaultDetails = {
    id: "test payment",
    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: "30.00"
        },
        selected: false,
      },
    ],
  };

  const defaultOptions = {
    requestPayerName: true,
    requestPayerEmail: false,
    requestPayerPhone: false,
    requestShipping: false,
    shippingType: "shipping"
  };

  const updatedOptionDetails = {
    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: "30.00"
        },
        selected: true,
      },
    ],
  };

  const nonSupportedMethods = [{
    supportedMethods: "nonsupported-method",
  }];


  function updateWithShippingAddress() {
    return new Promise((resolve, reject) => {
      resolve(defaultDetails);
    });
  }

  function updateWithShippingOption() {
    return new Promise((resolve, reject) => {
      resolve(updatedOptionDetails);
    });
  }

  function testShow() {
    gScript.sendAsyncMessage("set-normal-ui-service");
    return new Promise((resolve, reject) => {
      const payRequest = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);

      payRequest.addEventListener("shippingaddresschange", event => {
        event.updateWith(updateWithShippingAddress());
      });
      payRequest.addEventListener("shippingoptionchange", event => {
        event.updateWith(updateWithShippingOption());
      });

      const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
      payRequest.show().then(response => {
        response.complete("success").then(() =>{
          resolve();
        }).catch(e => {
          ok(false, "Unexpected error: " + e.name);
          resolve();
        });
      }).catch( e => {
        ok(false, "Unexpected error: " + e.name);
        resolve();
      }).finally(handler.destruct);
    });
  }

  function teardown() {
    ok(true, "Mandatory assert");
    gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() {
      gScript.removeMessageListener("teardown-complete", teardownCompleteHandler);
      gScript.removeMessageListener("test-fail", testFailHandler)
      gScript.destroy();
      SimpleTest.finish();
    });
    gScript.sendAsyncMessage("teardown");
  }

  function runTests() {
    testShow()
    .then(teardown)
    .catch( e => {
      ok(false, "Unexpected error: " + e.name);
      SimpleTest.finish();
    });
  }

  window.addEventListener('load', function() {
    SpecialPowers.pushPrefEnv({
      'set': [
        ['dom.payments.request.enabled', true],
      ]
    }, runTests);
  });

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1436903">Mozilla Bug 1436903</a>
</body>
</html>