Bug 1234287 - Refactor webconsole netlogging tests. r=bgrins
authorLin Clark <lclark@mozilla.com>
Fri, 22 Jan 2016 16:44:45 -0800
changeset 281379 65e26c4110070a05956f665a7b6ac0abbca7e10a
parent 281378 b7ef5a9212597310bd405b70975bf7e4e56499ea
child 281380 a26c5c9ab77dc3c9eb843b19e672c11ed305aeea
push id29935
push userphilringnalda@gmail.com
push dateSun, 24 Jan 2016 02:12:02 +0000
treeherdermozilla-central@a2e81822194a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1234287
milestone46.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1234287 - Refactor webconsole netlogging tests. r=bgrins
devtools/client/webconsole/test/browser.ini
devtools/client/webconsole/test/browser_console_netlogging.js
devtools/client/webconsole/test/browser_webconsole_basic_net_logging.js
devtools/client/webconsole/test/browser_webconsole_netlogging.js
devtools/client/webconsole/test/browser_webconsole_netlogging_basic.js
devtools/client/webconsole/test/browser_webconsole_netlogging_panel.js
devtools/client/webconsole/test/head.js
--- a/devtools/client/webconsole/test/browser.ini
+++ b/devtools/client/webconsole/test/browser.ini
@@ -202,17 +202,16 @@ skip-if = e10s && debug && os == 'win'
 skip-if = buildapp == 'mulet'
 [browser_result_format_as_string.js]
 [browser_warn_user_about_replaced_api.js]
 [browser_webconsole_abbreviate_source_url.js]
 [browser_webconsole_allow_mixedcontent_securityerrors.js]
 tags = mcb
 skip-if = buildapp == 'mulet'
 [browser_webconsole_assert.js]
-[browser_webconsole_basic_net_logging.js]
 [browser_webconsole_block_mixedcontent_securityerrors.js]
 tags = mcb
 skip-if = buildapp == 'mulet'
 [browser_webconsole_bug_579412_input_focus.js]
 [browser_webconsole_bug_580001_closing_after_completion.js]
 [browser_webconsole_bug_580030_errors_after_page_reload.js]
 [browser_webconsole_bug_580454_timestamp_l10n.js]
 [browser_webconsole_bug_582201_duplicate_errors.js]
@@ -334,16 +333,18 @@ skip-if = buildapp == 'mulet' || e10s # 
 [browser_webconsole_inspect-parsed-documents.js]
 [browser_webconsole_js_input_expansion.js]
 [browser_webconsole_jsterm.js]
 skip-if = e10s # Bug 1042253 - webconsole e10s tests (Linux debug timeout)
 [browser_webconsole_live_filtering_of_message_types.js]
 [browser_webconsole_live_filtering_on_search_strings.js]
 [browser_webconsole_message_node_id.js]
 [browser_webconsole_netlogging.js]
+[browser_webconsole_netlogging_basic.js]
+[browser_webconsole_netlogging_panel.js]
 [browser_webconsole_netlogging_reset_filter.js]
 [browser_webconsole_notifications.js]
 [browser_webconsole_open-links-without-callback.js]
 [browser_webconsole_promise.js]
 [browser_webconsole_output_copy_newlines.js]
 [browser_webconsole_output_order.js]
 [browser_webconsole_property_provider.js]
 skip-if = e10s # Bug 1042253 - webconsole tests disabled with e10s
--- a/devtools/client/webconsole/test/browser_console_netlogging.js
+++ b/devtools/client/webconsole/test/browser_console_netlogging.js
@@ -2,49 +2,34 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // Tests that network log messages bring up the network panel.
 
 "use strict";
 
-const TEST_URI = "data:text/html;charset=utf-8,Web Console network " +
-                 "logging tests";
-
 const TEST_NETWORK_REQUEST_URI =
   "http://example.com/browser/devtools/client/webconsole/test/" +
   "test-network-request.html";
 
