Bug 1182269 - Create UrlClassifierTestUtils.jsm to share URL Classifier test code. r=francois
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Thu, 09 Jul 2015 16:40:24 -0400
changeset 284023 75dda7e302d58fb81bbac5cb8d1049995d286841
parent 284022 4ec4878f071bd8365bf12bae6ca2cb4a0c168f48
child 284024 08f9ce20e80384f27a1ca5c96df3ee0cfedf289e
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1182269
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1182269 - Create UrlClassifierTestUtils.jsm to share URL Classifier test code. r=francois
browser/base/content/test/general/browser_trackingUI_1.js
browser/base/content/test/general/browser_trackingUI_2.js
browser/base/content/test/general/head.js
toolkit/components/url-classifier/tests/UrlClassifierTestUtils.jsm
toolkit/components/url-classifier/tests/mochitest/test_allowlisted_annotations.html
toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html
toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_bug1157081.html
toolkit/components/url-classifier/tests/moz.build
--- a/browser/base/content/test/general/browser_trackingUI_1.js
+++ b/browser/base/content/test/general/browser_trackingUI_1.js
@@ -4,25 +4,29 @@
 
 // Test that the Tracking Protection section is visible in the Control Center
 // and has the correct state for the cases when:
 //   * A page with no tracking elements is loaded.
 //   * A page with tracking elements is loaded and they are blocked.
 //   * A page with tracking elements is loaded and they are not blocked.
 // See also Bugs 1175327, 1043801, 1178985
 
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 const PREF = "privacy.trackingprotection.enabled";
 const PB_PREF = "privacy.trackingprotection.pbmode.enabled";
 const BENIGN_PAGE = "http://tracking.example.org/browser/browser/base/content/test/general/benignPage.html";
 const TRACKING_PAGE = "http://tracking.example.org/browser/browser/base/content/test/general/trackingPage.html";
 let TrackingProtection = null;
 let browser = null;
 
