addon-sdk/source/test/addons/places/places-helper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/addons/places/places-helper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,237 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 + 'use strict'
     1.8 +
     1.9 +const { Cc, Ci } = require('chrome');
    1.10 +const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
    1.11 +                    getService(Ci.nsINavBookmarksService);
    1.12 +const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
    1.13 +              getService(Ci.nsINavHistoryService);
    1.14 +const brsrv = Cc["@mozilla.org/browser/nav-history-service;1"]
    1.15 +                     .getService(Ci.nsIBrowserHistory);
    1.16 +const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
    1.17 +              getService(Ci.nsITaggingService);
    1.18 +const asyncHistory = Cc['@mozilla.org/browser/history;1'].
    1.19 +              getService(Ci.mozIAsyncHistory);
    1.20 +const { send } = require('sdk/addon/events');
    1.21 +const { setTimeout } = require('sdk/timers');
    1.22 +const { newURI } = require('sdk/url/utils');
    1.23 +const { defer, all } = require('sdk/core/promise');
    1.24 +const { once } = require('sdk/system/events');
    1.25 +const { set } = require('sdk/preferences/service');
    1.26 +const {
    1.27 +  Bookmark, Group, Separator,
    1.28 +  save, search,
    1.29 +  MENU, TOOLBAR, UNSORTED
    1.30 +} = require('sdk/places/bookmarks');
    1.31 +
    1.32 +function invalidResolve (assert) {
    1.33 +  return function (e) {
    1.34 +    assert.fail('Resolve state should not be called: ' + e);
    1.35 +  };
    1.36 +}
    1.37 +exports.invalidResolve = invalidResolve;
    1.38 +
    1.39 +function invalidReject (assert) {
    1.40 +  return function (e) {
    1.41 +    assert.fail('Reject state should not be called: ' + e);
    1.42 +  };
    1.43 +}
    1.44 +exports.invalidReject = invalidReject;
    1.45 +
    1.46 +// Removes all children of group
    1.47 +function clearBookmarks (group) {
    1.48 +  group
    1.49 +   ? bmsrv.removeFolderChildren(group.id)
    1.50 +   : clearAllBookmarks();
    1.51 +}
    1.52 +
    1.53 +function clearAllBookmarks () {
    1.54 +  [MENU, TOOLBAR, UNSORTED].forEach(clearBookmarks);
    1.55 +}
    1.56 +
    1.57 +function clearHistory (done) {
    1.58 +  hsrv.removeAllPages();
    1.59 +  once('places-expiration-finished', done);
    1.60 +}
    1.61 +
    1.62 +// Cleans bookmarks and history and disables maintanance
    1.63 +function resetPlaces (done) {
    1.64 +  // Set last maintenance to current time to prevent
    1.65 +  // Places DB maintenance occuring and locking DB
    1.66 +  set('places.database.lastMaintenance', Math.floor(Date.now() / 1000));
    1.67 +  clearAllBookmarks();
    1.68 +  clearHistory(done);
    1.69 +}
    1.70 +exports.resetPlaces = resetPlaces;
    1.71 +
    1.72 +function compareWithHost (assert, item) {
    1.73 +  let id = item.id;
    1.74 +  let type = item.type === 'group' ? bmsrv.TYPE_FOLDER : bmsrv['TYPE_' + item.type.toUpperCase()];
    1.75 +  let url = item.url && !item.url.endsWith('/') ? item.url + '/' : item.url;
    1.76 +
    1.77 +  if (type === bmsrv.TYPE_BOOKMARK) {
    1.78 +    assert.equal(url, bmsrv.getBookmarkURI(id).spec.toString(), 'Matches host url');
    1.79 +    let tags = tagsrv.getTagsForURI(newURI(item.url));
    1.80 +    for (let tag of tags) {
    1.81 +      // Handle both array for raw data and set for instances
    1.82 +      if (Array.isArray(item.tags))
    1.83 +        assert.ok(~item.tags.indexOf(tag), 'has correct tag');
    1.84 +      else
    1.85 +        assert.ok(item.tags.has(tag), 'has correct tag');
    1.86 +    }
    1.87 +    assert.equal(tags.length,
    1.88 +      Array.isArray(item.tags) ? item.tags.length : item.tags.size,
    1.89 +      'matches tag count');
    1.90 +  }
    1.91 +  if (type !== bmsrv.TYPE_SEPARATOR) {
    1.92 +    assert.equal(item.title, bmsrv.getItemTitle(id), 'Matches host title');
    1.93 +  }
    1.94 +  assert.equal(item.index, bmsrv.getItemIndex(id), 'Matches host index');
    1.95 +  assert.equal(item.group.id || item.group, bmsrv.getFolderIdForItem(id), 'Matches host group id');
    1.96 +  assert.equal(type, bmsrv.getItemType(id), 'Matches host type');
    1.97 +}
    1.98 +exports.compareWithHost = compareWithHost;
    1.99 +
   1.100 +function addVisits (urls) {
   1.101 +  var deferred = defer();
   1.102 +  asyncHistory.updatePlaces([].concat(urls).map(createVisit), {
   1.103 +    handleResult: function () {},
   1.104 +    handleError: deferred.reject,
   1.105 +    handleCompletion: deferred.resolve
   1.106 +  });
   1.107 +
   1.108 +  return deferred.promise;
   1.109 +}
   1.110 +exports.addVisits = addVisits;
   1.111 +
   1.112 +function removeVisits (urls) {
   1.113 +  [].concat(urls).map(url => {
   1.114 +    hsrv.removePage(newURI(url));
   1.115 +  });
   1.116 +}
   1.117 +exports.removeVisits = removeVisits;
   1.118 +
   1.119 +// Creates a mozIVisitInfo object
   1.120 +function createVisit (url) {
   1.121 +  let place = {}
   1.122 +  place.uri = newURI(url);
   1.123 +  place.title = "Test visit for " + place.uri.spec;
   1.124 +  place.visits = [{
   1.125 +    transitionType: hsrv.TRANSITION_LINK,
   1.126 +    visitDate: +(new Date()) * 1000,
   1.127 +    referredURI: undefined
   1.128 +  }];
   1.129 +  return place;
   1.130 +}
   1.131 +
   1.132 +function createBookmark (data) {
   1.133 +  data = data || {};
   1.134 +  let item = {
   1.135 +    title: data.title || 'Moz',
   1.136 +    url: data.url || (!data.type || data.type === 'bookmark' ?
   1.137 +      'http://moz.com/' :
   1.138 +      undefined),
   1.139 +    tags: data.tags || (!data.type || data.type === 'bookmark' ?
   1.140 +      ['firefox'] :
   1.141 +      undefined),
   1.142 +    type: data.type || 'bookmark',
   1.143 +    group: data.group
   1.144 +  };
   1.145 +  return send('sdk-places-bookmarks-create', item);
   1.146 +}
   1.147 +exports.createBookmark = createBookmark;
   1.148 +
   1.149 +function createBookmarkItem (data) {
   1.150 +  let deferred = defer();
   1.151 +  data = data || {};
   1.152 +  save({
   1.153 +    title: data.title || 'Moz',
   1.154 +    url: data.url || 'http://moz.com/',
   1.155 +    tags: data.tags || (!data.type || data.type === 'bookmark' ?
   1.156 +      ['firefox'] :
   1.157 +      undefined),
   1.158 +    type: data.type || 'bookmark',
   1.159 +    group: data.group
   1.160 +  }).on('end', function (bookmark) {
   1.161 +    deferred.resolve(bookmark[0]);
   1.162 +  });
   1.163 +  return deferred.promise;
   1.164 +}
   1.165 +exports.createBookmarkItem = createBookmarkItem;
   1.166 +
   1.167 +function createBookmarkTree () {
   1.168 +  let agg = [];
   1.169 +  return createBookmarkItem({ type: 'group', title: 'mozgroup' })
   1.170 +    .then(group => {
   1.171 +    agg.push(group);
   1.172 +    return all([createBookmarkItem({
   1.173 +      title: 'mozilla.com',
   1.174 +      url: 'http://mozilla.com/',
   1.175 +      group: group,
   1.176 +      tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
   1.177 +    }), createBookmarkItem({
   1.178 +      title: 'mozilla.org',
   1.179 +      url: 'http://mozilla.org/',
   1.180 +      group: group,
   1.181 +      tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
   1.182 +    }), createBookmarkItem({
   1.183 +      title: 'firefox',
   1.184 +      url: 'http://firefox.com/',
   1.185 +      group: group,
   1.186 +      tags: ['mozilla', 'firefox', 'browser']
   1.187 +    }), createBookmarkItem({
   1.188 +      title: 'thunderbird',
   1.189 +      url: 'http://mozilla.org/thunderbird/',
   1.190 +      group: group,
   1.191 +      tags: ['mozilla', 'thunderbird', 'email']
   1.192 +    }), createBookmarkItem({
   1.193 +      title: 'moz subfolder',
   1.194 +      group: group,
   1.195 +      type: 'group'
   1.196 +    })
   1.197 +    ]);
   1.198 +  })
   1.199 +  .then(results => {
   1.200 +    agg = agg.concat(results);
   1.201 +    let subfolder = results.filter(item => item.type === 'group')[0];
   1.202 +    return createBookmarkItem({
   1.203 +      title: 'dark javascript secrets',
   1.204 +      url: 'http://w3schools.com',
   1.205 +      group: subfolder,
   1.206 +      tags: []
   1.207 +    });
   1.208 +  }).then(item => {
   1.209 +    agg.push(item);
   1.210 +    return createBookmarkItem(
   1.211 +      { type: 'group', group: MENU, title: 'other stuff' }
   1.212 +    );
   1.213 +  }).then(newGroup => {
   1.214 +    agg.push(newGroup);
   1.215 +    return all([
   1.216 +      createBookmarkItem({
   1.217 +        title: 'mdn',
   1.218 +        url: 'http://developer.mozilla.org/en-US/',
   1.219 +        group: newGroup,
   1.220 +        tags: ['javascript']
   1.221 +      }),
   1.222 +      createBookmarkItem({
   1.223 +        title: 'web audio',
   1.224 +        url: 'http://webaud.io',
   1.225 +        group: newGroup,
   1.226 +        tags: ['javascript', 'web audio']
   1.227 +      }),
   1.228 +      createBookmarkItem({
   1.229 +        title: 'web audio components',
   1.230 +        url: 'http://component.fm',
   1.231 +        group: newGroup,
   1.232 +        tags: ['javascript', 'web audio', 'components']
   1.233 +      })
   1.234 +    ]);
   1.235 +  }).then(results => {
   1.236 +    agg = agg.concat(results);
   1.237 +    return agg;
   1.238 +  });
   1.239 +}
   1.240 +exports.createBookmarkTree = createBookmarkTree;

mercurial