tor-browser

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

browser_webconsole_warning_groups.js (9074B)


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