summaryrefslogtreecommitdiffstats
path: root/dom/payments/test/test_payment-request-in-iframe.html
blob: 0a4b690f9b0ca7d798bdb5d3e20440c7279f8b85 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1318988
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1318988</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript">

  "use strict";
  SimpleTest.waitForExplicitFinish();

  function testRequestInSameOrigin() {
    return new Promise((resolve, reject) => {
      var ifr = document.createElement('iframe');

      let listener = function(event) {
        is(event.data, "successful",
          "Expected 'successful', but got '" + event.data + "'");
        resolve();
      };

      window.addEventListener("message", listener);

      ifr.src = "simple_payment_request.html";
      document.body.appendChild(ifr);

      ifr.addEventListener('load', function() {
        window.removeEventListener("message", listener);
      });
    });
  }

  function testRequestInIFrame() {
    return new Promise((resolve, reject) => {
      var ifr = document.createElement('iframe');

      let listener = function(event) {
        is(event.data, "SecurityError",
           "Expected 'SecurityError', but got '" + event.data + "'");
        resolve();
      };

      window.addEventListener("message", listener);

      ifr.src = "https://test1.example.com:443/tests/dom/payments/test/simple_payment_request.html";
      document.body.appendChild(ifr);

      ifr.addEventListener('load', function() {
        window.removeEventListener("message", listener);
      });
    });
  }

  function testRequestInIFrameWithAttribute() {
    return new Promise((resolve, reject) => {
      var ifrr = document.createElement('iframe');

      let listener = function(event) {
        is(event.data, "successful",
          "Expected 'successful', but got '" + event.data + "'");
        resolve();
      };

      window.addEventListener("message", listener);

      ifrr.setAttribute('allow', 'payment');
      ifrr.src = "https://test1.example.com:443/tests/dom/payments/test/simple_payment_request.html";
      document.body.appendChild(ifrr);

      ifrr.addEventListener('load', function() {
        window.removeEventListener("message", listener);
      });
    });
  }

  function testRequestWithAttributeChanged() {
    return new Promise((resolve, reject) => {
      var ifrr = document.createElement('iframe');

      let i = 0;

      ifrr.addEventListener('load', function() {
        if (i === 0) {
          ifrr.removeAttribute("allow");
        }
        ifrr.contentWindow.postMessage('new PaymentRequest', '*');
      });

      let listener = function(event) {
        i++;
        if (i === 1) {
          is(event.data, "successful",
             "Expected successful when running with allow=payment attribute.");
          ifrr.contentWindow.location.href = ifrr.src;
        } else {
          is(event.data, "SecurityError",
             "Expected SecurityError when running without allow=payment attribute.");
          window.removeEventListener("message", listener);
          resolve();
        }
      }
      window.addEventListener("message", listener);

      ifrr.setAttribute("allow", "payment");
      ifrr.src = "https://test1.example.com:443/tests/dom/payments/test/echo_payment_request.html";

      document.body.appendChild(ifrr);
    });
  }

  function testRequestInCrossOriginNestedIFrame() {
    return new Promise((resolve, reject) => {
      var ifrr = document.createElement('iframe');

      let listener = function(event) {
        if (ifrr.allow =! 'payment') {
          is(event.data, "SecurityError",
            "Expected 'SecurityError' without allow=payment in nested iframe");
          ifrr.setAttribute('allow', "payment");
          ifrr.contentWindow.location.href = ifrr.src;
        } else {
          is(event.data, "successful",
             "Expected 'successful' with allow='payment' in nested iframe");
          window.removeEventListener("message", listener);
          resolve();
        }
      };
      window.addEventListener("message", listener);

      ifrr.addEventListener("load", function() {
        ifrr.contentWindow.postMessage('new PaymentRequest in a new iframe', '*');
      })

      ifrr.src = "https://test1.example.com:443/tests/dom/payments/test/echo_payment_request.html";
      document.body.appendChild(ifrr);
    });
  }

  function runTests() {
    testRequestInSameOrigin()
    .then(testRequestInIFrame)
    .then(testRequestInIFrameWithAttribute)
    .then(testRequestWithAttributeChanged)
    .then(testRequestInCrossOriginNestedIFrame)
    .then(SimpleTest.finish)
    .catch( e => {
      ok(false, "Unexpected error: " + e.name);
      SimpleTest.finish();
    });
  }

  window.addEventListener('load', function() {
    SpecialPowers.pushPrefEnv({
      'set': [
        ['dom.payments.request.enabled', true],
      ]
    }, runTests);
  });
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1318988">Mozilla Bug 1318988</a>
</body>
</html>