summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_adaptive.js
blob: 78ffaedb518105fb5c2de5a658b07f3f1574755a (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */

/**
 * Test for bug 395739 to make sure the feedback to the search results in those
 * entries getting better ranks. Additionally, exact matches should be ranked
 * higher. Because the interactions among adaptive rank and visit counts is not
 * well defined, this test holds one of the two values constant when modifying
 * the other.
 *
 * This also tests bug 395735 for the instrumentation feedback mechanism.
 *
 * Bug 411293 is tested to make sure the drop down strongly prefers previously
 * typed pages that have been selected and are moved to the top with adaptive
 * learning.
 */

function AutoCompleteInput(aSearches) {
  this.searches = aSearches;
}
AutoCompleteInput.prototype = {
  constructor: AutoCompleteInput,

  get minResultsForPopup() {
    return 0;
  },
  get timeout() {
    return 10;
  },
  get searchParam() {
    return "";
  },
  get textValue() {
    return "";
  },
  get disableAutoComplete() {
    return false;
  },
  get completeDefaultIndex() {
    return false;
  },

  get searchCount() {
    return this.searches.length;
  },
  getSearchAt: function (aIndex) {
    return this.searches[aIndex];
  },

  onSearchBegin: function () {},
  onSearchComplete: function() {},

  get popupOpen() {
    return false;
  },
  popup: {
    set selectedIndex(aIndex) {},
    invalidate: function () {},
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup])
  },

  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput])
}

/**
 * Checks that autocomplete results are ordered correctly.
 */
function ensure_results(expected, searchTerm)
{
  let controller = Cc["@mozilla.org/autocomplete/controller;1"].
                   getService(Ci.nsIAutoCompleteController);

  // Make an AutoCompleteInput that uses our searches
  // and confirms results on search complete.
  let input = new AutoCompleteInput(["unifiedcomplete"]);

  controller.input = input;

  input.onSearchComplete = function() {
    do_check_eq(controller.searchStatus,
                Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
    do_check_eq(controller.matchCount, expected.length);
    for (let i = 0; i < controller.matchCount; i++) {
      print("Testing for '" + expected[i].uri.spec + "' got '" + controller.getValueAt(i) + "'");
      do_check_eq(controller.getValueAt(i), expected[i].uri.spec);
      do_check_eq(controller.getStyleAt(i), expected[i].style);
    }

    deferEnsureResults.resolve();
  };

  controller.startSearch(searchTerm);
}

/**
 * Asynchronous task that bumps up the rank for an uri.
 */
function* task_setCountRank(aURI, aCount, aRank, aSearch, aBookmark)
{
  // Bump up the visit count for the uri.
  let visits = [];
  for (let i = 0; i < aCount; i++) {
    visits.push({ uri: aURI, visitDate: d1, transition: TRANSITION_TYPED });
  }
  yield PlacesTestUtils.addVisits(visits);

  // Make a nsIAutoCompleteController and friends for instrumentation feedback.
  let thing = {
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput,
                                           Ci.nsIAutoCompletePopup,
                                           Ci.nsIAutoCompleteController]),
    get popup() {
      return thing;
    },
    get controller() {
      return thing;
    },
    popupOpen: true,
    selectedIndex: 0,
    getValueAt: function() {
      return aURI.spec;
    },
    searchString: aSearch
  };

  // Bump up the instrumentation feedback.
  for (let i = 0; i < aRank; i++) {
    Services.obs.notifyObservers(thing, "autocomplete-will-enter-text", null);
  }

  // If this is supposed to be a bookmark, add it.
  if (aBookmark) {
    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
                                         aURI,
                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
                                         "test_book");

    // And add the tag if we need to.
    if (aBookmark == "tag") {
      PlacesUtils.tagging.tagURI(aURI, ["test_tag"]);
    }
  }
}

/**
 * Decay the adaptive entries by sending the daily idle topic.
 */
function doAdaptiveDecay()
{
  PlacesUtils.history.runInBatchMode({
    runBatched: function() {
      for (let i = 0; i < 10; i++) {
        PlacesUtils.history.QueryInterface(Ci.nsIObserver)
                           .observe(null, "idle-daily", null);
      }
    }
  }, this);
}

var uri1 = uri("http://site.tld/1");
var uri2 = uri("http://site.tld/2");

// d1 is some date for the page visit
var d1 = new Date(Date.now() - 1000 * 60 * 60) * 1000;
// c1 is larger (should show up higher) than c2
var c1 = 10;
var c2 = 1;
// s1 is a partial match of s2
var s0 = "";
var s1 = "si";
var s2 = "site";

var observer = {
  results: null,
  search: null,
  runCount: -1,
  observe: function(aSubject, aTopic, aData)
  {
    if (--this.runCount > 0)
      return;
    ensure_results(this.results, this.search);
  }
};
Services.obs.addObserver(observer, PlacesUtils.TOPIC_FEEDBACK_UPDATED, false);

/**
 * Make the result object for a given URI that will be passed to ensure_results.
 */
function makeResult(aURI, aStyle = "favicon") {
  return {
    uri: aURI,
    style: aStyle,
  };
}

