dom/tests/browser/browser_ConsoleStorageAPITests.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 const TEST_URI = "http://example.com/browser/dom/tests/browser/test-console-api.html";
michael@0 5 const TEST_URI_NAV = "http://example.com/browser/dom/tests/browser/";
michael@0 6
michael@0 7 let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
michael@0 8 .getService(Ci.nsIConsoleAPIStorage);
michael@0 9
michael@0 10 var apiCallCount;
michael@0 11
michael@0 12 var ConsoleObserver = {
michael@0 13 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
michael@0 14
michael@0 15 init: function CO_init()
michael@0 16 {
michael@0 17 Services.obs.addObserver(this, "console-storage-cache-event", false);
michael@0 18 apiCallCount = 0;
michael@0 19 },
michael@0 20
michael@0 21 observe: function CO_observe(aSubject, aTopic, aData)
michael@0 22 {
michael@0 23 if (aTopic == "console-storage-cache-event") {
michael@0 24 apiCallCount ++;
michael@0 25 if (apiCallCount == 4) {
michael@0 26 Services.obs.removeObserver(this, "console-storage-cache-event");
michael@0 27
michael@0 28 try {
michael@0 29 let tab = gBrowser.selectedTab;
michael@0 30 let browser = gBrowser.selectedBrowser;
michael@0 31 let win = browser.contentWindow;
michael@0 32 let windowID = getWindowId(win);
michael@0 33 let messages = ConsoleAPIStorage.getEvents(windowID);
michael@0 34 ok(messages.length >= 4, "Some messages found in the storage service");
michael@0 35
michael@0 36 ConsoleAPIStorage.clearEvents();
michael@0 37 messages = ConsoleAPIStorage.getEvents(windowID);
michael@0 38 is(messages.length, 0, "Cleared Storage");
michael@0 39
michael@0 40 // make sure a closed window's events are in fact removed from the
michael@0 41 // storage cache
michael@0 42 win.console.log("adding a new event");
michael@0 43 // Close the window.
michael@0 44 gBrowser.removeTab(tab, {animate: false});
michael@0 45 // Ensure actual window destruction is not delayed (too long).
michael@0 46 SpecialPowers.DOMWindowUtils.garbageCollect();
michael@0 47 // Ensure "inner-window-destroyed" event is processed,
michael@0 48 // so the storage cache is cleared.
michael@0 49 executeSoon(function () {
michael@0 50 // use the old windowID again to see if we have any stray cached messages
michael@0 51 messages = ConsoleAPIStorage.getEvents(windowID);
michael@0 52 is(messages.length, 0, "tab close is clearing the cache");
michael@0 53 finish();
michael@0 54 });
michael@0 55 } catch (ex) {
michael@0 56 dump(ex + "\n\n\n");
michael@0 57 dump(ex.stack + "\n\n\n");
michael@0 58 ok(false, "We got an unexpected exception");
michael@0 59 }
michael@0 60 }
michael@0 61 }
michael@0 62 }
michael@0 63 };
michael@0 64
michael@0 65 function tearDown()
michael@0 66 {
michael@0 67 while (gBrowser.tabs.length > 1)
michael@0 68 gBrowser.removeCurrentTab();
michael@0 69 }
michael@0 70
michael@0 71 function test()
michael@0 72 {
michael@0 73 // Don't cache removed tabs, so "clear console cache on tab close" triggers.
michael@0 74 Services.prefs.setIntPref("browser.tabs.max_tabs_undo", 0);
michael@0 75 registerCleanupFunction(function() {
michael@0 76 Services.prefs.clearUserPref("browser.tabs.max_tabs_undo");
michael@0 77 });
michael@0 78
michael@0 79 registerCleanupFunction(tearDown);
michael@0 80
michael@0 81 ConsoleObserver.init();
michael@0 82
michael@0 83 waitForExplicitFinish();
michael@0 84
michael@0 85 var tab = gBrowser.addTab(TEST_URI);
michael@0 86 gBrowser.selectedTab = tab;
michael@0 87 var browser = gBrowser.selectedBrowser;
michael@0 88 browser.addEventListener("DOMContentLoaded", function onLoad(event) {
michael@0 89 browser.removeEventListener("DOMContentLoaded", onLoad, false);
michael@0 90 executeSoon(function test_executeSoon() {
michael@0 91 let win = browser.contentWindow;
michael@0 92 win.console.log("this", "is", "a", "log message");
michael@0 93 win.console.info("this", "is", "a", "info message");
michael@0 94 win.console.warn("this", "is", "a", "warn message");
michael@0 95 win.console.error("this", "is", "a", "error message");
michael@0 96 });
michael@0 97 }, false);
michael@0 98 }
michael@0 99
michael@0 100 function getWindowId(aWindow)
michael@0 101 {
michael@0 102 return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 103 .getInterface(Ci.nsIDOMWindowUtils)
michael@0 104 .currentInnerWindowID;
michael@0 105 }

mercurial