summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_menus_errors.js
blob: 15696449962cd02e599a89314abaf0d922de8ec3 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

add_task(async function test_create_error() {
  // lastError is the only means to communicate errors in the menus.create API,
  // so make sure that a warning is logged to the console if the error is not
  // checked.
  let waitForConsole = new Promise(resolve => {
    SimpleTest.waitForExplicitFinish();
    SimpleTest.monitorConsole(resolve, [
      // Callback exists, lastError is checked. Should not be logged.
      {
        message: /Unchecked lastError value: Error: ID already exists: some_id/,
        forbid: true,
      },
      // No callback, lastError not checked. Should be logged.
      {
        message:
          /Unchecked lastError value: Error: Could not find any MenuItem with id: noCb/,
      },
      // Callback exists, lastError not checked. Should be logged.
      {
        message:
          /Unchecked lastError value: Error: Could not find any MenuItem with id: cbIgnoreError/,
      },
    ]);
  });

  async function background() {
    // Note: browser.menus.create returns the menu ID instead of a promise, so
    // we have to use callbacks.
    await new Promise(resolve => {
      browser.menus.create({ id: "some_id", title: "menu item" }, () => {
        browser.test.assertEq(
          null,
          browser.runtime.lastError,
          "Expected no error"
        );
        resolve();
      });
    });

    // Callback exists, lastError is checked:
    await new Promise(resolve => {
      browser.menus.create({ id: "some_id", title: "menu item" }, () => {
        browser.test.assertEq(
          "ID already exists: some_id",
          browser.runtime.lastError.message,
          "Expected error"
        );
        resolve();
      });
    });

    // No callback, lastError not checked:
    browser.menus.create({ id: "noCb", parentId: "noCb", title: "menu item" });

    // Callback exists, lastError not checked:
    await new Promise(resolve => {
      browser.menus.create(
        { id: "cbIgnoreError", parentId: "cbIgnoreError", title: "menu item" },
        () => {
          resolve();
        }
      );
    });

    // Do another roundtrip with the menus API to ensure that any console
    // error messages from the previous call are flushed.
    await browser.menus.removeAll();

    browser.test.sendMessage("done");
  }

  const extension = ExtensionTestUtils.loadExtension({
    manifest: { permissions: ["menus"] },
    background,
  });
  await extension.startup();
  await extension.awaitMessage("done");

  await extension.unload();

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

add_task(async function test_update_error() {
  async function background() {
    const id = browser.menus.create({ title: "menu item" });

    await browser.test.assertRejects(
      browser.menus.update(id, { parentId: "bogus" }),
      "Could not find any MenuItem with id: bogus",
      "menus.update with invalid parentMenuId should fail"
    );

    await browser.test.assertRejects(
      browser.menus.update(id, { parentId: id }),
      "MenuItem cannot be an ancestor (or self) of its new parent.",
      "menus.update cannot assign itself as the parent of a menu."
    );

    browser.test.sendMessage("done");
  }

  const extension = ExtensionTestUtils.loadExtension({
    manifest: { permissions: ["menus"] },
    background,
  });
  await extension.startup();
  await extension.awaitMessage("done");
  await extension.unload();
});

add_task(async function test_invalid_documentUrlPatterns() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    async background() {
      await new Promise(resolve => {
        browser.menus.create(
          {
            title: "invalid url",
            contexts: ["tab"],
            documentUrlPatterns: ["test1"],
          },
          () => {
            browser.test.assertEq(
              "Invalid url pattern: test1",
              browser.runtime.lastError.message,
              "Expected invalid match pattern"
            );
            resolve();
          }
        );
      });
      await new Promise(resolve => {
        browser.menus.create(
          {
            title: "invalid url",
            contexts: ["link"],
            targetUrlPatterns: ["test2"],
          },
          () => {
            browser.test.assertEq(
              "Invalid url pattern: test2",
              browser.runtime.lastError.message,
              "Expected invalid match pattern"
            );
            resolve();
          }
        );
      });
      browser.test.sendMessage("done");
    },
  });
  await extension.startup();
  await extension.awaitMessage("done");
  await extension.unload();
});