summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/tests/xpcshell/test_PlacesProvider.js
blob: 22815741bb2e632af9aa183d59de472077e06396 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"use strict";

/* global XPCOMUtils, PlacesUtils, PlacesTestUtils, PlacesProvider, NetUtil */
/* global do_get_profile, run_next_test, add_task */
/* global equal, ok */

const {
  utils: Cu,
  interfaces: Ci,
} = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "PlacesProvider",
    "resource:///modules/PlacesProvider.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
    "resource://gre/modules/PlacesUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
    "resource://testing-common/PlacesTestUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
    "resource://gre/modules/NetUtil.jsm");

// ensure a profile exists
do_get_profile();

function run_test() {
  PlacesProvider.links.init();
  run_next_test();
}

// url prefix for test history population
const TEST_URL = "https://mozilla.com/";
// time when the test starts execution
const TIME_NOW = (new Date()).getTime();

// utility function to compute past timestap
function timeDaysAgo(numDays) {
  return TIME_NOW - (numDays * 24 * 60 * 60 * 1000);
}

// utility function to make a visit for insetion into places db
function makeVisit(index, daysAgo, isTyped, domain=TEST_URL) {
  let {
    TRANSITION_TYPED,
    TRANSITION_LINK
  } = PlacesUtils.history;

  return {
    uri: NetUtil.newURI(`${domain}${index}`),
    visitDate: timeDaysAgo(daysAgo),
    transition: (isTyped) ? TRANSITION_TYPED : TRANSITION_LINK,
  };
}

/** Test LinkChecker **/

add_task(function test_LinkChecker_securityCheck() {

  let urls = [
    {url: "javascript:alert('hello')", expected: false}, // jshint ignore:line
    {url: "data:image/png;base64,XXX", expected: false},
    {url: "about:newtab", expected: true},
    {url: "https://example.com", expected: true},
    {url: "ftp://example.com", expected: true},
    {url: "file://home/file/image.png", expected: true},
    {url: "resource:///modules/PlacesProvider.jsm", expected: true},
  ];
  for (let {url, expected} of urls) {
    let observed = PlacesProvider.LinkChecker.checkLoadURI(url);
    equal(observed, expected, `can load "${url}"?`);
  }
});

/** Test Provider **/

add_task(function* test_Links_getLinks() {
  yield PlacesTestUtils.clearHistory();
  let provider = PlacesProvider.links;

  let links = yield provider.getLinks();
  equal(links.length, 0, "empty history yields empty links");

  // add a visit
  let testURI = NetUtil.newURI("http://mozilla.com");
  yield PlacesTestUtils.addVisits(testURI);

  links = yield provider.getLinks();
  equal(links.length, 1, "adding a visit yields a link");
  equal(links[0].url, testURI.spec, "added visit corresponds to added url");
});

add_task(function* test_Links_getLinks_Order() {
  yield PlacesTestUtils.clearHistory();
  let provider = PlacesProvider.links;

  // all four visits must come from different domains to avoid deduplication
  let visits = [
    makeVisit(0, 0, true, "http://bar.com/"), // frecency 200, today
    makeVisit(1, 0, true, "http://foo.com/"), // frecency 200, today
    makeVisit(2, 2, true, "http://buz.com/"), // frecency 200, 2 days ago
    makeVisit(3, 2, false, "http://aaa.com/"), // frecency 10, 2 days ago, transition
  ];

  let links = yield provider.getLinks();
  equal(links.length, 0, "empty history yields empty links");
  yield PlacesTestUtils.addVisits(visits);

  links = yield provider.getLinks();
  equal(links.length, visits.length, "number of links added is the same as obtain by getLinks");
  for (let i = 0; i < links.length; i++) {
    equal(links[i].url, visits[i].uri.spec, "links are obtained in the expected order");
  }
});

