Commit 86c65085 authored by Butkovits Atila's avatar Butkovits Atila
Browse files

Backed out 6 changesets (bug 1445134) for causing xpcshell failures. CLOSED TREE

Backed out changeset 59f902b761b5 (bug 1445134)
Backed out changeset 2ec41e23c593 (bug 1445134)
Backed out changeset b0dc583fff47 (bug 1445134)
Backed out changeset a09ccaf19501 (bug 1445134)
Backed out changeset 2d736481d13d (bug 1445134)
Backed out changeset 57ec56757493 (bug 1445134)
parent 708068a7
Loading
Loading
Loading
Loading
+69 −4
Original line number Diff line number Diff line
@@ -2007,10 +2007,6 @@ export var Policies = {
    },
    onAllWindowsRestored(manager, param) {
      Services.search.init().then(async () => {
        // Adding of engines is handled by the SearchService in the init().
        // Remove can happen after those are added - no engines are allowed
        // to replace the application provided engines, even if they have been
        // removed.
        if (param.Remove) {
          // Only rerun if the list of engine names has changed.
          await runOncePerModification(
@@ -2030,6 +2026,46 @@ export var Policies = {
            }
          );
        }
        if (param.Add) {
          // Rerun if any engine info has changed.
          let engineInfoHash = md5Hash(JSON.stringify(param.Add));
          await runOncePerModification(
            "addSearchEngines",
            engineInfoHash,
            async function() {
              for (let newEngine of param.Add) {
                let manifest = {
                  description: newEngine.Description,
                  iconURL: newEngine.IconURL ? newEngine.IconURL.href : null,
                  name: newEngine.Name,
                  // If the encoding is not specified or is falsy, the
                  // search service will fall back to the default encoding.
                  encoding: newEngine.Encoding,
                  search_url: encodeURI(newEngine.URLTemplate),
                  keyword: newEngine.Alias,
                  search_url_post_params:
                    newEngine.Method == "POST" ? newEngine.PostData : undefined,
                  suggest_url: newEngine.SuggestURLTemplate,
                };

                let engine = Services.search.getEngineByName(newEngine.Name);
                if (engine) {
                  try {
                    await Services.search.updatePolicyEngine(manifest);
                  } catch (ex) {
                    lazy.log.error("Unable to update the search engine", ex);
                  }
                } else {
                  try {
                    await Services.search.addPolicyEngine(manifest);
                  } catch (ex) {
                    lazy.log.error("Unable to add search engine", ex);
                  }
                }
              }
            }
          );
        }
        if (param.Default) {
          await runOncePerModification(
            "setDefaultSearchEngine",
@@ -2764,3 +2800,32 @@ function processMIMEInfo(mimeInfo, realMIMEInfo) {
  }
  lazy.gHandlerService.store(realMIMEInfo);
}

// Copied from PlacesUIUtils.jsm

// Keep a hasher for repeated hashings
let gCryptoHash = null;

/**
 * Run some text through md5 and return the base64 result.
 * @param {string} data The string to hash.
 * @returns {string} md5 hash of the input string.
 */
function md5Hash(data) {
  // Lazily create a reusable hasher
  if (gCryptoHash === null) {
    gCryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(
      Ci.nsICryptoHash
    );
  }

  gCryptoHash.init(gCryptoHash.MD5);

  // Convert the data to a byte array for hashing
  gCryptoHash.update(
    data.split("").map(c => c.charCodeAt(0)),
    data.length
  );
  // Request the has result as ASCII base64
  return gCryptoHash.finish(true);
}
+498 −0
Original line number Diff line number Diff line
@@ -2,9 +2,15 @@
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  SearchTestUtils: "resource://testing-common/SearchTestUtils.sys.mjs",
  SearchUtils: "resource://gre/modules/SearchUtils.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
  CustomizableUITestUtils:
    "resource://testing-common/CustomizableUITestUtils.jsm",
  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.jsm",
});

let gCUITestUtils = new CustomizableUITestUtils(window);
@@ -59,6 +65,217 @@ async function test_opensearch(shouldWork) {
  await BrowserTestUtils.removeTab(tab);
}

add_task(async function test_install_and_set_default() {
  Services.telemetry.clearEvents();
  Services.fog.testResetFOG();

  // Make sure we are starting in an expected state to avoid false positive
  // test results.
  let prevEngine = await Services.search.getDefault();
  isnot(
    prevEngine.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",
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  // If this passes, it means that the new search engine was properly installed
  // *and* was properly set as the default.
  is(
    (await Services.search.getDefault()).name,
    "MozSearch",
    "Specified search engine should be the default"
  );
  TelemetryTestUtils.assertEvents(
    [
      {
        object: "change_default",
        value: "enterprise",
        extra: {
          prev_id: prevEngine.telemetryId,
          new_id: "other-MozSearch",
          new_name: "MozSearch",
          new_load_path: "[other]addEngineWithDetails:set-via-policy",
          new_sub_url: "",
        },
      },
    ],
    { category: "search", method: "engine" }
  );

  let snapshot = await Glean.searchEngineDefault.changed.testGetValue();
  delete snapshot[0].timestamp;
  Assert.deepEqual(
    snapshot[0],
    {
      category: "search.engine.default",
      name: "changed",
      extra: {
        change_source: "enterprise",
        previous_engine_id: prevEngine.telemetryId,
        new_engine_id: "other-MozSearch",
        new_display_name: "MozSearch",
        new_load_path: "[other]addEngineWithDetails:set-via-policy",
        new_submission_url: "",
      },
    },
    "Should have received the correct event details"
  );

  // Clean up
  await Services.search.removeEngine(await Services.search.getDefault());
  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_install_and_set_default_private() {
  Services.telemetry.clearEvents();
  Services.fog.testResetFOG();

  // Make sure we are starting in an expected state to avoid false positive
  // test results.
  let prevEngine = await Services.search.getDefaultPrivate();
  isnot(
    prevEngine.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}",
          },
        ],
        DefaultPrivate: "MozSearch",
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  // If this passes, it means that the new search engine was properly installed
  // *and* was properly set as the default.
  is(
    (await Services.search.getDefaultPrivate()).name,
    "MozSearch",
    "Specified search engine should be the default private engine"
  );

  TelemetryTestUtils.assertEvents(
    [
      {
        // TODO: Bug 1791658 - this should be `change_private` but this test
        // is not currently properly testing setting of the default private engine.
        object: "change_default",
        value: "enterprise",
        extra: {
          prev_id: prevEngine.telemetryId,
          new_id: "other-MozSearch",
          new_name: "MozSearch",
          new_load_path: "[other]addEngineWithDetails:set-via-policy",
          new_sub_url: "",
        },
      },
    ],
    { category: "search", method: "engine" }
  );

  let snapshot = await Glean.searchEngineDefault.changed.testGetValue();
  delete snapshot[0].timestamp;
  Assert.deepEqual(
    snapshot[0],
    {
      // TODO: Bug 1791658 - this should be `search.engine.private` but this test
      // is not currently properly testing setting of the default private engine.
      category: "search.engine.default",
      name: "changed",
      extra: {
        change_source: "enterprise",
        previous_engine_id: prevEngine.telemetryId,
        new_engine_id: "other-MozSearch",
        new_display_name: "MozSearch",
        new_load_path: "[other]addEngineWithDetails:set-via-policy",
        new_submission_url: "",
      },
    },
    "Should have received the correct event details"
  );

  // Clean up
  await Services.search.removeEngine(await Services.search.getDefaultPrivate());
  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(
    (await Services.search.getDefault()).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,
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  is(
    (await Services.search.getDefault()).name,
    "MozSearch",
    "Specified search engine should be the default"
  );

  // Clean up
  await Services.search.removeEngine(await Services.search.getDefault());
  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_opensearch_works() {
  // Clear out policies so we can test with no policies applied
  await setupPolicyEngineWithJson({
@@ -110,3 +327,284 @@ add_task(async function test_opensearch_disabled() {
  // Check that search engines cannot be added via opensearch
  await test_opensearch(false);
});

add_task(async function test_install_and_remove() {
  let iconURL =
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";

  is(
    Services.search.getEngineByName("Foo"),
    null,
    'Engine "Foo" should not be present when test starts'
  );

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Add: [
          {
            Name: "Foo",
            URLTemplate: "http://example.com/?q={searchTerms}",
            IconURL: iconURL,
          },
        ],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  // If this passes, it means that the new search engine was properly installed

  let engine = Services.search.getEngineByName("Foo");
  isnot(engine, null, "Specified search engine should be installed");

  is(engine.wrappedJSObject.iconURI.spec, iconURL, "Icon should be present");
  is(engine.wrappedJSObject.queryCharset, "UTF-8", "Should default to utf-8");

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Remove: ["Foo"],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  // If this passes, it means that the specified engine was properly removed
  is(
    Services.search.getEngineByName("Foo"),
    null,
    "Specified search engine should not be installed"
  );

  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_install_post_method_engine() {
  is(
    Services.search.getEngineByName("Post"),
    null,
    'Engine "Post" should not be present when test starts'
  );

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Add: [
          {
            Name: "Post",
            Method: "POST",
            PostData: "q={searchTerms}&anotherParam=yes",
            URLTemplate: "http://example.com/",
          },
        ],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  let engine = Services.search.getEngineByName("Post");
  isnot(engine, null, "Specified search engine should be installed");

  is(engine.wrappedJSObject._urls[0].method, "POST", "Method should be POST");

  let submission = engine.getSubmission("term", "text/html");
  isnot(submission.postData, null, "Post data should not be null");

  let scriptableInputStream = Cc[
    "@mozilla.org/scriptableinputstream;1"
  ].createInstance(Ci.nsIScriptableInputStream);
  scriptableInputStream.init(submission.postData);
  is(
    scriptableInputStream.read(scriptableInputStream.available()),
    "q=term&anotherParam=yes",
    "Post data should be present"
  );

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Remove: ["Post"],
      },
    },
  });
  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_install_with_encoding() {
  // Make sure we are starting in an expected state to avoid false positive
  // test results.
  is(
    Services.search.getEngineByName("Encoding"),
    null,
    'Engine "Encoding" should not be present when test starts'
  );

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Add: [
          {
            Name: "Encoding",
            Encoding: "windows-1252",
            URLTemplate: "http://example.com/?q={searchTerms}",
          },
        ],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  let engine = Services.search.getEngineByName("Encoding");
  is(
    engine.wrappedJSObject.queryCharset,
    "windows-1252",
    "Should have correct encoding"
  );

  // Clean up
  await Services.search.removeEngine(engine);
  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_install_and_update() {
  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Add: [
          {
            Name: "ToUpdate",
            URLTemplate: "http://initial.example.com/?q={searchTerms}",
          },
        ],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  let engine = Services.search.getEngineByName("ToUpdate");
  isnot(engine, null, "Specified search engine should be installed");

  is(
    engine.getSubmission("test").uri.spec,
    "http://initial.example.com/?q=test",
    "Initial submission URL should be correct."
  );

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Add: [
          {
            Name: "ToUpdate",
            URLTemplate: "http://update.example.com/?q={searchTerms}",
          },
        ],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  is(
    engine.getSubmission("test").uri.spec,
    "http://update.example.com/?q=test",
    "Updated Submission URL should be correct."
  );

  // Clean up
  await Services.search.removeEngine(engine);
  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_install_with_suggest() {
  // Make sure we are starting in an expected state to avoid false positive
  // test results.
  is(
    Services.search.getEngineByName("Suggest"),
    null,
    'Engine "Suggest" should not be present when test starts'
  );

  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Add: [
          {
            Name: "Suggest",
            URLTemplate: "http://example.com/?q={searchTerms}",
            SuggestURLTemplate: "http://suggest.example.com/?q={searchTerms}",
          },
        ],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  let engine = Services.search.getEngineByName("Suggest");

  is(
    engine.getSubmission("test", "application/x-suggestions+json").uri.spec,
    "http://suggest.example.com/?q=test",
    "Updated Submission URL should be correct."
  );

  // Clean up
  await Services.search.removeEngine(engine);
  EnterprisePolicyTesting.resetRunOnceState();
});

add_task(async function test_reset_default() {
  await setupPolicyEngineWithJson({
    policies: {
      SearchEngines: {
        Remove: ["DuckDuckGo"],
      },
    },
  });
  // Get in line, because the Search policy callbacks are async.
  await TestUtils.waitForTick();

  let engine = Services.search.getEngineByName("DuckDuckGo");

  is(engine.hidden, true, "Application specified engine should be hidden.");

  await BrowserTestUtils.withNewTab(
    "about:preferences#search",
    async browser => {
      let tree = browser.contentDocument.querySelector("#engineList");
      for (let i = 0; i < tree.view.rowCount; i++) {
        let cellName = tree.view.getCellText(
          i,
          tree.columns.getNamedColumn("engineName")
        );
        isnot(cellName, "DuckDuckGo", "DuckDuckGo should be invisible");
      }
      let restoreDefaultsButton = browser.contentDocument.getElementById(
        "restoreDefaultSearchEngines"
      );
      let updatedPromise = SearchTestUtils.promiseSearchNotification(
        SearchUtils.MODIFIED_TYPE.CHANGED,
        SearchUtils.TOPIC_ENGINE_MODIFIED
      );
      restoreDefaultsButton.click();
      await updatedPromise;
      for (let i = 0; i < tree.view.rowCount; i++) {
        let cellName = tree.view.getCellText(
          i,
          tree.columns.getNamedColumn("engineName")
        );
        isnot(cellName, "DuckDuckGo", "DuckDuckGo should be invisible");
      }
    }
  );

  engine.hidden = false;
  EnterprisePolicyTesting.resetRunOnceState();
});
+0 −38
Original line number Diff line number Diff line
@@ -4,14 +4,9 @@

"use strict";

const lazy = {};

const { Preferences } = ChromeUtils.import(
  "resource://gre/modules/Preferences.jsm"
);
const { SearchSettings } = ChromeUtils.importESModule(
  "resource://gre/modules/SearchSettings.sys.mjs"
);
const { updateAppInfo, getAppInfo } = ChromeUtils.import(
  "resource://testing-common/AppInfo.jsm"
);
@@ -21,9 +16,6 @@ const { FileTestUtils } = ChromeUtils.import(
const { PermissionTestUtils } = ChromeUtils.import(
  "resource://testing-common/PermissionTestUtils.jsm"
);
ChromeUtils.defineESModuleGetters(lazy, {
  SearchTestUtils: "resource://testing-common/SearchTestUtils.sys.mjs",
});
const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
  "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
);
@@ -41,8 +33,6 @@ let policies = Cc["@mozilla.org/enterprisepolicies;1"].getService(
);
policies.observe(null, "policies-startup", null);

SearchSettings.SETTINGS_INVALIDATION_DELAY = 100;

async function setupPolicyEngineWithJson(json, customSchema) {
  if (typeof json != "object") {
    let filePath = do_get_file(json ? json : "non-existing-file.json").path;
@@ -54,34 +44,6 @@ async function setupPolicyEngineWithJson(json, customSchema) {
  return EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema);
}

/**
 * Loads a new enterprise policy, and re-initialise the search service
 * with the new policy. Also waits for the search service to write the settings
 * file to disk.
 *
 * @param {object} policy
 *   The enterprise policy to use.
 * @param {object} customSchema
 *   A custom schema to use to validate the enterprise policy.
 */
async function setupPolicyEngineWithJsonWithSearch(json, customSchema) {
  Services.search.wrappedJSObject.reset();
  if (typeof json != "object") {
    let filePath = do_get_file(json ? json : "non-existing-file.json").path;
    await EnterprisePolicyTesting.setupPolicyEngineWithJson(
      filePath,
      customSchema
    );
  } else {
    await EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema);
  }
  let settingsWritten = lazy.SearchTestUtils.promiseSearchNotification(
    "write-settings-to-disk-complete"
  );
  await Services.search.init();
  return settingsWritten;
}

function checkLockedPref(prefName, prefValue) {
  equal(
    Preferences.locked(prefName),
+0 −490

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −1
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ support-files =
[test_macosparser_unflatten.js]
skip-if = os != 'mac'
[test_permissions.js]
[test_policy_search_engine.js]
[test_popups_cookies_addons_flash.js]
support-files = config_popups_cookies_addons_flash.json
[test_preferences.js]
Loading