summaryrefslogtreecommitdiffstats
path: root/remote/shared/messagehandler/test/browser/browser_events_interception.js
blob: aaf39353a69d9a6a2522f9c40bf21ce80ad39cc3 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

/**
 * Test that events can be intercepted in the windowglobal-in-root layer.
 */
add_task(async function test_intercepted_event() {
  const tab = BrowserTestUtils.addTab(
    gBrowser,
    "https://example.com/document-builder.sjs?html=tab"
  );
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  const browsingContext = tab.linkedBrowser.browsingContext;

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

  const onInterceptedEvent = rootMessageHandler.once(
    "event.testEventWithInterception"
  );
  rootMessageHandler.handleCommand({
    moduleName: "event",
    commandName: "testEmitEventWithInterception",
    destination: {
      type: WindowGlobalMessageHandler.type,
      id: browsingContext.id,
    },
  });

  const interceptedEvent = await onInterceptedEvent;
  is(
    interceptedEvent.additionalInformation,
    "information added through interception",
    "Intercepted event contained additional information"
  );

  rootMessageHandler.destroy();
  gBrowser.removeTab(tab);
});

/**
 * Test that events can be canceled in the windowglobal-in-root layer.
 */
add_task(async function test_cancelable_event() {
  const tab = BrowserTestUtils.addTab(
    gBrowser,
    "https://example.com/document-builder.sjs?html=tab"
  );
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  const browsingContext = tab.linkedBrowser.browsingContext;

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

  const cancelableEvents = [];
  const onCancelableEvent = (name, event) => cancelableEvents.push(event);
  rootMessageHandler.on(
    "event.testEventCancelableWithInterception",
    onCancelableEvent
  );

  // Emit an event that should be canceled in the windowglobal-in-root layer.
  // Note that `shouldCancel` is only something supported for this test event,
  // and not a general message handler mechanism to cancel events.
  await rootMessageHandler.handleCommand({
    moduleName: "event",
    commandName: "testEmitEventCancelableWithInterception",
    destination: {
      type: WindowGlobalMessageHandler.type,
      id: browsingContext.id,
    },
    params: {
      shouldCancel: true,
    },
  });

  is(cancelableEvents.length, 0, "No event was received");

  // Emit another event which should not be canceled (shouldCancel: false).
  await rootMessageHandler.handleCommand({
    moduleName: "event",
    commandName: "testEmitEventCancelableWithInterception",
    destination: {
      type: WindowGlobalMessageHandler.type,
      id: browsingContext.id,
    },
    params: {
      shouldCancel: false,
    },
  });

  await TestUtils.waitForCondition(() => cancelableEvents.length == 1);
  is(cancelableEvents[0].shouldCancel, false, "Expected event was received");

  rootMessageHandler.off(
    "event.testEventCancelableWithInterception",
    onCancelableEvent
  );
  rootMessageHandler.destroy();
  gBrowser.removeTab(tab);
});