var tests = [
  // Test things without a search term.
  function*() {
    print("Test 0 same count, diff rank, same term; no search");
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s0;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c1, s2);
    yield task_setCountRank(uri2, c1, c2, s2);
  },
  function*() {
    print("Test 1 same count, diff rank, same term; no search");
    observer.results = [
      makeResult(uri2),
      makeResult(uri1),
    ];
    observer.search = s0;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c2, s2);
    yield task_setCountRank(uri2, c1, c1, s2);
  },
  function*() {
    print("Test 2 diff count, same rank, same term; no search");
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s0;
    observer.runCount = c1 + c1;
    yield task_setCountRank(uri1, c1, c1, s2);
    yield task_setCountRank(uri2, c2, c1, s2);
  },
  function*() {
    print("Test 3 diff count, same rank, same term; no search");
    observer.results = [
      makeResult(uri2),
      makeResult(uri1),
    ];
    observer.search = s0;
    observer.runCount = c1 + c1;
    yield task_setCountRank(uri1, c2, c1, s2);
    yield task_setCountRank(uri2, c1, c1, s2);
  },

  // Test things with a search term (exact match one, partial other).
  function*() {
    print("Test 4 same count, same rank, diff term; one exact/one partial search");
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s1;
    observer.runCount = c1 + c1;
    yield task_setCountRank(uri1, c1, c1, s1);
    yield task_setCountRank(uri2, c1, c1, s2);
  },
  function*() {
    print("Test 5 same count, same rank, diff term; one exact/one partial search");
    observer.results = [
      makeResult(uri2),
      makeResult(uri1),
    ];
    observer.search = s1;
    observer.runCount = c1 + c1;
    yield task_setCountRank(uri1, c1, c1, s2);
    yield task_setCountRank(uri2, c1, c1, s1);
  },

  // Test things with a search term (exact match both).
  function*() {
    print("Test 6 same count, diff rank, same term; both exact search");
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s1;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c1, s1);
    yield task_setCountRank(uri2, c1, c2, s1);
  },
  function*() {
    print("Test 7 same count, diff rank, same term; both exact search");
    observer.results = [
      makeResult(uri2),
      makeResult(uri1),
    ];
    observer.search = s1;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c2, s1);
    yield task_setCountRank(uri2, c1, c1, s1);
  },

  // Test things with a search term (partial match both).
  function*() {
    print("Test 8 same count, diff rank, same term; both partial search");
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s1;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c1, s2);
    yield task_setCountRank(uri2, c1, c2, s2);
  },
  function*() {
    print("Test 9 same count, diff rank, same term; both partial search");
    observer.results = [
      makeResult(uri2),
      makeResult(uri1),
    ];
    observer.search = s1;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c2, s2);
    yield task_setCountRank(uri2, c1, c1, s2);
  },
  function*() {
    print("Test 10 same count, same rank, same term, decay first; exact match");
    observer.results = [
      makeResult(uri2),
      makeResult(uri1),
    ];
    observer.search = s1;
    observer.runCount = c1 + c1;
    yield task_setCountRank(uri1, c1, c1, s1);
    doAdaptiveDecay();
    yield task_setCountRank(uri2, c1, c1, s1);
  },
  function*() {
    print("Test 11 same count, same rank, same term, decay second; exact match");
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s1;
    observer.runCount = c1 + c1;
    yield task_setCountRank(uri2, c1, c1, s1);
    doAdaptiveDecay();
    yield task_setCountRank(uri1, c1, c1, s1);
  },
  // Test that bookmarks are hidden if the preferences are set right.
  function*() {
    print("Test 12 same count, diff rank, same term; no search; history only");
    Services.prefs.setBoolPref("browser.urlbar.suggest.history", true);
    Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", false);
    Services.prefs.setBoolPref("browser.urlbar.suggest.openpage", false);
    observer.results = [
      makeResult(uri1),
      makeResult(uri2),
    ];
    observer.search = s0;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c1, s2, "bookmark");
    yield task_setCountRank(uri2, c1, c2, s2);
  },
  // Test that tags are shown if the preferences are set right.
  function*() {
    print("Test 13 same count, diff rank, same term; no search; history only with tag");
    Services.prefs.setBoolPref("browser.urlbar.suggest.history", true);
    Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", false);
    Services.prefs.setBoolPref("browser.urlbar.suggest.openpage", false);
    observer.results = [
      makeResult(uri1, "tag"),
      makeResult(uri2),
    ];
    observer.search = s0;
    observer.runCount = c1 + c2;
    yield task_setCountRank(uri1, c1, c1, s2, "tag");
    yield task_setCountRank(uri2, c1, c2, s2);
  },
];

/**
 * This deferred object contains a promise that is resolved when the
 * ensure_results function has finished its execution.
 */
var deferEnsureResults;

/**
 * Test adaptive autocomplete.
 */
add_task(function* test_adaptive()
{
  // Disable autoFill for this test.
  Services.prefs.setBoolPref("browser.urlbar.autoFill", false);
  do_register_cleanup(() => Services.prefs.clearUserPref("browser.urlbar.autoFill"));
  for (let test of tests) {
    // Cleanup.
    PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
    PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.tagsFolderId);
    observer.runCount = -1;

    let types = ["history", "bookmark", "openpage"];
    for (let type of types) {
      Services.prefs.clearUserPref("browser.urlbar.suggest." + type);
    }

    yield PlacesTestUtils.clearHistory();

    deferEnsureResults = Promise.defer();
    yield test();
    yield deferEnsureResults.promise;
  }

  Services.obs.removeObserver(observer, PlacesUtils.TOPIC_FEEDBACK_UPDATED);
});