-var hud;
+add_task(function* () {
+  let finishedRequest = waitForFinishedRequest();
+  const hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI,
+                                      "browserConsole");
+  let request = yield finishedRequest;
 
-function test() {
-  loadTab(TEST_URI).then((tab) => {
-    HUDService.openBrowserConsoleOrFocus().then(aHud => {
-      hud = aHud;
-      HUDService.lastFinishedRequest.callback = testResponse;
-      BrowserTestUtils.loadURI(gBrowser.selectedBrowser,
-                               TEST_NETWORK_REQUEST_URI);
-    });
-  });
-}
+  ok(request, "Page load was logged");
 
-function testResponse(request) {
-  hud.ui.webConsoleClient.getResponseContent(request.actor,
-    function(contentPacket) {
-      hud.ui.webConsoleClient.getRequestPostData(request.actor,
-        function(postDataPacket) {
-          // Check if page load was logged correctly.
-          ok(request, "Page load was logged");
+  let client = hud.ui.webConsoleClient;
+  let args = [request.actor];
+  const postData = yield getPacket(client, "getRequestPostData", args);
+  const responseContent = yield getPacket(client, "getResponseContent", args);
 
-          is(request.request.url, TEST_NETWORK_REQUEST_URI,
-            "Logged network entry is page load");
-          is(request.request.method, "GET", "Method is correct");
-          ok(!postDataPacket.postData.text, "No request body was stored");
-          ok(postDataPacket.postDataDiscarded, "Request body was discarded");
-          ok(!contentPacket.content.text, "No response body was stored");
-          ok(contentPacket.contentDiscarded || request.fromCache,
-             "Response body was discarded or response came from the cache");
-
-          executeSoon(finishTest);
-        });
-    });
-}
+  is(request.request.url, TEST_NETWORK_REQUEST_URI,
+    "Logged network entry is page load");
+  is(request.request.method, "GET", "Method is correct");
+  ok(!postData.postData.text, "No request body was stored");
+  ok(postData.postDataDiscarded, "Request body was discarded");
+  ok(!responseContent.content.text, "No response body was stored");
+  ok(responseContent.contentDiscarded || request.fromCache,
+     "Response body was discarded or response came from the cache");
+});
--- a/devtools/client/webconsole/test/browser_webconsole_netlogging.js
+++ b/devtools/client/webconsole/test/browser_webconsole_netlogging.js
@@ -1,171 +1,109 @@
 /* vim:set ts=2 sw=2 sts=2 et: */
-/* ***** BEGIN LICENSE BLOCK *****
- * Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/
- *
- * Contributor(s):
- *  Julian Viereck <jviereck@mozilla.com>
- *  Patrick Walton <pcwalton@mozilla.com>
- *  Mihai Șucan <mihai.sucan@gmail.com>
- *
- * ***** END LICENSE BLOCK ***** */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-// Tests that network log messages bring up the network panel.
+// Tests response logging for different request types.
 
 "use strict";
 
-const TEST_URI = "data:text/html;charset=utf-8,Web Console network " +
-                 "logging tests";
-
 const TEST_NETWORK_REQUEST_URI =
   "http://example.com/browser/devtools/client/webconsole/test/" +
   "test-network-request.html";
 
-const TEST_IMG = "http://example.com/browser/devtools/client/webconsole/" +
-                 "test/test-image.png";
-
 const TEST_DATA_JSON_CONTENT =
   '{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }';
 
-var lastRequest = null;
-var requestCallback = null;
-var browser, hud;
-
-function test() {
-  loadTab(TEST_URI).then((tab) => {
-    browser = tab.browser;
-
-    openConsole().then((aHud) => {
-      hud = aHud;
-
-      HUDService.lastFinishedRequest.callback = requestCallbackWrapper;
+add_task(function* testPageLoad() {
+  let finishedRequest = waitForFinishedRequest();
+  let hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI);
+  let request = yield finishedRequest;
 
-      executeSoon(testPageLoad);
-    });
-  });
-}
+  ok(request, "Page load was logged");
 
-function requestCallbackWrapper(request) {
-  lastRequest = request;
-
-  hud.ui.webConsoleClient.getResponseContent(lastRequest.actor,
-    function(aResponse) {
-      lastRequest.response.content = aResponse.content;
-      lastRequest.discardResponseBody = aResponse.contentDiscarded;
+  let client = hud.ui.webConsoleClient;
+  let args = [request.actor];
+  const postData = yield getPacket(client, "getRequestPostData", args);
+  const responseContent = yield getPacket(client, "getResponseContent", args);
 
-      hud.ui.webConsoleClient.getRequestPostData(lastRequest.actor,
-        function(response) {
-          lastRequest.request.postData = response.postData;
-          lastRequest.discardRequestBody = response.postDataDiscarded;
-
-          if (requestCallback) {
-            requestCallback();
-          }
-        });
-    });
-}
+  is(request.request.url, TEST_NETWORK_REQUEST_URI,
+    "Logged network entry is page load");
+  is(request.request.method, "GET", "Method is correct");
+  ok(!postData.postData.text, "No request body was stored");
+  ok(!postData.postDataDiscarded,
+    "Request body was not discarded");
+  is(responseContent.content.text.indexOf("<!DOCTYPE HTML>"), 0,
+    "Response body's beginning is okay");
+});
 
