fune/browser/components/urlbar/tests/unit/test_exposure.js
Drew Willcoxon a10a5ff60a Bug 1861540 - Part 2: Update tests to use the local remote settings server. r=daisuke
This modifies tests to use the server. There are a few important points to call
out:

This means tests are now using the real Rust component, and we need to make sure
the test RS data is valid and matches what Rust expects. For example, I had to
add `icon` properties to suggestions and set the `advertiser` to "Wikipedia" for
non-sponsored suggestions. Otherwise Rust hits an error on ingest. I also
removed some test cases because they tested behaviors that are impossible with
Rust, for example Pocket keywords that are duplicated in the high- and
low-confidence arrays.

We need to be careful to wait until Suggest is done syncing from remote settings
regardless of whether it's using the JS or Rust backend. I added a way to force
the backends to sync. That way, tests can force a sync, wait for it to finish,
and be sure that all sync activity is done.

A common pattern in tests is to call `ensureQuickSuggestInit()` and then set
Suggest-related prefs (or vice versa). This is a little problematic because both
`ensureQuickSuggestInit()` and setting prefs can cause Suggest to start a sync.
It's more problematic now that we're not mocking remote settings or Rust. So I
combined the two by adding a `prefs` param to `ensureQuickSuggestInit()`. That
way, tests can be sure that all syncing is done once that function returns.

Depends on D192037, D192124

Differential Revision: https://phabricator.services.mozilla.com/D192038
2023-10-30 23:33:12 +00:00

184 lines
5.5 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
ChromeUtils.defineESModuleGetters(this, {
QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
UrlbarProviderQuickSuggest:
"resource:///modules/UrlbarProviderQuickSuggest.sys.mjs",
});
// Tests that registering an exposureResults pref and triggering a match causes
// the exposure event to be recorded on the UrlbarResults.
const REMOTE_SETTINGS_RESULTS = [
{
id: 1,
url: "http://test.com/q=frabbits",
title: "frabbits",
keywords: ["test"],
click_url: "http://click.reporting.test.com/",
impression_url: "http://impression.reporting.test.com/",
advertiser: "TestAdvertiser",
},
{
id: 2,
url: "http://test.com/q=frabbits",
title: "frabbits",
keywords: ["non_sponsored"],
click_url: "http://click.reporting.test.com/",
impression_url: "http://impression.reporting.test.com/",
advertiser: "wikipedia",
iab_category: "5 - Education",
},
];
const EXPECTED_REMOTE_SETTINGS_URLBAR_RESULT = {
type: UrlbarUtils.RESULT_TYPE.URL,
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
heuristic: false,
payload: {
telemetryType: "adm_sponsored",
qsSuggestion: "test",
title: "frabbits",
url: "http://test.com/q=frabbits",
originalUrl: "http://test.com/q=frabbits",
icon: null,
sponsoredImpressionUrl: "http://impression.reporting.test.com/",
sponsoredClickUrl: "http://click.reporting.test.com/",
sponsoredBlockId: 1,
sponsoredAdvertiser: "TestAdvertiser",
isSponsored: true,
descriptionL10n: { id: "urlbar-result-action-sponsored" },
helpUrl: QuickSuggest.HELP_URL,
helpL10n: {
id: "urlbar-result-menu-learn-more-about-firefox-suggest",
},
isBlockable: true,
blockL10n: {
id: "urlbar-result-menu-dismiss-firefox-suggest",
},
displayUrl: "http://test.com/q=frabbits",
source: "remote-settings",
provider: "AdmWikipedia",
},
};
const EXPECTED_NON_SPONSORED_REMOTE_SETTINGS_RESULT = {
type: UrlbarUtils.RESULT_TYPE.URL,
source: UrlbarUtils.RESULT_SOURCE.SEARCH,
heuristic: false,
payload: {
telemetryType: "adm_nonsponsored",
qsSuggestion: "non_sponsored",
title: "frabbits",
url: "http://test.com/q=frabbits",
originalUrl: "http://test.com/q=frabbits",
icon: null,
sponsoredImpressionUrl: "http://impression.reporting.test.com/",
sponsoredClickUrl: "http://click.reporting.test.com/",
sponsoredBlockId: 2,
sponsoredAdvertiser: "wikipedia",
sponsoredIabCategory: "5 - Education",
isSponsored: false,
helpUrl: QuickSuggest.HELP_URL,
helpL10n: {
id: "urlbar-result-menu-learn-more-about-firefox-suggest",
},
isBlockable: true,
blockL10n: {
id: "urlbar-result-menu-dismiss-firefox-suggest",
},
displayUrl: "http://test.com/q=frabbits",
source: "remote-settings",
provider: "AdmWikipedia",
},
};
add_setup(async function test_setup() {
// FOG needs a profile directory to put its data in.
do_get_profile();
// FOG needs to be initialized in order for data to flow.
Services.fog.initializeFOG();
// Set up the remote settings client with the test data.
await QuickSuggestTestUtils.ensureQuickSuggestInit({
remoteSettingsRecords: [
{
type: "data",
attachment: REMOTE_SETTINGS_RESULTS,
},
],
prefs: [
["suggest.quicksuggest.nonsponsored", true],
["suggest.quicksuggest.sponsored", true],
],
});
});
add_task(async function testExposureCheck() {
UrlbarPrefs.set("exposureResults", "rs_adm_sponsored");
UrlbarPrefs.set("showExposureResults", true);
let context = createContext("test", {
providers: [UrlbarProviderQuickSuggest.name],
isPrivate: false,
});
await check_results({
context,
matches: [EXPECTED_REMOTE_SETTINGS_URLBAR_RESULT],
});
Assert.equal(context.results[0].exposureResultType, "rs_adm_sponsored");
Assert.equal(context.results[0].exposureResultHidden, false);
});
add_task(async function testExposureCheckMultiple() {
UrlbarPrefs.set("exposureResults", "rs_adm_sponsored,rs_adm_nonsponsored");
UrlbarPrefs.set("showExposureResults", true);
let context = createContext("test", {
providers: [UrlbarProviderQuickSuggest.name],
isPrivate: false,
});
await check_results({
context,
matches: [EXPECTED_REMOTE_SETTINGS_URLBAR_RESULT],
});
Assert.equal(context.results[0].exposureResultType, "rs_adm_sponsored");
Assert.equal(context.results[0].exposureResultHidden, false);
context = createContext("non_sponsored", {
providers: [UrlbarProviderQuickSuggest.name],
isPrivate: false,
});
await check_results({
context,
matches: [EXPECTED_NON_SPONSORED_REMOTE_SETTINGS_RESULT],
});
Assert.equal(context.results[0].exposureResultType, "rs_adm_nonsponsored");
Assert.equal(context.results[0].exposureResultHidden, false);
});
add_task(async function exposureDisplayFiltering() {
UrlbarPrefs.set("exposureResults", "rs_adm_sponsored");
UrlbarPrefs.set("showExposureResults", false);
let context = createContext("test", {
providers: [UrlbarProviderQuickSuggest.name],
isPrivate: false,
});
await check_results({
context,
matches: [EXPECTED_REMOTE_SETTINGS_URLBAR_RESULT],
});
Assert.equal(context.results[0].exposureResultType, "rs_adm_sponsored");
Assert.equal(context.results[0].exposureResultHidden, true);
});