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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict'
michael@0 5
michael@0 6 const { Cc, Ci } = require('chrome');
michael@0 7 const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
michael@0 8 getService(Ci.nsINavBookmarksService);
michael@0 9 const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
michael@0 10 getService(Ci.nsINavHistoryService);
michael@0 11 const brsrv = Cc["@mozilla.org/browser/nav-history-service;1"]
michael@0 12 .getService(Ci.nsIBrowserHistory);
michael@0 13 const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
michael@0 14 getService(Ci.nsITaggingService);
michael@0 15 const asyncHistory = Cc['@mozilla.org/browser/history;1'].
michael@0 16 getService(Ci.mozIAsyncHistory);
michael@0 17 const { send } = require('sdk/addon/events');
michael@0 18 const { setTimeout } = require('sdk/timers');
michael@0 19 const { newURI } = require('sdk/url/utils');
michael@0 20 const { defer, all } = require('sdk/core/promise');
michael@0 21 const { once } = require('sdk/system/events');
michael@0 22 const { set } = require('sdk/preferences/service');
michael@0 23 const {
michael@0 24 Bookmark, Group, Separator,
michael@0 25 save, search,
michael@0 26 MENU, TOOLBAR, UNSORTED
michael@0 27 } = require('sdk/places/bookmarks');
michael@0 28
michael@0 29 function invalidResolve (assert) {
michael@0 30 return function (e) {
michael@0 31 assert.fail('Resolve state should not be called: ' + e);
michael@0 32 };
michael@0 33 }
michael@0 34 exports.invalidResolve = invalidResolve;
michael@0 35
michael@0 36 function invalidReject (assert) {
michael@0 37 return function (e) {
michael@0 38 assert.fail('Reject state should not be called: ' + e);
michael@0 39 };
michael@0 40 }
michael@0 41 exports.invalidReject = invalidReject;
michael@0 42
michael@0 43 // Removes all children of group
michael@0 44 function clearBookmarks (group) {
michael@0 45 group
michael@0 46 ? bmsrv.removeFolderChildren(group.id)
michael@0 47 : clearAllBookmarks();
michael@0 48 }
michael@0 49
michael@0 50 function clearAllBookmarks () {
michael@0 51 [MENU, TOOLBAR, UNSORTED].forEach(clearBookmarks);
michael@0 52 }
michael@0 53
michael@0 54 function clearHistory (done) {
michael@0 55 hsrv.removeAllPages();
michael@0 56 once('places-expiration-finished', done);
michael@0 57 }
michael@0 58
michael@0 59 // Cleans bookmarks and history and disables maintanance
michael@0 60 function resetPlaces (done) {
michael@0 61 // Set last maintenance to current time to prevent
michael@0 62 // Places DB maintenance occuring and locking DB
michael@0 63 set('places.database.lastMaintenance', Math.floor(Date.now() / 1000));
michael@0 64 clearAllBookmarks();
michael@0 65 clearHistory(done);
michael@0 66 }
michael@0 67 exports.resetPlaces = resetPlaces;
michael@0 68
michael@0 69 function compareWithHost (assert, item) {
michael@0 70 let id = item.id;
michael@0 71 let type = item.type === 'group' ? bmsrv.TYPE_FOLDER : bmsrv['TYPE_' + item.type.toUpperCase()];
michael@0 72 let url = item.url && !item.url.endsWith('/') ? item.url + '/' : item.url;
michael@0 73
michael@0 74 if (type === bmsrv.TYPE_BOOKMARK) {
michael@0 75 assert.equal(url, bmsrv.getBookmarkURI(id).spec.toString(), 'Matches host url');
michael@0 76 let tags = tagsrv.getTagsForURI(newURI(item.url));
michael@0 77 for (let tag of tags) {
michael@0 78 // Handle both array for raw data and set for instances
michael@0 79 if (Array.isArray(item.tags))
michael@0 80 assert.ok(~item.tags.indexOf(tag), 'has correct tag');
michael@0 81 else
michael@0 82 assert.ok(item.tags.has(tag), 'has correct tag');
michael@0 83 }
michael@0 84 assert.equal(tags.length,
michael@0 85 Array.isArray(item.tags) ? item.tags.length : item.tags.size,
michael@0 86 'matches tag count');
michael@0 87 }
michael@0 88 if (type !== bmsrv.TYPE_SEPARATOR) {
michael@0 89 assert.equal(item.title, bmsrv.getItemTitle(id), 'Matches host title');
michael@0 90 }
michael@0 91 assert.equal(item.index, bmsrv.getItemIndex(id), 'Matches host index');
michael@0 92 assert.equal(item.group.id || item.group, bmsrv.getFolderIdForItem(id), 'Matches host group id');
michael@0 93 assert.equal(type, bmsrv.getItemType(id), 'Matches host type');
michael@0 94 }
michael@0 95 exports.compareWithHost = compareWithHost;
michael@0 96
michael@0 97 function addVisits (urls) {
michael@0 98 var deferred = defer();
michael@0 99 asyncHistory.updatePlaces([].concat(urls).map(createVisit), {
michael@0 100 handleResult: function () {},
michael@0 101 handleError: deferred.reject,
michael@0 102 handleCompletion: deferred.resolve
michael@0 103 });
michael@0 104
michael@0 105 return deferred.promise;
michael@0 106 }
michael@0 107 exports.addVisits = addVisits;
michael@0 108
michael@0 109 function removeVisits (urls) {
michael@0 110 [].concat(urls).map(url => {
michael@0 111 hsrv.removePage(newURI(url));
michael@0 112 });
michael@0 113 }
michael@0 114 exports.removeVisits = removeVisits;
michael@0 115
michael@0 116 // Creates a mozIVisitInfo object
michael@0 117 function createVisit (url) {
michael@0 118 let place = {}
michael@0 119 place.uri = newURI(url);
michael@0 120 place.title = "Test visit for " + place.uri.spec;
michael@0 121 place.visits = [{
michael@0 122 transitionType: hsrv.TRANSITION_LINK,
michael@0 123 visitDate: +(new Date()) * 1000,
michael@0 124 referredURI: undefined
michael@0 125 }];
michael@0 126 return place;
michael@0 127 }
michael@0 128
michael@0 129 function createBookmark (data) {
michael@0 130 data = data || {};
michael@0 131 let item = {
michael@0 132 title: data.title || 'Moz',
michael@0 133 url: data.url || (!data.type || data.type === 'bookmark' ?
michael@0 134 'http://moz.com/' :
michael@0 135 undefined),
michael@0 136 tags: data.tags || (!data.type || data.type === 'bookmark' ?
michael@0 137 ['firefox'] :
michael@0 138 undefined),
michael@0 139 type: data.type || 'bookmark',
michael@0 140 group: data.group
michael@0 141 };
michael@0 142 return send('sdk-places-bookmarks-create', item);
michael@0 143 }
michael@0 144 exports.createBookmark = createBookmark;
michael@0 145
michael@0 146 function createBookmarkItem (data) {
michael@0 147 let deferred = defer();
michael@0 148 data = data || {};
michael@0 149 save({
michael@0 150 title: data.title || 'Moz',
michael@0 151 url: data.url || 'http://moz.com/',
michael@0 152 tags: data.tags || (!data.type || data.type === 'bookmark' ?
michael@0 153 ['firefox'] :
michael@0 154 undefined),
michael@0 155 type: data.type || 'bookmark',
michael@0 156 group: data.group
michael@0 157 }).on('end', function (bookmark) {
michael@0 158 deferred.resolve(bookmark[0]);
michael@0 159 });
michael@0 160 return deferred.promise;
michael@0 161 }
michael@0 162 exports.createBookmarkItem = createBookmarkItem;
michael@0 163
michael@0 164 function createBookmarkTree () {
michael@0 165 let agg = [];
michael@0 166 return createBookmarkItem({ type: 'group', title: 'mozgroup' })
michael@0 167 .then(group => {
michael@0 168 agg.push(group);
michael@0 169 return all([createBookmarkItem({
michael@0 170 title: 'mozilla.com',
michael@0 171 url: 'http://mozilla.com/',
michael@0 172 group: group,
michael@0 173 tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
michael@0 174 }), createBookmarkItem({
michael@0 175 title: 'mozilla.org',
michael@0 176 url: 'http://mozilla.org/',
michael@0 177 group: group,
michael@0 178 tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
michael@0 179 }), createBookmarkItem({
michael@0 180 title: 'firefox',
michael@0 181 url: 'http://firefox.com/',
michael@0 182 group: group,
michael@0 183 tags: ['mozilla', 'firefox', 'browser']
michael@0 184 }), createBookmarkItem({
michael@0 185 title: 'thunderbird',
michael@0 186 url: 'http://mozilla.org/thunderbird/',
michael@0 187 group: group,
michael@0 188 tags: ['mozilla', 'thunderbird', 'email']
michael@0 189 }), createBookmarkItem({
michael@0 190 title: 'moz subfolder',
michael@0 191 group: group,
michael@0 192 type: 'group'
michael@0 193 })
michael@0 194 ]);
michael@0 195 })
michael@0 196 .then(results => {
michael@0 197 agg = agg.concat(results);
michael@0 198 let subfolder = results.filter(item => item.type === 'group')[0];
michael@0 199 return createBookmarkItem({
michael@0 200 title: 'dark javascript secrets',
michael@0 201 url: 'http://w3schools.com',
michael@0 202 group: subfolder,
michael@0 203 tags: []
michael@0 204 });
michael@0 205 }).then(item => {
michael@0 206 agg.push(item);
michael@0 207 return createBookmarkItem(
michael@0 208 { type: 'group', group: MENU, title: 'other stuff' }
michael@0 209 );
michael@0 210 }).then(newGroup => {
michael@0 211 agg.push(newGroup);
michael@0 212 return all([
michael@0 213 createBookmarkItem({
michael@0 214 title: 'mdn',
michael@0 215 url: 'http://developer.mozilla.org/en-US/',
michael@0 216 group: newGroup,
michael@0 217 tags: ['javascript']
michael@0 218 }),
michael@0 219 createBookmarkItem({
michael@0 220 title: 'web audio',
michael@0 221 url: 'http://webaud.io',
michael@0 222 group: newGroup,
michael@0 223 tags: ['javascript', 'web audio']
michael@0 224 }),
michael@0 225 createBookmarkItem({
michael@0 226 title: 'web audio components',
michael@0 227 url: 'http://component.fm',
michael@0 228 group: newGroup,
michael@0 229 tags: ['javascript', 'web audio', 'components']
michael@0 230 })
michael@0 231 ]);
michael@0 232 }).then(results => {
michael@0 233 agg = agg.concat(results);
michael@0 234 return agg;
michael@0 235 });
michael@0 236 }
michael@0 237 exports.createBookmarkTree = createBookmarkTree;

mercurial