Debian Codegraph @

dijit/tests/Destroyable.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5 <title>Destroyable unit test</title>
6
7 <script src="boilerplate.js"></script>
8
9 <script type="text/javascript">
10 require([
11 "doh/runner",
12 "dojo/aspect", "dojo/_base/declare", "dojo/Deferred", "dojo/dom", "dojo/dom-construct",
13 "dojo/_base/lang", "dojo/on", "dojo/Stateful",
14 "dijit/Destroyable", "dojo/domReady!"
15 ], function(doh, aspect, declare, Deferred, dom, domConstruct, lang, on, Stateful, Destroyable){
16 doh.register("Destroyable", [
17 function general(){
18 var SupportingWidget = declare([], {
19 destroyCalls: 0,
20 constructor: function(name){
21 this.name = name;
22 },
23 destroy: function(){
24 this.destroyCalls++;
25 }
26 });
27 var watchMe = new Stateful({
28 name: "watchMe",
29 x: 0
30 });
31 var DestroyableSubClass = declare(Destroyable, {
32 // number of times my button was clicked
33 clicks: 0,
34
35 // number of times watchMe changed value of x
36 watches: 0,
37
38 constructor: function(){
39 var self = this;
40 this.domNode = domConstruct.create("button");
41 this.own(
42 // setup an event handler (to be destroyed when I'm destroyed)
43 on(this.domNode, "click", function(){ self.clicks++; }),
44
45 // watch external watchMe class (to be unwatch()'d when I'm destroyed)
46 watchMe.watch("x", function(name, oVal, nVal){ self.watches++; })
47 );
48
49 // Setup two supporting widgets, to be destroyed when I'm destroyed
50 this.own(this.sw1 = new SupportingWidget("sw1"));
51 this.own(this.sw2 = new SupportingWidget("sw2"));
52 }
53 });
54
55 var destroyable1 = new DestroyableSubClass();
56 dom.byId("container").appendChild(destroyable1.domNode);
57
58 // make sure event handler was setup
59 destroyable1.domNode.click();
60 doh.is(1, destroyable1.clicks, "one click");
61
62 // make sure watch handler was setup
63 watchMe.set("x", 1);
64 doh.is(1, destroyable1.watches, "one watch notification");
65
66 // manually destroy one of the supporting widgets
67 destroyable1.sw1.destroy();
68 doh.is(1, destroyable1.sw1.destroyCalls);
69
70 // Destroy the Destroyable instance itself. destroyable1 should:
71 // - destroy the sw2 supporting widget, but not try to re-destroy sw1
72 // - disconnect the watch() listener on watchMe
73 // - disconnect the click event handler on destroyable1.domNode
74 destroyable1.destroy();
75 doh.is(1, destroyable1.sw1.destroyCalls, "sw1 wasn't redestroyed");
76 doh.is(1, destroyable1.sw2.destroyCalls, "sw2 was destroyed");
77
78 destroyable1.domNode.click();
79 doh.is(1, destroyable1.clicks, "no new click notification");
80 watchMe.set("x", 2);
81 doh.is(1, destroyable1.watches, "no new watch notification");
82 },
83
84 function multipleDestroyFunctions(){
85 var destroyRecursiveCount = 0;
86 var destroyCount = 0;
87 var W1 = declare([Destroyable], {
88 destroyRecursive: function() {
89 destroyRecursiveCount++;
90 this.destroy();
91 this.inherited(arguments);
92 },
93 destroy: function() {
94 destroyCount++;
95 this.inherited(arguments);
96 }
97 });
98 var W2 = declare([Destroyable], {
99 test: function() {
100 var w1 = new W1();
101 this.own(w1);
102 w1.destroy();
103 },
104 destroy: function() {
105 this.inherited(arguments);
106 }
107 });
108 var W3 = declare([Destroyable], {
109 test: function() {
110 var w1 = new W1();
111 this.own(w1);
112 w1.destroyRecursive();
113 },
114 destroy: function() {
115 this.inherited(arguments);
116 }
117 });
118 var w2 = new W2();
119 w2.test();
120 w2.destroy();
121 doh.is(0, destroyRecursiveCount, "destroyRecursive was not called as expected");
122 doh.is(1, destroyCount, "destroy was not called as expected");
123 destroyRecursiveCount = 0;
124 destroyCount = 0;
125 var w3 = new W3();
126 w3.test();
127 w3.destroy();
128 doh.is(1, destroyRecursiveCount, "destroyRecursive was not called as expected");
129 doh.is(1, destroyCount, "destroy was not called as expected");
130 },
131
132 function owningPromises(){
133 var cancels = 0;
134 var W1 = declare(Destroyable, {
135 constructor: function(){
136 this.p1 = new Deferred(function(){ console.log("cancel p1"); cancels++; });
137 this.p2 = new Deferred(function(){ console.log("cancel p2"); cancels++; });
138 this.p3 = new Deferred(function(){ console.log("cancel p3"); cancels++; });
139 this.p4 = new Deferred(function(){ console.log("cancel p4"); cancels++; });
140 this.own(this.p1, this.p2, this.p3, this.p4);
141 }
142 });
143
144 var w1 = new W1();
145
146 w1.p1.resolve(true);
147 aspect.after(w1.p1, "cancel", function(){
148 throw new Error("p1 shouldn't have been canceled");
149 }, true);
150
151 w1.p2.reject(new Error("I was rejected"));
152 aspect.after(w1.p2, "cancel", function(){
153 throw new Error("p2 shouldn't have been canceled");
154 }, true);
155
156 w1.p3.cancel();
157 doh.is(1, cancels, "one promise canceled manually before destroy");
158
159 // Destroying the widget should only cancel p4; it's the only Promise that hasn't been dealt with already.
160 // OTOH if Destroyable is broken, one of the asserts above may go off during the destroy() call.
161 w1.destroy();
162
163 doh.is(2, cancels, "only p4 canceled on widget destroy");
164 }
165
166 ]); // doh.register()
167
168 doh.run();
169 }); // require()
170
171 </script>
172 </head>
173 <body>
174 <h1>dijit/Destroyable Unit Test</h1>
175 <div id="container"></div>
176 </body>
177 </html>