Bug 1265842 - Implement browser.history.deleteAll and deleteRange. r=aswan
authorBob Silverberg <bsilverberg@mozilla.com>
Wed, 27 Apr 2016 16:51:11 -0400
changeset 295945 e02797aae3cea963d11f2bffefe397b1f3159554
parent 295944 ec30b1b889eb1e2ee9c8ba3ab08e62483ef6e6e3
child 296060 7e47812f913f9a374a2577c11aa4608dd7a01e21
push id19056
push usercbook@mozilla.com
push dateWed, 04 May 2016 09:03:35 +0000
treeherderfx-team@e02797aae3ce [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1265842
milestone49.0a1
Bug 1265842 - Implement browser.history.deleteAll and deleteRange. r=aswan MozReview-Commit-ID: 6rcQfPajmfw
browser/components/extensions/ext-history.js
browser/components/extensions/schemas/history.json
browser/components/extensions/test/browser/browser_ext_history.js
--- a/browser/components/extensions/ext-history.js
+++ b/browser/components/extensions/ext-history.js
@@ -7,16 +7,27 @@ const {classes: Cc, interfaces: Ci, util
 XPCOMUtils.defineLazyGetter(this, "History", () => {
   Cu.import("resource://gre/modules/PlacesUtils.jsm");
   return PlacesUtils.history;
 });
 
 extensions.registerSchemaAPI("history", "history", (extension, context) => {
   return {
     history: {
+      deleteAll: function() {
+        return History.clear();
+      },
+      deleteRange: function(filter) {
+        let newFilter = {
+          beginDate: new Date(filter.startTime),
+          endDate: new Date(filter.endTime),
+        };
+        // History.removeVisitsByFilter returns a boolean, but our API should return nothing
+        return History.removeVisitsByFilter(newFilter).then(() => undefined);
+      },
       deleteUrl: function(details) {
         let url = details.url;
         // History.remove returns a boolean, but our API should return nothing
         return History.remove(url).then(() => undefined);
       },
     },
   };
 });
--- a/browser/components/extensions/schemas/history.json
+++ b/browser/components/extensions/schemas/history.json
@@ -219,17 +219,16 @@
             "type": "function",
             "optional": true,
             "parameters": []
           }
         ]
       },
       {
         "name": "deleteRange",
-        "unsupported": true,
         "type": "function",
         "description": "Removes all items within the specified date range from the history.  Pages will not be removed from the history unless all visits fall within the range.",
         "async": "callback",
         "parameters": [
           {
             "name": "range",
             "type": "object",
             "properties": {
@@ -247,17 +246,16 @@
             "name": "callback",
             "type": "function",
             "parameters": []
           }
         ]
       },
       {
         "name": "deleteAll",
-        "unsupported": true,
         "type": "function",
         "description": "Deletes all items from the history.",
         "async": "callback",
         "parameters": [
           {
             "name": "callback",
             "type": "function",
             "parameters": []
--- a/browser/components/extensions/test/browser/browser_ext_history.js
+++ b/browser/components/extensions/test/browser/browser_ext_history.js
@@ -1,58 +1,104 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
 XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
                                   "resource://testing-common/PlacesTestUtils.jsm");
 
-add_task(function* test_history_schema() {
+add_task(function* test_delete() {
   function background() {
-    browser.test.assertTrue(browser.history, "browser.history API exists");
-    browser.test.notifyPass("history-schema");
-  }
-
-  let extension = ExtensionTestUtils.loadExtension({
-    manifest: {
-      permissions: ["history"],
-    },
-    background: `(${background})()`,
-  });
-  yield extension.startup();
-  yield extension.awaitFinish("history-schema");
-  yield extension.unload();
-});
-
-add_task(function* test_delete_url() {
-  const TEST_URL = `http://example.com/${Math.random()}`;
-
-  function background() {
-    browser.test.onMessage.addListener((msg, url) => {
-      browser.history.deleteUrl({url: url}).then(result => {
-        browser.test.assertEq(undefined, result, "browser.history.deleteUrl returns nothing");
-        browser.test.sendMessage("url-deleted");
-      });
+    browser.test.onMessage.addListener((msg, arg) => {
+      if (msg === "delete-url") {
+        browser.history.deleteUrl({url: arg}).then(result => {
+          browser.test.assertEq(undefined, result, "browser.history.deleteUrl returns nothing");
+          browser.test.sendMessage("url-deleted");
+        });
+      } else if (msg === "delete-range") {
+        browser.history.deleteRange(arg).then(result => {
+          browser.test.assertEq(undefined, result, "browser.history.deleteUrl returns nothing");
+          browser.test.sendMessage("range-deleted");
+        });
+      } else if (msg === "delete-all") {
+        browser.history.deleteAll().then(result => {
+          browser.test.assertEq(undefined, result, "browser.history.deleteUrl returns nothing");
+          browser.test.sendMessage("urls-deleted");
+        });
+      }
     });
 
     browser.test.sendMessage("ready");
   }
 
+  const REFERENCE_DATE = new Date(1999, 9, 9, 9, 9);
+  let {PlacesUtils} = Cu.import("resource://gre/modules/PlacesUtils.jsm", {});
+
   let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: ["history"],
     },
     background: `(${background})()`,
   });
 
   yield extension.startup();
   yield PlacesTestUtils.clearHistory();
   yield extension.awaitMessage("ready");
 
-  yield PlacesTestUtils.addVisits(TEST_URL);
-  ok(yield PlacesTestUtils.isPageInDB(TEST_URL), `${TEST_URL} found in history database`);
+  let visits = [];
+
+  // Add 5 visits for one uri and 3 visits for 3 others
+  for (let i = 0; i < 8; ++i) {
+    let baseUri = "http://mozilla.com/test_history/";
+    let uri = (i > 4) ? `${baseUri}${i}/` : baseUri;
+    let dbDate = (Number(REFERENCE_DATE) + 3600 * 1000 * i) * 1000;
+
+    let visit = {
+      uri,
+      title: "visit " + i,
+      visitDate: dbDate,
+    };
+    visits.push(visit);
+  }
+
+  yield PlacesTestUtils.addVisits(visits);
+
+  is(yield PlacesTestUtils.visitsInDB(visits[0].uri), 5, "5 visits for uri found in history database");
+
+  let testUrl = visits[6].uri.spec;
+  ok(yield PlacesTestUtils.isPageInDB(testUrl), "expected url found in history database");
+
+  extension.sendMessage("delete-url", testUrl);
+  yield extension.awaitMessage("url-deleted");
+  is(yield PlacesTestUtils.isPageInDB(testUrl), false, "expected url not found in history database");
 
-  extension.sendMessage("delete-url", TEST_URL);
-  yield extension.awaitMessage("url-deleted");
-  ok(!(yield PlacesTestUtils.isPageInDB(TEST_URL)), `${TEST_URL} not found in history database`);
+  let filter = {
+    startTime: visits[1].visitDate / 1000,
+    endTime: visits[3].visitDate / 1000,
+  };
+
+  extension.sendMessage("delete-range", filter);
+  yield extension.awaitMessage("range-deleted");
+
+  ok(yield PlacesTestUtils.isPageInDB(visits[0].uri), "expected uri found in history database");
+  is(yield PlacesTestUtils.visitsInDB(visits[0].uri), 2, "2 visits for uri found in history database");
+  ok(yield PlacesTestUtils.isPageInDB(visits[5].uri), "expected uri found in history database");
+  is(yield PlacesTestUtils.visitsInDB(visits[5].uri), 1, "1 visit for uri found in history database");
+
+  filter.startTime = visits[0].visitDate / 1000;
+  filter.endTime = visits[5].visitDate / 1000;
+
+  extension.sendMessage("delete-range", filter);
+  yield extension.awaitMessage("range-deleted");
+
+  is(yield PlacesTestUtils.isPageInDB(visits[0].uri), false, "expected uri not found in history database");
+  is(yield PlacesTestUtils.visitsInDB(visits[0].uri), 0, "0 visits for uri found in history database");
+  is(yield PlacesTestUtils.isPageInDB(visits[5].uri), false, "expected uri not found in history database");
+  is(yield PlacesTestUtils.visitsInDB(visits[5].uri), 0, "0 visits for uri found in history database");
+
+  ok(yield PlacesTestUtils.isPageInDB(visits[7].uri), "expected uri found in history database");
+
+  extension.sendMessage("delete-all");
+  yield extension.awaitMessage("urls-deleted");
+  is(PlacesUtils.history.hasHistoryEntries, false, "history is empty");
 
   yield extension.unload();
 });