Allow to simulate key event with the interpreter (C++)

This commit is contained in:
Olivier Goffart 2021-06-11 11:21:09 +02:00
parent 7f9bb18b9a
commit 7b63bb7d65
2 changed files with 41 additions and 1 deletions

View file

@ -836,3 +836,18 @@ public:
};
}
namespace sixtyfps::testing {
/// Send a key events to the given component instance
inline void send_keyboard_string_sequence(const sixtyfps::interpreter::ComponentInstance *component,
const sixtyfps::SharedString &str,
KeyboardModifiers modifiers = {})
{
cbindgen_private::ComponentWindowOpaque win;
cbindgen_private::sixtyfps_interpreter_component_instance_window(
reinterpret_cast<const cbindgen_private::ErasedComponentBox *>(component), &win);
cbindgen_private::send_keyboard_string_sequence(
&str, modifiers, reinterpret_cast<cbindgen_private::ComponentWindow *>(&win));
cbindgen_private::sixtyfps_component_window_drop(&win);
}
}

View file

@ -418,3 +418,28 @@ SCENARIO("Component Definition Name")
auto comp_def = *compiler.build_from_source("export IHaveAName := Rectangle { }", "");
REQUIRE(comp_def.name() == "IHaveAName");
}
SCENARIO("Send key events")
{
using namespace sixtyfps::interpreter;
using namespace sixtyfps;
ComponentCompiler compiler;
auto comp_def = compiler.build_from_source(R"(
export Dummy := Rectangle {
forward-focus: scope;
property <string> result;
scope := FocusScope {
key-pressed(event) => {
result += event.text;
return accept;
}
}
}
)",
"");
REQUIRE(comp_def.has_value());
auto instance = comp_def->create();
sixtyfps::testing::send_keyboard_string_sequence(&*instance, "Hello keys!", {});
REQUIRE(*instance->get_property("result")->to_string() == "Hello keys!");
}