add_task(function* test_Links_getLinks_Deduplication() {
  yield PlacesTestUtils.clearHistory();
  let provider = PlacesProvider.links;

  // all for visits must come from different domains to avoid deduplication
  let visits = [
    makeVisit(0, 2, true, "http://bar.com/"), // frecency 200, 2 days ago
    makeVisit(1, 0, true, "http://bar.com/"), // frecency 200, today
    makeVisit(2, 0, false, "http://foo.com/"), // frecency 10, today
    makeVisit(3, 0, true, "http://foo.com/"), // frecency 200, today
  ];

  let links = yield provider.getLinks();
  equal(links.length, 0, "empty history yields empty links");
  yield PlacesTestUtils.addVisits(visits);

  links = yield provider.getLinks();
  equal(links.length, 2, "only two links must be left after deduplication");
  equal(links[0].url, visits[1].uri.spec, "earliest link is present");
  equal(links[1].url, visits[3].uri.spec, "most fresent link is present");
});

add_task(function* test_Links_onLinkChanged() {
  let provider = PlacesProvider.links;

  let url = "https://example.com/onFrecencyChanged1";
  let linkChangedMsgCount = 0;

  let linkChangedPromise = new Promise(resolve => {
    let handler = (_, link) => { // jshint ignore:line
      /* There are 3 linkChanged events:
       * 1. visit insertion (-1 frecency by default)
       * 2. frecency score update (after transition type calculation etc)
       * 3. title change
       */
      if (link.url === url) {
        equal(link.url, url, `expected url on linkChanged event`);
        linkChangedMsgCount += 1;
        if (linkChangedMsgCount === 3) {
          ok(true, `all linkChanged events captured`);
          provider.off("linkChanged", this);
          resolve();
        }
      }
    };
    provider.on("linkChanged", handler);
  });

  // add a visit
  let testURI = NetUtil.newURI(url);
  yield PlacesTestUtils.addVisits(testURI);
  yield linkChangedPromise;

  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_Links_onClearHistory() {
  let provider = PlacesProvider.links;

  let clearHistoryPromise = new Promise(resolve => {
    let handler = () => {
      ok(true, `clearHistory event captured`);
      provider.off("clearHistory", handler);
      resolve();
    };
    provider.on("clearHistory", handler);
  });

  // add visits
  for (let i = 0; i <= 10; i++) {
    let url = `https://example.com/onClearHistory${i}`;
    let testURI = NetUtil.newURI(url);
    yield PlacesTestUtils.addVisits(testURI);
  }
  yield PlacesTestUtils.clearHistory();
  yield clearHistoryPromise;
});

add_task(function* test_Links_onDeleteURI() {
  let provider = PlacesProvider.links;

  let testURL = "https://example.com/toDelete";

  let deleteURIPromise = new Promise(resolve => {
    let handler = (_, {url}) => { // jshint ignore:line
      equal(testURL, url, "deleted url and expected url are the same");
      provider.off("deleteURI", handler);
      resolve();
    };

    provider.on("deleteURI", handler);
  });

  let testURI = NetUtil.newURI(testURL);
  yield PlacesTestUtils.addVisits(testURI);
  yield PlacesUtils.history.remove(testURL);
  yield deleteURIPromise;
});

add_task(function* test_Links_onManyLinksChanged() {
  let provider = PlacesProvider.links;

  let promise = new Promise(resolve => {
    let handler = () => {
      ok(true);
      provider.off("manyLinksChanged", handler);
      resolve();
    };

    provider.on("manyLinksChanged", handler);
  });

  let testURL = "https://example.com/toDelete";
  let testURI = NetUtil.newURI(testURL);
  yield PlacesTestUtils.addVisits(testURI);

  // trigger DecayFrecency
  PlacesUtils.history.QueryInterface(Ci.nsIObserver).
    observe(null, "idle-daily", "");

  yield promise;
});

add_task(function* test_Links_execute_query() {
  yield PlacesTestUtils.clearHistory();
  let provider = PlacesProvider.links;

  let visits = [
    makeVisit(0, 0, true), // frecency 200, today
    makeVisit(1, 0, true), // frecency 200, today
    makeVisit(2, 2, true), // frecency 200, 2 days ago
    makeVisit(3, 2, false), // frecency 10, 2 days ago, transition
  ];

  yield PlacesTestUtils.addVisits(visits);

  function testItemValue(results, index, value) {
    equal(results[index][0], `${TEST_URL}${value}`, "raw url");
    equal(results[index][1], `test visit for ${TEST_URL}${value}`, "raw title");
  }

  function testItemObject(results, index, columnValues) {
    Object.keys(columnValues).forEach(name => {
      equal(results[index][name], columnValues[name], "object name " + name);
    });
  }

  // select all 4 records
  let results = yield provider.executePlacesQuery("select url, title from moz_places");
  equal(results.length, 4, "expect 4 items");
  // check for insert order sequence
  for (let i = 0; i < results.length; i++) {
    testItemValue(results, i, i);
  }

  // test parameter passing
  results = yield provider.executePlacesQuery(
              "select url, title from moz_places limit :limit",
              {params: {limit: 2}}
            );
  equal(results.length, 2, "expect 2 items");
  for (let i = 0; i < results.length; i++) {
    testItemValue(results, i, i);
  }

  // test extracting items by name
  results = yield provider.executePlacesQuery(
              "select url, title from moz_places limit :limit",
              {columns: ["url", "title"], params: {limit: 4}}
            );
  equal(results.length, 4, "expect 4 items");
  for (let i = 0; i < results.length; i++) {
    testItemObject(results, i, {
      "url": `${TEST_URL}${i}`,
      "title": `test visit for ${TEST_URL}${i}`,
    });
  }

  // test ordering
  results = yield provider.executePlacesQuery(
              "select url, title, last_visit_date, frecency from moz_places " +
              "order by frecency DESC, last_visit_date DESC, url DESC limit :limit",
              {columns: ["url", "title", "last_visit_date", "frecency"], params: {limit: 4}}
            );
  equal(results.length, 4, "expect 4 items");
  testItemObject(results, 0, {url: `${TEST_URL}1`});
  testItemObject(results, 1, {url: `${TEST_URL}0`});
  testItemObject(results, 2, {url: `${TEST_URL}2`});
  testItemObject(results, 3, {url: `${TEST_URL}3`});

  // test callback passing
  results = [];
  function handleRow(aRow) {
    results.push({
      url:              aRow.getResultByName("url"),
      title:            aRow.getResultByName("title"),
      last_visit_date:  aRow.getResultByName("last_visit_date"),
      frecency:         aRow.getResultByName("frecency")
    });
  }
  yield provider.executePlacesQuery(
        "select url, title, last_visit_date, frecency from moz_places " +
        "order by frecency DESC, last_visit_date DESC, url DESC",
        {callback: handleRow}
      );
  equal(results.length, 4, "expect 4 items");
  testItemObject(results, 0, {url: `${TEST_URL}1`});
  testItemObject(results, 1, {url: `${TEST_URL}0`});
  testItemObject(results, 2, {url: `${TEST_URL}2`});
  testItemObject(results, 3, {url: `${TEST_URL}3`});

  // negative test cases
  // bad sql
  try {
    yield provider.executePlacesQuery("select from moz");
    do_throw("bad sql should've thrown");
  }
  catch (e) {
    do_check_true("expected failure - bad sql");
  }
  // missing bindings
  try {
    yield provider.executePlacesQuery("select * from moz_places limit :limit");
    do_throw("bad sql should've thrown");
  }
  catch (e) {
    do_check_true("expected failure - missing bidning");
  }
  // non-existent column name
  try {
    yield provider.executePlacesQuery("select * from moz_places limit :limit",
                                     {columns: ["no-such-column"], params: {limit: 4}});
    do_throw("bad sql should've thrown");
  }
  catch (e) {
    do_check_true("expected failure - wrong column name");
  }

  // cleanup
  yield PlacesTestUtils.clearHistory();
});