Skip to content

Commit

Permalink
Check the return value of chewing_commit_preedit_buf.
Browse files Browse the repository at this point in the history
Unlike chewing_handle_, the output may not be updated upon failure.

Fix #17
  • Loading branch information
wengxt committed Apr 28, 2024
1 parent 322925a commit 924e4c6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 20 deletions.
32 changes: 19 additions & 13 deletions src/eim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fcitx/userinterface.h>
#include <fcitx/userinterfacemanager.h>
#include <memory>
#include <mod_aux.h>
#include <utility>

FCITX_DEFINE_LOG_CATEGORY(chewing_log, "chewing");
Expand Down Expand Up @@ -318,7 +319,9 @@ void ChewingEngine::reset(const InputMethodEntry & /*entry*/,

void ChewingEngine::doReset(InputContextEvent &event) {
ChewingContext *ctx = context_.get();
chewing_handle_Esc(ctx);
chewing_cand_close(ctx);
chewing_clean_preedit_buf(ctx);
chewing_clean_bopomofo_buf(ctx);
updateUI(event.inputContext());
}

Expand Down Expand Up @@ -625,27 +628,30 @@ void ChewingEngine::updateUI(InputContext *ic) {

void ChewingEngine::flushBuffer(InputContextEvent &event) {
auto *ctx = context_.get();
std::string text;
if (*config_.switchInputMethodBehavior ==
SwitchInputMethodBehavior::CommitPreedit ||
*config_.switchInputMethodBehavior ==
SwitchInputMethodBehavior::CommitDefault) {
chewing_handle_Enter(ctx);
if (chewing_commit_Check(ctx)) {
UniqueCPtr<char, chewing_free> str(chewing_commit_String(ctx));
event.inputContext()->commitString(str.get());
chewing_cand_close(ctx);
if (chewing_buffer_Check(ctx)) {
// When not success, chewing_commit_preedit_buf will not change the
// output value. while chewing_handle_* will always update button
// result.
if (chewing_commit_preedit_buf(ctx) == 0 &&
chewing_commit_Check(ctx)) {
text.append(chewing_commit_String_static(ctx));
}
}
}

if (*config_.switchInputMethodBehavior ==
SwitchInputMethodBehavior::CommitPreedit) {
UniqueCPtr<char, chewing_free> buf_str(chewing_buffer_String(ctx));
const char *zuin_str = chewing_bopomofo_String_static(ctx);
std::string text = buf_str.get();
std::string zuin = zuin_str;
text += zuin;
if (!text.empty()) {
event.inputContext()->commitString(text);
}
text.append(chewing_buffer_String_static(ctx));
text.append(chewing_bopomofo_String_static(ctx));
}
if (!text.empty()) {
event.inputContext()->commitString(text);
}
doReset(event);
}
Expand Down
88 changes: 81 additions & 7 deletions test/testchewing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "testdir.h"
#include "testfrontend_public.h"
#include <fcitx-utils/eventdispatcher.h>
#include <fcitx-utils/keysym.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/standardpath.h>
#include <fcitx-utils/testing.h>
Expand All @@ -18,7 +19,7 @@
using namespace fcitx;

void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
dispatcher->schedule([dispatcher, instance]() {
dispatcher->schedule([instance]() {
auto *chewing = instance->addonManager().addon("chewing", true);
FCITX_ASSERT(chewing);
auto defaultGroup = instance->inputMethodManager().currentGroup();
Expand Down Expand Up @@ -112,14 +113,87 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
testfrontend->call<ITestFrontend::pushCommitExpectation>(text);

instance->deactivate();
dispatcher->schedule([dispatcher, instance]() {
dispatcher->detach();
instance->exit();
});
});
}

void runInstance() {}
dispatcher->schedule([instance]() {
auto *chewing = instance->addonManager().addon("chewing", true);
FCITX_ASSERT(chewing);
RawConfig config;
config.setValueByPath("Layout", "Default");
chewing->setConfig(config);
auto *testfrontend = instance->addonManager().addon("testfrontend");
auto uuid =
testfrontend->call<ITestFrontend::createInputContext>("testapp");
auto *ic = instance->inputContextManager().findByUUID(uuid);
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("Control+space"), false));
FCITX_ASSERT(instance->inputMethod(ic) == "chewing");

FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("z"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("p"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("space"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("z"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("p"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("space"), false));
auto text = ic->inputPanel().preedit().toString();
testfrontend->call<ITestFrontend::pushCommitExpectation>(text);
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("Return"), false));
FCITX_ASSERT(!testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key(FcitxKey_BackSpace), false));

instance->deactivate();
});

dispatcher->schedule([instance]() {
auto *chewing = instance->addonManager().addon("chewing", true);
FCITX_ASSERT(chewing);
RawConfig config;
config.setValueByPath("Layout", "Default Keyboard");
config.setValueByPath("SwitchInputMethodBehavior",
"Commit current preedit");
chewing->setConfig(config);
auto *testfrontend = instance->addonManager().addon("testfrontend");
auto uuid =
testfrontend->call<ITestFrontend::createInputContext>("testapp");
auto *ic = instance->inputContextManager().findByUUID(uuid);
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("Control+space"), false));
FCITX_ASSERT(instance->inputMethod(ic) == "chewing");

FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("z"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("p"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("space"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("z"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("p"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("space"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("z"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("p"), false));
auto text = ic->inputPanel().preedit().toString();
testfrontend->call<ITestFrontend::pushCommitExpectation>(text);

instance->deactivate();
});

dispatcher->schedule([dispatcher, instance]() {
dispatcher->detach();
instance->exit();
});
}

int main() {
setupTestingEnvironment(TESTING_BINARY_DIR, {TESTING_BINARY_DIR "/src"},
Expand Down

0 comments on commit 924e4c6

Please sign in to comment.