+let {UrlClassifierTestUtils} = Cu.import("resource://testing-common/UrlClassifierTestUtils.jsm", {});
+
 registerCleanupFunction(function() {
   TrackingProtection = browser = null;
+  UrlClassifierTestUtils.cleanupTestTrackers();
   Services.prefs.clearUserPref(PREF);
   Services.prefs.clearUserPref(PB_PREF);
   while (gBrowser.tabs.length > 1) {
     gBrowser.removeCurrentTab();
   }
 });
 
 function hidden(sel) {
@@ -103,17 +107,17 @@ function* testTrackingProtectionForTab(t
   clickButton("#tracking-action-block");
 
   info("Wait for tab to reload following TP black-listing");
   yield promiseTabLoadEvent(tab);
   testTrackingPage();
 }
 
 add_task(function* testNormalBrowsing() {
-  yield updateTrackingProtectionDatabase();
+  yield UrlClassifierTestUtils.addTestTrackers();
 
   browser = gBrowser;
   let tab = browser.selectedTab = browser.addTab();
 
   TrackingProtection = gBrowser.ownerGlobal.TrackingProtection;
   ok (TrackingProtection, "TP is attached to the browser window");
   is (TrackingProtection.enabled, Services.prefs.getBoolPref(PREF),
     "TP.enabled is based on the original pref value");
--- a/browser/base/content/test/general/browser_trackingUI_2.js
+++ b/browser/base/content/test/general/browser_trackingUI_2.js
@@ -1,34 +1,38 @@
 /* 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/. */
 
 // Test that the Tracking Protection section is never visible in the
 // Control Center when the feature is off.
 // See also Bugs 1175327, 1043801, 1178985.
 
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 const PREF = "privacy.trackingprotection.enabled";
 const PB_PREF = "privacy.trackingprotection.pbmode.enabled";
 const BENIGN_PAGE = "http://tracking.example.org/browser/browser/base/content/test/general/benignPage.html";
 const TRACKING_PAGE = "http://tracking.example.org/browser/browser/base/content/test/general/trackingPage.html";
 let TrackingProtection = null;
 let browser = null;
 
+let {UrlClassifierTestUtils} = Cu.import("resource://testing-common/UrlClassifierTestUtils.jsm", {});
+
 registerCleanupFunction(function() {
   TrackingProtection = browser = null;
+  UrlClassifierTestUtils.cleanupTestTrackers();
   Services.prefs.clearUserPref(PREF);
   Services.prefs.clearUserPref(PB_PREF);
   while (gBrowser.tabs.length > 1) {
     gBrowser.removeCurrentTab();
   }
 });
 
 add_task(function* testNormalBrowsing() {
-  yield updateTrackingProtectionDatabase();
+  yield UrlClassifierTestUtils.addTestTrackers();
 
   browser = gBrowser;
   let tab = browser.selectedTab = browser.addTab();
 
   TrackingProtection = browser.ownerGlobal.TrackingProtection;
   ok (TrackingProtection, "TP is attached to the browser window");
   is (TrackingProtection.enabled, Services.prefs.getBoolPref(PREF),
     "TP.enabled is based on the original pref value");
--- a/browser/base/content/test/general/head.js
+++ b/browser/base/content/test/general/head.js
@@ -655,65 +655,16 @@ function promiseWindow(url) {
 function promiseIndicatorWindow() {
   // We don't show the indicator window on Mac.
   if ("nsISystemStatusBar" in Ci)
     return Promise.resolve();
 
   return promiseWindow("chrome://browser/content/webrtcIndicator.xul");
 }
 
-/**
- * Add some entries to a test tracking protection database, and reset
- * back to the default database after the test ends.
- */
-function updateTrackingProtectionDatabase() {
-  let TABLE = "urlclassifier.trackingTable";
-  Services.prefs.setCharPref(TABLE, "test-track-simple");
-
-  registerCleanupFunction(function() {
-    Services.prefs.clearUserPref(TABLE);
-  });
-
-  // Add some URLs to the tracking database (to be blocked)
-  let testData = "tracking.example.com/";
-  let testUpdate =
-    "n:1000\ni:test-track-simple\nad:1\n" +
-    "a:524:32:" + testData.length + "\n" +
-    testData;
-
-  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 => {
-        ok(false, "Couldn't update classifier.");
-        resolve();
-      },
-      updateSuccess: requestedTimeout => {
-        resolve();
-      }
-    };
-
-    dbService.beginUpdate(listener, "test-track-simple", "");
-    dbService.beginStream("", "");
-    dbService.updateStream(testUpdate);
-    dbService.finishStream();
-    dbService.finishUpdate();
-  });
-}
-
 function assertWebRTCIndicatorStatus(expected) {
   let ui = Cu.import("resource:///modules/webrtcUI.jsm", {}).webrtcUI;
   let expectedState = expected ? "visible" : "hidden";
   let msg = "WebRTC indicator " + expectedState;
   if (!expected && ui.showGlobalIndicator) {
     // It seems the global indicator is not always removed synchronously
     // in some cases.
     info("waiting for the global indicator to be hidden");
new file mode 100644
--- /dev/null
+++ b/toolkit/components/url-classifier/tests/UrlClassifierTestUtils.jsm
@@ -0,0 +1,67 @@
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["UrlClassifierTestUtils"];
+
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+this.UrlClassifierTestUtils = {
+
+  addTestTrackers() {
+    const TABLE_PREF = "urlclassifier.trackingTable";
+    const TABLE_NAME = "test-track-simple";
+
+    // Add some URLs to the tracking database (to be blocked)
+    let testData = "tracking.example.com/";
+    let testUpdate =
+          "n:1000\ni:test-track-simple\nad:1\n" +
+          "a:524:32:" + testData.length + "\n" +
+          testData;
+
+    return this.useTestDatabase(TABLE_PREF, TABLE_NAME, testUpdate);
+  },
+
+  cleanupTestTrackers() {
+    const TABLE_PREF = "urlclassifier.trackingTable";
+    Services.prefs.clearUserPref(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(tablePref, tableName, update) {
+    Services.prefs.setCharPref(tablePref, tableName);
+
+    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("Couldn't update classifier.");
+        },
+        updateSuccess: requestedTimeout => {
+          resolve();
+        }
+      };
+
+      dbService.beginUpdate(listener, tableName, "");
+      dbService.beginStream("", "");
+      dbService.updateStream(update);
+      dbService.finishStream();
+      dbService.finishUpdate();
+    });
+  },
+};
--- a/toolkit/components/url-classifier/tests/mochitest/test_allowlisted_annotations.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_allowlisted_annotations.html
@@ -12,73 +12,43 @@
 </div>
 <pre id="test">
 
 <script class="testbody" type="text/javascript">
 
 var Cc = SpecialPowers.Cc;
 var Ci = SpecialPowers.Ci;
 
+Components.utils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
+
 // Add https://allowlisted.example.com to the permissions manager
 SpecialPowers.addPermission("trackingprotection",
                             Ci.nsIPermissionManager.ALLOW_ACTION,
                             { url: "https://allowlisted.example.com" });
 
-// Add some URLs to the tracking database
-var testData = "tracking.example.com/";
-var testUpdate =
-  "n:1000\ni:test-track-simple\nad:1\n" +
-  "a:524:32:" + testData.length + "\n" +
-  testData;
-
-var dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
-                .getService(Ci.nsIUrlClassifierDBService);
-
 function clearPermissions() {
   SpecialPowers.removePermission("trackingprotection",
                                  { url: "https://allowlisted.example.com" });
   ok(!SpecialPowers.testPermission("trackingprotection",
                                    Ci.nsIPermissionManager.ALLOW_ACTION,
                                    { url: "https://allowlisted.example.com" }));
 }
 
-function doUpdate(update) {
-  var listener = {
-    QueryInterface: function(iid)
-    {
-      if (iid.equals(Ci.nsISupports) ||
-          iid.equals(Ci.nsIUrlClassifierUpdateObserver))
-        return this;
-
-      throw Cr.NS_ERROR_NO_INTERFACE;
-    },
-    updateUrlRequested: function(url) { },
-    streamFinished: function(status) { },
-    updateError: function(errorCode) {
-      ok(false, "Couldn't update classifier.");
-      // Abort test.
-      SimpleTest.finish();
-    },
-    updateSuccess: function(requestedTimeout) {
-      document.getElementById("testFrame").src = "allowlistAnnotatedFrame.html";
-    }
-  };
-
-  dbService.beginUpdate(listener, "test-track-simple", "");
-  dbService.beginStream("", "");
-  dbService.updateStream(update);
-  dbService.finishStream();
-  dbService.finishUpdate();
-}
-
 SpecialPowers.pushPrefEnv(
   {"set" : [["urlclassifier.trackingTable", "test-track-simple"],
             ["privacy.trackingprotection.enabled", true],
             ["channelclassifier.allowlist_example", true]]},
-  function() { doUpdate(testUpdate); });
+  test);
+
+function test() {
+  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
+  UrlClassifierTestUtils.addTestTrackers().then(() => {
+    document.getElementById("testFrame").src = "allowlistAnnotatedFrame.html";
+  });
+}
 
 // Expected finish() call is in "allowlistedAnnotatedFrame.html".
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 </pre>
 <iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
--- a/toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html
@@ -12,68 +12,37 @@
 </div>
 <pre id="test">
 
 <script class="testbody" type="text/javascript">
 
 var Cc = SpecialPowers.Cc;
 var Ci = SpecialPowers.Ci;
 
-// Add some URLs to the tracking database
-var testData = "tracking.example.com/";
-var testUpdate =
-  "n:1000\ni:test-track-simple\nad:1\n" +
-  "a:524:32:" + testData.length + "\n" +
-  testData;
+Components.utils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
 
 function cleanup() {
   SpecialPowers.clearUserPref("privacy.trackingprotection.enabled");
   SpecialPowers.clearUserPref("channelclassifier.allowlist_example");
 }
 
-var dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
-                .getService(Ci.nsIUrlClassifierDBService);
-
-function doUpdate(update) {
-  var listener = {
-    QueryInterface: function(iid)
-    {
-      if (iid.equals(Ci.nsISupports) ||
-          iid.equals(Ci.nsIUrlClassifierUpdateObserver))
-        return this;
-
-      throw Cr.NS_ERROR_NO_INTERFACE;
-    },
-    updateUrlRequested: function(url) { },
-    streamFinished: function(status) { },
-    updateError: function(errorCode) {
-      ok(false, "Couldn't update classifier.");
-      // Abort test.
-      SimpleTest.finish();
-    },
-    updateSuccess: function(requestedTimeout) {
-      SpecialPowers.setBoolPref("privacy.trackingprotection.enabled", true);
-      // Make sure chrome:// URIs are processed. This does not white-list
-      // any URIs unless 'https://allowlisted.example.com' is added in the
-      // permission manager (see test_allowlisted_annotations.html)
-      SpecialPowers.setBoolPref("channelclassifier.allowlist_example", true);
-      document.getElementById("testFrame").src = "classifiedAnnotatedFrame.html";
-    }
-  };
-
-  dbService.beginUpdate(listener, "test-track-simple", "");
-  dbService.beginStream("", "");
-  dbService.updateStream(update);
-  dbService.finishStream();
-  dbService.finishUpdate();
-}
-
 SpecialPowers.pushPrefEnv(
   {"set" : [["urlclassifier.trackingTable", "test-track-simple"]]},
-  function() { doUpdate(testUpdate); });
+  test);
+
+function test() {
+  UrlClassifierTestUtils.addTestTrackers().then(() => {
+    SpecialPowers.setBoolPref("privacy.trackingprotection.enabled", true);
+    // Make sure chrome:// URIs are processed. This does not white-list
+    // any URIs unless 'https://allowlisted.example.com' is added in the
+    // permission manager (see test_allowlisted_annotations.html)
+    SpecialPowers.setBoolPref("channelclassifier.allowlist_example", true);
+    document.getElementById("testFrame").src = "classifiedAnnotatedFrame.html";
+  });
+}
 
 // Expected finish() call is in "classifiedAnnotatedFrame.html".
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 </pre>
 <iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
--- a/toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
@@ -23,16 +23,17 @@ var mainWindow = window.QueryInterface(C
                     .getInterface(Ci.nsIWebNavigation)
                     .QueryInterface(Ci.nsIDocShellTreeItem)
                     .rootTreeItem
                     .QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIDOMWindow);
 var contentPage = "chrome://mochitests/content/chrome/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedPBFrame.html"
 
 Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
 
 function whenDelayedStartupFinished(aWindow, aCallback) {
   Services.obs.addObserver(function observer(aSubject, aTopic) {
     if (aWindow == aSubject) {
       Services.obs.removeObserver(observer, aTopic);
       setTimeout(aCallback, 0);
     }
   }, "browser-delayed-startup-finished", false);
@@ -55,23 +56,16 @@ function testOnWindow(aPrivate, aCallbac
           SimpleTest.executeSoon(function() { aCallback(win); });
         }, false, true);
       }, true);
       SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
     });
   }, true);
 }
 
