toolkit/content/tests/chrome/test_autocomplete3.xul
author Malini Das <mdas@mozilla.com>
Fri, 12 Aug 2011 12:21:36 -0400
changeset 74314 67f6f1908b0f4cb8c00d3de763787fdb8fe8eeeb
parent 62046 6882211c4a80df43d9ea10904ac26ada8fbd4875
permissions -rw-r--r--
Bug 367393 - Add a packed MochiKit that contains only SimpleTest dependencies- chrome. r=jmaher, a=test-only

<?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="Autocomplete Widget Test 3"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

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

<textbox id="autocomplete" type="autocomplete"
         autocompletesearch="simple"
         onsearchcomplete="checkResult();"/>

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

// Set to indicate whether or not we want autoCompleteSimple to return a result
var returnResult = true;

const ACR = Components.interfaces.nsIAutoCompleteResult;

// This result can't be constructed in-line, because otherwise we leak memory.
function nsAutoCompleteSimpleResult(aString)
{
  this.searchString = aString;
  if (returnResult) {
    this.searchResult = ACR.RESULT_SUCCESS;
    this.matchCount = 1;
    this._param = "Result";
  }
}

nsAutoCompleteSimpleResult.prototype = {
 _param: "",
 searchString: null,
 searchResult: ACR.RESULT_FAILURE,
 defaultIndex: 0,
 errorDescription: null,
 matchCount: 0,
 getValueAt: function() { return this._param; },
 getCommentAt: function() { return null; },
 getStyleAt: function() { return null; },
 getImageAt: function() { return null; },
 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=simple"
var autoCompleteSimple = {
  QueryInterface: function(iid) {
    if (iid.equals(Components.interfaces.nsISupports) ||
        iid.equals(Components.interfaces.nsIFactory) ||
        iid.equals(Components.interfaces.nsIAutoCompleteSearch))
      return this;

    throw Components.results.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(Components.interfaces.nsIComponentRegistrar);
componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete",
                                 autoCompleteSimpleName, autoCompleteSimple);


// Test Bug 325842 - completeDefaultIndex

SimpleTest.waitForExplicitFinish();
setTimeout(startTest, 0);

var currentTest = 0;

// Note the entries for these tests (key) are incremental.
const tests = [
  { completeDefaultIndex: "false", key: "r", result: "r",
    start: 1, end: 1 },
  { completeDefaultIndex: "true", key: "e", result: "result",
    start: 2, end: 6 },
  { completeDefaultIndex: "true", key: "t", result: "ret >> Result",
    start: 3, end: 13 }
];

function startTest() {
  var autocomplete = $("autocomplete");

  // These should not be set by default.
  is(autocomplete.hasAttribute("completedefaultindex"), false,
     "completedefaultindex not set by default");

  autocomplete.completeDefaultIndex = "true";

  is(autocomplete.getAttribute("completedefaultindex"), "true",
     "completedefaultindex attribute set correctly");
  is(autocomplete.completeDefaultIndex, true,
     "autoFill getter returned correctly");

  autocomplete.completeDefaultIndex = "false";

  is(autocomplete.getAttribute("completedefaultindex"), "false",
     "completedefaultindex attribute set to false correctly");
  is(autocomplete.completeDefaultIndex, false,
     "completeDefaultIndex getter returned false correctly");

  checkNext();
}

function checkNext() {
  var autocomplete = $("autocomplete");

  autocomplete.completeDefaultIndex = tests[currentTest].completeDefaultIndex;
  autocomplete.focus();

  synthesizeKey(tests[currentTest].key, {});
}

function checkResult() {
  var autocomplete = $("autocomplete");
  var style = window.getComputedStyle(autocomplete, "");

  is(autocomplete.value, tests[currentTest].result,
     "Test " + currentTest + ": autocomplete.value should equal '" +
     tests[currentTest].result + "'");

  is(autocomplete.selectionStart, tests[currentTest].start,
     "Test " + currentTest + ": autocomplete selection should start at " +
     tests[currentTest].start);

  is(autocomplete.selectionEnd, tests[currentTest].end,
     "Test " + currentTest + ": autocomplete selection should end at " +
     tests[currentTest].end);

  ++currentTest;

  if (currentTest < tests.length)
    setTimeout(checkNext, 0);
  else {
    // TODO (bug 494809): Autocomplete-in-the-middle should take in count RTL
    // and complete on VK_RIGHT or VK_LEFT based on that.  It should also revert
    // what user has typed to far if he moves in the opposite direction.
    if (autocomplete.value.indexOf(">>") == -1) {
      // Test result if user accepts autocomplete suggestion.
      synthesizeKey("VK_RIGHT", {});
      is(autocomplete.value, "Result",
         "Test complete: autocomplete.value should equal 'Result'");
      is(autocomplete.selectionStart, 6,
         "Test complete: autocomplete selection should start at 6");
      is(autocomplete.selectionEnd, 6,
         "Test complete: autocomplete selection should end at 6");
    }

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

]]>
</script>

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

</window>