Skip to content

Commit 0c30244

Browse files
committed
Add tests for KeyEvent result
1 parent d755243 commit 0c30244

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

test/lua/testlua/test.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,27 @@ end
5656
function testUtf8Conversion(str)
5757
return fcitx.UTF16ToUTF8(str)
5858
end
59+
60+
function getLastCommit()
61+
return fcitx.lastCommit()
62+
end
63+
64+
fcitx.watchEvent(fcitx.EventType.KeyEvent, "testKeyEventResultHandler")
65+
66+
function testKeyEventResultHandler(sym, state, release)
67+
local key = string.char(sym)
68+
if key == "x" then
69+
return fcitx.KeyEventResult.NotHandled
70+
elseif key == "y" then
71+
return fcitx.KeyEventResult.Handled
72+
elseif key == "z" then
73+
return fcitx.KeyEventResult.Passthrough
74+
elseif key == "e" then
75+
return false
76+
elseif key == "f" then
77+
return
78+
elseif key == "g" then
79+
return true
80+
end
81+
return fcitx.KeyEventResult.NotHandled
82+
end

test/testlua.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,42 @@
2929

3030
using namespace fcitx;
3131

32+
void testKeyEventResult(AddonInstance *testfrontend, AddonInstance *luaaddon,
33+
ICUUID uuid, InputContext *ic) {
34+
auto sendKey = [&](const std::string &key) {
35+
return testfrontend->call<ITestFrontend::sendKeyEvent>(uuid, Key(key),
36+
false);
37+
};
38+
auto getLastCommit = [&]() {
39+
return luaaddon
40+
->call<ILuaAddon::invokeLuaFunction>(ic, "getLastCommit",
41+
RawConfig{})
42+
.value();
43+
};
44+
45+
// Test KeyEventResult enum values
46+
47+
FCITX_ASSERT(sendKey("x")) << "NotHandled: should accept the event";
48+
FCITX_ASSERT(getLastCommit() == "x") << "NotHandled: should commit 'x'";
49+
50+
FCITX_ASSERT(sendKey("y")) << "Handled: should accept the event";
51+
FCITX_ASSERT(getLastCommit() == "x") << "Handled: should still be 'x'";
52+
53+
FCITX_ASSERT(!sendKey("z")) << "Passthrough: should not accept the event";
54+
FCITX_ASSERT(getLastCommit() == "x") << "Passthrough: should still be 'x'";
55+
56+
// Legacy boolean/nil return values
57+
58+
FCITX_ASSERT(sendKey("e")) << "false: should accept the event";
59+
FCITX_ASSERT(getLastCommit() == "e") << "false: should commit 'e'";
60+
61+
FCITX_ASSERT(sendKey("f")) << "nil: should accept the event";
62+
FCITX_ASSERT(getLastCommit() == "f") << "nil: should commit 'f'";
63+
64+
FCITX_ASSERT(sendKey("g")) << "true: should accept the event";
65+
FCITX_ASSERT(getLastCommit() == "f") << "true: should still be 'f'";
66+
}
67+
3268
void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
3369
dispatcher->schedule([instance]() {
3470
auto *luaaddonloader =
@@ -126,6 +162,8 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
126162
ic, "testUtf8Conversion", strConfig);
127163
FCITX_ASSERT(ret.value() == testString) << ret;
128164

165+
testKeyEventResult(testfrontend, luaaddon, uuid, ic);
166+
129167
dispatcher->detach();
130168
instance->exit();
131169
});

0 commit comments

Comments
 (0)