1 | define([ |
---|
2 | "doh", "require", |
---|
3 | "dojo/_base/declare", "dojo/Evented", "dojo/has", "dojo/on", "dojo/query", "dojo/topic" |
---|
4 | ], function(doh, require, declare, Evented, has, on, query, topic){ |
---|
5 | |
---|
6 | doh.register("tests.on", [ |
---|
7 | function object(t){ |
---|
8 | var order = []; |
---|
9 | var obj = new Evented(); |
---|
10 | obj.oncustom = function(event){ |
---|
11 | order.push(event.a); |
---|
12 | return event.a+1; |
---|
13 | }; |
---|
14 | var signal = on.pausable(obj, "custom", function(event){ |
---|
15 | order.push(0); |
---|
16 | return event.a+1; |
---|
17 | }); |
---|
18 | obj.oncustom({a:0}); |
---|
19 | var signal2 = on(obj, "custom, foo", function(event){ |
---|
20 | order.push(event.a); |
---|
21 | }); |
---|
22 | on.emit(obj, "custom", { |
---|
23 | a: 3 |
---|
24 | }); |
---|
25 | signal.pause(); |
---|
26 | var signal3 = on(obj, "custom", function(a){ |
---|
27 | order.push(3); |
---|
28 | }, true); |
---|
29 | on.emit(obj, "custom", { |
---|
30 | a: 3 |
---|
31 | }); |
---|
32 | signal2.remove(); |
---|
33 | signal.resume(); |
---|
34 | on.emit(obj, "custom", { |
---|
35 | a: 6 |
---|
36 | }); |
---|
37 | signal3.remove(); |
---|
38 | var signal4 = on(obj, "foo, custom", function(a){ |
---|
39 | order.push(4); |
---|
40 | }, true); |
---|
41 | signal.remove(); |
---|
42 | on.emit(obj, "custom", { |
---|
43 | a: 7 |
---|
44 | }); |
---|
45 | t.is(order, [0,0,3,0,3,3,3,3,6,0,3,7,4]); |
---|
46 | }, |
---|
47 | function once(t){ |
---|
48 | var order = []; |
---|
49 | var obj = new Evented(); |
---|
50 | obj.on("custom", function(event){ |
---|
51 | order.push(event.a); |
---|
52 | }); |
---|
53 | var signal = on.once(obj, "custom", function(event){ |
---|
54 | order.push(1); |
---|
55 | }); |
---|
56 | obj.emit("custom",{a:0}); |
---|
57 | obj.oncustom({a:2}); // should call original method, but not listener |
---|
58 | t.is(order, [0,1,2]); |
---|
59 | }, |
---|
60 | function dom(t){ |
---|
61 | var div = document.body.appendChild(document.createElement("div")); |
---|
62 | var span = div.appendChild(document.createElement("span")); |
---|
63 | var order = []; |
---|
64 | var signal = on(div,"custom", function(event){ |
---|
65 | order.push(event.a); |
---|
66 | event.addedProp += "ue"; |
---|
67 | }); |
---|
68 | on(span,"custom", function(event){ |
---|
69 | event.addedProp = "val"; |
---|
70 | }); |
---|
71 | on.emit(div, "custom", { |
---|
72 | target: div, |
---|
73 | currentTarget:div, |
---|
74 | relatedTarget: div, |
---|
75 | a: 0 |
---|
76 | }); |
---|
77 | on.emit(div, "otherevent", { |
---|
78 | a: 0 |
---|
79 | }); |
---|
80 | t.is(on.emit(span, "custom", { |
---|
81 | a: 1, |
---|
82 | bubbles: true, |
---|
83 | cancelable: true |
---|
84 | }).addedProp, "value"); |
---|
85 | t.t(on.emit(span, "custom", { |
---|
86 | a: 1, |
---|
87 | bubbles: false, |
---|
88 | cancelable: true |
---|
89 | })); |
---|
90 | var signal2 = on.pausable(div,"custom", function(event){ |
---|
91 | order.push(event.a + 1); |
---|
92 | event.preventDefault(); |
---|
93 | }); |
---|
94 | t.f(on.emit(span, "custom", { |
---|
95 | a: 2, |
---|
96 | bubbles: true, |
---|
97 | cancelable: true |
---|
98 | })); |
---|
99 | signal2.pause(); |
---|
100 | t.is(on.emit(span, "custom", { |
---|
101 | a: 4, |
---|
102 | bubbles: true, |
---|
103 | cancelable: true |
---|
104 | }).type, "custom"); |
---|
105 | signal2.resume(); |
---|
106 | signal.remove(); |
---|
107 | t.f(on.emit(span, "custom", { |
---|
108 | a: 4, |
---|
109 | bubbles: true, |
---|
110 | cancelable: true |
---|
111 | })); |
---|
112 | on(span, "custom", function(event){ |
---|
113 | order.push(6); |
---|
114 | event.stopPropagation(); |
---|
115 | }); |
---|
116 | t.t(on.emit(span, "custom", { |
---|
117 | a: 1, |
---|
118 | bubbles: true, |
---|
119 | cancelable: true |
---|
120 | })); |
---|
121 | |
---|
122 | // make sure we are propagating natively created events too, and that defaultPrevented works |
---|
123 | var button = span.appendChild(document.createElement("button")), |
---|
124 | defaultPrevented = false, |
---|
125 | signal2Fired = false; |
---|
126 | signal = on(span, "click", function(event){ |
---|
127 | event.preventDefault(); |
---|
128 | }); |
---|
129 | signal2 = on(div, "click", function(event){ |
---|
130 | order.push(7); |
---|
131 | signal2Fired = true; |
---|
132 | defaultPrevented = event.defaultPrevented; |
---|
133 | }); |
---|
134 | button.click(); |
---|
135 | t.t(signal2Fired, "bubbled click event on div"); |
---|
136 | t.t(defaultPrevented, "defaultPrevented for click event"); |
---|
137 | signal.remove(); |
---|
138 | signal2.remove(); |
---|
139 | |
---|
140 | // make sure that evt.defaultPrevented gets set for synthetic events too |
---|
141 | signal = on(span, "click", function(event){ |
---|
142 | event.preventDefault(); |
---|
143 | }); |
---|
144 | signal2 = on(div, "click", function(event){ |
---|
145 | signal2Fired = true; |
---|
146 | defaultPrevented = event.defaultPrevented; |
---|
147 | }); |
---|
148 | signal2Fired = false; |
---|
149 | on.emit(button, "click", {bubbles: true, cancelable: true}); |
---|
150 | t.t(signal2Fired, "bubbled synthetic event on div"); |
---|
151 | t.t(defaultPrevented, "defaultPrevented set for synthetic event on div"); |
---|
152 | signal.remove(); |
---|
153 | signal2.remove(); |
---|
154 | |
---|
155 | // make sure 'document' and 'window' can also emit events |
---|
156 | var eventEmitted; |
---|
157 | var globalObjects = [document, window]; |
---|
158 | for(var i = 0, len = globalObjects.length; i < len; i++) { |
---|
159 | eventEmitted = false; |
---|
160 | on(globalObjects[i], 'custom-test-event', function () { |
---|
161 | eventEmitted = true; |
---|
162 | }); |
---|
163 | on.emit(globalObjects[i], 'custom-test-event', {}); |
---|
164 | t.is(true, eventEmitted); |
---|
165 | } |
---|
166 | |
---|
167 | // test out event delegation |
---|
168 | if(query){ |
---|
169 | // if dojo.query is loaded, test event delegation |
---|
170 | on(div, "button:click", function(){ |
---|
171 | order.push(8); |
---|
172 | }); |
---|
173 | on(document, "button:click", function(){ |
---|
174 | }); // just make sure this doesn't throw an error |
---|
175 | }else{//just pass then |
---|
176 | order.push(8); |
---|
177 | } |
---|
178 | // test out event delegation using a custom selector |
---|
179 | on(div, on.selector(function(node){ |
---|
180 | return node.tagName == "BUTTON"; |
---|
181 | }, "click"), function(){ |
---|
182 | order.push(9); |
---|
183 | }); |
---|
184 | button.click(); |
---|
185 | t.is(order, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
---|
186 | on(span, "propertychange", function(){}); // make sure it doesn't throw an error |
---|
187 | }, |
---|
188 | /* |
---|
189 | This only works if the test page has the focus, so you can enable if you want to test focus functionality and allow the test page to have focus |
---|
190 | function focus(t){ |
---|
191 | var div = document.body.appendChild(document.createElement("div")); |
---|
192 | var input = div.appendChild(document.createElement("input")); |
---|
193 | var order = []; |
---|
194 | var signal = on(div,"input:focusin", function(event){ |
---|
195 | order.push('in'); |
---|
196 | }); |
---|
197 | var signal = on(div,"input:focusout", function(event){ |
---|
198 | order.push('out'); |
---|
199 | }); |
---|
200 | var otherInput = document.body.appendChild(document.createElement("input")); |
---|
201 | input.focus(); |
---|
202 | otherInput.focus(); |
---|
203 | d = new doh.Deferred(); |
---|
204 | setTimeout(function(){ |
---|
205 | t.is(['in', 'out'], order); |
---|
206 | d.callback(true); |
---|
207 | }, 1); |
---|
208 | return d; |
---|
209 | },*/ |
---|
210 | function extensionEvent(t){ |
---|
211 | var div = document.body.appendChild(document.createElement("div")); |
---|
212 | var span = div.appendChild(document.createElement("span")); |
---|
213 | span.setAttribute("foo", 2); |
---|
214 | var order = []; |
---|
215 | var customEvent = function(target, listener){ |
---|
216 | return on(target, "custom", listener); |
---|
217 | }; |
---|
218 | on(div, customEvent, function(event){ |
---|
219 | order.push(event.a); |
---|
220 | }); |
---|
221 | on(div, on.selector("span", customEvent), function(event){ |
---|
222 | order.push(+this.getAttribute("foo")); |
---|
223 | }); |
---|
224 | on.emit(div, "custom", { |
---|
225 | a: 0 |
---|
226 | }); |
---|
227 | // should trigger selector |
---|
228 | t.t(on.emit(span, "custom", { |
---|
229 | a: 1, |
---|
230 | bubbles: true, |
---|
231 | cancelable: true |
---|
232 | })); |
---|
233 | // shouldn't trigger selector |
---|
234 | t.t(on.emit(div, "custom", { |
---|
235 | a: 3, |
---|
236 | bubbles: true, |
---|
237 | cancelable: true |
---|
238 | })); |
---|
239 | t.is(order, [0, 1, 2, 3]); |
---|
240 | }, |
---|
241 | function testEvented(t){ |
---|
242 | var MyClass = declare([Evented],{ |
---|
243 | |
---|
244 | }); |
---|
245 | var order = []; |
---|
246 | myObject = new MyClass; |
---|
247 | myObject.on("custom", function(event){ |
---|
248 | order.push(event.a); |
---|
249 | }); |
---|
250 | myObject.emit("custom", {a:0}); |
---|
251 | t.is(order, [0]); |
---|
252 | }, |
---|
253 | function pubsub(t){ |
---|
254 | var fooCount = 0; |
---|
255 | topic.subscribe("/test/foo", function(event, secondArg){ |
---|
256 | t.is("value", event.foo); |
---|
257 | t.is("second", secondArg); |
---|
258 | fooCount++; |
---|
259 | }); |
---|
260 | topic.publish("/test/foo", {foo: "value"}, "second"); |
---|
261 | t.is(1, fooCount); |
---|
262 | }, |
---|
263 | function touch(t){ |
---|
264 | console.log("has", has); |
---|
265 | if(has("touch")){ |
---|
266 | var div = document.body.appendChild(document.createElement("div")); |
---|
267 | on(div, "touchstart", function(event){ |
---|
268 | t.t("rotation" in event); |
---|
269 | t.t("pageX" in event); |
---|
270 | }); |
---|
271 | on.emit(div, "touchstart", {changedTouches: [{pageX:100}]}); |
---|
272 | } |
---|
273 | }, |
---|
274 | function stopImmediatePropagation(t){ |
---|
275 | var button = document.body.appendChild(document.createElement("button")); |
---|
276 | on(button, "click", function(event){ |
---|
277 | event.stopImmediatePropagation(); |
---|
278 | }); |
---|
279 | var afterStop = false; |
---|
280 | on(button, "click", function(event){ |
---|
281 | afterStop = true; |
---|
282 | }); |
---|
283 | button.click(); |
---|
284 | t.f(afterStop); |
---|
285 | }, |
---|
286 | function eventAugmentation(t){ |
---|
287 | var div = document.body.appendChild(document.createElement("div")); |
---|
288 | var button = div.appendChild(document.createElement("button")); |
---|
289 | on(button, "click", function(event){ |
---|
290 | event.modified = true; |
---|
291 | event.test = 3; |
---|
292 | }); |
---|
293 | var testValue; |
---|
294 | on(div, "click", function(event){ |
---|
295 | testValue = event.test; |
---|
296 | }); |
---|
297 | button.click(); |
---|
298 | t.is(testValue, 3); |
---|
299 | } |
---|
300 | ]); |
---|
301 | |
---|
302 | if(has("host-browser")){ |
---|
303 | doh.registerUrl("tests.on.event-focusin", require.toUrl("./event-focusin.html"), 30000); |
---|
304 | } |
---|
305 | }); |
---|