summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/places/lib/test-places-history.js
blob: 0a1e2b8cc35686070b4541d2d0b2e71ba02a26a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
  'engines': {
    'Firefox': '*'
  }
};

const { Cc, Ci } = require('chrome');
const { defer, all } = require('sdk/core/promise');
const { has } = require('sdk/util/array');
const { setTimeout } = require('sdk/timers');
const { before, after } = require('sdk/test/utils');
const { set } = require('sdk/preferences/service');
const {
  search
} = require('sdk/places/history');
const {
  invalidResolve, createTree,
  compareWithHost, addVisits, resetPlaces
} = require('./places-helper');
const { promisedEmitter } = require('sdk/places/utils');

exports.testEmptyQuery = function*(assert) {
  let within = toBeWithin();
  yield addVisits([
    'http://simplequery-1.com', 'http://simplequery-2.com'
  ]);

  let results = yield searchP();
  assert.equal(results.length, 2, 'Correct number of entries returned');
  assert.equal(results[0].url, 'http://simplequery-1.com/',
    'matches url');
  assert.equal(results[1].url, 'http://simplequery-2.com/',
    'matches url');
  assert.equal(results[0].title, 'Test visit for ' + results[0].url,
    'title matches');
  assert.equal(results[1].title, 'Test visit for ' + results[1].url,
    'title matches');
  assert.equal(results[0].visitCount, 1, 'matches access');
  assert.equal(results[1].visitCount, 1, 'matches access');
  assert.ok(within(results[0].time), 'accurate access time');
  assert.ok(within(results[1].time), 'accurate access time');
  assert.equal(Object.keys(results[0]).length, 4,
    'no addition exposed properties on history result');
};

exports.testVisitCount = function*(assert) {
  yield addVisits([
    'http://simplequery-1.com', 'http://simplequery-1.com',
    'http://simplequery-1.com', 'http://simplequery-1.com'
  ]);
  let results = yield searchP();
  assert.equal(results.length, 1, 'Correct number of entries returned');
  assert.equal(results[0].url, 'http://simplequery-1.com/', 'correct url');
  assert.equal(results[0].visitCount, 4, 'matches access count');
};

/*
 * Tests 4 scenarios
 * '*.mozilla.org'
 * 'mozilla.org'
 * 'http://mozilla.org/'
 * 'http://mozilla.org/*'
 */
exports.testSearchURLForHistory = function*(assert) {
  yield addVisits([
    'http://developer.mozilla.org', 'http://mozilla.org',
    'http://mozilla.org/index', 'https://mozilla.org'
  ]);

  let results = yield searchP({ url: 'http://mozilla.org/' });
  assert.equal(results.length, 1, 'should just be an exact match');

  results = yield searchP({ url: '*.mozilla.org' });
  assert.equal(results.length, 4, 'returns all entries');

  results = yield searchP({ url: 'mozilla.org' });
  assert.equal(results.length, 3, 'returns entries where mozilla.org is host');

  results = yield searchP({ url: 'http://mozilla.org/*' });
  assert.equal(results.length, 2, 'should match anything starting with substring');
};

