michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const TEST_URI = "http://example.com/browser/dom/tests/browser/test-console-api.html"; michael@0: const TEST_URI_NAV = "http://example.com/browser/dom/tests/browser/"; michael@0: michael@0: let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] michael@0: .getService(Ci.nsIConsoleAPIStorage); michael@0: michael@0: var apiCallCount; michael@0: michael@0: var ConsoleObserver = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]), michael@0: michael@0: init: function CO_init() michael@0: { michael@0: Services.obs.addObserver(this, "console-storage-cache-event", false); michael@0: apiCallCount = 0; michael@0: }, michael@0: michael@0: observe: function CO_observe(aSubject, aTopic, aData) michael@0: { michael@0: if (aTopic == "console-storage-cache-event") { michael@0: apiCallCount ++; michael@0: if (apiCallCount == 4) { michael@0: Services.obs.removeObserver(this, "console-storage-cache-event"); michael@0: michael@0: try { michael@0: let tab = gBrowser.selectedTab; michael@0: let browser = gBrowser.selectedBrowser; michael@0: let win = browser.contentWindow; michael@0: let windowID = getWindowId(win); michael@0: let messages = ConsoleAPIStorage.getEvents(windowID); michael@0: ok(messages.length >= 4, "Some messages found in the storage service"); michael@0: michael@0: ConsoleAPIStorage.clearEvents(); michael@0: messages = ConsoleAPIStorage.getEvents(windowID); michael@0: is(messages.length, 0, "Cleared Storage"); michael@0: michael@0: // make sure a closed window's events are in fact removed from the michael@0: // storage cache michael@0: win.console.log("adding a new event"); michael@0: // Close the window. michael@0: gBrowser.removeTab(tab, {animate: false}); michael@0: // Ensure actual window destruction is not delayed (too long). michael@0: SpecialPowers.DOMWindowUtils.garbageCollect(); michael@0: // Ensure "inner-window-destroyed" event is processed, michael@0: // so the storage cache is cleared. michael@0: executeSoon(function () { michael@0: // use the old windowID again to see if we have any stray cached messages michael@0: messages = ConsoleAPIStorage.getEvents(windowID); michael@0: is(messages.length, 0, "tab close is clearing the cache"); michael@0: finish(); michael@0: }); michael@0: } catch (ex) { michael@0: dump(ex + "\n\n\n"); michael@0: dump(ex.stack + "\n\n\n"); michael@0: ok(false, "We got an unexpected exception"); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: function tearDown() michael@0: { michael@0: while (gBrowser.tabs.length > 1) michael@0: gBrowser.removeCurrentTab(); michael@0: } michael@0: michael@0: function test() michael@0: { michael@0: // Don't cache removed tabs, so "clear console cache on tab close" triggers. michael@0: Services.prefs.setIntPref("browser.tabs.max_tabs_undo", 0); michael@0: registerCleanupFunction(function() { michael@0: Services.prefs.clearUserPref("browser.tabs.max_tabs_undo"); michael@0: }); michael@0: michael@0: registerCleanupFunction(tearDown); michael@0: michael@0: ConsoleObserver.init(); michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: var tab = gBrowser.addTab(TEST_URI); michael@0: gBrowser.selectedTab = tab; michael@0: var browser = gBrowser.selectedBrowser; michael@0: browser.addEventListener("DOMContentLoaded", function onLoad(event) { michael@0: browser.removeEventListener("DOMContentLoaded", onLoad, false); michael@0: executeSoon(function test_executeSoon() { michael@0: let win = browser.contentWindow; michael@0: win.console.log("this", "is", "a", "log message"); michael@0: win.console.info("this", "is", "a", "info message"); michael@0: win.console.warn("this", "is", "a", "warn message"); michael@0: win.console.error("this", "is", "a", "error message"); michael@0: }); michael@0: }, false); michael@0: } michael@0: michael@0: function getWindowId(aWindow) michael@0: { michael@0: return aWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindowUtils) michael@0: .currentInnerWindowID; michael@0: }