Debian Codegraph @

dijit/tests/_HasDropDown.html

raw symbols SCIP
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>_HasDropDown unit test</title>
5
6 <script type="text/javascript" src="boilerplate.js"></script>
7
8 <script type="text/javascript">
9 require([
10 "doh/runner",
11 "dojo/_base/declare", "dojo/dom-geometry", "dojo/has", "dojo/keys", "dojo/on", "dojo/_base/window",
12 "dijit/_HasDropDown", "dijit/_WidgetBase", "dijit/_TemplatedMixin",
13 "dijit/tests/helpers", "dojo/domReady!"
14 ], function(doh, declare, domGeom, has, keys, on, win,
15 _HasDropDown, _WidgetBase, _TemplatedMixin, helpers){
16
17
18 var SimplePopup = declare([_WidgetBase, _TemplatedMixin], {
19 // summary:
20 // A trivial popup widget
21
22 templateString: "<div></div>",
23
24 label: "i'm a popup",
25 _setLabelAttr: { node: "domNode", type: "innerText" }
26 });
27
28 var SimpleDropDownButton = declare([_WidgetBase, _TemplatedMixin, _HasDropDown], {
29 // summary:
30 // A button that shows a popup.
31
32 templateString: "<button></button>",
33
34 label: "show dropdown",
35 _setLabelAttr: { node: "domNode", type: "innerText" },
36
37 popupLabel: "i'm a popup",
38 _setPopupLabel: "dropDown",
39
40 orient: ["below"],
41
42 postCreate: function(){
43 this.inherited(arguments);
44 this.dropDown = new SimplePopup({
45 id: this.id + "_popup"
46 });
47 }
48 });
49
50 var NonFocusableDropDownButton = declare([_WidgetBase, _TemplatedMixin, _HasDropDown], {
51 // summary:
52 // A non-focusable "button" that shows a popup. Should work for mouse, although not for keyboard.
53
54 label: "show popup (non-focusable)",
55 orient: ["below"],
56
57 templateString: "<span>${label}</span>",
58
59 postCreate: function(){
60 this.inherited(arguments);
61 this.dropDown = new SimplePopup({
62 id: this.id + "_popup",
63 label: "popup from non-focusable"
64 });
65 }
66 });
67
68 // Synthetic events. Could alternately use robot.
69 function key(node, key){
70 on.emit(node, "keydown", {
71 keyCode: key,
72 bubbles: true
73 });
74 on.emit(node, "keyup", {
75 keyCode: key,
76 bubbles: true
77 });
78 }
79 function click(node){
80 on.emit(node, has("pointer-events") ? "pointerdown" : has("MSPointer") ? "MSPointerDown" : "mousedown", {
81 button: 0, // left button (except on IE quirks mode, when it's 1)
82 bubbles: true
83 });
84 on.emit(node, has("pointer-events") ? "pointerup" : has("MSPointer") ? "MSPointerUp" : "mouseup", {
85 button: 0, // left button (except on IE quirks mode, when it's 1)
86 bubbles: true
87 });
88 on.emit(node, "click", {
89 bubbles: true
90 });
91 }
92
93 doh.register("basic", [
94 function setup(){
95 dd = new SimpleDropDownButton({
96 id: "dd",
97 label: "show dropdown - ltr"
98 }).placeAt(win.body());
99 popup = dd.dropDown;
100 doh.t(!!popup, "popup exists");
101 },
102 function open(){
103 var d = new doh.Deferred();
104 click(dd.domNode);
105 setTimeout(d.getTestCallback(function(){
106 doh.t(helpers.isVisible(popup), "popup visible");
107 var anchorPos = domGeom.position(dd.domNode),
108 dropDownPos = domGeom.position(popup.domNode);
109 doh.t(Math.abs(anchorPos.x - dropDownPos.x) < 1, "drop down and anchor left aligned");
110 doh.t(Math.abs(anchorPos.w - dropDownPos.w) < 1, "drop down same width as anchor");
111 }), 10);
112 return d;
113 },
114 function close(){
115 dd.closeDropDown();
116 doh.t(helpers.isHidden(popup), "popup hidden");
117 },
118 function openBySpace(){
119 var d = new doh.Deferred();
120 key(dd.domNode, keys.SPACE);
121 setTimeout(d.getTestCallback(function(){
122 doh.t(!!popup, "popup exists");
123 doh.t(helpers.isVisible(popup), "popup visible again");
124 }), 10);
125 return d;
126 },
127 function close2(){
128 dd.closeDropDown();
129 doh.t(helpers.isHidden(popup), "popup hidden again");
130 }
131 // TODO: open by down arrow
132 ]);
133
134 doh.register("rtl", [
135 function setup(){
136 dd = new SimpleDropDownButton({
137 id: "rdd",
138 dir: "rtl",
139 label: "show dropdown - rtl"
140 }).placeAt(win.body());
141 popup = dd.dropDown;
142 doh.t(!!popup, "popup exists");
143 },
144 function open(){
145 var d = new doh.Deferred();
146 click(dd.domNode);
147 setTimeout(d.getTestCallback(function(){
148 doh.t(helpers.isVisible(popup), "popup visible");
149 var anchorPos = domGeom.position(dd.domNode),
150 dropDownPos = domGeom.position(popup.domNode);
151 doh.t(Math.abs(anchorPos.x - dropDownPos.x) < 1, "drop down and anchor left aligned");
152 doh.t(Math.abs(anchorPos.w - dropDownPos.w) < 1, "drop down same width as anchor");
153 }), 10);
154 return d;
155 }
156 ]);
157
158 doh.register("non focusable", [
159 function setup(){
160 ndd = new NonFocusableDropDownButton({id: "ndd"}).placeAt(win.body());
161 popup = ndd.dropDown;
162 doh.t(!!popup, "popup exists");
163 },
164 function open(){
165 var d = new doh.Deferred();
166 click(ndd.domNode);
167 setTimeout(d.getTestCallback(function(){
168 doh.t(helpers.isVisible(popup), "popup visible");
169 }), 10);
170 return d;
171 },
172 function close(){
173 ndd.closeDropDown();
174 }
175 ]);
176
177 doh.register("alignment", [
178 function left(){
179 var d = new doh.Deferred();
180 var ltr = new SimpleDropDownButton({
181 id: "ltr",
182 label: "show non-auto-width dropdown - ltr",
183 autoWidth: false
184 }).placeAt(win.body());
185 click(ltr.domNode);
186 setTimeout(d.getTestCallback(function(){
187 var anchorPos = domGeom.position(ltr.domNode),
188 dropDownPos = domGeom.position(ltr.dropDown.domNode);
189 doh.t(Math.abs(anchorPos.x - dropDownPos.x) < 1, "drop down and anchor left aligned");
190 doh.t(anchorPos.w > dropDownPos.w, "anchor wider than drop down");
191 }), 10);
192 return d;
193 },
194 function right(){
195 var d = new doh.Deferred();
196 var rtl = new SimpleDropDownButton({
197 id: "rtl",
198 dir: "rtl",
199 label: "show non-auto-width dropdown - rtl",
200 autoWidth: false
201 }).placeAt(win.body());
202 click(rtl.domNode);
203 setTimeout(d.getTestCallback(function(){
204 var anchorPos = domGeom.position(rtl.domNode),
205 dropDownPos = domGeom.position(rtl.dropDown.domNode);
206 doh.t(Math.abs((anchorPos.x + anchorPos.w) - (dropDownPos.x + dropDownPos.w)) < 1,
207 "drop down and anchor right aligned");
208 doh.t(anchorPos.w > dropDownPos.w, "anchor wider than drop down");
209 }), 10);
210 return d;
211 }
212 ]);
213
214 doh.register("autoWidth", [
215 function setup(){
216 dd = new SimpleDropDownButton({
217 id: "autowidth",
218 label: "auto width",
219 popupLabel: "x" // start with narrow popup to test it gets expanded
220 }).placeAt(win.body());
221 popup = dd.dropDown;
222 doh.t(!!popup, "popup exists");
223 },
224 function open(){
225 var d = new doh.Deferred();
226 click(dd.domNode);
227 setTimeout(d.getTestCallback(function(){
228 doh.t(helpers.isVisible(popup), "popup visible");
229 var anchorPos = domGeom.position(dd.domNode),
230 dropDownPos = domGeom.position(popup.domNode);
231 doh.t(Math.abs(anchorPos.w - dropDownPos.w) < 1, "drop down same width as anchor");
232 }), 10);
233 return d;
234 },
235 function close(){
236 dd.closeDropDown();
237 doh.t(helpers.isHidden(popup), "popup hidden");
238 },
239 function open2(){
240 var d = new doh.Deferred();
241 dd.dropDown.set("label", "this is a very wide dropdown, wider than the button")
242 click(dd.domNode);
243 setTimeout(d.getTestCallback(function(){
244 doh.t(helpers.isVisible(popup), "popup visible");
245 var anchorPos = domGeom.position(dd.domNode),
246 dropDownPos = domGeom.position(popup.domNode);
247 doh.t(dropDownPos.w > anchorPos.w + 1,
248 "drop down wider than anchor, " + dropDownPos.w + ", " + anchorPos.w);
249 }), 10);
250 return d;
251 },
252 function close2(){
253 dd.closeDropDown();
254 doh.t(helpers.isHidden(popup), "popup hidden");
255 }
256 ]);
257
258 doh.register("destroy", [
259 function setup(){
260 dd = new SimpleDropDownButton({id: "dd2"}).placeAt(win.body());
261 popup = dd.dropDown;
262 doh.t(!!popup, "popup exists");
263 },
264 function open(){
265 var d = new doh.Deferred();
266 click(dd.domNode);
267 setTimeout(d.getTestCallback(function(){
268 doh.t(helpers.isVisible(popup), "popup visible");
269 doh.is(1, require("dijit/popup")._stack.length, "in popup manager stack");
270 }), 10);
271 return d;
272 },
273 function destroy(){
274 dd.destroy();
275 doh.is(0, require("dijit/popup")._stack.length, "popup was closed");
276 }
277 ]);
278
279 doh.run();
280 });
281 </script>
282 </head>
283 <body>
284 <h1>_HasDropDown Unit Test</h1>
285 </body>
286 </html>