Bug 890690: added unit test for POST search engines in about:home, r=gavin
authorMike de Boer <mdeboer@mozilla.com>
Thu, 25 Jul 2013 02:15:54 +0200
changeset 152260 0cf201bba70d20db6875faf5ef3462fb3a09909a
parent 152259 48772f78d69608031aef7144a4e3dc08553c5aa3
child 152261 156f5120a9f03747ebd91f9ade0b2e03288bb2b1
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs890690
milestone25.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 890690: added unit test for POST search engines in about:home, r=gavin
browser/base/content/test/browser_aboutHome.js
--- a/browser/base/content/test/browser_aboutHome.js
+++ b/browser/base/content/test/browser_aboutHome.js
@@ -237,28 +237,84 @@ let gTests = [
     }
     // Do a sanity check that all attributes are correctly set to begin with
     checkSearchUI(currEngine);
 
     let deferred = Promise.defer();
     promiseBrowserAttributes(gBrowser.selectedTab).then(function() {
       // Test if the update propagated
       checkSearchUI(unusedEngines[0]);
+      searchbar.currentEngine = currEngine;
       deferred.resolve();
     });
 
-    // The following cleanup function will set currentEngine back to the previous engine
+    // The following cleanup function will set currentEngine back to the previous
+    // engine if we fail to do so above.
     registerCleanupFunction(function() {
       searchbar.currentEngine = currEngine;
     });
     // Set the current search engine to an unused one
     searchbar.currentEngine = unusedEngines[0];
     searchbar.select();
     return deferred.promise;
   }
+},
+
+{
+  desc: "Check POST search engine support",
+  setup: function() {},
+  run: function()
+  {
+    let deferred = Promise.defer();
+    let currEngine = Services.search.defaultEngine;
+    let searchObserver = function search_observer(aSubject, aTopic, aData) {
+      let engine = aSubject.QueryInterface(Ci.nsISearchEngine);
+      info("Observer: " + aData + " for " + engine.name);
+
+      if (aData != "engine-added")
+        return;
+
+      if (engine.name != "POST Search")
+        return;
+
+      Services.search.defaultEngine = engine;
+
+      registerCleanupFunction(function() {
+        Services.search.removeEngine(engine);
+        Services.search.defaultEngine = currEngine;
+      });
+
+      let needle = "Search for something awesome.";
+
+      // Ready to execute the tests!
+      promiseBrowserAttributes(gBrowser.selectedTab).then(function() {
+        let document = gBrowser.selectedTab.linkedBrowser.contentDocument;
+        let searchText = document.getElementById("searchText");
+
+        waitForLoad(function() {
+          let loadedText = gBrowser.contentDocument.body.textContent;
+          ok(loadedText, "search page loaded");
+          is(loadedText, "searchterms=" + escape(needle.replace(/\s/g, "+")),
+             "Search text should arrive correctly");
+          deferred.resolve();
+        });
+
+        searchText.value = needle;
+        searchText.focus();
+        EventUtils.synthesizeKey("VK_RETURN", {});
+      });
+    };
+    Services.obs.addObserver(searchObserver, "browser-search-engine-modified", false);
+    registerCleanupFunction(function () {
+      Services.obs.removeObserver(searchObserver, "browser-search-engine-modified");
+    });
+    Services.search.addEngine("http://test:80/browser/browser/base/content/test/POSTSearchEngine.xml",
+                              Ci.nsISearchEngine.DATA_XML, null, false);
+    return deferred.promise;
+  }
 }
 
 ];
 
 function test()
 {
   waitForExplicitFinish();
   requestLongerTimeout(2);
@@ -437,8 +493,20 @@ function getNumberOfSearchesByDate(aEngi
 
     if (day.has(field)) {
       return day.get(field) || 0;
     }
   }
 
   return 0; // No records found.
 }
+
+function waitForLoad(cb) {
+  let browser = gBrowser.selectedBrowser;
+  browser.addEventListener("load", function listener() {
+    if (browser.currentURI.spec == "about:blank")
+      return;
+    info("Page loaded: " + browser.currentURI.spec);
+    browser.removeEventListener("load", listener, true);
+
+    cb();
+  }, true);
+}