| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | |
| 6 | <title>Test Dijit Internal Event: "ondijitclick"</title> |
| 7 | |
| 8 | <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug: true, async: true"></script> |
| 9 | <script type="text/javascript"> |
| 10 | require([ |
| 11 | "doh/runner", "dojo/robotx", |
| 12 | "dojo/_base/array", "dojo/aspect", "dojo/dom", "dojo/keys", "dojo/on", "dojo/domReady!" |
| 13 | ], function(doh, robot, array, aspect, dom, keys, on){ |
| 14 | |
| 15 | robot.initRobot('../_Widget-ondijitclick.html'); |
| 16 | |
| 17 | // Event monitoring |
| 18 | var widgetClicks = 0, buttonClicks = 0, button2Clicks = 0, thirdClicks = 0; |
| 19 | var w; |
| 20 | |
| 21 | doh.register(function setup(){ |
| 22 | // get pointer to registry in the iframe |
| 23 | registry = robot.window.require("dijit/registry"); |
| 24 | }); |
| 25 | |
| 26 | doh.register("setup", function(){ |
| 27 | robot.sequence(function(){ |
| 28 | w = registry.byId("first"); |
| 29 | aspect.after(w, "_onClick", function(){ |
| 30 | widgetClicks++; |
| 31 | }, true); |
| 32 | on(dom.byId("plainbutton", robot.doc), "click", function(){ |
| 33 | buttonClicks++; |
| 34 | }); |
| 35 | on(dom.byId("plainbutton2", robot.doc), "click", function(){ |
| 36 | button2Clicks++; |
| 37 | }); |
| 38 | on(dom.byId("third", robot.doc), "click", function(){ |
| 39 | thirdClicks++; |
| 40 | }); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | // Test ondijitclick on a <div> which refocuses onto a native <button> node. |
| 45 | // Make sure that the <button> doesn't get a spurious click event. |
| 46 | array.forEach(["SPACE", "ENTER"], function(key){ |
| 47 | |
| 48 | doh.register("ondijitclick by " + key, [ |
| 49 | { |
| 50 | name: "ondijitclick by " + key, |
| 51 | timeout: 5000, |
| 52 | runTest: function(){ |
| 53 | widgetClicks = buttonClicks = 0; |
| 54 | var d = new doh.Deferred(); |
| 55 | |
| 56 | // Keyboard-click the widget |
| 57 | robot.sequence(function(){ |
| 58 | w.domNode.focus(); |
| 59 | }, 500); |
| 60 | robot.keyDown(keys[key], 250, {}); |
| 61 | robot.keyUp(keys[key], 250, {}); |
| 62 | |
| 63 | // Check that ondijitclick fired but no spurious event |
| 64 | // on the widget that got focused in the ondijitclick handler |
| 65 | robot.sequence(d.getTestCallback(function(){ |
| 66 | doh.is(1, widgetClicks, "ondijitclick handler fired"); |
| 67 | doh.is(0, buttonClicks, "spurious button click event"); |
| 68 | }), 500); |
| 69 | |
| 70 | return d; |
| 71 | } |
| 72 | }, |
| 73 | { |
| 74 | name: "ondijitclick by " + key + " w/modifier", |
| 75 | timeout: 5000, |
| 76 | runTest: function(){ |
| 77 | widgetClicks = buttonClicks = 0; |
| 78 | var d = new doh.Deferred(); |
| 79 | |
| 80 | robot.sequence(function(){ |
| 81 | w.domNode.focus(); |
| 82 | }, 500); |
| 83 | |
| 84 | // Try shift-space/shift-enter. |
| 85 | // Don't try ctrl-space or meta-space since those keystrokes have special functions |
| 86 | // (like context menu) in various browsers. |
| 87 | |
| 88 | var shift; |
| 89 | aspect.after(w, "onClick", function(evt){ |
| 90 | shift = evt.shiftKey; |
| 91 | }, true); |
| 92 | |
| 93 | robot.keyPress(keys[key], 100, {shift: true}); |
| 94 | |
| 95 | robot.sequence(d.getTestCallback(function(){ |
| 96 | doh.is(1, widgetClicks, "ondijitclick handler fired"); |
| 97 | doh.t(shift, "shift flag was set"); |
| 98 | doh.is(0, buttonClicks, "button click handler wasn't fired"); |
| 99 | }), 500); |
| 100 | |
| 101 | return d; |
| 102 | } |
| 103 | } |
| 104 | ]); |
| 105 | }); |
| 106 | |
| 107 | // Test ondijitclick on a <div> which refocuses onto a native <textarea> node. |
| 108 | // Tests that the keyboard event gets suppressed so it doesn't modify the focused node. |
| 109 | array.forEach(["SPACE","ENTER"], function(key){ |
| 110 | |
| 111 | doh.register("focus to textarea by " + key, [ |
| 112 | { |
| 113 | name: "ondijitclick by " + key, |
| 114 | timeout: 5000, |
| 115 | runTest: function(){ |
| 116 | var d = new doh.Deferred(), |
| 117 | w = registry.byId("second"); |
| 118 | |
| 119 | // Keyboard-click the widget |
| 120 | robot.sequence(function(){ |
| 121 | w.domNode.focus(); |
| 122 | }, 500); |
| 123 | robot.keyPress(keys[key], 500, {}); |
| 124 | |
| 125 | // Check that ondijitclick fired but that focused textarea didn't get the key |
| 126 | robot.sequence(d.getTestCallback(function(){ |
| 127 | doh.is("hello world", dom.byId("textarea", robot.doc).value); |
| 128 | }), 500); |
| 129 | |
| 130 | return d; |
| 131 | } |
| 132 | } |
| 133 | ]); |
| 134 | }); |
| 135 | |
| 136 | // Test onclick on a <button> which refocuses onto a <div> w/an ondijitclick handler. |
| 137 | // Make sure that the <div> doesn't get a spurious click event. |
| 138 | array.forEach(["SPACE", "ENTER"], function(key){ |
| 139 | |
| 140 | doh.register("focus to ondijitclick-div by " + key, [ |
| 141 | { |
| 142 | name: "click by " + key, |
| 143 | timeout: 5000, |
| 144 | runTest: function(){ |
| 145 | button2Clicks = thirdClicks = 0; |
| 146 | |
| 147 | var d = new doh.Deferred(), |
| 148 | button = dom.byId("plainbutton2", robot.doc); |
| 149 | |
| 150 | // Keyboard-click the native button |
| 151 | robot.sequence(function(){ |
| 152 | button.focus(); |
| 153 | }, 500); |
| 154 | robot.keyPress(keys[key], 500, {}); |
| 155 | |
| 156 | // Check that ondijitclick didn't fire |
| 157 | robot.sequence(d.getTestCallback(function(){ |
| 158 | doh.is(1, button2Clicks, "button2 was clicked (and focus moved to ondijitclick-div)"); |
| 159 | doh.is(0, thirdClicks, "ondijitclick-div didn't get a spurious click event"); |
| 160 | }), 500); |
| 161 | |
| 162 | return d; |
| 163 | } |
| 164 | } |
| 165 | ]); |
| 166 | }); |
| 167 | |
| 168 | // Test clicking on a <button> with an ondijitclick handler. |
| 169 | // The main danger is getting two click events because the browser natively handles space-->click |
| 170 | array.forEach(["SPACE", "ENTER"], function(key){ |
| 171 | |
| 172 | doh.register("click ondijitclick button by " + key, [ |
| 173 | { |
| 174 | name: "click by " + key, |
| 175 | timeout: 5000, |
| 176 | runTest: function(){ |
| 177 | |
| 178 | var d = new doh.Deferred(), |
| 179 | buttonWidget = registry.byId("widgetbutton"), |
| 180 | buttonNode = dom.byId("widgetbutton", robot.doc); |
| 181 | |
| 182 | buttonWidget.clickCount = 0; |
| 183 | |
| 184 | // Keyboard-click the widget button |
| 185 | robot.sequence(function(){ |
| 186 | buttonNode.focus(); |
| 187 | }, 500); |
| 188 | robot.keyPress(keys[key], 500, {}); |
| 189 | |
| 190 | // Check that ondijitclick fired exactly once |
| 191 | robot.sequence(d.getTestCallback(function(){ |
| 192 | doh.is(1, buttonWidget.clickCount, "one click event"); |
| 193 | }), 500); |
| 194 | |
| 195 | return d; |
| 196 | } |
| 197 | } |
| 198 | ]); |
| 199 | }); |
| 200 | |
| 201 | doh.register("bubbling", [ |
| 202 | { |
| 203 | name: "bubbling", |
| 204 | timeout: 5000, |
| 205 | runTest: function(){ |
| 206 | |
| 207 | var d = new doh.Deferred(), |
| 208 | outerDiv = dom.byId("outer", robot.doc), |
| 209 | buttonNode = dom.byId("bubbleTestWidget", robot.doc), |
| 210 | buttonWidget = registry.byId("bubbleTestWidget"); |
| 211 | |
| 212 | // Listen for click events on the outer DOMNode. |
| 213 | var clicks = 0; |
| 214 | on(outerDiv, "click", function(){ |
| 215 | clicks++; |
| 216 | }); |
| 217 | |
| 218 | // Keyboard-click the widget button |
| 219 | robot.sequence(function(){ |
| 220 | buttonNode.focus(); |
| 221 | }, 500); |
| 222 | robot.keyPress(keys.ENTER, 500, {}); |
| 223 | |
| 224 | // Check that one click event bubbled |
| 225 | robot.sequence(d.getTestCallback(function(){ |
| 226 | doh.is(1, buttonWidget.clickCount, "one click event"); |
| 227 | doh.is(1, clicks, "one click event bubbled"); |
| 228 | }), 500); |
| 229 | |
| 230 | return d; |
| 231 | } |
| 232 | } |
| 233 | ]); |
| 234 | |
| 235 | doh.run(); |
| 236 | }); |
| 237 | |
| 238 | </script> |
| 239 | </head> |
| 240 | </html> |