Backed out changeset 2bdf648e7015
[mozilla-central.git] / testing / mochitest / ipc.js
blob0a95b6f43d6aaec5f0d798db1a5b4132927bff6d
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Mozilla's layout acceptance tests.
15  *
16  * The Initial Developer of the Original Code is the Mozilla Foundation.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *   Joel Maher <joel.maher@gmail.com>, Mozilla Corporation (original author)
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
37 if (Cc === undefined) {
38   var Cc = Components.classes;
39   var Ci = Components.interfaces;
42 function ipcEvent(e) {
43     var sync = e.getData("sync");
44     var type = e.getData("type");
45     var data = JSON.parse(e.getData("data"));
47     switch(type) {
48     case 'LoggerInit':
49       MozillaFileLogger.init(data.filename);
50       break;
51     case 'Logger':
52       var logger = MozillaFileLogger.getLogCallback();
53       logger({"num":data.num, "level":data.level, "info": Array(data.info)});
54       break;
55     case 'LoggerClose':
56       MozillaFileLogger.close();
57       break;
58     case 'waitForFocus':
59       if (content)
60         var wrapper = content.wrappedJSObject.frames[0].SimpleTest;
61       else
62         var wrapper = SimpleTest;
63       ipctest.waitForFocus(wrapper[data.callback], data.targetWindow, data.expectBlankPage);
64       break;
65     default:
66       if (type == 'QuitApplication') {
67         removeEventListener("contentEvent", function (e) { ipcEvent(e); }, false, true);
68       }
70       if (sync == 1) {
71         return sendSyncMessage("chromeEvent", {"type":type, "data":data});
72       } else {
73         sendAsyncMessage("chromeEvent", {"type":type, "data":data});
74       }
75     }
78 var xulRuntime = Components.classes["@mozilla.org/xre/app-info;1"]
79                  .getService(Components.interfaces.nsIXULRuntime);
81 //check if we are in content process
82 if (xulRuntime.processType == 2) {
83   addEventListener("contentEvent", function (e) { ipcEvent(e); }, false, true);
86 var ipctest = {};
88 ipctest.waitForFocus_started = false;
89 ipctest.waitForFocus_loaded = false;
90 ipctest.waitForFocus_focused = false;
92 ipctest.waitForFocus = function (callback, targetWindow, expectBlankPage) {
94     if (targetWindow && targetWindow != undefined) {
95       var tempID = targetWindow;
96       targetWindow = null;
97       var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
98                          .getService(Ci.nsIWindowMediator);
99       var wm_enum = wm.getXULWindowEnumerator(null);
101       while(wm_enum.hasMoreElements()) {
102         var win = wm_enum.getNext().QueryInterface(Ci.nsIXULWindow)
103                   .docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow)
104                   .content.wrappedJSObject;
106         var domutils = win.QueryInterface(Ci.nsIInterfaceRequestor).
107                            getInterface(Ci.nsIDOMWindowUtils);
109         if (domutils.outerWindowID == tempID) {
110           targetWindow = win;
111         }
112       }
113     }
115     if (targetWindow == null || targetWindow == undefined)
116       if (content)
117         targetWindow = content.document.defaultView;
118       else
119         targetWindow = window;
121     ipctest.waitForFocus_started = false;
122     expectBlankPage = !!expectBlankPage;
124     var fm = Cc["@mozilla.org/focus-manager;1"].
125                         getService(Ci.nsIFocusManager);
127     var childTargetWindow = { };
128     fm.getFocusedElementForWindow(targetWindow, true, childTargetWindow);
129     childTargetWindow = childTargetWindow.value;
131     function debugFocusLog(prefix) {
132         if (content)
133           var wrapper = content.wrappedJSObject.frames[0].SimpleTest;
134         else
135           var wrapper = SimpleTest;
136         wrapper.ok(true, prefix + " -- loaded: " + targetWindow.document.readyState +
137            " active window: " +
138                (fm.activeWindow ? "(" + fm.activeWindow + ") " + fm.activeWindow.location : "<no window active>") +
139            " focused window: " +
140                (fm.focusedWindow ? "(" + fm.focusedWindow + ") " + fm.focusedWindow.location : "<no window focused>") +
141            " desired window: (" + targetWindow + ") " + targetWindow.location +
142            " child window: (" + childTargetWindow + ") " + childTargetWindow.location);
143     }
145     debugFocusLog("before wait for focus");
147     function maybeRunTests() {
148         debugFocusLog("maybe run tests <load:" +
149                       ipctest.waitForFocus_loaded + ", focus:" + ipctest.waitForFocus_focused + ">");
150         if (ipctest.waitForFocus_loaded &&
151             ipctest.waitForFocus_focused &&
152             !ipctest.waitForFocus_started) {
153             ipctest.waitForFocus_started = true;
154             if (content)
155               content.setTimeout(function() { callback(); }, 0, targetWindow);
156             else
157               setTimeout(function() { callback(); }, 0, targetWindow);
158         }
159     }
161     function waitForEvent(event) {
162         try {
163             debugFocusLog("waitForEvent called <type:" + event.type + ", target" + event.target + ">");
165             // Check to make sure that this isn't a load event for a blank or
166             // non-blank page that wasn't desired.
167             if (event.type == "load" && (expectBlankPage != (event.target.location == "about:blank")))
168                 return;
170             ipctest["waitForFocus_" + event.type + "ed"] = true;
171             var win = (event.type == "load") ? targetWindow : childTargetWindow;
172             win.removeEventListener(event.type, waitForEvent, true);
173             maybeRunTests();
174         } catch (e) {
175         }
176     }
178     // If the current document is about:blank and we are not expecting a blank
179     // page (or vice versa), and the document has not yet loaded, wait for the
180     // page to load. A common situation is to wait for a newly opened window
181     // to load its content, and we want to skip over any intermediate blank
182     // pages that load. This issue is described in bug 554873.
183     ipctest.waitForFocus_loaded =
184         (expectBlankPage == (targetWindow.location == "about:blank")) &&
185         targetWindow.document.readyState == "complete";
186     if (!ipctest.waitForFocus_loaded) {
187         targetWindow.addEventListener("load", waitForEvent, true);
188     }
190     // Check if the desired window is already focused.
191     var focusedChildWindow = { };
192     if (fm.activeWindow) {
193         fm.getFocusedElementForWindow(fm.activeWindow, true, focusedChildWindow);
194         focusedChildWindow = focusedChildWindow.value;
195     }
197     // If this is a child frame, ensure that the frame is focused.
198     ipctest.waitForFocus_focused = (focusedChildWindow == childTargetWindow);
199     if (ipctest.waitForFocus_focused) {
200         maybeRunTests();
201     }
202     else {
203         //TODO: is this really the childTargetWindow 
204         //      and are we really doing something with it here?
205         if (content) {
206           var wr = childTargetWindow.wrappedJSObject;
207           wr.addEventListener("focus", waitForEvent, true);
208           sendAsyncMessage("chromeEvent", {"type":"Focus", "data":{}});
209           wr.focus();
210         } else {
211           childTargetWindow.addEventListener("focus", waitForEvent, true);
212           childTargetWindow.focus();
213         }
214     }