Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | function test() { |
michael@0 | 5 | // initialization |
michael@0 | 6 | waitForExplicitFinish(); |
michael@0 | 7 | let windowsToClose = []; |
michael@0 | 8 | let innerID; |
michael@0 | 9 | let beforeEvents; |
michael@0 | 10 | let afterEvents; |
michael@0 | 11 | let storageShouldOccur; |
michael@0 | 12 | let consoleObserver; |
michael@0 | 13 | let testURI = |
michael@0 | 14 | "http://example.com/browser/dom/tests/browser/test-console-api.html"; |
michael@0 | 15 | let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] |
michael@0 | 16 | .getService(Ci.nsIConsoleAPIStorage); |
michael@0 | 17 | |
michael@0 | 18 | function getInnerWindowId(aWindow) { |
michael@0 | 19 | return aWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 20 | .getInterface(Ci.nsIDOMWindowUtils) |
michael@0 | 21 | .currentInnerWindowID; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function whenNewWindowLoaded(aOptions, aCallback) { |
michael@0 | 25 | let win = OpenBrowserWindow(aOptions); |
michael@0 | 26 | win.addEventListener("load", function onLoad() { |
michael@0 | 27 | win.removeEventListener("load", onLoad, false); |
michael@0 | 28 | aCallback(win); |
michael@0 | 29 | }, false); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function doTest(aIsPrivateMode, aWindow, aCallback) { |
michael@0 | 33 | aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
michael@0 | 34 | aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
michael@0 | 35 | |
michael@0 | 36 | consoleObserver = { |
michael@0 | 37 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 38 | if (aTopic == "console-api-log-event") { |
michael@0 | 39 | afterEvents = ConsoleAPIStorage.getEvents(innerID); |
michael@0 | 40 | is(beforeEvents.length == afterEvents.length - 1, storageShouldOccur, |
michael@0 | 41 | "storage should" + (storageShouldOccur ? "" : " not") + " occur"); |
michael@0 | 42 | |
michael@0 | 43 | executeSoon(function() { |
michael@0 | 44 | Services.obs.removeObserver(consoleObserver, "console-api-log-event"); |
michael@0 | 45 | aCallback(); |
michael@0 | 46 | }); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | aWindow.Services.obs.addObserver( |
michael@0 | 52 | consoleObserver, "console-api-log-event", false); |
michael@0 | 53 | aWindow.console.log("foo bar baz (private: " + aIsPrivateMode + ")"); |
michael@0 | 54 | }, true); |
michael@0 | 55 | |
michael@0 | 56 | // We expect that console API messages are always stored. |
michael@0 | 57 | storageShouldOccur = true; |
michael@0 | 58 | innerID = getInnerWindowId(aWindow); |
michael@0 | 59 | beforeEvents = ConsoleAPIStorage.getEvents(innerID); |
michael@0 | 60 | aWindow.gBrowser.selectedBrowser.loadURI(testURI); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function testOnWindow(aOptions, aCallback) { |
michael@0 | 64 | whenNewWindowLoaded(aOptions, function(aWin) { |
michael@0 | 65 | windowsToClose.push(aWin); |
michael@0 | 66 | // execute should only be called when need, like when you are opening |
michael@0 | 67 | // web pages on the test. If calling executeSoon() is not necesary, then |
michael@0 | 68 | // call whenNewWindowLoaded() instead of testOnWindow() on your test. |
michael@0 | 69 | executeSoon(function() aCallback(aWin)); |
michael@0 | 70 | }); |
michael@0 | 71 | }; |
michael@0 | 72 | |
michael@0 | 73 | // this function is called after calling finish() on the test. |
michael@0 | 74 | registerCleanupFunction(function() { |
michael@0 | 75 | windowsToClose.forEach(function(aWin) { |
michael@0 | 76 | aWin.close(); |
michael@0 | 77 | }); |
michael@0 | 78 | }); |
michael@0 | 79 | |
michael@0 | 80 | // test first when not on private mode |
michael@0 | 81 | testOnWindow({}, function(aWin) { |
michael@0 | 82 | doTest(false, aWin, function() { |
michael@0 | 83 | // then test when on private mode |
michael@0 | 84 | testOnWindow({private: true}, function(aWin) { |
michael@0 | 85 | doTest(true, aWin, finish); |
michael@0 | 86 | }); |
michael@0 | 87 | }); |
michael@0 | 88 | }); |
michael@0 | 89 | } |