tor-browser

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

browser_webconsole_warning_groups_toggle.js (9158B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that filtering the console output when there are warning groups works as expected.
      5 
      6 "use strict";
      7 requestLongerTimeout(2);
      8 
      9 const { PrefObserver } = require("resource://devtools/client/shared/prefs.js");
     10 
     11 const TEST_FILE =
     12  "browser/devtools/client/webconsole/test/browser/test-warning-groups.html";
     13 const TEST_URI = "https://example.org/" + TEST_FILE;
     14 
     15 const TRACKER_URL = "https://tracking.example.com/";
     16 const IMG_FILE =
     17  "browser/devtools/client/webconsole/test/browser/test-image.png";
     18 const CONTENT_BLOCKED_BY_ETP_URL = TRACKER_URL + IMG_FILE;
     19 const WARNING_GROUP_PREF = "devtools.webconsole.groupSimilarMessages";
     20 
     21 const { UrlClassifierTestUtils } = ChromeUtils.importESModule(
     22  "resource://testing-common/UrlClassifierTestUtils.sys.mjs"
     23 );
     24 UrlClassifierTestUtils.addTestTrackers();
     25 registerCleanupFunction(function () {
     26  UrlClassifierTestUtils.cleanupTestTrackers();
     27 });
     28 
     29 pushPref("privacy.trackingprotection.enabled", true);
     30 
     31 const ENHANCED_TRACKING_PROTECTION_GROUP_LABEL =
     32  "The resource at “<URL>” was blocked because Enhanced Tracking Protection is enabled.";
     33 
     34 add_task(async function testContentBlockingMessage() {
     35  // Enable persist log
     36  await pushPref("devtools.webconsole.persistlog", true);
     37 
     38  // Start with the warningGroup pref set to false.
     39  await pushPref(WARNING_GROUP_PREF, false);
     40 
     41  const hud = await openNewTabAndConsole(TEST_URI);
     42 
     43  info("Log a few tracking protection messages and simple ones");
     44  let onContentBlockedByETPWarningMessage = waitForMessageByType(
     45    hud,
     46    `${CONTENT_BLOCKED_BY_ETP_URL}?1`,
     47    ".warn"
     48  );
     49  emitEnhancedTrackingProtectionMessage(hud);
     50  await onContentBlockedByETPWarningMessage;
     51  await logString(hud, "simple message 1");
     52 
     53  onContentBlockedByETPWarningMessage = waitForMessageByType(
     54    hud,
     55    `${CONTENT_BLOCKED_BY_ETP_URL}?2`,
     56    ".warn"
     57  );
     58  emitEnhancedTrackingProtectionMessage(hud);
     59  await onContentBlockedByETPWarningMessage;
     60 
     61  onContentBlockedByETPWarningMessage = waitForMessageByType(
     62    hud,
     63    `${CONTENT_BLOCKED_BY_ETP_URL}?3`,
     64    ".warn"
     65  );
     66  emitEnhancedTrackingProtectionMessage(hud);
     67  await onContentBlockedByETPWarningMessage;
     68 
     69  await checkConsoleOutputForWarningGroup(hud, [
     70    `${CONTENT_BLOCKED_BY_ETP_URL}?1`,
     71    `simple message 1`,
     72    `${CONTENT_BLOCKED_BY_ETP_URL}?2`,
     73    `${CONTENT_BLOCKED_BY_ETP_URL}?3`,
     74  ]);
     75 
     76  info("Enable the warningGroup feature pref and check warnings were grouped");
     77  await toggleWarningGroupPreference(hud);
     78  let warningGroupMessage1 = await waitFor(() =>
     79    findWarningMessage(hud, ENHANCED_TRACKING_PROTECTION_GROUP_LABEL)
     80  );
     81  is(
     82    warningGroupMessage1.querySelector(".warning-group-badge").textContent,
     83    "3",
     84    "The badge has the expected text"
     85  );
     86 
     87  await checkConsoleOutputForWarningGroup(hud, [
     88    `▶︎⚠ ${ENHANCED_TRACKING_PROTECTION_GROUP_LABEL}`,
     89    `simple message 1`,
     90  ]);
     91 
     92  info("Add a new warning message and check it's placed in the closed group");
     93  emitEnhancedTrackingProtectionMessage(hud);
     94  await waitForBadgeNumber(warningGroupMessage1, "4");
     95 
     96  info(
     97    "Re-enable the warningGroup feature pref and check warnings are displayed"
     98  );
     99  await toggleWarningGroupPreference(hud);
    100  await waitFor(() =>
    101    findWarningMessage(hud, `${CONTENT_BLOCKED_BY_ETP_URL}?4`)
    102  );
    103 
    104  // Warning messages are displayed at the expected positions.
    105  await checkConsoleOutputForWarningGroup(hud, [
    106    `${CONTENT_BLOCKED_BY_ETP_URL}?1`,
    107    `simple message 1`,
    108    `${CONTENT_BLOCKED_BY_ETP_URL}?2`,
    109    `${CONTENT_BLOCKED_BY_ETP_URL}?3`,
    110    `${CONTENT_BLOCKED_BY_ETP_URL}?4`,
    111  ]);
    112 
    113  info("Re-disable the warningGroup feature pref");
    114  await toggleWarningGroupPreference(hud);
    115  console.log("toggle successful");
    116  warningGroupMessage1 = await waitFor(() =>
    117    findWarningMessage(hud, ENHANCED_TRACKING_PROTECTION_GROUP_LABEL)
    118  );
    119 
    120  await checkConsoleOutputForWarningGroup(hud, [
    121    `▶︎⚠ ${ENHANCED_TRACKING_PROTECTION_GROUP_LABEL}`,
    122    `simple message 1`,
    123  ]);
    124 
    125  info("Expand the warning group");
    126  warningGroupMessage1.querySelector(".arrow").click();
    127  await waitFor(() => findWarningMessage(hud, CONTENT_BLOCKED_BY_ETP_URL));
    128 
    129  await checkConsoleOutputForWarningGroup(hud, [
    130    `▼︎⚠ ${ENHANCED_TRACKING_PROTECTION_GROUP_LABEL}`,
    131    `| ${CONTENT_BLOCKED_BY_ETP_URL}?1`,
    132    `| ${CONTENT_BLOCKED_BY_ETP_URL}?2`,
    133    `| ${CONTENT_BLOCKED_BY_ETP_URL}?3`,
    134    `| ${CONTENT_BLOCKED_BY_ETP_URL}?4`,
    135    `simple message 1`,
    136  ]);
    137 
    138  info("Reload the page and wait for it to be ready");
    139  await reloadPage();
    140 
    141  // Wait for the navigation message to be displayed.
    142  await waitFor(() =>
    143    findMessageByType(hud, "Navigated to", ".navigationMarker")
    144  );
    145 
    146  info("Disable the warningGroup feature pref again");
    147  await toggleWarningGroupPreference(hud);
    148 
    149  info("Add one warning message and one simple message");
    150  await waitFor(() =>
    151    findWarningMessage(hud, `${CONTENT_BLOCKED_BY_ETP_URL}?4`)
    152  );
    153  onContentBlockedByETPWarningMessage = waitForMessageByType(
    154    hud,
    155    CONTENT_BLOCKED_BY_ETP_URL,
    156    ".warn"
    157  );
    158  emitEnhancedTrackingProtectionMessage(hud);
    159  await onContentBlockedByETPWarningMessage;
    160  await logString(hud, "simple message 2");
    161 
    162  // nothing is grouped.
    163  await checkConsoleOutputForWarningGroup(hud, [
    164    `${CONTENT_BLOCKED_BY_ETP_URL}?1`,
    165    `simple message 1`,
    166    `${CONTENT_BLOCKED_BY_ETP_URL}?2`,
    167    `${CONTENT_BLOCKED_BY_ETP_URL}?3`,
    168    `${CONTENT_BLOCKED_BY_ETP_URL}?4`,
    169    `Navigated to`,
    170    `${CONTENT_BLOCKED_BY_ETP_URL}?5`,
    171    `simple message 2`,
    172  ]);
    173 
    174  info(
    175    "Enable the warningGroup feature pref to check that the group is still expanded"
    176  );
    177  await toggleWarningGroupPreference(hud);
    178  await waitFor(() =>
    179    findWarningMessage(hud, ENHANCED_TRACKING_PROTECTION_GROUP_LABEL)
    180  );
    181 
    182  await checkConsoleOutputForWarningGroup(hud, [
    183    `▼︎⚠ ${ENHANCED_TRACKING_PROTECTION_GROUP_LABEL}`,
    184    `| ${CONTENT_BLOCKED_BY_ETP_URL}?1`,
    185    `| ${CONTENT_BLOCKED_BY_ETP_URL}?2`,
    186    `| ${CONTENT_BLOCKED_BY_ETP_URL}?3`,
    187    `| ${CONTENT_BLOCKED_BY_ETP_URL}?4`,
    188    `simple message 1`,
    189    `Navigated to`,
    190    `| ${CONTENT_BLOCKED_BY_ETP_URL}?5`,
    191    `simple message 2`,
    192  ]);
    193 
    194  info(
    195    "Add a second warning and check it's placed in the second, closed, group"
    196  );
    197  const onContentBlockedByETPWarningGroupMessage = waitForMessageByType(
    198    hud,
    199    ENHANCED_TRACKING_PROTECTION_GROUP_LABEL,
    200    ".warn"
    201  );
    202  emitEnhancedTrackingProtectionMessage(hud);
    203  const warningGroupMessage2 = (await onContentBlockedByETPWarningGroupMessage)
    204    .node;
    205  await waitForBadgeNumber(warningGroupMessage2, "2");
    206 
    207  await checkConsoleOutputForWarningGroup(hud, [
    208    `▼︎⚠ ${ENHANCED_TRACKING_PROTECTION_GROUP_LABEL}`,
    209    `| ${CONTENT_BLOCKED_BY_ETP_URL}?1`,
    210    `| ${CONTENT_BLOCKED_BY_ETP_URL}?2`,
    211    `| ${CONTENT_BLOCKED_BY_ETP_URL}?3`,
    212    `| ${CONTENT_BLOCKED_BY_ETP_URL}?4`,
    213    `simple message 1`,
    214    `Navigated to`,
    215    `▶︎⚠ ${ENHANCED_TRACKING_PROTECTION_GROUP_LABEL}`,
    216    `simple message 2`,
    217  ]);
    218 
    219  info(
    220    "Disable the warningGroup pref and check all warning messages are visible"
    221  );
    222  await toggleWarningGroupPreference(hud);
    223  await waitFor(() =>
    224    findWarningMessage(hud, `${CONTENT_BLOCKED_BY_ETP_URL}?6`)
    225  );
    226 
    227  await checkConsoleOutputForWarningGroup(hud, [
    228    `${CONTENT_BLOCKED_BY_ETP_URL}?1`,
    229    `simple message 1`,
    230    `${CONTENT_BLOCKED_BY_ETP_URL}?2`,
    231    `${CONTENT_BLOCKED_BY_ETP_URL}?3`,
    232    `${CONTENT_BLOCKED_BY_ETP_URL}?4`,
    233    `Navigated to`,
    234    `${CONTENT_BLOCKED_BY_ETP_URL}?5`,
    235    `simple message 2`,
    236    `${CONTENT_BLOCKED_BY_ETP_URL}?6`,
    237  ]);
    238 
    239  // Clean the pref for the next tests.
    240  Services.prefs.clearUserPref(WARNING_GROUP_PREF);
    241 });
    242 
    243 let cpt = 0;
    244 /**
    245 * Emit an Enhanced Tracking Protection message. This is done by loading an image from an origin
    246 * tagged as tracker. The image is loaded with a incremented counter query parameter
    247 * each time so we can get the warning message.
    248 */
    249 function emitEnhancedTrackingProtectionMessage() {
    250  const url = `${CONTENT_BLOCKED_BY_ETP_URL}?${++cpt}`;
    251  SpecialPowers.spawn(gBrowser.selectedBrowser, [url], function (innerURL) {
    252    content.wrappedJSObject.loadImage(innerURL);
    253  });
    254 }
    255 
    256 /**
    257 * Log a string from the content page.
    258 *
    259 * @param {WebConsole} hud
    260 * @param {string} str
    261 */
    262 function logString(hud, str) {
    263  const onMessage = waitForMessageByType(hud, str, ".console-api");
    264  SpecialPowers.spawn(gBrowser.selectedBrowser, [str], function (arg) {
    265    content.console.log(arg);
    266  });
    267  return onMessage;
    268 }
    269 
    270 function waitForBadgeNumber(message, expectedNumber) {
    271  return waitFor(
    272    () =>
    273      message.querySelector(".warning-group-badge").textContent ==
    274      expectedNumber
    275  );
    276 }
    277 
    278 async function toggleWarningGroupPreference(hud) {
    279  info("Open the settings panel");
    280  const observer = new PrefObserver("");
    281 
    282  info("Change warning preference");
    283  const prefChanged = observer.once(WARNING_GROUP_PREF, () => {});
    284 
    285  await toggleConsoleSetting(
    286    hud,
    287    ".webconsole-console-settings-menu-item-warning-groups"
    288  );
    289 
    290  await prefChanged;
    291  observer.destroy();
    292 }