Bug 1445134 - Move most of browser_policy_search_engine.js to an xpcshell-test as it doesn't need to be run as mochitest. r=mkaply

Differential Revision: https://phabricator.services.mozilla.com/D158859
This commit is contained in:
Mark Banner 2022-10-18 07:21:45 +00:00
parent c6d73448ce
commit 3498aa8c9a
3 changed files with 435 additions and 498 deletions

View file

@ -2,15 +2,9 @@
* 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);
@ -65,217 +59,6 @@ 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({
@ -327,284 +110,3 @@ 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();
});

View file

@ -0,0 +1,434 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { SearchTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/SearchTestUtils.sys.mjs"
);
const { TestUtils } = ChromeUtils.import(
"resource://testing-common/TestUtils.jsm"
);
var { AddonTestUtils } = ChromeUtils.import(
"resource://testing-common/AddonTestUtils.jsm"
);
SearchTestUtils.init(this);
AddonTestUtils.init(this, false);
AddonTestUtils.createAppInfo(
"xpcshell@tests.mozilla.org",
"XPCShell",
"48",
"48"
);
add_setup(async () => {
await AddonTestUtils.promiseStartupManager();
await Services.search.init();
console.log("done init");
});
add_task(async function test_install_and_set_default() {
// Make sure we are starting in an expected state to avoid false positive
// test results.
Assert.notEqual(
(await Services.search.getDefault()).name,
"MozSearch",
"Default search engine should not be MozSearch when test starts"
);
Assert.equal(
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.
Assert.equal(
(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_install_and_set_default_private() {
// Make sure we are starting in an expected state to avoid false positive
// test results.
Assert.notEqual(
(await Services.search.getDefaultPrivate()).name,
"MozSearch",
"Default search engine should not be MozSearch when test starts"
);
Assert.equal(
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.
Assert.equal(
(await Services.search.getDefaultPrivate()).name,
"MozSearch",
"Specified search engine should be the default private engine"
);
// 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() {
Assert.notEqual(
(await Services.search.getDefault()).name,
"MozSearch",
"Default search engine should not be MozSearch when test starts"
);
Assert.equal(
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();
Assert.equal(
(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_install_and_remove() {
let iconURL =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
Assert.equal(
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");
Assert.notEqual(engine, null, "Specified search engine should be installed");
Assert.equal(
engine.wrappedJSObject.iconURI.spec,
iconURL,
"Icon should be present"
);
Assert.equal(
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
Assert.equal(
Services.search.getEngineByName("Foo"),
null,
"Specified search engine should not be installed"
);
EnterprisePolicyTesting.resetRunOnceState();
});
add_task(async function test_install_post_method_engine() {
Assert.equal(
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");
Assert.notEqual(engine, null, "Specified search engine should be installed");
Assert.equal(
engine.wrappedJSObject._urls[0].method,
"POST",
"Method should be POST"
);
let submission = engine.getSubmission("term", "text/html");
Assert.notEqual(submission.postData, null, "Post data should not be null");
let scriptableInputStream = Cc[
"@mozilla.org/scriptableinputstream;1"
].createInstance(Ci.nsIScriptableInputStream);
scriptableInputStream.init(submission.postData);
Assert.equal(
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.
Assert.equal(
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");
Assert.equal(
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");
Assert.notEqual(engine, null, "Specified search engine should be installed");
Assert.equal(
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();
engine = Services.search.getEngineByName("ToUpdate");
Assert.notEqual(engine, null, "Specified search engine should be installed");
Assert.equal(
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.
Assert.equal(
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");
Assert.equal(
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");
Assert.equal(
engine.hidden,
true,
"Application specified engine should be hidden."
);
await Services.search.restoreDefaultEngines();
engine = Services.search.getEngineByName("DuckDuckGo");
Assert.equal(
engine.hidden,
false,
"Application specified engine should not be hidden"
);
EnterprisePolicyTesting.resetRunOnceState();
});

View file

@ -19,6 +19,7 @@ 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]