Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W3c test keyboard events #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions tests/Js/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,61 @@ public function testMouseOver()
/**
* @dataProvider provideKeyboardEventsModifiers
*/
public function testKeyboardEvents($modifier, $eventProperties)
public function testKeyboardEvents($method, $char, $charCode, $modifier, $eventProperties)
{
$this->getSession()->visit($this->pathTo('/js_test.html'));
$webAssert = $this->getAssertSession();

$input1 = $webAssert->elementExists('css', '.elements input.input.first');
$input2 = $webAssert->elementExists('css', '.elements input.input.second');
$input3 = $webAssert->elementExists('css', '.elements input.input.third');
$input = $webAssert->elementExists('css', '.elements input.input');
$event = $webAssert->elementExists('css', '.elements .text-event');

$input1->keyDown('u', $modifier);
$this->assertEquals('key downed:'.$eventProperties, $event->getText());

$input2->keyPress('r', $modifier);
$this->assertEquals('key pressed:114 / '.$eventProperties, $event->getText());

$input3->keyUp(78, $modifier);
$this->assertEquals('key upped:78 / '.$eventProperties, $event->getText());
$input->$method($char, $modifier);
$this->assertContains('event=' . strtolower($method) . ';keyCode=' . $charCode . ';modifier='.$eventProperties, $event->getText());
}

public function provideKeyboardEventsModifiers()
{
return array(
'none' => array(null, '0 / 0 / 0 / 0'),
'alt' => array('alt', '1 / 0 / 0 / 0'),
// jQuery considers ctrl as being a metaKey in the normalized event
'ctrl' => array('ctrl', '0 / 1 / 0 / 1'),
'shift' => array('shift', '0 / 0 / 1 / 0'),
'meta' => array('meta', '0 / 0 / 0 / 1'),
$data = array(
'none-keyDown' => array('keyDown', 'u', 85, null, '0 / 0 / 0 / 0'),
'none-keyPress' => array('keyPress', 'b', 98, null, '0 / 0 / 0 / 0'),
/**
* @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.
*/
'none-keyUp' => array('keyUp', 110, 78, null, '0 / 0 / 0 / 0'), // 110 = n 78 = N

// see explanation from above why sending 110 but expecting 78
'alt-keyUp' => array('keyUp', 110, 78, 'alt', '1 / 0 / 0 / 0'),

// jQuery considers ctrl as being a metaKey in the normalized event
'ctrl-keyDown' => array('keyDown', 'u', 85, 'ctrl', '0 / 1 / 0 / 1'),
// see explanation from above why sending 110 but expecting 78
'ctrl-keyUp' => array('keyUp', 110, 78, 'ctrl', '0 / 1 / 0 / 1'),

'shift-keyDown' => array('keyDown', 'u', 85, 'shift', '0 / 0 / 1 / 0'), // 85 = U
'shift-keyPress' => array('keyPress', 'b', 66, 'shift', '0 / 0 / 1 / 0'), // 66 = B
// see explanation from above why sending 110 but expecting 78
'shift-keyUp' => array('keyUp', 110, 78, 'shift', '0 / 0 / 1 / 0'),

'meta-keyDown' => array('keyDown', 'u', 85, 'meta', '0 / 0 / 0 / 1'),
'meta-keyPress' => array('keyPress', 'b', 98, 'meta', '0 / 0 / 0 / 1'),
// see explanation from above why sending 110 but expecting 78
'meta-keyUp' => array('keyUp', 110, 78, 'meta', '0 / 0 / 0 / 1'),
);

// https://bugs.chromium.org/p/chromium/issues/detail?id=13891#c16
// Control + <char> will not trigger keypress
// Option + <char> will output different results "special char" ©
if (PHP_OS !== 'Darwin') {
$data['alt-keyDown'] = array('keyDown', 'u', 85, 'alt', '1 / 0 / 0 / 0');
$data['alt-keyPress'] = array('keyPress', 'b', 98, 'alt', '1 / 0 / 0 / 0');
$data['ctrl-keyPress'] = array('keyPress', 'b', 98, 'ctrl', '0 / 1 / 0 / 1'); // do not use "r" because it will trigger page reload in firefox
}

return $data;
}
}
23 changes: 10 additions & 13 deletions web-fixtures/js_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
<div id="mouseover-detector">no mouse action detected</div>
<div id="invisible" style="display: none">invisible man</div>
<input id="focus-blur-detector" type="text" value="no action detected"/>
<input class="input first" type="text" value="" />
<input class="input second" type="text" value="" />
<input class="input third" type="text" value="" />
<input class="input" type="text" value="" />
<div class="text-event"></div>
</div>

Expand Down Expand Up @@ -72,16 +70,15 @@
$(this).text('mouse overed');
});

$('.elements input.input.first').keydown(function(ev) {
$('.text-event').text('key downed:' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
});

$('.elements input.input.second').keypress(function(ev) {
$('.text-event').text('key pressed:' + ev.which + ' / ' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
});

$('.elements input.input.third').keyup(function(ev) {
$('.text-event').text('key upped:' + ev.which + ' / ' + ev.altKey * 1 + ' / ' + ev.ctrlKey * 1 + ' / ' + ev.shiftKey * 1 + ' / ' + ev.metaKey * 1);
$('.elements input.input').bind('keydown keypress keyup', function(ev) {
console.log('event=%s keyCode=%s altKey=%s ctrlKey=%s shiftKey=%s metaKey=%s', ev.type, ev.keyCode, ev.altKey, ev.ctrlKey, ev.shiftKey, ev.metaKey);
$('.text-event').append([
'event=' + ev.type,
// chrome and firefox are returning different values on keypress
'keyCode=' + (ev.keyCode || ev.charCode),
'modifier=' + ((ev.altKey * 1) + ' / ' + (ev.ctrlKey * 1) + ' / ' + (ev.shiftKey * 1) + ' / ' + (ev.metaKey * 1)),
null
].join(';') + "<br/>\n");
});

$( "#draggable" ).draggable();
Expand Down