Bug 1274105 - Refactor classifierHelper.js. r=gcp
authorDimi Lee <dlee@mozilla.com>
Wed, 25 May 2016 20:20:42 +0800
changeset 298919 6926b3233f9c5b40ea0310eb855f3ee7acfcb35c
parent 298918 7932129b6e5c819a8bee07f7c21260296a988d56
child 298920 bb91ab70a57bd2340ac1606b563c7cee5d8c3c76
push id77372
push usergpascutto@mozilla.com
push dateWed, 25 May 2016 13:16:29 +0000
treeherdermozilla-inbound@6926b3233f9c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1274105
milestone49.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 1274105 - Refactor classifierHelper.js. r=gcp MozReview-Commit-ID: ljq4KPzUqu
toolkit/components/url-classifier/tests/mochitest/classifierCommon.js
toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
toolkit/components/url-classifier/tests/mochitest/test_classifier.html
toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html
toolkit/components/url-classifier/tests/mochitest/test_classify_track.html
--- a/toolkit/components/url-classifier/tests/mochitest/classifierCommon.js
+++ b/toolkit/components/url-classifier/tests/mochitest/classifierCommon.js
@@ -11,17 +11,17 @@ function doUpdate(update) {
           iid.equals(Ci.nsIUrlClassifierUpdateObserver))
         return this;
 
       throw Cr.NS_ERROR_NO_INTERFACE;
     },
     updateUrlRequested: function(url) { },
     streamFinished: function(status) { },
     updateError: function(errorCode) {
-      sendAsyncMessage("updateError", { errorCode });
+      sendAsyncMessage("updateError", errorCode);
     },
     updateSuccess: function(requestedTimeout) {
       sendAsyncMessage("updateSuccess");
     }
   };
 
   let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
                   .getService(Ci.nsIUrlClassifierDBService);
--- a/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
+++ b/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
@@ -1,115 +1,137 @@
 if (typeof(classifierHelper) == "undefined") {
   var classifierHelper = {};
 }
 
 const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
-var classifierCommonScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
+var gScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
 
 const ADD_CHUNKNUM = 524;
 const SUB_CHUNKNUM = 523;
 const HASHLEN = 32;
 
 // addUrlToDB & removeUrlFromDB are asynchronous, queue the task to ensure
 // the callback follow correct order.
 classifierHelper._updates = [];
 
 // Keep urls added to database, those urls should be automatically
 // removed after test complete.
 classifierHelper._updatesToCleanup = [];
 
 // Pass { url: ..., db: ... } to add url to database,
 // onsuccess/onerror will be called when update complete.
