summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_layout-reflows-observer.js
blob: 7f6d1e9004a1bc9d991110cf55c5bc9746465f65 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the LayoutChangesObserver

/* eslint-disable mozilla/use-chromeutils-generateqi */

var {
  getLayoutChangesObserver,
  releaseLayoutChangesObserver,
  LayoutChangesObserver,
} = require("devtools/server/actors/reflow");
const EventEmitter = require("devtools/shared/event-emitter");

// Override set/clearTimeout on LayoutChangesObserver to avoid depending on
// time in this unit test. This means that LayoutChangesObserver.eventLoopTimer
// will be the timeout callback instead of the timeout itself, so test cases
// will need to execute it to fake a timeout
LayoutChangesObserver.prototype._setTimeout = cb => cb;
LayoutChangesObserver.prototype._clearTimeout = function() {};

// Mock the targetActor since we only really want to test the LayoutChangesObserver
// and don't want to depend on a window object, nor want to test protocol.js
class MockTargetActor extends EventEmitter {
  constructor() {
    super();
    this.docShell = new MockDocShell();
    this.window = new MockWindow(this.docShell);
    this.windows = [this.window];
    this.attached = true;
  }

  get chromeEventHandler() {
    return this.docShell.chromeEventHandler;
  }
}

function MockWindow(docShell) {
  this.docShell = docShell;
}
MockWindow.prototype = {
  QueryInterface: function() {
    const self = this;
    return {
      getInterface: function() {
        return {
          QueryInterface: function() {
            return self.docShell;
          },
        };
      },
    };
  },
  setTimeout: function(cb) {
    // Simply return the cb itself so that we can execute it in the test instead
    // of depending on a real timeout
    return cb;
  },
  clearTimeout: function() {},
};

function MockDocShell() {
  this.observer = null;
}
MockDocShell.prototype = {
  addWeakReflowObserver: function(observer) {
    this.observer = observer;
  },
  removeWeakReflowObserver: function() {},
  get chromeEventHandler() {
    return {
      addEventListener: (type, cb) => {
        if (type === "resize") {
          this.resizeCb = cb;
        }
      },
      removeEventListener: (type, cb) => {
        if (type === "resize" && cb === this.resizeCb) {
          this.resizeCb = null;
        }
      },
    };
  },
  mockResize: function() {
    if (this.resizeCb) {
      this.resizeCb();
    }
  },
};

function run_test() {
  instancesOfObserversAreSharedBetweenWindows();
  eventsAreBatched();
  noEventsAreSentWhenThereAreNoReflowsAndLoopTimeouts();
  observerIsAlreadyStarted();
  destroyStopsObserving();
  stoppingAndStartingSeveralTimesWorksCorrectly();
  reflowsArentStackedWhenStopped();
  stackedReflowsAreResetOnStop();
}

function instancesOfObserversAreSharedBetweenWindows() {
  info(
    "Checking that when requesting twice an instances of the observer " +
      "for the same BrowsingContextTargetActor, the instance is shared"
  );

  info("Checking 2 instances of the observer for the targetActor 1");
  const targetActor1 = new MockTargetActor();
  const obs11 = getLayoutChangesObserver(targetActor1);
  const obs12 = getLayoutChangesObserver(targetActor1);
  Assert.equal(obs11, obs12);

  info("Checking 2 instances of the observer for the targetActor 2");
  const targetActor2 = new MockTargetActor();
  const obs21 = getLayoutChangesObserver(targetActor2);
  const obs22 = getLayoutChangesObserver(targetActor2);
  Assert.equal(obs21, obs22);

  info(
    "Checking that observers instances for 2 different targetActors are " +
      "different"
  );
  Assert.notEqual(obs11, obs21);

  releaseLayoutChangesObserver(targetActor1);
  releaseLayoutChangesObserver(targetActor1);
  releaseLayoutChangesObserver(targetActor2);
  releaseLayoutChangesObserver(targetActor2);
}

