michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Tests that network log messages bring up the network panel. michael@0: michael@0: const TEST_NETWORK_REQUEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-network-request.html"; michael@0: michael@0: const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png"; michael@0: michael@0: const TEST_DATA_JSON_CONTENT = michael@0: '{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }'; michael@0: michael@0: let lastRequest = null; michael@0: let requestCallback = null; michael@0: michael@0: function test() michael@0: { michael@0: const PREF = "devtools.webconsole.persistlog"; michael@0: let original = Services.prefs.getBoolPref("devtools.webconsole.filter.networkinfo"); michael@0: Services.prefs.setBoolPref("devtools.webconsole.filter.networkinfo", true); michael@0: Services.prefs.setBoolPref(PREF, true); michael@0: registerCleanupFunction(() => { michael@0: Services.prefs.setBoolPref("devtools.webconsole.filter.networkinfo", original); michael@0: Services.prefs.clearUserPref(PREF); michael@0: }); michael@0: michael@0: addTab("data:text/html;charset=utf-8,Web Console network logging tests"); michael@0: michael@0: browser.addEventListener("load", function onLoad() { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: michael@0: openConsole(null, function(aHud) { michael@0: hud = aHud; michael@0: michael@0: HUDService.lastFinishedRequest.callback = function(aRequest) { michael@0: lastRequest = aRequest; michael@0: if (requestCallback) { michael@0: requestCallback(); michael@0: } michael@0: }; michael@0: michael@0: executeSoon(testPageLoad); michael@0: }); michael@0: }, true); michael@0: } michael@0: michael@0: function testPageLoad() michael@0: { michael@0: requestCallback = function() { michael@0: // Check if page load was logged correctly. michael@0: ok(lastRequest, "Page load was logged"); michael@0: is(lastRequest.request.url, TEST_NETWORK_REQUEST_URI, michael@0: "Logged network entry is page load"); michael@0: is(lastRequest.request.method, "GET", "Method is correct"); michael@0: lastRequest = null; michael@0: requestCallback = null; michael@0: executeSoon(testPageLoadBody); michael@0: }; michael@0: michael@0: content.location = TEST_NETWORK_REQUEST_URI; michael@0: } michael@0: michael@0: function testPageLoadBody() michael@0: { michael@0: let loaded = false; michael@0: let requestCallbackInvoked = false; michael@0: michael@0: // Turn off logging of request bodies and check again. michael@0: requestCallback = function() { michael@0: ok(lastRequest, "Page load was logged again"); michael@0: lastRequest = null; michael@0: requestCallback = null; michael@0: requestCallbackInvoked = true; michael@0: michael@0: if (loaded) { michael@0: executeSoon(testXhrGet); michael@0: } michael@0: }; michael@0: michael@0: browser.addEventListener("load", function onLoad() { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: loaded = true; michael@0: michael@0: if (requestCallbackInvoked) { michael@0: executeSoon(testXhrGet); michael@0: } michael@0: }, true); michael@0: michael@0: content.location.reload(); michael@0: } michael@0: michael@0: function testXhrGet() michael@0: { michael@0: requestCallback = function() { michael@0: ok(lastRequest, "testXhrGet() was logged"); michael@0: is(lastRequest.request.method, "GET", "Method is correct"); michael@0: lastRequest = null; michael@0: requestCallback = null; michael@0: executeSoon(testXhrPost); michael@0: }; michael@0: michael@0: // Start the XMLHttpRequest() GET test. michael@0: content.wrappedJSObject.testXhrGet(); michael@0: } michael@0: michael@0: function testXhrPost() michael@0: { michael@0: requestCallback = function() { michael@0: ok(lastRequest, "testXhrPost() was logged"); michael@0: is(lastRequest.request.method, "POST", "Method is correct"); michael@0: lastRequest = null; michael@0: requestCallback = null; michael@0: executeSoon(testFormSubmission); michael@0: }; michael@0: michael@0: // Start the XMLHttpRequest() POST test. michael@0: content.wrappedJSObject.testXhrPost(); michael@0: } michael@0: michael@0: function testFormSubmission() michael@0: { michael@0: // Start the form submission test. As the form is submitted, the page is michael@0: // loaded again. Bind to the load event to catch when this is done. michael@0: requestCallback = function() { michael@0: ok(lastRequest, "testFormSubmission() was logged"); michael@0: is(lastRequest.request.method, "POST", "Method is correct"); michael@0: michael@0: // There should be 3 network requests pointing to the HTML file. michael@0: waitForMessages({ michael@0: webconsole: hud, michael@0: messages: [ michael@0: { michael@0: text: "test-network-request.html", michael@0: category: CATEGORY_NETWORK, michael@0: severity: SEVERITY_LOG, michael@0: count: 3, michael@0: }, michael@0: { michael@0: text: "test-data.json", michael@0: category: CATEGORY_NETWORK, michael@0: severity: SEVERITY_LOG, michael@0: count: 2, michael@0: }, michael@0: ], michael@0: }).then(testLiveFilteringOnSearchStrings); michael@0: }; michael@0: michael@0: let form = content.document.querySelector("form"); michael@0: ok(form, "we have the HTML form"); michael@0: form.submit(); michael@0: } michael@0: michael@0: function testLiveFilteringOnSearchStrings() { michael@0: setStringFilter("http"); michael@0: isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + michael@0: "search string is set to \"http\""); michael@0: michael@0: setStringFilter("HTTP"); michael@0: isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + michael@0: "search string is set to \"HTTP\""); michael@0: michael@0: setStringFilter("hxxp"); michael@0: is(countMessageNodes(), 0, "the log nodes are hidden when the search " + michael@0: "string is set to \"hxxp\""); michael@0: michael@0: setStringFilter("ht tp"); michael@0: isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + michael@0: "search string is set to \"ht tp\""); michael@0: michael@0: setStringFilter(""); michael@0: isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " + michael@0: "search string is removed"); michael@0: michael@0: setStringFilter("json"); michael@0: is(countMessageNodes(), 2, "the log nodes show only the nodes with \"json\""); michael@0: michael@0: setStringFilter("'foo'"); michael@0: is(countMessageNodes(), 0, "the log nodes are hidden when searching for " + michael@0: "the string 'foo'"); michael@0: michael@0: setStringFilter("foo\"bar'baz\"boo'"); michael@0: is(countMessageNodes(), 0, "the log nodes are hidden when searching for " + michael@0: "the string \"foo\"bar'baz\"boo'\""); michael@0: michael@0: HUDService.lastFinishedRequest.callback = null; michael@0: lastRequest = null; michael@0: requestCallback = null; michael@0: finishTest(); michael@0: } michael@0: michael@0: function countMessageNodes() { michael@0: let messageNodes = hud.outputNode.querySelectorAll(".message"); michael@0: let displayedMessageNodes = 0; michael@0: let view = hud.iframeWindow; michael@0: for (let i = 0; i < messageNodes.length; i++) { michael@0: let computedStyle = view.getComputedStyle(messageNodes[i], null); michael@0: if (computedStyle.display !== "none") michael@0: displayedMessageNodes++; michael@0: } michael@0: michael@0: return displayedMessageNodes; michael@0: } michael@0: michael@0: function setStringFilter(aValue) michael@0: { michael@0: hud.ui.filterBox.value = aValue; michael@0: hud.ui.adjustVisibilityOnSearchStringChange(); michael@0: }