From 9580196ab9d06a6a81305c299a24080d387c76c1 Mon Sep 17 00:00:00 2001 From: Oleg Andreyev Date: Sun, 30 Dec 2018 23:50:28 +0200 Subject: [PATCH 1/2] making testKeyboardEvents w3c compatible --- tests/Js/EventsTest.php | 54 ++++++++++++++++++++++++++------------- web-fixtures/js_test.html | 23 ++++++++--------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/tests/Js/EventsTest.php b/tests/Js/EventsTest.php index f06999b..2afa06b 100644 --- a/tests/Js/EventsTest.php +++ b/tests/Js/EventsTest.php @@ -88,35 +88,53 @@ 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'), + '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 + + 'alt-keyDown' => array('keyDown', 'u', 85, 'alt', '1 / 0 / 0 / 0'), + 'alt-keyPress' => array('keyPress', 'b', 98, 'alt', '1 / 0 / 0 / 0'), + // 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'), + 'ctrl-keyPress' => array('keyPress', 'b', 98, 'ctrl', '0 / 1 / 0 / 1'), // do not use "r" because it will trigger page reload in firefox + // 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'), ); } } diff --git a/web-fixtures/js_test.html b/web-fixtures/js_test.html index 6285455..7bf34a1 100644 --- a/web-fixtures/js_test.html +++ b/web-fixtures/js_test.html @@ -26,9 +26,7 @@
no mouse action detected
- - - +
@@ -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(';') + "
\n"); }); $( "#draggable" ).draggable(); From b0c2b70befa40686d5d4defc5ee4ca29ecc168a7 Mon Sep 17 00:00:00 2001 From: Oleg Andreyev Date: Thu, 26 Dec 2019 20:39:09 +0200 Subject: [PATCH 2/2] updated provideKeyboardEventsModifiers for Darwin (macos) --- tests/Js/EventsTest.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/Js/EventsTest.php b/tests/Js/EventsTest.php index 2afa06b..6e41789 100644 --- a/tests/Js/EventsTest.php +++ b/tests/Js/EventsTest.php @@ -102,7 +102,7 @@ public function testKeyboardEvents($method, $char, $charCode, $modifier, $eventP public function provideKeyboardEventsModifiers() { - return array( + $data = array( 'none-keyDown' => array('keyDown', 'u', 85, null, '0 / 0 / 0 / 0'), 'none-keyPress' => array('keyPress', 'b', 98, null, '0 / 0 / 0 / 0'), /** @@ -115,14 +115,11 @@ public function provideKeyboardEventsModifiers() */ 'none-keyUp' => array('keyUp', 110, 78, null, '0 / 0 / 0 / 0'), // 110 = n 78 = N - 'alt-keyDown' => array('keyDown', 'u', 85, 'alt', '1 / 0 / 0 / 0'), - 'alt-keyPress' => array('keyPress', 'b', 98, 'alt', '1 / 0 / 0 / 0'), // 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'), - 'ctrl-keyPress' => array('keyPress', 'b', 98, 'ctrl', '0 / 1 / 0 / 1'), // do not use "r" because it will trigger page reload in firefox // see explanation from above why sending 110 but expecting 78 'ctrl-keyUp' => array('keyUp', 110, 78, 'ctrl', '0 / 1 / 0 / 1'), @@ -136,5 +133,16 @@ public function provideKeyboardEventsModifiers() // 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 + will not trigger keypress + // Option + 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; } }