Bug 1251268 - MozParam condition="pref" should use values from the default pref branch and URI encode them, r=adw.
authorFlorian Quèze <florian@queze.net>
Mon, 29 Feb 2016 11:45:33 +0100
changeset 324241 67ab004a728d94fc173a4a451c8c4e419555c2e9
parent 324240 008d7f4d10bf9694b2dc3bdd3568575e22cdbf6a
child 324242 7d72d3447ec40fef848203373a2adbc6cec89b64
push id1128
push userjlund@mozilla.com
push dateWed, 01 Jun 2016 01:31:59 +0000
treeherdermozilla-release@fe0d30de989d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1251268
milestone47.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 1251268 - MozParam condition="pref" should use values from the default pref branch and URI encode them, r=adw.
toolkit/components/search/nsSearchService.js
toolkit/components/search/tests/xpcshell/data/engine-pref.xml
toolkit/components/search/tests/xpcshell/data/list.txt
toolkit/components/search/tests/xpcshell/test_pref.js
toolkit/components/search/tests/xpcshell/xpcshell.ini
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -958,17 +958,18 @@ function sanitizeName(aName) {
 
 /**
  * Retrieve a pref from the search param branch.
  *
  * @param prefName
  *        The name of the pref.
  **/
 function getMozParamPref(prefName) {
-  return Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "param." + prefName);
+  let branch = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF + "param.");
+  return encodeURIComponent(branch.getCharPref(prefName));
 }
 
 /**
  * Notifies watchers of SEARCH_ENGINE_TOPIC about changes to an engine or to
  * the state of the search service.
  *
  * @param aEngine
  *        The nsISearchEngine object to which the change applies.
new file mode 100644
--- /dev/null
+++ b/toolkit/components/search/tests/xpcshell/data/engine-pref.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
+<ShortName>engine-pref</ShortName>
+<Url type="text/html" method="GET" template="http://www.google.com/search">
+  <Param name="q" value="{searchTerms}"/>
+  <!-- Dynamic parameters -->
+  <MozParam name="code" condition="pref" pref="code"/>
+</Url>
+</SearchPlugin>
--- a/toolkit/components/search/tests/xpcshell/data/list.txt
+++ b/toolkit/components/search/tests/xpcshell/data/list.txt
@@ -1,3 +1,4 @@
 engine
+engine-pref
 engine-rel-searchform-purpose
 engine-system-purpose
copy from toolkit/components/search/tests/xpcshell/test_purpose.js
copy to toolkit/components/search/tests/xpcshell/test_pref.js
--- a/toolkit/components/search/tests/xpcshell/test_purpose.js
+++ b/toolkit/components/search/tests/xpcshell/test_pref.js
@@ -1,69 +1,36 @@
 /* Any copyright is dedicated to the Public Domain.
- *    http://creativecommons.org/publicdomain/zero/1.0/ */
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-/*
- * Test that a search purpose can be specified and that query parameters for
- * that purpose are included in the search URL.
- */
+/* Test that MozParam condition="pref" values used in search URLs are from the
+ * default branch, and that their special characters are URL encoded. */
 
 "use strict";
 
 function run_test() {
-  removeMetadata();
   updateAppInfo();
 
   // The test engines used in this test need to be recognized as 'default'
-  // engines, or their MozParams used to set the purpose will be ignored.
+  // engines, or their MozParams will be ignored.
   let url = "resource://test/data/";
   let resProt = Services.io.getProtocolHandler("resource")
                         .QueryInterface(Ci.nsIResProtocolHandler);
   resProt.setSubstitution("search-plugins",
                           Services.io.newURI(url, null, null));
 
   run_next_test();
 }
 
