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

"use strict";

requestLongerTimeout(2);

add_task(async function noEventsWhenPageDomainDisabled({ client }) {
  await runPageLifecycleTest(client, 0, async () => {
    info("Navigate to a page with nested iframes");
    await loadURL(FRAMESET_NESTED_URL);
  });
});

add_task(async function noEventAfterPageDomainDisabled({ client }) {
  const { Page } = client;

  await Page.enable();
  await Page.disable();

  await runPageLifecycleTest(client, 0, async () => {
    info("Navigate to a page with nested iframes");
    await loadURL(FRAMESET_NESTED_URL);
  });
});

add_task(async function noEventWhenLifeCycleDisabled({ client }) {
  const { Page } = client;

  await Page.enable();

  await runPageLifecycleTest(client, 0, async () => {
    info("Navigate to a page with nested iframes");
    await loadURL(FRAMESET_NESTED_URL);
  });
});

add_task(async function noEventAfterLifeCycleDisabled({ client }) {
  const { Page } = client;

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });
  await Page.setLifecycleEventsEnabled({ enabled: false });

  await runPageLifecycleTest(client, 0, async () => {
    info("Navigate to a page with nested iframes");
    await loadURL(FRAMESET_NESTED_URL);
  });
});

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

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });

  await runPageLifecycleTest(client, 1, async () => {
    info("Navigate to a page with no iframes");
    await loadURL(PAGE_URL);
  });
});

add_task(async function eventsWhenNavigatingToURLWithNoFrames({ client }) {
  const { Page } = client;

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });

  await runPageLifecycleTest(client, 1, async () => {
    info("Navigate to a URL with no frames");
    await loadURL(PAGE_URL);
  });
});

add_task(async function eventsWhenNavigatingToSameURLWithNoFrames({ client }) {
  const { Page } = client;

  info("Navigate to a page with no iframes");
  await loadURL(PAGE_URL);

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });

  await runPageLifecycleTest(client, 1, async () => {
    info("Navigate to the same page with no iframes");
    await loadURL(PAGE_URL);
  });
});

add_task(async function eventsWhenReloadingSameURLWithNoFrames({ client }) {
  const { Page } = client;

  info("Navigate to a page with no iframes");
  await loadURL(PAGE_URL);

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });

  await runPageLifecycleTest(client, 1, async () => {
    info("Reload page with no iframes");
    const pageLoaded = Page.loadEventFired();
    await Page.reload();
    await pageLoaded;
  });
});

add_task(async function eventsWhenNavigatingWithFrames({ client }) {
  const { Page } = client;

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });

  await runPageLifecycleTest(client, 3, async () => {
    info("Navigate to a page with iframes");
    await loadURL(FRAMESET_MULTI_URL);
  });
});

add_task(async function eventsWhenNavigatingWithNestedFrames({ client }) {
  const { Page } = client;

  await Page.enable();
  await Page.setLifecycleEventsEnabled({ enabled: true });

  await runPageLifecycleTest(client, 4, async () => {
    info("Navigate to a page with nested iframes");
    await loadURL(FRAMESET_NESTED_URL);
  });
});

async function runPageLifecycleTest(client, expectedEventSets, callback) {
  const { Page } = client;

  const LIFECYCLE = "Page.lifecycleEvent";
  const LIFECYCLE_EVENTS = ["init", "DOMContentLoaded", "load"];

  const expectedEventCount = expectedEventSets * LIFECYCLE_EVENTS.length;
  const history = new RecordEvents(expectedEventCount);
  history.addRecorder({
    event: Page.lifecycleEvent,
    eventName: LIFECYCLE,
    messageFn: payload => {
      return (
        `Received "${payload.name}" ${LIFECYCLE} ` +
        `for frame id ${payload.frameId}`
      );
    },
  });

  await callback();

  const flattenedFrameTree = await getFlattenedFrameTree(client);

  const lifecycleEvents = await history.record();
  is(
    lifecycleEvents.length,
    expectedEventCount,
    "Got expected amount of lifecycle events"
  );

  if (expectedEventCount == 0) {
    return;
  }

  // Check lifecycle events for each frame
  for (const frame of flattenedFrameTree.values()) {
    info(`Check frame id ${frame.id}`);

    const frameEvents = lifecycleEvents.filter(({ payload }) => {
      return payload.frameId == frame.id;
    });

    Assert.deepEqual(
      frameEvents.map(event => event.payload.name),
      LIFECYCLE_EVENTS,
      "Received various lifecycle events in the expected order"
    );

    // Check data as exposed by each of these events
    let lastTimestamp = frameEvents[0].payload.timestamp;
    frameEvents.forEach(({ payload }, index) => {
      ok(
        payload.timestamp >= lastTimestamp,
        "timestamp succeeds the one from the former event"
      );
      lastTimestamp = payload.timestamp;

      is(payload.loaderId, frame.loaderId, `event has expected loaderId`);
    });
  }
}