summaryrefslogtreecommitdiffstats
path: root/dom/payments/test/test_payerDetails.html
blob: 9a241803af357c060606895a8df1209c76d1b851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!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>