toolkit/components/places/tests/unit/test_adaptive.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 /**
     8  * Test for bug 395739 to make sure the feedback to the search results in those
     9  * entries getting better ranks. Additionally, exact matches should be ranked
    10  * higher. Because the interactions among adaptive rank and visit counts is not
    11  * well defined, this test holds one of the two values constant when modifying
    12  * the other.
    13  *
    14  * This also tests bug 395735 for the instrumentation feedback mechanism.
    15  *
    16  * Bug 411293 is tested to make sure the drop down strongly prefers previously
    17  * typed pages that have been selected and are moved to the top with adaptive
    18  * learning.
    19  */
    21 function AutoCompleteInput(aSearches) {
    22   this.searches = aSearches;
    23 }
    24 AutoCompleteInput.prototype = {
    25   constructor: AutoCompleteInput,
    27   get minResultsForPopup() 0,
    28   get timeout() 10,
    29   get searchParam() "",
    30   get textValue() "",
    31   get disableAutoComplete() false,
    32   get completeDefaultIndex() false,
    34   get searchCount() this.searches.length,
    35   getSearchAt: function (aIndex) this.searches[aIndex],
    37   onSearchBegin: function () {},
    38   onSearchComplete: function() {},
    40   get popupOpen() false,
    41   popup: {
    42     set selectedIndex(aIndex) aIndex,
    43     invalidate: function () {},
    44     QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup])
    45   },
    47   QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput])
    48 }
    50 /**
    51  * Checks that autocomplete results are ordered correctly.
    52  */
    53 function ensure_results(expected, searchTerm)
    54 {
    55   let controller = Cc["@mozilla.org/autocomplete/controller;1"].
    56                    getService(Ci.nsIAutoCompleteController);
    58   // Make an AutoCompleteInput that uses our searches
    59   // and confirms results on search complete.
    60   let input = new AutoCompleteInput(["history"]);
    62   controller.input = input;
    64   input.onSearchComplete = function() {
    65     do_check_eq(controller.searchStatus,
    66                 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
    67     do_check_eq(controller.matchCount, expected.length);
    68     for (let i = 0; i < controller.matchCount; i++) {
    69       print("Testing for '" + expected[i].uri.spec + "' got '" + controller.getValueAt(i) + "'");
    70       do_check_eq(controller.getValueAt(i), expected[i].uri.spec);
    71       do_check_eq(controller.getStyleAt(i), expected[i].style);
    72     }
    74     deferEnsureResults.resolve();
    75   };
    77   controller.startSearch(searchTerm);
    78 }
    80 /**
    81  * Asynchronous task that bumps up the rank for an uri.
    82  */
    83 function task_setCountRank(aURI, aCount, aRank, aSearch, aBookmark)
    84 {
    85   // Bump up the visit count for the uri.
    86   let visits = [];
    87   for (let i = 0; i < aCount; i++) {
    88     visits.push({ uri: aURI, visitDate: d1, transition: TRANSITION_TYPED });
    89   }
    90   yield promiseAddVisits(visits);
    92   // Make a nsIAutoCompleteController and friends for instrumentation feedback.
    93   let thing = {
    94     QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput,
    95                                            Ci.nsIAutoCompletePopup,
    96                                            Ci.nsIAutoCompleteController]),
    97     get popup() thing,
    98     get controller() thing,
    99     popupOpen: true,
   100     selectedIndex: 0,
   101     getValueAt: function() aURI.spec,
   102     searchString: aSearch
   103   };
   105   // Bump up the instrumentation feedback.
   106   for (let i = 0; i < aRank; i++) {
   107     Services.obs.notifyObservers(thing, "autocomplete-will-enter-text", null);
   108   }
   110   // If this is supposed to be a bookmark, add it.
   111   if (aBookmark) {
   112     PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
   113                                          aURI,
   114                                          PlacesUtils.bookmarks.DEFAULT_INDEX,
   115                                          "test_book");
   117     // And add the tag if we need to.
   118     if (aBookmark == "tag") {
   119       PlacesUtils.tagging.tagURI(aURI, ["test_tag"]);
   120     }
   121   }
   122 }
   124 /**
   125  * Decay the adaptive entries by sending the daily idle topic.
   126  */
   127 function doAdaptiveDecay()
   128 {
   129   PlacesUtils.history.runInBatchMode({
   130     runBatched: function() {
   131       for (let i = 0; i < 10; i++) {
   132         PlacesUtils.history.QueryInterface(Ci.nsIObserver)
   133                            .observe(null, "idle-daily", null);
   134       }
   135     }
   136   }, this);
   137 }
   139 let uri1 = uri("http://site.tld/1");
   140 let uri2 = uri("http://site.tld/2");
   142 // d1 is some date for the page visit
   143 let d1 = new Date(Date.now() - 1000 * 60 * 60) * 1000;
   144 // c1 is larger (should show up higher) than c2
   145 let c1 = 10;
   146 let c2 = 1;
   147 // s1 is a partial match of s2
   148 let s0 = "";
   149 let s1 = "si";
   150 let s2 = "site";
   152 let observer = {
   153   results: null,
   154   search: null,
   155   runCount: -1,
   156   observe: function(aSubject, aTopic, aData)
   157   {
   158     if (--this.runCount > 0)
   159       return;
   160     ensure_results(this.results, this.search);
   161   }
   162 };
   163 Services.obs.addObserver(observer, PlacesUtils.TOPIC_FEEDBACK_UPDATED, false);
   165 /**
   166  * Make the result object for a given URI that will be passed to ensure_results.
   167  */
   168 function makeResult(aURI) {
   169   return {
   170     uri: aURI,
   171     style: "favicon",
   172   };
   173 }
   175 let tests = [
   176   // Test things without a search term.
   177   function() {
   178     print("Test 0 same count, diff rank, same term; no search");
   179     observer.results = [
   180       makeResult(uri1),
   181       makeResult(uri2),
   182     ];
   183     observer.search = s0;
   184     observer.runCount = c1 + c2;
   185     yield task_setCountRank(uri1, c1, c1, s2);
   186     yield task_setCountRank(uri2, c1, c2, s2);
   187   },
   188   function() {
   189     print("Test 1 same count, diff rank, same term; no search");
   190     observer.results = [
   191       makeResult(uri2),
   192       makeResult(uri1),
   193     ];
   194     observer.search = s0;
   195     observer.runCount = c1 + c2;
   196     yield task_setCountRank(uri1, c1, c2, s2);
   197     yield task_setCountRank(uri2, c1, c1, s2);
   198   },
   199   function() {
   200     print("Test 2 diff count, same rank, same term; no search");
   201     observer.results = [
   202       makeResult(uri1),
   203       makeResult(uri2),
   204     ];
   205     observer.search = s0;
   206     observer.runCount = c1 + c1;
   207     yield task_setCountRank(uri1, c1, c1, s2);
   208     yield task_setCountRank(uri2, c2, c1, s2);
   209   },
   210   function() {
   211     print("Test 3 diff count, same rank, same term; no search");
   212     observer.results = [
   213       makeResult(uri2),
   214       makeResult(uri1),
   215     ];
   216     observer.search = s0;
   217     observer.runCount = c1 + c1;
   218     yield task_setCountRank(uri1, c2, c1, s2);
   219     yield task_setCountRank(uri2, c1, c1, s2);
   220   },
   222   // Test things with a search term (exact match one, partial other).
   223   function() {
   224     print("Test 4 same count, same rank, diff term; one exact/one partial search");
   225     observer.results = [
   226       makeResult(uri1),
   227       makeResult(uri2),
   228     ];
   229     observer.search = s1;
   230     observer.runCount = c1 + c1;
   231     yield task_setCountRank(uri1, c1, c1, s1);
   232     yield task_setCountRank(uri2, c1, c1, s2);
   233   },
   234   function() {
   235     print("Test 5 same count, same rank, diff term; one exact/one partial search");
   236     observer.results = [
   237       makeResult(uri2),
   238       makeResult(uri1),
   239     ];
   240     observer.search = s1;
   241     observer.runCount = c1 + c1;
   242     yield task_setCountRank(uri1, c1, c1, s2);
   243     yield task_setCountRank(uri2, c1, c1, s1);
   244   },
   246   // Test things with a search term (exact match both).
   247   function() {
   248     print("Test 6 same count, diff rank, same term; both exact search");
   249     observer.results = [
   250       makeResult(uri1),
   251       makeResult(uri2),
   252     ];
   253     observer.search = s1;
   254     observer.runCount = c1 + c2;
   255     yield task_setCountRank(uri1, c1, c1, s1);
   256     yield task_setCountRank(uri2, c1, c2, s1);
   257   },
   258   function() {
   259     print("Test 7 same count, diff rank, same term; both exact search");
   260     observer.results = [
   261       makeResult(uri2),
   262       makeResult(uri1),
   263     ];
   264     observer.search = s1;
   265     observer.runCount = c1 + c2;
   266     yield task_setCountRank(uri1, c1, c2, s1);
   267     yield task_setCountRank(uri2, c1, c1, s1);
   268   },
   270   // Test things with a search term (partial match both).
   271   function() {
   272     print("Test 8 same count, diff rank, same term; both partial search");
   273     observer.results = [
   274       makeResult(uri1),
   275       makeResult(uri2),
   276     ];
   277     observer.search = s1;
   278     observer.runCount = c1 + c2;
   279     yield task_setCountRank(uri1, c1, c1, s2);
   280     yield task_setCountRank(uri2, c1, c2, s2);
   281   },
   282   function() {
   283     print("Test 9 same count, diff rank, same term; both partial search");
   284     observer.results = [
   285       makeResult(uri2),
   286       makeResult(uri1),
   287     ];
   288     observer.search = s1;
   289     observer.runCount = c1 + c2;
   290     yield task_setCountRank(uri1, c1, c2, s2);
   291     yield task_setCountRank(uri2, c1, c1, s2);
   292   },
   293   function() {
   294     print("Test 10 same count, same rank, same term, decay first; exact match");
   295     observer.results = [
   296       makeResult(uri2),
   297       makeResult(uri1),
   298     ];
   299     observer.search = s1;
   300     observer.runCount = c1 + c1;
   301     yield task_setCountRank(uri1, c1, c1, s1);
   302     doAdaptiveDecay();
   303     yield task_setCountRank(uri2, c1, c1, s1);
   304   },
   305   function() {
   306     print("Test 11 same count, same rank, same term, decay second; exact match");
   307     observer.results = [
   308       makeResult(uri1),
   309       makeResult(uri2),
   310     ];
   311     observer.search = s1;
   312     observer.runCount = c1 + c1;
   313     yield task_setCountRank(uri2, c1, c1, s1);
   314     doAdaptiveDecay();
   315     yield task_setCountRank(uri1, c1, c1, s1);
   316   },
   317   // Test that bookmarks or tags are hidden if the preferences are set right.
   318   function() {
   319     print("Test 12 same count, diff rank, same term; no search; history only");
   320     Services.prefs.setIntPref("browser.urlbar.matchBehavior",
   321                               Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY);
   322     observer.results = [
   323       makeResult(uri1),
   324       makeResult(uri2),
   325     ];
   326     observer.search = s0;
   327     observer.runCount = c1 + c2;
   328     yield task_setCountRank(uri1, c1, c1, s2, "bookmark");
   329     yield task_setCountRank(uri2, c1, c2, s2);
   330   },
   331   function() {
   332     print("Test 13 same count, diff rank, same term; no search; history only with tag");
   333     Services.prefs.setIntPref("browser.urlbar.matchBehavior",
   334                               Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY);
   335     observer.results = [
   336       makeResult(uri1),
   337       makeResult(uri2),
   338     ];
   339     observer.search = s0;
   340     observer.runCount = c1 + c2;
   341     yield task_setCountRank(uri1, c1, c1, s2, "tag");
   342     yield task_setCountRank(uri2, c1, c2, s2);
   343   },
   344 ];
   346 /**
   347  * This deferred object contains a promise that is resolved when the
   348  * ensure_results function has finished its execution.
   349  */
   350 let deferEnsureResults;
   352 /**
   353  * Test adaptive autocomplete.
   354  */
   355 function run_test()
   356 {
   357   run_next_test();
   358 }
   360 add_task(function test_adaptive()
   361 {
   362   for (let [, test] in Iterator(tests)) {
   363     // Cleanup.
   364     PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
   365     PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.tagsFolderId);
   366     observer.runCount = -1;
   368     yield promiseClearHistory();
   370     deferEnsureResults = Promise.defer();
   371     yield test();
   372     yield deferEnsureResults.promise;
   373   }
   375   Services.obs.removeObserver(observer, PlacesUtils.TOPIC_FEEDBACK_UPDATED);
   376 });

mercurial