summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/urlbar/browser_urlbar_stop_pending.js
blob: 6b6a10ea34b6cac9157bd38533009a32f88ea8a1 (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
"use strict";

const SLOW_PAGE = "http://www.example.com/browser/browser/base/content/test/urlbar/slow-page.sjs";
const SLOW_PAGE2 = "http://mochi.test:8888/browser/browser/base/content/test/urlbar/slow-page.sjs?faster";

/**
 * Check that if we:
 * 1) have a loaded page
 * 2) load a separate URL
 * 3) before the URL for step 2 has finished loading, load a third URL
 * we don't revert to the URL from (1).
 */
add_task(function*() {
  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.com", true, true);

  let expectedURLBarChange = SLOW_PAGE;
  let sawChange = false;
  let handler = e => {
    sawChange = true;
    is(gURLBar.value, expectedURLBarChange, "Should not change URL bar value!");
  };

  let obs = new MutationObserver(handler);

  obs.observe(gURLBar, {attributes: true});
  gURLBar.value = SLOW_PAGE;
  gURLBar.handleCommand();

  // If this ever starts going intermittent, we've broken this.
  yield new Promise(resolve => setTimeout(resolve, 200));
  expectedURLBarChange = SLOW_PAGE2;
  let pageLoadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  gURLBar.value = expectedURLBarChange;
  gURLBar.handleCommand();
  is(gURLBar.value, expectedURLBarChange, "Should not have changed URL bar value synchronously.");
  yield pageLoadPromise;
  ok(sawChange, "The URL bar change handler should have been called by the time the page was loaded");
  obs.disconnect();
  obs = null;
  yield BrowserTestUtils.removeTab(tab);
});

/**
 * Check that if we:
 * 1) middle-click a link to a separate page whose server doesn't respond
 * 2) we switch to that tab and stop the request
 *
 * The URL bar continues to contain the URL of the page we wanted to visit.
 */
add_task(function*() {
  let socket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
  socket.init(-1, true, -1);
  const PORT = socket.port;
  registerCleanupFunction(() => { socket.close(); });

  const TEST_PATH = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "https://example.com");
  const BASE_PAGE = TEST_PATH + "dummy_page.html";
  const SLOW_HOST = `https://localhost:${PORT}/`;
  info("Using URLs: " + SLOW_HOST);
  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_PAGE);
  info("opened tab");
  yield ContentTask.spawn(tab.linkedBrowser, SLOW_HOST, URL => {
    let link = content.document.createElement("a");
    link.href = URL;
    link.textContent = "click me to open a slow page";
    link.id = "clickme"
    content.document.body.appendChild(link);
  });
  info("added link");
  let newTabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
  // Middle click the link:
  yield BrowserTestUtils.synthesizeMouseAtCenter("#clickme", { button: 1 }, tab.linkedBrowser);
  // get new tab, switch to it
  let newTab = (yield newTabPromise).target;
  yield BrowserTestUtils.switchTab(gBrowser, newTab);
  is(gURLBar.value, SLOW_HOST, "Should have slow page in URL bar");
  let browserStoppedPromise = BrowserTestUtils.browserStopped(newTab.linkedBrowser);
  BrowserStop();
  yield browserStoppedPromise;

  is(gURLBar.value, SLOW_HOST, "Should still have slow page in URL bar after stop");
  yield BrowserTestUtils.removeTab(newTab);
  yield BrowserTestUtils.removeTab(tab);
});
/**
 * Check that if we:
 * 1) middle-click a link to a separate page whose server doesn't respond
 * 2) we alter the URL on that page to some other server that doesn't respond
 * 3) we stop the request
 *
 * The URL bar continues to contain the second URL.
 */
add_task(function*() {
  let socket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
  socket.init(-1, true, -1);
  const PORT1 = socket.port;
  let socket2 = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
  socket2.init(-1, true, -1);
  const PORT2 = socket2.port;
  registerCleanupFunction(() => { socket.close(); socket2.close(); });

  const TEST_PATH = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "https://example.com");
  const BASE_PAGE = TEST_PATH + "dummy_page.html";
  const SLOW_HOST1 = `https://localhost:${PORT1}/`;
  const SLOW_HOST2 = `https://localhost:${PORT2}/`;
  info("Using URLs: " + SLOW_HOST1 + " and " + SLOW_HOST2);
  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_PAGE);
  info("opened tab");
  yield ContentTask.spawn(tab.linkedBrowser, SLOW_HOST1, URL => {
    let link = content.document.createElement("a");
    link.href = URL;
    link.textContent = "click me to open a slow page";
    link.id = "clickme"
    content.document.body.appendChild(link);
  });
  info("added link");
  let newTabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
  // Middle click the link:
  yield BrowserTestUtils.synthesizeMouseAtCenter("#clickme", { button: 1 }, tab.linkedBrowser);
  // get new tab, switch to it
  let newTab = (yield newTabPromise).target;
  yield BrowserTestUtils.switchTab(gBrowser, newTab);
  is(gURLBar.value, SLOW_HOST1, "Should have slow page in URL bar");
  let browserStoppedPromise = BrowserTestUtils.browserStopped(newTab.linkedBrowser);
  gURLBar.value = SLOW_HOST2;
  gURLBar.handleCommand();
  yield browserStoppedPromise;

  is(gURLBar.value, SLOW_HOST2, "Should have second slow page in URL bar");
  browserStoppedPromise = BrowserTestUtils.browserStopped(newTab.linkedBrowser);
  BrowserStop();
  yield browserStoppedPromise;

  is(gURLBar.value, SLOW_HOST2, "Should still have second slow page in URL bar after stop");
  yield BrowserTestUtils.removeTab(newTab);
  yield BrowserTestUtils.removeTab(tab);
});