diff options
Diffstat (limited to 'browser/components/search/test/browser/telemetry/head.js')
-rw-r--r-- | browser/components/search/test/browser/telemetry/head.js | 83 |
1 files changed, 80 insertions, 3 deletions
diff --git a/browser/components/search/test/browser/telemetry/head.js b/browser/components/search/test/browser/telemetry/head.js index 416451e400..b798099bdd 100644 --- a/browser/components/search/test/browser/telemetry/head.js +++ b/browser/components/search/test/browser/telemetry/head.js @@ -45,6 +45,10 @@ ChromeUtils.defineLazyGetter(this, "SEARCH_AD_CLICK_SCALARS", () => { ]; }); +ChromeUtils.defineLazyGetter(this, "gCryptoHash", () => { + return Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash); +}); + // For use with categorization. const APP_MAJOR_VERSION = parseInt(Services.appinfo.version).toString(); const CHANNEL = SearchUtils.MODIFIED_APP_CHANNEL; @@ -207,6 +211,11 @@ function resetTelemetry() { * values we use to validate the recorded Glean impression events. */ function assertSERPTelemetry(expectedEvents) { + // Do a deep copy of impressions in case the input is using constants, as + // we insert impression id into the expected events to make it easier to + // run Assert.deepEqual() on the expected and actual result. + expectedEvents = JSON.parse(JSON.stringify(expectedEvents)); + // A single test might run assertImpressionEvents more than once // so the Set needs to be cleared or else the impression event // check will throw. @@ -385,6 +394,46 @@ add_setup(function () { }); }); +async function openSerpInNewTab(url, expectedAds = true) { + let promise; + if (expectedAds) { + promise = waitForPageWithAdImpressions(); + } + let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url); + await promise; + + let cleanup = async () => { + await BrowserTestUtils.removeTab(tab); + resetTelemetry(); + }; + + return { tab, cleanup }; +} + +async function synthesizePageAction({ + selector, + event = {}, + tab, + expectEngagement = true, +} = {}) { + let promise; + if (expectEngagement) { + promise = waitForPageWithAction(); + } else { + // Wait roughly around how much it might take for a possible page action + // to be registered in telemetry. + /* eslint-disable-next-line mozilla/no-arbitrary-setTimeout */ + promise = new Promise(resolve => setTimeout(resolve, 50)); + } + await BrowserTestUtils.synthesizeMouseAtCenter( + selector, + event, + tab.linkedBrowser + ); + + await promise; +} + function assertCategorizationValues(expectedResults) { // TODO Bug 1868476: Replace with calls to Glean telemetry. let actualResults = [...fakeTelemetryStorage]; @@ -435,6 +484,10 @@ function assertCategorizationValues(expectedResults) { } } +function waitForPageWithAction() { + return TestUtils.topicObserved("reported-page-with-action"); +} + function waitForPageWithAdImpressions() { return TestUtils.topicObserved("reported-page-with-ad-impressions"); } @@ -459,10 +512,9 @@ registerCleanupFunction(async () => { await PlacesUtils.history.clear(); }); -async function mockRecordWithAttachment({ id, version, filename }) { +async function mockRecordWithAttachment({ id, version, filename, mapping }) { // Get the bytes of the file for the hash and size for attachment metadata. - let data = await IOUtils.readUTF8(getTestFilePath(filename)); - let buffer = new TextEncoder().encode(data).buffer; + let buffer = new TextEncoder().encode(JSON.stringify(mapping)).buffer; let stream = Cc["@mozilla.org/io/arraybuffer-input-stream;1"].createInstance( Ci.nsIArrayBufferInputStream ); @@ -506,6 +558,30 @@ async function resetCategorizationCollection(record) { await client.db.importChanges({}, Date.now()); } +const MOCK_ATTACHMENT_VALUES = { + "abc.com": [2, 95], + "abc.org": [4, 90], + "def.com": [2, 78, 4, 10], + "def.org": [4, 90], + "foobar.org": [3, 90], +}; + +const CONVERTED_ATTACHMENT_VALUES = convertDomainsToHashes( + MOCK_ATTACHMENT_VALUES +); + +function convertDomainsToHashes(domainsToCategories) { + let newObj = {}; + for (let [key, value] of Object.entries(domainsToCategories)) { + gCryptoHash.init(gCryptoHash.SHA256); + let bytes = new TextEncoder().encode(key); + gCryptoHash.update(bytes, key.length); + let hash = gCryptoHash.finish(true); + newObj[hash] = value; + } + return newObj; +} + async function insertRecordIntoCollection() { const client = RemoteSettings(TELEMETRY_CATEGORIZATION_KEY); const db = client.db; @@ -515,6 +591,7 @@ async function insertRecordIntoCollection() { id: "example_id", version: 1, filename: "domain_category_mappings.json", + mapping: CONVERTED_ATTACHMENT_VALUES, }); await db.create(record); await client.attachments.cacheImpl.set(record.id, attachment); |