<!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 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>
