test-places-history.js 8.3 KB

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