test-places-events.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 { filter } = require('sdk/event/utils');
  13. const { on, off } = require('sdk/event/core');
  14. const { events } = require('sdk/places/events');
  15. const { setTimeout } = require('sdk/timers');
  16. const { before, after } = require('sdk/test/utils');
  17. const {
  18. search
  19. } = require('sdk/places/history');
  20. const {
  21. invalidResolve, invalidReject, createTree, createBookmark,
  22. compareWithHost, addVisits, resetPlaces, createBookmarkItem,
  23. removeVisits
  24. } = require('../places-helper');
  25. const { save, MENU, UNSORTED } = require('sdk/places/bookmarks');
  26. const { promisedEmitter } = require('sdk/places/utils');
  27. exports['test bookmark-item-added'] = function (assert, done) {
  28. function handler ({type, data}) {
  29. if (type !== 'bookmark-item-added') return;
  30. if (data.title !== 'bookmark-added-title') return;
  31. assert.equal(type, 'bookmark-item-added', 'correct type in bookmark-added event');
  32. assert.equal(data.type, 'bookmark', 'correct data in bookmark-added event');
  33. assert.ok(data.id != null, 'correct data in bookmark-added event');
  34. assert.ok(data.parentId != null, 'correct data in bookmark-added event');
  35. assert.ok(data.index != null, 'correct data in bookmark-added event');
  36. assert.equal(data.url, 'http://moz.com/', 'correct data in bookmark-added event');
  37. assert.ok(data.dateAdded != null, 'correct data in bookmark-added event');
  38. events.off('data', handler);
  39. done();
  40. }
  41. events.on('data', handler);
  42. createBookmark({ title: 'bookmark-added-title' });
  43. };
  44. exports['test bookmark-item-changed'] = function (assert, done) {
  45. let id;
  46. let complete = makeCompleted(done);
  47. function handler ({type, data}) {
  48. if (type !== 'bookmark-item-changed') return;
  49. if (data.id !== id) return;
  50. assert.equal(type, 'bookmark-item-changed',
  51. 'correct type in bookmark-item-changed event');
  52. assert.equal(data.type, 'bookmark',
  53. 'correct data in bookmark-item-changed event');
  54. assert.equal(data.property, 'title',
  55. 'correct property in bookmark-item-changed event');
  56. assert.equal(data.value, 'bookmark-changed-title-2',
  57. 'correct value in bookmark-item-changed event');
  58. assert.ok(data.id === id, 'correct id in bookmark-item-changed event');
  59. assert.ok(data.parentId != null, 'correct data in bookmark-added event');
  60. events.off('data', handler);
  61. complete();
  62. }
  63. events.on('data', handler);
  64. createBookmarkItem({ title: 'bookmark-changed-title' }).then(item => {
  65. id = item.id;
  66. item.title = 'bookmark-changed-title-2';
  67. return saveP(item);
  68. }).then(complete);
  69. };
  70. exports['test bookmark-item-moved'] = function (assert, done) {
  71. let id;
  72. let complete = makeCompleted(done);
  73. function handler ({type, data}) {
  74. if (type !== 'bookmark-item-moved') return;
  75. if (data.id !== id) return;
  76. assert.equal(type, 'bookmark-item-moved',
  77. 'correct type in bookmark-item-moved event');
  78. assert.equal(data.type, 'bookmark',
  79. 'correct data in bookmark-item-moved event');
  80. assert.ok(data.id === id, 'correct id in bookmark-item-moved event');
  81. assert.equal(data.previousParentId, UNSORTED.id,
  82. 'correct previousParentId');
  83. assert.equal(data.currentParentId, MENU.id,
  84. 'correct currentParentId');
  85. assert.equal(data.previousIndex, 0, 'correct previousIndex');
  86. assert.equal(data.currentIndex, 0, 'correct currentIndex');
  87. events.off('data', handler);
  88. complete();
  89. }
  90. events.on('data', handler);
  91. createBookmarkItem({
  92. title: 'bookmark-moved-title',
  93. group: UNSORTED
  94. }).then(item => {
  95. id = item.id;
  96. item.group = MENU;
  97. return saveP(item);
  98. }).then(complete);
  99. };
  100. exports['test bookmark-item-removed'] = function (assert, done) {
  101. let id;
  102. let complete = makeCompleted(done);
  103. function handler ({type, data}) {
  104. if (type !== 'bookmark-item-removed') return;
  105. if (data.id !== id) return;
  106. assert.equal(type, 'bookmark-item-removed',
  107. 'correct type in bookmark-item-removed event');
  108. assert.equal(data.type, 'bookmark',
  109. 'correct data in bookmark-item-removed event');
  110. assert.ok(data.id === id, 'correct id in bookmark-item-removed event');
  111. assert.equal(data.parentId, UNSORTED.id,
  112. 'correct parentId in bookmark-item-removed');
  113. assert.equal(data.url, 'http://moz.com/',
  114. 'correct url in bookmark-item-removed event');
  115. assert.equal(data.index, 0,
  116. 'correct index in bookmark-item-removed event');
  117. events.off('data', handler);
  118. complete();
  119. }
  120. events.on('data', handler);
  121. createBookmarkItem({
  122. title: 'bookmark-item-remove-title',
  123. group: UNSORTED
  124. }).then(item => {
  125. id = item.id;
  126. item.remove = true;
  127. return saveP(item);
  128. }).then(complete);
  129. };
  130. exports['test bookmark-item-visited'] = function (assert, done) {
  131. let id;
  132. let complete = makeCompleted(done);
  133. function handler ({type, data}) {
  134. if (type !== 'bookmark-item-visited') return;
  135. if (data.id !== id) return;
  136. assert.equal(type, 'bookmark-item-visited',
  137. 'correct type in bookmark-item-visited event');
  138. assert.ok(data.id === id, 'correct id in bookmark-item-visited event');
  139. assert.equal(data.parentId, UNSORTED.id,
  140. 'correct parentId in bookmark-item-visited');
  141. assert.ok(data.transitionType != null,
  142. 'has a transition type in bookmark-item-visited event');
  143. assert.ok(data.time != null,
  144. 'has a time in bookmark-item-visited event');
  145. assert.ok(data.visitId != null,
  146. 'has a visitId in bookmark-item-visited event');
  147. assert.equal(data.url, 'http://bookmark-item-visited.com/',
  148. 'correct url in bookmark-item-visited event');
  149. events.off('data', handler);
  150. complete();
  151. }
  152. events.on('data', handler);
  153. createBookmarkItem({
  154. title: 'bookmark-item-visited',
  155. url: 'http://bookmark-item-visited.com/'
  156. }).then(item => {
  157. id = item.id;
  158. return addVisits('http://bookmark-item-visited.com/');
  159. }).then(complete);
  160. };
  161. exports['test history-start-batch, history-end-batch, history-start-clear'] = function (assert, done) {
  162. let complete = makeCompleted(done, 4);
  163. let startEvent = filter(events, ({type}) => type === 'history-start-batch');
  164. let endEvent = filter(events, ({type}) => type === 'history-end-batch');
  165. let clearEvent = filter(events, ({type}) => type === 'history-start-clear');
  166. function startHandler ({type, data}) {
  167. assert.pass('history-start-batch called');
  168. assert.equal(type, 'history-start-batch',
  169. 'history-start-batch has correct type');
  170. off(startEvent, 'data', startHandler);
  171. on(endEvent, 'data', endHandler);
  172. complete();
  173. }
  174. function endHandler ({type, data}) {
  175. assert.pass('history-end-batch called');
  176. assert.equal(type, 'history-end-batch',
  177. 'history-end-batch has correct type');
  178. off(endEvent, 'data', endHandler);
  179. complete();
  180. }
  181. function clearHandler ({type, data}) {
  182. assert.pass('history-start-clear called');
  183. assert.equal(type, 'history-start-clear',
  184. 'history-start-clear has correct type');
  185. off(clearEvent, 'data', clearHandler);
  186. complete();
  187. }
  188. on(startEvent, 'data', startHandler);
  189. on(clearEvent, 'data', clearHandler);
  190. createBookmark().then(() => {
  191. resetPlaces(complete);
  192. })
  193. };
  194. exports['test history-visit, history-title-changed'] = function (assert, done) {
  195. let complete = makeCompleted(() => {
  196. off(titleEvents, 'data', titleHandler);
  197. off(visitEvents, 'data', visitHandler);
  198. done();
  199. }, 6);
  200. let visitEvents = filter(events, ({type}) => type === 'history-visit');
  201. let titleEvents = filter(events, ({type}) => type === 'history-title-changed');
  202. let urls = ['http://moz.com/', 'http://firefox.com/', 'http://mdn.com/'];
  203. function visitHandler ({type, data}) {
  204. assert.equal(type, 'history-visit', 'correct type in history-visit');
  205. assert.ok(~urls.indexOf(data.url), 'history-visit has correct url');
  206. assert.ok(data.visitId != null, 'history-visit has a visitId');
  207. assert.ok(data.time != null, 'history-visit has a time');
  208. assert.ok(data.sessionId != null, 'history-visit has a sessionId');
  209. assert.ok(data.referringId != null, 'history-visit has a referringId');
  210. assert.ok(data.transitionType != null, 'history-visit has a transitionType');
  211. complete();
  212. }
  213. function titleHandler ({type, data}) {
  214. assert.equal(type, 'history-title-changed',
  215. 'correct type in history-title-changed');
  216. assert.ok(~urls.indexOf(data.url),
  217. 'history-title-changed has correct url');
  218. assert.ok(data.title, 'history-title-changed has title');
  219. complete();
  220. }
  221. on(titleEvents, 'data', titleHandler);
  222. on(visitEvents, 'data', visitHandler);
  223. addVisits(urls);
  224. }
  225. exports['test history-delete-url'] = function (assert, done) {
  226. let complete = makeCompleted(() => {
  227. events.off('data', handler);
  228. done();
  229. }, 3);
  230. let urls = ['http://moz.com/', 'http://firefox.com/', 'http://mdn.com/'];
  231. function handler({type, data}) {
  232. if (type !== 'history-delete-url') return;
  233. assert.equal(type, 'history-delete-url',
  234. 'history-delete-url has correct type');
  235. assert.ok(~urls.indexOf(data.url), 'history-delete-url has correct url');
  236. complete();
  237. }
  238. events.on('data', handler);
  239. addVisits(urls).then(() => {
  240. removeVisits(urls);
  241. });
  242. };
  243. exports['test history-page-changed'] = function (assert) {
  244. assert.pass('history-page-changed tested in test-places-favicons');
  245. };
  246. exports['test history-delete-visits'] = function (assert) {
  247. assert.pass('TODO test history-delete-visits');
  248. };
  249. before(exports, (name, assert, done) => resetPlaces(done));
  250. after(exports, (name, assert, done) => resetPlaces(done));
  251. function saveP () {
  252. return promisedEmitter(save.apply(null, Array.slice(arguments)));
  253. }
  254. function makeCompleted (done, countTo) {
  255. let count = 0;
  256. countTo = countTo || 2;
  257. return function () {
  258. if (++count === countTo) done();
  259. };
  260. }