REGRESSION: svg/custom/use-instanceRoot-event-listeners.xhtml & svg/custom/pointer...
[WebKit-https.git] / LayoutTests / svg / custom / resources / use-instanceRoot-event-listeners.js
1 var counter = 0;
2 var tests = 8;
3 var expected = "";
4 var logEvent = false;
5 var rectElement = document.getElementById("target");
6 var useElement = document.getElementById("test");
7 description("Test attaching event listeners on SVG use elements in different ways: ");
8
9 if (window.layoutTestController)
10     layoutTestController.waitUntilDone();
11
12 function eventHandler(evt)
13 {
14     if (logEvent) {
15         if (expected == evt.type)
16             debug("Test " + counter + " / " + tests + " PASSED");
17         else
18             debug("Test " + counter + " / " + tests + " FAILED (expected: '" + expected + "' actual: '" + evt.type + "')");
19     }
20
21     setTimeout(counter < tests ? driveTests : finishTest, 0);
22 }
23
24 function finishTest()
25 {
26     successfullyParsed = true;
27
28     useElement.instanceRoot.correspondingElement.setAttribute("fill", "green");
29     shouldBeTrue("successfullyParsed");
30     debug('<br /><span class="pass">TEST COMPLETE</span>');
31
32     if (window.layoutTestController)
33         layoutTestController.notifyDone();
34 }
35
36 function recordMouseEvent(type)
37 {
38     expected = type;
39     logEvent = true;
40
41     ++counter;
42     fireDelayedEvent();
43 }
44
45 function sendMouseEvent(type, increment)
46 {
47     expected = type;
48     logEvent = false;
49     fireDelayedEvent();
50 }
51
52 function fireDelayedEvent()
53 {
54     if (expected == "mouseover")
55         eventSender.mouseMoveTo(50, 50);
56     else if (expected == "mouseout")
57         eventSender.mouseMoveTo(115, 55);
58     else if (expected == "mouseup") {
59         eventSender.mouseMoveTo(50, 50);
60         eventSender.mouseDown();
61         eventSender.mouseUp();
62     } else if (expected == "mousedown") {
63         eventSender.mouseMoveTo(50, 50);
64         eventSender.mouseDown();
65         eventSender.mouseUp();
66     }
67 }
68
69 function testOne()
70 {
71     // Install event listener on correspondingElement via setAttribute
72     useElement.instanceRoot.correspondingElement.setAttribute("onmouseover", "eventHandler(evt)");
73     recordMouseEvent("mouseover");
74 }
75
76 function testTwo()
77 {
78     // Install event listener on correspondingElement via onmouseout JS magic
79     useElement.instanceRoot.correspondingElement.onmouseout = eventHandler;
80     recordMouseEvent("mouseout");
81 }
82
83 function testThree()
84 {
85     // Clean event listeners on different ways
86     useElement.instanceRoot.correspondingElement.removeAttribute("onmouseover");
87     useElement.instanceRoot.correspondingElement.onmouseout = 0;
88
89     // Verify they really got removed
90     sendMouseEvent("mouseover");
91     sendMouseEvent("mouseout");
92
93     // Verify the original listener still works
94     recordMouseEvent("mousedown");
95 }
96
97 function testFour()
98 {
99     useElement.instanceRoot.correspondingElement.removeAttribute("onmousedown");
100
101     // Install event listener on the referenced element, without using the SVGElementInstance interface
102     rectElement.setAttribute("onmouseup", "eventHandler(evt)");
103     recordMouseEvent("mouseup");
104 }
105
106 function testFive()
107 {
108     rectElement.onmouseout = eventHandler;
109     recordMouseEvent("mouseout");
110 }
111
112 function testSix()
113 {
114     useElement.instanceRoot.correspondingElement.onmouseout = null;
115     sendMouseEvent("mouseout");
116
117     useElement.instanceRoot.correspondingElement.removeAttribute('onmouseup');
118     sendMouseEvent("mouseup");
119
120     useElement.instanceRoot.correspondingElement.onmouseup = eventHandler;
121     recordMouseEvent("mouseup");
122 }
123
124 function testSeven()
125 {
126     useElement.instanceRoot.addEventListener("mouseout", eventHandler, false);
127     recordMouseEvent("mouseout");
128 }
129
130 function testEight()
131 {
132     useElement.instanceRoot.correspondingElement.addEventListener("mouseover", eventHandler, false);
133     recordMouseEvent("mouseover");
134 }
135
136 function testNine()
137 {
138     rectElement.addEventListener("mousedown", eventHandler, false);
139     recordMouseEvent("mousedown");
140 }
141
142 function driveTests()
143 {
144     switch (counter) {
145     case 0:
146         testOne();
147         break;
148     case 1:
149         testTwo();
150         break;
151     case 2:
152         testThree();
153         break;
154     case 3:
155         testFour();
156         break;
157     case 4:
158         testFive();
159         break;
160     case 5:
161         testSix();
162         break;
163     case 6:
164         testSeven();
165         break;
166     case 7:
167         testEight();
168         break;
169     }
170 }
171
172 // Start tests
173 if (window.eventSender) {
174     document.documentElement.offsetLeft;
175     eventSender.mouseMoveTo(115, 55);
176     driveTests();
177 } else
178     alert("This test must be run via DRT!");