Wed, 31 Dec 2014 06:09:35 +0100
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 | module.metadata = { |
michael@0 | 7 | 'engines': { |
michael@0 | 8 | 'Firefox': '*' |
michael@0 | 9 | } |
michael@0 | 10 | }; |
michael@0 | 11 | |
michael@0 | 12 | const { Cc, Ci } = require('chrome'); |
michael@0 | 13 | const { defer, all } = require('sdk/core/promise'); |
michael@0 | 14 | const { setTimeout } = require('sdk/timers'); |
michael@0 | 15 | const { newURI } = require('sdk/url/utils'); |
michael@0 | 16 | const { send } = require('sdk/addon/events'); |
michael@0 | 17 | const { set } = require('sdk/preferences/service'); |
michael@0 | 18 | const { before, after } = require('sdk/test/utils'); |
michael@0 | 19 | |
michael@0 | 20 | require('sdk/places/host/host-bookmarks'); |
michael@0 | 21 | require('sdk/places/host/host-tags'); |
michael@0 | 22 | require('sdk/places/host/host-query'); |
michael@0 | 23 | const { |
michael@0 | 24 | invalidResolve, invalidReject, createTree, |
michael@0 | 25 | compareWithHost, createBookmark, createBookmarkTree, resetPlaces |
michael@0 | 26 | } = require('../places-helper'); |
michael@0 | 27 | |
michael@0 | 28 | const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1']. |
michael@0 | 29 | getService(Ci.nsINavBookmarksService); |
michael@0 | 30 | const hsrv = Cc['@mozilla.org/browser/nav-history-service;1']. |
michael@0 | 31 | getService(Ci.nsINavHistoryService); |
michael@0 | 32 | const tagsrv = Cc['@mozilla.org/browser/tagging-service;1']. |
michael@0 | 33 | getService(Ci.nsITaggingService); |
michael@0 | 34 | |
michael@0 | 35 | exports.testBookmarksCreate = function (assert, done) { |
michael@0 | 36 | let items = [{ |
michael@0 | 37 | title: 'my title', |
michael@0 | 38 | url: 'http://test-places-host.com/testBookmarksCreate/', |
michael@0 | 39 | tags: ['some', 'tags', 'yeah'], |
michael@0 | 40 | type: 'bookmark' |
michael@0 | 41 | }, { |
michael@0 | 42 | title: 'my folder', |
michael@0 | 43 | type: 'group', |
michael@0 | 44 | group: bmsrv.bookmarksMenuFolder |
michael@0 | 45 | }, { |
michael@0 | 46 | type: 'separator', |
michael@0 | 47 | group: bmsrv.unfiledBookmarksFolder |
michael@0 | 48 | }]; |
michael@0 | 49 | |
michael@0 | 50 | all(items.map(function (item) { |
michael@0 | 51 | return send('sdk-places-bookmarks-create', item).then(function (data) { |
michael@0 | 52 | compareWithHost(assert, data); |
michael@0 | 53 | }, invalidReject(assert)); |
michael@0 | 54 | })).then(function () { |
michael@0 | 55 | done(); |
michael@0 | 56 | }, invalidReject(assert)); |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | exports.testBookmarksCreateFail = function (assert, done) { |
michael@0 | 60 | let items = [{ |
michael@0 | 61 | title: 'my title', |
michael@0 | 62 | url: 'not-a-url', |
michael@0 | 63 | type: 'bookmark' |
michael@0 | 64 | }, { |
michael@0 | 65 | type: 'group', |
michael@0 | 66 | group: bmsrv.bookmarksMenuFolder |
michael@0 | 67 | }, { |
michael@0 | 68 | group: bmsrv.unfiledBookmarksFolder |
michael@0 | 69 | }]; |
michael@0 | 70 | all(items.map(function (item) { |
michael@0 | 71 | return send('sdk-places-bookmarks-create', item).then(null, function (reason) { |
michael@0 | 72 | assert.ok(reason, 'bookmark create should fail'); |
michael@0 | 73 | }); |
michael@0 | 74 | })).then(done); |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | exports.testBookmarkLastUpdated = function (assert, done) { |
michael@0 | 78 | let timestamp; |
michael@0 | 79 | let item; |
michael@0 | 80 | createBookmark({ |
michael@0 | 81 | url: 'http://test-places-host.com/testBookmarkLastUpdated' |
michael@0 | 82 | }).then(function (data) { |
michael@0 | 83 | item = data; |
michael@0 | 84 | timestamp = item.updated; |
michael@0 | 85 | return send('sdk-places-bookmarks-last-updated', { id: item.id }); |
michael@0 | 86 | }).then(function (updated) { |
michael@0 | 87 | let { resolve, promise } = defer(); |
michael@0 | 88 | assert.equal(timestamp, updated, 'should return last updated time'); |
michael@0 | 89 | item.title = 'updated mozilla'; |
michael@0 | 90 | setTimeout(() => { |
michael@0 | 91 | resolve(send('sdk-places-bookmarks-save', item)); |
michael@0 | 92 | }, 100); |
michael@0 | 93 | return promise; |
michael@0 | 94 | }).then(function (data) { |
michael@0 | 95 | assert.ok(data.updated > timestamp, 'time has elapsed and updated the updated property'); |
michael@0 | 96 | done(); |
michael@0 | 97 | }); |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | exports.testBookmarkRemove = function (assert, done) { |
michael@0 | 101 | let id; |
michael@0 | 102 | createBookmark({ |
michael@0 | 103 | url: 'http://test-places-host.com/testBookmarkRemove/' |
michael@0 | 104 | }).then(function (data) { |
michael@0 | 105 | id = data.id; |
michael@0 | 106 | compareWithHost(assert, data); // ensure bookmark exists |
michael@0 | 107 | bmsrv.getItemTitle(id); // does not throw an error |
michael@0 | 108 | return send('sdk-places-bookmarks-remove', data); |
michael@0 | 109 | }).then(function () { |
michael@0 | 110 | assert.throws(function () { |
michael@0 | 111 | bmsrv.getItemTitle(id); |
michael@0 | 112 | }, 'item should no longer exist'); |
michael@0 | 113 | done(); |
michael@0 | 114 | }, assert.fail); |
michael@0 | 115 | }; |
michael@0 | 116 | |
michael@0 | 117 | exports.testBookmarkGet = function (assert, done) { |
michael@0 | 118 | let bookmark; |
michael@0 | 119 | createBookmark({ |
michael@0 | 120 | url: 'http://test-places-host.com/testBookmarkGet/' |
michael@0 | 121 | }).then(function (data) { |
michael@0 | 122 | bookmark = data; |
michael@0 | 123 | return send('sdk-places-bookmarks-get', { id: data.id }); |
michael@0 | 124 | }).then(function (data) { |
michael@0 | 125 | 'title url index group updated type tags'.split(' ').map(function (prop) { |
michael@0 | 126 | if (prop === 'tags') { |
michael@0 | 127 | for (let tag of bookmark.tags) { |
michael@0 | 128 | assert.ok(~data.tags.indexOf(tag), |
michael@0 | 129 | 'correctly fetched tag ' + tag); |
michael@0 | 130 | } |
michael@0 | 131 | assert.equal(bookmark.tags.length, data.tags.length, |
michael@0 | 132 | 'same amount of tags'); |
michael@0 | 133 | } |
michael@0 | 134 | else |
michael@0 | 135 | assert.equal(bookmark[prop], data[prop], 'correctly fetched ' + prop); |
michael@0 | 136 | }); |
michael@0 | 137 | done(); |
michael@0 | 138 | }); |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | exports.testTagsTag = function (assert, done) { |
michael@0 | 142 | let url; |
michael@0 | 143 | createBookmark({ |
michael@0 | 144 | url: 'http://test-places-host.com/testTagsTag/', |
michael@0 | 145 | }).then(function (data) { |
michael@0 | 146 | url = data.url; |
michael@0 | 147 | return send('sdk-places-tags-tag', { |
michael@0 | 148 | url: data.url, tags: ['mozzerella', 'foxfire'] |
michael@0 | 149 | }); |
michael@0 | 150 | }).then(function () { |
michael@0 | 151 | let tags = tagsrv.getTagsForURI(newURI(url)); |
michael@0 | 152 | assert.ok(~tags.indexOf('mozzerella'), 'first tag found'); |
michael@0 | 153 | assert.ok(~tags.indexOf('foxfire'), 'second tag found'); |
michael@0 | 154 | assert.ok(~tags.indexOf('firefox'), 'default tag found'); |
michael@0 | 155 | assert.equal(tags.length, 3, 'no extra tags'); |
michael@0 | 156 | done(); |
michael@0 | 157 | }); |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | exports.testTagsUntag = function (assert, done) { |
michael@0 | 161 | let item; |
michael@0 | 162 | createBookmark({ |
michael@0 | 163 | url: 'http://test-places-host.com/testTagsUntag/', |
michael@0 | 164 | tags: ['tag1', 'tag2', 'tag3'] |
michael@0 | 165 | }).then(data => { |
michael@0 | 166 | item = data; |
michael@0 | 167 | return send('sdk-places-tags-untag', { |
michael@0 | 168 | url: item.url, |
michael@0 | 169 | tags: ['tag2', 'firefox'] |
michael@0 | 170 | }); |
michael@0 | 171 | }).then(function () { |
michael@0 | 172 | let tags = tagsrv.getTagsForURI(newURI(item.url)); |
michael@0 | 173 | assert.ok(~tags.indexOf('tag1'), 'first tag persisted'); |
michael@0 | 174 | assert.ok(~tags.indexOf('tag3'), 'second tag persisted'); |
michael@0 | 175 | assert.ok(!~tags.indexOf('firefox'), 'first tag removed'); |
michael@0 | 176 | assert.ok(!~tags.indexOf('tag2'), 'second tag removed'); |
michael@0 | 177 | assert.equal(tags.length, 2, 'no extra tags'); |
michael@0 | 178 | done(); |
michael@0 | 179 | }); |
michael@0 | 180 | }; |
michael@0 | 181 | |
michael@0 | 182 | exports.testTagsGetURLsByTag = function (assert, done) { |
michael@0 | 183 | let item; |
michael@0 | 184 | createBookmark({ |
michael@0 | 185 | url: 'http://test-places-host.com/testTagsGetURLsByTag/' |
michael@0 | 186 | }).then(function (data) { |
michael@0 | 187 | item = data; |
michael@0 | 188 | return send('sdk-places-tags-get-urls-by-tag', { |
michael@0 | 189 | tag: 'firefox' |
michael@0 | 190 | }); |
michael@0 | 191 | }).then(function(urls) { |
michael@0 | 192 | assert.equal(item.url, urls[0], 'returned correct url'); |
michael@0 | 193 | assert.equal(urls.length, 1, 'returned only one url'); |
michael@0 | 194 | done(); |
michael@0 | 195 | }); |
michael@0 | 196 | }; |
michael@0 | 197 | |
michael@0 | 198 | exports.testTagsGetTagsByURL = function (assert, done) { |
michael@0 | 199 | let item; |
michael@0 | 200 | createBookmark({ |
michael@0 | 201 | url: 'http://test-places-host.com/testTagsGetURLsByTag/', |
michael@0 | 202 | tags: ['firefox', 'mozilla', 'metal'] |
michael@0 | 203 | }).then(function (data) { |
michael@0 | 204 | item = data; |
michael@0 | 205 | return send('sdk-places-tags-get-tags-by-url', { |
michael@0 | 206 | url: data.url, |
michael@0 | 207 | }); |
michael@0 | 208 | }).then(function(tags) { |
michael@0 | 209 | assert.ok(~tags.indexOf('firefox'), 'returned first tag'); |
michael@0 | 210 | assert.ok(~tags.indexOf('mozilla'), 'returned second tag'); |
michael@0 | 211 | assert.ok(~tags.indexOf('metal'), 'returned third tag'); |
michael@0 | 212 | assert.equal(tags.length, 3, 'returned all tags'); |
michael@0 | 213 | done(); |
michael@0 | 214 | }); |
michael@0 | 215 | }; |
michael@0 | 216 | |
michael@0 | 217 | exports.testHostQuery = function (assert, done) { |
michael@0 | 218 | all([ |
michael@0 | 219 | createBookmark({ |
michael@0 | 220 | url: 'http://firefox.com/testHostQuery/', |
michael@0 | 221 | tags: ['firefox', 'mozilla'] |
michael@0 | 222 | }), |
michael@0 | 223 | createBookmark({ |
michael@0 | 224 | url: 'http://mozilla.com/testHostQuery/', |
michael@0 | 225 | tags: ['mozilla'] |
michael@0 | 226 | }), |
michael@0 | 227 | createBookmark({ url: 'http://thunderbird.com/testHostQuery/' }) |
michael@0 | 228 | ]).then(data => { |
michael@0 | 229 | return send('sdk-places-query', { |
michael@0 | 230 | queries: { tags: ['mozilla'] }, |
michael@0 | 231 | options: { sortingMode: 6, queryType: 1 } // sort by URI ascending, bookmarks only |
michael@0 | 232 | }); |
michael@0 | 233 | }).then(results => { |
michael@0 | 234 | assert.equal(results.length, 2, 'should only return two'); |
michael@0 | 235 | assert.equal(results[0].url, |
michael@0 | 236 | 'http://mozilla.com/testHostQuery/', 'is sorted by URI asc'); |
michael@0 | 237 | return send('sdk-places-query', { |
michael@0 | 238 | queries: { tags: ['mozilla'] }, |
michael@0 | 239 | options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only |
michael@0 | 240 | }); |
michael@0 | 241 | }).then(results => { |
michael@0 | 242 | assert.equal(results.length, 2, 'should only return two'); |
michael@0 | 243 | assert.equal(results[0].url, |
michael@0 | 244 | 'http://firefox.com/testHostQuery/', 'is sorted by URI desc'); |
michael@0 | 245 | done(); |
michael@0 | 246 | }); |
michael@0 | 247 | }; |
michael@0 | 248 | |
michael@0 | 249 | exports.testHostMultiQuery = function (assert, done) { |
michael@0 | 250 | all([ |
michael@0 | 251 | createBookmark({ |
michael@0 | 252 | url: 'http://firefox.com/testHostMultiQuery/', |
michael@0 | 253 | tags: ['firefox', 'mozilla'] |
michael@0 | 254 | }), |
michael@0 | 255 | createBookmark({ |
michael@0 | 256 | url: 'http://mozilla.com/testHostMultiQuery/', |
michael@0 | 257 | tags: ['mozilla'] |
michael@0 | 258 | }), |
michael@0 | 259 | createBookmark({ url: 'http://thunderbird.com/testHostMultiQuery/' }) |
michael@0 | 260 | ]).then(data => { |
michael@0 | 261 | return send('sdk-places-query', { |
michael@0 | 262 | queries: [{ tags: ['firefox'] }, { uri: 'http://thunderbird.com/testHostMultiQuery/' }], |
michael@0 | 263 | options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only |
michael@0 | 264 | }); |
michael@0 | 265 | }).then(results => { |
michael@0 | 266 | assert.equal(results.length, 2, 'should return 2 results ORing queries'); |
michael@0 | 267 | assert.equal(results[0].url, |
michael@0 | 268 | 'http://firefox.com/testHostMultiQuery/', 'should match URL or tag'); |
michael@0 | 269 | assert.equal(results[1].url, |
michael@0 | 270 | 'http://thunderbird.com/testHostMultiQuery/', 'should match URL or tag'); |
michael@0 | 271 | return send('sdk-places-query', { |
michael@0 | 272 | queries: [{ tags: ['firefox'], url: 'http://mozilla.com/testHostMultiQuery/' }], |
michael@0 | 273 | options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only |
michael@0 | 274 | }); |
michael@0 | 275 | }).then(results => { |
michael@0 | 276 | assert.equal(results.length, 0, 'query props should be AND\'d'); |
michael@0 | 277 | done(); |
michael@0 | 278 | }); |
michael@0 | 279 | }; |
michael@0 | 280 | |
michael@0 | 281 | exports.testGetAllBookmarks = function (assert, done) { |
michael@0 | 282 | createBookmarkTree().then(() => { |
michael@0 | 283 | return send('sdk-places-bookmarks-get-all', {}); |
michael@0 | 284 | }).then(res => { |
michael@0 | 285 | assert.equal(res.length, 8, 'all bookmarks returned'); |
michael@0 | 286 | done(); |
michael@0 | 287 | }, assert.fail); |
michael@0 | 288 | }; |
michael@0 | 289 | |
michael@0 | 290 | exports.testGetAllChildren = function (assert, done) { |
michael@0 | 291 | createBookmarkTree().then(results => { |
michael@0 | 292 | return send('sdk-places-bookmarks-get-children', { |
michael@0 | 293 | id: results.filter(({title}) => title === 'mozgroup')[0].id |
michael@0 | 294 | }); |
michael@0 | 295 | }).then(results => { |
michael@0 | 296 | assert.equal(results.length, 5, |
michael@0 | 297 | 'should return all children and folders at a single depth'); |
michael@0 | 298 | done(); |
michael@0 | 299 | }); |
michael@0 | 300 | }; |
michael@0 | 301 | |
michael@0 | 302 | before(exports, (name, assert, done) => resetPlaces(done)); |
michael@0 | 303 | after(exports, (name, assert, done) => resetPlaces(done)); |