<!DOCTYPE HTML>
<meta charset="utf-8">
<title>Test for PaymentResponse.prototype.onpayerdetailchange</title>
<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="./DefaultData.js"></script>
<script>
  SimpleTest.waitForExplicitFinish();

  const gUrl = SimpleTest.getTestFileURL("PayerDetailsChromeScript.js");
  const gScript = SpecialPowers.loadChromeScript(gUrl);

  function okTester(result) {
    return message => ok(result, message);
  }
  const passListener = okTester(true);
  const failListener = okTester(false);

  gScript.addMessageListener("test-fail", failListener);
  gScript.addMessageListener("test-pass", passListener);

  function sendOnce(message) {
    return data => {
      return new Promise(resolve => {
        const doneMsg = `${message}-complete`;
        gScript.addMessageListener(doneMsg, function listener() {
          gScript.removeMessageListener(doneMsg, listener);
          resolve();
        });
        gScript.sendAsyncMessage(message, data);
      });
    };
  }
  const sendTearDown = sendOnce("teardown");

  async function loopTest(iterations) {
    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(
      true
    );
    const options = {
      requestPayerName: true,
      requestPayerEmail: true,
      requestPayerPhone: true,
    }
    const request = new PaymentRequest(defaultMethods, defaultDetails, options);
    const response = await request.show();
    is(response.payerName, "", ".payerName must initially be ''");
    is(response.payerEmail, "", ".payerEmail must initially be ''");
    is(response.payerPhone, "", ".payerPhone must initially be ''");
    for (let i = 0; i < iterations; i++) {
      const payer = {
        name: `test name ${i}`,
        phone: `test phone ${i}`,
        email: `test email ${i}`,
      }

      // Capture the event to firing
      const eventPromise = new Promise(resolve => {
        response.onpayerdetailchange = resolve;
      });
      const retryPromise = response.retry({
        error: "retry-fire-payerdetaichangeevent",
        payer
      });
      const event = await eventPromise;

      // Check things got updated
      is(response.payerName, payer.name, `.payerName must be "${payer.name}"`);
      is(response.payerEmail, payer.email, `.payerEmail must be "${payer.email}"`);
      is(response.payerPhone, payer.phone, `.payerPhone must be "${payer.phone}"`);

      // Finally, let's do an updateWith()
      event.updateWith({ error: "update-with", payerErrors: payer, ...defaultDetails });

      await retryPromise;
    }

    await response.complete("success");
    handler.destruct();
  }

  async function teardown() {
    await sendTearDown();
    gScript.removeMessageListener("test-fail", failListener);
    gScript.removeMessageListener("test-pass", passListener);
    gScript.destroy();
    SimpleTest.finish();
  }

  async function runTests() {
    try {
      await loopTest(5); // lets go around 5 times
    } catch (err) {
      ok(false, `Unexpected error: ${err}.`);
    } finally {
      await teardown();
    }
  }

  window.addEventListener("load", () => {
    const prefs = [["dom.payments.request.enabled", true]];
    SpecialPowers.pushPrefEnv({ set: prefs }, runTests);
  });
</script>

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