Bug 1889677 - Modify SERP categorization tests to wait for events - r=scunnane

Differential Revision: https://phabricator.services.mozilla.com/D206909
This commit is contained in:
James Teow
2024-04-08 19:24:50 +00:00
parent 8d58312986
commit 8c082e0c2c
11 changed files with 125 additions and 28 deletions

View File

@@ -2159,6 +2159,7 @@ class CategorizationRecorder {
Services.obs.addObserver(this, "user-interaction-inactive");
this.#init = true;
this.submitPing("startup");
Services.obs.notifyObservers(null, "categorization-recorder-init");
}
uninit() {

View File

@@ -71,6 +71,16 @@ add_setup(async function () {
await promise;
registerCleanupFunction(async () => {
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
}
SearchSERPTelemetry.overrideSearchTelemetryForTests();
resetTelemetry();
});

View File

@@ -76,7 +76,6 @@ const db = client.db;
// Shorten the timer so that tests don't have to wait too long.
const TIMEOUT_IN_MS = 250;
let didPushPrefEnv = false;
add_setup(async function () {
SearchSERPTelemetry.overrideSearchTelemetryForTests(TEST_PROVIDER_INFO);
await waitForIdle();
@@ -91,7 +90,6 @@ add_setup(async function () {
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
didPushPrefEnv = true;
let promise = waitForDomainToCategoriesUninit();
await SpecialPowers.pushPrefEnv({
set: [["browser.search.serpEventTelemetryCategorization.enabled", false]],
@@ -115,10 +113,14 @@ add_setup(async function () {
TELEMETRY_CATEGORIZATION_DOWNLOAD_SETTINGS.maxAdjust = 0;
registerCleanupFunction(async () => {
// Manually pop the preference so that we wait for the map to be ready
// for the next test.
if (didPushPrefEnv) {
await SpecialPowers.popPrefEnv();
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be initialized.
await SpecialPowers.popPrefEnv();
if (
Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesInit();
}
SearchSERPTelemetry.overrideSearchTelemetryForTests();

View File

@@ -397,15 +397,22 @@ const TESTS = [
add_setup(async function () {
await SpecialPowers.pushPrefEnv({
set: [
["browser.search.serpEventTelemetry.enabled", true],
["browser.search.serpEventTelemetryCategorization.enabled", true],
],
set: [["browser.search.serpEventTelemetryCategorization.enabled", true]],
});
await SearchSERPTelemetry.init();
registerCleanupFunction(async () => {
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
}
resetTelemetry();
});
});

View File

@@ -56,6 +56,16 @@ add_setup(async function () {
await promise;
registerCleanupFunction(async () => {
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
}
SearchSERPTelemetry.overrideSearchTelemetryForTests();
resetTelemetry();
});

View File

@@ -77,11 +77,6 @@ add_setup(async function () {
// Enable local telemetry recording for the duration of the tests.
let oldCanRecord = Services.telemetry.canRecordExtended;
Services.telemetry.canRecordExtended = true;
await SpecialPowers.pushPrefEnv({
set: [["browser.search.serpEventTelemetry.enabled", true]],
});
await db.clear();
let promise = waitForDomainToCategoriesUpdate();
@@ -92,6 +87,17 @@ add_setup(async function () {
await promise;
registerCleanupFunction(async () => {
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
}
SearchSERPTelemetry.overrideSearchTelemetryForTests();
Services.telemetry.canRecordExtended = oldCanRecord;
resetTelemetry();

View File

@@ -78,6 +78,17 @@ add_setup(async function () {
Assert.equal(Region.home, "DE", "Region");
registerCleanupFunction(async () => {
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
}
Region._setHomeRegion(originalHomeRegion);
Region._setCurrentRegion(originalCurrentRegion);

View File

@@ -90,6 +90,16 @@ add_setup(async function () {
await promise;
registerCleanupFunction(async () => {
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
}
SearchSERPTelemetry.overrideSearchTelemetryForTests();
Services.telemetry.canRecordExtended = oldCanRecord;
resetTelemetry();

View File

@@ -87,9 +87,20 @@ add_setup(async function () {
await promise;
registerCleanupFunction(async () => {
// The scheduler uses the mock idle service.
SearchSERPCategorizationEventScheduler.uninit();
SearchSERPCategorizationEventScheduler.init();
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
} else {
// The scheduler uses the mock idle service.
SearchSERPCategorizationEventScheduler.uninit();
SearchSERPCategorizationEventScheduler.init();
}
SearchSERPTelemetry.overrideSearchTelemetryForTests();
resetTelemetry();
});

View File

@@ -92,9 +92,20 @@ add_setup(async function () {
await promise;
registerCleanupFunction(async () => {
// The scheduler uses the mock idle service.
SearchSERPCategorizationEventScheduler.uninit();
SearchSERPCategorizationEventScheduler.init();
// Manually unload the pref so that we can check if we should wait for the
// the categories map to be un-initialized.
await SpecialPowers.popPrefEnv();
if (
!Services.prefs.getBoolPref(
"browser.search.serpEventTelemetryCategorization.enabled"
)
) {
await waitForDomainToCategoriesUninit();
} else {
// The scheduler uses the mock idle service.
SearchSERPCategorizationEventScheduler.uninit();
SearchSERPCategorizationEventScheduler.init();
}
CATEGORIZATION_SETTINGS.WAKE_TIMEOUT_MS = oldWakeTimeout;
SearchSERPTelemetry.overrideSearchTelemetryForTests();
resetTelemetry();

View File

@@ -15,12 +15,32 @@ class TestPingSubmitted(MarionetteTestCase):
self.marionette.enforce_gecko_prefs(
{
"browser.search.serpEventTelemetryCategorization.enabled": True,
"datareporting.healthreport.uploadEnabled": True,
"telemetry.fog.test.localhost_port": 3000,
"browser.search.log": True,
}
)
# The categorization ping is submitted on startup. If anything delays
# its initialization, turning the preference on and immediately
# attaching a categorization event could result in the ping being
# submitted after the test event is reported but before the browser
# restarts.
script = """
let [outerResolve] = arguments;
(async () => {
if (!Services.prefs.getBoolPref("browser.search.serpEventTelemetryCategorization.enabled")) {
let inited = new Promise(innerResolve => {
Services.obs.addObserver(function callback() {
Services.obs.removeObserver(callback, "categorization-recorder-init");
innerResolve();
}, "categorization-recorder-init");
});
Services.prefs.setBoolPref("browser.search.serpEventTelemetryCategorization.enabled", true);
await inited;
}
})().then(outerResolve);
"""
self.marionette.execute_async_script(script)
def test_ping_submit_on_start(self):
# Record an event for the ping to eventually submit.
@@ -48,14 +68,13 @@ class TestPingSubmitted(MarionetteTestCase):
"""
)
self.assertEqual(
self.marionette.execute_script(
Wait(self.marionette, timeout=60).until(
lambda _: self.marionette.execute_script(
"""
return Glean.serp.categorization.testGetValue()?.length ?? 0;
return (Glean.serp.categorization.testGetValue()?.length ?? 0) == 1;
"""
),
1,
msg="Should have recorded a SERP categorization event before restart.",
message="Should have recorded a SERP categorization event before restart.",
)
self.marionette.restart(clean=False, in_app=True)
@@ -66,6 +85,5 @@ class TestPingSubmitted(MarionetteTestCase):
return (Glean.serp.categorization.testGetValue()?.length ?? 0) == 0;
"""
),
0,
message="SERP categorization should have been sent some time after restart.",
)