tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_webconsole_clear_cache.js (3906B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // Check that clearing the output also clears the console cache.
      6 
      7 "use strict";
      8 
      9 const TEST_URI =
     10  "data:text/html;charset=utf8,<!DOCTYPE html>Test clear cache<script>abcdef</script>";
     11 const EXPECTED_REPORT = "ReferenceError: abcdef is not defined";
     12 
     13 add_task(async function () {
     14  const tab = await addTab(TEST_URI);
     15  let hud = await openConsole(tab);
     16 
     17  const CACHED_MESSAGE = "CACHED_MESSAGE";
     18  await logTextToConsole(hud, CACHED_MESSAGE);
     19 
     20  info("Close and re-open the console");
     21  await closeToolboxIfOpen();
     22  hud = await openConsole(tab);
     23 
     24  await waitFor(() => findErrorMessage(hud, EXPECTED_REPORT));
     25  await waitFor(() => findConsoleAPIMessage(hud, CACHED_MESSAGE));
     26 
     27  info(
     28    "Click the clear output button and wait until there's no messages in the output"
     29  );
     30  let onMessagesCacheCleared = hud.ui.once("messages-cache-cleared");
     31  hud.ui.window.document.querySelector(".devtools-clear-icon").click();
     32  await onMessagesCacheCleared;
     33 
     34  info("Close and re-open the console");
     35  await closeToolboxIfOpen();
     36  hud = await openConsole(tab);
     37 
     38  info("Log a smoke message in order to know that the console is ready");
     39  await logTextToConsole(hud, "Smoke message");
     40  is(
     41    findConsoleAPIMessage(hud, CACHED_MESSAGE),
     42    undefined,
     43    "The cached message is not visible anymore"
     44  );
     45  is(
     46    findErrorMessage(hud, EXPECTED_REPORT),
     47    undefined,
     48    "The cached error message is not visible anymore as well"
     49  );
     50 
     51  // Test that we also clear the cache when calling console.clear().
     52  const NEW_CACHED_MESSAGE = "NEW_CACHED_MESSAGE";
     53  await logTextToConsole(hud, NEW_CACHED_MESSAGE);
     54 
     55  info("Send a console.clear() from the content page");
     56  onMessagesCacheCleared = hud.ui.once("messages-cache-cleared");
     57  const onConsoleCleared = waitForMessageByType(
     58    hud,
     59    "Console was cleared",
     60    ".console-api"
     61  );
     62  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     63    content.wrappedJSObject.console.clear();
     64  });
     65  await Promise.all([onConsoleCleared, onMessagesCacheCleared]);
     66 
     67  info("Close and re-open the console");
     68  await closeToolboxIfOpen();
     69  hud = await openConsole(tab);
     70 
     71  info("Log a smoke message in order to know that the console is ready");
     72  await logTextToConsole(hud, "Second smoke message");
     73  is(
     74    findConsoleAPIMessage(hud, NEW_CACHED_MESSAGE),
     75    undefined,
     76    "The new cached message is not visible anymore"
     77  );
     78 });
     79 
     80 add_task(async function consoleClearPersist() {
     81  // persist logs
     82  await pushPref("devtools.webconsole.persistlog", true);
     83  const tab = await addTab(TEST_URI);
     84  let hud = await openConsole(tab);
     85 
     86  // Test that we also clear the cache when calling console.clear().
     87  const CACHED_MESSAGE = "CACHED_MESSAGE_PERSIST";
     88  await logTextToConsole(hud, CACHED_MESSAGE);
     89 
     90  info("Send a console.clear() from the content page");
     91 
     92  const onConsoleClearPrevented = waitForMessageByType(
     93    hud,
     94    "console.clear() was prevented",
     95    ".console-api"
     96  );
     97  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     98    content.wrappedJSObject.console.clear();
     99  });
    100 
    101  await onConsoleClearPrevented;
    102 
    103  info("Close and re-open the console");
    104  await closeToolboxIfOpen();
    105  hud = await openConsole(tab);
    106 
    107  info("Log a smoke message in order to know that the console is ready");
    108  await logTextToConsole(hud, "smoke message for persist");
    109  is(
    110    findConsoleAPIMessage(hud, CACHED_MESSAGE),
    111    undefined,
    112    "The cached message is not visible anymore"
    113  );
    114 });
    115 
    116 function logTextToConsole(hud, text) {
    117  const onMessage = waitForMessageByType(hud, text, ".console-api");
    118  SpecialPowers.spawn(gBrowser.selectedBrowser, [text], function (str) {
    119    content.wrappedJSObject.console.log(str);
    120  });
    121  return onMessage;
    122 }