michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: function test() { michael@0: // initialization michael@0: waitForExplicitFinish(); michael@0: let windowsToClose = []; michael@0: let innerID; michael@0: let beforeEvents; michael@0: let afterEvents; michael@0: let storageShouldOccur; michael@0: let consoleObserver; michael@0: let testURI = michael@0: "http://example.com/browser/dom/tests/browser/test-console-api.html"; michael@0: let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] michael@0: .getService(Ci.nsIConsoleAPIStorage); michael@0: michael@0: function getInnerWindowId(aWindow) { michael@0: return aWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindowUtils) michael@0: .currentInnerWindowID; michael@0: } michael@0: michael@0: function whenNewWindowLoaded(aOptions, aCallback) { michael@0: let win = OpenBrowserWindow(aOptions); michael@0: win.addEventListener("load", function onLoad() { michael@0: win.removeEventListener("load", onLoad, false); michael@0: aCallback(win); michael@0: }, false); michael@0: } michael@0: michael@0: function doTest(aIsPrivateMode, aWindow, aCallback) { michael@0: aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: michael@0: consoleObserver = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: if (aTopic == "console-api-log-event") { michael@0: afterEvents = ConsoleAPIStorage.getEvents(innerID); michael@0: is(beforeEvents.length == afterEvents.length - 1, storageShouldOccur, michael@0: "storage should" + (storageShouldOccur ? "" : " not") + " occur"); michael@0: michael@0: executeSoon(function() { michael@0: Services.obs.removeObserver(consoleObserver, "console-api-log-event"); michael@0: aCallback(); michael@0: }); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: aWindow.Services.obs.addObserver( michael@0: consoleObserver, "console-api-log-event", false); michael@0: aWindow.console.log("foo bar baz (private: " + aIsPrivateMode + ")"); michael@0: }, true); michael@0: michael@0: // We expect that console API messages are always stored. michael@0: storageShouldOccur = true; michael@0: innerID = getInnerWindowId(aWindow); michael@0: beforeEvents = ConsoleAPIStorage.getEvents(innerID); michael@0: aWindow.gBrowser.selectedBrowser.loadURI(testURI); michael@0: } michael@0: michael@0: function testOnWindow(aOptions, aCallback) { michael@0: whenNewWindowLoaded(aOptions, function(aWin) { michael@0: windowsToClose.push(aWin); michael@0: // execute should only be called when need, like when you are opening michael@0: // web pages on the test. If calling executeSoon() is not necesary, then michael@0: // call whenNewWindowLoaded() instead of testOnWindow() on your test. michael@0: executeSoon(function() aCallback(aWin)); michael@0: }); michael@0: }; michael@0: michael@0: // this function is called after calling finish() on the test. michael@0: registerCleanupFunction(function() { michael@0: windowsToClose.forEach(function(aWin) { michael@0: aWin.close(); michael@0: }); michael@0: }); michael@0: michael@0: // test first when not on private mode michael@0: testOnWindow({}, function(aWin) { michael@0: doTest(false, aWin, function() { michael@0: // then test when on private mode michael@0: testOnWindow({private: true}, function(aWin) { michael@0: doTest(true, aWin, finish); michael@0: }); michael@0: }); michael@0: }); michael@0: }