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