1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/browser/browser_ConsoleStoragePBTest_perwindowpb.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +function test() { 1.8 + // initialization 1.9 + waitForExplicitFinish(); 1.10 + let windowsToClose = []; 1.11 + let innerID; 1.12 + let beforeEvents; 1.13 + let afterEvents; 1.14 + let storageShouldOccur; 1.15 + let consoleObserver; 1.16 + let testURI = 1.17 + "http://example.com/browser/dom/tests/browser/test-console-api.html"; 1.18 + let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] 1.19 + .getService(Ci.nsIConsoleAPIStorage); 1.20 + 1.21 + function getInnerWindowId(aWindow) { 1.22 + return aWindow.QueryInterface(Ci.nsIInterfaceRequestor) 1.23 + .getInterface(Ci.nsIDOMWindowUtils) 1.24 + .currentInnerWindowID; 1.25 + } 1.26 + 1.27 + function whenNewWindowLoaded(aOptions, aCallback) { 1.28 + let win = OpenBrowserWindow(aOptions); 1.29 + win.addEventListener("load", function onLoad() { 1.30 + win.removeEventListener("load", onLoad, false); 1.31 + aCallback(win); 1.32 + }, false); 1.33 + } 1.34 + 1.35 + function doTest(aIsPrivateMode, aWindow, aCallback) { 1.36 + aWindow.gBrowser.selectedBrowser.addEventListener("load", function onLoad() { 1.37 + aWindow.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); 1.38 + 1.39 + consoleObserver = { 1.40 + observe: function(aSubject, aTopic, aData) { 1.41 + if (aTopic == "console-api-log-event") { 1.42 + afterEvents = ConsoleAPIStorage.getEvents(innerID); 1.43 + is(beforeEvents.length == afterEvents.length - 1, storageShouldOccur, 1.44 + "storage should" + (storageShouldOccur ? "" : " not") + " occur"); 1.45 + 1.46 + executeSoon(function() { 1.47 + Services.obs.removeObserver(consoleObserver, "console-api-log-event"); 1.48 + aCallback(); 1.49 + }); 1.50 + } 1.51 + } 1.52 + }; 1.53 + 1.54 + aWindow.Services.obs.addObserver( 1.55 + consoleObserver, "console-api-log-event", false); 1.56 + aWindow.console.log("foo bar baz (private: " + aIsPrivateMode + ")"); 1.57 + }, true); 1.58 + 1.59 + // We expect that console API messages are always stored. 1.60 + storageShouldOccur = true; 1.61 + innerID = getInnerWindowId(aWindow); 1.62 + beforeEvents = ConsoleAPIStorage.getEvents(innerID); 1.63 + aWindow.gBrowser.selectedBrowser.loadURI(testURI); 1.64 + } 1.65 + 1.66 + function testOnWindow(aOptions, aCallback) { 1.67 + whenNewWindowLoaded(aOptions, function(aWin) { 1.68 + windowsToClose.push(aWin); 1.69 + // execute should only be called when need, like when you are opening 1.70 + // web pages on the test. If calling executeSoon() is not necesary, then 1.71 + // call whenNewWindowLoaded() instead of testOnWindow() on your test. 1.72 + executeSoon(function() aCallback(aWin)); 1.73 + }); 1.74 + }; 1.75 + 1.76 + // this function is called after calling finish() on the test. 1.77 + registerCleanupFunction(function() { 1.78 + windowsToClose.forEach(function(aWin) { 1.79 + aWin.close(); 1.80 + }); 1.81 + }); 1.82 + 1.83 + // test first when not on private mode 1.84 + testOnWindow({}, function(aWin) { 1.85 + doTest(false, aWin, function() { 1.86 + // then test when on private mode 1.87 + testOnWindow({private: true}, function(aWin) { 1.88 + doTest(true, aWin, finish); 1.89 + }); 1.90 + }); 1.91 + }); 1.92 +}