-function testPageLoad() {
-  requestCallback = function() {
-    // Check if page load was logged correctly.
-    ok(lastRequest, "Page load was logged");
+add_task(function* testXhrGet() {
+  let hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI);
 
-    is(lastRequest.request.url, TEST_NETWORK_REQUEST_URI,
-      "Logged network entry is page load");
-    is(lastRequest.request.method, "GET", "Method is correct");
-    ok(!lastRequest.request.postData.text, "No request body was stored");
-    ok(!lastRequest.discardRequestBody, "Request body was not discarded");
-    is(lastRequest.response.content.text.indexOf("<!DOCTYPE HTML>"), 0,
-      "Response body's beginning is okay");
+  let finishedRequest = waitForFinishedRequest();
+  content.wrappedJSObject.testXhrGet();
+  let request = yield finishedRequest;
 
-    lastRequest = null;
-    requestCallback = null;
-    executeSoon(testXhrGet);
-  };
+  ok(request, "testXhrGet() was logged");
 
-  content.location = TEST_NETWORK_REQUEST_URI;
-}
+  let client = hud.ui.webConsoleClient;
+  let args = [request.actor];
+  const postData = yield getPacket(client, "getRequestPostData", args);
+  const responseContent = yield getPacket(client, "getResponseContent", args);
 
-function testXhrGet() {
-  requestCallback = function() {
-    ok(lastRequest, "testXhrGet() was logged");
-    is(lastRequest.request.method, "GET", "Method is correct");
-    ok(!lastRequest.request.postData.text, "No request body was sent");
-    ok(!lastRequest.discardRequestBody, "Request body was not discarded");
-    is(lastRequest.response.content.text, TEST_DATA_JSON_CONTENT,
-      "Response is correct");
+  is(request.request.method, "GET", "Method is correct");
+  ok(!postData.postData.text, "No request body was sent");
+  ok(!postData.postDataDiscarded,
+    "Request body was not discarded");
+  is(responseContent.content.text, TEST_DATA_JSON_CONTENT,
+    "Response is correct");
+});
 
-    lastRequest = null;
-    requestCallback = null;
-    executeSoon(testXhrPost);
-  };
-
-  // Start the XMLHttpRequest() GET test.
-  content.wrappedJSObject.testXhrGet();
-}
+add_task(function* testXhrPost() {
+  let hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI);
 
-function testXhrPost() {
-  requestCallback = function() {
-    ok(lastRequest, "testXhrPost() was logged");
-    is(lastRequest.request.method, "POST", "Method is correct");
-    is(lastRequest.request.postData.text, "Hello world!",
-      "Request body was logged");
-    is(lastRequest.response.content.text, TEST_DATA_JSON_CONTENT,
-      "Response is correct");
+  let finishedRequest = waitForFinishedRequest();
+  content.wrappedJSObject.testXhrPost();
+  let request = yield finishedRequest;
 
-    lastRequest = null;
-    requestCallback = null;
-    executeSoon(testFormSubmission);
-  };
+  ok(request, "testXhrPost() was logged");
 
-  // Start the XMLHttpRequest() POST test.
-  content.wrappedJSObject.testXhrPost();
-}
+  let client = hud.ui.webConsoleClient;
+  let args = [request.actor];
+  const postData = yield getPacket(client, "getRequestPostData", args);
+  const responseContent = yield getPacket(client, "getResponseContent", args);
 
-function testFormSubmission() {
-  // Start the form submission test. As the form is submitted, the page is
-  // loaded again. Bind to the load event to catch when this is done.
-  requestCallback = function() {
-    ok(lastRequest, "testFormSubmission() was logged");
-    is(lastRequest.request.method, "POST", "Method is correct");
-    isnot(lastRequest.request.postData.text
-      .indexOf("Content-Type: application/x-www-form-urlencoded"), -1,
-      "Content-Type is correct");
-    isnot(lastRequest.request.postData.text
-      .indexOf("Content-Length: 20"), -1, "Content-length is correct");
-    isnot(lastRequest.request.postData.text
-      .indexOf("name=foo+bar&age=144"), -1, "Form data is correct");
-    is(lastRequest.response.content.text.indexOf("<!DOCTYPE HTML>"), 0,
-      "Response body's beginning is okay");
+  is(request.request.method, "POST", "Method is correct");
+  is(postData.postData.text, "Hello world!", "Request body was logged");
+  is(responseContent.content.text, TEST_DATA_JSON_CONTENT,
+    "Response is correct");
+});
 
-    executeSoon(testNetworkPanel);
-  };
+add_task(function* testFormSubmission() {
+  let hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI);
+
+  let finishedRequest = waitForFinishedRequest();
   ContentTask.spawn(gBrowser.selectedBrowser, { }, `function()
   {
     let form = content.document.querySelector("form");
     form.submit();
   }`);
-}
+  let request = yield finishedRequest;
+
+  ok(request, "testFormSubmission() was logged");
+
+  let client = hud.ui.webConsoleClient;
+  let args = [request.actor];
+  const postData = yield getPacket(client, "getRequestPostData", args);
+  const responseContent = yield getPacket(client, "getResponseContent", args);
 