function eventsAreBatched() {
  info(
    "Checking that reflow events are batched and only sent when the " +
      "timeout expires"
  );

  // Note that in this test, we mock the target actor and its window property, so we also
  // mock the setTimeout/clearTimeout mechanism and just call the callback manually
  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);

  const reflowsEvents = [];
  const onReflows = reflows => reflowsEvents.push(reflows);
  observer.on("reflows", onReflows);

  const resizeEvents = [];
  const onResize = () => resizeEvents.push("resize");
  observer.on("resize", onResize);

  info("Fake one reflow event");
  targetActor.window.docShell.observer.reflow();
  info("Checking that no batched reflow event has been emitted");
  Assert.equal(reflowsEvents.length, 0);

  info("Fake another reflow event");
  targetActor.window.docShell.observer.reflow();
  info("Checking that still no batched reflow event has been emitted");
  Assert.equal(reflowsEvents.length, 0);

  info("Fake a few of resize events too");
  targetActor.window.docShell.mockResize();
  targetActor.window.docShell.mockResize();
  targetActor.window.docShell.mockResize();
  info("Checking that still no batched resize event has been emitted");
  Assert.equal(resizeEvents.length, 0);

  info("Faking timeout expiration and checking that events are sent");
  observer.eventLoopTimer();
  Assert.equal(reflowsEvents.length, 1);
  Assert.equal(reflowsEvents[0].length, 2);
  Assert.equal(resizeEvents.length, 1);

  observer.off("reflows", onReflows);
  observer.off("resize", onResize);
  releaseLayoutChangesObserver(targetActor);
}

function noEventsAreSentWhenThereAreNoReflowsAndLoopTimeouts() {
  info(
    "Checking that if no reflows were detected and the event batching " +
      "loop expires, then no reflows event is sent"
  );

  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);

  const reflowsEvents = [];
  const onReflows = reflows => reflowsEvents.push(reflows);
  observer.on("reflows", onReflows);

  info("Faking timeout expiration and checking for reflows");
  observer.eventLoopTimer();
  Assert.equal(reflowsEvents.length, 0);

  observer.off("reflows", onReflows);
  releaseLayoutChangesObserver(targetActor);
}

function observerIsAlreadyStarted() {
  info("Checking that the observer is already started when getting it");

  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);
  Assert.ok(observer.isObserving);

  observer.stop();
  Assert.ok(!observer.isObserving);

  observer.start();
  Assert.ok(observer.isObserving);

  releaseLayoutChangesObserver(targetActor);
}

function destroyStopsObserving() {
  info("Checking that the destroying the observer stops it");

  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);
  Assert.ok(observer.isObserving);

  observer.destroy();
  Assert.ok(!observer.isObserving);

  releaseLayoutChangesObserver(targetActor);
}

function stoppingAndStartingSeveralTimesWorksCorrectly() {
  info(
    "Checking that the stopping and starting several times the observer" +
      " works correctly"
  );

  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);

  Assert.ok(observer.isObserving);
  observer.start();
  observer.start();
  observer.start();
  Assert.ok(observer.isObserving);

  observer.stop();
  Assert.ok(!observer.isObserving);

  observer.stop();
  observer.stop();
  Assert.ok(!observer.isObserving);

  releaseLayoutChangesObserver(targetActor);
}

function reflowsArentStackedWhenStopped() {
  info("Checking that when stopped, reflows aren't stacked in the observer");

  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);

  info("Stoping the observer");
  observer.stop();

  info("Faking reflows");
  targetActor.window.docShell.observer.reflow();
  targetActor.window.docShell.observer.reflow();
  targetActor.window.docShell.observer.reflow();

  info("Checking that reflows aren't recorded");
  Assert.equal(observer.reflows.length, 0);

  info("Starting the observer and faking more reflows");
  observer.start();
  targetActor.window.docShell.observer.reflow();
  targetActor.window.docShell.observer.reflow();
  targetActor.window.docShell.observer.reflow();

  info("Checking that reflows are recorded");
  Assert.equal(observer.reflows.length, 3);

  releaseLayoutChangesObserver(targetActor);
}

function stackedReflowsAreResetOnStop() {
  info("Checking that stacked reflows are reset on stop");

  const targetActor = new MockTargetActor();
  const observer = getLayoutChangesObserver(targetActor);

  targetActor.window.docShell.observer.reflow();
  Assert.equal(observer.reflows.length, 1);

  observer.stop();
  Assert.equal(observer.reflows.length, 0);

  targetActor.window.docShell.observer.reflow();
  Assert.equal(observer.reflows.length, 0);

  observer.start();
  Assert.equal(observer.reflows.length, 0);

  targetActor.window.docShell.observer.reflow();
  Assert.equal(observer.reflows.length, 1);

  releaseLayoutChangesObserver(targetActor);
}