toolkit/components/autocomplete/tests/unit/test_stopSearch.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_stopSearch.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/*
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +/**
    1.10 + * Purpose of the test is to check that a stopSearch call comes always before a
    1.11 + * startSearch call.
    1.12 + */
    1.13 +
    1.14 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +
    1.16 +
    1.17 +/**
    1.18 + * Dummy nsIAutoCompleteInput source that returns
    1.19 + * the given list of AutoCompleteSearch names.
    1.20 + *
    1.21 + * Implements only the methods needed for this test.
    1.22 + */
    1.23 +function AutoCompleteInput(aSearches)
    1.24 +{
    1.25 +  this.searches = aSearches;
    1.26 +}
    1.27 +AutoCompleteInput.prototype = {
    1.28 +  constructor: AutoCompleteInput,
    1.29 +  minResultsForPopup: 0,
    1.30 +  timeout: 10,
    1.31 +  searchParam: "",
    1.32 +  textValue: "hello",
    1.33 +  disableAutoComplete: false, 
    1.34 +  completeDefaultIndex: false,
    1.35 +  set popupOpen(val) { return val; }, // ignore
    1.36 +  get popupOpen() { return false; },
    1.37 +  get searchCount() { return this.searches.length; },
    1.38 +  getSearchAt: function(aIndex) { return this.searches[aIndex]; },
    1.39 +  onSearchBegin: function() {},
    1.40 +  onSearchComplete: function() {},
    1.41 +  onTextReverted: function () {},
    1.42 +  onTextEntered: function () {},
    1.43 +  popup: {
    1.44 +    selectBy: function() {},
    1.45 +    invalidate: function() {},
    1.46 +    set selectedIndex(val) { return val; }, // ignore
    1.47 +    get selectedIndex() { return -1 },
    1.48 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup])
    1.49 +  },
    1.50 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput])
    1.51 +}
    1.52 +
    1.53 +
    1.54 +/**
    1.55 + * nsIAutoCompleteSearch implementation.
    1.56 + */
    1.57 +function AutoCompleteSearch(aName)
    1.58 +{
    1.59 +  this.name = aName;
    1.60 +}
    1.61 +AutoCompleteSearch.prototype = {
    1.62 +  constructor: AutoCompleteSearch,
    1.63 +  stopSearchInvoked: true,
    1.64 +  startSearch: function(aSearchString, aSearchParam, aPreviousResult, aListener)
    1.65 +  {
    1.66 +    print("Check stop search has been called");
    1.67 +    do_check_true(this.stopSearchInvoked);
    1.68 +    this.stopSearchInvoked = false;
    1.69 +  },
    1.70 +  stopSearch: function()
    1.71 +  {
    1.72 +    this.stopSearchInvoked = true;
    1.73 +  },
    1.74 +  QueryInterface: XPCOMUtils.generateQI([
    1.75 +    Ci.nsIFactory
    1.76 +  , Ci.nsIAutoCompleteSearch
    1.77 +  ]),
    1.78 +  createInstance: function(outer, iid)
    1.79 +  {
    1.80 +    return this.QueryInterface(iid);
    1.81 +  }
    1.82 +}
    1.83 +
    1.84 +
    1.85 +/** 
    1.86 + * Helper to register an AutoCompleteSearch with the given name.
    1.87 + * Allows the AutoCompleteController to find the search.
    1.88 + */
    1.89 +function registerAutoCompleteSearch(aSearch)
    1.90 +{
    1.91 +  let name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
    1.92 +  let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
    1.93 +                      getService(Ci.nsIUUIDGenerator);
    1.94 +  let cid = uuidGenerator.generateUUID();
    1.95 +  let desc = "Test AutoCompleteSearch";
    1.96 +  let componentManager = Components.manager
    1.97 +                                   .QueryInterface(Ci.nsIComponentRegistrar);
    1.98 +  componentManager.registerFactory(cid, desc, name, aSearch);
    1.99 +  // Keep the id on the object so we can unregister later
   1.100 +  aSearch.cid = cid; 
   1.101 +}
   1.102 +
   1.103 +
   1.104 +/** 
   1.105 + * Helper to unregister an AutoCompleteSearch. 
   1.106 + */
   1.107 +function unregisterAutoCompleteSearch(aSearch) {
   1.108 +  let componentManager = Components.manager
   1.109 +                                   .QueryInterface(Ci.nsIComponentRegistrar);  
   1.110 +  componentManager.unregisterFactory(aSearch.cid, aSearch);
   1.111 +}
   1.112 +
   1.113 +
   1.114 +let gTests = [
   1.115 +  function(controller) {
   1.116 +    print("handleText");
   1.117 +    controller.input.textValue = "hel";
   1.118 +    controller.handleText();
   1.119 +  },
   1.120 +  function(controller) {
   1.121 +    print("handleStartComposition");
   1.122 +    controller.handleStartComposition();
   1.123 +  },
   1.124 +  function(controller) {
   1.125 +    print("handleEndComposition");
   1.126 +    controller.handleEndComposition();
   1.127 +    // an input event always follows compositionend event.
   1.128 +    controller.handleText();
   1.129 +  },
   1.130 +  function(controller) {
   1.131 +    print("handleEscape");
   1.132 +    controller.handleEscape();
   1.133 +  },
   1.134 +  function(controller) {
   1.135 +    print("handleEnter");
   1.136 +    controller.handleEnter(false);
   1.137 +  },
   1.138 +  function(controller) {
   1.139 +    print("handleTab");
   1.140 +    controller.handleTab();
   1.141 +  },
   1.142 +
   1.143 +  function(controller) {
   1.144 +    print("handleKeyNavigation");
   1.145 +    controller.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_UP);
   1.146 +  },
   1.147 +];
   1.148 +
   1.149 +
   1.150 +let gSearch;
   1.151 +let gCurrentTest;
   1.152 +function run_test() {
   1.153 +  // Make an AutoCompleteSearch that always returns nothing
   1.154 +  gSearch = new AutoCompleteSearch("test");
   1.155 +  registerAutoCompleteSearch(gSearch);
   1.156 +
   1.157 +  let controller = Cc["@mozilla.org/autocomplete/controller;1"].
   1.158 +                   getService(Ci.nsIAutoCompleteController);
   1.159 +
   1.160 +  // Make an AutoCompleteInput that uses our search.
   1.161 +  let input = new AutoCompleteInput([gSearch.name]);
   1.162 +  controller.input = input;
   1.163 +
   1.164 +  input.onSearchBegin = function() {
   1.165 +    do_execute_soon(function() {
   1.166 +      gCurrentTest(controller);
   1.167 +    });
   1.168 +  };
   1.169 +  input.onSearchComplete = function() {
   1.170 +    run_next_test(controller);
   1.171 +  }
   1.172 +
   1.173 +  // Search is asynchronous, so don't let the test finish immediately
   1.174 +  do_test_pending();
   1.175 +
   1.176 +  run_next_test(controller);
   1.177 +}
   1.178 +
   1.179 +function run_next_test(controller) {
   1.180 +  if (gTests.length == 0) {
   1.181 +    unregisterAutoCompleteSearch(gSearch);
   1.182 +    controller.stopSearch();
   1.183 +    controller.input = null;
   1.184 +    do_test_finished();
   1.185 +    return;
   1.186 +  }
   1.187 +
   1.188 +  gCurrentTest = gTests.shift();
   1.189 +  controller.startSearch("hello");
   1.190 +}

mercurial