summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_clear_cache.js
blob: 5b0ccda4d61463ac57b31c99b402339f5edd13bb (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Check that clearing the output also clears the console cache.

"use strict";

const TEST_URI =
  "data:text/html;charset=utf8,<!DOCTYPE html>Test clear cache<script>abcdef</script>";
const EXPECTED_REPORT = "ReferenceError: abcdef is not defined";

add_task(async function () {
  const tab = await addTab(TEST_URI);
  let hud = await openConsole(tab);

  const CACHED_MESSAGE = "CACHED_MESSAGE";
  await logTextToConsole(hud, CACHED_MESSAGE);

  info("Close and re-open the console");
  await closeToolbox();
  hud = await openConsole(tab);

  await waitFor(() => findErrorMessage(hud, EXPECTED_REPORT));
  await waitFor(() => findConsoleAPIMessage(hud, CACHED_MESSAGE));

  info(
    "Click the clear output button and wait until there's no messages in the output"
  );
  let onMessagesCacheCleared = hud.ui.once("messages-cache-cleared");
  hud.ui.window.document.querySelector(".devtools-clear-icon").click();
  await onMessagesCacheCleared;

  info("Close and re-open the console");
  await closeToolbox();
  hud = await openConsole(tab);

  info("Log a smoke message in order to know that the console is ready");
  await logTextToConsole(hud, "Smoke message");
  is(
    findConsoleAPIMessage(hud, CACHED_MESSAGE),
    undefined,
    "The cached message is not visible anymore"
  );
  is(
    findErrorMessage(hud, EXPECTED_REPORT),
    undefined,
    "The cached error message is not visible anymore as well"
  );

  // Test that we also clear the cache when calling console.clear().
  const NEW_CACHED_MESSAGE = "NEW_CACHED_MESSAGE";
  await logTextToConsole(hud, NEW_CACHED_MESSAGE);

  info("Send a console.clear() from the content page");
  onMessagesCacheCleared = hud.ui.once("messages-cache-cleared");
  const onConsoleCleared = waitForMessageByType(
    hud,
    "Console was cleared",
    ".console-api"
  );
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.wrappedJSObject.console.clear();
  });
  await Promise.all([onConsoleCleared, onMessagesCacheCleared]);

  info("Close and re-open the console");
  await closeToolbox();
  hud = await openConsole(tab);

  info("Log a smoke message in order to know that the console is ready");
  await logTextToConsole(hud, "Second smoke message");
  is(
    findConsoleAPIMessage(hud, NEW_CACHED_MESSAGE),
    undefined,
    "The new cached message is not visible anymore"
  );
});

add_task(async function consoleClearPersist() {
  // persist logs
  await pushPref("devtools.webconsole.persistlog", true);
  const tab = await addTab(TEST_URI);
  let hud = await openConsole(tab);

  // Test that we also clear the cache when calling console.clear().
  const CACHED_MESSAGE = "CACHED_MESSAGE_PERSIST";
  await logTextToConsole(hud, CACHED_MESSAGE);

  info("Send a console.clear() from the content page");

  const onConsoleClearPrevented = waitForMessageByType(
    hud,
    "console.clear() was prevented",
    ".console-api"
  );
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.wrappedJSObject.console.clear();
  });

  await onConsoleClearPrevented;

  info("Close and re-open the console");
  await closeToolbox();
  hud = await openConsole(tab);

  info("Log a smoke message in order to know that the console is ready");
  await logTextToConsole(hud, "smoke message for persist");
  is(
    findConsoleAPIMessage(hud, CACHED_MESSAGE),
    undefined,
    "The cached message is not visible anymore"
  );
});

function logTextToConsole(hud, text) {
  const onMessage = waitForMessageByType(hud, text, ".console-api");
  SpecialPowers.spawn(gBrowser.selectedBrowser, [text], function (str) {
    content.wrappedJSObject.console.log(str);
  });
  return onMessage;
}