toolkit/content/tests/chrome/test_autocomplete4.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/chrome/test_autocomplete4.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,236 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     1.7 +
     1.8 +<window title="Autocomplete Widget Test 4"
     1.9 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.10 +
    1.11 +  <script type="application/javascript"
    1.12 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.13 +  <script type="application/javascript"
    1.14 +          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    1.15 +
    1.16 +<textbox id="autocomplete"
    1.17 +         type="autocomplete"
    1.18 +         completedefaultindex="true"
    1.19 +
    1.20 +         onsearchcomplete="searchComplete();"
    1.21 +         autocompletesearch="simple"/>
    1.22 +
    1.23 +<script class="testbody" type="application/javascript">
    1.24 +<![CDATA[
    1.25 +
    1.26 +// Set to indicate whether or not we want autoCompleteSimple to return a result
    1.27 +var returnResult = true;
    1.28 +
    1.29 +const ACR = Components.interfaces.nsIAutoCompleteResult;
    1.30 +
    1.31 +// This result can't be constructed in-line, because otherwise we leak memory.
    1.32 +function nsAutoCompleteSimpleResult(aString)
    1.33 +{
    1.34 +  this.searchString = aString;
    1.35 +  if (returnResult) {
    1.36 +    this.searchResult = ACR.RESULT_SUCCESS;
    1.37 +    this.matchCount = 1;
    1.38 +    this._param = "Result";
    1.39 +  }
    1.40 +}
    1.41 +
    1.42 +nsAutoCompleteSimpleResult.prototype = {
    1.43 + _param: "",
    1.44 + searchString: null,
    1.45 + searchResult: ACR.RESULT_FAILURE,
    1.46 + defaultIndex: 0,
    1.47 + errorDescription: null,
    1.48 + matchCount: 0,
    1.49 + getValueAt: function() { return this._param; },
    1.50 + getCommentAt: function() { return null; },
    1.51 + getStyleAt: function() { return null; },
    1.52 + getImageAt: function() { return null; },
    1.53 + getFinalCompleteValueAt: function() { return this.getValueAt(); },
    1.54 + getLabelAt: function() { return null; },
    1.55 + removeValueAt: function() {}
    1.56 +};
    1.57 +
    1.58 +// A basic autocomplete implementation that either returns one result or none
    1.59 +var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca");
    1.60 +var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple"
    1.61 +var autoCompleteSimple = {
    1.62 +  QueryInterface: function(iid) {
    1.63 +    if (iid.equals(Components.interfaces.nsISupports) ||
    1.64 +        iid.equals(Components.interfaces.nsIFactory) ||
    1.65 +        iid.equals(Components.interfaces.nsIAutoCompleteSearch))
    1.66 +      return this;
    1.67 +
    1.68 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.69 +  },
    1.70 +
    1.71 +  createInstance: function(outer, iid) {
    1.72 +    return this.QueryInterface(iid);
    1.73 +  },
    1.74 +
    1.75 +  startSearch: function(aString, aParam, aResult, aListener) {
    1.76 +    var result = new nsAutoCompleteSimpleResult(aString);
    1.77 +    aListener.onSearchResult(this, result);
    1.78 +  },
    1.79 +
    1.80 +  stopSearch: function() {}
    1.81 +};
    1.82 +
    1.83 +var componentManager = Components.manager
    1.84 +                                 .QueryInterface(Components.interfaces.nsIComponentRegistrar);
    1.85 +componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete",
    1.86 +                                 autoCompleteSimpleName, autoCompleteSimple);
    1.87 +
    1.88 +
    1.89 +// Test Bug 325842 - completeDefaultIndex
    1.90 +
    1.91 +SimpleTest.waitForExplicitFinish();
    1.92 +setTimeout(nextTest, 0);
    1.93 +
    1.94 +var currentTest = null;
    1.95 +
    1.96 +// Note the entries for these tests (key) are incremental.
    1.97 +const tests = [
    1.98 +  {
    1.99 +    desc: "HOME key remove selection",
   1.100 +    key: "VK_HOME",
   1.101 +    removeSelection: true,
   1.102 +    result: "re",
   1.103 +    start: 0, end: 0
   1.104 +  },
   1.105 +  {
   1.106 +    desc: "LEFT key remove selection",
   1.107 +    key: "VK_LEFT",
   1.108 +    removeSelection: true,
   1.109 +    result: "re",
   1.110 +    start: 1, end: 1
   1.111 +  },
   1.112 +  { desc: "RIGHT key remove selection",
   1.113 +    key: "VK_RIGHT",
   1.114 +    removeSelection: true,
   1.115 +    result: "re",
   1.116 +    start: 2, end: 2
   1.117 +  },
   1.118 +  { desc: "ENTER key remove selection",
   1.119 +    key: "VK_RETURN",
   1.120 +    removeSelection: true,
   1.121 +    result: "re",
   1.122 +    start: 2, end: 2
   1.123 +  },
   1.124 +  {
   1.125 +    desc: "HOME key",
   1.126 +    key: "VK_HOME",
   1.127 +    removeSelection: false,
   1.128 +    result: "Result",
   1.129 +    start: 0, end: 0
   1.130 +  },
   1.131 +  {
   1.132 +    desc: "LEFT key",
   1.133 +    key: "VK_LEFT",
   1.134 +    removeSelection: false,
   1.135 +    result: "Result",
   1.136 +    start: 5, end: 5
   1.137 +  },
   1.138 +  { desc: "RIGHT key",
   1.139 +    key: "VK_RIGHT",
   1.140 +    removeSelection: false,
   1.141 +    result: "Result",
   1.142 +    start: 6, end: 6
   1.143 +  },
   1.144 +  { desc: "RETURN key",
   1.145 +    key: "VK_RETURN",
   1.146 +    removeSelection: false,
   1.147 +    result: "Result",
   1.148 +    start: 6, end: 6
   1.149 +  },
   1.150 +  { desc: "TAB key should confirm suggestion when forcecomplete is set",
   1.151 +    key: "VK_TAB",
   1.152 +    removeSelection: false,
   1.153 +    forceComplete: true,
   1.154 +    result: "Result",
   1.155 +    start: 6, end: 6
   1.156 +  },
   1.157 +];
   1.158 +
   1.159 +function nextTest() {
   1.160 +  if (!tests.length) {
   1.161 +    // No more tests to run, finish.
   1.162 +    setTimeout(function() {
   1.163 +      // Unregister the factory so that we don't get in the way of other tests
   1.164 +      componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple);
   1.165 +      SimpleTest.finish();
   1.166 +    }, 0);
   1.167 +    return;
   1.168 +  }
   1.169 +
   1.170 +  var autocomplete = $("autocomplete");
   1.171 +  autocomplete.value = "";
   1.172 +  currentTest = tests.shift();
   1.173 +
   1.174 +  // HOME key works differently on Mac, so we skip tests using it.
   1.175 +  if (currentTest.key == "VK_HOME" && navigator.platform.indexOf("Mac") != -1)
   1.176 +    nextTest();
   1.177 +  else
   1.178 +    setTimeout(runCurrentTest, 0);
   1.179 +}
   1.180 +
   1.181 +function runCurrentTest() {
   1.182 +  var autocomplete = $("autocomplete");
   1.183 +
   1.184 +  autocomplete.focus();
   1.185 +
   1.186 +  synthesizeKey("r", {});
   1.187 +  synthesizeKey("e", {});
   1.188 +}
   1.189 +
   1.190 +function searchComplete() {
   1.191 +  var autocomplete = $("autocomplete");
   1.192 +  is(autocomplete.value, "result",
   1.193 +     "Test '" + currentTest.desc + "': autocomplete.value should equal 'result'");
   1.194 +
   1.195 +  autocomplete.setAttribute("forcecomplete", currentTest.forceComplete ? true : false);
   1.196 +
   1.197 +  if (autocomplete.selectionStart == 2) {  // Finished inserting "re" string.
   1.198 +    if (currentTest.removeSelection) {
   1.199 +      // remove current selection
   1.200 +      synthesizeKey("VK_DELETE", {});
   1.201 +    }
   1.202 +
   1.203 +    synthesizeKey(currentTest.key, {});
   1.204 +
   1.205 +    checkResult();
   1.206 +  }
   1.207 +}
   1.208 +
   1.209 +function checkResult() {
   1.210 +  var autocomplete = $("autocomplete");
   1.211 +
   1.212 +  is(autocomplete.value, currentTest.result,
   1.213 +     "Test '" + currentTest.desc + "': autocomplete.value should equal '" +
   1.214 +     currentTest.result + "'");
   1.215 +
   1.216 +  is(autocomplete.selectionStart, currentTest.start,
   1.217 +     "Test '" + currentTest.desc + "': autocomplete selection should start at " +
   1.218 +     currentTest.start);
   1.219 +
   1.220 +  is(autocomplete.selectionEnd, currentTest.end,
   1.221 +     "Test '" + currentTest.desc + "': autocomplete selection should end at " +
   1.222 +     currentTest.end);
   1.223 +
   1.224 +  setTimeout(nextTest, 0);
   1.225 +}
   1.226 +
   1.227 +]]>
   1.228 +</script>
   1.229 +
   1.230 +<body xmlns="http://www.w3.org/1999/xhtml">
   1.231 +<p id="display">
   1.232 +</p>
   1.233 +<div id="content" style="display: none">
   1.234 +</div>
   1.235 +<pre id="test">
   1.236 +</pre>
   1.237 +</body>
   1.238 +
   1.239 +</window>

mercurial