source: Dev/branches/rest-dojo-ui/client/dojo/tests/on.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 5.7 KB
Line 
1dojo.provide("dojo.tests.on");
2
3var on = dojo.require("dojo.on");
4var has = dojo.require("dojo.has");
5var topic = dojo.require("dojo.topic");
6var Evented = dojo.require("dojo.Evented");
7doh.register("tests.on",
8        [
9                function object(t){
10                        var order = [];
11                        var obj = new dojo.Evented();
12                        obj.oncustom = function(event){
13                                order.push(event.a);
14                                return event.a+1;
15                        };
16                        var signal = on.pausable(obj, "custom", function(event){
17                                order.push(0);
18                                return event.a+1;
19                        });
20                        obj.oncustom({a:0});
21                        var signal2 = on(obj, "custom, foo", function(event){
22                                order.push(event.a);
23                        });
24                        on.emit(obj, "custom", {
25                                a: 3
26                        });
27                        signal.pause();
28                        var signal3 = on(obj, "custom", function(a){
29                                order.push(3);
30                        }, true);
31                        on.emit(obj, "custom", {
32                                a: 3
33                        });
34                        signal2.remove();
35                        signal.resume();
36                        on.emit(obj, "custom", {
37                                a: 6
38                        });
39                        signal3.remove();
40                        var signal4 = on(obj, "foo, custom", function(a){
41                                order.push(4);
42                        }, true);
43                        signal.remove();
44                        on.emit(obj, "custom", {
45                                a: 7
46                        });
47                        t.is(order, [0,0,3,0,3,3,3,3,6,0,3,7,4]);
48                },
49                function once(t){
50                        var order = [];
51                        var obj = new dojo.Evented();
52                        obj.on("custom", function(event){
53                                order.push(event.a);
54                        });
55                        var signal = on.once(obj, "custom", function(event){
56                                order.push(1);
57                        });
58                        obj.emit("custom",{a:0});
59                        obj.oncustom({a:2}); // should call original method, but not listener
60                        t.is(order, [0,1,2]);
61                },
62                function dom(t){
63                        var div = document.body.appendChild(document.createElement("div"));
64                        var span = div.appendChild(document.createElement("span"));
65                        var order = [];
66                        var signal = on(div,"custom", function(event){
67                                order.push(event.a);
68                                event.addedProp += "ue";
69                        });
70                        on(span,"custom", function(event){
71                                event.addedProp = "val";
72                        });
73                        on.emit(div, "custom", {
74                                target: div,
75                                currentTarget:div,
76                                relatedTarget: div,
77                                a: 0
78                        });
79                        on.emit(div, "otherevent", {
80                                a: 0
81                        });
82                        t.is(on.emit(span, "custom", {
83                                a: 1,
84                                bubbles: true,
85                                cancelable: true
86                        }).addedProp, "value");
87                        t.t(on.emit(span, "custom", {
88                                a: 1,
89                                bubbles: false,
90                                cancelable: true
91                        }));
92                        var signal2 = on.pausable(div,"custom", function(event){
93                                order.push(event.a + 1);
94                                event.preventDefault();
95                        });
96                        t.f(on.emit(span, "custom", {
97                                a: 2,
98                                bubbles: true,
99                                cancelable: true
100                        }));
101                        signal2.pause();
102                        t.is(on.emit(span, "custom", {
103                                a: 4,
104                                bubbles: true,
105                                cancelable: true
106                        }).type, "custom");
107                        signal2.resume();
108                        signal.remove();
109                        t.f(on.emit(span, "custom", {
110                                a: 4,
111                                bubbles: true,
112                                cancelable: true
113                        }));
114                        on(span, "custom", function(event){
115                                order.push(6);
116                                event.stopPropagation();
117                        });
118                        t.t(on.emit(span, "custom", {
119                                a: 1,
120                                bubbles: true,
121                                cancelable: true
122                        }));
123                        var button = div.appendChild(document.createElement("button"));
124                        // make sure we are propagating natively created events too
125                        signal = on(div, "click", function(){
126                                order.push(7);
127                        });
128                        button.click();
129                        signal.remove();
130                        // test out event delegation
131                        if(dojo.query){
132                                // if dojo.query is loaded, test event delegation
133                                on(div, "button:click", function(){
134                                        order.push(8);
135                                });
136                                button.click();
137                        }else{//just pass then
138                                order.push(8);
139                        }
140                        t.is(order, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
141                },
142/*
143 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 
144                function focus(t){
145                        var div = document.body.appendChild(document.createElement("div"));
146                        var input = div.appendChild(document.createElement("input"));
147                        var order = [];
148                        var signal = on(div,"input:focusin", function(event){
149                                order.push('in');
150                        });
151                        var signal = on(div,"input:focusout", function(event){
152                                order.push('out');
153                        });
154                        var otherInput = document.body.appendChild(document.createElement("input"));
155                        input.focus();
156                        otherInput.focus();
157                        d = new doh.Deferred();
158                        setTimeout(function(){
159                                t.is(['in', 'out'], order);
160                                d.callback(true);
161                        }, 1);
162                        return d;
163                },*/
164                function extensionEvent(t){
165                        var div = document.body.appendChild(document.createElement("div"));
166                        var span = div.appendChild(document.createElement("span"));
167                        span.setAttribute("foo", 2);
168                        var order = [];
169                        var customEvent = function(target, listener){
170                                return on(target, "custom", listener);
171                        };
172                        var signal = on(div, customEvent, function(event){
173                                order.push(event.a);
174                        });
175                        var signal = on(div, on.selector("span", customEvent), function(event){
176                                order.push(+this.getAttribute("foo"));
177                        });
178                        on.emit(div, "custom", {
179                                a: 0
180                        });
181                        // should trigger selector
182                        t.t(on.emit(span, "custom", {
183                                a: 1,
184                                bubbles: true,
185                                cancelable: true
186                        }));
187                        // shouldn't trigger selector
188                        t.t(on.emit(div, "custom", {
189                                a: 3,
190                                bubbles: true,
191                                cancelable: true
192                        }));
193                        t.is(order, [0, 1, 2, 3]);
194                },
195                function testEvented(t){
196                        var MyClass = dojo.declare([Evented],{
197
198                        });
199                        var order = [];
200                        myObject = new MyClass;
201                        myObject.on("custom", function(event){
202                                order.push(event.a);
203                        });
204                        myObject.emit("custom", {a:0});
205                        t.is(order, [0]);
206                },
207                function pubsub(t){
208                        var fooCount = 0;
209                        topic.subscribe("/test/foo", function(event, secondArg){
210                                t.is("value", event.foo);
211                                t.is("second", secondArg);
212                                fooCount++;
213                        });
214                        topic.publish("/test/foo", {foo: "value"}, "second");
215                        t.is(1, fooCount);
216                },
217                function touch(t){
218                        console.log("has", has);
219                        if(has("touch")){
220                                var div = document.body.appendChild(document.createElement("div"));
221                                on(div, "touchstart", function(event){
222                                        t.t("rotation" in event);
223                                        t.t("pageX" in event);
224                                });
225                                on.emit(div, "touchstart", {changedTouches: [{pageX:100}]});
226                        }
227                }
228        ]
229);
Note: See TracBrowser for help on using the repository browser.