Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: os == 'android'
- Manifest: toolkit/components/search/tests/xpcshell/xpcshell.toml
/* Any copyright is dedicated to the Public Domain.
/*
* Tests that Enterprise Policy Engines can be installed correctly.
*/
"use strict";
const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
);
const CONFIG = [
{ identifier: "appDefaultEngine" },
{ identifier: "otherEngine" },
{ identifier: "otherEngineToMakeDefault" },
];
SearchSettings.SETTINGS_INVALIDATION_DELAY = 100;
/**
* 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.
*/
async function setupPolicyEngineWithJson(policy) {
Services.search.wrappedJSObject.reset();
await EnterprisePolicyTesting.setupPolicyEngineWithJson(policy);
let settingsWritten = SearchTestUtils.promiseSearchNotification(
"write-settings-to-disk-complete"
);
await Services.search.init();
await settingsWritten;
}
add_setup(async function () {
// This initializes the policy engine for xpcshell tests
let policies = Cc["@mozilla.org/enterprisepolicies;1"].getService(
Ci.nsIObserver
);
policies.observe(null, "policies-startup", null);
Services.fog.initializeFOG();
SearchTestUtils.setRemoteSettingsConfig(CONFIG);
});
add_task(async function test_enterprise_policy_engine() {
await setupPolicyEngineWithJson({
policies: {
SearchEngines: {
Add: [
{
Name: "policy",
Description: "Test policy engine",
IconURL: "data:image/gif;base64,R0lGODl",
Alias: "p",
},
],
},
},
});
let engine = Services.search.getEngineByName("policy");
Assert.ok(engine, "Should have installed the engine.");
Assert.equal(engine.name, "policy", "Should have the correct name");
Assert.equal(
engine.description,
"Test policy engine",
"Should have a description"
);
Assert.deepEqual(engine.aliases, ["p"], "Should have the correct alias");
let submission = engine.getSubmission("foo");
Assert.equal(
submission.uri.spec,
"Should have the correct search url"
);
submission = engine.getSubmission("foo", SearchUtils.URL_TYPE.SUGGEST_JSON);
Assert.equal(
submission.uri.spec,
"Should have the correct suggest url"
);
Services.search.defaultEngine = engine;
await assertGleanDefaultEngine({
normal: {
engineId: "other-policy",
displayName: "policy",
loadPath: "[policy]",
submissionUrl: "blank:",
verified: "verified",
},
});
});
add_task(async function test_enterprise_policy_engine_hidden_persisted() {
// Set the engine alias, and wait for the settings to be written.
let settingsWritten = SearchTestUtils.promiseSearchNotification(
"write-settings-to-disk-complete"
);
let engine = Services.search.getEngineByName("policy");
engine.hidden = "p1";
engine.alias = "p1";
await settingsWritten;
// This will reset and re-initialise the search service.
await setupPolicyEngineWithJson({
policies: {
SearchEngines: {
Add: [
{
Name: "policy",
Description: "Test policy engine",
IconURL: "data:image/gif;base64,R0lGODl",
Alias: "p",
},
],
},
},
});
engine = Services.search.getEngineByName("policy");
Assert.equal(engine.alias, "p1", "Should have retained the engine alias");
Assert.ok(engine.hidden, "Should have kept the engine hidden");
});
add_task(async function test_enterprise_policy_engine_remove() {
// This will reset and re-initialise the search service.
await setupPolicyEngineWithJson({
policies: {},
});
Assert.ok(
!Services.search.getEngineByName("policy"),
"Should not have the policy engine installed"
);
let settings = await promiseSettingsData();
Assert.ok(
!settings.engines.find(e => e.name == "p1"),
"Should not have the engine settings stored"
);
});
add_task(async function test_enterprise_policy_hidden_default() {
await setupPolicyEngineWithJson({
policies: {
SearchEngines: {
Remove: ["appDefaultEngine"],
},
},
});
Services.search.resetToAppDefaultEngine();
Assert.ok(
Services.search.getEngineById("appDefaultEngine").hidden,
"Should have removed the application default engine"
);
Assert.equal(Services.search.defaultEngine.identifier, "otherEngine");
});
add_task(async function test_enterprise_policy_default() {
await setupPolicyEngineWithJson({
policies: {
SearchEngines: {
Default: "otherEngineToMakeDefault",
},
},
});
Services.search.resetToAppDefaultEngine();
Assert.equal(
Services.search.defaultEngine.identifier,
"otherEngineToMakeDefault"
);
});