-// Add some URLs to the tracking database
-var testData = "tracking.example.com/";
-var testUpdate =
-  "n:1000\ni:test-track-simple\nad:1\n" +
-  "a:524:32:" + testData.length + "\n" +
-  testData;
-
 var badids = [
   "badscript",
   "badimage",
   "badcss"
 ];
 
 function checkLoads(aWindow, aBlocked) {
   var win = aWindow.content;
@@ -112,72 +106,47 @@ function checkLoads(aWindow, aBlocked) {
         (blockedTrackingNodes[i] == win.document.getElementById(badids[j]));
     }
 
     allNodeMatch = allNodeMatch && nodeMatch;
   }
   is(allNodeMatch, aBlocked, "All tracking nodes are expected to be annotated as such");
 }
 
-var dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
-                .getService(Ci.nsIUrlClassifierDBService);
-
-function doUpdate(update) {
-  var listener = {
-    QueryInterface: function(iid)
-    {
-      if (iid.equals(Ci.nsISupports) ||
-          iid.equals(Ci.nsIUrlClassifierUpdateObserver))
-        return this;
-
-      throw Cr.NS_ERROR_NO_INTERFACE;
-    },
-    updateUrlRequested: function(url) { },
-    streamFinished: function(status) { },
-    updateError: function(errorCode) {
-      ok(false, "Couldn't update classifier.");
-      // Abort test.
-      SimpleTest.finish();
-    },
-    updateSuccess: function(requestedTimeout) {
-      // Normal mode, with the pref (trackers should be loaded)
-      testOnWindow(false, function(aWindow) {
-        checkLoads(aWindow, false);
-        aWindow.close();
-
-        // Private Browsing, with the pref (trackers should be blocked)
-        testOnWindow(true, function(aWindow) {
-          checkLoads(aWindow, true);
-          aWindow.close();
-
-          // Private Browsing, without the pref (trackers should be loaded)
-          SpecialPowers.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);
-          testOnWindow(true, function(aWindow) {
-             checkLoads(aWindow, false);
-             aWindow.close();
-             SimpleTest.finish();
-          });
-        });
-      });
-    }
-  };
-
-  dbService.beginUpdate(listener, "test-track-simple", "");
-  dbService.beginStream("", "");
-  dbService.updateStream(update);
-  dbService.finishStream();
-  dbService.finishUpdate();
-}
-
 SpecialPowers.pushPrefEnv(
   {"set" : [["urlclassifier.trackingTable", "test-track-simple"],
             ["privacy.trackingprotection.enabled", false],
             ["privacy.trackingprotection.pbmode.enabled", true],
             ["channelclassifier.allowlist_example", true]]},
-  function() { doUpdate(testUpdate); });
+  test);
+
+function test() {
+  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
+  UrlClassifierTestUtils.addTestTrackers().then(() => {
+    // Normal mode, with the pref (trackers should be loaded)
+    testOnWindow(false, function(aWindow) {
+      checkLoads(aWindow, false);
+      aWindow.close();
+
+      // Private Browsing, with the pref (trackers should be blocked)
+      testOnWindow(true, function(aWindow) {
+        checkLoads(aWindow, true);
+        aWindow.close();
+
+        // Private Browsing, without the pref (trackers should be loaded)
+        SpecialPowers.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);
+        testOnWindow(true, function(aWindow) {
+          checkLoads(aWindow, false);
+          aWindow.close();
+          SimpleTest.finish();
+        });
+      });
+    });
+  });
+}
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 </pre>
 <iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
 </body>
