summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/browser/browser_NavigationManager_notify.js
blob: 4dca0f7b4ec28ab2df38b1ef030f140311b550a5 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { NavigationManager, notifyNavigationStarted, notifyNavigationStopped } =
  ChromeUtils.importESModule(
    "chrome://remote/content/shared/NavigationManager.sys.mjs"
  );
const { TabManager } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/TabManager.sys.mjs"
);

const FIRST_URL = "https://example.com/document-builder.sjs?html=first";
const SECOND_URL = "https://example.com/document-builder.sjs?html=second";

add_task(async function test_notifyNavigationStartedStopped() {
  const tab = addTab(gBrowser, FIRST_URL);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser, false, FIRST_URL);

  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  navigationManager.startMonitoring();

  const navigableId = TabManager.getIdForBrowser(browser);

  info("Programmatically start a navigation");
  const startedNavigation = notifyNavigationStarted({
    contextDetails: {
      context: browser.browsingContext,
    },
    url: SECOND_URL,
  });

  const navigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  assertNavigation(navigation, SECOND_URL);

  is(
    startedNavigation,
    navigation,
    "notifyNavigationStarted returned the expected navigation"
  );
  is(events.length, 1, "Only one event recorded");

  info("Attempt to start a navigation while another one is in progress");
  const alreadyStartedNavigation = notifyNavigationStarted({
    contextDetails: {
      context: browser.browsingContext,
    },
    url: SECOND_URL,
  });
  is(
    alreadyStartedNavigation,
    navigation,
    "notifyNavigationStarted returned the ongoing navigation"
  );
  is(events.length, 1, "Still only one event recorded");

  info("Programmatically stop the navigation");
  const stoppedNavigation = notifyNavigationStopped({
    contextDetails: {
      context: browser.browsingContext,
    },
    url: SECOND_URL,
  });
  is(
    stoppedNavigation,
    navigation,
    "notifyNavigationStopped returned the expected navigation"
  );

  is(events.length, 2, "Two events recorded");
  assertNavigationEvents(
    events,
    SECOND_URL,
    navigation.navigationId,
    navigableId
  );

  info("Attempt to stop an already stopped navigation");
  const alreadyStoppedNavigation = notifyNavigationStopped({
    contextDetails: {
      context: browser.browsingContext,
    },
    url: SECOND_URL,
  });
  is(
    alreadyStoppedNavigation,
    navigation,
    "notifyNavigationStopped returned the already stopped navigation"
  );
  is(events.length, 2, "Still only two events recorded");

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
  navigationManager.stopMonitoring();
});

add_task(async function test_notifyNavigationWithContextDetails() {
  const tab = addTab(gBrowser, FIRST_URL);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser, false, FIRST_URL);

  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  navigationManager.startMonitoring();

  const navigableId = TabManager.getIdForBrowser(browser);

  info("Programmatically start a navigation using browsing context details");
  const startedNavigation = notifyNavigationStarted({
    contextDetails: {
      browsingContextId: browser.browsingContext.id,
      browserId: browser.browsingContext.browserId,
      isTopBrowsingContext: browser.browsingContext.parent === null,
    },
    url: SECOND_URL,
  });

  const navigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  assertNavigation(navigation, SECOND_URL);

  is(
    startedNavigation,
    navigation,
    "notifyNavigationStarted returned the expected navigation"
  );
  is(events.length, 1, "Only one event recorded");

  info("Programmatically stop the navigation using browsing context details");
  const stoppedNavigation = notifyNavigationStopped({
    contextDetails: {
      browsingContextId: browser.browsingContext.id,
      browserId: browser.browsingContext.browserId,
      isTopBrowsingContext: browser.browsingContext.parent === null,
    },
    url: SECOND_URL,
  });
  is(
    stoppedNavigation,
    navigation,
    "notifyNavigationStopped returned the expected navigation"
  );

  is(events.length, 2, "Two events recorded");
  assertNavigationEvents(
    events,
    SECOND_URL,
    navigation.navigationId,
    navigableId
  );

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
  navigationManager.stopMonitoring();
});