-function testNetworkPanel() {
-  // Open the NetworkPanel. The functionality of the NetworkPanel is tested
-  // within separate test files.
-  hud.ui.openNetworkPanel(lastRequest.actor).then(() => {
-    let toolbox = gDevTools.getToolbox(hud.target);
-    is(toolbox.currentToolId, "netmonitor", "Network panel was opened");
-    let panel = toolbox.getCurrentPanel();
-    let selected = panel.panelWin.NetMonitorView.RequestsMenu.selectedItem;
-    is(selected.attachment.method, lastRequest.request.method,
-       "The correct request is selected");
-    is(selected.attachment.url, lastRequest.request.url,
-       "The correct request is definitely selected");
-
-    // All tests are done. Shutdown.
-    lastRequest = null;
-    HUDService.lastFinishedRequest.callback = null;
-    browser = requestCallback = hud = null;
-    executeSoon(finishTest);
-  }).then(null, error => {
-    ok(false, "Got an error: " + error.message + "\n" + error.stack);
-  });
-}
+  is(request.request.method, "POST", "Method is correct");
+  isnot(postData.postData.text
+    .indexOf("Content-Type: application/x-www-form-urlencoded"), -1,
+    "Content-Type is correct");
+  isnot(postData.postData.text
+    .indexOf("Content-Length: 20"), -1, "Content-length is correct");
+  isnot(postData.postData.text
+    .indexOf("name=foo+bar&age=144"), -1, "Form data is correct");
+  is(responseContent.content.text.indexOf("<!DOCTYPE HTML>"), 0,
+    "Response body's beginning is okay");
+});
rename from devtools/client/webconsole/test/browser_webconsole_basic_net_logging.js
rename to devtools/client/webconsole/test/browser_webconsole_netlogging_basic.js
--- a/devtools/client/webconsole/test/browser_webconsole_basic_net_logging.js
+++ b/devtools/client/webconsole/test/browser_webconsole_netlogging_basic.js
@@ -12,17 +12,17 @@ const TEST_NETWORK_URI = "http://example
                          "webconsole/test/test-network.html" + "?_date=" +
                          Date.now();
 
 add_task(function* () {
   yield loadTab("data:text/html;charset=utf-8,Web Console basic network " +
                 "logging test");
   let hud = yield openConsole();
 
-  content.location = TEST_NETWORK_URI;
+  yield BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TEST_NETWORK_URI);
 
   yield waitForMessages({
     webconsole: hud,
     messages: [{
       text: "running network console",
       category: CATEGORY_WEBDEV,
       severity: SEVERITY_LOG,
     },
new file mode 100644
--- /dev/null
+++ b/devtools/client/webconsole/test/browser_webconsole_netlogging_panel.js
@@ -0,0 +1,28 @@
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Tests that network log messages bring up the network panel.
+
+"use strict";
+
+const TEST_NETWORK_REQUEST_URI =
+  "http://example.com/browser/devtools/client/webconsole/test/" +
+  "test-network-request.html";
+
+add_task(function* () {
+  let finishedRequest = waitForFinishedRequest();
+  const hud = yield loadPageAndGetHud(TEST_NETWORK_REQUEST_URI);
+  let request = yield finishedRequest;
+
+  yield hud.ui.openNetworkPanel(request.actor);
+  let toolbox = gDevTools.getToolbox(hud.target);
+  is(toolbox.currentToolId, "netmonitor", "Network panel was opened");
+  let panel = toolbox.getCurrentPanel();
+  let selected = panel.panelWin.NetMonitorView.RequestsMenu.selectedItem;
+  is(selected.attachment.method, request.request.method,
+     "The correct request is selected");
+  is(selected.attachment.url, request.request.url,
+     "The correct request is definitely selected");
+});
--- a/devtools/client/webconsole/test/head.js
+++ b/devtools/client/webconsole/test/head.js
@@ -77,16 +77,55 @@ function closeTab(tab) {
     deferred.resolve(null);
   }, true);
 
   gBrowser.removeTab(tab);
 
   return deferred.promise;
 }
 
+/**
+ * Load the page and return the associated HUD.
+ *
+ * @param string uri
+ *   The URI of the page to load.
+ * @param string consoleType [optional]
+ *   The console type, either "browserConsole" or "webConsole". Defaults to
+ *   "webConsole".
+ * @return object
+ *   The HUD associated with the console
+ */
+function* loadPageAndGetHud(uri, consoleType) {
+  let { browser } = yield loadTab("data:text/html;charset=utf-8,Loading tab for tests");
+
+  let hud;
+  if (consoleType === "browserConsole") {
+    hud = yield HUDService.openBrowserConsoleOrFocus();
+  } else {
+    hud = yield openConsole();
+  }
+
+  ok(hud, "Console was opened");
+
+  let loaded = loadBrowser(browser);
+  yield BrowserTestUtils.loadURI(gBrowser.selectedBrowser, uri);
+  yield loaded;
+
+  yield waitForMessages({
+    webconsole: hud,
+    messages: [{
+      text: uri,
+      category: CATEGORY_NETWORK,
+      severity: SEVERITY_LOG,
+    }],
+  });
+
+  return hud;
+}
+
 function afterAllTabsLoaded(callback, win) {
   win = win || window;
 
   let stillToLoad = 0;
 
   function onLoad() {
     this.removeEventListener("load", onLoad, true);
     stillToLoad--;
@@ -1583,16 +1622,33 @@ function checkOutputForInputs(hud, input
          "opened tab '" + uri + "', expected tab '" + entry.expectedTab + "'");
       return closeTab(tab);
     }).then(resolve, reject);
   }
 
   return Task.spawn(runner);
 }
 
