Bug 1560424 - Add a dummy API for getting and setting the default engine for private browsing mode. r=daleharvey
authorMark Banner <standard8@mozilla.com>
Mon, 24 Jun 2019 14:42:58 +0000
changeset 542805 7877de0a1529873bca86c012608249e7bf4cae2d
parent 542804 49799d8989589e9efc34091979ab77a5d2ff53c1
child 542806 21826fb830de97ce71635ad784acf5a0b0bf237c
push id2131
push userffxbld-merge
push dateMon, 26 Aug 2019 18:30:20 +0000
treeherdermozilla-release@b19ffb3ca153 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdaleharvey
bugs1560424
milestone69.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 1560424 - Add a dummy API for getting and setting the default engine for private browsing mode. r=daleharvey Differential Revision: https://phabricator.services.mozilla.com/D35487
toolkit/components/search/SearchService.jsm
toolkit/components/search/nsISearchService.idl
toolkit/components/search/tests/xpcshell/test_defaultPrivateEngine.js
toolkit/components/search/tests/xpcshell/xpcshell.ini
--- a/toolkit/components/search/SearchService.jsm
+++ b/toolkit/components/search/SearchService.jsm
@@ -2243,16 +2243,34 @@ SearchService.prototype = {
     return this.defaultEngine;
   },
 
   async setDefault(engine) {
     await this.init(true);
     return this.defaultEngine = engine;
   },
 
+  get defaultPrivateEngine() {
+    return this.defaultEngine;
+  },
+
+  set defaultPrivateEngine(engine) {
+    return this.defaultEngine = engine;
+  },
+
+  async getDefaultPrivate() {
+    await this.init(true);
+    return this.defaultEngine;
+  },
+
+  async setDefaultPrivate(engine) {
+    await this.init(true);
+    return this.defaultEngine = engine;
+  },
+
   async getDefaultEngineInfo() {
     let result = {};
 
     let engine;
     try {
       engine = await this.getDefault();
     } catch (e) {
       // The defaultEngine getter will throw if there's no engine at all,
--- a/toolkit/components/search/nsISearchService.idl
+++ b/toolkit/components/search/nsISearchService.idl
@@ -295,17 +295,17 @@ interface nsISearchService : nsISupports
    *
    * @param name
    *        The search engine's name. Must be unique. Must not be null.
    *
    * @param  details
    *         An object that can contain the following fields:
    *
    *         {iconURL} Optional: A URL string pointing to the icon to be used to
-   *                   represent the engine. 
+   *                   represent the engine.
    *
    *         {alias} Optional: A unique shortcut that can be used to retrieve the
    *                 search engine.
    *
    *         {description} Optional: A description of the search engine.
    *
    *         {method} Optional: The HTTP request method used when submitting a
    *                  search query. Case insensitive value of either "get" or "post".
@@ -443,16 +443,25 @@ interface nsISearchService : nsISupports
    * default engine.
    */
   attribute nsISearchEngine defaultEngine;
 
   Promise getDefault();
   Promise setDefault(in nsISearchEngine engine);
 
   /**
+   * The currently active search engine for private browsing mode.
+   * @see defaultEngine.
+   */
+  attribute nsISearchEngine defaultPrivateEngine;
+
+  Promise getDefaultPrivate();
+  Promise setDefaultPrivate(in nsISearchEngine engine);
+
+  /**
    * Gets a representation of the default engine in an anonymized JSON
    * string suitable for recording in the Telemetry environment.
    *
    * @return an object containing anonymized info about the default engine:
    *         name, loadPath, submissionURL (for default engines).
    */
   Promise getDefaultEngineInfo();
 
new file mode 100644
--- /dev/null
+++ b/toolkit/components/search/tests/xpcshell/test_defaultPrivateEngine.js
@@ -0,0 +1,49 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Test that defaultEngine property can be set and yields the proper events and\
+ * behavior (search results)
+ */
+
+"use strict";
+
+add_task(async function setup() {
+  useHttpServer();
+  await AddonTestUtils.promiseStartupManager();
+});
+
+add_task(async function test_defaultPrivateEngine() {
+  let search = Services.search;
+  await search.init();
+
+  let originalDefault = search.defaultPrivateEngine;
+
+  let [engine1, engine2] = await addTestEngines([
+    { name: "Test search engine", xmlFileName: "engine.xml" },
+    { name: "A second test engine", xmlFileName: "engine2.xml" },
+  ]);
+
+  search.defaultPrivateEngine = engine1;
+  Assert.equal(search.defaultPrivateEngine, engine1);
+  search.defaultPrivateEngine = engine2;
+  Assert.equal(search.defaultPrivateEngine, engine2);
+  search.defaultPrivateEngine = engine1;
+  Assert.equal(search.defaultPrivateEngine, engine1);
+
+  // Test that hiding the currently-default engine affects the defaultEngine getter
+  // We fallback first to the original default...
+  engine1.hidden = true;
+  Assert.equal(search.defaultPrivateEngine, originalDefault);
+
+  // ... and then to the first visible engine in the list, so move our second
+  // engine to that position.
+  await search.moveEngine(engine2, 0);
+  originalDefault.hidden = true;
+  Assert.equal(search.defaultPrivateEngine, engine2);
+
+  // Test that setting defaultEngine to an already-hidden engine works, but
+  // doesn't change the return value of the getter
+  search.defaultPrivateEngine = engine1;
+  Assert.equal(search.defaultPrivateEngine, engine2);
+});
--- a/toolkit/components/search/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/search/tests/xpcshell/xpcshell.ini
@@ -73,16 +73,17 @@ support-files = data/search_ignorelist.j
 [test_location_error.js]
 [test_location_malformed_json.js]
 [test_location_timeout.js]
 [test_location_timeout_xhr.js]
 [test_nodb_pluschanges.js]
 [test_save_sorted_engines.js]
 [test_pref.js]
 [test_defaultEngine.js]
+[test_defaultPrivateEngine.js]
 [test_notifications.js]
 [test_parseSubmissionURL.js]
 [test_SearchStaticData.js]
 [test_addEngine_callback.js]
 [test_multipleIcons.js]
 [test_resultDomain.js]
 [test_searchSuggest.js]
 [test_searchSuggest_cookies.js]