|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 function test() { |
|
5 // initialization |
|
6 waitForExplicitFinish(); |
|
7 let windowsToClose = []; |
|
8 let innerID; |
|
9 let beforeEvents; |
|
10 let afterEvents; |
|
11 let storageShouldOccur; |
|
12 let consoleObserver; |
|
13 let testURI = |
|
14 "http://example.com/browser/dom/tests/browser/test-console-api.html"; |
|
15 let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] |
|
16 .getService(Ci.nsIConsoleAPIStorage); |
|
17 |
|
18 function getInnerWindowId(aWindow) { |
|
19 return aWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
|
20 .getInterface(Ci.nsIDOMWindowUtils) |
|
21 .currentInnerWindowID; |
|
22 } |
|
23 |
|
24 function whenNewWindowLoaded(aOptions, aCallback) { |
|
25 let win = OpenBrowserWindow(aOptions); |
|
26 win.addEventListener("load", function onLoad() { |
|
27 win.removeEventListener("load", onLoad, false); |
|
28 aCallback(win); |
|
29 }, false); |
|
30 } |
|
31 |
|
32 function doTest(aIsPrivateMode, aWindow, aCallback) { |
|
33 aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
34 aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
35 |
|
36 consoleObserver = { |
|
37 observe: function(aSubject, aTopic, aData) { |
|
38 if (aTopic == "console-api-log-event") { |
|
39 afterEvents = ConsoleAPIStorage.getEvents(innerID); |
|
40 is(beforeEvents.length == afterEvents.length - 1, storageShouldOccur, |
|
41 "storage should" + (storageShouldOccur ? "" : " not") + " occur"); |
|
42 |
|
43 executeSoon(function() { |
|
44 Services.obs.removeObserver(consoleObserver, "console-api-log-event"); |
|
45 aCallback(); |
|
46 }); |
|
47 } |
|
48 } |
|
49 }; |
|
50 |
|
51 aWindow.Services.obs.addObserver( |
|
52 consoleObserver, "console-api-log-event", false); |
|
53 aWindow.console.log("foo bar baz (private: " + aIsPrivateMode + ")"); |
|
54 }, true); |
|
55 |
|
56 // We expect that console API messages are always stored. |
|
57 storageShouldOccur = true; |
|
58 innerID = getInnerWindowId(aWindow); |
|
59 beforeEvents = ConsoleAPIStorage.getEvents(innerID); |
|
60 aWindow.gBrowser.selectedBrowser.loadURI(testURI); |
|
61 } |
|
62 |
|
63 function testOnWindow(aOptions, aCallback) { |
|
64 whenNewWindowLoaded(aOptions, function(aWin) { |
|
65 windowsToClose.push(aWin); |
|
66 // execute should only be called when need, like when you are opening |
|
67 // web pages on the test. If calling executeSoon() is not necesary, then |
|
68 // call whenNewWindowLoaded() instead of testOnWindow() on your test. |
|
69 executeSoon(function() aCallback(aWin)); |
|
70 }); |
|
71 }; |
|
72 |
|
73 // this function is called after calling finish() on the test. |
|
74 registerCleanupFunction(function() { |
|
75 windowsToClose.forEach(function(aWin) { |
|
76 aWin.close(); |
|
77 }); |
|
78 }); |
|
79 |
|
80 // test first when not on private mode |
|
81 testOnWindow({}, function(aWin) { |
|
82 doTest(false, aWin, function() { |
|
83 // then test when on private mode |
|
84 testOnWindow({private: true}, function(aWin) { |
|
85 doTest(true, aWin, finish); |
|
86 }); |
|
87 }); |
|
88 }); |
|
89 } |