browser/devtools/webconsole/test/browser_console_private_browsing.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.

     1 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 // Bug 874061: test for how the browser and web consoles display messages coming
     7 // from private windows. See bug for description of expected behavior.
     9 function test()
    10 {
    11   const TEST_URI = "data:text/html;charset=utf8,<p>hello world! bug 874061" +
    12                    "<button onclick='console.log(\"foobar bug 874061\");" +
    13                    "fooBazBaz.yummy()'>click</button>";
    14   let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
    15                             .getService(Ci.nsIConsoleAPIStorage);
    16   let privateWindow, privateBrowser, privateTab, privateContent;
    17   let hud, expectedMessages, nonPrivateMessage;
    19   // This test is slightly more involved: it opens the web console twice,
    20   // a new private window once, and the browser console twice. We can get
    21   // a timeout with debug builds on slower machines.
    22   requestLongerTimeout(2);
    23   start();
    25   function start()
    26   {
    27     gBrowser.selectedTab = gBrowser.addTab("data:text/html;charset=utf8," +
    28                                            "<p>hello world! I am not private!");
    29     gBrowser.selectedBrowser.addEventListener("load", onLoadTab, true);
    30   }
    32   function onLoadTab()
    33   {
    34     gBrowser.selectedBrowser.removeEventListener("load", onLoadTab, true);
    35     info("onLoadTab()");
    37     // Make sure we have a clean state to start with.
    38     Services.console.reset();
    39     ConsoleAPIStorage.clearEvents();
    41     // Add a non-private message to the browser console.
    42     content.console.log("bug874061-not-private");
    44     nonPrivateMessage = {
    45       name: "console message from a non-private window",
    46       text: "bug874061-not-private",
    47       category: CATEGORY_WEBDEV,
    48       severity: SEVERITY_LOG,
    49     };
    51     privateWindow = OpenBrowserWindow({ private: true });
    52     ok(privateWindow, "new private window");
    53     ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private");
    55     whenDelayedStartupFinished(privateWindow, onPrivateWindowReady);
    56   }
    58   function onPrivateWindowReady()
    59   {
    60     info("private browser window opened");
    61     privateBrowser = privateWindow.gBrowser;
    63     privateTab = privateBrowser.selectedTab = privateBrowser.addTab(TEST_URI);
    64     privateBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    65       info("private tab opened");
    66       privateBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    67       privateContent = privateBrowser.selectedBrowser.contentWindow;
    68       ok(PrivateBrowsingUtils.isWindowPrivate(privateContent), "tab window is private");
    69       openConsole(privateTab, consoleOpened);
    70     }, true);
    71   }
    73   function addMessages()
    74   {
    75     let button = privateContent.document.querySelector("button");
    76     ok(button, "button in page");
    77     EventUtils.synthesizeMouse(button, 2, 2, {}, privateContent);
    78   }
    80   function consoleOpened(aHud)
    81   {
    82     hud = aHud;
    83     ok(hud, "web console opened");
    85     addMessages();
    86     expectedMessages = [
    87       {
    88         name: "script error",
    89         text: "fooBazBaz is not defined",
    90         category: CATEGORY_JS,
    91         severity: SEVERITY_ERROR,
    92       },
    93       {
    94         name: "console message",
    95         text: "foobar bug 874061",
    96         category: CATEGORY_WEBDEV,
    97         severity: SEVERITY_LOG,
    98       },
    99     ];
   101     // Make sure messages are displayed in the web console as they happen, even
   102     // if this is a private tab.
   103     waitForMessages({
   104       webconsole: hud,
   105       messages: expectedMessages,
   106     }).then(testCachedMessages);
   107   }
   109   function testCachedMessages()
   110   {
   111     info("testCachedMessages()");
   112     closeConsole(privateTab, () => {
   113       info("web console closed");
   114       openConsole(privateTab, consoleReopened);
   115     });
   116   }
   118   function consoleReopened(aHud)
   119   {
   120     hud = aHud;
   121     ok(hud, "web console reopened");
   123     // Make sure that cached messages are displayed in the web console, even
   124     // if this is a private tab.
   125     waitForMessages({
   126       webconsole: hud,
   127       messages: expectedMessages,
   128     }).then(testBrowserConsole);
   129   }
   131   function testBrowserConsole()
   132   {
   133     info("testBrowserConsole()");
   134     closeConsole(privateTab, () => {
   135       info("web console closed");
   136       privateWindow.HUDService.toggleBrowserConsole().then(onBrowserConsoleOpen);
   137     });
   138   }
   140   // Make sure that the cached messages from private tabs are not displayed in
   141   // the browser console.
   142   function checkNoPrivateMessages()
   143   {
   144     let text = hud.outputNode.textContent;
   145     is(text.indexOf("fooBazBaz"), -1, "no exception displayed");
   146     is(text.indexOf("bug 874061"), -1, "no console message displayed");
   147   }
   149   function onBrowserConsoleOpen(aHud)
   150   {
   151     hud = aHud;
   152     ok(hud, "browser console opened");
   154     checkNoPrivateMessages();
   155     addMessages();
   156     expectedMessages.push(nonPrivateMessage);
   158     // Make sure that live messages are displayed in the browser console, even
   159     // from private tabs.
   160     waitForMessages({
   161       webconsole: hud,
   162       messages: expectedMessages,
   163     }).then(testPrivateWindowClose);
   164   }
   166   function testPrivateWindowClose()
   167   {
   168     info("close the private window and check if the private messages are removed");
   169     hud.jsterm.once("private-messages-cleared", () => {
   170       isnot(hud.outputNode.textContent.indexOf("bug874061-not-private"), -1,
   171             "non-private messages are still shown after private window closed");
   172       checkNoPrivateMessages();
   174       info("close the browser console");
   175       privateWindow.HUDService.toggleBrowserConsole().then(() => {
   176         info("reopen the browser console");
   177         executeSoon(() =>
   178           HUDService.toggleBrowserConsole().then(onBrowserConsoleReopen));
   179       });
   180     });
   181     privateWindow.BrowserTryToCloseWindow();
   182   }
   184   function onBrowserConsoleReopen(aHud)
   185   {
   186     hud = aHud;
   187     ok(hud, "browser console reopened");
   189     // Make sure that the non-private message is still shown after reopen.
   190     waitForMessages({
   191       webconsole: hud,
   192       messages: [nonPrivateMessage],
   193     }).then(() => {
   194       // Make sure that no private message is displayed after closing the private
   195       // window and reopening the Browser Console.
   196       checkNoPrivateMessages();
   197       executeSoon(finishTest);
   198     });
   199   }
   200 }

mercurial