tor-browser

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

browser_webconsole_script_errordoc_urls.js (2140B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Ensure that [Learn More] links appear alongside any errors listed
      5 // in "errordocs.js". Note: this only tests script execution.
      6 
      7 "use strict";
      8 
      9 const ErrorDocs = require("resource://devtools/server/actors/errordocs.js");
     10 const TEST_URI = "data:text/html;charset=utf8,<!DOCTYPE html>errordoc tests";
     11 
     12 function makeURIData(script) {
     13  return `data:text/html;charset=utf8,<!DOCTYPE html><script>${script}</script>`;
     14 }
     15 
     16 const TestData = [
     17  {
     18    jsmsg: "JSMSG_READ_ONLY",
     19    script:
     20      "'use strict'; (Object.freeze({name: 'Elsa', score: 157})).score = 0;",
     21    selector: ".error",
     22    isException: true,
     23    expected: 'TypeError: "score" is read-only',
     24  },
     25  {
     26    jsmsg: "JSMSG_STMT_AFTER_RETURN",
     27    script: "function a() { return; 1 + 1; };",
     28    selector: ".warn",
     29    isException: false,
     30    expected: "unreachable code after return statement",
     31  },
     32 ];
     33 
     34 add_task(async function () {
     35  const hud = await openNewTabAndConsole(TEST_URI);
     36 
     37  for (const data of TestData) {
     38    await testScriptError(hud, data);
     39  }
     40 });
     41 
     42 async function testScriptError(hud, testData) {
     43  const isE10s = Services.appinfo.browserTabsRemoteAutostart;
     44  if (testData.isException && !isE10s) {
     45    expectUncaughtException();
     46  }
     47 
     48  await navigateTo(makeURIData(testData.script));
     49 
     50  const msg = "the expected error message was displayed";
     51  info(`waiting for ${msg} to be displayed`);
     52  await waitFor(() =>
     53    findMessageByType(hud, testData.expected, testData.selector)
     54  );
     55  ok(true, msg);
     56 
     57  // grab the most current error doc URL.
     58  const urlObj = new URL(
     59    ErrorDocs.GetURL({ errorMessageName: testData.jsmsg })
     60  );
     61 
     62  // strip all params from the URL.
     63  const url = `${urlObj.origin}${urlObj.pathname}`;
     64 
     65  // Gather all URLs displayed in the console. [Learn More] links have no href
     66  // but have the URL in the title attribute.
     67  const hrefs = new Set();
     68  for (const link of hud.ui.outputNode.querySelectorAll("a")) {
     69    hrefs.add(link.title);
     70  }
     71 
     72  ok(hrefs.has(url), `Expected a link to ${url}.`);
     73 
     74  await clearOutput(hud);
     75 }