-add_task(function* test_purpose() {
-  let engine = Services.search.getEngineByName("Test search engine");
-
-  function check_submission(aExpected, aSearchTerm, aType, aPurpose) {
-    do_check_eq(engine.getSubmission(aSearchTerm, aType, aPurpose).uri.spec,
-                base + aExpected);
-  }
-
-  let base = "http://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t";
-  check_submission("",              "foo");
-  check_submission("",              "foo", null);
-  check_submission("",              "foo", "text/html");
-  check_submission("&channel=rcs",  "foo", null,        "contextmenu");
-  check_submission("&channel=rcs",  "foo", "text/html", "contextmenu");
-  check_submission("&channel=fflb", "foo", null,        "keyword");
-  check_submission("&channel=fflb", "foo", "text/html", "keyword");
-  check_submission("",              "foo", "text/html", "invalid");
+add_task(function* test_pref() {
+  let defaultBranch = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF);
+  defaultBranch.setCharPref("param.code", "good&id=unique");
+  Services.prefs.setCharPref(BROWSER_SEARCH_PREF + "param.code", "bad");
 
-  // Tests for a param that varies with a purpose but has a default value.
-  base = "http://www.google.com/search?q=foo";
-  check_submission("&channel=none", "foo", "application/x-moz-default-purpose");
-  check_submission("&channel=none", "foo", "application/x-moz-default-purpose", null);
-  check_submission("&channel=none", "foo", "application/x-moz-default-purpose", "");
-  check_submission("&channel=rcs",  "foo", "application/x-moz-default-purpose", "contextmenu");
-  check_submission("&channel=fflb", "foo", "application/x-moz-default-purpose", "keyword");
-  check_submission("",              "foo", "application/x-moz-default-purpose", "invalid");
+  yield asyncInit();
 
-  // Tests for a purpose on the search form (ie. empty query).
-  engine = Services.search.getEngineByName("engine-rel-searchform-purpose");
-  base = "http://www.google.com/search?q=";
-  check_submission("&channel=sb", "", null,        "searchbar");
-  check_submission("&channel=sb", "", "text/html", "searchbar");
-
-  // verify that the 'system' purpose falls back to the 'searchbar' purpose.
-  base = "http://www.google.com/search?q=foo";
-  check_submission("&channel=sb", "foo", "text/html", "system");
-  check_submission("&channel=sb", "foo", "text/html", "searchbar");
-  // Use an engine that actually defines the 'system' purpose...
-  engine = Services.search.getEngineByName("engine-system-purpose");
-  // ... and check that the system purpose is used correctly.
-  check_submission("&channel=sys", "foo", "text/html", "system");
+  let engine = Services.search.getEngineByName("engine-pref");
+  let base = "http://www.google.com/search?q=foo&code=";
+  do_check_eq(engine.getSubmission("foo").uri.spec,
+              base + "good%26id%3Dunique");
 
   do_test_finished();
 });
--- a/toolkit/components/search/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/search/tests/xpcshell/xpcshell.ini
@@ -7,16 +7,17 @@ support-files =
   data/chrome.manifest
   data/engine.xml
   data/engine2.xml
   data/engine-addon.xml
   data/engine-override.xml
   data/engine-app.xml
   data/engine-fr.xml
   data/engineMaker.sjs
+  data/engine-pref.xml
   data/engine-rel-searchform.xml
   data/engine-rel-searchform-post.xml
   data/engine-rel-searchform-purpose.xml
   data/engine-system-purpose.xml
   data/engineImages.xml
   data/ico-size-16x16-png.ico
   data/invalid-engine.xml
   data/install.rdf
@@ -46,16 +47,17 @@ support-files =
 [test_location_partner.js]
 [test_location_funnelcake.js]
 [test_location_sync.js]
 [test_location_timeout.js]
 [test_location_timeout_xhr.js]
 [test_nodb.js]
 [test_nodb_pluschanges.js]
 [test_save_sorted_engines.js]
+[test_pref.js]
 [test_purpose.js]
 [test_defaultEngine.js]
 [test_notifications.js]
 [test_parseSubmissionURL.js]
 [test_SearchStaticData.js]
 [test_addEngine_callback.js]
 [test_multipleIcons.js]
 [test_resultDomain.js]