summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/browser/file_web_channel.html
blob: bcc9a13da03b7421405a4a6a15f03a6839c7c4c9 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>web_channel_test</title>
</head>
<body>
<script>
   var IFRAME_SRC_ROOT =
     "http://mochi.test:8888" +
     location.pathname.replace("web_channel.html", "web_channel_iframe.html");

  window.onload = function() {
    var testName = window.location.search.replace(/^\?/, "");

    switch (testName) {
      case "generic":
        test_generic();
        break;
      case "twoway":
        test_twoWay();
        break;
      case "multichannel":
        test_multichannel();
        break;
      case "iframe":
        test_iframe();
        break;
      case "iframe_pre_redirect":
        test_iframe_pre_redirect();
        break;
      case "unsolicited":
        test_unsolicited();
        break;
      case "bubbles":
        test_bubbles();
        break;
      case "object":
        test_object();
        break;
      case "error_thrown":
        test_error_thrown();
        break;
      case "error_invalid_channel":
        test_error_invalid_channel();
        break;
      default:
        throw new Error(`INVALID TEST NAME ${testName}`);
    }
  };

  function test_generic() {
    var event = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "generic",
        message: {
          something: {
            nested: "hello",
          },
        },
      }),
    });

    window.dispatchEvent(event);
  }

  function test_twoWay() {
    var firstMessage = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "twoway",
        message: {
          command: "one",
        },
      }),
    });

    window.addEventListener("WebChannelMessageToContent", function(e) {
      var secondMessage = new window.CustomEvent("WebChannelMessageToChrome", {
        detail: JSON.stringify({
          id: "twoway",
          message: {
            command: "two",
            detail: e.detail.message,
          },
        }),
      });

      if (!e.detail.message.error) {
        window.dispatchEvent(secondMessage);
      }
    }, true);

    window.dispatchEvent(firstMessage);
  }

  function test_multichannel() {
    var event1 = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "wrongchannel",
        message: {},
      }),
    });

    var event2 = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "multichannel",
        message: {},
      }),
    });

    window.dispatchEvent(event1);
    window.dispatchEvent(event2);
  }

  function test_iframe() {
    // Note that this message is the response to the message sent
    // by the iframe!  This is bad, as this page is *not* trusted.
    window.addEventListener("WebChannelMessageToContent", function(e) {
      // the test parent will fail if the echo message is received.
      echoEventToChannel(e, "echo");
    });

    // only attach the iframe for the iframe test to avoid
    // interfering with other tests.
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", IFRAME_SRC_ROOT + "?iframe");
    document.body.appendChild(iframe);
  }

  function test_iframe_pre_redirect() {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", IFRAME_SRC_ROOT + "?iframe_pre_redirect");
    document.body.appendChild(iframe);
  }

  function test_unsolicited() {
    // echo any unsolicted events back to chrome.
    window.addEventListener("WebChannelMessageToContent", function(e) {
      echoEventToChannel(e, "echo");
    }, true);
  }

  function test_bubbles() {
    var event = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "not_a_window",
        message: {
          command: "start",
        },
      }),
    });

    var nonWindowTarget = document.getElementById("not_a_window");

    nonWindowTarget.addEventListener("WebChannelMessageToContent", function(e) {
      echoEventToChannel(e, "not_a_window");
    }, true);


    nonWindowTarget.dispatchEvent(event);
  }

  function test_object() {
    let objectMessage = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: {
        id: "objects",
        message: { type: "object" },
      },
    });

    let stringMessage = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: "objects",
        message: { type: "string" },
      }),
    });
    // Test fails if objectMessage is received, we send stringMessage to know
    // when we should stop listening for objectMessage
    window.dispatchEvent(objectMessage);
    window.dispatchEvent(stringMessage);
  }

  function test_error_thrown() {
   var event = new window.CustomEvent("WebChannelMessageToChrome", {
     detail: JSON.stringify({
       id: "error",
       message: {
         command: "oops",
       },
     }),
   });

   // echo the response back to chrome - chrome will check it is the
   // expected error.
   window.addEventListener("WebChannelMessageToContent", function(e) {
     echoEventToChannel(e, "echo");
   }, true);

   window.dispatchEvent(event);
  }

  function test_error_invalid_channel() {
   var event = new window.CustomEvent("WebChannelMessageToChrome", {
     detail: JSON.stringify({
       id: "invalid-channel",
       message: {
         command: "oops",
       },
     }),
   });

   // echo the response back to chrome - chrome will check it is the
   // expected error.
   window.addEventListener("WebChannelMessageToContent", function(e) {
     echoEventToChannel(e, "echo");
   }, true);

   window.dispatchEvent(event);
  }

  function echoEventToChannel(e, channelId) {
    var echoedEvent = new window.CustomEvent("WebChannelMessageToChrome", {
      detail: JSON.stringify({
        id: channelId,
        message: e.detail.message,
      }),
    });

    e.target.dispatchEvent(echoedEvent);
  }
</script>

<div id="not_a_window"></div>
</body>
</html>