summaryrefslogtreecommitdiffstats
path: root/remote/shared/messagehandler/test/browser/browser_handle_command_errors.js
blob: 5f0bdb78df2b76eb139a680a075d66be4615aa13 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { RootMessageHandler } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/messagehandler/RootMessageHandler.sys.mjs"
);

// Check that errors from WindowGlobal modules can be caught by the consumer
// of the RootMessageHandler.
add_task(async function test_module_error() {
  const browsingContextId = gBrowser.selectedBrowser.browsingContext.id;

  const rootMessageHandler = createRootMessageHandler("session-id-error");

  info("Call a module method which will throw");
  try {
    await rootMessageHandler.handleCommand({
      moduleName: "commandwindowglobalonly",
      commandName: "testError",
      destination: {
        type: WindowGlobalMessageHandler.type,
        id: browsingContextId,
      },
    });
    ok(false, "Error from window global module was not caught");
  } catch (e) {
    ok(true, "Error from window global module caught");
  }

  rootMessageHandler.destroy();
});

// Check that sending commands to incorrect destinations creates an error which
// can be caught by the consumer of the RootMessageHandler.
add_task(async function test_destination_error() {
  const rootMessageHandler = createRootMessageHandler("session-id-error");

  const fakeBrowsingContextId = -1;
  ok(
    !BrowsingContext.get(fakeBrowsingContextId),
    "No browsing context matches fakeBrowsingContextId"
  );

  info("Call a valid module method, but on a non-existent browsing context id");
  Assert.throws(
    () =>
      rootMessageHandler.handleCommand({
        moduleName: "commandwindowglobalonly",
        commandName: "testOnlyInWindowGlobal",
        destination: {
          type: WindowGlobalMessageHandler.type,
          id: fakeBrowsingContextId,
        },
      }),
    err => err.message == `Unable to find a BrowsingContext for id -1`
  );

  rootMessageHandler.destroy();
});

add_task(async function test_invalid_module_error() {
  const rootMessageHandler = createRootMessageHandler(
    "session-id-missing_module"
  );

  info("Attempt to call a Root module which has a syntax error");
  Assert.throws(
    () =>
      rootMessageHandler.handleCommand({
        moduleName: "invalid",
        commandName: "someMethod",
        destination: {
          type: RootMessageHandler.type,
        },
      }),
    err =>
      err.name === "SyntaxError" &&
      err.message == "expected expression, got ';'"
  );

  rootMessageHandler.destroy();
});

add_task(async function test_missing_root_module_error() {
  const rootMessageHandler = createRootMessageHandler(
    "session-id-missing_module"
  );

  info("Attempt to call a Root module which doesn't exist");
  Assert.throws(
    () =>
      rootMessageHandler.handleCommand({
        moduleName: "missingmodule",
        commandName: "someMethod",
        destination: {
          type: RootMessageHandler.type,
        },
      }),
    err =>
      err.name == "UnsupportedCommandError" &&
      err.message ==
        `missingmodule.someMethod not supported for destination ROOT`
  );

  rootMessageHandler.destroy();
});

add_task(async function test_missing_windowglobal_module_error() {
  const browsingContextId = gBrowser.selectedBrowser.browsingContext.id;
  const rootMessageHandler = createRootMessageHandler(
    "session-id-missing_windowglobal_module"
  );

  info("Attempt to call a WindowGlobal module which doesn't exist");
  Assert.throws(
    () =>
      rootMessageHandler.handleCommand({
        moduleName: "missingmodule",
        commandName: "someMethod",
        destination: {
          type: WindowGlobalMessageHandler.type,
          id: browsingContextId,
        },
      }),
    err =>
      err.name == "UnsupportedCommandError" &&
      err.message ==
        `missingmodule.someMethod not supported for destination WINDOW_GLOBAL`
  );

  rootMessageHandler.destroy();
});

add_task(async function test_missing_root_method_error() {
  const rootMessageHandler = createRootMessageHandler(
    "session-id-missing_root_method"
  );

  info("Attempt to call an invalid method on a Root module");
  Assert.throws(
    () =>
      rootMessageHandler.handleCommand({
        moduleName: "command",
        commandName: "wrongMethod",
        destination: {
          type: RootMessageHandler.type,
        },
      }),
    err =>
      err.name == "UnsupportedCommandError" &&
      err.message == `command.wrongMethod not supported for destination ROOT`
  );

  rootMessageHandler.destroy();
});

add_task(async function test_missing_windowglobal_method_error() {
  const browsingContextId = gBrowser.selectedBrowser.browsingContext.id;
  const rootMessageHandler = createRootMessageHandler(
    "session-id-missing_windowglobal_method"
  );

  info("Attempt to call an invalid method on a WindowGlobal module");
  Assert.throws(
    () =>
      rootMessageHandler.handleCommand({
        moduleName: "commandwindowglobalonly",
        commandName: "wrongMethod",
        destination: {
          type: WindowGlobalMessageHandler.type,
          id: browsingContextId,
        },
      }),
    err =>
      err.name == "UnsupportedCommandError" &&
      err.message ==
        `commandwindowglobalonly.wrongMethod not supported for destination WINDOW_GLOBAL`
  );

  rootMessageHandler.destroy();
});

/**
 * This test checks that even if a command is rerouted to another command after
 * the RootMessageHandler, we still check the new command and log a useful
 * error message.
 *
 * This illustrates why it is important to perform the command check at each
 * layer of the MessageHandler network.
 */
add_task(async function test_missing_intermediary_method_error() {
  const browsingContextId = gBrowser.selectedBrowser.browsingContext.id;
  const rootMessageHandler = createRootMessageHandler(
    "session-id-missing_intermediary_method"
  );

  info(
    "Call a (valid) command that relies on another (missing) command on a WindowGlobal module"
  );
  await Assert.rejects(
    rootMessageHandler.handleCommand({
      moduleName: "commandwindowglobalonly",
      commandName: "testMissingIntermediaryMethod",
      destination: {
        type: WindowGlobalMessageHandler.type,
        id: browsingContextId,
      },
    }),
    err =>
      err.name == "UnsupportedCommandError" &&
      err.message ==
        `commandwindowglobalonly.missingMethod not supported for destination WINDOW_GLOBAL`
  );

  rootMessageHandler.destroy();
});