toolkit/components/autocomplete/tests/unit/test_660156.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 /**
     5  * Search object that returns results at different times.
     6  * First, the search that returns results asynchronously.
     7  */
     8 function AutoCompleteAsyncSearch(aName, aResult) {
     9   this.name = aName;
    10   this._result = aResult;
    11 }
    12 AutoCompleteAsyncSearch.prototype = Object.create(AutoCompleteSearchBase.prototype);
    13 AutoCompleteAsyncSearch.prototype.startSearch = function(aSearchString, 
    14                                                          aSearchParam, 
    15                                                          aPreviousResult, 
    16                                                          aListener) {
    17   setTimeout(this._returnResults.bind(this), 500, aListener);
    18 };
    20 AutoCompleteAsyncSearch.prototype._returnResults = function(aListener) {
    21   var result = this._result;
    23   result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
    24   aListener.onSearchResult(this, result);
    25 };
    27 /**
    28  * The synchronous version
    29  */
    30 function AutoCompleteSyncSearch(aName, aResult) {
    31   this.name = aName;
    32   this._result = aResult;
    33 }
    34 AutoCompleteSyncSearch.prototype = Object.create(AutoCompleteAsyncSearch.prototype);
    35 AutoCompleteSyncSearch.prototype.startSearch = function(aSearchString, 
    36                                                         aSearchParam, 
    37                                                         aPreviousResult, 
    38                                                         aListener) {
    39   this._returnResults(aListener);
    40 };
    42 /**
    43  * Results object
    44  */
    45 function AutoCompleteResult(aValues, aDefaultIndex) {
    46   this._values = aValues;
    47   this.defaultIndex = aDefaultIndex;
    48 }
    49 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);
    52 /** 
    53  * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them
    54  * (index != 0) returning before the rest.
    55  */
    56 function run_test() {
    57   do_test_pending();
    59   var results = ["mozillaTest"];
    60   var inputStr = "moz";
    62   // Async search
    63   var asyncSearch = new AutoCompleteAsyncSearch("Async", 
    64                                                 new AutoCompleteResult(results, -1));
    65   // Sync search
    66   var syncSearch = new AutoCompleteSyncSearch("Sync",
    67                                               new AutoCompleteResult(results, 0));
    69   // Register searches so AutoCompleteController can find them
    70   registerAutoCompleteSearch(asyncSearch);
    71   registerAutoCompleteSearch(syncSearch);
    73   var controller = Cc["@mozilla.org/autocomplete/controller;1"].
    74                    getService(Ci.nsIAutoCompleteController);  
    76   // Make an AutoCompleteInput that uses our searches
    77   // and confirms results on search complete.
    78   // Async search MUST be FIRST to trigger the bug this tests.
    79   var input = new AutoCompleteInputBase([asyncSearch.name, syncSearch.name]);
    80   input.completeDefaultIndex = true;
    81   input.textValue = inputStr;
    83   // Caret must be at the end. Autofill doesn't happen unless you're typing
    84   // characters at the end.
    85   var strLen = inputStr.length;
    86   input.selectTextRange(strLen, strLen);
    88   controller.input = input;
    89   controller.startSearch(inputStr);
    91   input.onSearchComplete = function() {
    92     do_check_eq(input.textValue, results[0]);
    94     // Unregister searches
    95     unregisterAutoCompleteSearch(asyncSearch);
    96     unregisterAutoCompleteSearch(syncSearch);
    97     do_test_finished();
    98   };
    99 }

mercurial