summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/runtime/browser_executionContextEvents.js
blob: 9e6230901a8e834643164eb1444124be0d79333a (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the Runtime execution context events

const DESTROYED = "Runtime.executionContextDestroyed";
const CREATED = "Runtime.executionContextCreated";
const CLEARED = "Runtime.executionContextsCleared";

add_task(async function noEventsWhenRuntimeDomainDisabled({ client }) {
  const { Runtime } = client;

  const history = recordContextEvents(Runtime, 0);
  await loadURL(PAGE_FRAME_URL);
  await assertEventOrder({ history, expectedEvents: [] });
});

add_task(async function noEventsAfterRuntimeDomainDisabled({ client }) {
  const { Runtime } = client;

  await Runtime.enable();
  await Runtime.disable();

  const history = recordContextEvents(Runtime, 0);
  await loadURL(PAGE_FRAME_URL);
  await assertEventOrder({ history, expectedEvents: [] });
});

add_task(async function eventsWhenNavigatingWithNoFrames({ client }) {
  const { Page, Runtime } = client;

  const previousContext = await enableRuntime(client);
  const history = recordContextEvents(Runtime, 3);

  const { frameId } = await Page.navigate({ url: PAGE_FRAME_URL });
  await assertEventOrder({ history });

  const { executionContextId: destroyedId } =
    history.findEvent(DESTROYED).payload;
  is(
    destroyedId,
    previousContext.id,
    "The destroyed event reports the previous context id"
  );

  const { context: contextCreated } = history.findEvent(CREATED).payload;
  checkDefaultContext(contextCreated);
  isnot(
    contextCreated.id,
    previousContext.id,
    "The new execution context has a different id"
  );
  is(
    contextCreated.auxData.frameId,
    frameId,
    "The execution context frame id is the same " +
      "than the one returned by Page.navigate"
  );
});

add_task(async function eventsWhenNavigatingFrameSet({ client }) {
  const { Runtime } = client;

  const previousContext = await enableRuntime(client);

  // Check navigation to a frameset
  const historyTo = recordContextEvents(Runtime, 4);
  await loadURL(FRAMESET_SINGLE_URL);
  await assertEventOrder({
    history: historyTo,
    expectedEvents: [DESTROYED, CLEARED, CREATED, CREATED],
  });

  const { executionContextId: destroyedId } =
    historyTo.findEvent(DESTROYED).payload;
  is(
    destroyedId,
    previousContext.id,
    "The destroyed event reports the previous context id"
  );

  const contexts = historyTo.findEvents(CREATED);
  const createdTopContext = contexts[0].payload.context;
  const createdFrameContext = contexts[1].payload.context;

  checkDefaultContext(createdTopContext);
  isnot(
    createdTopContext.id,
    previousContext.id,
    "The new execution context has a different id"
  );
  is(
    createdTopContext.origin,
    BASE_ORIGIN,
    "The execution context origin is the frameset"
  );

  checkDefaultContext(createdFrameContext);
  isnot(
    createdFrameContext.id,
    createdTopContext.id,
    "The new frame's execution context has a different id"
  );
  is(
    createdFrameContext.origin,
    BASE_ORIGIN,
    "The frame's execution context origin is the frame"
  );

  // Check navigation from a frameset
  const historyFrom = recordContextEvents(Runtime, 4);
  await loadURL(PAGE_FRAME_URL);
  await assertEventOrder({
    history: historyFrom,
    // Bug 1644657: The cleared event should come last but we emit destroy events
    // for the top-level context and for frames afterward. Chrome only sends out
    // the cleared event on navigation.
    expectedEvents: [DESTROYED, CLEARED, CREATED, DESTROYED],
  });

  const destroyedContextIds = historyFrom.findEvents(DESTROYED);
  is(
    destroyedContextIds[0].payload.executionContextId,
    createdTopContext.id,
    "The destroyed event reports the previous context id"
  );
  is(
    destroyedContextIds[1].payload.executionContextId,
    createdFrameContext.id,
    "The destroyed event reports the previous frame's context id"
  );

  const { context: contextCreated } = historyFrom.findEvent(CREATED).payload;
  checkDefaultContext(contextCreated);
  isnot(
    contextCreated.id,
    createdTopContext.id,
    "The new execution context has a different id"
  );
  is(
    contextCreated.origin,
    BASE_ORIGIN,
    "The execution context origin is not the frameset"
  );
});

add_task(async function eventsWhenNavigatingBackWithNoFrames({ client }) {
  const { Runtime } = client;

  // Load an initial URL so that navigating back will work
  await loadURL(PAGE_FRAME_URL);
  const previousContext = await enableRuntime(client);

  const executionContextCreated = Runtime.executionContextCreated();
  await loadURL(PAGE_URL);
  const { context: createdContext } = await executionContextCreated;

  const history = recordContextEvents(Runtime, 3);
  gBrowser.selectedBrowser.goBack();
  await assertEventOrder({ history });

  const { executionContextId: destroyedId } =
    history.findEvent(DESTROYED).payload;
  is(
    destroyedId,
    createdContext.id,
    "The destroyed event reports the current context id"
  );

  const { context } = history.findEvent(CREATED).payload;
  checkDefaultContext(context);
  is(
    context.origin,
    previousContext.origin,
    "The new execution context has the same origin as the previous one."
  );
  isnot(
    context.id,
    previousContext.id,
    "The new execution context has a different id"
  );
  ok(context.auxData.isDefault, "The execution context is the default one");
  is(
    context.auxData.frameId,
    previousContext.auxData.frameId,
    "The execution context frame id is always the same"
  );
  is(context.auxData.type, "default", "Execution context has 'default' type");
  is(context.name, "", "The default execution context is named ''");

  const { result } = await Runtime.evaluate({
    contextId: context.id,
    expression: "location.href",
  });
  is(
    result.value,
    PAGE_FRAME_URL,
    "Runtime.evaluate works and is against the page we just navigated to"
  );
});

add_task(async function eventsWhenReloadingPageWithNoFrames({ client }) {
  const { Page, Runtime } = client;

  // Load an initial URL so that reload will work
  await loadURL(PAGE_FRAME_URL);
  const previousContext = await enableRuntime(client);

  await Page.enable();

  const history = recordContextEvents(Runtime, 3);
  const frameNavigated = Page.frameNavigated();
  gBrowser.selectedBrowser.reload();
  await frameNavigated;

  await assertEventOrder({ history });

  const { executionContextId } = history.findEvent(DESTROYED).payload;
  is(
    executionContextId,
    previousContext.id,
    "The destroyed event reports the previous context id"
  );

  const { context } = history.findEvent(CREATED).payload;
  checkDefaultContext(context);
  is(
    context.auxData.frameId,
    previousContext.auxData.frameId,
    "The execution context frame id is the same as before reloading"
  );

  isnot(
    executionContextId,
    context.id,
    "The destroyed id is different from the created one"
  );
});

add_task(async function eventsWhenNavigatingByLocationWithNoFrames({ client }) {
  const { Runtime } = client;

  const previousContext = await enableRuntime(client);
  const history = recordContextEvents(Runtime, 3);

  await Runtime.evaluate({
    contextId: previousContext.id,
    expression: `window.location = '${PAGE_FRAME_URL}';`,
  });
  await assertEventOrder({ history });

  const { executionContextId: destroyedId } =
    history.findEvent(DESTROYED).payload;
  is(
    destroyedId,
    previousContext.id,
    "The destroyed event reports the previous context id"
  );

  const { context: createdContext } = history.findEvent(CREATED).payload;
  checkDefaultContext(createdContext);
  is(
    createdContext.auxData.frameId,
    previousContext.auxData.frameId,
    "The execution context frame id is identical " +
      "to the one from before before setting the window's location"
  );
  isnot(
    destroyedId,
    createdContext.id,
    "The destroyed id is different from the created one"
  );
});

function recordContextEvents(Runtime, total) {
  const history = new RecordEvents(total);

  history.addRecorder({
    event: Runtime.executionContextDestroyed,
    eventName: DESTROYED,
    messageFn: payload => {
      return `Received ${DESTROYED} for id ${payload.executionContextId}`;
    },
  });
  history.addRecorder({
    event: Runtime.executionContextCreated,
    eventName: CREATED,
    messageFn: ({ context }) => {
      return (
        `Received ${CREATED} for id ${context.id}` +
        ` type: ${context.auxData.type}` +
        ` name: ${context.name}` +
        ` origin: ${context.origin}`
      );
    },
  });
  history.addRecorder({
    event: Runtime.executionContextsCleared,
    eventName: CLEARED,
  });

  return history;
}

async function assertEventOrder(options = {}) {
  const { history, expectedEvents = [DESTROYED, CLEARED, CREATED] } = options;
  const events = await history.record();
  const eventNames = events.map(item => item.eventName);
  info(`Expected events: ${expectedEvents}`);
  info(`Received events: ${eventNames}`);

  is(
    events.length,
    expectedEvents.length,
    "Received expected number of Runtime context events"
  );
  Assert.deepEqual(
    events.map(item => item.eventName),
    expectedEvents,
    "Received Runtime context events in expected order"
  );
}

function checkDefaultContext(context) {
  ok(!!context.id, "The execution context has an id");
  ok(context.auxData.isDefault, "The execution context is the default one");
  is(context.auxData.type, "default", "Execution context has 'default' type");
  ok(!!context.origin, "The execution context has an origin");
  is(context.name, "", "The default execution context is named ''");
}