1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/addons/places/tests/test-places-host.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,303 @@ 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 +module.metadata = { 1.10 + 'engines': { 1.11 + 'Firefox': '*' 1.12 + } 1.13 +}; 1.14 + 1.15 +const { Cc, Ci } = require('chrome'); 1.16 +const { defer, all } = require('sdk/core/promise'); 1.17 +const { setTimeout } = require('sdk/timers'); 1.18 +const { newURI } = require('sdk/url/utils'); 1.19 +const { send } = require('sdk/addon/events'); 1.20 +const { set } = require('sdk/preferences/service'); 1.21 +const { before, after } = require('sdk/test/utils'); 1.22 + 1.23 +require('sdk/places/host/host-bookmarks'); 1.24 +require('sdk/places/host/host-tags'); 1.25 +require('sdk/places/host/host-query'); 1.26 +const { 1.27 + invalidResolve, invalidReject, createTree, 1.28 + compareWithHost, createBookmark, createBookmarkTree, resetPlaces 1.29 +} = require('../places-helper'); 1.30 + 1.31 +const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1']. 1.32 + getService(Ci.nsINavBookmarksService); 1.33 +const hsrv = Cc['@mozilla.org/browser/nav-history-service;1']. 1.34 + getService(Ci.nsINavHistoryService); 1.35 +const tagsrv = Cc['@mozilla.org/browser/tagging-service;1']. 1.36 + getService(Ci.nsITaggingService); 1.37 + 1.38 +exports.testBookmarksCreate = function (assert, done) { 1.39 + let items = [{ 1.40 + title: 'my title', 1.41 + url: 'http://test-places-host.com/testBookmarksCreate/', 1.42 + tags: ['some', 'tags', 'yeah'], 1.43 + type: 'bookmark' 1.44 + }, { 1.45 + title: 'my folder', 1.46 + type: 'group', 1.47 + group: bmsrv.bookmarksMenuFolder 1.48 + }, { 1.49 + type: 'separator', 1.50 + group: bmsrv.unfiledBookmarksFolder 1.51 + }]; 1.52 + 1.53 + all(items.map(function (item) { 1.54 + return send('sdk-places-bookmarks-create', item).then(function (data) { 1.55 + compareWithHost(assert, data); 1.56 + }, invalidReject(assert)); 1.57 + })).then(function () { 1.58 + done(); 1.59 + }, invalidReject(assert)); 1.60 +}; 1.61 + 1.62 +exports.testBookmarksCreateFail = function (assert, done) { 1.63 + let items = [{ 1.64 + title: 'my title', 1.65 + url: 'not-a-url', 1.66 + type: 'bookmark' 1.67 + }, { 1.68 + type: 'group', 1.69 + group: bmsrv.bookmarksMenuFolder 1.70 + }, { 1.71 + group: bmsrv.unfiledBookmarksFolder 1.72 + }]; 1.73 + all(items.map(function (item) { 1.74 + return send('sdk-places-bookmarks-create', item).then(null, function (reason) { 1.75 + assert.ok(reason, 'bookmark create should fail'); 1.76 + }); 1.77 + })).then(done); 1.78 +}; 1.79 + 1.80 +exports.testBookmarkLastUpdated = function (assert, done) { 1.81 + let timestamp; 1.82 + let item; 1.83 + createBookmark({ 1.84 + url: 'http://test-places-host.com/testBookmarkLastUpdated' 1.85 + }).then(function (data) { 1.86 + item = data; 1.87 + timestamp = item.updated; 1.88 + return send('sdk-places-bookmarks-last-updated', { id: item.id }); 1.89 + }).then(function (updated) { 1.90 + let { resolve, promise } = defer(); 1.91 + assert.equal(timestamp, updated, 'should return last updated time'); 1.92 + item.title = 'updated mozilla'; 1.93 + setTimeout(() => { 1.94 + resolve(send('sdk-places-bookmarks-save', item)); 1.95 + }, 100); 1.96 + return promise; 1.97 + }).then(function (data) { 1.98 + assert.ok(data.updated > timestamp, 'time has elapsed and updated the updated property'); 1.99 + done(); 1.100 + }); 1.101 +}; 1.102 + 1.103 +exports.testBookmarkRemove = function (assert, done) { 1.104 + let id; 1.105 + createBookmark({ 1.106 + url: 'http://test-places-host.com/testBookmarkRemove/' 1.107 + }).then(function (data) { 1.108 + id = data.id; 1.109 + compareWithHost(assert, data); // ensure bookmark exists 1.110 + bmsrv.getItemTitle(id); // does not throw an error 1.111 + return send('sdk-places-bookmarks-remove', data); 1.112 + }).then(function () { 1.113 + assert.throws(function () { 1.114 + bmsrv.getItemTitle(id); 1.115 + }, 'item should no longer exist'); 1.116 + done(); 1.117 + }, assert.fail); 1.118 +}; 1.119 + 1.120 +exports.testBookmarkGet = function (assert, done) { 1.121 + let bookmark; 1.122 + createBookmark({ 1.123 + url: 'http://test-places-host.com/testBookmarkGet/' 1.124 + }).then(function (data) { 1.125 + bookmark = data; 1.126 + return send('sdk-places-bookmarks-get', { id: data.id }); 1.127 + }).then(function (data) { 1.128 + 'title url index group updated type tags'.split(' ').map(function (prop) { 1.129 + if (prop === 'tags') { 1.130 + for (let tag of bookmark.tags) { 1.131 + assert.ok(~data.tags.indexOf(tag), 1.132 + 'correctly fetched tag ' + tag); 1.133 + } 1.134 + assert.equal(bookmark.tags.length, data.tags.length, 1.135 + 'same amount of tags'); 1.136 + } 1.137 + else 1.138 + assert.equal(bookmark[prop], data[prop], 'correctly fetched ' + prop); 1.139 + }); 1.140 + done(); 1.141 + }); 1.142 +}; 1.143 + 1.144 +exports.testTagsTag = function (assert, done) { 1.145 + let url; 1.146 + createBookmark({ 1.147 + url: 'http://test-places-host.com/testTagsTag/', 1.148 + }).then(function (data) { 1.149 + url = data.url; 1.150 + return send('sdk-places-tags-tag', { 1.151 + url: data.url, tags: ['mozzerella', 'foxfire'] 1.152 + }); 1.153 + }).then(function () { 1.154 + let tags = tagsrv.getTagsForURI(newURI(url)); 1.155 + assert.ok(~tags.indexOf('mozzerella'), 'first tag found'); 1.156 + assert.ok(~tags.indexOf('foxfire'), 'second tag found'); 1.157 + assert.ok(~tags.indexOf('firefox'), 'default tag found'); 1.158 + assert.equal(tags.length, 3, 'no extra tags'); 1.159 + done(); 1.160 + }); 1.161 +}; 1.162 + 1.163 +exports.testTagsUntag = function (assert, done) { 1.164 + let item; 1.165 + createBookmark({ 1.166 + url: 'http://test-places-host.com/testTagsUntag/', 1.167 + tags: ['tag1', 'tag2', 'tag3'] 1.168 + }).then(data => { 1.169 + item = data; 1.170 + return send('sdk-places-tags-untag', { 1.171 + url: item.url, 1.172 + tags: ['tag2', 'firefox'] 1.173 + }); 1.174 + }).then(function () { 1.175 + let tags = tagsrv.getTagsForURI(newURI(item.url)); 1.176 + assert.ok(~tags.indexOf('tag1'), 'first tag persisted'); 1.177 + assert.ok(~tags.indexOf('tag3'), 'second tag persisted'); 1.178 + assert.ok(!~tags.indexOf('firefox'), 'first tag removed'); 1.179 + assert.ok(!~tags.indexOf('tag2'), 'second tag removed'); 1.180 + assert.equal(tags.length, 2, 'no extra tags'); 1.181 + done(); 1.182 + }); 1.183 +}; 1.184 + 1.185 +exports.testTagsGetURLsByTag = function (assert, done) { 1.186 + let item; 1.187 + createBookmark({ 1.188 + url: 'http://test-places-host.com/testTagsGetURLsByTag/' 1.189 + }).then(function (data) { 1.190 + item = data; 1.191 + return send('sdk-places-tags-get-urls-by-tag', { 1.192 + tag: 'firefox' 1.193 + }); 1.194 + }).then(function(urls) { 1.195 + assert.equal(item.url, urls[0], 'returned correct url'); 1.196 + assert.equal(urls.length, 1, 'returned only one url'); 1.197 + done(); 1.198 + }); 1.199 +}; 1.200 + 1.201 +exports.testTagsGetTagsByURL = function (assert, done) { 1.202 + let item; 1.203 + createBookmark({ 1.204 + url: 'http://test-places-host.com/testTagsGetURLsByTag/', 1.205 + tags: ['firefox', 'mozilla', 'metal'] 1.206 + }).then(function (data) { 1.207 + item = data; 1.208 + return send('sdk-places-tags-get-tags-by-url', { 1.209 + url: data.url, 1.210 + }); 1.211 + }).then(function(tags) { 1.212 + assert.ok(~tags.indexOf('firefox'), 'returned first tag'); 1.213 + assert.ok(~tags.indexOf('mozilla'), 'returned second tag'); 1.214 + assert.ok(~tags.indexOf('metal'), 'returned third tag'); 1.215 + assert.equal(tags.length, 3, 'returned all tags'); 1.216 + done(); 1.217 + }); 1.218 +}; 1.219 + 1.220 +exports.testHostQuery = function (assert, done) { 1.221 + all([ 1.222 + createBookmark({ 1.223 + url: 'http://firefox.com/testHostQuery/', 1.224 + tags: ['firefox', 'mozilla'] 1.225 + }), 1.226 + createBookmark({ 1.227 + url: 'http://mozilla.com/testHostQuery/', 1.228 + tags: ['mozilla'] 1.229 + }), 1.230 + createBookmark({ url: 'http://thunderbird.com/testHostQuery/' }) 1.231 + ]).then(data => { 1.232 + return send('sdk-places-query', { 1.233 + queries: { tags: ['mozilla'] }, 1.234 + options: { sortingMode: 6, queryType: 1 } // sort by URI ascending, bookmarks only 1.235 + }); 1.236 + }).then(results => { 1.237 + assert.equal(results.length, 2, 'should only return two'); 1.238 + assert.equal(results[0].url, 1.239 + 'http://mozilla.com/testHostQuery/', 'is sorted by URI asc'); 1.240 + return send('sdk-places-query', { 1.241 + queries: { tags: ['mozilla'] }, 1.242 + options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only 1.243 + }); 1.244 + }).then(results => { 1.245 + assert.equal(results.length, 2, 'should only return two'); 1.246 + assert.equal(results[0].url, 1.247 + 'http://firefox.com/testHostQuery/', 'is sorted by URI desc'); 1.248 + done(); 1.249 + }); 1.250 +}; 1.251 + 1.252 +exports.testHostMultiQuery = function (assert, done) { 1.253 + all([ 1.254 + createBookmark({ 1.255 + url: 'http://firefox.com/testHostMultiQuery/', 1.256 + tags: ['firefox', 'mozilla'] 1.257 + }), 1.258 + createBookmark({ 1.259 + url: 'http://mozilla.com/testHostMultiQuery/', 1.260 + tags: ['mozilla'] 1.261 + }), 1.262 + createBookmark({ url: 'http://thunderbird.com/testHostMultiQuery/' }) 1.263 + ]).then(data => { 1.264 + return send('sdk-places-query', { 1.265 + queries: [{ tags: ['firefox'] }, { uri: 'http://thunderbird.com/testHostMultiQuery/' }], 1.266 + options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only 1.267 + }); 1.268 + }).then(results => { 1.269 + assert.equal(results.length, 2, 'should return 2 results ORing queries'); 1.270 + assert.equal(results[0].url, 1.271 + 'http://firefox.com/testHostMultiQuery/', 'should match URL or tag'); 1.272 + assert.equal(results[1].url, 1.273 + 'http://thunderbird.com/testHostMultiQuery/', 'should match URL or tag'); 1.274 + return send('sdk-places-query', { 1.275 + queries: [{ tags: ['firefox'], url: 'http://mozilla.com/testHostMultiQuery/' }], 1.276 + options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only 1.277 + }); 1.278 + }).then(results => { 1.279 + assert.equal(results.length, 0, 'query props should be AND\'d'); 1.280 + done(); 1.281 + }); 1.282 +}; 1.283 + 1.284 +exports.testGetAllBookmarks = function (assert, done) { 1.285 + createBookmarkTree().then(() => { 1.286 + return send('sdk-places-bookmarks-get-all', {}); 1.287 + }).then(res => { 1.288 + assert.equal(res.length, 8, 'all bookmarks returned'); 1.289 + done(); 1.290 + }, assert.fail); 1.291 +}; 1.292 + 1.293 +exports.testGetAllChildren = function (assert, done) { 1.294 + createBookmarkTree().then(results => { 1.295 + return send('sdk-places-bookmarks-get-children', { 1.296 + id: results.filter(({title}) => title === 'mozgroup')[0].id 1.297 + }); 1.298 + }).then(results => { 1.299 + assert.equal(results.length, 5, 1.300 + 'should return all children and folders at a single depth'); 1.301 + done(); 1.302 + }); 1.303 +}; 1.304 + 1.305 +before(exports, (name, assert, done) => resetPlaces(done)); 1.306 +after(exports, (name, assert, done) => resetPlaces(done));