<!DOCTYPE HTML>
<html>
<head>
  <title>Profiling test suite for EventUtils</title>
  <script type="text/javascript">
  var start = new Date();
  </script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript">
  var loadTime = new Date();
  </script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="starttest()">
<input type="radio" id="radioTarget1" name="group">Radio Target 1</input>
<input id="textBoxA">
<input id="textBoxB">
<input id="testMouseEvent" type="button" value="click">
<input id="testKeyEvent" >
<input id="testStrEvent" >
<div id="scrollB" style="width: 190px;height: 250px;overflow:auto">
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
<p>blah blah blah blah</p>
</div>
<script class="testbody" type="text/javascript">
info("\nProfile::EventUtilsLoadTime: " + (loadTime - start) + "\n");
function starttest() {
  SimpleTest.waitForFocus(
    function () {
      SimpleTest.waitForExplicitFinish();
      var startTime = new Date();
      var check = false;
      
      /* test send* functions */
      $("testMouseEvent").addEventListener("click", function() { check=true; }, false);
      sendMouseEvent({type:'click'}, "testMouseEvent");
      is(check, true, 'sendMouseEvent should dispatch click event');
      
      check = false;
      $("testKeyEvent").addEventListener("keypress", function() { check = true; }, false);
      $("testKeyEvent").focus();
      sendChar("x");
      is($("testKeyEvent").value, "x", "sendChar should work");
      is(check, true, "sendChar should dispatch keyPress");
      $("testKeyEvent").value = "";
    
      $("testStrEvent").focus();
      sendString("string");
      is($("testStrEvent").value, "string", "sendString should work");
      $("testStrEvent").value = "";
    
      check = false;
      $("testKeyEvent").focus();
      sendKey("DOWN");
      is(check, true, "sendKey should dispatch keyPress");
    
      /* test synthesizeMouse* */
      //focus trick enables us to run this in iframes
      $("radioTarget1").addEventListener('focus', function (aEvent) {
        $("radioTarget1").removeEventListener('focus', arguments.callee, false);
        synthesizeMouse($("radioTarget1"), 1, 1, {});
        is($("radioTarget1").checked, true, "synthesizeMouse should work")
        $("radioTarget1").checked = false;
        disableNonTestMouseEvents(true);
        synthesizeMouse($("radioTarget1"), 1, 1, {});
        is($("radioTarget1").checked, true, "synthesizeMouse should still work with non-test mouse events disabled");
        $("radioTarget1").checked = false;
        disableNonTestMouseEvents(false);
      });
      $("radioTarget1").focus();

      //focus trick enables us to run this in iframes
      $("textBoxA").addEventListener("focus", function (aEvent) {
        $("textBoxA").removeEventListener("focus", arguments.callee, false);
        check = false;
        $("textBoxA").addEventListener("click", function() { check = true; }, false);
        synthesizeMouseAtCenter($("textBoxA"), {});
        is(check, true, 'synthesizeMouse should dispatch mouse event');
    
        check = false;
        synthesizeMouseExpectEvent($("textBoxA"), 1, 1, {}, $("textBoxA"), "click", "synthesizeMouseExpectEvent should fire click event");
        is(check, true, 'synthesizeMouse should dispatch mouse event');
      });
      $("textBoxA").focus();
    
      /**
       * TODO: testing synthesizeWheel requires a setTimeout 
       * since there is delay between the scroll event and a check, so for now just test
       * that we can successfully call it to avoid having setTimeout vary the runtime metric.
       * Testing of this method is currently done here:
       * toolkit/content/tests/chrome/test_mousescroll.xul
       */
      synthesizeWheel($("scrollB"), 5, 5, {'deltaY': 10.0, deltaMode: WheelEvent.DOM_DELTA_LINE});
    
      /* test synthesizeKey* */
      check = false;
      $("testKeyEvent").addEventListener("keypress", function() { check = true; }, false);
      $("testKeyEvent").focus();
      synthesizeKey("a", {});
      is($("testKeyEvent").value, "a", "synthesizeKey should work");
      is(check, true, "synthesizeKey should dispatch keyPress");
      $("testKeyEvent").value = "";
    
      check = false;
      synthesizeKeyExpectEvent("a", {}, $("testKeyEvent"), "keypress");
      is($("testKeyEvent").value, "a", "synthesizeKey should work");
      is(check, true, "synthesizeKey should dispatch keyPress");
      $("testKeyEvent").value = "";
    
      /* test synthesizeComposition */
      $("textBoxB").focus();
      check = false;
      window.addEventListener("compositionstart", function() { check = true; }, false);
      synthesizeComposition({ type: "compositionstart" });
      is(check, true, 'synthesizeComposition() should dispatch compositionstart');
    
      check = false;
      window.addEventListener("compositionupdate", function() { check = true; }, false);
      synthesizeComposition({ type: "compositionupdate", data: "a" });
      is(check, false, 'synthesizeComposition() should not dispatch compositionupdate without error');

      check = false;
      window.addEventListener("text", function() { check = true; }, false);
      synthesizeCompositionChange(
        { "composition":
          { "string": "a",
            "clauses":
            [
              { "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE }
            ]
          },
          "caret": { "start": 1, "length": 0 }
        }
      );
      is(check, true, "synthesizeCompositionChange should cause dispatching a DOM text event");

      synthesizeCompositionChange(
        { "composition":
          { "string": "a",
            "clauses":
            [
              { "length": 0, "attr": 0 }
            ]
          },
          "caret": { "start": 1, "length": 0 }
        }
      );

      check = false;
      window.addEventListener("compositionend", function() { check = true; }, false);
      synthesizeComposition({ type: "compositionend", data: "a" });
      is(check, false, 'synthesizeComposition() should not dispatch compositionend');

      synthesizeComposition({ type: "compositioncommit", data: "a" });
      is(check, true, 'synthesizeComposition() should dispatch compositionend');

      var querySelectedText = synthesizeQuerySelectedText();
      ok(querySelectedText, "query selected text event result is null");
      ok(querySelectedText.succeeded, "query selected text event failed");
      is(querySelectedText.offset, 1,
         "query selected text event returns wrong offset");
      is(querySelectedText.text, "",
         "query selected text event returns wrong selected text");
      $("textBoxB").value = "";
    
      querySelectedText = synthesizeQuerySelectedText();
      ok(querySelectedText, "query selected text event result is null");
      ok(querySelectedText.succeeded, "query selected text event failed");
      is(querySelectedText.offset, 0,
         "query selected text event returns wrong offset");
      is(querySelectedText.text, "",
         "query selected text event returns wrong selected text");
      var endTime = new Date();
      info("\nProfile::EventUtilsRunTime: " + (endTime-startTime) + "\n");
      SimpleTest.finish();
    }
  );
};
</script>
</body>
</html>