Bug 1086549 - Converting add-on sdk tests from removePage to History.remove. Patch by Yoric, updated by Standard8. r?mak draft
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Tue, 28 Oct 2014 11:59:51 +0100
changeset 498327 ae6ed8804835329424977564b6d6d3cccf0ff20d
parent 498326 e4f86b041f96e7b604fa714eea06d3d2b3afd645
child 498328 3b3bd1869ba2beddc2aa5fd9fb433cbd9edae814
push id49145
push userbmo:standard8@mozilla.com
push dateTue, 14 Mar 2017 15:05:49 +0000
reviewersmak
bugs1086549
milestone55.0a1
Bug 1086549 - Converting add-on sdk tests from removePage to History.remove. Patch by Yoric, updated by Standard8. r?mak MozReview-Commit-ID: KjIyAi1aUg6
addon-sdk/source/test/addons/places/lib/places-helper.js
addon-sdk/source/test/addons/places/lib/test-places-history.js
--- a/addon-sdk/source/test/addons/places/lib/places-helper.js
+++ b/addon-sdk/source/test/addons/places/lib/places-helper.js
@@ -1,24 +1,14 @@
 /* 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/. */
  'use strict'
 
 const { Cc, Ci, Cu } = require('chrome');
-const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
-                    getService(Ci.nsINavBookmarksService);
-const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
-              getService(Ci.nsINavHistoryService);
-const brsrv = Cc["@mozilla.org/browser/nav-history-service;1"]
-                     .getService(Ci.nsIBrowserHistory);
-const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
-              getService(Ci.nsITaggingService);
-const asyncHistory = Cc['@mozilla.org/browser/history;1'].
-              getService(Ci.mozIAsyncHistory);
 const { send } = require('sdk/addon/events');
 const { setTimeout } = require('sdk/timers');
 const { newURI } = require('sdk/url/utils');
 const { defer, all } = require('sdk/core/promise');
 const { once } = require('sdk/system/events');
 const { set } = require('sdk/preferences/service');
 const {
   Bookmark, Group, Separator,
@@ -36,17 +26,17 @@ function invalidResolve (assert) {
     assert.fail('Resolve state should not be called: ' + e);
   };
 }
 exports.invalidResolve = invalidResolve;
 
 // Removes all children of group
 function clearBookmarks (group) {
   group
-   ? bmsrv.removeFolderChildren(group.id)
+   ? PlacesUtils.bookmarks.removeFolderChildren(group.id)
    : clearAllBookmarks();
 }
 
 function clearAllBookmarks () {
   [MENU, TOOLBAR, UNSORTED].forEach(clearBookmarks);
 }
 
 function clearHistory (done) {
@@ -60,72 +50,75 @@ function resetPlaces (done) {
   set('places.database.lastMaintenance', Math.floor(Date.now() / 1000));
   clearAllBookmarks();
   clearHistory(done);
 }
 exports.resetPlaces = resetPlaces;
 
 function compareWithHost (assert, item) {
   let id = item.id;
-  let type = item.type === 'group' ? bmsrv.TYPE_FOLDER : bmsrv['TYPE_' + item.type.toUpperCase()];
+  let type = item.type === 'group' ?
+    PlacesUtils.bookmarks.TYPE_FOLDER :
+    PlacesUtils.bookmarks['TYPE_' + item.type.toUpperCase()];
   let url = item.url && !item.url.endsWith('/') ? item.url + '/' : item.url;
 
-  if (type === bmsrv.TYPE_BOOKMARK) {
-    assert.equal(url, bmsrv.getBookmarkURI(id).spec.toString(), 'Matches host url');
-    let tags = tagsrv.getTagsForURI(newURI(item.url));
+  if (type === PlacesUtils.bookmarks.TYPE_BOOKMARK) {
+    assert.equal(url, PlacesUtils.bookmarks.getBookmarkURI(id).spec.toString(),
+                 'Matches host url');
+    let tags = PlacesUtils.tagging.getTagsForURI(newURI(item.url));
     for (let tag of tags) {
       // Handle both array for raw data and set for instances
       if (Array.isArray(item.tags))
         assert.ok(~item.tags.indexOf(tag), 'has correct tag');
       else
         assert.ok(item.tags.has(tag), 'has correct tag');
     }
     assert.equal(tags.length,
       Array.isArray(item.tags) ? item.tags.length : item.tags.size,
       'matches tag count');
   }
-  if (type !== bmsrv.TYPE_SEPARATOR) {
-    assert.equal(item.title, bmsrv.getItemTitle(id), 'Matches host title');
+  if (type !== PlacesUtils.bookmarks.TYPE_SEPARATOR) {
+    assert.equal(item.title, PlacesUtils.bookmarks.getItemTitle(id),
+                 'Matches host title');
   }
-  assert.equal(item.index, bmsrv.getItemIndex(id), 'Matches host index');
-  assert.equal(item.group.id || item.group, bmsrv.getFolderIdForItem(id), 'Matches host group id');
-  assert.equal(type, bmsrv.getItemType(id), 'Matches host type');
+  assert.equal(item.index, PlacesUtils.bookmarks.getItemIndex(id),
+               'Matches host index');
+  assert.equal(item.group.id || item.group,
+               PlacesUtils.bookmarks.getFolderIdForItem(id),
+               'Matches host group id');
+  assert.equal(type, PlacesUtils.bookmarks.getItemType(id),
+               'Matches host type');
 }
 exports.compareWithHost = compareWithHost;
 
+/**
+ * Adds visits to places.
+ *
+ * @param {Array|String} urls Either an array of urls to add, or a single url
+ *                            as a string.
+ */
 function addVisits (urls) {
-  var deferred = defer();
-  asyncHistory.updatePlaces([].concat(urls).map(createVisit), {
-    handleResult: function () {},
-    handleError: deferred.reject,
-    handleCompletion: deferred.resolve
-  });
-
-  return deferred.promise;
+  return PlacesUtils.history.insertMany([].concat(urls).map(createVisit));
 }
 exports.addVisits = addVisits;
 
 function removeVisits (urls) {
-  [].concat(urls).map(url => {
-    hsrv.removePage(newURI(url));
-  });
+  PlacesUtils.history.remove(urls);
 }
 exports.removeVisits = removeVisits;
 
 // Creates a mozIVisitInfo object
 function createVisit (url) {
-  let place = {}
-  place.uri = newURI(url);
-  place.title = "Test visit for " + place.uri.spec;
-  place.visits = [{
-    transitionType: hsrv.TRANSITION_LINK,
-    visitDate: +(new Date()) * 1000,
-    referredURI: undefined
-  }];
-  return place;
+  return {
+    url,
+    title: "Test visit for " + url,
+    visits: [{
+      transition: PlacesUtils.history.TRANSITION_LINK
+    }]
+  };
 }
 
 function createBookmark (data) {
   data = data || {};
   let item = {
     title: data.title || 'Moz',
     url: data.url || (!data.type || data.type === 'bookmark' ?
       'http://moz.com/' :
@@ -136,17 +129,17 @@ function createBookmark (data) {
     type: data.type || 'bookmark',
     group: data.group
   };
   return send('sdk-places-bookmarks-create', item);
 }
 exports.createBookmark = createBookmark;
 
 function historyBatch () {
-  hsrv.runInBatchMode(() => {}, null);
+  PlacesUtils.history.runInBatchMode(() => {}, null);
 }
 exports.historyBatch = historyBatch;
 
 function createBookmarkItem (data) {
   let deferred = defer();
   data = data || {};
   save({
     title: data.title || 'Moz',
--- a/addon-sdk/source/test/addons/places/lib/test-places-history.js
+++ b/addon-sdk/source/test/addons/places/lib/test-places-history.js
@@ -31,19 +31,19 @@ exports.testEmptyQuery = function*(asser
   ]);
 
   let results = yield searchP();
   assert.equal(results.length, 2, 'Correct number of entries returned');
   assert.equal(results[0].url, 'http://simplequery-1.com/',
     'matches url');
   assert.equal(results[1].url, 'http://simplequery-2.com/',
     'matches url');
-  assert.equal(results[0].title, 'Test visit for ' + results[0].url,
+  assert.equal(results[0].title, 'Test visit for ' + 'http://simplequery-1.com',
     'title matches');
-  assert.equal(results[1].title, 'Test visit for ' + results[1].url,
+  assert.equal(results[1].title, 'Test visit for ' + 'http://simplequery-2.com',
     'title matches');
   assert.equal(results[0].visitCount, 1, 'matches access');
   assert.equal(results[1].visitCount, 1, 'matches access');
   assert.ok(within(results[0].time), 'accurate access time');
   assert.ok(within(results[1].time), 'accurate access time');
   assert.equal(Object.keys(results[0]).length, 4,
     'no addition exposed properties on history result');
 };