Bug 1449681 - Fix bug where opensearch API is not always successfully blocked by policy. r=Felipe, a=RyanVM
authorKirk Steuber <ksteuber@mozilla.com>
Wed, 28 Mar 2018 12:28:06 -0700
changeset 776129 b875912b97139513d8cf8319988cb08390abc2a3
parent 776128 d12219f7d74b2ec74df6b9df88fab3e700ca51c3
child 776130 a84862621c4cc3d7f36584f6bdfafbf2377ad53b
push id104821
push userbmo:rrosario@mozilla.com
push dateMon, 02 Apr 2018 18:45:53 +0000
reviewersFelipe, RyanVM
bugs1449681
milestone60.0
Bug 1449681 - Fix bug where opensearch API is not always successfully blocked by policy. r=Felipe, a=RyanVM MozReview-Commit-ID: 7ZrmiMop9IB
browser/components/enterprisepolicies/Policies.jsm
browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -348,16 +348,21 @@ var Policies = {
 
   "RememberPasswords": {
     onBeforeUIStartup(manager, param) {
       setAndLockPref("signon.rememberSignons", param);
     }
   },
 
   "SearchEngines": {
+    onBeforeUIStartup(manager, param) {
+      if (param.PreventInstalls) {
+        manager.disallowFeature("installSearchEngine", true);
+      }
+    },
     onAllWindowsRestored(manager, param) {
       Services.search.init(() => {
         if (param.Add) {
           // Only rerun if the list of engine names has changed.
           let engineNameList = param.Add.map(engine => engine.Name);
           runOncePerModification("addSearchEngines",
                                  JSON.stringify(engineNameList),
                                  () => {
@@ -397,19 +402,16 @@ var Policies = {
               try {
                 Services.search.currentEngine = defaultEngine;
               } catch (ex) {
                 log.error("Unable to set the default search engine", ex);
               }
             }
           });
         }
-        if (param.PreventInstalls) {
-          manager.disallowFeature("installSearchEngine");
-        }
       });
     }
   }
 };
 
 /*
  * ====================
  * = HELPER FUNCTIONS =
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_search_engine.js
@@ -67,19 +67,56 @@ add_task(async function test_install_and
 
   // If this passes, it means that the new search engine was properly installed
   // *and* was properly set as the default.
   is(Services.search.currentEngine.name, "MozSearch",
      "Specified search engine should be the default");
 
   // Clean up
   Services.search.removeEngine(Services.search.currentEngine);
+  EnterprisePolicyTesting.resetRunOnceState();
+});
+
+// Same as the last test, but with "PreventInstalls" set to true to make sure
+// it does not prevent search engines from being installed properly
+add_task(async function test_install_and_set_default_prevent_installs() {
+  isnot(Services.search.currentEngine.name, "MozSearch",
+        "Default search engine should not be MozSearch when test starts");
+  is(Services.search.getEngineByName("Foo"), null,
+     "Engine \"Foo\" should not be present when test starts");
+
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "SearchEngines": {
+        "Add": [
+          {
+            "Name": "MozSearch",
+            "URLTemplate": "http://example.com/?q={searchTerms}"
+          }
+        ],
+        "Default": "MozSearch",
+        "PreventInstalls": true
+      }
+    }
+  });
+
+  is(Services.search.currentEngine.name, "MozSearch",
+     "Specified search engine should be the default");
+
+  // Clean up
+  Services.search.removeEngine(Services.search.currentEngine);
+  EnterprisePolicyTesting.resetRunOnceState();
 });
 
 add_task(async function test_opensearch_works() {
+  // Clear out policies so we can test with no policies applied
+  await setupPolicyEngineWithJson({
+    "policies": {
+    }
+  });
   // Ensure that opensearch works before we make sure that it can be properly
   // disabled
   await test_opensearch(true);
 });
 
 add_task(async function setup_prevent_installs() {
   await setupPolicyEngineWithJson({
     "policies": {