Bug 1228319 - Search service tests draft
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 18 Jan 2016 16:00:25 -0500
changeset 322464 db9b9cc2e4e10631a759dd3a55c7f385b29d7d0e
parent 322460 6d15578b08c07437c15041ca0a5892c4b1716fd9
child 513122 14434f4988cb661dde07355927480efc14b678a4
push id9625
push usermleibovic@mozilla.com
push dateMon, 18 Jan 2016 21:00:44 +0000
bugs1228319
milestone46.0a1
Bug 1228319 - Search service tests
mobile/android/tests/browser/chrome/chrome.ini
mobile/android/tests/browser/chrome/head_search.js
mobile/android/tests/browser/chrome/test_search_service.html
--- a/mobile/android/tests/browser/chrome/chrome.ini
+++ b/mobile/android/tests/browser/chrome/chrome.ini
@@ -25,14 +25,15 @@ support-files =
 [test_java_addons.html]
 [test_jni.html]
 [test_migrate_ui.html]
 [test_network_manager.html]
 [test_offline_page.html]
 [test_reader_view.html]
 [test_resource_substitutions.html]
 [test_restricted_profiles.html]
+[test_search_service.html]
 [test_selectoraddtab.html]
 [test_session_form_data.html]
 [test_shared_preferences.html]
 [test_simple_discovery.html]
 [test_video_discovery.html]
 [test_web_channel.html]
--- a/mobile/android/tests/browser/chrome/head_search.js
+++ b/mobile/android/tests/browser/chrome/head_search.js
@@ -1,16 +1,52 @@
 // Bits and pieces copied from toolkit/components/search/tests/xpcshell/head_search.js
 
 var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 
 /**
+ * isUSTimezone taken from nsSearchService.js
+ */
+function isUSTimezone() {
+  // Timezone assumptions! We assume that if the system clock's timezone is
+  // between Newfoundland and Hawaii, that the user is in North America.
+
+  // This includes all of South America as well, but we have relatively few
+  // en-US users there, so that's OK.
+
+  // 150 minutes = 2.5 hours (UTC-2.5), which is
+  // Newfoundland Daylight Time (http://www.timeanddate.com/time/zones/ndt)
+
+  // 600 minutes = 10 hours (UTC-10), which is
+  // Hawaii-Aleutian Standard Time (http://www.timeanddate.com/time/zones/hast)
+
+  let UTCOffset = (new Date()).getTimezoneOffset();
+  return UTCOffset >= 150 && UTCOffset <= 600;
+}
+
+const kDefaultenginenamePref = "browser.search.defaultenginename";
+const kLocalePref = "general.useragent.locale";
+
+function getDefaultEngineName(isUS) {
+  const nsIPLS = Ci.nsIPrefLocalizedString;
+  // Copy the logic from nsSearchService
+  let pref = kDefaultenginenamePref;
+  if (isUS === undefined)
+    isUS = Services.prefs.getCharPref(kLocalePref) == "en-US" && isUSTimezone();
+  if (isUS) {
+    pref += ".US";
+  }
+  return Services.prefs.getComplexValue(pref, nsIPLS).data;
+}
+
+
+/**
  * Adds test engines and returns a promise resolved when they are installed.
  *
  * The engines are added in the given order.
  *
  * @param aItems
  *        Array of objects with the following properties:
  *        {
  *          name: Engine name, used to wait for it to be loaded.
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/chrome/test_search_service.html
@@ -0,0 +1,71 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1154504</title>
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="head_search.js"></script>
+  <script type="application/javascript;version=1.7">
+
+      is(true, false, "defaultName: " + getDefaultEngineName());
+      is(true, false, "Services.search.currentEngine.name: " + Services.search.currentEngine.name);
+      is(Services.search.currentEngine.name, getDefaultEngineName(), "current engine is default engine");
+
+      // Set current engine to a default engine that isn't the original default.
+      let builtInEngines = Services.search.getDefaultEngines();
+      let defaultName = getDefaultEngineName();
+      let nonDefaultBuiltInEngine;
+      for (let engine of builtInEngines) {
+        if (engine.name != defaultName) {
+          nonDefaultBuiltInEngine = engine;
+          break;
+        }
+      }
+      Services.search.currentEngine = nonDefaultBuiltInEngine;
+      is(Services.search.currentEngine.name, nonDefaultBuiltInEngine.name, "current engine is non-default built-in engine");
+
+      // Remove that engine...
+      Services.search.removeEngine(nonDefaultBuiltInEngine);
+      // The engine being a default (built-in) one, it should be hidden
+      // rather than actually removed.
+      is(nonDefaultBuiltInEngine.hidden, true, "non-default built-in engine is hidden");
+
+      // Using the currentEngine getter should force a fallback to the
+      // original default engine.
+      is(Services.search.currentEngine.name, defaultName, "current engine is original default engine");
+
+      // Restoring the default engines should unhide our built-in test
+      // engine, but not change the value of currentEngine.
+      Services.search.restoreDefaultEngines();
+      is(nonDefaultBuiltInEngine.hidden, false, "non-default built-in engine is not hidden");
+      is(Services.search.currentEngine.name, defaultName, "current engine is still original default engine");
+
+      addTestEngines([
+        { name: "bacon", details: ["", "bacon", "Search Bacon", "GET",
+                                   "http://www.bacon.moz/?search={searchTerms}"] },
+      ]).then(engines => {
+        // Set a new custom engine as the default engine
+        let newEngine = engines[0];
+        Services.search.defaultEngine = newEngine;
+        is(Services.search.currentEngine.name, newEngine.name, "current engine updated to new non-built-iin engine");
+
+        // Remove the new engine, check that old default is restored
+        Services.search.removeEngine(newEngine);
+        is(Services.search.currentEngine.name, getDefaultEngineName(), "current engine is once again original default engine");
+      });
+
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1228319">Mozilla Bug 1228319</a>
+<br>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+</pre>
+</body>
+</html>