From: <rb...@us...> - 2014-02-27 19:18:53
|
Revision: 9149 http://sourceforge.net/p/htmlunit/code/9149 Author: rbri Date: 2014-02-27 19:18:46 +0000 (Thu, 27 Feb 2014) Log Message: ----------- Wrong charCode reported by the onKeyPress keyboard event for the space key Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEvent.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEventTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2014-02-27 18:44:15 UTC (rev 9148) +++ trunk/htmlunit/src/changes/changes.xml 2014-02-27 19:18:46 UTC (rev 9149) @@ -8,6 +8,9 @@ <body> <release version="2.15" date="???" description="Bugfixes"> + <action type="fix" dev="rbri" issue="1578"> + JavaScript: Wrong charCode reported by the onKeyPress keyboard event for the space key. + </action> <action type="fix" dev="rbri" issue="1580"> JavaScript: Fix our implementation of innerText for table elements. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEvent.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEvent.java 2014-02-27 18:44:15 UTC (rev 9148) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEvent.java 2014-02-27 19:18:46 UTC (rev 9149) @@ -538,7 +538,7 @@ } setKeyCode(keyCode); if (getType().equals(Event.TYPE_KEY_PRESS)) { - if ((character >= 33 && character <= 126) + if ((character >= 32 && character <= 126) || !getBrowserVersion().hasFeature(JS_EVENT_DISTINGUISH_PRINTABLE_KEY)) { charCode_ = character; } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEventTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEventTest.java 2014-02-27 18:44:15 UTC (rev 9148) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/KeyboardEventTest.java 2014-02-27 19:18:46 UTC (rev 9149) @@ -134,7 +134,7 @@ */ @Test @Alerts({ "32", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57" }) - public void keyCodes() throws Exception { + public void keyCodes_keyup() throws Exception { final String html = "<html><head>" + "<script>" + "function handleKey(e) {\n" @@ -158,7 +158,7 @@ @Test @Alerts({ "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90" }) - public void keyCodes2() throws Exception { + public void keyCodes2_keyup() throws Exception { final String html = "<html><head>" + "<script>" + "function handleKey(e) {\n" @@ -179,6 +179,104 @@ /** * @throws Exception if the test fails */ + @Test + @Alerts({ "32", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57" }) + public void keyCodes_keydown() throws Exception { + final String html = "<html><head>" + + "<script>" + + "function handleKey(e) {\n" + + " alert(e.keyCode);" + + "}" + + "</script>\n" + + "</head><body>\n" + + "<input id='t' onkeydown='handleKey(event)'/>\n" + + "</body></html>"; + + final WebDriver driver = loadPage2(html); + final WebElement field = driver.findElement(By.id("t")); + + field.sendKeys(" 0123456789"); + assertEquals(getExpectedAlerts(), getCollectedAlerts(driver)); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts({ "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", + "82", "83", "84", "85", "86", "87", "88", "89", "90" }) + public void keyCodes2_keydown() throws Exception { + final String html = "<html><head>" + + "<script>" + + "function handleKey(e) {\n" + + " alert(e.keyCode);" + + "}" + + "</script>\n" + + "</head><body>\n" + + "<input id='t' onkeydown='handleKey(event)'/>\n" + + "</body></html>"; + + final WebDriver driver = loadPage2(html); + final WebElement field = driver.findElement(By.id("t")); + + field.sendKeys("abcdefghijklmnopqrstuvwxyz"); + assertEquals(getExpectedAlerts(), getCollectedAlerts(driver)); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = { "32", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57" }, + IE8 = { "undefined", "undefined", "undefined", "undefined", "undefined", + "undefined", "undefined", "undefined", "undefined", "undefined", "undefined" }) + public void keyCodes_keypress() throws Exception { + final String html = "<html><head>" + + "<script>" + + "function handleKey(e) {\n" + + " alert(e.charCode);" + + "}" + + "</script>\n" + + "</head><body>\n" + + "<input id='t' onkeypress='handleKey(event)'/>\n" + + "</body></html>"; + + final WebDriver driver = loadPage2(html); + final WebElement field = driver.findElement(By.id("t")); + + field.sendKeys(" 0123456789"); + assertEquals(getExpectedAlerts(), getCollectedAlerts(driver)); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts({ "97", "98", "99", + "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", + "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", + "120", "121", "122" }) + public void keyCodes2_keypress() throws Exception { + final String html = "<html><head>" + + "<script>" + + "function handleKey(e) {\n" + + " alert(e.charCode);" + + "}" + + "</script>\n" + + "</head><body>\n" + + "<input id='t' onkeypress='handleKey(event)'/>\n" + + "</body></html>"; + + final WebDriver driver = loadPage2(html); + final WebElement field = driver.findElement(By.id("t")); + + field.sendKeys("abcdefghijklmnopqrstuvwxyz"); + assertEquals(getExpectedAlerts(), getCollectedAlerts(driver)); + } + + /** + * @throws Exception if the test fails + */ // FF seem to generate seperate events for the shift key (tested with // real FF17 and 24) @Test |