|
14 | 14 | htmlElement = HtmlElement.fromHtml("<div></div>"); |
15 | 15 | }); |
16 | 16 |
|
17 | | - it("triggers mouse events relative to element and handles them relative to page", function() { |
18 | | - testEvent(htmlElement.onSelectStart_ie8Only, htmlElement.doSelectStart); |
19 | | - testEvent(htmlElement.onMouseDown, htmlElement.doMouseDown); |
20 | | - testEvent(htmlElement.onMouseMove, htmlElement.doMouseMove); |
21 | | - testEvent(htmlElement.onMouseLeave, htmlElement.doMouseLeave); |
22 | | - testEvent(htmlElement.onMouseUp, htmlElement.doMouseUp); |
23 | | - }); |
24 | 17 |
|
25 | | - it("handles single-touch events", function() { |
26 | | - if (!browser.supportsTouchEvents()) return; |
27 | | - testEvent(htmlElement.onSingleTouchStart, htmlElement.doSingleTouchStart); |
28 | | - testEvent(htmlElement.onSingleTouchMove, htmlElement.doSingleTouchMove); |
29 | | - testEvent(htmlElement.onSingleTouchEnd, htmlElement.doSingleTouchEnd); |
30 | | - testEvent(htmlElement.onSingleTouchCancel, htmlElement.doSingleTouchCancel); |
31 | | - }); |
| 18 | + describe("event handling", function() { |
32 | 19 |
|
33 | | - it("handles multi-touch events", function() { |
34 | | - if (!browser.supportsTouchEvents()) return; |
35 | | - var eventTriggered = false; |
36 | | - htmlElement.onMultiTouchStart(function() { |
37 | | - eventTriggered = true; |
| 20 | + it("triggers mouse events relative to element and handles them relative to page", function() { |
| 21 | + testEvent(htmlElement.onSelectStart_ie8Only, htmlElement.doSelectStart); |
| 22 | + testEvent(htmlElement.onMouseDown, htmlElement.doMouseDown); |
| 23 | + testEvent(htmlElement.onMouseMove, htmlElement.doMouseMove); |
| 24 | + testEvent(htmlElement.onMouseLeave, htmlElement.doMouseLeave); |
| 25 | + testEvent(htmlElement.onMouseUp, htmlElement.doMouseUp); |
38 | 26 | }); |
39 | 27 |
|
40 | | - htmlElement.doMultiTouchStart(1, 2, 3, 4); |
41 | | - expect(eventTriggered).to.be(true); |
42 | | - }); |
| 28 | + it("simulates buggy IE 8 behavior (where user events on window aren't sent to window object)", function() { |
| 29 | + // to do |
| 30 | + }); |
43 | 31 |
|
44 | | - it("allows mouse events to be triggered without coordinates", function() { |
45 | | - var eventPageOffset; |
46 | | - htmlElement.onMouseDown(function(pageOffset) { |
47 | | - eventPageOffset = pageOffset; |
| 32 | + it("handles single-touch events", function() { |
| 33 | + if (!browser.supportsTouchEvents()) return; |
| 34 | + testEvent(htmlElement.onSingleTouchStart, htmlElement.doSingleTouchStart); |
| 35 | + testEvent(htmlElement.onSingleTouchMove, htmlElement.doSingleTouchMove); |
| 36 | + testEvent(htmlElement.onSingleTouchEnd, htmlElement.doSingleTouchEnd); |
| 37 | + testEvent(htmlElement.onSingleTouchCancel, htmlElement.doSingleTouchCancel); |
48 | 38 | }); |
49 | 39 |
|
50 | | - htmlElement.doMouseDown(); |
51 | | - expect(eventPageOffset).to.eql({ x: 0, y: 0 }); |
52 | | - }); |
| 40 | + it("handles multi-touch events", function() { |
| 41 | + if (!browser.supportsTouchEvents()) return; |
| 42 | + var eventTriggered = false; |
| 43 | + htmlElement.onMultiTouchStart(function() { |
| 44 | + eventTriggered = true; |
| 45 | + }); |
53 | 46 |
|
54 | | - it("allows touch events to be triggered without coordinates", function() { |
55 | | - if (!browser.supportsTouchEvents()) return; |
56 | | - var eventPageOffset; |
57 | | - htmlElement.onSingleTouchStart(function(pageOffset) { |
58 | | - eventPageOffset = pageOffset; |
| 47 | + htmlElement.doMultiTouchStart(1, 2, 3, 4); |
| 48 | + expect(eventTriggered).to.be(true); |
59 | 49 | }); |
60 | 50 |
|
61 | | - htmlElement.doSingleTouchStart(); |
62 | | - expect(eventPageOffset).to.eql({ x: 0, y: 0 }); |
63 | | - }); |
| 51 | + it("allows mouse events to be triggered without coordinates", function() { |
| 52 | + var eventPageOffset; |
| 53 | + htmlElement.onMouseDown(function(pageOffset) { |
| 54 | + eventPageOffset = pageOffset; |
| 55 | + }); |
64 | 56 |
|
65 | | - it("clears all event handlers (useful for testing)", function() { |
66 | | - htmlElement.onMouseDown(function() { |
67 | | - throw new Error("event handler should have been removed"); |
| 57 | + htmlElement.doMouseDown(); |
| 58 | + expect(eventPageOffset).to.eql({ x: 0, y: 0 }); |
68 | 59 | }); |
69 | 60 |
|
70 | | - htmlElement.removeAllEventHandlers(); |
71 | | - htmlElement.doMouseDown(0, 0); |
72 | | - }); |
| 61 | + it("allows touch events to be triggered without coordinates", function() { |
| 62 | + if (!browser.supportsTouchEvents()) return; |
| 63 | + var eventPageOffset; |
| 64 | + htmlElement.onSingleTouchStart(function(pageOffset) { |
| 65 | + eventPageOffset = pageOffset; |
| 66 | + }); |
| 67 | + |
| 68 | + htmlElement.doSingleTouchStart(); |
| 69 | + expect(eventPageOffset).to.eql({ x: 0, y: 0 }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("clears all event handlers (useful for testing)", function() { |
| 73 | + htmlElement.onMouseDown(function() { |
| 74 | + throw new Error("event handler should have been removed"); |
| 75 | + }); |
| 76 | + |
| 77 | + htmlElement.removeAllEventHandlers(); |
| 78 | + htmlElement.doMouseDown(0, 0); |
| 79 | + }); |
73 | 80 |
|
74 | | - it("converts page coordinates into relative element coordinates", function() { |
75 | | - try { |
76 | | - htmlElement.appendSelfToBody(); |
77 | | - var offset = htmlElement.relativeOffset({x: 100, y: 150}); |
| 81 | + function testEvent(eventSender, eventHandler) { |
| 82 | + try { |
| 83 | + htmlElement.appendSelfToBody(); |
78 | 84 |
|
79 | | - if (browser.reportsElementPositionOffByOneSometimes()) { |
80 | | - // compensate for off-by-one error in IE 8 |
81 | | - expect(offset.x).to.equal(92); |
82 | | - expect(offset.y === 141 || offset.y === 142).to.be(true); |
| 85 | + var eventPageOffset = null; |
| 86 | + eventSender.call(htmlElement, function(pageOffset) { |
| 87 | + eventPageOffset = pageOffset; |
| 88 | + }); |
| 89 | + eventHandler.call(htmlElement, 42, 13); |
| 90 | + expect(htmlElement.relativeOffset(eventPageOffset)).to.eql({ x: 42, y: 13}); |
83 | 91 | } |
84 | | - else { |
85 | | - expect(offset).to.eql({x: 92, y: 142}); |
| 92 | + finally { |
| 93 | + htmlElement.remove(); |
86 | 94 | } |
87 | | - } finally { |
88 | | - htmlElement.remove(); |
89 | 95 | } |
90 | 96 | }); |
91 | 97 |
|
92 | | - it("converts relative coordinates into page coordinates", function() { |
93 | | - try { |
94 | | - htmlElement.appendSelfToBody(); |
95 | | - var offset = htmlElement.pageOffset({x: 100, y: 150}); |
96 | 98 |
|
97 | | - if (browser.reportsElementPositionOffByOneSometimes()) { |
98 | | - // compensate for off-by-one error in IE 8 |
99 | | - expect(offset.x).to.equal(108); |
100 | | - expect(offset.y === 158 || offset.y === 159).to.be(true); |
| 99 | + describe("coordinate conversion", function() { |
| 100 | + |
| 101 | + it("converts page coordinates into relative element coordinates", function() { |
| 102 | + try { |
| 103 | + htmlElement.appendSelfToBody(); |
| 104 | + var offset = htmlElement.relativeOffset({x: 100, y: 150}); |
| 105 | + |
| 106 | + if (browser.reportsElementPositionOffByOneSometimes()) { |
| 107 | + // compensate for off-by-one error in IE 8 |
| 108 | + expect(offset.x).to.equal(92); |
| 109 | + expect(offset.y === 141 || offset.y === 142).to.be(true); |
| 110 | + } |
| 111 | + else { |
| 112 | + expect(offset).to.eql({x: 92, y: 142}); |
| 113 | + } |
| 114 | + } finally { |
| 115 | + htmlElement.remove(); |
101 | 116 | } |
102 | | - else { |
103 | | - expect(offset).to.eql({x: 108, y: 158}); |
| 117 | + }); |
| 118 | + |
| 119 | + it("converts relative coordinates into page coordinates", function() { |
| 120 | + try { |
| 121 | + htmlElement.appendSelfToBody(); |
| 122 | + var offset = htmlElement.pageOffset({x: 100, y: 150}); |
| 123 | + |
| 124 | + if (browser.reportsElementPositionOffByOneSometimes()) { |
| 125 | + // compensate for off-by-one error in IE 8 |
| 126 | + expect(offset.x).to.equal(108); |
| 127 | + expect(offset.y === 158 || offset.y === 159).to.be(true); |
| 128 | + } |
| 129 | + else { |
| 130 | + expect(offset).to.eql({x: 108, y: 158}); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + htmlElement.remove(); |
104 | 134 | } |
105 | | - } finally { |
106 | | - htmlElement.remove(); |
107 | | - } |
| 135 | + }); |
108 | 136 | }); |
109 | 137 |
|
110 | | - it("appends elements", function() { |
111 | | - htmlElement.append(HtmlElement.fromHtml("<div></div>")); |
112 | | - expect(htmlElement._element.children().length).to.equal(1); |
113 | | - }); |
114 | 138 |
|
115 | | - it("appends to body", function() { |
116 | | - try { |
117 | | - var body = new HtmlElement(document.body); |
118 | | - var childrenBeforeAppend = body._element.children().length; |
| 139 | + describe("DOM tree modification", function() { |
119 | 140 |
|
120 | | - htmlElement.appendSelfToBody(); |
121 | | - var childrenAfterAppend = body._element.children().length; |
122 | | - expect(childrenBeforeAppend + 1).to.equal(childrenAfterAppend); |
123 | | - } finally { |
124 | | - htmlElement.remove(); |
125 | | - } |
126 | | - }); |
| 141 | + it("appends elements", function() { |
| 142 | + htmlElement.append(HtmlElement.fromHtml("<div></div>")); |
| 143 | + expect(htmlElement._element.children().length).to.equal(1); |
| 144 | + }); |
127 | 145 |
|
128 | | - it("removes elements", function() { |
129 | | - var elementToAppend = HtmlElement.fromHtml("<div></div>"); |
130 | | - htmlElement.append(elementToAppend); |
131 | | - elementToAppend.remove(); |
132 | | - expect(htmlElement._element.children().length).to.equal(0); |
133 | | - }); |
| 146 | + it("appends to body", function() { |
| 147 | + try { |
| 148 | + var body = new HtmlElement(document.body); |
| 149 | + var childrenBeforeAppend = body._element.children().length; |
134 | 150 |
|
135 | | - it("converts to DOM element", function() { |
136 | | - var domElement = htmlElement.toDomElement(); |
137 | | - expect(domElement.tagName).to.equal("DIV"); |
138 | | - }); |
| 151 | + htmlElement.appendSelfToBody(); |
| 152 | + var childrenAfterAppend = body._element.children().length; |
| 153 | + expect(childrenBeforeAppend + 1).to.equal(childrenAfterAppend); |
| 154 | + } finally { |
| 155 | + htmlElement.remove(); |
| 156 | + } |
| 157 | + }); |
139 | 158 |
|
140 | | - function testEvent(eventSender, eventHandler) { |
141 | | - try { |
142 | | - htmlElement.appendSelfToBody(); |
| 159 | + it("removes elements", function() { |
| 160 | + var elementToAppend = HtmlElement.fromHtml("<div></div>"); |
| 161 | + htmlElement.append(elementToAppend); |
| 162 | + elementToAppend.remove(); |
| 163 | + expect(htmlElement._element.children().length).to.equal(0); |
| 164 | + }); |
143 | 165 |
|
144 | | - var eventPageOffset = null; |
145 | | - eventSender.call(htmlElement, function(pageOffset) { |
146 | | - eventPageOffset = pageOffset; |
147 | | - }); |
148 | | - eventHandler.call(htmlElement, 42, 13); |
149 | | - expect(htmlElement.relativeOffset(eventPageOffset)).to.eql({ x: 42, y: 13}); |
150 | | - } |
151 | | - finally { |
152 | | - htmlElement.remove(); |
153 | | - } |
154 | | - } |
| 166 | + it("converts to DOM element", function() { |
| 167 | + var domElement = htmlElement.toDomElement(); |
| 168 | + expect(domElement.tagName).to.equal("DIV"); |
| 169 | + }); |
| 170 | + }); |
155 | 171 |
|
156 | 172 | }); |
157 | 173 |
|
|
0 commit comments