fune/browser/components/attribution/test/xpcshell/test_MacAttribution.js
Nick Alexander de497caa38 Bug 1525076 - Part 4: Cache attribution code on macOS. r=mixedpuppy
Querying the macOS system quarantine database is relatively slow, so
we'd like to do it only once.  This is relevant because the Telemetry
subsystem causes the attribution data to be fetched relatively early
during startup.  By caching, we accept additional disk activity but
don't have to invoke an external process, query a database, etc.

We must make BROWSER_ATTRIBUTION_ERRORS histogram apply to macOS as
well as Windows.  We add error codes to capture macOS-specific
detalis.  And we push this out to a later cycle, since there's no
reason to revisit this immediately.

Differential Revision: https://phabricator.services.mozilla.com/D92695
2020-10-11 18:20:11 +00:00

93 lines
3 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
const { MacAttribution } = ChromeUtils.import(
"resource:///modules/MacAttribution.jsm"
);
add_task(async () => {
await setupStubs();
});
add_task(async function testValidAttrCodes() {
let appPath = MacAttribution.applicationPath;
let attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
Ci.nsIMacAttributionService
);
for (let entry of validAttrCodes) {
// Set a url referrer. In the macOS quarantine database, the
// referrer URL has components that areURI-encoded. Our test data
// URI-encodes the components and also the separators (?, &, =).
// So we decode it and re-encode it to leave just the components
// URI-encoded.
let url = `http://example.com?${encodeURI(decodeURIComponent(entry.code))}`;
attributionSvc.setReferrerUrl(appPath, url, true);
let referrer = await MacAttribution.getReferrerUrl(appPath);
equal(referrer, url, "overwrite referrer url");
// Read attribution code from referrer.
await AttributionCode.deleteFileAsync();
AttributionCode._clearCache();
let result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(
result,
entry.parsed,
"Parsed code should match expected value, code was: " + entry.code
);
// Read attribution code from file.
AttributionCode._clearCache();
result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(
result,
entry.parsed,
"Parsed code should match expected value, code was: " + entry.code
);
// Does not overwrite cached existing attribution code.
attributionSvc.setReferrerUrl(appPath, "http://test.com", false);
referrer = await MacAttribution.getReferrerUrl(appPath);
equal(referrer, url, "update referrer url");
result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(
result,
entry.parsed,
"Parsed code should match expected value, code was: " + entry.code
);
}
});
add_task(async function testInvalidAttrCodes() {
let appPath = MacAttribution.applicationPath;
let attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
Ci.nsIMacAttributionService
);
for (let code of invalidAttrCodes) {
// Set a url referrer. Not all of these invalid codes can be represented
// in the quarantine database; skip those ones.
let url = `http://example.com?${code}`;
let referrer;
try {
attributionSvc.setReferrerUrl(appPath, url, true);
referrer = await MacAttribution.getReferrerUrl(appPath);
} catch (ex) {
continue;
}
if (!referrer) {
continue;
}
equal(referrer, url, "overwrite referrer url");
// Read attribution code from referrer.
await AttributionCode.deleteFileAsync();
AttributionCode._clearCache();
let result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(result, {}, "Code should have failed to parse: " + code);
}
});