summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/Harness_sanity/test_sanityEventUtils.html
blob: 07efaaf4d096880a31332f9dbd38188e52978a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!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>