+
+/**
+ * Finish the request and resolve with the request object.
+ *
+ * @return promise
+ * @resolves The request object.
+ */
+function waitForFinishedRequest() {
+  registerCleanupFunction(function() {
+    HUDService.lastFinishedRequest.callback = null;
+  });
+
+  return new Promise(resolve => {
+    HUDService.lastFinishedRequest.callback = request => { resolve(request) };
+  });
+}
+
 /**
  * Wait for eventName on target.
  * @param {Object} target An observable object that either supports on/off or
  * addEventListener/removeEventListener
  * @param {String} eventName
  * @param {Boolean} useCapture Optional for addEventListener/removeEventListener
  * @return A promise that resolves when the event has been handled
  */
@@ -1649,16 +1705,31 @@ function checkLinkToInspector(hasLinkToI
 }
 
 function getSourceActor(sources, URL) {
   let item = sources.getItemForAttachment(a => a.source.url === URL);
   return item && item.value;
 }
 
 /**
+ * Make a request against an actor and resolve with the packet.
+ * @param object client
+ *   The client to use when making the request.
+ * @param function requestType
+ *   The client request function to run.
+ * @param array args
+ *   The arguments to pass into the function.
+ */
+function getPacket(client, requestType, args) {
+  return new Promise(resolve => {
+    client[requestType](...args, packet => resolve(packet));
+  });
+}
+
+/**
  * Verify that clicking on a link from a popup notification message tries to
  * open the expected URL.
  */
 function simulateMessageLinkClick(element, expectedLink) {
   let deferred = promise.defer();
 
   // Invoke the click event and check if a new tab would
   // open to the correct page.