<!DOCTYPE html>
<!--  Copyright © 2017 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang).  -->
<!--  Copyright (C) 2017 Apple Inc. All rights reserved.  -->
<!-- FIXME: Upstream this test to web-platform-tests/payment-request/. -->
<meta charset="utf-8">
<title>Test for PaymentRequest.abort() method</title>
<link rel="help" href="https://w3c.github.io/browser-payment-api/#abort-method">
<script src="/js-test-resources/ui-helper.js"></script>
<script src="/resources/payment-request.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
"use strict";
setup(() => {}, {
  // Ignore unhandled rejections resulting from .show()'s acceptPromise
  // not being explicitly handled.
  allow_uncaught_exception: true,
});
const applePay = Object.freeze({
    supportedMethods: "https://apple.com/apple-pay",
    data: {
        version: 2,
        merchantIdentifier: '',
        merchantCapabilities: ['supports3DS'],
        supportedNetworks: ['visa', 'masterCard'],
        countryCode: 'US',
    },
});
const defaultMethods = Object.freeze([applePay]);
const defaultDetails = Object.freeze({
  total: {
    label: "Total",
    amount: {
      currency: "USD",
      value: "1.00",
    },
  },
});

promise_test(async t => {
  // request is in "created" state
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  await promise_rejects_dom(t, "InvalidStateError", request.abort());
}, `Throws if the promise [[state]] is not "interactive"`);

promise_test(async t => {
  // request is in "created" state.
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  await promise_rejects_dom(t, "InvalidStateError", request.abort());
  // Call it again, for good measure.
  await promise_rejects_dom(t, "InvalidStateError", request.abort());
  // The request's state is "created", so let's show it
  // which changes the state to "interactive.".
  await activateThen(async () => {
    request.show().catch((error) => { });
    // Let's set request the state to "closed" by calling .abort()
    try {
      await request.abort();
    } catch (err) {
      assert_true(false, "Unexpected promise rejection: " + err.message);
    } 
  });
  // The request is now "closed", so...
  await promise_rejects_dom(t, "InvalidStateError", request.abort());
}, `Calling abort must not change the [[state]] until after "interactive"`);

user_activation_test(async t => {
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  const acceptPromise = request.show();
  try {
    await request.abort();
  } catch (err) {
    assert_true(false, "Unexpected promise rejection: " + err.message);
  }
  await promise_rejects_dom(t, "AbortError", acceptPromise);
  // As request is now "closed", trying to show it will fail
  await activateThen(async () => {
    await promise_rejects_dom(t, "InvalidStateError", request.show());
  });
}, "calling .abort() causes acceptPromise to reject and closes the request.");

</script>
