tor-browser

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

error_reporting_helpers.js (2615B)


      1 "use strict";
      2 
      3 /**
      4 * Helpers for use in tests that want to verify that localized error messages
      5 * are logged during the test.  Because most of our errors (ex:
      6 * ServiceWorkerManager) generate nsIScriptError instances with flattened
      7 * strings (the interpolated arguments aren't kept around), we load the string
      8 * bundle and use it to derive the exact string message we expect for the given
      9 * payload.
     10 */
     11 
     12 let stringBundleService = SpecialPowers.Cc[
     13  "@mozilla.org/intl/stringbundle;1"
     14 ].getService(SpecialPowers.Ci.nsIStringBundleService);
     15 let localizer = stringBundleService.createBundle(
     16  "chrome://global/locale/dom/dom.properties"
     17 );
     18 
     19 /**
     20 * Start monitoring the console for the given localized error message string(s)
     21 * with the given arguments to be logged.  Call before running code that will
     22 * generate the console message.  Pair with a call to
     23 * `wait_for_expected_message` invoked after the message should have been
     24 * generated.
     25 *
     26 * Multiple error messages can be expected, just repeat the msgId and args
     27 * argument pair as needed.
     28 *
     29 * @param {string} msgId
     30 *   The localization message identifier used in the properties file.
     31 * @param {string[]} args
     32 *   The list of formatting arguments we expect the error to be generated with.
     33 * @return {object} Promise/handle to pass to wait_for_expected_message.
     34 */
     35 function expect_console_message(/* msgId, args, ... */) {
     36  let expectations = [];
     37  // process repeated paired arguments of: msgId, args
     38  for (let i = 0; i < arguments.length; i += 2) {
     39    let msgId = arguments[i];
     40    let args = arguments[i + 1];
     41    if (args.length === 0) {
     42      expectations.push({ errorMessage: localizer.GetStringFromName(msgId) });
     43    } else {
     44      expectations.push({
     45        errorMessage: localizer.formatStringFromName(msgId, args),
     46      });
     47    }
     48  }
     49  return new Promise(resolve => {
     50    SimpleTest.monitorConsole(resolve, expectations);
     51  });
     52 }
     53 let expect_console_messages = expect_console_message;
     54 
     55 /**
     56 * Stop monitoring the console, returning a Promise that will be resolved when
     57 * the sentinel console message sent through the async data path has been
     58 * received.  The Promise will not reject on failure; instead a mochitest
     59 * failure will have been generated by ok(false)/equivalent by the time it is
     60 * resolved.
     61 */
     62 function wait_for_expected_message(expectedPromise) {
     63  SimpleTest.endMonitorConsole();
     64  return expectedPromise;
     65 }
     66 
     67 /**
     68 * Derive an absolute URL string from a relative URL to simplify error message
     69 * argument generation.
     70 */
     71 function make_absolute_url(relUrl) {
     72  return new URL(relUrl, window.location).href;
     73 }