<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="/tests/SimpleTest/test.css" type="text/css"?>

<window title="Colorpicker Tests"
  onload="setTimeout(runTests, 0);"
  onpopupshown="popupShown();"
  onpopuphidden="popupHiding();"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <title>Colorpicker Tests</title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>      
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>      

<colorpicker id="colorpicker-popup" type="button" color="#FF0000" tabindex="1"/>

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

var gTestPhase = -1;
var gCp = null;

SimpleTest.waitForExplicitFinish();

function preventDefault(event) {
  event.preventDefault();
}

function runTests()
{
  gCp = document.getElementById("colorpicker-popup");
  is(gCp.color, "#FF0000", "popup color is initialized");
  is(gCp.tabIndex, 1, "button tabindex is initialized");
  is(gCp.disabled, false, "button is not disabled");
  
  document.addEventListener("keypress", preventDefault, false);

  goNext();
}

var phases = [ "mouse click", "showPopup",
               "key left", "key right", "key up", "key down", "key space" ];

function popupShown()
{
  if (gTestPhase >= phases.length)
    return;

  var phase = phases[gTestPhase];

  is(gCp.open, true, phase + " popup shown, open property is true");

  switch (phase) {
    case "mouse click":
      synthesizeMouse(gCp, 2, 2, { });
      break;
    case "showPopup":
      gCp.hidePopup();
      break;
    case "key left":
      synthesizeKey("VK_LEFT", { });
      synthesizeKeyExpectEvent("VK_RETURN", { });
      is(gCp.color, "#C0C0C0", "key left while open");
      break;
    case "key right":
      synthesizeKey("VK_RIGHT", { });
      synthesizeKeyExpectEvent("VK_SPACE", { });
      is(gCp.color, "#FF0000", "key right while open");
      break;
    case "key up":
      synthesizeKey("VK_UP", { });
      synthesizeKeyExpectEvent("VK_RETURN", { });
      is(gCp.color, "#FF6666", "key up while open");
      break;
    case "key down":
      synthesizeKey("VK_DOWN", { });
      synthesizeKeyExpectEvent("VK_SPACE", { });
      is(gCp.color, "#FF0000", "key down while open");
      break;
    default:
      synthesizeMouse(gCp, 2, 2, { });
// this breaks on the Mac, so disable for now
//      synthesizeKey("VK_ESCAPE", { });
      break;
  }
}

function popupHiding()
{
  var phase = phases[gTestPhase];
  if (phase == "showPopup")
    phase = "hidePopup";
  if (phase == "key left")
    phase = "escape";
  is(gCp.open, false, phase + " popup hidden, open property is false");

  goNext();
}

function goNext()
{
  gTestPhase++;
  if (gTestPhase >= phases.length) {
    document.removeEventListener("keypress", preventDefault, false);
    SimpleTest.finish();
    return;
  }

  gCp.focus();

  var phase = phases[gTestPhase];
  switch (phase) {
    case "mouse click":
      synthesizeMouse(gCp, 2, 2, { });
      break;
    case "showPopup":
      gCp.showPopup();
      break;
    case "key left":
      synthesizeKey("VK_LEFT", { });
      break;
    case "key right":
      synthesizeKey("VK_RIGHT", { });
      break;
    case "key down":
      synthesizeKey("VK_UP", { });
      break;
    case "key up":
      synthesizeKey("VK_DOWN", { });
      break;
    case "key space":
      synthesizeKey("VK_SPACE", { });
      break;
  }
}

]]>
</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>
