addon-sdk/source/test/addons/places/tests/test-places-events.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 { filter } = require('sdk/event/utils');
michael@0 15 const { on, off } = require('sdk/event/core');
michael@0 16 const { events } = require('sdk/places/events');
michael@0 17 const { setTimeout } = require('sdk/timers');
michael@0 18 const { before, after } = require('sdk/test/utils');
michael@0 19 const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
michael@0 20 getService(Ci.nsINavBookmarksService);
michael@0 21 const { release, platform } = require('node/os');
michael@0 22
michael@0 23 const isOSX10_6 = (() => {
michael@0 24 let vString = release();
michael@0 25 return vString && /darwin/.test(platform()) && /10\.6/.test(vString);
michael@0 26 })();
michael@0 27
michael@0 28 const {
michael@0 29 search
michael@0 30 } = require('sdk/places/history');
michael@0 31 const {
michael@0 32 invalidResolve, invalidReject, createTree, createBookmark,
michael@0 33 compareWithHost, addVisits, resetPlaces, createBookmarkItem,
michael@0 34 removeVisits
michael@0 35 } = require('../places-helper');
michael@0 36 const { save, MENU, UNSORTED } = require('sdk/places/bookmarks');
michael@0 37 const { promisedEmitter } = require('sdk/places/utils');
michael@0 38
michael@0 39 exports['test bookmark-item-added'] = function (assert, done) {
michael@0 40 function handler ({type, data}) {
michael@0 41 if (type !== 'bookmark-item-added') return;
michael@0 42 if (data.title !== 'bookmark-added-title') return;
michael@0 43
michael@0 44 assert.equal(type, 'bookmark-item-added', 'correct type in bookmark-added event');
michael@0 45 assert.equal(data.type, 'bookmark', 'correct data in bookmark-added event');
michael@0 46 assert.ok(data.id != null, 'correct data in bookmark-added event');
michael@0 47 assert.ok(data.parentId != null, 'correct data in bookmark-added event');
michael@0 48 assert.ok(data.index != null, 'correct data in bookmark-added event');
michael@0 49 assert.equal(data.url, 'http://moz.com/', 'correct data in bookmark-added event');
michael@0 50 assert.ok(data.dateAdded != null, 'correct data in bookmark-added event');
michael@0 51 events.off('data', handler);
michael@0 52 done();
michael@0 53 }
michael@0 54 events.on('data', handler);
michael@0 55 createBookmark({ title: 'bookmark-added-title' });
michael@0 56 };
michael@0 57
michael@0 58 exports['test bookmark-item-changed'] = function (assert, done) {
michael@0 59 let id;
michael@0 60 let complete = makeCompleted(done);
michael@0 61
michael@0 62 // Due to bug 969616 and bug 971964, disabling tests in 10.6 (happens only
michael@0 63 // in debug builds) to prevent intermittent failures
michael@0 64 if (isOSX10_6) {
michael@0 65 assert.ok(true, 'skipping test in OSX 10.6');
michael@0 66 return done();
michael@0 67 }
michael@0 68
michael@0 69 function handler ({type, data}) {
michael@0 70 if (type !== 'bookmark-item-changed') return;
michael@0 71 if (data.id !== id) return;
michael@0 72 // Abort if the 'bookmark-item-changed' event isn't for the `title` property,
michael@0 73 // as sometimes the event can be for the `url` property.
michael@0 74 // Intermittent failure, bug 969616
michael@0 75 if (data.property !== 'title') return;
michael@0 76
michael@0 77 assert.equal(type, 'bookmark-item-changed',
michael@0 78 'correct type in bookmark-item-changed event');
michael@0 79 assert.equal(data.type, 'bookmark',
michael@0 80 'correct data in bookmark-item-changed event');
michael@0 81 assert.equal(data.property, 'title',
michael@0 82 'correct property in bookmark-item-changed event');
michael@0 83 assert.equal(data.value, 'bookmark-changed-title-2',
michael@0 84 'correct value in bookmark-item-changed event');
michael@0 85 assert.ok(data.id === id, 'correct id in bookmark-item-changed event');
michael@0 86 assert.ok(data.parentId != null, 'correct data in bookmark-added event');
michael@0 87
michael@0 88 events.off('data', handler);
michael@0 89 complete();
michael@0 90 }
michael@0 91 events.on('data', handler);
michael@0 92
michael@0 93 createBookmarkItem({ title: 'bookmark-changed-title' }).then(item => {
michael@0 94 id = item.id;
michael@0 95 item.title = 'bookmark-changed-title-2';
michael@0 96 return saveP(item);
michael@0 97 }).then(complete);
michael@0 98 };
michael@0 99
michael@0 100 exports['test bookmark-item-moved'] = function (assert, done) {
michael@0 101 let id;
michael@0 102 let complete = makeCompleted(done);
michael@0 103 let previousIndex, previousParentId;
michael@0 104
michael@0 105 // Due to bug 969616 and bug 971964, disabling tests in 10.6 (happens only
michael@0 106 // in debug builds) to prevent intermittent failures
michael@0 107 if (isOSX10_6) {
michael@0 108 assert.ok(true, 'skipping test in OSX 10.6');
michael@0 109 return done();
michael@0 110 }
michael@0 111
michael@0 112 function handler ({type, data}) {
michael@0 113 if (type !== 'bookmark-item-moved') return;
michael@0 114 if (data.id !== id) return;
michael@0 115 assert.equal(type, 'bookmark-item-moved',
michael@0 116 'correct type in bookmark-item-moved event');
michael@0 117 assert.equal(data.type, 'bookmark',
michael@0 118 'correct data in bookmark-item-moved event');
michael@0 119 assert.ok(data.id === id, 'correct id in bookmark-item-moved event');
michael@0 120 assert.equal(data.previousParentId, previousParentId,
michael@0 121 'correct previousParentId');
michael@0 122 assert.equal(data.currentParentId, bmsrv.getFolderIdForItem(id),
michael@0 123 'correct currentParentId');
michael@0 124 assert.equal(data.previousIndex, previousIndex, 'correct previousIndex');
michael@0 125 assert.equal(data.currentIndex, bmsrv.getItemIndex(id), 'correct currentIndex');
michael@0 126
michael@0 127 events.off('data', handler);
michael@0 128 complete();
michael@0 129 }
michael@0 130 events.on('data', handler);
michael@0 131
michael@0 132 createBookmarkItem({
michael@0 133 title: 'bookmark-moved-title',
michael@0 134 group: UNSORTED
michael@0 135 }).then(item => {
michael@0 136 id = item.id;
michael@0 137 previousIndex = bmsrv.getItemIndex(id);
michael@0 138 previousParentId = bmsrv.getFolderIdForItem(id);
michael@0 139 item.group = MENU;
michael@0 140 return saveP(item);
michael@0 141 }).then(complete);
michael@0 142 };
michael@0 143
michael@0 144 exports['test bookmark-item-removed'] = function (assert, done) {
michael@0 145 let id;
michael@0 146 let complete = makeCompleted(done);
michael@0 147 function handler ({type, data}) {
michael@0 148 if (type !== 'bookmark-item-removed') return;
michael@0 149 if (data.id !== id) return;
michael@0 150 assert.equal(type, 'bookmark-item-removed',
michael@0 151 'correct type in bookmark-item-removed event');
michael@0 152 assert.equal(data.type, 'bookmark',
michael@0 153 'correct data in bookmark-item-removed event');
michael@0 154 assert.ok(data.id === id, 'correct id in bookmark-item-removed event');
michael@0 155 assert.equal(data.parentId, UNSORTED.id,
michael@0 156 'correct parentId in bookmark-item-removed');
michael@0 157 assert.equal(data.url, 'http://moz.com/',
michael@0 158 'correct url in bookmark-item-removed event');
michael@0 159 assert.equal(data.index, 0,
michael@0 160 'correct index in bookmark-item-removed event');
michael@0 161
michael@0 162 events.off('data', handler);
michael@0 163 complete();
michael@0 164 }
michael@0 165 events.on('data', handler);
michael@0 166
michael@0 167 createBookmarkItem({
michael@0 168 title: 'bookmark-item-remove-title',
michael@0 169 group: UNSORTED
michael@0 170 }).then(item => {
michael@0 171 id = item.id;
michael@0 172 item.remove = true;
michael@0 173 return saveP(item);
michael@0 174 }).then(complete);
michael@0 175 };
michael@0 176
michael@0 177 exports['test bookmark-item-visited'] = function (assert, done) {
michael@0 178 let id;
michael@0 179 let complete = makeCompleted(done);
michael@0 180 function handler ({type, data}) {
michael@0 181 if (type !== 'bookmark-item-visited') return;
michael@0 182 if (data.id !== id) return;
michael@0 183 assert.equal(type, 'bookmark-item-visited',
michael@0 184 'correct type in bookmark-item-visited event');
michael@0 185 assert.ok(data.id === id, 'correct id in bookmark-item-visited event');
michael@0 186 assert.equal(data.parentId, UNSORTED.id,
michael@0 187 'correct parentId in bookmark-item-visited');
michael@0 188 assert.ok(data.transitionType != null,
michael@0 189 'has a transition type in bookmark-item-visited event');
michael@0 190 assert.ok(data.time != null,
michael@0 191 'has a time in bookmark-item-visited event');
michael@0 192 assert.ok(data.visitId != null,
michael@0 193 'has a visitId in bookmark-item-visited event');
michael@0 194 assert.equal(data.url, 'http://bookmark-item-visited.com/',
michael@0 195 'correct url in bookmark-item-visited event');
michael@0 196
michael@0 197 events.off('data', handler);
michael@0 198 complete();
michael@0 199 }
michael@0 200 events.on('data', handler);
michael@0 201
michael@0 202 createBookmarkItem({
michael@0 203 title: 'bookmark-item-visited',
michael@0 204 url: 'http://bookmark-item-visited.com/'
michael@0 205 }).then(item => {
michael@0 206 id = item.id;
michael@0 207 return addVisits('http://bookmark-item-visited.com/');
michael@0 208 }).then(complete);
michael@0 209 };
michael@0 210
michael@0 211 exports['test history-start-batch, history-end-batch, history-start-clear'] = function (assert, done) {
michael@0 212 let complete = makeCompleted(done, 4);
michael@0 213 let startEvent = filter(events, ({type}) => type === 'history-start-batch');
michael@0 214 let endEvent = filter(events, ({type}) => type === 'history-end-batch');
michael@0 215 let clearEvent = filter(events, ({type}) => type === 'history-start-clear');
michael@0 216 function startHandler ({type, data}) {
michael@0 217 assert.pass('history-start-batch called');
michael@0 218 assert.equal(type, 'history-start-batch',
michael@0 219 'history-start-batch has correct type');
michael@0 220 off(startEvent, 'data', startHandler);
michael@0 221 on(endEvent, 'data', endHandler);
michael@0 222 complete();
michael@0 223 }
michael@0 224 function endHandler ({type, data}) {
michael@0 225 assert.pass('history-end-batch called');
michael@0 226 assert.equal(type, 'history-end-batch',
michael@0 227 'history-end-batch has correct type');
michael@0 228 off(endEvent, 'data', endHandler);
michael@0 229 complete();
michael@0 230 }
michael@0 231 function clearHandler ({type, data}) {
michael@0 232 assert.pass('history-start-clear called');
michael@0 233 assert.equal(type, 'history-start-clear',
michael@0 234 'history-start-clear has correct type');
michael@0 235 off(clearEvent, 'data', clearHandler);
michael@0 236 complete();
michael@0 237 }
michael@0 238
michael@0 239 on(startEvent, 'data', startHandler);
michael@0 240 on(clearEvent, 'data', clearHandler);
michael@0 241
michael@0 242 createBookmark().then(() => {
michael@0 243 resetPlaces(complete);
michael@0 244 })
michael@0 245 };
michael@0 246
michael@0 247 exports['test history-visit, history-title-changed'] = function (assert, done) {
michael@0 248 let complete = makeCompleted(() => {
michael@0 249 off(titleEvents, 'data', titleHandler);
michael@0 250 off(visitEvents, 'data', visitHandler);
michael@0 251 done();
michael@0 252 }, 6);
michael@0 253 let visitEvents = filter(events, ({type}) => type === 'history-visit');
michael@0 254 let titleEvents = filter(events, ({type}) => type === 'history-title-changed');
michael@0 255
michael@0 256 let urls = ['http://moz.com/', 'http://firefox.com/', 'http://mdn.com/'];
michael@0 257
michael@0 258 function visitHandler ({type, data}) {
michael@0 259 assert.equal(type, 'history-visit', 'correct type in history-visit');
michael@0 260 assert.ok(~urls.indexOf(data.url), 'history-visit has correct url');
michael@0 261 assert.ok(data.visitId != null, 'history-visit has a visitId');
michael@0 262 assert.ok(data.time != null, 'history-visit has a time');
michael@0 263 assert.ok(data.sessionId != null, 'history-visit has a sessionId');
michael@0 264 assert.ok(data.referringId != null, 'history-visit has a referringId');
michael@0 265 assert.ok(data.transitionType != null, 'history-visit has a transitionType');
michael@0 266 complete();
michael@0 267 }
michael@0 268
michael@0 269 function titleHandler ({type, data}) {
michael@0 270 assert.equal(type, 'history-title-changed',
michael@0 271 'correct type in history-title-changed');
michael@0 272 assert.ok(~urls.indexOf(data.url),
michael@0 273 'history-title-changed has correct url');
michael@0 274 assert.ok(data.title, 'history-title-changed has title');
michael@0 275 complete();
michael@0 276 }
michael@0 277
michael@0 278 on(titleEvents, 'data', titleHandler);
michael@0 279 on(visitEvents, 'data', visitHandler);
michael@0 280 addVisits(urls);
michael@0 281 }
michael@0 282
michael@0 283 exports['test history-delete-url'] = function (assert, done) {
michael@0 284 let complete = makeCompleted(() => {
michael@0 285 events.off('data', handler);
michael@0 286 done();
michael@0 287 }, 3);
michael@0 288 let urls = ['http://moz.com/', 'http://firefox.com/', 'http://mdn.com/'];
michael@0 289 function handler({type, data}) {
michael@0 290 if (type !== 'history-delete-url') return;
michael@0 291 assert.equal(type, 'history-delete-url',
michael@0 292 'history-delete-url has correct type');
michael@0 293 assert.ok(~urls.indexOf(data.url), 'history-delete-url has correct url');
michael@0 294 complete();
michael@0 295 }
michael@0 296
michael@0 297 events.on('data', handler);
michael@0 298 addVisits(urls).then(() => {
michael@0 299 removeVisits(urls);
michael@0 300 });
michael@0 301 };
michael@0 302
michael@0 303 exports['test history-page-changed'] = function (assert) {
michael@0 304 assert.pass('history-page-changed tested in test-places-favicons');
michael@0 305 };
michael@0 306
michael@0 307 exports['test history-delete-visits'] = function (assert) {
michael@0 308 assert.pass('TODO test history-delete-visits');
michael@0 309 };
michael@0 310
michael@0 311 before(exports, (name, assert, done) => resetPlaces(done));
michael@0 312 after(exports, (name, assert, done) => resetPlaces(done));
michael@0 313
michael@0 314 function saveP () {
michael@0 315 return promisedEmitter(save.apply(null, Array.slice(arguments)));
michael@0 316 }
michael@0 317
michael@0 318 function makeCompleted (done, countTo) {
michael@0 319 let count = 0;
michael@0 320 countTo = countTo || 2;
michael@0 321 return function () {
michael@0 322 if (++count === countTo) done();
michael@0 323 };
michael@0 324 }

mercurial