toolkit/content/tests/chrome/test_editor_for_textbox_with_autocomplete.xul
author Mihai Alexandru Michis <malexandru@mozilla.com>
Tue, 17 Sep 2019 07:51:36 +0300
changeset 493494 17e8e912e185c5c3543e6bc248c78ec5e2401c03
permissions -rw-r--r--
Backed out changeset e859a5aebb5b (bug 1534455) for causing failures at test_bug437844.xul

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<window title="Basic editor behavior for XUL textbox element with autocomplete"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js" />
  <script type="text/javascript"
          src="file_editor_with_autocomplete.js" />

  <textbox id="textbox" type="autocomplete"
           autocompletesearch="simpleForComposition"/>

<body  xmlns="http://www.w3.org/1999/xhtml">
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

<script class="testbody" type="application/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

const nsIAutoCompleteResult = Ci.nsIAutoCompleteResult;

// This result can't be constructed in-line, because otherwise we leak memory.
function nsAutoCompleteSimpleResult(aString)
{
  this.searchString = aString;
  if (aString == "" ||
      aString.toLowerCase() == "mozilla".substr(0, aString.length)) {
    this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS;
    this.matchCount = 1;
    this._value = "Mozilla";
  } else {
    this.searchResult = nsIAutoCompleteResult.RESULT_NOMATCH;
    this.matchCount = 0;
    this._value = "";
  }
}

nsAutoCompleteSimpleResult.prototype = {
 _value: "",
 searchString: null,
 searchResult: nsIAutoCompleteResult.RESULT_FAILURE,
 defaultIndex: 0,
 errorDescription: null,
 matchCount: 0,
 getValueAt: function(aIndex) { return aIndex == 0 ? this._value : null; },
 getCommentAt: function() { return null; },
 getStyleAt: function() { return null; },
 getImageAt: function() { return null; },
 getFinalCompleteValueAt: function(aIndex) { return this.getValueAt(aIndex); },
 getLabelAt: function() { return null; },
 removeValueAt: function() {}
};

// A basic autocomplete implementation that either returns one result or none
var autoCompleteSimpleID =
  Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca");
var autoCompleteSimpleName =
  "@mozilla.org/autocomplete/search;1?name=simpleForComposition"
var autoCompleteSimple = {
  QueryInterface: function(iid) {
    if (iid.equals(Ci.nsISupports) ||
        iid.equals(Ci.nsIFactory) ||
        iid.equals(Ci.nsIAutoCompleteSearch))
      return this;

    throw Cr.NS_ERROR_NO_INTERFACE;
  },

  createInstance: function(outer, iid) {
    return this.QueryInterface(iid);
  },

  startSearch: function(aString, aParam, aResult, aListener) {
    var result = new nsAutoCompleteSimpleResult(aString);
    aListener.onSearchResult(this, result);
  },

  stopSearch: function() {}
};

var componentManager =
  Components.manager
            .QueryInterface(Ci.nsIComponentRegistrar);
componentManager.registerFactory(autoCompleteSimpleID,
                                 "Test Simple Autocomplete for composition",
                                 autoCompleteSimpleName, autoCompleteSimple);

async function runTests()
{
  var target = document.getElementById("textbox");

  target.setAttribute("timeout", 1);
  let tests1 = new nsDoTestsForEditorWithAutoComplete(
    "Testing on XUL textbox (asynchronously search)",
    window, target, target.controller, is, todo_is,
    function() { return target.value; });
  await tests1.run();

  target.setAttribute("timeout", 0);
  let tests2 = new nsDoTestsForEditorWithAutoComplete(
        "Testing on XUL textbox (synchronously search)",
        window, target, target.controller, is, todo_is,
        function() { return target.value; });
  await tests2.run();

  // Unregister the factory so that we don't get in the way of other
  // tests
  componentManager.unregisterFactory(autoCompleteSimpleID,
                                     autoCompleteSimple);
  SimpleTest.finish();
}

SimpleTest.waitForFocus(runTests);
]]>
</script>
</window>