Bug 1073846 - Wrong URL loads after autofill of a hostname that matches a search engine. r=mak
authorBlair McBride <bmcbride@mozilla.com>
Tue, 30 Sep 2014 15:04:58 +1300
changeset 231863 250cb4595afb4256a3199bd9ade06010f620d03d
parent 231862 cb6a72e1fd95fb615ccb77c5cb1f4be3799cd3c4
child 231864 84204f7936028d2e78b670b001aa79d8eda922f0
child 231928 cd9cc5f3d9ed0c692263033d8b83b09685cf5ba1
push id4187
push userbhearsum@mozilla.com
push dateFri, 28 Nov 2014 15:29:12 +0000
treeherdermozilla-beta@f23cc6a30c11 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1073846
milestone35.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 1073846 - Wrong URL loads after autofill of a hostname that matches a search engine. r=mak
toolkit/components/places/UnifiedComplete.js
toolkit/components/places/tests/unifiedcomplete/data/engine-rel-searchform.xml
toolkit/components/places/tests/unifiedcomplete/test_searchEngine_host.js
toolkit/components/places/tests/unifiedcomplete/xpcshell.ini
--- a/toolkit/components/places/UnifiedComplete.js
+++ b/toolkit/components/places/UnifiedComplete.js
@@ -707,30 +707,31 @@ Search.prototype = {
       hasFirstResult = true;
     }
 
     if (this._enableActions && !hasFirstResult) {
       // If it's not a bookmarked keyword, then it may be a search engine
       // with an alias - which works like a keyword.
       hasFirstResult = yield this._matchSearchEngineAlias();
     }
+
     let shouldAutofill = this._shouldAutofill;
     if (this.pending && !hasFirstResult && shouldAutofill) {
-      // Or it may look like a URL we know about from search engines.
-      hasFirstResult = yield this._matchSearchEngineUrl();
-    }
-
-    if (this.pending && !hasFirstResult && shouldAutofill) {
       // It may also look like a URL we know from the database.
       // Here we can only try to predict whether the URL autofill query is
       // likely to return a result.  If the prediction ends up being wrong,
       // later we will need to make up for the lack of a special first result.
       hasFirstResult = yield this._matchKnownUrl(conn, queries);
     }
 
+    if (this.pending && !hasFirstResult && shouldAutofill) {
+      // Or it may look like a URL we know about from search engines.
+      hasFirstResult = yield this._matchSearchEngineUrl();
+    }
+
     if (this.pending && this._enableActions && !hasFirstResult) {
       // If we don't have a result that matches what we know about, then
       // we use a fallback for things we don't know about.
       yield this._matchHeuristicFallback();
     }
 
     // IMPORTANT: No other first result heuristics should run after
     // _matchHeuristicFallback().
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unifiedcomplete/data/engine-rel-searchform.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
+<ShortName>engine-rel-searchform.xml</ShortName>
+<Url type="text/html" method="GET" template="http://example.com/?search" rel="searchform"/>
+</SearchPlugin>
--- a/toolkit/components/places/tests/unifiedcomplete/test_searchEngine_host.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_searchEngine_host.js
@@ -1,28 +1,84 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
+Cu.import("resource://testing-common/httpd.js");
+
+function* addTestEngines(items) {
+  let httpServer = new HttpServer();
+  httpServer.start(-1);
+  httpServer.registerDirectory("/", do_get_cwd());
+  let gDataUrl = "http://localhost:" + httpServer.identity.primaryPort + "/data/";
+  do_register_cleanup(() => httpServer.stop(() => {}));
+
+  let engines = [];
+
+  for (let item of items) {
+    do_print("Adding engine: " + item);
+    yield new Promise(resolve => {
+      Services.obs.addObserver(function obs(subject, topic, data) {
+        let engine = subject.QueryInterface(Ci.nsISearchEngine);
+        do_print("Observed " + data + " for " + engine.name);
+        if (data != "engine-added" || engine.name != item) {
+          return;
+        }
+
+        Services.obs.removeObserver(obs, "browser-search-engine-modified");
+        engines.push(engine);
+        resolve();
+      }, "browser-search-engine-modified", false);
+
+      do_print("`Adding engine from URL: " + gDataUrl + item);
+      Services.search.addEngine(gDataUrl + item,
+                                Ci.nsISearchEngine.DATA_XML, null, false);
+    });
+  }
+
+  return engines;
+}
+
+
 add_task(function* test_searchEngine_autoFill() {
   Services.search.addEngineWithDetails("MySearchEngine", "", "", "",
                                        "GET", "http://my.search.com/");
   let engine = Services.search.getEngineByName("MySearchEngine");
   do_register_cleanup(() => Services.search.removeEngine(engine));
 
   // Add an uri that matches the search string with high frecency.
   let uri = NetUtil.newURI("http://www.example.com/my/");
   let visits = [];
   for (let i = 0; i < 100; ++i) {
     visits.push({ uri , title: "Terms - SearchEngine Search" });
   }
   yield promiseAddVisits(visits);
   addBookmark({ uri: uri, title: "Example bookmark" });
-  Assert.ok(frecencyForUrl(uri) > 10000);
+  ok(frecencyForUrl(uri) > 10000, "Adeded URI should have expected high frecency");
 
   do_log_info("Check search domain is autoFilled even if there's an higher frecency match");
   yield check_autocomplete({
     search: "my",
     autofilled: "my.search.com",
     completed: "http://my.search.com"
   });
 
   yield cleanup();
 });
+
+add_task(function* test_searchEngine_noautoFill() {
+  let engineName = "engine-rel-searchform.xml";
+  let [engine] = yield addTestEngines([engineName]);
+  do_register_cleanup(() => Services.search.removeEngine(engine));
+  equal(engine.searchForm, "http://example.com/?search");
+
+  Services.prefs.setBoolPref("browser.urlbar.autoFill.typed", false);
+  yield promiseAddVisits(NetUtil.newURI("http://example.com/my/"));
+
+  do_print("Check search domain is not autoFilled if it matches a visited domain");
+  yield check_autocomplete({
+    search: "example",
+    autofilled: "example.com/",
+    completed: "example.com/"
+  });
+
+  yield cleanup();
+});
+
--- a/toolkit/components/places/tests/unifiedcomplete/xpcshell.ini
+++ b/toolkit/components/places/tests/unifiedcomplete/xpcshell.ini
@@ -1,11 +1,14 @@
 [DEFAULT]
 head = head_autocomplete.js
 tail = 
+support-files =
+  data/engine-rel-searchform.xml
+
 
 [test_416211.js]
 [test_416214.js]
 [test_417798.js]
 [test_418257.js]
 [test_422277.js]
 [test_autocomplete_functional.js]
 [test_autocomplete_on_value_removed_479089.js]