| 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>dijit/focus automated test</title> |
| 6 | |
| 7 | <script src="boilerplate.js"></script> |
| 8 | |
| 9 | <script type="text/javascript"> |
| 10 | require([ |
| 11 | "doh/runner", "dojo/_base/array", "dojo/dom", "dojo/on", "dojo/parser", "dojo/query", |
| 12 | "dijit/focus", "dijit/registry", "dijit/tests/helpers", |
| 13 | "dojo/domReady!", |
| 14 | "dijit/form/DateTextBox", "dijit/form/ComboBox", "dijit/form/NumberSpinner","dijit/form/ComboButton", |
| 15 | "dijit/Menu", "dijit/MenuItem", "dijit/PopupMenuItem", "dijit/layout/ContentPane", "dijit/Editor" |
| 16 | ], function(doh, array, dom, on, parser, query, focus, registry, helpers){ |
| 17 | |
| 18 | doh.register("setup", [ |
| 19 | function parse(){ |
| 20 | parser.parse(); |
| 21 | return helpers.waitForLoad(); |
| 22 | }, |
| 23 | function listeners(){ |
| 24 | array.forEach(["form", "fieldset1", "fieldset2", "select", "editor", "spinner"], function(id){ |
| 25 | var w = registry.byId(id); |
| 26 | |
| 27 | // old API is to connect to onFocus/onBlur (remove in 2.0) |
| 28 | w.on("focus", function(){ |
| 29 | focusEvents[id] = true; |
| 30 | }); |
| 31 | w.on("blur", function(){ |
| 32 | blurEvents[id] = true; |
| 33 | }); |
| 34 | |
| 35 | // new API is to watch "focused" attribute |
| 36 | w.watch("focused", function(name, oldValue, newValue){ |
| 37 | // keep track of current state |
| 38 | focusedState[name] = newValue; |
| 39 | |
| 40 | // keep log of every watch notification |
| 41 | var nvs = newValue ? "focused" : "blurred"; |
| 42 | focusedWatchLog[w.id] = !focusedWatchLog[w.id] ? nvs : focusedWatchLog[w.id] + ", " + nvs; |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | // And there's also a curNode attribute that you can watch |
| 47 | focus.watch("curNode", function(name, oldValue, newValue){ |
| 48 | curNodeWatchLog.push(newValue ? newValue.id || newValue.tagName : "null") |
| 49 | }); |
| 50 | } |
| 51 | ]); |
| 52 | |
| 53 | function resetEvents(){ |
| 54 | // These are objects used to track calls to _onFocus and _onBlur in various widgets |
| 55 | focusEvents = {}; |
| 56 | blurEvents = {}; |
| 57 | focusedState = {}; |
| 58 | focusedWatchLog = {}; |
| 59 | |
| 60 | // This tracks watch("curNode") |
| 61 | curNodeWatchLog = []; |
| 62 | } |
| 63 | resetEvents(); |
| 64 | |
| 65 | function click(node){ |
| 66 | on.emit(node, "mousedown", { |
| 67 | bubbles: true, |
| 68 | cancelable: true, |
| 69 | which: 1 |
| 70 | }); |
| 71 | on.emit(node, "mouseup", { |
| 72 | bubbles: true, |
| 73 | cancelable: true, |
| 74 | which: 1 |
| 75 | }); |
| 76 | on.emit(node, "click", { |
| 77 | bubbles: true, |
| 78 | cancelable: true, |
| 79 | which: 1 |
| 80 | }); |
| 81 | } |
| 82 | doh.register("basic", [ |
| 83 | { |
| 84 | name: "focus simple input", |
| 85 | timeout: 4000, |
| 86 | setUp: function(){ |
| 87 | resetEvents(); |
| 88 | }, |
| 89 | runTest: function(){ |
| 90 | var d = new doh.Deferred(); |
| 91 | |
| 92 | // Focus the simple input |
| 93 | dom.byId("first").focus(); |
| 94 | |
| 95 | setTimeout(d.getTestCallback(function(){ |
| 96 | // Make sure that focus manager caught the focus event |
| 97 | doh.is(dom.byId("first"), focus.curNode); |
| 98 | doh.is("first", curNodeWatchLog.join(", "), "curNodeWatchLog"); |
| 99 | |
| 100 | // And that the dijit.form.Form widget is marked as |
| 101 | // being "in focus" |
| 102 | doh.t(focusEvents["form"], "form focused"); |
| 103 | |
| 104 | // And that it got one watch event |
| 105 | doh.is("focused", focusedWatchLog["form"], "watch callback"); |
| 106 | }), 500); |
| 107 | |
| 108 | return d; |
| 109 | } |
| 110 | }, |
| 111 | { |
| 112 | name: "focus another simple input", |
| 113 | timeout: 4000, |
| 114 | setUp: function(){ |
| 115 | resetEvents(); |
| 116 | }, |
| 117 | runTest: function(){ |
| 118 | var d = new doh.Deferred(); |
| 119 | |
| 120 | // Focus the simple input |
| 121 | dom.byId("second").focus(); |
| 122 | |
| 123 | setTimeout(d.getTestCallback(function(){ |
| 124 | // Make sure that focus manager caught the focus event |
| 125 | doh.is(dom.byId("second"), focus.curNode); |
| 126 | doh.is("second", curNodeWatchLog.join(", "), "curNodeWatchLog"); |
| 127 | |
| 128 | // Since the dijit.form.Form widget didn't leave the focus chain it |
| 129 | // shouldn't have any more events (since the resetEvents() call in setUp() above) |
| 130 | doh.f("form" in focusEvents, "form no new focus event"); |
| 131 | }), 500); |
| 132 | |
| 133 | return d; |
| 134 | } |
| 135 | }, |
| 136 | { |
| 137 | name: "focus combobox", |
| 138 | timeout: 4000, |
| 139 | setUp: function(){ |
| 140 | resetEvents(); |
| 141 | }, |
| 142 | runTest: function(){ |
| 143 | var d = new doh.Deferred(); |
| 144 | |
| 145 | var watchedActiveStack, activeStackWatchNotifications = 0; |
| 146 | focus.watch("activeStack", function(attr, oldVal, newVal){ |
| 147 | watchedActiveStack = newVal; |
| 148 | activeStackWatchNotifications++; |
| 149 | }); |
| 150 | |
| 151 | // This onFocus() function from helpers.js would be useful here, except it |
| 152 | // defeats the purpose of the test by using dijit/focus. |
| 153 | on.once(registry.byId("select").focusNode, "focus", function(){ |
| 154 | setTimeout(d.getTestCallback(function(){ |
| 155 | // Focus goes to an <input> node deep inside of select.domNode, |
| 156 | // but that <input> node has the id of the widget |
| 157 | doh.is(dom.byId("select"), focus.curNode); |
| 158 | |
| 159 | // The focus stack should show the ComboBox plus all parent widgets |
| 160 | var stack = focus.activeStack; |
| 161 | doh.is("form, fieldset1, select", stack.join(", "), "active stack"); |
| 162 | |
| 163 | // Also check that we got (exactly) one watch notification about active stack |
| 164 | doh.is(1, activeStackWatchNotifications, "one watch() on activeStack"); |
| 165 | doh.is("form, fieldset1, select", watchedActiveStack.join(", "), "watched active stack"); |
| 166 | |
| 167 | // _onFocus()/_onBlur was called appropriately |
| 168 | doh.f(focusEvents["form"], "form was already focused, no duplicate event"); |
| 169 | doh.f(blurEvents["form"], "form wasn't blurred"); |
| 170 | doh.t(focusEvents["fieldset1"], "fieldset1 focused"); |
| 171 | doh.t(focusEvents["select"], "select focused"); |
| 172 | |
| 173 | doh.f(focusedWatchLog["form"], "form watch callback (no new notification)"); |
| 174 | doh.is("focused", focusedWatchLog["fieldset1"], "fieldset watch callback"); |
| 175 | doh.is("focused", focusedWatchLog["select"], "select watch callback"); |
| 176 | }), 100); |
| 177 | }); |
| 178 | registry.byId("select").focus(); |
| 179 | |
| 180 | return d; |
| 181 | } |
| 182 | }, |
| 183 | { |
| 184 | name: "focus combobox again", |
| 185 | timeout: 4000, |
| 186 | setUp: function(){ |
| 187 | resetEvents(); |
| 188 | }, |
| 189 | runTest: function(){ |
| 190 | var d = new doh.Deferred(); |
| 191 | |
| 192 | var again; |
| 193 | focus.watch("activeStack", function(){ |
| 194 | again = true; |
| 195 | }); |
| 196 | |
| 197 | registry.byId("select").focus(); |
| 198 | |
| 199 | on.emit(registry.byId("select").focusNode, "mousedown", {bubbles: true}); |
| 200 | on.emit(registry.byId("select").focusNode, "mouseup", {bubbles: true}); |
| 201 | |
| 202 | setTimeout(d.getTestCallback(function(){ |
| 203 | doh.f(again, "duplicate notification about activeStack"); |
| 204 | }), 100); |
| 205 | |
| 206 | return d; |
| 207 | } |
| 208 | }, |
| 209 | { |
| 210 | name: "focus editor", |
| 211 | timeout: 4000, |
| 212 | setUp: function(){ |
| 213 | resetEvents(); |
| 214 | }, |
| 215 | runTest: function(){ |
| 216 | var d = new doh.Deferred(); |
| 217 | |
| 218 | registry.byId("editor").focus(); |
| 219 | |
| 220 | setTimeout(d.getTestCallback(function(){ |
| 221 | // The focus stack should show the Editor plus all parent widgets |
| 222 | var stack = focus.activeStack; |
| 223 | doh.is("form, editor", stack.join(", "), "active stack"); |
| 224 | |
| 225 | // _onFocus()/_onBlur was called appropriately |
| 226 | doh.f(focusEvents["form"], "form was already focused, no duplicate event"); |
| 227 | doh.f(blurEvents["form"], "form wasn't blurred"); |
| 228 | doh.t(blurEvents["fieldset1"], "fieldset no longer focused"); |
| 229 | doh.t(focusEvents["editor"], "editor focused"); |
| 230 | |
| 231 | doh.f(focusedWatchLog["form"], "form watch callback (no new notification)"); |
| 232 | doh.is("blurred", focusedWatchLog["fieldset1"], "fieldset watch callback, no longer focused"); |
| 233 | doh.is("blurred", focusedWatchLog["select"], "select watch callback, no longer focused"); |
| 234 | doh.is("focused", focusedWatchLog["editor"], "editor watch callback"); |
| 235 | }), 500); |
| 236 | |
| 237 | return d; |
| 238 | } |
| 239 | }, |
| 240 | |
| 241 | // clicking spinner buttons should activate the spinner, even |
| 242 | // though there's no actual DOM focus event |
| 243 | { |
| 244 | name: "spinner", |
| 245 | timeout: 4000, |
| 246 | runTest: function(){ |
| 247 | var d = new doh.Deferred(); |
| 248 | |
| 249 | var upArrow = query(".dijitSpinner .dijitUpArrowButton")[0]; |
| 250 | doh.t(upArrow, "found the up arrow"); |
| 251 | |
| 252 | click(upArrow); |
| 253 | |
| 254 | setTimeout(d.getTestCallback(function(){ |
| 255 | // The focus stack should show the Spinner plus all parent widgets |
| 256 | var stack = focus.activeStack; |
| 257 | doh.is("form, fieldset2, spinner", stack.join(", "), "active stack"); |
| 258 | |
| 259 | // check watch callbacks |
| 260 | doh.f(focusedWatchLog["form"], "grandparent of spinner stayed focused, so no new watch event (watch)"); |
| 261 | doh.is("focused", focusedWatchLog["fieldset2"], "parent of spinner (watch)"); |
| 262 | doh.is("focused", focusedWatchLog["spinner"], "spinner (watch)"); |
| 263 | }), 500); |
| 264 | |
| 265 | return d; |
| 266 | } |
| 267 | }, |
| 268 | |
| 269 | // Clicking a tabIndex=-1 node should set focus.curNode even though it's not tab navigable |
| 270 | { |
| 271 | name: "tabIndex=-1", |
| 272 | timeout: 4000, |
| 273 | setUp: function(){ |
| 274 | resetEvents(); |
| 275 | }, |
| 276 | runTest: function(){ |
| 277 | var d = new doh.Deferred(); |
| 278 | |
| 279 | var minusOne = query("#minusOne")[0]; |
| 280 | doh.t(minusOne, "found tabIndex=-1 button"); |
| 281 | |
| 282 | minusOne.focus(); |
| 283 | |
| 284 | setTimeout(d.getTestCallback(function(){ |
| 285 | doh.is("minusOne", focus.curNode.id); |
| 286 | }), 500); |
| 287 | |
| 288 | return d; |
| 289 | } |
| 290 | }, |
| 291 | |
| 292 | // Clicking a disabled button should not set focus.curNode |
| 293 | { |
| 294 | name: "disabled button", |
| 295 | timeout: 4000, |
| 296 | setUp: function(){ |
| 297 | resetEvents(); |
| 298 | }, |
| 299 | runTest: function(){ |
| 300 | var d = new doh.Deferred(); |
| 301 | |
| 302 | var disabledButton = query("#disabledButton")[0]; |
| 303 | doh.t(disabledButton, "found disabled button"); |
| 304 | |
| 305 | try{ |
| 306 | disabledButton.focus(); |
| 307 | }catch(e){ |
| 308 | /* IE throws exception trying to focus disabled button */ |
| 309 | } |
| 310 | |
| 311 | setTimeout(d.getTestCallback(function(){ |
| 312 | doh.is("minusOne", focus.curNode.id, "focus.curNode still old value"); |
| 313 | }), 500); |
| 314 | |
| 315 | return d; |
| 316 | } |
| 317 | } |
| 318 | /*, |
| 319 | // FIXME: this test is invalid because focus is not designed to change on mouse click to the first item in the menu |
| 320 | { |
| 321 | name: "combo button menu", |
| 322 | timeout: 4000, |
| 323 | runTest: function(){ |
| 324 | var d = new doh.Deferred(); |
| 325 | |
| 326 | var button = registry.byId('button').focusNode; |
| 327 | doh.t(button, "found drop down button"); |
| 328 | |
| 329 | doh.robot.mouseMoveAt(button); |
| 330 | doh.robot.mouseClick({left: true}, 500); |
| 331 | |
| 332 | setTimeout(d.getTestCallback(function(){ |
| 333 | // Focus goes to an first item in the drop down menu |
| 334 | doh.is(dom.byId("mi1").id, focus.curNode.id); |
| 335 | |
| 336 | // The focus stack should show the ComboBox plus all parent widgets |
| 337 | var stack = focus.activeStack; |
| 338 | console.log("menu stack is ", stack); |
| 339 | doh.is("form, fieldset2, button, menu, mil", stack.join(", "), "active stack"); |
| 340 | }), 500); |
| 341 | |
| 342 | return d; |
| 343 | }, |
| 344 | tearDown: function(){ |
| 345 | dojo.disconnect(handle); |
| 346 | } |
| 347 | }*/ |
| 348 | |
| 349 | /* |
| 350 | // Commented out because |
| 351 | // in order to allow dijit.popup's getTopPopup() to work,a sub menu's popupParent |
| 352 | // points to the parent Menu, bypassing the parent MenuItem... thus the |
| 353 | // MenuItem is not in the chain of active widgets |
| 354 | { |
| 355 | name: "nested menu", |
| 356 | timeout: 4000, |
| 357 | runTest: function(){ |
| 358 | var d = new doh.Deferred(); |
| 359 | |
| 360 | doh.robot.mouseMoveAt("popupMenuItem"); |
| 361 | |
| 362 | setTimeout(d.getTestCallback(function(){ |
| 363 | // Focus goes to an first item in the sub menu |
| 364 | doh.is(dom.byId("smi1"), focus.curNode); |
| 365 | |
| 366 | // The focus stack should show the two submenus and then upwards |
| 367 | // to the ComboButton, and the rest |
| 368 | var stack = focus.activeStack; |
| 369 | console.log("menu stack is ", stack); |
| 370 | doh.is("form, fieldset2, button, menu, mil, submenu, smil", stack.join(", "), "active stack"); |
| 371 | }), 1000); |
| 372 | |
| 373 | return d; |
| 374 | } |
| 375 | } |
| 376 | */ |
| 377 | ]); |
| 378 | |
| 379 | doh.run(); |
| 380 | }); |
| 381 | </script> |
| 382 | </head> |
| 383 | <body style="background-color: #fff; color: black; padding: 0; margin: 0"> |
| 384 | |
| 385 | <h3>Dijit/focus Automated Test</h3> |
| 386 | |
| 387 | <label>a form ContentPane widget:</label><br> |
| 388 | <form id="form" data-dojo-type="dijit/layout/ContentPane" > |
| 389 | <label for="first">simple input: </label><input id="first"/><br> |
| 390 | <label for="second">another simple input: </label><input id="second"/><br> |
| 391 | |
| 392 | <label>a fieldset ContentPane widget:</label><br> |
| 393 | <fieldset id=fieldset1 data-dojo-type="dijit/layout/ContentPane"> |
| 394 | <label for="select">a ComboBox widget:</label> |
| 395 | <select id=select data-dojo-type="dijit/form/ComboBox"> |
| 396 | <option>this</option> |
| 397 | <option>is</option> |
| 398 | <option>a</option> |
| 399 | <option>list</option> |
| 400 | </select> |
| 401 | <label for="plain">a plain input:</label> |
| 402 | <input id=plain value=plain/> |
| 403 | </fieldset> |
| 404 | <div id=editor data-dojo-type="dijit/Editor" > |
| 405 | Hello world, this is an <i>editor</i> |
| 406 | </div> |
| 407 | <br> |
| 408 | <label>another fieldset ContentPane:</label><br> |
| 409 | <fieldset id=fieldset2 data-dojo-type="dijit/layout/ContentPane"> |
| 410 | <label for="date">a DateTextBox widget:</label> |
| 411 | <input id=date data-dojo-type="dijit/form/DateTextBox"/><br> |
| 412 | |
| 413 | <label for="textarea">a plain textarea:</label><br> |
| 414 | <textarea id=textarea>hello there!</textarea><br> |
| 415 | |
| 416 | <label for="spinner">a Spinner widget:</label> |
| 417 | <input id=spinner data-dojo-type="dijit/form/NumberSpinner" data-dojo-props='value:100'/><br> |
| 418 | |
| 419 | <label>a Combobutton widget:</label> |
| 420 | <div id=button data-dojo-type="dijit/form/ComboButton" data-dojo-props='tabIndex:"0"'> |
| 421 | <span>push me</span> |
| 422 | <div id=menu data-dojo-type="dijit/Menu"> |
| 423 | <div id=mi1 data-dojo-type="dijit/MenuItem">menu item 1</div> |
| 424 | <div id=mi2 data-dojo-type="dijit/MenuItem">menu item 2</div> |
| 425 | <div id=popupMenuItem data-dojo-type="dijit/PopupMenuItem"> |
| 426 | <span>submenu</span> |
| 427 | <div id=submenu data-dojo-type="dijit/Menu"> |
| 428 | <div id=smi1 data-dojo-type="dijit/MenuItem">submenu item 1</div> |
| 429 | <div id=smi2 data-dojo-type="dijit/MenuItem">submenu item 2</div> |
| 430 | </div> |
| 431 | </div> |
| 432 | </div> |
| 433 | </div> |
| 434 | </fieldset> |
| 435 | <button tabIndex="-1" id="minusOne" type="button">non tab-navigable button</button> |
| 436 | <button id="disabledButton" type="button" disabled>disabled button</button> |
| 437 | </form> |
| 438 | </body> |
| 439 | </html> |