--- a/toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_bug1157081.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_bug1157081.html
@@ -23,16 +23,17 @@ var mainWindow = window.QueryInterface(C
                     .getInterface(Ci.nsIWebNavigation)
                     .QueryInterface(Ci.nsIDocShellTreeItem)
                     .rootTreeItem
                     .QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIDOMWindow);
 var contentPage = "chrome://mochitests/content/chrome/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedPBFrame.html"
 
 Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
 
 function whenDelayedStartupFinished(aWindow, aCallback) {
   Services.obs.addObserver(function observer(aSubject, aTopic) {
     if (aWindow == aSubject) {
       Services.obs.removeObserver(observer, aTopic);
       setTimeout(aCallback, 0);
     }
   }, "browser-delayed-startup-finished", false);
@@ -55,83 +56,51 @@ function testOnWindow(aCallback) {
           SimpleTest.executeSoon(function() { aCallback(win); });
         }, false, true);
       }, true);
       SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
     });
   }, true);
 }
 
-// Add some URLs to the tracking database
-var testData = "tracking.example.com/";
-var testUpdate =
-  "n:1000\ni:test-track-simple\nad:1\n" +
-  "a:524:32:" + testData.length + "\n" +
-  testData;
-
 var badids = [
   "badscript"
 ];
 
 function checkLoads(aWindow, aBlocked) {
   var win = aWindow.content;
   is(win.document.getElementById("badscript").dataset.touched, aBlocked ? "no" : "yes", "Should not load tracking javascript");
 }
 
