browser_console_private_browsing.js 6.3 KB

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