1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/addons/places/tests/test-places-history.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,249 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +module.metadata = { 1.10 + 'engines': { 1.11 + 'Firefox': '*' 1.12 + } 1.13 +}; 1.14 + 1.15 +const { Cc, Ci } = require('chrome'); 1.16 +const { defer, all } = require('sdk/core/promise'); 1.17 +const { has } = require('sdk/util/array'); 1.18 +const { setTimeout } = require('sdk/timers'); 1.19 +const { before, after } = require('sdk/test/utils'); 1.20 +const { set } = require('sdk/preferences/service'); 1.21 +const { 1.22 + search 1.23 +} = require('sdk/places/history'); 1.24 +const { 1.25 + invalidResolve, invalidReject, createTree, 1.26 + compareWithHost, addVisits, resetPlaces 1.27 +} = require('../places-helper'); 1.28 +const { promisedEmitter } = require('sdk/places/utils'); 1.29 + 1.30 +exports.testEmptyQuery = function (assert, done) { 1.31 + let within = toBeWithin(); 1.32 + addVisits([ 1.33 + 'http://simplequery-1.com', 'http://simplequery-2.com' 1.34 + ]).then(searchP).then(results => { 1.35 + assert.equal(results.length, 2, 'Correct number of entries returned'); 1.36 + assert.equal(results[0].url, 'http://simplequery-1.com/', 1.37 + 'matches url'); 1.38 + assert.equal(results[1].url, 'http://simplequery-2.com/', 1.39 + 'matches url'); 1.40 + assert.equal(results[0].title, 'Test visit for ' + results[0].url, 1.41 + 'title matches'); 1.42 + assert.equal(results[1].title, 'Test visit for ' + results[1].url, 1.43 + 'title matches'); 1.44 + assert.equal(results[0].visitCount, 1, 'matches access'); 1.45 + assert.equal(results[1].visitCount, 1, 'matches access'); 1.46 + assert.ok(within(results[0].time), 'accurate access time'); 1.47 + assert.ok(within(results[1].time), 'accurate access time'); 1.48 + assert.equal(Object.keys(results[0]).length, 4, 1.49 + 'no addition exposed properties on history result'); 1.50 + done(); 1.51 + }, invalidReject); 1.52 +}; 1.53 + 1.54 +exports.testVisitCount = function (assert, done) { 1.55 + addVisits([ 1.56 + 'http://simplequery-1.com', 'http://simplequery-1.com', 1.57 + 'http://simplequery-1.com', 'http://simplequery-1.com' 1.58 + ]).then(searchP).then(results => { 1.59 + assert.equal(results.length, 1, 'Correct number of entries returned'); 1.60 + assert.equal(results[0].url, 'http://simplequery-1.com/', 'correct url'); 1.61 + assert.equal(results[0].visitCount, 4, 'matches access count'); 1.62 + done(); 1.63 + }, invalidReject); 1.64 +}; 1.65 + 1.66 +/* 1.67 + * Tests 4 scenarios 1.68 + * '*.mozilla.org' 1.69 + * 'mozilla.org' 1.70 + * 'http://mozilla.org/' 1.71 + * 'http://mozilla.org/*' 1.72 + */ 1.73 +exports.testSearchURL = function (assert, done) { 1.74 + addVisits([ 1.75 + 'http://developer.mozilla.org', 'http://mozilla.org', 1.76 + 'http://mozilla.org/index', 'https://mozilla.org' 1.77 + ]).then(() => searchP({ url: '*.mozilla.org' })) 1.78 + .then(results => { 1.79 + assert.equal(results.length, 4, 'returns all entries'); 1.80 + return searchP({ url: 'mozilla.org' }); 1.81 + }).then(results => { 1.82 + assert.equal(results.length, 3, 'returns entries where mozilla.org is host'); 1.83 + return searchP({ url: 'http://mozilla.org/' }); 1.84 + }).then(results => { 1.85 + assert.equal(results.length, 1, 'should just be an exact match'); 1.86 + return searchP({ url: 'http://mozilla.org/*' }); 1.87 + }).then(results => { 1.88 + assert.equal(results.length, 2, 'should match anything starting with substring'); 1.89 + done(); 1.90 + }); 1.91 +}; 1.92 + 1.93 +// Disabling due to intermittent Bug 892619 1.94 +// TODO solve this 1.95 +/* 1.96 +exports.testSearchTimeRange = function (assert, done) { 1.97 + let firstTime, secondTime; 1.98 + addVisits([ 1.99 + 'http://earlyvisit.org', 'http://earlyvisit.org/earlytown.html' 1.100 + ]).then(searchP).then(results => { 1.101 + firstTime = results[0].time; 1.102 + var deferred = defer(); 1.103 + setTimeout(function () deferred.resolve(), 1000); 1.104 + return deferred.promise; 1.105 + }).then(() => { 1.106 + return addVisits(['http://newvisit.org', 'http://newvisit.org/whoawhoa.html']); 1.107 + }).then(searchP).then(results => { 1.108 + results.filter(({url, time}) => { 1.109 + if (/newvisit/.test(url)) secondTime = time; 1.110 + }); 1.111 + return searchP({ from: firstTime - 1000 }); 1.112 + }).then(results => { 1.113 + assert.equal(results.length, 4, 'should return all entries'); 1.114 + return searchP({ to: firstTime + 500 }); 1.115 + }).then(results => { 1.116 + assert.equal(results.length, 2, 'should return only first entries'); 1.117 + results.map(item => { 1.118 + assert.ok(/earlyvisit/.test(item.url), 'correct entry'); 1.119 + }); 1.120 + return searchP({ from: firstTime + 500 }); 1.121 + }).then(results => { 1.122 + assert.equal(results.length, 2, 'should return only last entries'); 1.123 + results.map(item => { 1.124 + assert.ok(/newvisit/.test(item.url), 'correct entry'); 1.125 + }); 1.126 + done(); 1.127 + }); 1.128 +}; 1.129 +*/ 1.130 +exports.testSearchQuery = function (assert, done) { 1.131 + addVisits([ 1.132 + 'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd' 1.133 + ]).then(() => { 1.134 + return searchP({ query: 'moz' }); 1.135 + }).then(results => { 1.136 + assert.equal(results.length, 2, 'should return urls that match substring'); 1.137 + results.map(({url}) => { 1.138 + assert.ok(/moz/.test(url), 'correct item'); 1.139 + }); 1.140 + return searchP([{ query: 'webfwd' }, { query: 'aud.io' }]); 1.141 + }).then(results => { 1.142 + assert.equal(results.length, 2, 'should OR separate queries'); 1.143 + results.map(({url}) => { 1.144 + assert.ok(/webfwd|aud\.io/.test(url), 'correct item'); 1.145 + }); 1.146 + done(); 1.147 + }); 1.148 +}; 1.149 + 1.150 +/* 1.151 + * Query Options 1.152 + */ 1.153 + 1.154 +exports.testSearchCount = function (assert, done) { 1.155 + addVisits([ 1.156 + 'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd', 1.157 + 'http://developer.mozilla.com', 'http://bandcamp.com' 1.158 + ]).then(testCount(1)) 1.159 + .then(testCount(2)) 1.160 + .then(testCount(3)) 1.161 + .then(testCount(5)) 1.162 + .then(done); 1.163 + 1.164 + function testCount (n) { 1.165 + return function () { 1.166 + return searchP({}, { count: n }).then(results => { 1.167 + assert.equal(results.length, n, 1.168 + 'count ' + n + ' returns ' + n + ' results'); 1.169 + }); 1.170 + }; 1.171 + } 1.172 +}; 1.173 + 1.174 +exports.testSearchSort = function (assert, done) { 1.175 + let places = [ 1.176 + 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/', 1.177 + 'http://developer.mozilla.com/', 'http://bandcamp.com/' 1.178 + ]; 1.179 + addVisits(places).then(() => { 1.180 + return searchP({}, { sort: 'title' }); 1.181 + }).then(results => { 1.182 + checkOrder(results, [4,3,0,2,1]); 1.183 + return searchP({}, { sort: 'title', descending: true }); 1.184 + }).then(results => { 1.185 + checkOrder(results, [1,2,0,3,4]); 1.186 + return searchP({}, { sort: 'url' }); 1.187 + }).then(results => { 1.188 + checkOrder(results, [4,3,0,2,1]); 1.189 + return searchP({}, { sort: 'url', descending: true }); 1.190 + }).then(results => { 1.191 + checkOrder(results, [1,2,0,3,4]); 1.192 + return addVisits('http://mozilla.com') // for visit conut 1.193 + .then(() => addVisits('http://github.com')); // for checking date 1.194 + }).then(() => { 1.195 + return searchP({}, { sort: 'visitCount' }); 1.196 + }).then(results => { 1.197 + assert.equal(results[5].url, 'http://mozilla.com/', 1.198 + 'last entry is the highest visit count'); 1.199 + return searchP({}, { sort: 'visitCount', descending: true }); 1.200 + }).then(results => { 1.201 + assert.equal(results[0].url, 'http://mozilla.com/', 1.202 + 'first entry is the highest visit count'); 1.203 + return searchP({}, { sort: 'date' }); 1.204 + }).then(results => { 1.205 + assert.equal(results[5].url, 'http://github.com/', 1.206 + 'latest visited should be first'); 1.207 + return searchP({}, { sort: 'date', descending: true }); 1.208 + }).then(results => { 1.209 + assert.equal(results[0].url, 'http://github.com/', 1.210 + 'latest visited should be at the end'); 1.211 + }).then(done); 1.212 + 1.213 + function checkOrder (results, nums) { 1.214 + assert.equal(results.length, nums.length, 'expected return count'); 1.215 + for (let i = 0; i < nums.length; i++) { 1.216 + assert.equal(results[i].url, places[nums[i]], 'successful order'); 1.217 + } 1.218 + } 1.219 +}; 1.220 + 1.221 +exports.testEmitters = function (assert, done) { 1.222 + let urls = [ 1.223 + 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/', 1.224 + 'http://developer.mozilla.com/', 'http://bandcamp.com/' 1.225 + ]; 1.226 + addVisits(urls).then(() => { 1.227 + let count = 0; 1.228 + search().on('data', item => { 1.229 + assert.ok(~urls.indexOf(item.url), 'data value found in url list'); 1.230 + count++; 1.231 + }).on('end', results => { 1.232 + assert.equal(results.length, 5, 'correct count of items'); 1.233 + assert.equal(count, 5, 'data event called 5 times'); 1.234 + done(); 1.235 + }); 1.236 + }); 1.237 +}; 1.238 + 1.239 +function toBeWithin (range) { 1.240 + range = range || 2000; 1.241 + var current = new Date() * 1000; // convert to microseconds 1.242 + return compared => { 1.243 + return compared - current < range; 1.244 + }; 1.245 +} 1.246 + 1.247 +function searchP () { 1.248 + return promisedEmitter(search.apply(null, Array.slice(arguments))); 1.249 +} 1.250 + 1.251 +before(exports, (name, assert, done) => resetPlaces(done)); 1.252 +after(exports, (name, assert, done) => resetPlaces(done));