summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_console_webconsole_private_browsing.js
blob: 0fed8c03c58385ecbb754d1f5f7880e09e9b1a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Bug 874061: test for how the browser and web consoles display messages coming
// from private windows. See bug for description of expected behavior.

"use strict";

const NON_PRIVATE_MESSAGE = "This is not a private message";
const PRIVATE_MESSAGE = "This is a private message";
const PRIVATE_UNDEFINED_FN = "privateException";
const PRIVATE_EXCEPTION = `${PRIVATE_UNDEFINED_FN} is not defined`;

const NON_PRIVATE_TEST_URI =
  "data:text/html;charset=utf8,<!DOCTYPE html>Not private";
const PRIVATE_TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html>Test console in private windows
  <script>
    function logMessages() {
      /* Wrap the exception so we don't throw in ContentTask. */
      setTimeout(() => {
        console.log("${PRIVATE_MESSAGE}");
        ${PRIVATE_UNDEFINED_FN}();
      }, 10);
    }
  </script>`;

add_task(async function () {
  await pushPref("devtools.browsertoolbox.scope", "everything");

  const publicTab = await addTab(NON_PRIVATE_TEST_URI);

  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private");
  const privateBrowser = privateWindow.gBrowser;
  privateBrowser.selectedTab = BrowserTestUtils.addTab(
    privateBrowser,
    PRIVATE_TEST_URI
  );
  const privateTab = privateBrowser.selectedTab;

  info("private tab opened");
  ok(
    PrivateBrowsingUtils.isBrowserPrivate(privateBrowser.selectedBrowser),
    "tab window is private"
  );

  let hud = await openConsole(privateTab);
  ok(hud, "web console opened");

  const onLogMessage = waitForMessageByType(
    hud,
    PRIVATE_MESSAGE,
    ".console-api"
  );
  const onErrorMessage = waitForMessageByType(hud, PRIVATE_EXCEPTION, ".error");
  logPrivateMessages(privateBrowser.selectedBrowser);

  await onLogMessage;
  await onErrorMessage;
  ok(true, "Messages are displayed as expected");

  info("Check that commands executed in private windows aren't put in history");
  const privateCommand = `"command in private window"`;
  await executeAndWaitForResultMessage(hud, privateCommand, "");

  const publicHud = await openConsole(publicTab);
  const historyMessage = await executeAndWaitForMessageByType(
    publicHud,
    ":history",
    "",
    ".simpleTable"
  );

  ok(
    Array.from(
      historyMessage.node.querySelectorAll("tr td:last-of-type")
    ).every(td => td.textContent !== privateCommand),
    "command from private window wasn't added to the history"
  );
  await closeConsole(publicTab);

  info("test cached messages");
  await closeConsole(privateTab);
  info("web console closed");
  hud = await openConsole(privateTab);
  ok(hud, "web console reopened");

  await waitFor(() => findConsoleAPIMessage(hud, PRIVATE_MESSAGE));
  await waitFor(() => findErrorMessage(hud, PRIVATE_EXCEPTION));
  ok(
    true,
    "Messages are still displayed after closing and reopening the console"
  );

  info("Test Browser Console");
  await closeConsole(privateTab);
  info("web console closed");
  hud = await BrowserConsoleManager.toggleBrowserConsole();

  // Add a non-private message to the console.
  const onBrowserConsoleNonPrivateMessage = waitForMessageByType(
    hud,
    NON_PRIVATE_MESSAGE,
    ".console-api"
  );
  SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [NON_PRIVATE_MESSAGE],
    function (msg) {
      content.console.log(msg);
    }
  );
  await onBrowserConsoleNonPrivateMessage;

  info(
    "Check that cached messages from private tabs are not displayed in the browser console"
  );
  // We do the check at this moment, after we received the "live" message, so the browser
  // console would have displayed any cached messages by now.
  assertNoPrivateMessages(hud);

  const onBrowserConsolePrivateLogMessage = waitForMessageByType(
    hud,
    PRIVATE_MESSAGE,
    ".console-api"
  );
  const onBrowserConsolePrivateErrorMessage = waitForMessageByType(
    hud,
    PRIVATE_EXCEPTION,
    ".error"
  );
  logPrivateMessages(privateBrowser.selectedBrowser);

  info("Wait for private log message");
  await onBrowserConsolePrivateLogMessage;
  info("Wait for private error message");
  await onBrowserConsolePrivateErrorMessage;
  ok(true, "Messages are displayed as expected");

  info("close the private window and check if private messages are removed");
  const onPrivateMessagesCleared = hud.ui.once("private-messages-cleared");
  privateWindow.BrowserTryToCloseWindow();
  await onPrivateMessagesCleared;

  ok(
    findConsoleAPIMessage(hud, NON_PRIVATE_MESSAGE),
    "non-private messages are still shown after private window closed"
  );
  assertNoPrivateMessages(hud);

  info("close the browser console");
  await safeCloseBrowserConsole();

  info("reopen the browser console");
  hud = await BrowserConsoleManager.toggleBrowserConsole();
  ok(hud, "browser console reopened");

  assertNoPrivateMessages(hud);

  info("close the browser console again");
  await safeCloseBrowserConsole();
});

function logPrivateMessages(browser) {
  SpecialPowers.spawn(browser, [], () => content.wrappedJSObject.logMessages());
}

function assertNoPrivateMessages(hud) {
  is(
    findConsoleAPIMessage(hud, PRIVATE_MESSAGE, ":not(.error)")?.textContent,
    undefined,
    "no console message displayed"
  );
  is(
    findErrorMessage(hud, PRIVATE_EXCEPTION)?.textContent,
    undefined,
    "no exception displayed"
  );
}