Bug 1315514 - Bookmark keywords are still broken for POST forms. r=adw
authorMarco Bonardo <mbonardo@mozilla.com>
Mon, 07 Nov 2016 14:58:49 +0100
changeset 351605 7348925c1148ebc2f925c9a64867e2f91e2db883
parent 351604 e7aa936d8b6e3707a29665d9eb6623d21ff5deea
child 351606 e35ee6e46109004db4abf0ad805f5518036ca12b
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1315514
milestone52.0a1
Bug 1315514 - Bookmark keywords are still broken for POST forms. r=adw MozReview-Commit-ID: 3lrRbZVtgii
browser/base/content/test/general/browser_addKeywordSearch.js
browser/base/content/test/urlbar/browser.ini
browser/base/content/test/urlbar/browser_action_keyword.js
browser/base/content/test/urlbar/print_postdata.sjs
browser/base/content/urlbarBindings.xml
--- a/browser/base/content/test/general/browser_addKeywordSearch.js
+++ b/browser/base/content/test/general/browser_addKeywordSearch.js
@@ -1,82 +1,81 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/ */
-
 var testData = [
-  /* baseURI, field name, expected */
-  [ 'http://example.com/', 'q', 'http://example.com/?q=%s' ],
-  [ 'http://example.com/new-path-here/', 'q', 'http://example.com/new-path-here/?q=%s' ],
-  [ '', 'q', 'http://example.org/browser/browser/base/content/test/general/dummy_page.html?q=%s' ],
-  // Tests for proper behaviour when called on a form whose action contains a question mark.
-  [ 'http://example.com/search?oe=utf-8', 'q', 'http://example.com/search?oe=utf-8&q=%s' ],
+  { desc: "No path",
+    action: "http://example.com/",
+    param: "q",
+  },
+  { desc: "With path",
+    action: "http://example.com/new-path-here/",
+    param: "q",
+  },
+  { desc: "No action",
+    action: "",
+    param: "q",
+  },
+  { desc: "With Query String",
+    action: "http://example.com/search?oe=utf-8",
+    param: "q",
+  },
 ];
 
 add_task(function*() {
-  yield BrowserTestUtils.withNewTab({
-    gBrowser,
-    url: "http://example.org/browser/browser/base/content/test/general/dummy_page.html",
-  }, function* (browser) {
-    yield ContentTask.spawn(browser, null, function* () {
-      let doc = content.document;
-      let base = doc.createElement("base");
-      doc.head.appendChild(base);
-    });
-
-    var mm = browser.messageManager;
+  const TEST_URL = "http://example.org/browser/browser/base/content/test/general/dummy_page.html";
+  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
 
-    for (let [baseURI, fieldName, expected] of testData) {
-      let popupShownPromise = BrowserTestUtils.waitForEvent(document.getElementById("contentAreaContextMenu"),
-                                                            "popupshown");
+  let count = 0;
+  for (let method of ["GET", "POST"]) {
+    for (let {desc, action, param } of testData) {
+      info(`Running ${method} keyword test '${desc}'`);
+      let id = `keyword-form-${count++}`;
+      let contextMenu = document.getElementById("contentAreaContextMenu");
+      let contextMenuPromise =
+        BrowserTestUtils.waitForEvent(contextMenu, "popupshown")
+                        .then(() => gContextMenuContentData.popupNode);
 
-      yield ContentTask.spawn(browser, { baseURI, fieldName }, function* (args) {
+      yield ContentTask.spawn(tab.linkedBrowser,
+                              { action, param, method, id }, function* (args) {
         let doc = content.document;
-
-        let base = doc.querySelector('head > base');
-        base.href = args.baseURI;
-
         let form = doc.createElement("form");
-        form.id = "keyword-form";
+        form.id = args.id;
+        form.method = args.method;
+        form.action = args.action;
         let element = doc.createElement("input");
         element.setAttribute("type", "text");
-        element.setAttribute("name", args.fieldName);
+        element.setAttribute("name", args.param);
         form.appendChild(element);
         doc.body.appendChild(form);
-
-        /* Open context menu so chrome can access the element */
-        const domWindowUtils =
-          content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
-                 .getInterface(Components.interfaces.nsIDOMWindowUtils);
-        let rect = element.getBoundingClientRect();
-        let left = rect.left + rect.width / 2;
-        let top = rect.top + rect.height / 2;
-        domWindowUtils.sendMouseEvent("contextmenu", left, top, 2,
-                                      1, 0, false, 0, 0, true);
       });
 
-      yield popupShownPromise;
+      yield BrowserTestUtils.synthesizeMouseAtCenter(`#${id} > input`,
+                                                     { type : "contextmenu", button : 2 },
+                                                     tab.linkedBrowser);
+      let target = yield contextMenuPromise;
 
-      let target = gContextMenuContentData.popupNode;
-
-      let urlCheck = new Promise((resolve, reject) => {
+      yield new Promise(resolve => {
+        let url = action || tab.linkedBrowser.currentURI.spec;
+        let mm = tab.linkedBrowser.messageManager;
         let onMessage = (message) => {
           mm.removeMessageListener("ContextMenu:SearchFieldBookmarkData:Result", onMessage);
-
-          is(message.data.spec, expected,
-             `Bookmark spec for search field named ${fieldName} and baseURI ${baseURI} incorrect`);
+          if (method == "GET") {
+            ok(message.data.spec.endsWith(`${param}=%s`),
+             `Check expected url for field named ${param} and action ${action}`);
+          } else {
+            is(message.data.spec, url,
+             `Check expected url for field named ${param} and action ${action}`);
+            is(message.data.postData, `${param}%3D%25s`,
+             `Check expected POST data for field named ${param} and action ${action}`);
+          }
           resolve();
         };
         mm.addMessageListener("ContextMenu:SearchFieldBookmarkData:Result", onMessage);
 
         mm.sendAsyncMessage("ContextMenu:SearchFieldBookmarkData", null, { target });
       });
 
-      yield urlCheck;
-
-      document.getElementById("contentAreaContextMenu").hidePopup();
+      let popupHiddenPromise = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden");
+      contextMenu.hidePopup();
+      yield popupHiddenPromise;
+    }
+  }
 
-      yield ContentTask.spawn(browser, null, function* () {
-        let doc = content.document;
-        doc.body.removeChild(doc.getElementById("keyword-form"));
-      });
-    }
-  });
+  yield BrowserTestUtils.removeTab(tab);
 });
--- a/browser/base/content/test/urlbar/browser.ini
+++ b/browser/base/content/test/urlbar/browser.ini
@@ -2,16 +2,18 @@
 support-files =
   dummy_page.html
   head.js
 
 [browser_URLBarSetURI.js]
 skip-if = (os == "linux" || os == "mac") && debug # bug 970052, bug 970053
 [browser_action_keyword.js]
 skip-if = os == "linux" # Bug 1188154
+support-files =
+  print_postdata.sjs
 [browser_action_keyword_override.js]
 [browser_action_searchengine.js]
 [browser_action_searchengine_alias.js]
 [browser_autocomplete_a11y_label.js]
 [browser_autocomplete_autoselect.js]
 [browser_autocomplete_cursor.js]
 [browser_autocomplete_edit_completed.js]
 [browser_autocomplete_enter_race.js]
--- a/browser/base/content/test/urlbar/browser_action_keyword.js
+++ b/browser/base/content/test/urlbar/browser_action_keyword.js
@@ -1,81 +1,119 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/ */
-
-var gOnSearchComplete = null;
-
 function* promise_first_result(inputText) {
   yield promiseAutocompleteResultPopup(inputText);
 
   let firstResult = gURLBar.popup.richlistbox.firstChild;
   return firstResult;
 }
 
-
-add_task(function*() {
-  let tab = gBrowser.selectedTab = gBrowser.addTab("about:mozilla");
-  let tabs = [tab];
-  registerCleanupFunction(function* () {
-    for (let tab of tabs)
-      gBrowser.removeTab(tab);
-    yield PlacesUtils.bookmarks.remove(bm);
-  });
+const TEST_URL = "http://mochi.test:8888/browser/browser/base/content/test/urlbar/print_postdata.sjs";
 
-  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+add_task(function* setup() {
+  yield PlacesUtils.keywords.insert({ keyword: "get",
+                                      url: TEST_URL + "?q=%s" });
+  yield PlacesUtils.keywords.insert({ keyword: "post",
+                                      url: TEST_URL,
+                                      postData: "q=%s" });
+  registerCleanupFunction(function* () {
+    yield PlacesUtils.keywords.remove("get");
+    yield PlacesUtils.keywords.remove("post");
+    while (gBrowser.tabs.length > 1) {
+      yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
+    }
+  });
+});
 
-  let bm = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
-                                                url: "http://example.com/?q=%s",
-                                                title: "test" });
-  yield PlacesUtils.keywords.insert({ keyword: "keyword",
-                                      url: "http://example.com/?q=%s" });
+add_task(function* get_keyword() {
+  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:mozilla");
 
-  let result = yield promise_first_result("keyword something");
+  let result = yield promise_first_result("get something");
   isnot(result, null, "Expect a keyword result");
 
   let types = new Set(result.getAttribute("type").split(/\s+/));
   Assert.ok(types.has("keyword"));
   is(result.getAttribute("actiontype"), "keyword", "Expect correct `actiontype` attribute");
-  is(result.getAttribute("title"), "example.com", "Expect correct title");
+  is(result.getAttribute("title"), "mochi.test:8888", "Expect correct title");
 
   // We need to make a real URI out of this to ensure it's normalised for
   // comparison.
   let uri = NetUtil.newURI(result.getAttribute("url"));
-  is(uri.spec, PlacesUtils.mozActionURI("keyword", {url: "http://example.com/?q=something", input: "keyword something"}), "Expect correct url");
+  is(uri.spec, PlacesUtils.mozActionURI("keyword",
+                                        { url: TEST_URL + "?q=something",
+                                          input: "get something"}),
+     "Expect correct url");
 
   let titleHbox = result._titleText.parentNode.parentNode;
   ok(titleHbox.classList.contains("ac-title"), "Title hbox element sanity check");
   is_element_visible(titleHbox, "Title element should be visible");
-  is(result._titleText.textContent, "example.com: something", "Node should contain the name of the bookmark and query");
+  is(result._titleText.textContent, "mochi.test:8888: something",
+     "Node should contain the name of the bookmark and query");
 
   let urlHbox = result._urlText.parentNode.parentNode;
   ok(urlHbox.classList.contains("ac-url"), "URL hbox element sanity check");
   is_element_hidden(urlHbox, "URL element should be hidden");
 
   let actionHbox = result._actionText.parentNode.parentNode;
   ok(actionHbox.classList.contains("ac-action"), "Action hbox element sanity check");
   is_element_visible(actionHbox, "Action element should be visible");
   is(result._actionText.textContent, "", "Action text should be empty");
 
   // Click on the result
   info("Normal click on result");
   let tabPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
   EventUtils.synthesizeMouseAtCenter(result, {});
   yield tabPromise;
-  is(tab.linkedBrowser.currentURI.spec, "http://example.com/?q=something", "Tab should have loaded from clicking on result");
+  is(tab.linkedBrowser.currentURI.spec, TEST_URL + "?q=something",
+     "Tab should have loaded from clicking on result");
 
   // Middle-click on the result
   info("Middle-click on result");
-  result = yield promise_first_result("keyword somethingmore");
+  result = yield promise_first_result("get somethingmore");
   isnot(result, null, "Expect a keyword result");
   // We need to make a real URI out of this to ensure it's normalised for
   // comparison.
   uri = NetUtil.newURI(result.getAttribute("url"));
-  is(uri.spec, PlacesUtils.mozActionURI("keyword", {url: "http://example.com/?q=somethingmore", input: "keyword somethingmore"}), "Expect correct url");
+  is(uri.spec, PlacesUtils.mozActionURI("keyword",
+                                        { url: TEST_URL + "?q=somethingmore",
+                                          input: "get somethingmore" }),
+     "Expect correct url");
 
   tabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
   EventUtils.synthesizeMouseAtCenter(result, {button: 1});
   let tabOpenEvent = yield tabPromise;
   let newTab = tabOpenEvent.target;
-  tabs.push(newTab);
   yield BrowserTestUtils.browserLoaded(newTab.linkedBrowser);
-  is(newTab.linkedBrowser.currentURI.spec, "http://example.com/?q=somethingmore", "Tab should have loaded from middle-clicking on result");
+  is(newTab.linkedBrowser.currentURI.spec,
+     TEST_URL + "?q=somethingmore",
+     "Tab should have loaded from middle-clicking on result");
 });
+
+
+add_task(function* post_keyword() {
+  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "about:mozilla");
+
+  let result = yield promise_first_result("post something");
+  isnot(result, null, "Expect a keyword result");
+
+  let types = new Set(result.getAttribute("type").split(/\s+/));
+  Assert.ok(types.has("keyword"));
+  is(result.getAttribute("actiontype"), "keyword", "Expect correct `actiontype` attribute");
+  is(result.getAttribute("title"), "mochi.test:8888", "Expect correct title");
+
+  is(result.getAttribute("url"),
+     PlacesUtils.mozActionURI("keyword", { url: TEST_URL,
+                                           input: "post something",
+                                           "postData": "q=something" }),
+     "Expect correct url");
+
+  // Click on the result
+  info("Normal click on result");
+  let tabPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+  EventUtils.synthesizeMouseAtCenter(result, {});
+  yield tabPromise;
+  is(tab.linkedBrowser.currentURI.spec, TEST_URL,
+     "Tab should have loaded from clicking on result");
+
+  let postData = yield ContentTask.spawn(tab.linkedBrowser, null, function* () {
+    return content.document.body.textContent;
+  });
+  is(postData, "q=something", "post data was submitted correctly");
+});
copy from browser/base/content/test/general/print_postdata.sjs
copy to browser/base/content/test/urlbar/print_postdata.sjs
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -439,17 +439,17 @@ file, You can obtain one at http://mozil
                 }
                 url = action.params.url;
                 break;
               case "remotetab":
                 url = action.params.url;
                 break;
               case "keyword":
                 if (action.params.postData) {
-                  postData = getPostDataStream(postData);
+                  postData = getPostDataStream(action.params.postData);
                 }
                 mayInheritPrincipal = true;
                 url = action.params.url;
                 break;
               case "switchtab":
                 url = action.params.url;
                 if (this.hasAttribute("actiontype")) {
                   this.handleRevert();