gecko-dev/toolkit/components/url-classifier/tests/UrlClassifierTestUtils.jsm
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

152 lines
5.1 KiB
JavaScript

"use strict";
var EXPORTED_SYMBOLS = ["UrlClassifierTestUtils"];
const ANNOTATION_TABLE_NAME = "mochitest1-track-simple";
const ANNOTATION_TABLE_PREF = "urlclassifier.trackingAnnotationTable";
const ANNOTATION_WHITELIST_TABLE_NAME = "mochitest1-trackwhite-simple";
const ANNOTATION_WHITELIST_TABLE_PREF = "urlclassifier.trackingAnnotationWhitelistTable";
const TRACKING_TABLE_NAME = "mochitest2-track-simple";
const TRACKING_TABLE_PREF = "urlclassifier.trackingTable";
const WHITELIST_TABLE_NAME = "mochitest2-trackwhite-simple";
const WHITELIST_TABLE_PREF = "urlclassifier.trackingWhitelistTable";
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
var UrlClassifierTestUtils = {
addTestTrackers() {
// Add some URLs to the tracking databases
let annotationURL1 = "tracking.example.org/"; // only for annotations
let annotationURL2 = "itisatracker.org/";
let annotationURL3 = "trackertest.org/";
let annotationURL4 = "another-tracking.example.net/";
let annotationWhitelistedURL = "itisatrap.org/?resource=example.org";
let trackingURL1 = "tracking.example.com/"; // only for TP
let trackingURL2 = "itisatracker.org/";
let trackingURL3 = "trackertest.org/";
let whitelistedURL = "itisatrap.org/?resource=itisatracker.org";
let annotationUpdate =
"n:1000\ni:" + ANNOTATION_TABLE_NAME + "\nad:4\n" +
"a:1:32:" + annotationURL1.length + "\n" +
annotationURL1 + "\n" +
"a:2:32:" + annotationURL2.length + "\n" +
annotationURL2 + "\n" +
"a:3:32:" + annotationURL3.length + "\n" +
annotationURL3 + "\n" +
"a:4:32:" + annotationURL4.length + "\n" +
annotationURL4 + "\n";
let annotationWhitelistUpdate =
"n:1000\ni:" + ANNOTATION_WHITELIST_TABLE_NAME + "\nad:1\n" +
"a:1:32:" + annotationWhitelistedURL.length + "\n" +
annotationWhitelistedURL + "\n";
let trackingUpdate =
"n:1000\ni:" + TRACKING_TABLE_NAME + "\nad:3\n" +
"a:1:32:" + trackingURL1.length + "\n" +
trackingURL1 + "\n" +
"a:2:32:" + trackingURL2.length + "\n" +
trackingURL2 + "\n" +
"a:3:32:" + trackingURL3.length + "\n" +
trackingURL3 + "\n";
let whitelistUpdate =
"n:1000\ni:" + WHITELIST_TABLE_NAME + "\nad:1\n" +
"a:1:32:" + whitelistedURL.length + "\n" +
whitelistedURL + "\n";
var tables = [
{
pref: ANNOTATION_TABLE_PREF,
name: ANNOTATION_TABLE_NAME,
update: annotationUpdate,
},
{
pref: ANNOTATION_WHITELIST_TABLE_PREF,
name: ANNOTATION_WHITELIST_TABLE_NAME,
update: annotationWhitelistUpdate,
},
{
pref: TRACKING_TABLE_PREF,
name: TRACKING_TABLE_NAME,
update: trackingUpdate,
},
{
pref: WHITELIST_TABLE_PREF,
name: WHITELIST_TABLE_NAME,
update: whitelistUpdate,
},
];
let tableIndex = 0;
let doOneUpdate = () => {
if (tableIndex == tables.length) {
return Promise.resolve();
}
return this.useTestDatabase(tables[tableIndex])
.then(() => {
tableIndex++;
return doOneUpdate();
}, aErrMsg => {
dump("Rejected: " + aErrMsg + ". Retry later.\n");
return new Promise(resolve => {
timer.initWithCallback(resolve, 100, Ci.nsITimer.TYPE_ONE_SHOT);
})
.then(doOneUpdate);
});
};
return doOneUpdate();
},
cleanupTestTrackers() {
Services.prefs.clearUserPref(ANNOTATION_TABLE_PREF);
Services.prefs.clearUserPref(ANNOTATION_WHITELIST_TABLE_PREF);
Services.prefs.clearUserPref(TRACKING_TABLE_PREF);
Services.prefs.clearUserPref(WHITELIST_TABLE_PREF);
},
/**
* Add some entries to a test tracking protection database, and resets
* back to the default database after the test ends.
*
* @return {Promise}
*/
useTestDatabase(table) {
Services.prefs.setCharPref(table.pref, table.name);
return new Promise((resolve, reject) => {
let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"].
getService(Ci.nsIUrlClassifierDBService);
let listener = {
QueryInterface: iid => {
if (iid.equals(Ci.nsISupports) ||
iid.equals(Ci.nsIUrlClassifierUpdateObserver))
return listener;
throw Cr.NS_ERROR_NO_INTERFACE;
},
updateUrlRequested: url => { },
streamFinished: status => { },
updateError: errorCode => {
reject("Got updateError when updating " + table.name);
},
updateSuccess: requestedTimeout => {
resolve();
},
};
try {
dbService.beginUpdate(listener, table.name, "");
dbService.beginStream("", "");
dbService.updateStream(table.update);
dbService.finishStream();
dbService.finishUpdate();
} catch (e) {
reject("Failed to update with dbService: " + table.name);
}
});
},
};