| 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>robot TextBox onInput and _processInput Test</title> |
| 6 |
|
| 7 |
<style> |
| 8 |
@import "../../../../util/doh/robot/robot.css"; |
| 9 |
</style> |
| 10 |
|
| 11 |
|
| 12 |
<script type="text/javascript" src="../../../../dojo/dojo.js"></script> |
| 13 |
|
| 14 |
<script type="text/javascript"> |
| 15 |
require([ |
| 16 |
"doh/runner", "dojo/robotx", |
| 17 |
"dojo/aspect", "dojo/json", "dojo/keys", "dojo/on", |
| 18 |
"dojo/domReady!" |
| 19 |
], function(doh, robot, aspect, json, keys, on){ |
| 20 |
robot.initRobot('../test_validate.html'); |
| 21 |
|
| 22 |
var widget, registry; |
| 23 |
|
| 24 |
function nop(){} |
| 25 |
|
| 26 |
doh.register("onInput", [ |
| 27 |
function setup(){ |
| 28 |
// get pointers to singletons loaded on test page |
| 29 |
registry = robot.window.require("dijit/registry"); |
| 30 |
widget = registry.byId('q01'); |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "focus", |
| 34 |
timeout: 2000, |
| 35 |
runTest: function(){ |
| 36 |
var d = new doh.Deferred(); |
| 37 |
var handler = on.once(widget.focusNode, "focus", function(){ |
| 38 |
setTimeout(function(){ |
| 39 |
d.resolve(); |
| 40 |
}, 0); |
| 41 |
}); |
| 42 |
setTimeout(function(){ |
| 43 |
widget.focus(); |
| 44 |
}, 1); // return d before calling focus |
| 45 |
return d; |
| 46 |
} |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "space", |
| 50 |
timeout: 2000, |
| 51 |
runTest: function(){ |
| 52 |
var d = new doh.Deferred(); |
| 53 |
|
| 54 |
widget.onInput = d.getTestErrback(function(e){ |
| 55 |
var expEvt = expected.shift() || {type: null, charOrCode: null}; |
| 56 |
if(expEvt.type != e.type || expEvt.charOrCode !== e.charOrCode || expEvt.ctrlKey != e.ctrlKey || expEvt.shiftKey != e.shiftKey || expEvt.altKey != e.altKey){ |
| 57 |
widget.onInput = nop; |
| 58 |
d.getTestCallback(function(){ |
| 59 |
doh.is(expEvt.type, e.type); |
| 60 |
doh.is(expEvt.charOrCode, e.charOrCode); |
| 61 |
doh.is(expEvt.ctrlKey, e.ctrlKey); |
| 62 |
doh.is(expEvt.shiftKey, e.shiftKey); |
| 63 |
doh.is(expEvt.altKey, e.altKey); |
| 64 |
})(); // fail asap |
| 65 |
}else if(expected.length == 0){ |
| 66 |
setTimeout(d.getTestCallback(function(){ |
| 67 |
widget.onInput = nop; |
| 68 |
}), 100); // wait to see if an extra onInput event arrives |
| 69 |
} |
| 70 |
}); |
| 71 |
var expected = [ |
| 72 |
{ type: "keypress", charOrCode: ' ', ctrlKey: false, shiftKey: false, altKey: false } |
| 73 |
]; |
| 74 |
robot.keyPress(keys.SPACE, 100, {}); |
| 75 |
|
| 76 |
return d; |
| 77 |
} |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "test all", |
| 81 |
timeout: 9000, |
| 82 |
runTest: function(){ |
| 83 |
var d = new doh.Deferred(); |
| 84 |
|
| 85 |
widget.onInput = d.getTestErrback(function(e){ |
| 86 |
var expEvt = expected.shift() || { type: null, charOrCode: null, ctrlKey: false, shiftKey: false, altKey: false }; |
| 87 |
if(expEvt.charOrCode == ' '/*the end sentinel*/ || (expEvt.type != e.type || expEvt.charOrCode !== e.charOrCode || expEvt.ctrlKey != e.ctrlKey || (expEvt.shiftKey != null && expEvt.shiftKey != e.shiftKey) || expEvt.altKey != e.altKey)){ |
| 88 |
widget.onInput = nop; |
| 89 |
d.getTestCallback(function(){ |
| 90 |
var hint = "Expected: " + |
| 91 |
json.stringify({ type: expEvt.type, charOrCode: expEvt.charOrCode, ctrlKey: expEvt.ctrlKey, shiftKey: expEvt.shiftKey, altKey: expEvt.altKey }) + |
| 92 |
", Actual " + |
| 93 |
json.stringify({ type: e.type, charOrCode: e.charOrCode, ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, altKey: e.altKey }); |
| 94 |
doh.is(expEvt.type, e.type, hint); |
| 95 |
doh.is(expEvt.charOrCode, e.charOrCode, hint); |
| 96 |
doh.is(expEvt.ctrlKey, e.ctrlKey, hint); |
| 97 |
doh.is(expEvt.altKey, e.altKey, hint); |
| 98 |
doh.t(expEvt.shiftKey == null || expEvt.shiftKey == e.shiftKey, hint); |
| 99 |
})(); |
| 100 |
} |
| 101 |
}); |
| 102 |
var expected = [ |
| 103 |
{ type: "keypress", charOrCode: 'a', ctrlKey: false, shiftKey: false, altKey: false }, |
| 104 |
{ type: "keypress", charOrCode: 'Z', ctrlKey: false, shiftKey: true, altKey: false }, |
| 105 |
{ type: "keypress", charOrCode: '0', ctrlKey: false, shiftKey: null, altKey: false }, |
| 106 |
{ type: "keypress", charOrCode: '_', ctrlKey: false, shiftKey: null, altKey: false }, |
| 107 |
{ type: "keypress", charOrCode: '-', ctrlKey: false, shiftKey: null, altKey: false }, |
| 108 |
{ type: "keypress", charOrCode: '+', ctrlKey: false, shiftKey: null, altKey: false }, |
| 109 |
{ type: "keypress", charOrCode: '=', ctrlKey: false, shiftKey: null, altKey: false }, |
| 110 |
{ type: "keypress", charOrCode: '/', ctrlKey: false, shiftKey: null, altKey: false }, |
| 111 |
{ type: "keypress", charOrCode: '*', ctrlKey: false, shiftKey: null, altKey: false }, |
| 112 |
{ type: "keypress", charOrCode: '.', ctrlKey: false, shiftKey: null, altKey: false }, |
| 113 |
{ type: "keypress", charOrCode: 'Q', ctrlKey: false, shiftKey: true, altKey: false }, |
| 114 |
{ type: "keydown", charOrCode: 'C', ctrlKey: true, shiftKey: false, altKey: false }, |
| 115 |
{ type: "keydown", charOrCode: keys.RIGHT_ARROW, ctrlKey: false, shiftKey: false, altKey: false }, |
| 116 |
{ type: "keydown", charOrCode: keys.ENTER, ctrlKey: true, shiftKey: false, altKey: true }, |
| 117 |
{ type: "keydown", charOrCode: keys.TAB, ctrlKey: false, shiftKey: false, altKey: false }, |
| 118 |
{ type: "keypress", charOrCode: ' ', ctrlKey: false, shiftKey: false, altKey: false } |
| 119 |
]; |
| 120 |
robot.typeKeys('aZ0_-+=/*.', 100, 2000); |
| 121 |
robot.keyPress('q', 200, { shift: true }); |
| 122 |
robot.keyPress('c', 200, { ctrl: true }); |
| 123 |
robot.keyPress(keys.RIGHT_ARROW, 200, {}); |
| 124 |
robot.keyPress(keys.ENTER, 200, { ctrl: true, shift: false, alt: true }); |
| 125 |
robot.keyPress(keys.TAB, 200, {}); |
| 126 |
robot.keyPress(keys.TAB, 500, { shift: true }); |
| 127 |
robot.keyPress(keys.SPACE, 500, {}); |
| 128 |
|
| 129 |
return d; |
| 130 |
} |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "advanced", |
| 134 |
timeout: 2000, |
| 135 |
runTest: function(){ |
| 136 |
var d = new doh.Deferred(); |
| 137 |
|
| 138 |
// note: using robot.sequence() to give time for final SPACE key in above test to process |
| 139 |
robot.sequence(d.getTestErrback(function(){ |
| 140 |
widget.set("value", ""); |
| 141 |
widget.focus(); |
| 142 |
|
| 143 |
// Test that when onInput is overridden and returns false, the character is canceled. |
| 144 |
// "a" should be ignored and only "b" will appear in the <input> and to |
| 145 |
// trigger the call to _processInput() |
| 146 |
widget.onInput = function(evt1){ |
| 147 |
if(evt1.charOrCode == "a"){ |
| 148 |
return false; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
// Test that _processInput(evt) is called with the event object after the character has |
| 153 |
// appeared in the <input> |
| 154 |
var handle = aspect.after(widget, "_processInput", d.getTestCallback(function(res, args){ |
| 155 |
var evt2 = args[0]; |
| 156 |
handle.remove(); |
| 157 |
doh.is("b", evt2.charOrCode, "charOrCode"); |
| 158 |
doh.is("b", widget.focusNode.value, "widget.focusNode.value"); |
| 159 |
})); |
| 160 |
|
| 161 |
}), 100); |
| 162 |
|
| 163 |
robot.keyPress("a", 100, {}); |
| 164 |
robot.sequence(d.getTestErrback(function(){ |
| 165 |
doh.is("", widget.focusNode.value, "widget.focusNode.value"); |
| 166 |
}), 100); |
| 167 |
|
| 168 |
robot.keyPress("b", 100, {}); |
| 169 |
|
| 170 |
return d; |
| 171 |
} |
| 172 |
} |
| 173 |
]); |
| 174 |
doh.run(); |
| 175 |
}); |
| 176 |
</script> |
| 177 |
</head> |
| 178 |
</html> |