1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/browser/browser_ConsoleStorageAPITests.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const TEST_URI = "http://example.com/browser/dom/tests/browser/test-console-api.html"; 1.8 +const TEST_URI_NAV = "http://example.com/browser/dom/tests/browser/"; 1.9 + 1.10 +let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] 1.11 + .getService(Ci.nsIConsoleAPIStorage); 1.12 + 1.13 +var apiCallCount; 1.14 + 1.15 +var ConsoleObserver = { 1.16 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]), 1.17 + 1.18 + init: function CO_init() 1.19 + { 1.20 + Services.obs.addObserver(this, "console-storage-cache-event", false); 1.21 + apiCallCount = 0; 1.22 + }, 1.23 + 1.24 + observe: function CO_observe(aSubject, aTopic, aData) 1.25 + { 1.26 + if (aTopic == "console-storage-cache-event") { 1.27 + apiCallCount ++; 1.28 + if (apiCallCount == 4) { 1.29 + Services.obs.removeObserver(this, "console-storage-cache-event"); 1.30 + 1.31 + try { 1.32 + let tab = gBrowser.selectedTab; 1.33 + let browser = gBrowser.selectedBrowser; 1.34 + let win = browser.contentWindow; 1.35 + let windowID = getWindowId(win); 1.36 + let messages = ConsoleAPIStorage.getEvents(windowID); 1.37 + ok(messages.length >= 4, "Some messages found in the storage service"); 1.38 + 1.39 + ConsoleAPIStorage.clearEvents(); 1.40 + messages = ConsoleAPIStorage.getEvents(windowID); 1.41 + is(messages.length, 0, "Cleared Storage"); 1.42 + 1.43 + // make sure a closed window's events are in fact removed from the 1.44 + // storage cache 1.45 + win.console.log("adding a new event"); 1.46 + // Close the window. 1.47 + gBrowser.removeTab(tab, {animate: false}); 1.48 + // Ensure actual window destruction is not delayed (too long). 1.49 + SpecialPowers.DOMWindowUtils.garbageCollect(); 1.50 + // Ensure "inner-window-destroyed" event is processed, 1.51 + // so the storage cache is cleared. 1.52 + executeSoon(function () { 1.53 + // use the old windowID again to see if we have any stray cached messages 1.54 + messages = ConsoleAPIStorage.getEvents(windowID); 1.55 + is(messages.length, 0, "tab close is clearing the cache"); 1.56 + finish(); 1.57 + }); 1.58 + } catch (ex) { 1.59 + dump(ex + "\n\n\n"); 1.60 + dump(ex.stack + "\n\n\n"); 1.61 + ok(false, "We got an unexpected exception"); 1.62 + } 1.63 + } 1.64 + } 1.65 + } 1.66 +}; 1.67 + 1.68 +function tearDown() 1.69 +{ 1.70 + while (gBrowser.tabs.length > 1) 1.71 + gBrowser.removeCurrentTab(); 1.72 +} 1.73 + 1.74 +function test() 1.75 +{ 1.76 + // Don't cache removed tabs, so "clear console cache on tab close" triggers. 1.77 + Services.prefs.setIntPref("browser.tabs.max_tabs_undo", 0); 1.78 + registerCleanupFunction(function() { 1.79 + Services.prefs.clearUserPref("browser.tabs.max_tabs_undo"); 1.80 + }); 1.81 + 1.82 + registerCleanupFunction(tearDown); 1.83 + 1.84 + ConsoleObserver.init(); 1.85 + 1.86 + waitForExplicitFinish(); 1.87 + 1.88 + var tab = gBrowser.addTab(TEST_URI); 1.89 + gBrowser.selectedTab = tab; 1.90 + var browser = gBrowser.selectedBrowser; 1.91 + browser.addEventListener("DOMContentLoaded", function onLoad(event) { 1.92 + browser.removeEventListener("DOMContentLoaded", onLoad, false); 1.93 + executeSoon(function test_executeSoon() { 1.94 + let win = browser.contentWindow; 1.95 + win.console.log("this", "is", "a", "log message"); 1.96 + win.console.info("this", "is", "a", "info message"); 1.97 + win.console.warn("this", "is", "a", "warn message"); 1.98 + win.console.error("this", "is", "a", "error message"); 1.99 + }); 1.100 + }, false); 1.101 +} 1.102 + 1.103 +function getWindowId(aWindow) 1.104 +{ 1.105 + return aWindow.QueryInterface(Ci.nsIInterfaceRequestor) 1.106 + .getInterface(Ci.nsIDOMWindowUtils) 1.107 + .currentInnerWindowID; 1.108 +}