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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_popupSelectionVsDefaultCompleteValue.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function AutoCompleteTypeAheadResult(aValues, aFinalCompleteValues) {
     1.9 +  this._values = aValues;
    1.10 +  this._finalCompleteValues = aFinalCompleteValues;
    1.11 +  this.defaultIndex = 0;
    1.12 +  this._typeAheadResult = true;
    1.13 +}
    1.14 +AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype);
    1.15 +
    1.16 +function AutoCompleteResult(aValues) {
    1.17 +  this._values = aValues;
    1.18 +}
    1.19 +AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);
    1.20 +
    1.21 +function AutoCompleteInput(aSearches) {
    1.22 +  this.searches = aSearches;
    1.23 +  this.popupOpen = true;
    1.24 +  this.completeDefaultIndex = true;
    1.25 +  this.completeSelectedIndex = true;
    1.26 +}
    1.27 +AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype);
    1.28 +
    1.29 +function run_test() {
    1.30 +  run_next_test();
    1.31 +}
    1.32 +
    1.33 +add_test(function test_handleEnter() {
    1.34 +  doSearch("moz", function(aController) {
    1.35 +    do_check_eq(aController.input.textValue, "mozilla.com");
    1.36 +    aController.handleEnter(true);
    1.37 +    do_check_eq(aController.input.textValue, "mozilla.org");
    1.38 +  });
    1.39 +});
    1.40 +
    1.41 +function doSearch(aSearchString, aOnCompleteCallback) {
    1.42 +  let typeAheadSearch = new AutoCompleteSearchBase(
    1.43 +    "typeAheadSearch",
    1.44 +    new AutoCompleteTypeAheadResult([ "mozilla.com" ], [ "http://www.mozilla.com" ])
    1.45 +  );
    1.46 +  registerAutoCompleteSearch(typeAheadSearch);
    1.47 +
    1.48 +  let search = new AutoCompleteSearchBase(
    1.49 +    "search",
    1.50 +    new AutoCompleteResult([ "mozilla.org" ])
    1.51 +  );
    1.52 +  registerAutoCompleteSearch(search);
    1.53 +
    1.54 +  let controller = Cc["@mozilla.org/autocomplete/controller;1"].
    1.55 +                   getService(Ci.nsIAutoCompleteController);
    1.56 +
    1.57 +  // Make an AutoCompleteInput that uses our searches and confirms results.
    1.58 +  let input = new AutoCompleteInput([ typeAheadSearch.name, search.name ]);
    1.59 +  input.textValue = aSearchString;
    1.60 +
    1.61 +  // Caret must be at the end for autofill to happen.
    1.62 +  let strLen = aSearchString.length;
    1.63 +  input.selectTextRange(strLen, strLen);
    1.64 +  controller.input = input;
    1.65 +  controller.startSearch(aSearchString);
    1.66 +
    1.67 +  input.onSearchComplete = function onSearchComplete() {
    1.68 +    aOnCompleteCallback(controller);
    1.69 +
    1.70 +    // Clean up.
    1.71 +    unregisterAutoCompleteSearch(search);
    1.72 +    run_next_test();
    1.73 +  };
    1.74 +}

mercurial