-var dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
-                .getService(Ci.nsIUrlClassifierDBService);
-
-function doUpdate(update) {
-  var listener = {
-    QueryInterface: function(iid)
-    {
-      if (iid.equals(Ci.nsISupports) ||
-          iid.equals(Ci.nsIUrlClassifierUpdateObserver))
-        return this;
-
-      throw Cr.NS_ERROR_NO_INTERFACE;
-    },
-    updateUrlRequested: function(url) { },
-    streamFinished: function(status) { },
-    updateError: function(errorCode) {
-      ok(false, "Couldn't update classifier.");
-      // Abort test.
-      SimpleTest.finish();
-    },
-    updateSuccess: function(requestedTimeout) {
-      // Safe Browsing turned OFF, tracking protection should work nevertheless
-      testOnWindow(function(aWindow) {
-        checkLoads(aWindow, true);
-        aWindow.close();
-
-        // Safe Browsing turned ON, tracking protection should still work
-        SpecialPowers.setBoolPref("browser.safebrowsing.enabled", true);
-        testOnWindow(function(aWindow) {
-          checkLoads(aWindow, true);
-          aWindow.close();
-          SimpleTest.finish();
-        });
-      });
-    }
-  };
-
-  dbService.beginUpdate(listener, "test-track-simple", "");
-  dbService.beginStream("", "");
-  dbService.updateStream(update);
-  dbService.finishStream();
-  dbService.finishUpdate();
-}
-
 SpecialPowers.pushPrefEnv(
   {"set" : [["urlclassifier.trackingTable", "test-track-simple"],
             ["privacy.trackingprotection.enabled", true],
             ["browser.safebrowsing.malware.enabled", false],
             ["browser.safebrowsing.enabled", false],
             ["channelclassifier.allowlist_example", true]]},
-  function() { doUpdate(testUpdate); });
+  test);
+
+function test() {
+  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
+  UrlClassifierTestUtils.addTestTrackers().then(() => {
+    // Safe Browsing turned OFF, tracking protection should work nevertheless
+    testOnWindow(function(aWindow) {
+      checkLoads(aWindow, true);
+      aWindow.close();
+
+      // Safe Browsing turned ON, tracking protection should still work
+      SpecialPowers.setBoolPref("browser.safebrowsing.enabled", true);
+      testOnWindow(function(aWindow) {
+        checkLoads(aWindow, true);
+        aWindow.close();
+        SimpleTest.finish();
+      });
+    });
+  });
+}
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 </pre>
 <iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
 </body>
--- a/toolkit/components/url-classifier/tests/moz.build
+++ b/toolkit/components/url-classifier/tests/moz.build
@@ -5,16 +5,20 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 MOCHITEST_MANIFESTS += ['mochitest/mochitest.ini']
 MOCHITEST_CHROME_MANIFESTS += ['mochitest/chrome.ini']
 XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini']
 
 JAR_MANIFESTS += ['jar.mn']
 
+TESTING_JS_MODULES += [
+    'UrlClassifierTestUtils.jsm',
+]
+
 #DEFINES['MOZILLA_INTERNAL_API'] = True
 
 # XXX Get this to work in libxul builds.
 ## simple c++ tests (no xpcom)
 #CPP_UNIT_TESTS += [ \
 #    'TestUrlClassifierUtils', \
 #]
 #