|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 XPCOMUtils.defineLazyModuleGetter(this, "Promise", |
|
5 "resource://gre/modules/Promise.jsm"); |
|
6 |
|
7 function whenNewWindowLoaded(aOptions, aCallback) { |
|
8 let win = OpenBrowserWindow(aOptions); |
|
9 let gotLoad = false; |
|
10 let gotActivate = (Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager).activeWindow == win); |
|
11 |
|
12 function maybeRunCallback() { |
|
13 if (gotLoad && gotActivate) { |
|
14 win.BrowserChromeTest.runWhenReady(function() { |
|
15 executeSoon(function() { aCallback(win); }); |
|
16 }); |
|
17 } |
|
18 } |
|
19 |
|
20 if (!gotActivate) { |
|
21 win.addEventListener("activate", function onActivate() { |
|
22 info("Got activate."); |
|
23 win.removeEventListener("activate", onActivate, false); |
|
24 gotActivate = true; |
|
25 maybeRunCallback(); |
|
26 }, false); |
|
27 } else { |
|
28 info("Was activated."); |
|
29 } |
|
30 |
|
31 win.addEventListener("load", function onLoad() { |
|
32 info("Got load"); |
|
33 win.removeEventListener("load", onLoad, false); |
|
34 gotLoad = true; |
|
35 maybeRunCallback(); |
|
36 }, false); |
|
37 return win; |
|
38 } |
|
39 |
|
40 /** |
|
41 * Recursively compare two objects and check that every property of expectedObj has the same value |
|
42 * on actualObj. |
|
43 */ |
|
44 function isSubObjectOf(expectedObj, actualObj, name) { |
|
45 for (let prop in expectedObj) { |
|
46 if (typeof expectedObj[prop] == 'function') |
|
47 continue; |
|
48 if (expectedObj[prop] instanceof Object) { |
|
49 is(actualObj[prop].length, expectedObj[prop].length, name + "[" + prop + "]"); |
|
50 isSubObjectOf(expectedObj[prop], actualObj[prop], name + "[" + prop + "]"); |
|
51 } else { |
|
52 is(actualObj[prop], expectedObj[prop], name + "[" + prop + "]"); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 function getLocale() { |
|
58 const localePref = "general.useragent.locale"; |
|
59 return getLocalizedPref(localePref, Services.prefs.getCharPref(localePref)); |
|
60 } |
|
61 |
|
62 /** |
|
63 * Wrapper for nsIPrefBranch::getComplexValue. |
|
64 * @param aPrefName |
|
65 * The name of the pref to get. |
|
66 * @returns aDefault if the requested pref doesn't exist. |
|
67 */ |
|
68 function getLocalizedPref(aPrefName, aDefault) { |
|
69 try { |
|
70 return Services.prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data; |
|
71 } catch (ex) { |
|
72 return aDefault; |
|
73 } |
|
74 |
|
75 return aDefault; |
|
76 } |
|
77 |
|
78 function waitForPopupShown(aPopupId, aCallback) { |
|
79 let popup = document.getElementById(aPopupId); |
|
80 info("waitForPopupShown: got popup: " + popup.id); |
|
81 function onPopupShown() { |
|
82 info("onPopupShown"); |
|
83 removePopupShownListener(); |
|
84 SimpleTest.executeSoon(aCallback); |
|
85 } |
|
86 function removePopupShownListener() { |
|
87 popup.removeEventListener("popupshown", onPopupShown); |
|
88 } |
|
89 popup.addEventListener("popupshown", onPopupShown); |
|
90 registerCleanupFunction(removePopupShownListener); |
|
91 } |
|
92 |
|
93 function* promiseEvent(aTarget, aEventName, aPreventDefault) { |
|
94 let deferred = Promise.defer(); |
|
95 aTarget.addEventListener(aEventName, function onEvent(aEvent) { |
|
96 aTarget.removeEventListener(aEventName, onEvent, true); |
|
97 if (aPreventDefault) { |
|
98 aEvent.preventDefault(); |
|
99 } |
|
100 deferred.resolve(); |
|
101 }, true); |
|
102 return deferred.promise; |
|
103 } |
|
104 |
|
105 function waitForBrowserContextMenu(aCallback) { |
|
106 waitForPopupShown(gBrowser.selectedBrowser.contextMenu, aCallback); |
|
107 } |
|
108 |
|
109 function doOnloadOnce(aCallback) { |
|
110 function doOnloadOnceListener(aEvent) { |
|
111 info("doOnloadOnce: " + aEvent.originalTarget.location); |
|
112 removeDoOnloadOnceListener(); |
|
113 SimpleTest.executeSoon(function doOnloadOnceCallback() { |
|
114 aCallback(aEvent); |
|
115 }); |
|
116 } |
|
117 function removeDoOnloadOnceListener() { |
|
118 gBrowser.removeEventListener("load", doOnloadOnceListener, true); |
|
119 } |
|
120 gBrowser.addEventListener("load", doOnloadOnceListener, true); |
|
121 registerCleanupFunction(removeDoOnloadOnceListener); |
|
122 } |
|
123 |
|
124 function* promiseOnLoad() { |
|
125 let deferred = Promise.defer(); |
|
126 |
|
127 gBrowser.addEventListener("load", function onLoadListener(aEvent) { |
|
128 info("onLoadListener: " + aEvent.originalTarget.location); |
|
129 gBrowser.removeEventListener("load", onLoadListener, true); |
|
130 deferred.resolve(aEvent); |
|
131 }, true); |
|
132 |
|
133 return deferred.promise; |
|
134 } |
|
135 |