Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* vim:set ts=2 sw=2 sts=2 et: */
2 /* Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/ */
5 // Tests that network log messages bring up the network panel.
7 const TEST_NETWORK_REQUEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-network-request.html";
9 const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png";
11 const TEST_DATA_JSON_CONTENT =
12 '{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }';
14 let lastRequest = null;
15 let requestCallback = null;
17 function test()
18 {
19 const PREF = "devtools.webconsole.persistlog";
20 let original = Services.prefs.getBoolPref("devtools.webconsole.filter.networkinfo");
21 Services.prefs.setBoolPref("devtools.webconsole.filter.networkinfo", true);
22 Services.prefs.setBoolPref(PREF, true);
23 registerCleanupFunction(() => {
24 Services.prefs.setBoolPref("devtools.webconsole.filter.networkinfo", original);
25 Services.prefs.clearUserPref(PREF);
26 });
28 addTab("data:text/html;charset=utf-8,Web Console network logging tests");
30 browser.addEventListener("load", function onLoad() {
31 browser.removeEventListener("load", onLoad, true);
33 openConsole(null, function(aHud) {
34 hud = aHud;
36 HUDService.lastFinishedRequest.callback = function(aRequest) {
37 lastRequest = aRequest;
38 if (requestCallback) {
39 requestCallback();
40 }
41 };
43 executeSoon(testPageLoad);
44 });
45 }, true);
46 }
48 function testPageLoad()
49 {
50 requestCallback = function() {
51 // Check if page load was logged correctly.
52 ok(lastRequest, "Page load was logged");
53 is(lastRequest.request.url, TEST_NETWORK_REQUEST_URI,
54 "Logged network entry is page load");
55 is(lastRequest.request.method, "GET", "Method is correct");
56 lastRequest = null;
57 requestCallback = null;
58 executeSoon(testPageLoadBody);
59 };
61 content.location = TEST_NETWORK_REQUEST_URI;
62 }
64 function testPageLoadBody()
65 {
66 let loaded = false;
67 let requestCallbackInvoked = false;
69 // Turn off logging of request bodies and check again.
70 requestCallback = function() {
71 ok(lastRequest, "Page load was logged again");
72 lastRequest = null;
73 requestCallback = null;
74 requestCallbackInvoked = true;
76 if (loaded) {
77 executeSoon(testXhrGet);
78 }
79 };
81 browser.addEventListener("load", function onLoad() {
82 browser.removeEventListener("load", onLoad, true);
83 loaded = true;
85 if (requestCallbackInvoked) {
86 executeSoon(testXhrGet);
87 }
88 }, true);
90 content.location.reload();
91 }
93 function testXhrGet()
94 {
95 requestCallback = function() {
96 ok(lastRequest, "testXhrGet() was logged");
97 is(lastRequest.request.method, "GET", "Method is correct");
98 lastRequest = null;
99 requestCallback = null;
100 executeSoon(testXhrPost);
101 };
103 // Start the XMLHttpRequest() GET test.
104 content.wrappedJSObject.testXhrGet();
105 }
107 function testXhrPost()
108 {
109 requestCallback = function() {
110 ok(lastRequest, "testXhrPost() was logged");
111 is(lastRequest.request.method, "POST", "Method is correct");
112 lastRequest = null;
113 requestCallback = null;
114 executeSoon(testFormSubmission);
115 };
117 // Start the XMLHttpRequest() POST test.
118 content.wrappedJSObject.testXhrPost();
119 }
121 function testFormSubmission()
122 {
123 // Start the form submission test. As the form is submitted, the page is
124 // loaded again. Bind to the load event to catch when this is done.
125 requestCallback = function() {
126 ok(lastRequest, "testFormSubmission() was logged");
127 is(lastRequest.request.method, "POST", "Method is correct");
129 // There should be 3 network requests pointing to the HTML file.
130 waitForMessages({
131 webconsole: hud,
132 messages: [
133 {
134 text: "test-network-request.html",
135 category: CATEGORY_NETWORK,
136 severity: SEVERITY_LOG,
137 count: 3,
138 },
139 {
140 text: "test-data.json",
141 category: CATEGORY_NETWORK,
142 severity: SEVERITY_LOG,
143 count: 2,
144 },
145 ],
146 }).then(testLiveFilteringOnSearchStrings);
147 };
149 let form = content.document.querySelector("form");
150 ok(form, "we have the HTML form");
151 form.submit();
152 }
154 function testLiveFilteringOnSearchStrings() {
155 setStringFilter("http");
156 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
157 "search string is set to \"http\"");
159 setStringFilter("HTTP");
160 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
161 "search string is set to \"HTTP\"");
163 setStringFilter("hxxp");
164 is(countMessageNodes(), 0, "the log nodes are hidden when the search " +
165 "string is set to \"hxxp\"");
167 setStringFilter("ht tp");
168 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
169 "search string is set to \"ht tp\"");
171 setStringFilter("");
172 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
173 "search string is removed");
175 setStringFilter("json");
176 is(countMessageNodes(), 2, "the log nodes show only the nodes with \"json\"");
178 setStringFilter("'foo'");
179 is(countMessageNodes(), 0, "the log nodes are hidden when searching for " +
180 "the string 'foo'");
182 setStringFilter("foo\"bar'baz\"boo'");
183 is(countMessageNodes(), 0, "the log nodes are hidden when searching for " +
184 "the string \"foo\"bar'baz\"boo'\"");
186 HUDService.lastFinishedRequest.callback = null;
187 lastRequest = null;
188 requestCallback = null;
189 finishTest();
190 }
192 function countMessageNodes() {
193 let messageNodes = hud.outputNode.querySelectorAll(".message");
194 let displayedMessageNodes = 0;
195 let view = hud.iframeWindow;
196 for (let i = 0; i < messageNodes.length; i++) {
197 let computedStyle = view.getComputedStyle(messageNodes[i], null);
198 if (computedStyle.display !== "none")
199 displayedMessageNodes++;
200 }
202 return displayedMessageNodes;
203 }
205 function setStringFilter(aValue)
206 {
207 hud.ui.filterBox.value = aValue;
208 hud.ui.adjustVisibilityOnSearchStringChange();
209 }