diff --git a/tests/Js/EventsTest.php b/tests/Js/EventsTest.php index 34d3dda..fb71e20 100644 --- a/tests/Js/EventsTest.php +++ b/tests/Js/EventsTest.php @@ -120,4 +120,68 @@ public function provideKeyboardEventsModifiers() 'meta' => array('meta', '0 / 0 / 0 / 1'), ); } + + /** + * @dataProvider provideKeyboardEventsPressKeyModifiers + */ + public function testKeyboardEventsPressKey($modifier, $char, array $expected) + { + $this->getSession()->visit($this->pathTo('/js_test.html')); + $webAssert = $this->getAssertSession(); + + $input = $webAssert->elementExists('css', '.elements input.input.fourth'); + $event = $webAssert->elementExists('css', '.elements .text-event'); + + $input->pressKey($char, $modifier); + $this->assertEquals($expected, array_map('trim', array_filter(explode(';', $event->getText())))); + } + + public function provideKeyboardEventsPressKeyModifiers() + { + /** + * @see http://api.jquery.com/keypress/ + * + * Note that keydown and keyup provide a code indicating which key is pressed, + * while keypress indicates which character was entered. + * For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. + * An uppercase "A" is reported as 65 by all events. + */ + return array( + 'none' => array(null, 'u', array( // u = 117 U = 85 + 'event=keydown keyCode=85 modifier=0 / 0 / 0 / 0', + 'event=keypress keyCode=117 modifier=0 / 0 / 0 / 0', + 'event=keyup keyCode=85 modifier=0 / 0 / 0 / 0' + )), + 'alt' => array('alt', 'a', array( // a = 97 A = 65 + 'event=keydown keyCode=18 modifier=1 / 0 / 0 / 0', + 'event=keydown keyCode=65 modifier=1 / 0 / 0 / 0', + 'event=keypress keyCode=97 modifier=1 / 0 / 0 / 0', + 'event=keyup keyCode=65 modifier=1 / 0 / 0 / 0', + 'event=keyup keyCode=18 modifier=0 / 0 / 0 / 0' + )), + // do not use ctrl+r it will force browser to reload (firefox) + // jQuery considers ctrl as being a metaKey in the normalized event + 'ctrl' => array('ctrl', 'b', array( // b = 98 B = 66 + 'event=keydown keyCode=17 modifier=0 / 1 / 0 / 1', + 'event=keydown keyCode=66 modifier=0 / 1 / 0 / 1', + 'event=keypress keyCode=98 modifier=0 / 1 / 0 / 1', + 'event=keyup keyCode=66 modifier=0 / 1 / 0 / 1', + 'event=keyup keyCode=17 modifier=0 / 0 / 0 / 0' + )), + 'shift' => array('shift', 'c', array( // c = 99 C = 67 + 'event=keydown keyCode=16 modifier=0 / 0 / 1 / 0', + 'event=keydown keyCode=67 modifier=0 / 0 / 1 / 0', + 'event=keypress keyCode=67 modifier=0 / 0 / 1 / 0', + 'event=keyup keyCode=67 modifier=0 / 0 / 1 / 0', + 'event=keyup keyCode=16 modifier=0 / 0 / 0 / 0' + )), + 'meta' => array('meta', 'd', array( // d = 100 D = 68 + 'event=keydown keyCode=224 modifier=0 / 0 / 0 / 1', + 'event=keydown keyCode=68 modifier=0 / 0 / 0 / 1', + 'event=keypress keyCode=100 modifier=0 / 0 / 0 / 1', + 'event=keyup keyCode=68 modifier=0 / 0 / 0 / 1', + 'event=keyup keyCode=224 modifier=0 / 0 / 0 / 0' + )) + ); + } } diff --git a/web-fixtures/js_test.html b/web-fixtures/js_test.html index 6285455..d14b79a 100644 --- a/web-fixtures/js_test.html +++ b/web-fixtures/js_test.html @@ -29,6 +29,7 @@ +
@@ -84,6 +85,15 @@ $('.text-event').text('key upped:' + ev.which + ' / ' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1); }); + $('.elements input.input.fourth').bind('keydown keypress keyup', function(ev) { + $('.text-event').append([ + 'event=' + ev.type, + // chrome and firefox are returning different values on keypress + 'keyCode=' + (ev.keyCode || ev.charCode || ev.key), + 'modifier=' + ((ev.altKey * 1) + ' / ' + (ev.ctrlKey * 1) + ' / ' + (ev.shiftKey * 1) + ' / ' + (ev.metaKey * 1)) + ].join(' ') + ';'); + }); + $( "#draggable" ).draggable(); $( "#droppable" ).droppable({ drop: function( event, ui ) {