Skip to content

Commit

Permalink
added testKeyboardEventsPressKey
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-andreyev committed Sep 2, 2019
1 parent 79510f3 commit 6130d81
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/Js/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
))
);
}
}
10 changes: 10 additions & 0 deletions web-fixtures/js_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<input class="input first" type="text" value="" />
<input class="input second" type="text" value="" />
<input class="input third" type="text" value="" />
<input class="input fourth" type="text" value=""/>
<div class="text-event"></div>
</div>

Expand Down Expand Up @@ -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 ) {
Expand Down

0 comments on commit 6130d81

Please sign in to comment.