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 (2839B)


      1 <!DOCTYPE html>
      2 <!-- Copyright © 2017 World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
      3 <meta charset="utf-8">
      4 <title>Payment Method Basic Card: Tests that basic card is a supported method</title>
      5 <link rel="help" href="https://w3c.github.io/browser-payment-api/#show-method">
      6 <script src="/resources/testharness.js"></script>
      7 <script src="/resources/testharnessreport.js"></script>
      8 <script>
      9 const basicCard = Object.freeze({ supportedMethods: "basic-card", data: {} });
     10 const defaultMethods = Object.freeze([basicCard]);
     11 const defaultDetails = Object.freeze({
     12  total: {
     13    label: "Total",
     14    amount: {
     15      currency: "USD",
     16      value: "1.00",
     17    },
     18  },
     19 });
     20 
     21 const notSupportedMethod = Object.freeze({
     22  supportedMethods: "this-is-not-supported",
     23 });
     24 
     25 promise_test(async t => {
     26  const request = new PaymentRequest([notSupportedMethod], defaultDetails);
     27  assert_false(
     28    await request.canMakePayment(),
     29    `canMakePaymentPromise should be false`
     30  );
     31 }, `Must return false when the PMI is not supported at by the user agent.`);
     32 
     33 function* pmiGenerator(howMany = 256) {
     34  for (i = 0; i < howMany; i++) {
     35    yield {
     36      supportedMethods: `this-is-not-supported`,
     37      data: { key: "value" },
     38    };
     39  }
     40 }
     41 
     42 promise_test(async t => {
     43  //Smoke test
     44  const canMakePaymentPromise = new PaymentRequest(
     45    [notSupportedMethod],
     46    defaultDetails
     47  ).canMakePayment();
     48  assert_false(
     49    await canMakePaymentPromise,
     50    "Expected canMakePaymentPromise smoke test to resolve with false"
     51  );
     52  // Actual test - Make a big array with random PMIs,
     53  // put basic-card in the middle of it!
     54  const pmis = []
     55    .concat(Array.from(pmiGenerator(250)))
     56    .concat(basicCard)
     57    .concat(Array.from(pmiGenerator(250)));
     58  const request = new PaymentRequest(pmis, defaultDetails);
     59  assert_true(
     60    await request.canMakePayment(),
     61    `canMakePaymentPromise should be true, because basic-card is present`
     62  );
     63 }, `Must return true when basic-card is amongst unsupported PMIs.`);
     64 
     65 promise_test(async t => {
     66  const request = new PaymentRequest(defaultMethods, defaultDetails);
     67  try {
     68    assert_true(
     69      await request.canMakePayment(),
     70      `canMakePaymentPromise must resolve to true`
     71    );
     72    assert_true(
     73      await request.canMakePayment(),
     74      `canMakePaymentPromise must resolve to true`
     75    );
     76    // try to trigger optional behavior
     77    for (let i = 0; i < 1024; i++) {
     78      const temp = new PaymentRequest(defaultMethods, defaultDetails);
     79      await Promise.all([temp.canMakePayment(), request.canMakePayment()]);
     80    }
     81  } catch (err) {
     82    assert_equals(
     83      err.name,
     84      "NotAllowedError",
     85      "if it throws, then it must be a NotAllowedError."
     86    );
     87  }
     88 }, `If basic-card is supported, then return a promise that resolves to true.`);
     89 </script>