|
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/ */ |
|
4 |
|
5 // Tests that network log messages bring up the network panel. |
|
6 |
|
7 const TEST_NETWORK_REQUEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-network-request.html"; |
|
8 |
|
9 const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png"; |
|
10 |
|
11 const TEST_DATA_JSON_CONTENT = |
|
12 '{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }'; |
|
13 |
|
14 let lastRequest = null; |
|
15 let requestCallback = null; |
|
16 |
|
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 }); |
|
27 |
|
28 addTab("data:text/html;charset=utf-8,Web Console network logging tests"); |
|
29 |
|
30 browser.addEventListener("load", function onLoad() { |
|
31 browser.removeEventListener("load", onLoad, true); |
|
32 |
|
33 openConsole(null, function(aHud) { |
|
34 hud = aHud; |
|
35 |
|
36 HUDService.lastFinishedRequest.callback = function(aRequest) { |
|
37 lastRequest = aRequest; |
|
38 if (requestCallback) { |
|
39 requestCallback(); |
|
40 } |
|
41 }; |
|
42 |
|
43 executeSoon(testPageLoad); |
|
44 }); |
|
45 }, true); |
|
46 } |
|
47 |
|
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 }; |
|
60 |
|
61 content.location = TEST_NETWORK_REQUEST_URI; |
|
62 } |
|
63 |
|
64 function testPageLoadBody() |
|
65 { |
|
66 let loaded = false; |
|
67 let requestCallbackInvoked = false; |
|
68 |
|
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; |
|
75 |
|
76 if (loaded) { |
|
77 executeSoon(testXhrGet); |
|
78 } |
|
79 }; |
|
80 |
|
81 browser.addEventListener("load", function onLoad() { |
|
82 browser.removeEventListener("load", onLoad, true); |
|
83 loaded = true; |
|
84 |
|
85 if (requestCallbackInvoked) { |
|
86 executeSoon(testXhrGet); |
|
87 } |
|
88 }, true); |
|
89 |
|
90 content.location.reload(); |
|
91 } |
|
92 |
|
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 }; |
|
102 |
|
103 // Start the XMLHttpRequest() GET test. |
|
104 content.wrappedJSObject.testXhrGet(); |
|
105 } |
|
106 |
|
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 }; |
|
116 |
|
117 // Start the XMLHttpRequest() POST test. |
|
118 content.wrappedJSObject.testXhrPost(); |
|
119 } |
|
120 |
|
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"); |
|
128 |
|
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 }; |
|
148 |
|
149 let form = content.document.querySelector("form"); |
|
150 ok(form, "we have the HTML form"); |
|
151 form.submit(); |
|
152 } |
|
153 |
|
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\""); |
|
158 |
|
159 setStringFilter("HTTP"); |
|
160 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + |
|
161 "search string is set to \"HTTP\""); |
|
162 |
|
163 setStringFilter("hxxp"); |
|
164 is(countMessageNodes(), 0, "the log nodes are hidden when the search " + |
|
165 "string is set to \"hxxp\""); |
|
166 |
|
167 setStringFilter("ht tp"); |
|
168 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + |
|
169 "search string is set to \"ht tp\""); |
|
170 |
|
171 setStringFilter(""); |
|
172 isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + |
|
173 "search string is removed"); |
|
174 |
|
175 setStringFilter("json"); |
|
176 is(countMessageNodes(), 2, "the log nodes show only the nodes with \"json\""); |
|
177 |
|
178 setStringFilter("'foo'"); |
|
179 is(countMessageNodes(), 0, "the log nodes are hidden when searching for " + |
|
180 "the string 'foo'"); |
|
181 |
|
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'\""); |
|
185 |
|
186 HUDService.lastFinishedRequest.callback = null; |
|
187 lastRequest = null; |
|
188 requestCallback = null; |
|
189 finishTest(); |
|
190 } |
|
191 |
|
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 } |
|
201 |
|
202 return displayedMessageNodes; |
|
203 } |
|
204 |
|
205 function setStringFilter(aValue) |
|
206 { |
|
207 hud.ui.filterBox.value = aValue; |
|
208 hud.ui.adjustVisibilityOnSearchStringChange(); |
|
209 } |