tor-browser

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

payment-request-canmakepayment-method.https.html (4354B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8">
      3 <title>Tests for PaymentRequest.canMakePayment() method</title>
      4 <link rel="help" href="https://w3c.github.io/payment-request/#canmakepayment-method">
      5 <script src="/resources/testharness.js"></script>
      6 <script src="/resources/testharnessreport.js"></script>
      7 <script src="/resources/testdriver.js"></script>
      8 <script src='/resources/testdriver-vendor.js'></script>
      9 <script>
     10 "use strict";
     11 const basicCard = Object.freeze({ supportedMethods: "basic-card" });
     12 const applePay = Object.freeze({
     13  supportedMethods: "https://apple.com/apple-pay",
     14  data: {
     15    version: 3,
     16    merchantIdentifier: "merchant.com.example",
     17    countryCode: "US",
     18    merchantCapabilities: ["supports3DS"],
     19    supportedNetworks: ["visa"],
     20  }
     21 });
     22 const defaultMethods = Object.freeze([basicCard, applePay]);
     23 const defaultDetails = Object.freeze({
     24  total: {
     25    label: "Total",
     26    amount: {
     27      currency: "USD",
     28      value: "1.00",
     29    },
     30  },
     31 });
     32 
     33 const unsupportedMethods = [
     34  { supportedMethods: "this-is-not-supported" },
     35  { supportedMethods: "https://not.supported" },
     36 ];
     37 
     38 promise_test(async t => {
     39  const request = new PaymentRequest(defaultMethods, defaultDetails);
     40  assert_true(
     41    await request.canMakePayment(),
     42    "one of the methods should be supported"
     43  );
     44 }, `If payment method identifier are supported, resolve promise with true.`);
     45 
     46 promise_test(async t => {
     47  const request = new PaymentRequest(defaultMethods, defaultDetails);
     48  try {
     49    assert_true(
     50      await request.canMakePayment(),
     51      `canMakePaymentPromise should be true`
     52    );
     53    assert_true(
     54      await request.canMakePayment(),
     55      `canMakePaymentPromise should be true`
     56    );
     57  } catch (err) {
     58    assert_equals(
     59      err.name,
     60      "NotAllowedError",
     61      "if it throws, then it must be a NotAllowedError."
     62    );
     63  }
     64 }, `If request.[[state]] is "created", then return a promise that resolves to true for known method.`);
     65 
     66 promise_test(async t => {
     67  const noneSupported = new PaymentRequest(
     68    unsupportedMethods,
     69    defaultDetails
     70  ).canMakePayment();
     71  assert_false(await noneSupported, `methods must not be supported`);
     72 }, "All methods are unsupported");
     73 
     74 promise_test(async t => {
     75  const someSupported = new PaymentRequest(
     76    [...unsupportedMethods, ...defaultMethods],
     77    defaultDetails
     78  ).canMakePayment();
     79  assert_true(await someSupported, `At least one method is expected to be supported.`);
     80 }, `Mix of supported and unsupported methods, at least one method is supported.`);
     81 
     82 promise_test(async t => {
     83  const request = new PaymentRequest(defaultMethods, defaultDetails);
     84  const [acceptPromise, canMakePaymentPromise] = await test_driver.bless(
     85    "show payment request",
     86    () => {
     87      const acceptPromise = request.show(); // Sets state to "interactive"
     88      const canMakePaymentPromise = request.canMakePayment();
     89      return [acceptPromise, canMakePaymentPromise];
     90    });
     91 
     92  await promise_rejects_dom(t, "InvalidStateError", canMakePaymentPromise);
     93  request.abort();
     94  await promise_rejects_dom(t, "AbortError", acceptPromise);
     95 
     96  // The state should be "closed"
     97  await promise_rejects_dom(t, "InvalidStateError", request.canMakePayment());
     98 }, 'If request.[[state]] is "interactive", then return a promise rejected with an "InvalidStateError" DOMException.');
     99 
    100 promise_test(async t => {
    101  const request = new PaymentRequest(defaultMethods, defaultDetails);
    102  const [abortPromise, acceptPromise] = await test_driver.bless(
    103    "show payment request",
    104    () => {
    105      const acceptPromise = request.show(); // Sets state to "interactive"
    106      acceptPromise.catch(() => {}); // no-op, just to silence unhandled rejection in devtools.
    107      const abortPromise = request.abort();  // Sets state to "closed"
    108      return [abortPromise, acceptPromise];
    109    });
    110 
    111  await abortPromise;
    112  await promise_rejects_dom(t, "AbortError", acceptPromise);
    113  await promise_rejects_dom(t, "InvalidStateError", request.canMakePayment());
    114 }, 'If request.[[state]] is "closed", then return a promise rejected with an "InvalidStateError" DOMException.');
    115 </script>
    116 
    117 <small>
    118  If you find a buggy test, please <a href="https://github.com/web-platform-tests/wpt/issues">file a bug</a>
    119  and tag one of the <a href="https://github.com/web-platform-tests/wpt/blob/master/payment-request/META.yml">suggested reviewers</a>.
    120 </small>