<p>This test verifies that a right mousebutton click generates a mousedown, context and mouseup event.</p>

</p>
<p>If the test passes, you'll see a PASS message below.
</p>

<p>To run this test in Safari:
<ol>
    <li>Right mouse down inside the box.</li>
    <li>Right mouse up.</li>
</ol>
</p>
<hr>
<style id="mystyle">
  .test-box {
    border: solid;
  }
</style>
<div class="test-box" id="click_target-1">box</div>
<pre id="log"></pre>

<script id="cody">

/**
 * Base namespace for test
 */
var rlightclickbug = rlightclickbug || {}; 

/**
 * Swallows an event.
 */
rlightclickbug.swallow = function(event) {
  event.preventDefault && event.preventDefault();
  event.stopImmediatePropagation && event.stopImmediatePropagation();
  event.stopPropagation && event.stopPropagation();
  event.preventBubble && event.preventBubble();
  return false;
};

/**
 * Dumps out a single line summary of a mouse event
 */
rlightclickbug.dumpShortEvent = function(event) {
  var values = []
  var keys = ['type', 'button'];
  for (var i = 0; i < keys.length; i++) {
    values.push(keys[i]);
    values.push(': ');
    values.push(event[keys[i]]);
    values.push(', ');
  }
  return values.join('');
};

/**
 * Writes a log message to the screen.
 */
rlightclickbug.logme = function(m) {
  var loggy = document.getElementById('log');
  loggy.innerHTML += ('\n' + m);
};

var _captureMouseEvents = function(event) {
  rlightclickbug.logme(rlightclickbug.dumpShortEvent(event));
  return rlightclickbug.swallow(event);
};


function main()
{
  var targetBox = document.getElementById('click_target-1');
  
  targetBox.addEventListener("mouseup", _captureMouseEvents, false);
  targetBox.addEventListener("mousedown", _captureMouseEvents, false);
  targetBox.addEventListener("click", _captureMouseEvents, false);
  targetBox.addEventListener("contextmenu", _captureMouseEvents, false);
    
  if (window.layoutTestController)
      layoutTestController.dumpAsText();
      
  if (window.eventSender) {
      // Confirm that left button works properly     
      eventSender.mouseMoveTo(targetBox.offsetLeft + 5,
          targetBox.offsetTop + 5); // in box
      eventSender.mouseDown();
      eventSender.mouseUp();
      
      // Confirm that right button works properly
      eventSender.mouseDown(2);
      eventSender.mouseUp(2);
      
      // Confirm that moving mouse while down outside of box 
      // does not fire a mouseup event.
      eventSender.mouseDown(2);
      eventSender.mouseMoveTo(0, 0); // outside of box
      eventSender.mouseUp(2);

      // order in IE is mousedown contextmenu mouseup
  }
}


main();
</script>
