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

"use strict";

add_task(async function noEventWhenPageDomainDisabled({ client }) {
  await loadURL(PAGE_URL);
  await runNavigatedWithinDocumentTest(client, 0, async () => {
    info("Navigate to the '#hash' anchor in the page");
    await navigateToAnchor(PAGE_URL, "hash");
  });
});

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

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

  await loadURL(PAGE_URL);

  await runNavigatedWithinDocumentTest(client, 0, async () => {
    info("Navigate to the '#hash' anchor in the page");
    await navigateToAnchor(PAGE_URL, "hash");
  });
});

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

  await Page.enable();

  await loadURL(PAGE_URL);

  await runNavigatedWithinDocumentTest(client, 1, async () => {
    info("Navigate to the '#hash' anchor in the page");
    await navigateToAnchor(PAGE_URL, "hash");
  });
});

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

  await Page.enable();

  await navigateToAnchor(PAGE_URL, "hash");

  await runNavigatedWithinDocumentTest(client, 1, async () => {
    info("Navigate to the '#hash' anchor in the page");
    await navigateToAnchor(PAGE_URL, "other-hash");
  });
});

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

  await Page.enable();

  await loadURL(FRAMESET_NESTED_URL);

  await runNavigatedWithinDocumentTest(client, 1, async () => {
    info("Navigate to the '#hash' anchor in the first iframe");
    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
      const iframe = content.frames[0];
      const baseUrl = iframe.location.href;
      iframe.location.href = baseUrl + "#hash-first-frame";
    });
  });

  await runNavigatedWithinDocumentTest(client, 2, async () => {
    info("Navigate to the '#hash' anchor in the nested iframes");
    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
      const nestedFrame1 = content.frames[0].frames[0];
      const baseUrl1 = nestedFrame1.location.href;
      nestedFrame1.location.href = baseUrl1 + "#hash-nested-frame-1";
      const nestedFrame2 = content.frames[0].frames[0];
      const baseUrl2 = nestedFrame2.location.href;
      nestedFrame2.location.href = baseUrl2 + "#hash-nested-frame-2";
    });
  });
});

async function runNavigatedWithinDocumentTest(
  client,
  expectedEventCount,
  callback
) {
  const { Page } = client;

  const NAVIGATED = "Page.navigatedWithinDocument";

  const history = new RecordEvents(expectedEventCount);
  history.addRecorder({
    event: Page.navigatedWithinDocument,
    eventName: NAVIGATED,
    messageFn: payload => {
      return `Received ${NAVIGATED} for frame id ${payload.frameId}`;
    },
  });

  await callback();

  const navigatedWithinDocumentEvents = await history.record();

  is(
    navigatedWithinDocumentEvents.length,
    expectedEventCount,
    "Got expected amount of navigatedWithinDocument events"
  );
  if (expectedEventCount == 0) {
    return;
  }

  const frames = await getFlattenedFrameTree(client);

  navigatedWithinDocumentEvents.forEach(({ payload }) => {
    const { frameId, url } = payload;

    const frame = frames.get(frameId);
    ok(frame, "Returned a valid frame id");
    is(url, frame.url, "Returned the expectedUrl");
  });
}

function navigateToAnchor(baseUrl, hash) {
  const url = `${baseUrl}#${hash}`;
  const onLocationChange = BrowserTestUtils.waitForLocationChange(
    gBrowser,
    url
  );
  BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, url);
  return onLocationChange;
}