// Disabling due to intermittent Bug 892619
// TODO solve this
/*
exports.testSearchTimeRange = function (assert, done) {
  let firstTime, secondTime;
  addVisits([
    'http://earlyvisit.org', 'http://earlyvisit.org/earlytown.html'
  ]).then(searchP).then(results => {
    firstTime = results[0].time;
    var deferred = defer();
    setTimeout(() => deferred.resolve(), 1000);
    return deferred.promise;
  }).then(() => {
    return addVisits(['http://newvisit.org', 'http://newvisit.org/whoawhoa.html']);
  }).then(searchP).then(results => {
    results.filter(({url, time}) => {
      if (/newvisit/.test(url)) secondTime = time;
    });
    return searchP({ from: firstTime - 1000 });
  }).then(results => {
    assert.equal(results.length, 4, 'should return all entries');
    return searchP({ to: firstTime + 500 });
  }).then(results => {
    assert.equal(results.length, 2, 'should return only first entries');
    results.map(item => {
      assert.ok(/earlyvisit/.test(item.url), 'correct entry');
    });
    return searchP({ from: firstTime + 500 });
  }).then(results => {
    assert.equal(results.length, 2, 'should return only last entries');
    results.map(item => {
      assert.ok(/newvisit/.test(item.url), 'correct entry');
    });
    done();
  });
};
*/
exports.testSearchQueryForHistory = function*(assert) {
  yield addVisits([
    'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd'
  ]);

  let results = yield searchP({ query: 'moz' });
  assert.equal(results.length, 2, 'should return urls that match substring');
  results.map(({url}) => {
    assert.ok(/moz/.test(url), 'correct item');
  });

  results = yield searchP([{ query: 'webfwd' }, { query: 'aud.io' }]);
  assert.equal(results.length, 2, 'should OR separate queries');
  results.map(({url}) => {
    assert.ok(/webfwd|aud\.io/.test(url), 'correct item');
  });
};

/*
 * Query Options
 */

exports.testSearchCount = function (assert, done) {
  addVisits([
    'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd',
    'http://developer.mozilla.com', 'http://bandcamp.com'
  ]).then(testCount(1))
  .then(testCount(2))
  .then(testCount(3))
  .then(testCount(5))
  .then(done);

  function testCount (n) {
    return function () {
      return searchP({}, { count: n }).then(results => {
        assert.equal(results.length, n,
          'count ' + n + ' returns ' + n + ' results');
      });
    };
  }
};

exports.testSearchSortForHistory = function*(assert) {
  function checkOrder (results, nums) {
    assert.equal(results.length, nums.length, 'expected return count');
    for (let i = 0; i < nums.length; i++) {
      assert.equal(results[i].url, places[nums[i]], 'successful order');
    }
  }

  let places = [
    'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/',
    'http://developer.mozilla.com/', 'http://bandcamp.com/'
  ];
  yield addVisits(places);

  let results = yield searchP({}, { sort: 'title' });
  checkOrder(results, [4,3,0,2,1]);

  results = yield searchP({}, { sort: 'title', descending: true });
  checkOrder(results, [1,2,0,3,4]);

  results = yield searchP({}, { sort: 'url' });
  checkOrder(results, [4,3,0,2,1]);

  results = yield searchP({}, { sort: 'url', descending: true });
  checkOrder(results, [1,2,0,3,4]);

  yield addVisits('http://mozilla.com'); // for visit conut
  yield addVisits('http://github.com'); // for checking date

  results = yield searchP({}, { sort: 'visitCount' });
  assert.equal(results[5].url, 'http://mozilla.com/',
    'last entry is the highest visit count');

  results = yield  searchP({}, { sort: 'visitCount', descending: true });
  assert.equal(results[0].url, 'http://mozilla.com/',
    'first entry is the highest visit count');

  results = yield  searchP({}, { sort: 'date' });
  assert.equal(results[5].url, 'http://github.com/',
    'latest visited should be first');

  results = yield  searchP({}, { sort: 'date', descending: true });
  assert.equal(results[0].url, 'http://github.com/',
    'latest visited should be at the end');
};

exports.testEmitters = function (assert, done) {
  let urls = [
    'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/',
    'http://developer.mozilla.com/', 'http://bandcamp.com/'
  ];
  addVisits(urls).then(() => {
    let count = 0;
    search().on('data', item => {
      assert.ok(~urls.indexOf(item.url), 'data value found in url list');
      count++;
    }).on('end', results => {
      assert.equal(results.length, 5, 'correct count of items');
      assert.equal(count, 5, 'data event called 5 times');
      done();
    });
  });
};

function toBeWithin (range) {
  range = range || 2000;
  var current = new Date() * 1000; // convert to microseconds
  return compared => {
    return compared - current < range;
  };
}

function searchP () {
  return promisedEmitter(search.apply(null, Array.prototype.slice.call(arguments)));
}

before(exports, (name, assert, done) => resetPlaces(done));
after(exports, (name, assert, done) => resetPlaces(done));