summaryrefslogtreecommitdiff
path: root/dom/ipc/test.xul
blob: 13425779c33106a3d07690180411819b65702be5 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="800" height="800" orient="vertical" onload="initRemoteFrameScript();">
  <script>

    function dumpClientRect(r) {
      dump(r.left + "," + r.top + "," + r.right + "," +
           r.bottom + "," + r.width + "," + r.height + "\n");
    }

    function handleMozAfterPaint(e) {
      return;
      dump(e.type + "\n")
      var rects = e.clientRects;
      var i;
      dump("\tclientRects:\n");
      for (i = 0; i &lt; rects.length; ++i) {
        var r = rects.item(i);
        dump("\t\t");
        dumpClientRect(rects.item(i));
      }

      dump("\tboundingClientRect\n\t\t");
      dumpClientRect(e.boundingClientRect);

      var paintRequests = e.paintRequests;
      dump("\tpaintRequests\n");
      for (i = 0; i &lt; paintRequests.length; ++i) {
        var pr = paintRequests.item(i);
        dump("\t\t");
        dumpClientRect(pr.clientRect);
        if (pr.reason)
          dump("\t\t" + pr.reason + "\n");
      }
    }

    function handleMozScrolledAreaChanged(e) {
      return;
      dump(e.type + "\n");
      dump("\t" + e.x + "," + e.y + "," + e.width + "," + e.height + "\n");
    }

    function restart() {
      var y = document.getElementById('page');
      var p = y.parentNode;
      p.removeChild(y);
      p.appendChild(y);

      var fl = y.QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
      fl.activateFrameEvent("MozAfterPaint", true);
      fl.activateFrameEvent("MozScrolledAreaChanged", true);
      y.addEventListener("MozAfterPaint", handleMozAfterPaint, true);
      y.addEventListener("MozScrolledAreaChanged", handleMozScrolledAreaChanged, true);
    }
    
    function loadURL(url) {
      document.getElementById('page').setAttribute('src', url);
    }

    function randomClick() {
       // First focus the remote frame, then dispatch click. This way remote frame gets focus before
       // mouse event.
       document.getElementById('page').focus();
       var frameLoader = document.getElementById('page').QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
       var x = parseInt(Math.random() * 100);
       var y = parseInt(Math.random() * 100);
       frameLoader.sendCrossProcessMouseEvent("mousedown", x, y, 0, 1, 0, false);
       frameLoader.sendCrossProcessMouseEvent("mouseup", x, y, 0, 1, 0, false);
     }

    function keyPress() {
       // First focus the remote frame, then dispatch click. This way remote frame gets focus before
       // mouse event.
       document.getElementById('page').focus();
       var frameLoader = document.getElementById('page').QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;

       var keyCode = Components.interfaces.nsIDOMKeyEvent.DOM_VK_A;
       frameLoader.sendCrossProcessKeyEvent("keydown", keyCode, 0, 0);
       frameLoader.sendCrossProcessKeyEvent("keypress", keyCode, 0, 0);
       frameLoader.sendCrossProcessKeyEvent("keyup", keyCode, 0, 0);
     }

    function openWindow() {
      window.open('chrome://global/content/test-ipc.xul', '_blank', 'chrome,resizable,width=800,height=800');
    }
    
    function closeWindow() {
      window.close();
    }

    function initRemoteFrameScript() {
      // 1. Test that loading a script works, and that accessing process level mm and
      //     global mm works.
      var ppm = Components.classes["@mozilla.org/parentprocessmessagemanager;1"]
                          .getService(Components.interfaces.nsIMessageBroadcaster);
      var gm = Components.classes["@mozilla.org/globalmessagemanager;1"]
                         .getService(Components.interfaces.nsIMessageBroadcaster);
      var cpm = Components.classes["@mozilla.org/childprocessmessagemanager;1"]
                            .getService(Components.interfaces.nsISyncMessageSender);

      if (ppm.childCount != 2) {
        alert("Should have two child processes!");
      }
      var childprocessmm = ppm.getChildAt(1); // 0 is the in-process child process mm
      
      childprocessmm.addMessageListener("ppm-sync",
        function(m) {
          if (m.target != childprocessmm) alert("Wrong target!");
          document.getElementById("messageLog").value += "[SYNC1 PPM]"; 
        }
      );
      
      ppm.addMessageListener("ppm-sync",
        function(m) {
          // Check that global process message manager gets the per-process mm as target.
          if (m.target != childprocessmm) alert("Wrong target!");
          document.getElementById("messageLog").value += "[SYNC2 PPM]"; 
        }
      );
      childprocessmm.addMessageListener("ppm-async",
        function(m) {
          if (m.target != childprocessmm) alert("Wrong target!");
          document.getElementById("messageLog").value += "[ASYNC1 PPM]"; 
        }
      );
      ppm.addMessageListener("ppm-async",
        function(m) {
          // Check that global process message manager gets the per-process mm as target.
          if (m.target != childprocessmm) alert("Wrong target!");
          document.getElementById("messageLog").value += "[ASYNC2 PPM]"; 
        }
      );
      messageManager.loadFrameScript("chrome://global/content/remote-test-ipc.js", true);
      ppm.sendAsyncMessage("cpm-async");

      // 2. Test that adding message listener works, and that receiving a sync message works.
      messageManager.addMessageListener("linkclick",
        function(m) {
          // This checks that json sending works in sync messages. 
          document.getElementById("messageLog").value = m.name + ": " + m.json.href;
          return { message: "linkclick-received" };
        });

      // 3. Test that returning multiple json results works.
      messageManager.addMessageListener("linkclick",
        function(m) {
          return { message: "linkclick-received" };
        });

      // 4. Test that receiving an async message works.
      //    Test also that sending async message to content works.
      //    Test also that { receiveMessage: function(m) {} } works.
      messageManager.addMessageListener("linkclick-reply-object",
        { foobarObjectVar: true,
          receiveMessage: function(m) {
            var s = (m.json.message == "linkclick-received") &amp;&amp;
                    (this.foobarObjectVar) ? "PASS" : "FAIL";
            messageManager.broadcastAsyncMessage("chrome-message", { ok : s } );
          }
        }
        );

      // 5. Final test to check that everything went ok.
      messageManager.addMessageListener("chrome-message-reply",
        function(m) {
          // Check that 'this' and .target values are handled correctly
          if (m.target == document.getElementById("page") &amp;&amp;
              this == messageManager) {
            // Check that the message properties are enumerable.
            var hasName = false;
            var hasSync = false;
            var hasJSON = false;
            for (i in m) {
              if (i == "name") {
                hasName = true;
              } else if (i == "sync") {
                hasSync = true;
              } else if (i == "json") {
                hasJSON = true;
              }
            }
            if (hasName &amp;&amp; hasSync &amp;&amp; hasJSON) {
              document.getElementById("messageLog").value += ", " + m.json.ok;
            }
          }
        });
    }

    var speedTestStartTime = 0;
    var speedTestCount = 0;
    function messageSpeed() {
      speedTestCount = 0;
      messageManager.addMessageListener("speed-test", speedHandler);
      messageManager.broadcastAsyncMessage("speed-test-start");
    }

    function speedHandler() {
      if (!speedTestCount) {
        speedTestStartTime = new Date().getTime();
      }
      if (++speedTestCount == 1000) {
        setTimeout("alert('" + speedTestCount + " in "  + (new Date().getTime() - speedTestStartTime) +  "ms')", 0);
        return { message: "done" };
      }
      return { message: "continue" };
    }

    var addRemoveTestCount = 0;

    function echoListener() {
      if (++addRemoveTestCount == 1) {
        alert("Expected echo message");
        messageManager.removeMessageListener("async-echo", echoListener);
        messageManager.broadcastAsyncMessage("async-echo");
        return;
      }
      alert("Unexpected echo message");
    }

    function listenerAddRemove() {
      addRemoveTestCount = 0;
      messageManager.addMessageListener("async-echo", echoListener);
      messageManager.broadcastAsyncMessage("async-echo");
    }

    var MozAfterPaintCount = 0;
    function enableMozAfterPaint() {
      messageManager.addMessageListener("MozAfterPaint",
        function(m) {
          document.getElementById("messageLog").value = m.name + "[" + (++MozAfterPaintCount) + "]";
        });
      messageManager.loadFrameScript("data:,addEventListener('MozAfterPaint', function(e) { sendAsyncMessage('MozAfterPaint'); },true);", false);
    }

   var Ci = Components.interfaces;
   var Cu = Components.utils;
   Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  </script>

  <toolbar id="controls">
    <toolbarbutton label="Back"/>
    <toolbarbutton label="Forward"/>
    <textbox onchange="loadURL(this.value)" flex="1" id="URL"/>
  </toolbar>
  <toolbar>
    <toolbarbutton onclick="restart()" label="Recover"/>
    <toolbarbutton onclick="randomClick()" label="random click"/>
    <toolbarbutton onclick="keyPress()" label="key press"/>
    <toolbarbutton onclick="messageSpeed()" label="test message handling speed"/>
    <toolbarbutton onclick="listenerAddRemove()" label="test listener add/remove"/>
    <toolbarbutton onclick="enableMozAfterPaint()" label="MozAfterPaint"/>
    <toolbarbutton onclick="openWindow()" label="open new window"/>
    <toolbarbutton onclick="closeWindow()" label="close this window"/>
  </toolbar>
  <toolbar><label value="Load a script (URL) to content process:"/>
    <textbox flex="1" id="script"/><button
      label="send" oncommand="document.getElementById('page')
                                      .QueryInterface(Components.interfaces.nsIFrameLoaderOwner)
                                      .frameLoader.messageManager
                                      .loadFrameScript(this.previousSibling.value, false);"/>
  </toolbar>
  <toolbar>
    <label value="Eval script in chrome context"/>
    <textbox flex="1"/><button label="run" oncommand="eval(this.previousSibling.value);"/>
  </toolbar>

  <browser type="content" src="http://www.google.com/" flex="1" id="page" remote="true"/>
  <label id="messageLog" value="" crop="center"/>
</window>