toolkit/content/tests/chrome/test_autocomplete5.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/chrome/test_autocomplete5.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,152 @@
     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 5"
     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" type="autocomplete"
    1.17 +         autocompletesearch="simple"
    1.18 +         ontextentered="checkTextEntered();"
    1.19 +         ontextreverted="checkTextReverted();"
    1.20 +         onsearchbegin="checkSearchBegin();"
    1.21 +         onsearchcomplete="checkSearchCompleted();"/>
    1.22 +
    1.23 +<script class="testbody" type="application/javascript">
    1.24 +<![CDATA[
    1.25 +
    1.26 +const ACR = Components.interfaces.nsIAutoCompleteResult;
    1.27 +
    1.28 +// This result can't be constructed in-line, because otherwise we leak memory.
    1.29 +function nsAutoCompleteSimpleResult(aString)
    1.30 +{
    1.31 +  this.searchString = aString;
    1.32 +  this.searchResult = ACR.RESULT_SUCCESS;
    1.33 +  this.matchCount = 1;
    1.34 +  this._param = "SUCCESS";
    1.35 +}
    1.36 +
    1.37 +nsAutoCompleteSimpleResult.prototype = {
    1.38 + _param: "",
    1.39 + searchString: null,
    1.40 + searchResult: ACR.RESULT_FAILURE,
    1.41 + defaultIndex: -1,
    1.42 + errorDescription: null,
    1.43 + matchCount: 0,
    1.44 + getValueAt: function() { return this._param; },
    1.45 + getCommentAt: function() { return null; },
    1.46 + getStyleAt: function() { return null; },
    1.47 + getImageAt: function() { return null; },
    1.48 + getFinalCompleteValueAt: function() { return this.getValueAt(); },
    1.49 + getLabelAt: function() { return null; },
    1.50 + removeValueAt: function() {}
    1.51 +};
    1.52 +
    1.53 +// A basic autocomplete implementation that either returns one result or none
    1.54 +var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca");
    1.55 +var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple"
    1.56 +var autoCompleteSimple = {
    1.57 +  QueryInterface: function(iid) {
    1.58 +    if (iid.equals(Components.interfaces.nsISupports) ||
    1.59 +        iid.equals(Components.interfaces.nsIFactory) ||
    1.60 +        iid.equals(Components.interfaces.nsIAutoCompleteSearch))
    1.61 +      return this;
    1.62 +
    1.63 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.64 +  },
    1.65 +
    1.66 +  createInstance: function(outer, iid) {
    1.67 +    return this.QueryInterface(iid);
    1.68 +  },
    1.69 +
    1.70 +  startSearch: function(aString, aParam, aResult, aListener) {
    1.71 +    var result = new nsAutoCompleteSimpleResult(aString);
    1.72 +    aListener.onSearchResult(this, result);
    1.73 +  },
    1.74 +
    1.75 +  stopSearch: function() {}
    1.76 +};
    1.77 +
    1.78 +var componentManager = Components.manager
    1.79 +                                 .QueryInterface(Components.interfaces.nsIComponentRegistrar);
    1.80 +componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete",
    1.81 +                                 autoCompleteSimpleName, autoCompleteSimple);
    1.82 +
    1.83 +SimpleTest.waitForExplicitFinish();
    1.84 +setTimeout(startTest, 0);
    1.85 +
    1.86 +function startTest() {
    1.87 +  let autocomplete = $("autocomplete");
    1.88 +
    1.89 +  // blur the field to ensure that the popup is closed and that the previous
    1.90 +  // search has stopped, then start a new search.
    1.91 +  autocomplete.blur();
    1.92 +  autocomplete.focus();
    1.93 +  synthesizeKey("r", {});
    1.94 +}
    1.95 +
    1.96 +let hasTextEntered = false;
    1.97 +let hasSearchBegun = false;
    1.98 +
    1.99 +function checkSearchBegin() {
   1.100 +  hasSearchBegun = true;
   1.101 +}
   1.102 +
   1.103 +let test = 0;
   1.104 +function checkSearchCompleted() {
   1.105 +  is(hasSearchBegun, true, "onsearchbegin handler has been correctly called.");
   1.106 +
   1.107 +  if (test == 0) {
   1.108 +    hasSearchBegun = false;
   1.109 +    synthesizeKey("VK_RETURN", { });
   1.110 +  } else if (test == 1) {
   1.111 +    hasSearchBegun = false;
   1.112 +    synthesizeKey("VK_ESCAPE", { });
   1.113 +  } else {
   1.114 +    throw "checkSearchCompleted should only be called twice.";
   1.115 +  }
   1.116 +}
   1.117 +
   1.118 +function checkTextEntered() {
   1.119 +  is(test, 0, "checkTextEntered should be reached from first test.");
   1.120 +  is(hasSearchBegun, false, "onsearchbegin handler should not be called on text revert.");
   1.121 +
   1.122 +  // fire second test
   1.123 +  test++;
   1.124 +
   1.125 +  let autocomplete = $("autocomplete");
   1.126 +  autocomplete.textValue = "";
   1.127 +  autocomplete.blur();
   1.128 +  autocomplete.focus();
   1.129 +  synthesizeKey("r", {});
   1.130 +}
   1.131 +
   1.132 +function checkTextReverted() {
   1.133 +  is(test, 1, "checkTextReverted should be the second test reached.");
   1.134 +  is(hasSearchBegun, false, "onsearchbegin handler should not be called on text revert.");
   1.135 +
   1.136 +  setTimeout(function() {
   1.137 +    // Unregister the factory so that we don't get in the way of other tests
   1.138 +    componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple);
   1.139 +    SimpleTest.finish();
   1.140 +  }, 0);
   1.141 +}
   1.142 +
   1.143 +]]>
   1.144 +</script>
   1.145 +
   1.146 +<body xmlns="http://www.w3.org/1999/xhtml">
   1.147 +<p id="display">
   1.148 +</p>
   1.149 +<div id="content" style="display: none">
   1.150 +</div>
   1.151 +<pre id="test">
   1.152 +</pre>
   1.153 +</body>
   1.154 +
   1.155 +</window>

mercurial