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

  <script type="application/javascript" 
          src="chrome://mochikit/content/MochiKit/packed.js"/>
  <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"
         completedefaultindex="true"
         onsearchcomplete="searchComplete();"
         autocompletesearch="simple"/>

<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; },
 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(nextTest, 0);

var currentTest = null;

// Note the entries for these tests (key) are incremental.
const tests = [
  {
    desc: "HOME key remove selection",
    key: "VK_HOME",
    removeSelection: true,
    result: "re",
    start: 0, end: 0
  },
  {
    desc: "LEFT key remove selection",
    key: "VK_LEFT",
    removeSelection: true,
    result: "re",
    start: 1, end: 1
  },
  { desc: "RIGHT key remove selection",
    key: "VK_RIGHT",
    removeSelection: true,
    result: "re",
    start: 2, end: 2
  },
  { desc: "ENTER key remove selection",
    key: "VK_RETURN",
    removeSelection: true,
    result: "re",
    start: 2, end: 2
  },
  {
    desc: "HOME key",
    key: "VK_HOME",
    removeSelection: false,
    result: "Result",
    start: 0, end: 0
  },
  {
    desc: "LEFT key",
    key: "VK_LEFT",
    removeSelection: false,
    result: "Result",
    start: 5, end: 5
  },
  { desc: "RIGHT key",
    key: "VK_RIGHT",
    removeSelection: false,
    result: "Result",
    start: 6, end: 6
  },
  { desc: "ENTER key",
    key: "VK_RETURN",
    removeSelection: false,
    result: "Result",
    start: 6, end: 6
  },
];

function nextTest() {
  if (!tests.length) {
    // No more tests to run, finish.
    setTimeout(function() {
      // Unregister the factory so that we don't get in the way of other tests
      componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple);
      SimpleTest.finish();
    }, 0);
    return;
  }

  var autocomplete = $("autocomplete");
  autocomplete.value = "";
  currentTest = tests.shift();

  // HOME key works differently on Mac, so we skip tests using it.
  if (currentTest.key == "VK_HOME" && navigator.platform.indexOf("Mac") != -1)
    nextTest();
  else
    setTimeout(runCurrentTest, 0);
}

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

  autocomplete.focus();

  synthesizeKey("r", {});
  synthesizeKey("e", {});
}

function searchComplete() {
  var autocomplete = $("autocomplete");
  is(autocomplete.value, "result",
     "Test '" + currentTest.desc + "': autocomplete.value should equal 'result'");

  if (autocomplete.selectionStart == 2) {  // Finished inserting "re" string.
    if (currentTest.removeSelection) {
      // remove current selection
      synthesizeKey("VK_DELETE", {});
    }

    synthesizeKey(currentTest.key, {});

    checkResult();
  }
}

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

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

  is(autocomplete.selectionStart, currentTest.start,
     "Test '" + currentTest.desc + "': autocomplete selection should start at " +
     currentTest.start);

  is(autocomplete.selectionEnd, currentTest.end,
     "Test '" + currentTest.desc + "': autocomplete selection should end at " +
     currentTest.end);

  setTimeout(nextTest, 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>
