summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/error_reporting_helpers.js
blob: 42ddbe42a2ce7e6be20873fd5f357296bc21cb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";

/**
 * Helpers for use in tests that want to verify that localized error messages
 * are logged during the test.  Because most of our errors (ex:
 * ServiceWorkerManager) generate nsIScriptError instances with flattened
 * strings (the interpolated arguments aren't kept around), we load the string
 * bundle and use it to derive the exact string message we expect for the given
 * payload.
 **/

let stringBundleService = SpecialPowers.Cc[
  "@mozilla.org/intl/stringbundle;1"
].getService(SpecialPowers.Ci.nsIStringBundleService);
let localizer = stringBundleService.createBundle(
  "chrome://global/locale/dom/dom.properties"
);

/**
 * Start monitoring the console for the given localized error message string(s)
 * with the given arguments to be logged.  Call before running code that will
 * generate the console message.  Pair with a call to
 * `wait_for_expected_message` invoked after the message should have been
 * generated.
 *
 * Multiple error messages can be expected, just repeat the msgId and args
 * argument pair as needed.
 *
 * @param {String} msgId
 *   The localization message identifier used in the properties file.
 * @param {String[]} args
 *   The list of formatting arguments we expect the error to be generated with.
 * @return {Object} Promise/handle to pass to wait_for_expected_message.
 */
function expect_console_message(/* msgId, args, ... */) {
  let expectations = [];
  // process repeated paired arguments of: msgId, args
  for (let i = 0; i < arguments.length; i += 2) {
    let msgId = arguments[i];
    let args = arguments[i + 1];
    if (args.length === 0) {
      expectations.push({ errorMessage: localizer.GetStringFromName(msgId) });
    } else {
      expectations.push({
        errorMessage: localizer.formatStringFromName(msgId, args),
      });
    }
  }
  return new Promise(resolve => {
    SimpleTest.monitorConsole(resolve, expectations);
  });
}
let expect_console_messages = expect_console_message;

/**
 * Stop monitoring the console, returning a Promise that will be resolved when
 * the sentinel console message sent through the async data path has been
 * received.  The Promise will not reject on failure; instead a mochitest
 * failure will have been generated by ok(false)/equivalent by the time it is
 * resolved.
 */
function wait_for_expected_message(expectedPromise) {
  SimpleTest.endMonitorConsole();
  return expectedPromise;
}

/**
 * Derive an absolute URL string from a relative URL to simplify error message
 * argument generation.
 */
function make_absolute_url(relUrl) {
  return new URL(relUrl, window.location).href;
}