Bug 1238516 - Migrate the defaultenginename preference. r=florian draft
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 18 Jan 2016 14:49:04 -0500
changeset 322460 6d15578b08c07437c15041ca0a5892c4b1716fd9
parent 322445 101c5632ebe7986b27f8d03f05ab3d6c8461d203
child 322464 db9b9cc2e4e10631a759dd3a55c7f385b29d7d0e
push id9621
push usermleibovic@mozilla.com
push dateMon, 18 Jan 2016 19:51:18 +0000
reviewersflorian
bugs1238516
milestone46.0a1
Bug 1238516 - Migrate the defaultenginename preference. r=florian
mobile/android/chrome/content/browser.js
mobile/android/tests/browser/chrome/chrome.ini
mobile/android/tests/browser/chrome/head_search.js
mobile/android/tests/browser/chrome/test_migrate_ui.html
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -1035,17 +1035,17 @@ var BrowserApp = {
     Services.obs.notifyObservers(null, "Passwords:Init", "");
 
     if (this._startupStatus === "upgrade") {
       this._migrateUI();
     }
   },
 
   _migrateUI: function() {
-    const UI_VERSION = 1;
+    const UI_VERSION = 2;
     let currentUIVersion = 0;
     try {
       currentUIVersion = Services.prefs.getIntPref("browser.migration.version");
     } catch(ex) {}
     if (currentUIVersion >= UI_VERSION) {
       return;
     }
 
@@ -1081,16 +1081,33 @@ var BrowserApp = {
 
       // We removed this pref from user visible settings, so we should reset it.
       // Power users can go into about:config to re-enable this if they choose.
       if (Services.prefs.prefHasUserValue("nglayout.debug.paint_flashing")) {
         Services.prefs.clearUserPref("nglayout.debug.paint_flashing");
       }
     }
 
+    if (currentUIVersion < 2) {
+      let name;
+      if (Services.prefs.prefHasUserValue("browser.search.defaultenginename")) {
+        name = Services.prefs.getCharPref("browser.search.defaultenginename");
+      }
+      if (!name && Services.prefs.prefHasUserValue("browser.search.defaultenginename.US")) {
+        name = Services.prefs.getCharPref("browser.search.defaultenginename.US");
+      }
+      let engine;
+      if (name) {
+        engine = Services.search.getEngineByName(name);
+      }
+      if (engine) {
+        Services.search.defaultEngine = engine;
+      }
+    }
+
     // Update the migration version.
     Services.prefs.setIntPref("browser.migration.version", UI_VERSION);
   },
 
   // This function returns false during periods where the browser displayed document is
   // different from the browser content document, so user actions and some kinds of viewport
   // updates should be ignored. This period starts when we start loading a new page or
   // switch tabs, and ends when the new browser content document has been drawn and handed
--- a/mobile/android/tests/browser/chrome/chrome.ini
+++ b/mobile/android/tests/browser/chrome/chrome.ini
@@ -1,15 +1,16 @@
 [DEFAULT]
 skip-if = os != 'android'
 support-files =
   basic_article.html
   desktopmode_user_agent.sjs
   devicesearch.xml
   head.js
+  head_search.js
   session_formdata_sample.html
   simpleservice.xml
   video_controls.html
   video_discovery.html
   web_channel.html
 
 [test_about_logins.html]
 [test_accounts.html]
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/chrome/head_search.js
@@ -0,0 +1,46 @@
+// 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");
+
+/**
+ * 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.
+ *          details: Array containing the parameters of addEngineWithDetails,
+ *                   except for the engine name.  Alternative to xmlFileName.
+ *        }
+ */
+var addTestEngines = Task.async(function* (aItems) {
+  let engines = [];
+
+  for (let item of aItems) {
+    yield new Promise((resolve, reject) => {
+      Services.obs.addObserver(function obs(subject, topic, data) {
+        try {
+          let engine = subject.QueryInterface(Ci.nsISearchEngine);
+          if (data != "engine-added" || engine.name != item.name) {
+            return;
+          }
+
+          Services.obs.removeObserver(obs, "browser-search-engine-modified");
+          engines.push(engine);
+          resolve();
+        } catch (ex) {
+          reject(ex);
+        }
+      }, "browser-search-engine-modified", false);
+
+      Services.search.addEngineWithDetails(item.name, ...item.details);
+    });
+  }
+
+  return engines;
+});
--- a/mobile/android/tests/browser/chrome/test_migrate_ui.html
+++ b/mobile/android/tests/browser/chrome/test_migrate_ui.html
@@ -5,31 +5,39 @@ https://bugzilla.mozilla.org/show_bug.cg
 Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
 -->
 <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">
 
-  Components.utils.import("resource://gre/modules/Services.jsm");
+    Services.prefs.clearUserPref("browser.migration.version");
+    Services.prefs.setBoolPref("nglayout.debug.paint_flashing", true);
 
-  Services.prefs.clearUserPref("browser.migration.version");
-  Services.prefs.setBoolPref("nglayout.debug.paint_flashing", true);
+    addTestEngines([
+      { name: "bacon", details: ["", "bacon", "Search Bacon", "GET",
+                                 "http://www.bacon.moz/?search={searchTerms}"] },
+    ]).then(engines => {
+      Services.prefs.setCharPref("browser.search.defaultenginename", engines[0].name);
 
-  let BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
-  BrowserApp._migrateUI();
+      let BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
+      BrowserApp._migrateUI();
+
+      // Check that migration version increased.
+      is(Services.prefs.getIntPref("browser.migration.version"), 2, "found expected version");
 
-  // Check that migration version increased.
-  is(Services.prefs.getIntPref("browser.migration.version"), 1, "found expected version");
+      // Check that user pref value was reset.
+      is(Services.prefs.prefHasUserValue("nglayout.debug.paint_flashing"), false, "found expected user value");
 
-  // Check that user pref value was reset.
-  is(Services.prefs.prefHasUserValue("nglayout.debug.paint_flashing"), false, "found expected user value");
+      is(Services.search.defaultEngine.name, engines[0].name, "found expected default search engine");
+    });
 
   </script>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1154504">Mozilla Bug 1154504</a>
 <br>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1184186">Migrated from Robocop testMigrateUI</a>
 <p id="display"></p>