test-places-host.js 9.6 KB

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