summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_navigateEvents.js
blob: f8652f197ea76330cf9a0046d8a683696888039a (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
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const URL1 = MAIN_DOMAIN + "navigate-first.html";
const URL2 = MAIN_DOMAIN + "navigate-second.html";

var events = require("sdk/event/core");
var client;

SpecialPowers.pushPrefEnv({"set": [["dom.require_user_interaction_for_beforeunload", false]]});

// State machine to check events order
var i = 0;
function assertEvent(event, data) {
  let x = 0;
  switch (i++) {
    case x++:
      is(event, "request", "Get first page load");
      is(data, URL1);
      break;
    case x++:
      is(event, "load-new-document", "Ask to load the second page");
      break;
    case x++:
      is(event, "unload-dialog", "We get the dialog on first page unload");
      break;
    case x++:
      is(event, "will-navigate", "The very first event is will-navigate on server side");
      is(data.newURI, URL2, "newURI property is correct");
      break;
    case x++:
      is(event, "request", "RDP is async with messageManager, the request happens after will-navigate");
      is(data, URL2);
      break;
    case x++:
      is(event, "tabNavigated", "After the request, the client receive tabNavigated");
      is(data.state, "start", "state is start");
      is(data.url, URL2, "url property is correct");
      is(data.nativeConsoleAPI, true, "nativeConsoleAPI is correct");
      break;
    case x++:
      is(event, "DOMContentLoaded");
      is(content.document.readyState, "interactive");
      break;
    case x++:
      is(event, "load");
      is(content.document.readyState, "complete");
      break;
    case x++:
      is(event, "navigate", "Then once the second doc is loaded, we get the navigate event");
      is(content.document.readyState, "complete", "navigate is emitted only once the document is fully loaded");
      break;
    case x++:
      is(event, "tabNavigated", "Finally, the receive the client event");
      is(data.state, "stop", "state is stop");
      is(data.url, URL2, "url property is correct");
      is(data.nativeConsoleAPI, true, "nativeConsoleAPI is correct");

      // End of test!
      cleanup();
      break;
  }
}

function waitForOnBeforeUnloadDialog(browser, callback) {
  browser.addEventListener("DOMWillOpenModalDialog", function onModalDialog() {
    browser.removeEventListener("DOMWillOpenModalDialog", onModalDialog, true);

    executeSoon(() => {
      let stack = browser.parentNode;
      let dialogs = stack.getElementsByTagName("tabmodalprompt");
      let {button0, button1} = dialogs[0].ui;
      callback(button0, button1);
    });
  }, true);
}

var httpObserver = function (subject, topic, state) {
  let channel = subject.QueryInterface(Ci.nsIHttpChannel);
  let url = channel.URI.spec;
  // Only listen for our document request, as many other requests can happen
  if (url == URL1 || url == URL2) {
    assertEvent("request", url);
  }
};
Services.obs.addObserver(httpObserver, "http-on-modify-request", false);

function onDOMContentLoaded() {
  assertEvent("DOMContentLoaded");
}
function onLoad() {
  assertEvent("load");
}

function getServerTabActor(callback) {
  // Ensure having a minimal server
  initDebuggerServer();

  // Connect to this tab
  let transport = DebuggerServer.connectPipe();
  client = new DebuggerClient(transport);
  connectDebuggerClient(client).then(form => {
    let actorID = form.actor;
    client.attachTab(actorID, function (aResponse, aTabClient) {
      // !Hack! Retrieve a server side object, the BrowserTabActor instance
      let tabActor = DebuggerServer._searchAllConnectionsForActor(actorID);
      callback(tabActor);
    });
  });

  client.addListener("tabNavigated", function (aEvent, aPacket) {
    assertEvent("tabNavigated", aPacket);
  });
}

function test() {
  // Open a test tab
  addTab(URL1).then(function (browser) {
    let doc = browser.contentDocument;
    getServerTabActor(function (tabActor) {
      // In order to listen to internal will-navigate/navigate events
      events.on(tabActor, "will-navigate", function (data) {
        assertEvent("will-navigate", data);
      });
      events.on(tabActor, "navigate", function (data) {
        assertEvent("navigate", data);
      });

      // Start listening for page load events
      browser.addEventListener("DOMContentLoaded", onDOMContentLoaded, true);
      browser.addEventListener("load", onLoad, true);

      // Listen for alert() call being made in navigate-first during unload
      waitForOnBeforeUnloadDialog(browser, function (btnLeave, btnStay) {
        assertEvent("unload-dialog");
        // accept to quit this page to another
        btnLeave.click();
      });

      // Load another document in this doc to dispatch these events
      assertEvent("load-new-document");
      content.location = URL2;
    });

  });
}

function cleanup() {
  let browser = gBrowser.selectedBrowser;
  browser.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
  browser.removeEventListener("load", onLoad);
  client.close().then(function () {
    Services.obs.addObserver(httpObserver, "http-on-modify-request", false);
    DebuggerServer.destroy();
    finish();
  });
}