-classifierHelper.addUrlToDB = function(updateData, onsuccess, onerror) {
-  var testUpdate = "";
-  for (var update of updateData) {
-    var LISTNAME = update.db;
-    var CHUNKDATA = update.url;
-    var CHUNKLEN = CHUNKDATA.length;
+classifierHelper.addUrlToDB = function(updateData) {
+  return new Promise(function(resolve, reject) {
+    var testUpdate = "";
+    for (var update of updateData) {
+      var LISTNAME = update.db;
+      var CHUNKDATA = update.url;
+      var CHUNKLEN = CHUNKDATA.length;
 
-    classifierHelper._updatesToCleanup.push(update);
-    testUpdate +=
-      "n:1000\n" +
-      "i:" + LISTNAME + "\n" +
-      "ad:1\n" +
-      "a:" + ADD_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
-      CHUNKDATA;
-  }
+      classifierHelper._updatesToCleanup.push(update);
+      testUpdate +=
+        "n:1000\n" +
+        "i:" + LISTNAME + "\n" +
+        "ad:1\n" +
+        "a:" + ADD_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
+        CHUNKDATA;
+    }
 
-  classifierHelper._update(testUpdate, onsuccess, onerror);
+    classifierHelper._update(testUpdate, resolve, reject);
+  });
 }
 
 // Pass { url: ..., db: ... } to remove url from database,
 // onsuccess/onerror will be called when update complete.
-classifierHelper.removeUrlFromDB = function(updateData, onsuccess, onerror) {
-  var testUpdate = "";
-  for (var update of updateData) {
-    var LISTNAME = update.db;
-    var CHUNKDATA = ADD_CHUNKNUM + ":" + update.url;
-    var CHUNKLEN = CHUNKDATA.length;
+classifierHelper.removeUrlFromDB = function(updateData) {
+  return new Promise(function(resolve, reject) {
+    var testUpdate = "";
+    for (var update of updateData) {
+      var LISTNAME = update.db;
+      var CHUNKDATA = ADD_CHUNKNUM + ":" + update.url;
+      var CHUNKLEN = CHUNKDATA.length;
+
+      testUpdate +=
+        "n:1000\n" +
+        "i:" + LISTNAME + "\n" +
+        "s:" + SUB_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
+        CHUNKDATA;
+    }
+
+    classifierHelper._updatesToCleanup =
+      classifierHelper._updatesToCleanup.filter((v) => {
+        return updateData.indexOf(v) == -1;
+      });
 
-    testUpdate +=
-      "n:1000\n" +
-      "i:" + LISTNAME + "\n" +
-      "s:" + SUB_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
-      CHUNKDATA;
-  }
+    classifierHelper._update(testUpdate, resolve, reject);
+  });
+};
 
-  classifierHelper._updatesToCleanup =
-    classifierHelper._updatesToCleanup.filter((v) => {
-      return updateData.indexOf(v) == -1;
-    });
+// This API is used to expire all add/sub chunks we have updated
+// by using addUrlToDB and removeUrlFromDB.
+classifierHelper.resetDB = function() {
+  return new Promise(function(resolve, reject) {
+    var testUpdate = "";
+    for (var update of classifierHelper._updatesToCleanup) {
+      if (testUpdate.includes(update.db))
+        continue;
 
-  classifierHelper._update(testUpdate, onsuccess, onerror);
+      testUpdate +=
+        "n:1000\n" +
+        "i:" + update.db + "\n" +
+        "ad:" + ADD_CHUNKNUM + "\n" +
+        "sd:" + SUB_CHUNKNUM + "\n"
+    }
+
+    classifierHelper._update(testUpdate, resolve, reject);
+  });
 };
 
 classifierHelper._update = function(testUpdate, onsuccess, onerror) {
   // Queue the task if there is still an on-going update
   classifierHelper._updates.push({"data": testUpdate,
                                   "onsuccess": onsuccess,
                                   "onerror": onerror});
   if (classifierHelper._updates.length != 1) {
     return;
   }
 
-  classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+  gScript.sendAsyncMessage("doUpdate", { testUpdate });
 };
 
 classifierHelper._updateSuccess = function() {
   var update = classifierHelper._updates.shift();
   update.onsuccess();
 
   if (classifierHelper._updates.length) {
     var testUpdate = classifierHelper._updates[0].data;
-    classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+    gScript.sendAsyncMessage("doUpdate", { testUpdate });
   }
 };
 
 classifierHelper._updateError = function(errorCode) {
   var update = classifierHelper._updates.shift();
   update.onerror(errorCode);
 
   if (classifierHelper._updates.length) {
     var testUpdate = classifierHelper._updates[0].data;
-    classifierCommonScript.sendAsyncMessage("doUpdate", { testUpdate });
+    gScript.sendAsyncMessage("doUpdate", { testUpdate });
   }
 };
 
 classifierHelper._setup = function() {
-  classifierCommonScript.addMessageListener("updateSuccess", classifierHelper._updateSuccess);
-  classifierCommonScript.addMessageListener("updateError", classifierHelper._updateError);
+  gScript.addMessageListener("updateSuccess", classifierHelper._updateSuccess);
+  gScript.addMessageListener("updateError", classifierHelper._updateError);
 
   // cleanup will be called at end of each testcase to remove all the urls added to database.
   SimpleTest.registerCleanupFunction(classifierHelper._cleanup);
 };
 
 classifierHelper._cleanup = function() {
   if (!classifierHelper._updatesToCleanup) {
     return Promise.resolve();
   }
 
-  return new Promise(function(resolve, reject) {
-    classifierHelper.removeUrlFromDB(classifierHelper._updatesToCleanup, resolve, reject);
-  });
+  return classifierHelper.resetDB();
 };
 
 classifierHelper._setup();
--- a/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
@@ -44,17 +44,21 @@ function updateError(errorCode) {
   // Abort test.
   SimpleTest.finish();
 }
 
 SpecialPowers.pushPrefEnv(
   {"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
             ["urlclassifier.phishTable", "test-phish-simple"]]},
   function() {
-    classifierHelper.addUrlToDB(testData, updateSuccess, updateError);
+    classifierHelper.addUrlToDB(testData)
+      .then(updateSuccess)
+      .catch(err => {
+        updateError(err);
+      });
   });
 
 </script>
 
 </pre>
 <iframe id="testFrame" onload=""></iframe>
 </body>
 </html>
--- a/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html
@@ -51,17 +51,21 @@ function updateError(errorCode) {
   // Abort test.
   SimpleTest.finish();
 };
 
 SpecialPowers.pushPrefEnv(
   {"set" : [["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
             ["urlclassifier.phishTable", "test-phish-simple"]]},
   function() {
-    classifierHelper.addUrlToDB(testData, updateSuccess, updateError);
+    classifierHelper.addUrlToDB(testData)
+      .then(updateSuccess)
+      .catch(err => {
+        updateError(err);
+      });
   });
 
 window.addEventListener("message", onmessage, false);
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
--- a/toolkit/components/url-classifier/tests/mochitest/test_classify_track.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classify_track.html
@@ -112,29 +112,28 @@
     });
   }
 
   function cleanup() {
     SpecialPowers.clearUserPref(PREF);
   }
 
   function addMalewareUrlToDB() {
-    return new Promise(function(resolve, reject) {
-      var testData = [
-        { url: "malware.example.com/",
-          db: "test-malware-simple"
-        }
-      ];
+    var testData = [
+      { url: "malware.example.com/",
+        db: "test-malware-simple"
+      }
+    ];
 
-      classifierHelper.addUrlToDB(testData, resolve, function() {
-        ok(false, "Couldn't update classifier. Error code: " + errorCode);
+    return classifierHelper.addUrlToDB(testData)
+      .catch(function(err) {
+        ok(false, "Couldn't update classifier. Error code: " + err);
         // Abort test.
         SimpleTest.finish();
       });
-    });
   }
 
   function runTest() {
     Promise.resolve()
       .then(addMalewareUrlToDB)
       .then(testValidTrack)
       .then(testBlacklistTrackSafebrowsingOff)
       .then(testBlacklistTrackSafebrowsingOn)