Skip to content

Commit

Permalink
tests: Decouple input.json from Flash key codes
Browse files Browse the repository at this point in the history
This refactor removes key codes from input.json aiming at moving key
code mapping to core and creating a common, Flash-independent interface
for input.
  • Loading branch information
kjarosh committed Feb 18, 2025
1 parent 78fd707 commit 106eae5
Show file tree
Hide file tree
Showing 71 changed files with 2,054 additions and 1,995 deletions.
53 changes: 48 additions & 5 deletions tests/framework/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ruffle_core::limits::ExecutionLimit;
use ruffle_core::tag_utils::SwfMovie;
use ruffle_core::{Player, PlayerBuilder, PlayerEvent};
use ruffle_input_format::{
AutomatedEvent, InputInjector, MouseButton as InputMouseButton,
AutomatedEvent, AutomatedKey, InputInjector, MouseButton as InputMouseButton,
TextControlCode as InputTextControlCode,
};
use ruffle_render::backend::{RenderBackend, ViewportDimensions};
Expand Down Expand Up @@ -235,12 +235,12 @@ impl TestRunner {
_ => panic!("MouseWheel: expected only one of 'lines' or 'pixels'"),
},
},
AutomatedEvent::KeyDown { key_code } => PlayerEvent::KeyDown {
key_code: KeyCode::from_code(*key_code),
AutomatedEvent::KeyDown { key } => PlayerEvent::KeyDown {
key_code: automated_key_to_key_code(*key),
key_char: None,
},
AutomatedEvent::KeyUp { key_code } => PlayerEvent::KeyUp {
key_code: KeyCode::from_code(*key_code),
AutomatedEvent::KeyUp { key } => PlayerEvent::KeyUp {
key_code: automated_key_to_key_code(*key),
key_char: None,
},
AutomatedEvent::TextInput { codepoint } => PlayerEvent::TextInput {
Expand Down Expand Up @@ -574,3 +574,46 @@ fn assert_text_matches(ruffle: &str, flash: &str) -> Result<()> {
Ok(())
}
}

fn automated_key_to_key_code(automated_key: AutomatedKey) -> KeyCode {
match automated_key {
AutomatedKey::Char(ch) => KeyCode::from_code(ch.to_ascii_uppercase() as u32),
AutomatedKey::Numpad(ch) => match ch {
'0' => KeyCode::NUMPAD_0,
'1' => KeyCode::NUMPAD_1,
'2' => KeyCode::NUMPAD_2,
'3' => KeyCode::NUMPAD_3,
'4' => KeyCode::NUMPAD_4,
'5' => KeyCode::NUMPAD_5,
'6' => KeyCode::NUMPAD_6,
'7' => KeyCode::NUMPAD_7,
'8' => KeyCode::NUMPAD_8,
'9' => KeyCode::NUMPAD_9,
'*' => KeyCode::NUMPAD_MULTIPLY,
'+' => KeyCode::NUMPAD_ADD,
'-' => KeyCode::NUMPAD_SUBTRACT,
'.' | ',' => KeyCode::NUMPAD_DECIMAL,
'/' => KeyCode::NUMPAD_DIVIDE,
ch => panic!("Unknown numpad key: {}", ch),
},
AutomatedKey::ArrowDown => KeyCode::DOWN,
AutomatedKey::ArrowLeft => KeyCode::LEFT,
AutomatedKey::ArrowRight => KeyCode::RIGHT,
AutomatedKey::ArrowUp => KeyCode::UP,
AutomatedKey::Backspace => KeyCode::BACKSPACE,
AutomatedKey::CapsLock => KeyCode::CAPS_LOCK,
AutomatedKey::Control => KeyCode::CONTROL,
AutomatedKey::Delete => KeyCode::DELETE,
AutomatedKey::End => KeyCode::END,
AutomatedKey::Enter => KeyCode::ENTER,
AutomatedKey::Escape => KeyCode::ESCAPE,
AutomatedKey::Home => KeyCode::HOME,
AutomatedKey::Insert => KeyCode::INSERT,
AutomatedKey::PageDown => KeyCode::PAGE_DOWN,
AutomatedKey::PageUp => KeyCode::PAGE_UP,
AutomatedKey::Shift => KeyCode::SHIFT,
AutomatedKey::Space => KeyCode::SPACE,
AutomatedKey::Tab => KeyCode::TAB,
AutomatedKey::Unknown => KeyCode::UNKNOWN,
}
}
32 changes: 30 additions & 2 deletions tests/input-format/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ pub enum TextControlCode {
Delete,
}

/// All possible keys which can be simulated in tests.
///
/// Note: Add more keys if needed.
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub enum AutomatedKey {
Char(char),
Numpad(char),
ArrowDown,
ArrowLeft,
ArrowRight,
ArrowUp,
Backspace,
CapsLock,
Control,
Delete,
End,
Enter,
Escape,
Home,
Insert,
PageDown,
PageUp,
Shift,
Space,
Tab,
Unknown,
}

/// All automated event types supported by FlashTAS.
///
/// A FlashTAS input file consists of a string of `AutomatedEvent`s which are
Expand Down Expand Up @@ -82,10 +110,10 @@ pub enum AutomatedEvent {
},

/// Press a key
KeyDown { key_code: u32 },
KeyDown { key: AutomatedKey },

/// Release a key
KeyUp { key_code: u32 },
KeyUp { key: AutomatedKey },

/// Input a character code
TextInput { codepoint: char },
Expand Down
2 changes: 1 addition & 1 deletion tests/input-format/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod format;
mod injector;

pub use format::{AutomatedEvent, MouseButton, TextControlCode};
pub use format::{AutomatedEvent, AutomatedKey, MouseButton, TextControlCode};
pub use injector::{InputInjector, MouseButtons};
20 changes: 10 additions & 10 deletions tests/tests/swfs/avm1/button_key_events/input.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[
{ "type": "KeyDown", "key_code": 65 },
{ "type": "KeyDown", "key": { "Char": "a" } },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyUp", "key_code": 65 },
{ "type": "KeyDown", "key_code": 66 },
{ "type": "KeyUp", "key": { "Char": "a" } },
{ "type": "KeyDown", "key": { "Char": "b" } },
{ "type": "TextInput", "codepoint": "b" },
{ "type": "KeyDown", "key_code": 65 },
{ "type": "KeyDown", "key": { "Char": "a" } },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyUp", "key_code": 66 },
{ "type": "KeyUp", "key_code": 65 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key": { "Char": "b" } },
{ "type": "KeyUp", "key": { "Char": "a" } },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 65 },
{ "type": "KeyUp", "key": "Tab" },
{ "type": "KeyDown", "key": { "Char": "a" } },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyUp", "key_code": 65 }
{ "type": "KeyUp", "key": { "Char": "a" } }
]
60 changes: 30 additions & 30 deletions tests/tests/swfs/avm1/button_key_events_special/input.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
[
{ "type": "KeyDown", "key_code": 37 },
{ "type": "KeyDown", "key": "ArrowLeft" },
{ "type": "TextControl", "code": "MoveLeft" },
{ "type": "KeyUp", "key_code": 37 },
{ "type": "KeyUp", "key": "ArrowLeft" },

{ "type": "KeyDown", "key_code": 39 },
{ "type": "KeyDown", "key": "ArrowRight" },
{ "type": "TextControl", "code": "MoveRight" },
{ "type": "KeyUp", "key_code": 39 },
{ "type": "KeyUp", "key": "ArrowRight" },

{ "type": "KeyDown", "key_code": 36 },
{ "type": "KeyDown", "key": "Home" },
{ "type": "TextControl", "code": "MoveLeftLine" },
{ "type": "KeyUp", "key_code": 36 },
{ "type": "KeyUp", "key": "Home" },

{ "type": "KeyDown", "key_code": 35 },
{ "type": "KeyDown", "key": "End" },
{ "type": "TextControl", "code": "MoveRightLine" },
{ "type": "KeyUp", "key_code": 35 },
{ "type": "KeyUp", "key": "End" },

{ "type": "KeyDown", "key_code": 45 },
{ "type": "KeyUp", "key_code": 45 },
{ "type": "KeyDown", "key": "Insert" },
{ "type": "KeyUp", "key": "Insert" },

{ "type": "KeyDown", "key_code": 46 },
{ "type": "KeyDown", "key": "Delete" },
{ "type": "TextControl", "code": "Delete" },
{ "type": "KeyUp", "key_code": 46 },
{ "type": "KeyUp", "key": "Delete" },

{ "type": "KeyDown", "key_code": 8 },
{ "type": "KeyDown", "key": "Backspace" },
{ "type": "TextControl", "code": "Backspace" },
{ "type": "KeyUp", "key_code": 8 },
{ "type": "KeyUp", "key": "Backspace" },

{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyUp", "key": "Tab" },

{ "type": "KeyDown", "key_code": 13 },
{ "type": "KeyDown", "key": "Enter" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "KeyUp", "key_code": 13 },
{ "type": "KeyUp", "key": "Enter" },

{ "type": "KeyDown", "key_code": 38 },
{ "type": "KeyUp", "key_code": 38 },
{ "type": "KeyDown", "key": "ArrowUp" },
{ "type": "KeyUp", "key": "ArrowUp" },

{ "type": "KeyDown", "key_code": 40 },
{ "type": "KeyUp", "key_code": 40 },
{ "type": "KeyDown", "key": "ArrowDown" },
{ "type": "KeyUp", "key": "ArrowDown" },

{ "type": "KeyDown", "key_code": 33 },
{ "type": "KeyUp", "key_code": 33 },
{ "type": "KeyDown", "key": "PageUp" },
{ "type": "KeyUp", "key": "PageUp" },

{ "type": "KeyDown", "key_code": 34 },
{ "type": "KeyUp", "key_code": 34 },
{ "type": "KeyDown", "key": "PageDown" },
{ "type": "KeyUp", "key": "PageDown" },

{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "TextInput", "codepoint": "\u001b" },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "KeyUp", "key": "Escape" },

{ "type": "KeyDown", "key_code": 32 },
{ "type": "KeyDown", "key": "Space" },
{ "type": "TextInput", "codepoint": " " },
{ "type": "KeyUp", "key_code": 32 }
{ "type": "KeyUp", "key": "Space" }
]
2 changes: 1 addition & 1 deletion tests/tests/swfs/avm1/button_keypress/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
{
"type": "KeyDown",
"key_code": 13
"key": "Enter"
},
{
"type": "Wait"
Expand Down
20 changes: 10 additions & 10 deletions tests/tests/swfs/avm1/button_keypress_vs_press/input.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[
{ "type": "KeyDown", "key_code": 13 },
{ "type": "KeyDown", "key": "Enter" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "KeyUp", "key_code": 13 },
{ "type": "KeyDown", "key_code": 32 },
{ "type": "KeyUp", "key": "Enter" },
{ "type": "KeyDown", "key": "Space" },
{ "type": "TextInput", "codepoint": " " },
{ "type": "KeyUp", "key_code": 32 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key": "Space" },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 13 },
{ "type": "KeyUp", "key": "Tab" },
{ "type": "KeyDown", "key": "Enter" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "KeyUp", "key_code": 13 },
{ "type": "KeyDown", "key_code": 32 },
{ "type": "KeyUp", "key": "Enter" },
{ "type": "KeyDown", "key": "Space" },
{ "type": "TextInput", "codepoint": " " },
{ "type": "KeyUp", "key_code": 32 }
{ "type": "KeyUp", "key": "Space" }
]
20 changes: 10 additions & 10 deletions tests/tests/swfs/avm1/button_keypress_vs_tab/input.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key": "Tab" },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyUp", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key": "Tab" },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "KeyUp", "key": "Escape" },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyUp", "key": "Tab" },
{ "type": "KeyDown", "key": "Tab" },
{ "type": "TextInput", "codepoint": "\t" },
{ "type": "KeyUp", "key_code": 9 }
{ "type": "KeyUp", "key": "Tab" }
]
8 changes: 4 additions & 4 deletions tests/tests/swfs/avm1/button_keypress_vs_textinput/input.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{ "type": "KeyDown", "key_code": 65 },
{ "type": "KeyDown", "key": { "Char": "a" } },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyUp", "key_code": 65 },
{ "type": "KeyDown", "key_code": 66 },
{ "type": "KeyUp", "key": { "Char": "a" } },
{ "type": "KeyDown", "key": { "Char": "b" } },
{ "type": "TextInput", "codepoint": "b" },
{ "type": "KeyUp", "key_code": 66 }
{ "type": "KeyUp", "key": { "Char": "b" } }
]
2 changes: 1 addition & 1 deletion tests/tests/swfs/avm1/edittext_input/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@
{ "type": "TextInput", "codepoint": "𝼙" },
{ "type": "TextInput", "codepoint": "𝼞" },
{ "type": "TextInput", "codepoint": "" },
{ "type": "KeyDown", "key_code": 27 }
{ "type": "KeyDown", "key": "Escape" }
]
2 changes: 1 addition & 1 deletion tests/tests/swfs/avm1/edittext_input/input.json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
for ch in characters:
print(f' {{ "type": "TextInput", "codepoint": "{ch}" }},')

