Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / tests / browser / browser_ConsoleStorageAPITests.js
blobee3e9e3f52bdddaf6deff2404e55b0a6a1a68dcc
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 const TEST_URI =
5 "http://example.com/browser/dom/tests/browser/test-console-api.html";
7 function tearDown() {
8 while (gBrowser.tabs.length > 1) {
9 gBrowser.removeCurrentTab();
13 add_task(async function () {
14 // Don't cache removed tabs, so "clear console cache on tab close" triggers.
15 await SpecialPowers.pushPrefEnv({ set: [["browser.tabs.max_tabs_undo", 0]] });
17 registerCleanupFunction(tearDown);
19 info(
20 "Open a keepalive tab in the background to make sure we don't accidentally kill the content process"
22 var keepaliveTab = await BrowserTestUtils.addTab(
23 gBrowser,
24 "data:text/html,<meta charset=utf8>Keep Alive Tab"
27 info("Open the main tab to run the test in");
28 var tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URI);
29 var browser = gBrowser.selectedBrowser;
31 const windowId = await ContentTask.spawn(browser, null, async function () {
32 let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(
33 Ci.nsIConsoleAPIStorage
36 let observerPromise = new Promise(resolve => {
37 let apiCallCount = 0;
38 function observe() {
39 apiCallCount++;
40 info(`Received ${apiCallCount} console log events`);
41 if (apiCallCount == 4) {
42 ConsoleAPIStorage.removeLogEventListener(observe);
43 resolve();
47 info("Setting up observer");
48 ConsoleAPIStorage.addLogEventListener(
49 observe,
50 Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal)
52 });
54 info("Emit a few console API logs");
55 content.console.log("this", "is", "a", "log", "message");
56 content.console.info("this", "is", "a", "info", "message");
57 content.console.warn("this", "is", "a", "warn", "message");
58 content.console.error("this", "is", "a", "error", "message");
60 info("Wait for the corresponding log event");
61 await observerPromise;
63 const innerWindowId = content.windowGlobalChild.innerWindowId;
64 const events = ConsoleAPIStorage.getEvents(innerWindowId).filter(
65 message =>
66 message.arguments[0] === "this" &&
67 message.arguments[1] === "is" &&
68 message.arguments[2] === "a" &&
69 message.arguments[4] === "message"
71 is(events.length, 4, "The storage service got the messages we emitted");
73 info("Ensure clearEvents does remove the events from storage");
74 ConsoleAPIStorage.clearEvents();
75 is(ConsoleAPIStorage.getEvents(innerWindowId).length, 0, "Cleared Storage");
77 return content.windowGlobalChild.innerWindowId;
78 });
80 await SpecialPowers.spawn(browser, [], function () {
81 // make sure a closed window's events are in fact removed from
82 // the storage cache
83 content.console.log("adding a new event");
84 });
86 info("Close the window");
87 gBrowser.removeTab(tab, { animate: false });
88 // Ensure actual window destruction is not delayed (too long).
89 SpecialPowers.DOMWindowUtils.garbageCollect();
91 // Spawn the check in the keepaliveTab, so that we can read the ConsoleAPIStorage correctly
92 gBrowser.selectedTab = keepaliveTab;
93 browser = gBrowser.selectedBrowser;
95 // Spin the event loop to make sure everything is cleared.
96 await SpecialPowers.spawn(browser, [], () => {});
98 await SpecialPowers.spawn(browser, [windowId], function (windowId) {
99 var ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(
100 Ci.nsIConsoleAPIStorage
103 ConsoleAPIStorage.getEvents(windowId).length,
105 "tab close is clearing the cache"