summaryrefslogtreecommitdiffstats
path: root/testing/marionette/test/unit/test_dom.js
blob: ddb1c7e30b9da9755585e3c976b61c7025879bce (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const {
  ContentEventObserverService,
  WebElementEventTarget,
} = ChromeUtils.import("chrome://marionette/content/dom.js");

class MessageSender {
  constructor() {
    this.listeners = {};
    this.sent = [];
  }

  addMessageListener(name, listener) {
    this.listeners[name] = listener;
  }

  sendAsyncMessage(name, data) {
    this.sent.push({ name, data });
  }
}

class Window {
  constructor() {
    this.events = [];
  }

  addEventListener(type) {
    this.events.push(type);
  }

  removeEventListener(type) {
    for (let i = 0; i < this.events.length; ++i) {
      if (this.events[i] === type) {
        this.events.splice(i, 1);
        return;
      }
    }
  }
}

add_test(function test_WebElementEventTarget_addEventListener_init() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);
  equal(Object.keys(eventTarget.listeners).length, 0);
  equal(Object.keys(ipc.listeners).length, 1);

  run_next_test();
});

add_test(function test_addEventListener() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);

  let listener = () => {};
  eventTarget.addEventListener("click", listener);

  // click listener was appended
  equal(Object.keys(eventTarget.listeners).length, 1);
  ok("click" in eventTarget.listeners);
  equal(eventTarget.listeners.click.length, 1);
  equal(eventTarget.listeners.click[0], listener);

  // should have sent a registration message
  deepEqual(ipc.sent[0], {
    name: "Marionette:DOM:AddEventListener",
    data: { type: "click" },
  });

  run_next_test();
});

add_test(function test_addEventListener_sameReference() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);

  let listener = () => {};
  eventTarget.addEventListener("click", listener);
  eventTarget.addEventListener("click", listener);
  equal(eventTarget.listeners.click.length, 1);

  run_next_test();
});

add_test(function test_WebElementEventTarget_addEventListener_once() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);

  eventTarget.addEventListener("click", () => {}, { once: true });
  equal(eventTarget.listeners.click[0].once, true);

  eventTarget.dispatchEvent({ type: "click" });
  equal(eventTarget.listeners.click.length, 0);
  deepEqual(ipc.sent[1], {
    name: "Marionette:DOM:RemoveEventListener",
    data: { type: "click" },
  });

  run_next_test();
});

add_test(function test_WebElementEventTarget_removeEventListener() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);

  equal(Object.keys(eventTarget.listeners).length, 0);
  eventTarget.removeEventListener("click", () => {});
  equal(Object.keys(eventTarget.listeners).length, 0);

  let firstListener = () => {};
  eventTarget.addEventListener("click", firstListener);
  equal(eventTarget.listeners.click.length, 1);
  ok(eventTarget.listeners.click[0] === firstListener);

  let secondListener = () => {};
  eventTarget.addEventListener("click", secondListener);
  equal(eventTarget.listeners.click.length, 2);
  ok(eventTarget.listeners.click[1] === secondListener);

  ok(eventTarget.listeners.click[0] !== eventTarget.listeners.click[1]);

  eventTarget.removeEventListener("click", secondListener);
  equal(eventTarget.listeners.click.length, 1);
  ok(eventTarget.listeners.click[0] === firstListener);

  // event should not have been unregistered
  // because there still exists another click event
  equal(ipc.sent[ipc.sent.length - 1].name, "Marionette:DOM:AddEventListener");

  eventTarget.removeEventListener("click", firstListener);
  equal(eventTarget.listeners.click.length, 0);
  deepEqual(ipc.sent[ipc.sent.length - 1], {
    name: "Marionette:DOM:RemoveEventListener",
    data: { type: "click" },
  });

  run_next_test();
});

add_test(function test_WebElementEventTarget_dispatchEvent() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);

  let listenerCalled = false;
  let listener = () => (listenerCalled = true);
  eventTarget.addEventListener("click", listener);
  eventTarget.dispatchEvent({ type: "click" });
  ok(listenerCalled);

  run_next_test();
});

add_test(function test_WebElementEventTarget_dispatchEvent_multipleListeners() {
  let ipc = new MessageSender();
  let eventTarget = new WebElementEventTarget(ipc);

  let clicksA = 0;
  let clicksB = 0;
  let listenerA = () => ++clicksA;
  let listenerB = () => ++clicksB;

  // the same listener should only be added, and consequently fire, once
  eventTarget.addEventListener("click", listenerA);
  eventTarget.addEventListener("click", listenerA);
  eventTarget.addEventListener("click", listenerB);
  eventTarget.dispatchEvent({ type: "click" });
  equal(clicksA, 1);
  equal(clicksB, 1);

  run_next_test();
});

add_test(function test_ContentEventObserverService_add() {
  let ipc = new MessageSender();
  let win = new Window();
  let obs = new ContentEventObserverService(
    win,
    ipc.sendAsyncMessage.bind(ipc)
  );

  equal(obs.events.size, 0);
  equal(win.events.length, 0);

  obs.add("foo");
  equal(obs.events.size, 1);
  equal(win.events.length, 1);
  equal(obs.events.values().next().value, "foo");
  equal(win.events[0], "foo");

  obs.add("foo");
  equal(obs.events.size, 1);
  equal(win.events.length, 1);

  run_next_test();
});

add_test(function test_ContentEventObserverService_remove() {
  let ipc = new MessageSender();
  let win = new Window();
  let obs = new ContentEventObserverService(
    win,
    ipc.sendAsyncMessage.bind(ipc)
  );

  obs.remove("foo");
  equal(obs.events.size, 0);
  equal(win.events.length, 0);

  obs.add("bar");
  equal(obs.events.size, 1);
  equal(win.events.length, 1);

  obs.remove("bar");
  equal(obs.events.size, 0);
  equal(win.events.length, 0);

  obs.add("baz");
  obs.add("baz");
  equal(obs.events.size, 1);
  equal(win.events.length, 1);

  obs.add("bah");
  equal(obs.events.size, 2);
  equal(win.events.length, 2);

  obs.remove("baz");
  equal(obs.events.size, 1);
  equal(win.events.length, 1);

  obs.remove("bah");
  equal(obs.events.size, 0);
  equal(win.events.length, 0);

  run_next_test();
});

add_test(function test_ContentEventObserverService_clear() {
  let ipc = new MessageSender();
  let win = new Window();
  let obs = new ContentEventObserverService(
    win,
    ipc.sendAsyncMessage.bind(ipc)
  );

  obs.clear();
  equal(obs.events.size, 0);
  equal(win.events.length, 0);

  obs.add("foo");
  obs.add("foo");
  obs.add("bar");
  equal(obs.events.size, 2);
  equal(win.events.length, 2);

  obs.clear();
  equal(obs.events.size, 0);
  equal(win.events.length, 0);

  run_next_test();
});

add_test(function test_ContentEventObserverService_handleEvent() {
  let ipc = new MessageSender();
  let win = new Window();
  let obs = new ContentEventObserverService(
    win,
    ipc.sendAsyncMessage.bind(ipc)
  );

  obs.handleEvent({ type: "click", target: win });
  deepEqual(ipc.sent[0], {
    name: "Marionette:DOM:OnEvent",
    data: { type: "click" },
  });

  run_next_test();
});