summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_lastError.js
blob: f7015f131b379ca9ef696536b58d34c3e8072273 (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
"use strict";

async function sendMessage(options) {
  function background(options) {
    browser.runtime.sendMessage(result => {
      browser.test.assertEq(undefined, result, "Argument value");
      if (options.checkLastError) {
        browser.test.assertEq(
          "runtime.sendMessage's message argument is missing",
          browser.runtime.lastError?.message,
          "lastError value"
        );
      }
      browser.test.sendMessage("done");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background: `(${background})(${JSON.stringify(options)})`,
  });

  await extension.startup();

  await extension.awaitMessage("done");

  await extension.unload();
}

add_task(async function testLastError() {
  // Not necessary in browser-chrome tests, but monitorConsole gripes
  // if we don't call it.
  SimpleTest.waitForExplicitFinish();

  // Check that we have no unexpected console messages when lastError is
  // checked.
  let waitForConsole = new Promise(resolve => {
    SimpleTest.monitorConsole(resolve, [
      { message: /message argument is missing/, forbid: true },
    ]);
  });

  await sendMessage({ checkLastError: true });

  SimpleTest.endMonitorConsole();
  await waitForConsole;

  // Check that we do have a console message when lastError is not checked.
  waitForConsole = new Promise(resolve => {
    SimpleTest.monitorConsole(resolve, [
      {
        message:
          /Unchecked lastError value: Error: runtime.sendMessage's message argument is missing/,
      },
    ]);
  });

  await sendMessage({});

  SimpleTest.endMonitorConsole();
  await waitForConsole;
});