summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/JSWindowActor/browser_event_listener.js
blob: 725c2c37533c9f15ce169622f9964bf2eaab9b64 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

async function createAndShowDropdown(browser) {
  // Add a select element to the DOM of the loaded document.
  await SpecialPowers.spawn(browser, [], async function () {
    content.document.body.innerHTML += `
      <select id="testSelect">
        <option>A</option>
        <option>B</option>
      </select>`;
  });

  // Click on the select to show the dropdown.
  await BrowserTestUtils.synthesizeMouseAtCenter("#testSelect", {}, browser);
}

declTest("test event triggering actor creation", {
  events: { mozshowdropdown: {} },

  async test(browser) {
    // Wait for the observer notification.
    let observePromise = TestUtils.topicObserved(
      "test-js-window-actor-parent-event"
    );

    await createAndShowDropdown(browser);

    // Wait for the observer notification to fire, and inspect the results.
    let [subject, data] = await observePromise;
    is(data, "mozshowdropdown");

    let parent = browser.browsingContext.currentWindowGlobal;
    let actorParent = parent.getActor("TestWindow");
    ok(actorParent, "JSWindowActorParent should have value.");
    is(
      subject.wrappedJSObject,
      actorParent,
      "Should have been recieved on the right actor"
    );
  },
});

declTest("test createActor:false not triggering actor creation", {
  events: { mozshowdropdown: { createActor: false } },

  async test(browser) {
    info("before actor has been created");
    const TOPIC = "test-js-window-actor-parent-event";
    function obs() {
      ok(false, "actor should not be created");
    }
    Services.obs.addObserver(obs, TOPIC);

    await createAndShowDropdown(browser);

    // Bounce into the content process & create the actor after showing the
    // dropdown, in order to be sure that if an event was going to be
    // delivered, it already would've been.
    await SpecialPowers.spawn(browser, [], async () => {
      content.windowGlobalChild.getActor("TestWindow");
    });
    ok(true, "observer notification should not have fired");
    Services.obs.removeObserver(obs, TOPIC);

    // ----------------------------------------------------
    info("after actor has been created");
    let observePromise = TestUtils.topicObserved(
      "test-js-window-actor-parent-event"
    );
    await createAndShowDropdown(browser);

    // Wait for the observer notification to fire, and inspect the results.
    let [subject, data] = await observePromise;
    is(data, "mozshowdropdown");

    let parent = browser.browsingContext.currentWindowGlobal;
    let actorParent = parent.getActor("TestWindow");
    ok(actorParent, "JSWindowActorParent should have value.");
    is(
      subject.wrappedJSObject,
      actorParent,
      "Should have been recieved on the right actor"
    );
  },
});

async function testEventProcessedOnce(browser, waitForUrl) {
  let notificationCount = 0;
  let firstNotificationResolve;
  let firstNotification = new Promise(resolve => {
    firstNotificationResolve = resolve;
  });

  const TOPIC = "test-js-window-actor-parent-event";
  function obs(subject, topic, data) {
    is(data, "mozwindowactortestevent");
    notificationCount++;
    if (firstNotificationResolve) {
      firstNotificationResolve();
      firstNotificationResolve = null;
    }
  }
  Services.obs.addObserver(obs, TOPIC);

  if (waitForUrl) {
    info("Waiting for URI to be alright");
    await TestUtils.waitForCondition(() => {
      if (!browser.browsingContext?.currentWindowGlobal) {
        info("No CWG yet");
        return false;
      }
      return SpecialPowers.spawn(browser, [waitForUrl], async function (url) {
        info(content.document.documentURI);
        return content.document.documentURI.includes(url);
      });
    });
  }

  info("Dispatching event");
  await SpecialPowers.spawn(browser, [], async function () {
    content.document.dispatchEvent(
      new content.CustomEvent("mozwindowactortestevent", { bubbles: true })
    );
  });

  info("Waiting for notification");
  await firstNotification;

  await new Promise(r => setTimeout(r, 0));

  is(notificationCount, 1, "Should get only one notification");

  Services.obs.removeObserver(obs, TOPIC);
}

declTest("test in-process content events are not processed twice", {
  url: "about:preferences",
  events: { mozwindowactortestevent: { wantUntrusted: true } },
  async test(browser) {
    is(
      browser.getAttribute("type"),
      "content",
      "Should be a content <browser>"
    );
    is(browser.getAttribute("remotetype"), "", "Should not be remote");
    await testEventProcessedOnce(browser);
  },
});

declTest("test in-process chrome events are processed correctly", {
  url: "about:blank",
  events: { mozwindowactortestevent: { wantUntrusted: true } },
  allFrames: true,
  includeChrome: true,
  async test(browser) {
    let dialogBox = gBrowser.getTabDialogBox(browser);
    let { dialogClosed, dialog } = dialogBox.open(
      "chrome://mochitests/content/browser/dom/ipc/tests/JSWindowActor/file_dummyChromePage.html"
    );
    let chromeBrowser = dialog._frame;
    is(chromeBrowser.getAttribute("type"), "", "Should be a chrome <browser>");
    is(chromeBrowser.getAttribute("remotetype"), "", "Should not be remote");

    await testEventProcessedOnce(chromeBrowser, "dummyChromePage.html");

    dialog.close();
    await dialogClosed;
  },
});