Bug 1266783 - Initial patch at unit test draft
authorMichael Kaply <mozilla@kaply.com>
Fri, 22 Apr 2016 11:52:41 -0500
changeset 355448 96c4d4ca9b3bc75d282e34c4ed7b5eb5b456df5b
parent 355447 e086812341563534fb85964abda7effdcca09e1c
child 519197 a9401d9ab5f598b8ded0f1f005595ecbaa4ac9ec
push id16289
push usermozilla@kaply.com
push dateFri, 22 Apr 2016 16:53:09 +0000
bugs1266783
milestone48.0a1
Bug 1266783 - Initial patch at unit test MozReview-Commit-ID: IpVnoRvjE9n
browser/components/search/test/browser_google_codes.js
new file mode 100644
--- /dev/null
+++ b/browser/components/search/test/browser_google_codes.js
@@ -0,0 +1,138 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+var originalGeoURL;
+
+/**
+ * Clean the profile of any cache file left from a previous run.
+ * Returns a boolean indicating if the cache file existed.
+ */
+function removeCacheFile()
+{
+  const CACHE_FILENAME = "search.json.mozlz4";
+
+  let file =  Services.dirsvc.get("ProfD", Ci.nsIFile);
+  file.append(CACHE_FILENAME);
+  if (file.exists()) {
+    file.remove(false);
+    return true;
+  }
+  return false;
+}
+
+/**
+ * Returns a promise that is resolved when an observer notification from the
+ * search service fires with the specified data.
+ *
+ * @param aExpectedData
+ *        The value the observer notification sends that causes us to resolve
+ *        the promise.
+ */
+function waitForSearchNotification(aExpectedData) {
+  return new Promise(resolve => {
+    const SEARCH_SERVICE_TOPIC = "browser-search-service";
+    Services.obs.addObserver(function observer(aSubject, aTopic, aData) {
+      if (aData != aExpectedData)
+        return;
+
+      Services.obs.removeObserver(observer, SEARCH_SERVICE_TOPIC);
+      resolve(aSubject);
+    }, SEARCH_SERVICE_TOPIC, false);
+  });
+}
+
+function asyncReInit() {
+  const kLocalePref = "general.useragent.locale";
+
+  let promise = waitForSearchNotification("reinit-complete");
+
+  Services.search.QueryInterface(Ci.nsIObserver)
+          .observe(null, "nsPref:changed", kLocalePref);
+
+  return promise;
+}
+
+add_task(function* () {
+  const kUrlPref = "geoSpecificDefaults.url";
+  const BROWSER_SEARCH_PREF = "browser.search.";
+
+  let unInitPromise = waitForSearchNotification("uninit-complete");
+  let reInitPromise = asyncReInit();
+  yield unInitPromise;
+
+  // Verify search service is not initialized
+  is(Services.search.isInitialized, false, "Search service should NOT be initialized");
+
+  removeCacheFile();
+
+  // Geo specific defaults won't be fetched if there's no country code.
+  Services.prefs.setCharPref("browser.search.geoip.url",
+                             'data:application/json,{"country_code": "US"}');
+
+  Services.prefs.setBoolPref("browser.search.geoSpecificDefaults", true);
+                             
+  // Make the new Google the only engine
+  originalGeoURL = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + kUrlPref);
+  let geoUrl = 'data:application/json,{"interval": 0, "settings": {"searchDefault": "Google", "visibleDefaultEngines": ["google"]}}';
+  Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).setCharPref(kUrlPref, geoUrl);
+  
+  yield asyncReInit();
+});
+
+add_task(function* () {
+  let engines = Services.search.getEngines();
+  is(Services.search.currentEngine.name, "Google", "Search engine should be Google");
+  is(engines.length, 1, "There should only be one engine");
+  
+  let engine = Services.search.getEngineByName("Google");
+  ok(engine, "Google");
+
+  let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&client=firefox-b";
+
+  // Keyword uses a slightly different code
+  let keywordBase = base + "-ab";
+
+  let url;
+
+  // Test search URLs (including purposes).
+  url = engine.getSubmission("foo", null, "contextmenu").uri.spec;
+  is(url, base, "Check context menu search URL for 'foo'");
+  url = engine.getSubmission("foo", null, "keyword").uri.spec;
+  is(url, keywordBase, "Check keyword search URL for 'foo'");
+  url = engine.getSubmission("foo", null, "searchbar").uri.spec;
+  is(url, base, "Check search bar search URL for 'foo'");
+  url = engine.getSubmission("foo", null, "homepage").uri.spec;
+  is(url, base, "Check homepage search URL for 'foo'");
+  url = engine.getSubmission("foo", null, "newtab").uri.spec;
+  is(url, base, "Check newtab search URL for 'foo'");
+  url = engine.getSubmission("foo", null, "system").uri.spec;
+  is(url, base, "Check system search URL for 'foo'");
+});
+
+
+add_task(function* () {
+  const kUrlPref = "geoSpecificDefaults.url";
+  const BROWSER_SEARCH_PREF = "browser.search.";
+
+  let unInitPromise = waitForSearchNotification("uninit-complete");
+  let reInitPromise = asyncReInit();
+  yield unInitPromise;
+
+  // Verify search service is not initialized
+  is(Services.search.isInitialized, false, "Search service should NOT be initialized");
+  removeCacheFile();
+
+  // Geo specific defaults won't be fetched if there's no country code.
+  Services.prefs.clearUserPref("browser.search.geoip.url");
+
+  Services.prefs.clearUserPref("browser.search.geoSpecificDefaults");
+
+try {
+// We can't set the pref back to the original because it references search.services.mozilla.com
+// which causes the tests to fail.
+//  Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).setCharPref(kUrlPref, originalGeoURL);
+} catch (e) {
+}
+});