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