print(f' {{ "type": "KeyDown", "key_code": 27 }}')
print(f' {{ "type": "KeyDown", "key": "Escape" }}')

print(']')
4 changes: 2 additions & 2 deletions tests/tests/swfs/avm1/edittext_input_newlines/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{ "type": "TextInput", "codepoint": "a" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyDown", "key_code": 27 }
{ "type": "KeyDown", "key": "Escape" }
]
8 changes: 4 additions & 4 deletions tests/tests/swfs/avm1/edittext_password_copy/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
{ "type": "SetClipboardText", "text": "password" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "SetClipboardText", "text": "" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "TextControl", "code": "SelectAll" },
{ "type": "TextControl", "code": "Copy" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "SetClipboardText", "text": "password" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "SetClipboardText", "text": "" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "TextControl", "code": "SelectAll" },
{ "type": "TextControl", "code": "Cut" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 }
{ "type": "KeyDown", "key": "Escape" }
]
4 changes: 2 additions & 2 deletions tests/tests/swfs/avm1/edittext_paste_empty/input.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{ "type": "SetClipboardText", "text": "test" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "SetClipboardText", "text": "" },
{ "type": "TextControl", "code": "SelectAll" },
{ "type": "TextControl", "code": "Paste" },
{ "type": "KeyDown", "key_code": 27 }
{ "type": "KeyDown", "key": "Escape" }
]
4 changes: 2 additions & 2 deletions tests/tests/swfs/avm1/edittext_place_caret/input.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key": "Escape" },
{ "type": "MouseMove", "pos": [400, 10] },
{ "type": "MouseDown", "pos": [400, 10], "btn": "Left" },
{ "type": "MouseUp", "pos": [400, 10], "btn": "Left" },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "TextInput", "codepoint": "s" },
{ "type": "TextInput", "codepoint": "d" },
{ "type": "TextInput", "codepoint": "f" },
{ "type": "KeyDown", "key_code": 27 }
{ "type": "KeyDown", "key": "Escape" }
]
Loading

0 comments on commit 106eae5

Please sign in to comment.