|
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 |
|
6 module.metadata = { |
|
7 'engines': { |
|
8 'Firefox': '*' |
|
9 } |
|
10 }; |
|
11 |
|
12 const { Cc, Ci } = require('chrome'); |
|
13 const { defer, all } = require('sdk/core/promise'); |
|
14 const { has } = require('sdk/util/array'); |
|
15 const { setTimeout } = require('sdk/timers'); |
|
16 const { before, after } = require('sdk/test/utils'); |
|
17 const { set } = require('sdk/preferences/service'); |
|
18 const { |
|
19 search |
|
20 } = require('sdk/places/history'); |
|
21 const { |
|
22 invalidResolve, invalidReject, createTree, |
|
23 compareWithHost, addVisits, resetPlaces |
|
24 } = require('../places-helper'); |
|
25 const { promisedEmitter } = require('sdk/places/utils'); |
|
26 |
|
27 exports.testEmptyQuery = function (assert, done) { |
|
28 let within = toBeWithin(); |
|
29 addVisits([ |
|
30 'http://simplequery-1.com', 'http://simplequery-2.com' |
|
31 ]).then(searchP).then(results => { |
|
32 assert.equal(results.length, 2, 'Correct number of entries returned'); |
|
33 assert.equal(results[0].url, 'http://simplequery-1.com/', |
|
34 'matches url'); |
|
35 assert.equal(results[1].url, 'http://simplequery-2.com/', |
|
36 'matches url'); |
|
37 assert.equal(results[0].title, 'Test visit for ' + results[0].url, |
|
38 'title matches'); |
|
39 assert.equal(results[1].title, 'Test visit for ' + results[1].url, |
|
40 'title matches'); |
|
41 assert.equal(results[0].visitCount, 1, 'matches access'); |
|
42 assert.equal(results[1].visitCount, 1, 'matches access'); |
|
43 assert.ok(within(results[0].time), 'accurate access time'); |
|
44 assert.ok(within(results[1].time), 'accurate access time'); |
|
45 assert.equal(Object.keys(results[0]).length, 4, |
|
46 'no addition exposed properties on history result'); |
|
47 done(); |
|
48 }, invalidReject); |
|
49 }; |
|
50 |
|
51 exports.testVisitCount = function (assert, done) { |
|
52 addVisits([ |
|
53 'http://simplequery-1.com', 'http://simplequery-1.com', |
|
54 'http://simplequery-1.com', 'http://simplequery-1.com' |
|
55 ]).then(searchP).then(results => { |
|
56 assert.equal(results.length, 1, 'Correct number of entries returned'); |
|
57 assert.equal(results[0].url, 'http://simplequery-1.com/', 'correct url'); |
|
58 assert.equal(results[0].visitCount, 4, 'matches access count'); |
|
59 done(); |
|
60 }, invalidReject); |
|
61 }; |
|
62 |
|
63 /* |
|
64 * Tests 4 scenarios |
|
65 * '*.mozilla.org' |
|
66 * 'mozilla.org' |
|
67 * 'http://mozilla.org/' |
|
68 * 'http://mozilla.org/*' |
|
69 */ |
|
70 exports.testSearchURL = function (assert, done) { |
|
71 addVisits([ |
|
72 'http://developer.mozilla.org', 'http://mozilla.org', |
|
73 'http://mozilla.org/index', 'https://mozilla.org' |
|
74 ]).then(() => searchP({ url: '*.mozilla.org' })) |
|
75 .then(results => { |
|
76 assert.equal(results.length, 4, 'returns all entries'); |
|
77 return searchP({ url: 'mozilla.org' }); |
|
78 }).then(results => { |
|
79 assert.equal(results.length, 3, 'returns entries where mozilla.org is host'); |
|
80 return searchP({ url: 'http://mozilla.org/' }); |
|
81 }).then(results => { |
|
82 assert.equal(results.length, 1, 'should just be an exact match'); |
|
83 return searchP({ url: 'http://mozilla.org/*' }); |
|
84 }).then(results => { |
|
85 assert.equal(results.length, 2, 'should match anything starting with substring'); |
|
86 done(); |
|
87 }); |
|
88 }; |
|
89 |
|
90 // Disabling due to intermittent Bug 892619 |
|
91 // TODO solve this |
|
92 /* |
|
93 exports.testSearchTimeRange = function (assert, done) { |
|
94 let firstTime, secondTime; |
|
95 addVisits([ |
|
96 'http://earlyvisit.org', 'http://earlyvisit.org/earlytown.html' |
|
97 ]).then(searchP).then(results => { |
|
98 firstTime = results[0].time; |
|
99 var deferred = defer(); |
|
100 setTimeout(function () deferred.resolve(), 1000); |
|
101 return deferred.promise; |
|
102 }).then(() => { |
|
103 return addVisits(['http://newvisit.org', 'http://newvisit.org/whoawhoa.html']); |
|
104 }).then(searchP).then(results => { |
|
105 results.filter(({url, time}) => { |
|
106 if (/newvisit/.test(url)) secondTime = time; |
|
107 }); |
|
108 return searchP({ from: firstTime - 1000 }); |
|
109 }).then(results => { |
|
110 assert.equal(results.length, 4, 'should return all entries'); |
|
111 return searchP({ to: firstTime + 500 }); |
|
112 }).then(results => { |
|
113 assert.equal(results.length, 2, 'should return only first entries'); |
|
114 results.map(item => { |
|
115 assert.ok(/earlyvisit/.test(item.url), 'correct entry'); |
|
116 }); |
|
117 return searchP({ from: firstTime + 500 }); |
|
118 }).then(results => { |
|
119 assert.equal(results.length, 2, 'should return only last entries'); |
|
120 results.map(item => { |
|
121 assert.ok(/newvisit/.test(item.url), 'correct entry'); |
|
122 }); |
|
123 done(); |
|
124 }); |
|
125 }; |
|
126 */ |
|
127 exports.testSearchQuery = function (assert, done) { |
|
128 addVisits([ |
|
129 'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd' |
|
130 ]).then(() => { |
|
131 return searchP({ query: 'moz' }); |
|
132 }).then(results => { |
|
133 assert.equal(results.length, 2, 'should return urls that match substring'); |
|
134 results.map(({url}) => { |
|
135 assert.ok(/moz/.test(url), 'correct item'); |
|
136 }); |
|
137 return searchP([{ query: 'webfwd' }, { query: 'aud.io' }]); |
|
138 }).then(results => { |
|
139 assert.equal(results.length, 2, 'should OR separate queries'); |
|
140 results.map(({url}) => { |
|
141 assert.ok(/webfwd|aud\.io/.test(url), 'correct item'); |
|
142 }); |
|
143 done(); |
|
144 }); |
|
145 }; |
|
146 |
|
147 /* |
|
148 * Query Options |
|
149 */ |
|
150 |
|
151 exports.testSearchCount = function (assert, done) { |
|
152 addVisits([ |
|
153 'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd', |
|
154 'http://developer.mozilla.com', 'http://bandcamp.com' |
|
155 ]).then(testCount(1)) |
|
156 .then(testCount(2)) |
|
157 .then(testCount(3)) |
|
158 .then(testCount(5)) |
|
159 .then(done); |
|
160 |
|
161 function testCount (n) { |
|
162 return function () { |
|
163 return searchP({}, { count: n }).then(results => { |
|
164 assert.equal(results.length, n, |
|
165 'count ' + n + ' returns ' + n + ' results'); |
|
166 }); |
|
167 }; |
|
168 } |
|
169 }; |
|
170 |
|
171 exports.testSearchSort = function (assert, done) { |
|
172 let places = [ |
|
173 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/', |
|
174 'http://developer.mozilla.com/', 'http://bandcamp.com/' |
|
175 ]; |
|
176 addVisits(places).then(() => { |
|
177 return searchP({}, { sort: 'title' }); |
|
178 }).then(results => { |
|
179 checkOrder(results, [4,3,0,2,1]); |
|
180 return searchP({}, { sort: 'title', descending: true }); |
|
181 }).then(results => { |
|
182 checkOrder(results, [1,2,0,3,4]); |
|
183 return searchP({}, { sort: 'url' }); |
|
184 }).then(results => { |
|
185 checkOrder(results, [4,3,0,2,1]); |
|
186 return searchP({}, { sort: 'url', descending: true }); |
|
187 }).then(results => { |
|
188 checkOrder(results, [1,2,0,3,4]); |
|
189 return addVisits('http://mozilla.com') // for visit conut |
|
190 .then(() => addVisits('http://github.com')); // for checking date |
|
191 }).then(() => { |
|
192 return searchP({}, { sort: 'visitCount' }); |
|
193 }).then(results => { |
|
194 assert.equal(results[5].url, 'http://mozilla.com/', |
|
195 'last entry is the highest visit count'); |
|
196 return searchP({}, { sort: 'visitCount', descending: true }); |
|
197 }).then(results => { |
|
198 assert.equal(results[0].url, 'http://mozilla.com/', |
|
199 'first entry is the highest visit count'); |
|
200 return searchP({}, { sort: 'date' }); |
|
201 }).then(results => { |
|
202 assert.equal(results[5].url, 'http://github.com/', |
|
203 'latest visited should be first'); |
|
204 return searchP({}, { sort: 'date', descending: true }); |
|
205 }).then(results => { |
|
206 assert.equal(results[0].url, 'http://github.com/', |
|
207 'latest visited should be at the end'); |
|
208 }).then(done); |
|
209 |
|
210 function checkOrder (results, nums) { |
|
211 assert.equal(results.length, nums.length, 'expected return count'); |
|
212 for (let i = 0; i < nums.length; i++) { |
|
213 assert.equal(results[i].url, places[nums[i]], 'successful order'); |
|
214 } |
|
215 } |
|
216 }; |
|
217 |
|
218 exports.testEmitters = function (assert, done) { |
|
219 let urls = [ |
|
220 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/', |
|
221 'http://developer.mozilla.com/', 'http://bandcamp.com/' |
|
222 ]; |
|
223 addVisits(urls).then(() => { |
|
224 let count = 0; |
|
225 search().on('data', item => { |
|
226 assert.ok(~urls.indexOf(item.url), 'data value found in url list'); |
|
227 count++; |
|
228 }).on('end', results => { |
|
229 assert.equal(results.length, 5, 'correct count of items'); |
|
230 assert.equal(count, 5, 'data event called 5 times'); |
|
231 done(); |
|
232 }); |
|
233 }); |
|
234 }; |
|
235 |
|
236 function toBeWithin (range) { |
|
237 range = range || 2000; |
|
238 var current = new Date() * 1000; // convert to microseconds |
|
239 return compared => { |
|
240 return compared - current < range; |
|
241 }; |
|
242 } |
|
243 |
|
244 function searchP () { |
|
245 return promisedEmitter(search.apply(null, Array.slice(arguments))); |
|
246 } |
|
247 |
|
248 before(exports, (name, assert, done) => resetPlaces(done)); |
|
249 after(exports, (name, assert, done) => resetPlaces(done)); |