toolkit/content/tests/chrome/test_autocomplete4.xul

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 <?xml version="1.0"?>
     2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     5 <window title="Autocomplete Widget Test 4"
     6         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     8   <script type="application/javascript"
     9           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    10   <script type="application/javascript"
    11           src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    13 <textbox id="autocomplete"
    14          type="autocomplete"
    15          completedefaultindex="true"
    17          onsearchcomplete="searchComplete();"
    18          autocompletesearch="simple"/>
    20 <script class="testbody" type="application/javascript">
    21 <![CDATA[
    23 // Set to indicate whether or not we want autoCompleteSimple to return a result
    24 var returnResult = true;
    26 const ACR = Components.interfaces.nsIAutoCompleteResult;
    28 // This result can't be constructed in-line, because otherwise we leak memory.
    29 function nsAutoCompleteSimpleResult(aString)
    30 {
    31   this.searchString = aString;
    32   if (returnResult) {
    33     this.searchResult = ACR.RESULT_SUCCESS;
    34     this.matchCount = 1;
    35     this._param = "Result";
    36   }
    37 }
    39 nsAutoCompleteSimpleResult.prototype = {
    40  _param: "",
    41  searchString: null,
    42  searchResult: ACR.RESULT_FAILURE,
    43  defaultIndex: 0,
    44  errorDescription: null,
    45  matchCount: 0,
    46  getValueAt: function() { return this._param; },
    47  getCommentAt: function() { return null; },
    48  getStyleAt: function() { return null; },
    49  getImageAt: function() { return null; },
    50  getFinalCompleteValueAt: function() { return this.getValueAt(); },
    51  getLabelAt: function() { return null; },
    52  removeValueAt: function() {}
    53 };
    55 // A basic autocomplete implementation that either returns one result or none
    56 var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca");
    57 var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple"
    58 var autoCompleteSimple = {
    59   QueryInterface: function(iid) {
    60     if (iid.equals(Components.interfaces.nsISupports) ||
    61         iid.equals(Components.interfaces.nsIFactory) ||
    62         iid.equals(Components.interfaces.nsIAutoCompleteSearch))
    63       return this;
    65     throw Components.results.NS_ERROR_NO_INTERFACE;
    66   },
    68   createInstance: function(outer, iid) {
    69     return this.QueryInterface(iid);
    70   },
    72   startSearch: function(aString, aParam, aResult, aListener) {
    73     var result = new nsAutoCompleteSimpleResult(aString);
    74     aListener.onSearchResult(this, result);
    75   },
    77   stopSearch: function() {}
    78 };
    80 var componentManager = Components.manager
    81                                  .QueryInterface(Components.interfaces.nsIComponentRegistrar);
    82 componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete",
    83                                  autoCompleteSimpleName, autoCompleteSimple);
    86 // Test Bug 325842 - completeDefaultIndex
    88 SimpleTest.waitForExplicitFinish();
    89 setTimeout(nextTest, 0);
    91 var currentTest = null;
    93 // Note the entries for these tests (key) are incremental.
    94 const tests = [
    95   {
    96     desc: "HOME key remove selection",
    97     key: "VK_HOME",
    98     removeSelection: true,
    99     result: "re",
   100     start: 0, end: 0
   101   },
   102   {
   103     desc: "LEFT key remove selection",
   104     key: "VK_LEFT",
   105     removeSelection: true,
   106     result: "re",
   107     start: 1, end: 1
   108   },
   109   { desc: "RIGHT key remove selection",
   110     key: "VK_RIGHT",
   111     removeSelection: true,
   112     result: "re",
   113     start: 2, end: 2
   114   },
   115   { desc: "ENTER key remove selection",
   116     key: "VK_RETURN",
   117     removeSelection: true,
   118     result: "re",
   119     start: 2, end: 2
   120   },
   121   {
   122     desc: "HOME key",
   123     key: "VK_HOME",
   124     removeSelection: false,
   125     result: "Result",
   126     start: 0, end: 0
   127   },
   128   {
   129     desc: "LEFT key",
   130     key: "VK_LEFT",
   131     removeSelection: false,
   132     result: "Result",
   133     start: 5, end: 5
   134   },
   135   { desc: "RIGHT key",
   136     key: "VK_RIGHT",
   137     removeSelection: false,
   138     result: "Result",
   139     start: 6, end: 6
   140   },
   141   { desc: "RETURN key",
   142     key: "VK_RETURN",
   143     removeSelection: false,
   144     result: "Result",
   145     start: 6, end: 6
   146   },
   147   { desc: "TAB key should confirm suggestion when forcecomplete is set",
   148     key: "VK_TAB",
   149     removeSelection: false,
   150     forceComplete: true,
   151     result: "Result",
   152     start: 6, end: 6
   153   },
   154 ];
   156 function nextTest() {
   157   if (!tests.length) {
   158     // No more tests to run, finish.
   159     setTimeout(function() {
   160       // Unregister the factory so that we don't get in the way of other tests
   161       componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple);
   162       SimpleTest.finish();
   163     }, 0);
   164     return;
   165   }
   167   var autocomplete = $("autocomplete");
   168   autocomplete.value = "";
   169   currentTest = tests.shift();
   171   // HOME key works differently on Mac, so we skip tests using it.
   172   if (currentTest.key == "VK_HOME" && navigator.platform.indexOf("Mac") != -1)
   173     nextTest();
   174   else
   175     setTimeout(runCurrentTest, 0);
   176 }
   178 function runCurrentTest() {
   179   var autocomplete = $("autocomplete");
   181   autocomplete.focus();
   183   synthesizeKey("r", {});
   184   synthesizeKey("e", {});
   185 }
   187 function searchComplete() {
   188   var autocomplete = $("autocomplete");
   189   is(autocomplete.value, "result",
   190      "Test '" + currentTest.desc + "': autocomplete.value should equal 'result'");
   192   autocomplete.setAttribute("forcecomplete", currentTest.forceComplete ? true : false);
   194   if (autocomplete.selectionStart == 2) {  // Finished inserting "re" string.
   195     if (currentTest.removeSelection) {
   196       // remove current selection
   197       synthesizeKey("VK_DELETE", {});
   198     }
   200     synthesizeKey(currentTest.key, {});
   202     checkResult();
   203   }
   204 }
   206 function checkResult() {
   207   var autocomplete = $("autocomplete");
   209   is(autocomplete.value, currentTest.result,
   210      "Test '" + currentTest.desc + "': autocomplete.value should equal '" +
   211      currentTest.result + "'");
   213   is(autocomplete.selectionStart, currentTest.start,
   214      "Test '" + currentTest.desc + "': autocomplete selection should start at " +
   215      currentTest.start);
   217   is(autocomplete.selectionEnd, currentTest.end,
   218      "Test '" + currentTest.desc + "': autocomplete selection should end at " +
   219      currentTest.end);
   221   setTimeout(nextTest, 0);
   222 }
   224 ]]>
   225 </script>
   227 <body xmlns="http://www.w3.org/1999/xhtml">
   228 <p id="display">
   229 </p>
   230 <div id="content" style="display: none">
   231 </div>
   232 <pre id="test">
   233 </pre>
   234 </body>
   236 </window>

mercurial