Skip to content

Commit

Permalink
Test pressing and typing
Browse files Browse the repository at this point in the history
And fix bugs thus revealed
  • Loading branch information
hadley committed Feb 1, 2024
1 parent 397e03c commit 6d6fe05
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/live.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ as_key_desc <- function(key, modifiers = character(), error_call = caller_env())
desc <- list()

desc$key <- def$key %||% ""
if ("shift" %in% modifiers && has_name(def, "shiftKey")) {
if ("Shift" %in% modifiers && has_name(def, "shiftKey")) {
desc$key <- def$shiftKey
}

desc$windowsVirtualKeyCode <- def$keyCode %||% 0
if ("shift" %in% modifiers && has_name(def, "shiftKeyCode")) {
if ("Shift" %in% modifiers && has_name(def, "shiftKeyCode")) {
desc$windowsVirtualKeyCode <- def$shiftKeyCode
}

Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/_snaps/live.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@
[20] <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font ...
...

# gracefully errors on bad inputs

Code
as_key_desc("xyz")
Condition
Error in `as_key_desc()`:
! No key definition for "xyz".
Code
as_key_desc("X", "Malt")
Condition
Error:
! `modifiers` must be one of "Alt", "Control", "Meta", or "Shift", not "Malt".
i Did you mean "Alt"?

18 changes: 18 additions & 0 deletions tests/testthat/html/press.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Keypress Description</title>
<script>
function describeKeypress(event) {
const keyInfo = `${event.key}/${event.code}`;
document.getElementById('keyInfo').textContent = keyInfo;
}
</script>
</head>
<body>

<input type="text" id="inputBox" onkeydown="describeKeypress(event)">
<p id="keyInfo"></p>

</body>
</html>
17 changes: 17 additions & 0 deletions tests/testthat/html/type.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Text Replication</title>
<script>
function replicateText() {
document.getElementById('replicatedText').textContent = document.getElementById('inputText').value;
}
</script>
</head>
<body>

<input type="text" id="inputText" oninput="replicateText()">
<p id="replicatedText"></p>

</body>
</html>
52 changes: 52 additions & 0 deletions tests/testthat/test-live.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,55 @@ test_that("can scroll in various ways", {
Sys.sleep(0.1)
expect_equal(sess$get_scroll_position(), list(x = 0, y = 685))
})

test_that("can type text", {
skip_if_no_chromote()

sess <- read_html_live(html_test_path("type"))
sess$type("#inputText", "hello")
expect_equal(html_text(html_element(sess, "#replicatedText")), "hello")
})

test_that("can press special keys",{
skip_if_no_chromote()

sess <- read_html_live(html_test_path("press"))
sess$press("#inputBox", "ArrowRight")
expect_equal(html_text(html_element(sess, "#keyInfo")), "ArrowRight/ArrowRight")

sess$press("#inputBox", "BracketRight")
expect_equal(html_text(html_element(sess, "#keyInfo")), "]/BracketRight")
})


# as_key_desc -------------------------------------------------------------

test_that("gracefully errors on bad inputs", {
expect_snapshot(error = TRUE, {
as_key_desc("xyz")
as_key_desc("X", "Malt")
})
})

test_that("automatically adjusts for shift key", {
# str(Filter(\(x) has_name(x, "shiftKey"), keydefs))
expect_equal(as_key_desc("KeyA")$key, "a")
expect_equal(as_key_desc("KeyA", "Shift")$key, "A")

# str(Filter(\(x) has_name(x, "shiftKeyCode"), keydefs))
expect_equal(as_key_desc("Numpad0")$windowsVirtualKeyCode, 45)
expect_equal(as_key_desc("Numpad0", "Shift")$windowsVirtualKeyCode, 96)
})

test_that("don't send text if modifier pushed", {
expect_equal(as_key_desc("KeyA")$text, "a")
expect_equal(as_key_desc("KeyA", "Shift")$text, "a")
expect_equal(as_key_desc("KeyA", "Alt")$text, "")
expect_equal(as_key_desc("KeyA", "Meta")$text, "")
expect_equal(as_key_desc("KeyA", "Control")$text, "")
})

test_that("modifiers are bitflag", {
expect_equal(as_key_desc("KeyA", "Shift")$modifiers, 8)
expect_equal(as_key_desc("KeyA", c("Alt", "Control"))$modifiers, 3)
})

0 comments on commit 6d6fe05

Please sign in to comment.