From 4594aab10e501c611a46ce877055f7ca97b9bc81 Mon Sep 17 00:00:00 2001 From: Weng Xuetian Date: Wed, 23 Oct 2024 14:04:30 -0700 Subject: [PATCH] Add basic test for anthy (#16) Prepare for investigating #15 issue --- CMakeLists.txt | 2 +- src/engine.h | 2 +- test/CMakeLists.txt | 7 ++++ test/addon/CMakeLists.txt | 2 + test/inputmethod/CMakeLists.txt | 2 + test/testanthy.cpp | 67 +++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 test/addon/CMakeLists.txt create mode 100644 test/inputmethod/CMakeLists.txt create mode 100644 test/testanthy.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cd2820..7c62a12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ option(ENABLE_TEST "Build Test" On) option(ENABLE_COVERAGE "Build the project with gcov support (Need ENABLE_TEST=On)" Off) find_package(Fcitx5Core 5.1.2 REQUIRED) -find_package(Fcitx5Module REQUIRED COMPONENTS Clipboard) +find_package(Fcitx5Module REQUIRED COMPONENTS Clipboard TestFrontend) find_package(Gettext REQUIRED) find_package(PkgConfig REQUIRED) diff --git a/src/engine.h b/src/engine.h index a21b252..ded409a 100644 --- a/src/engine.h +++ b/src/engine.h @@ -50,7 +50,7 @@ class AnthyEngine : public fcitx::InputMethodEngineV3 { void setConfig(const fcitx::RawConfig &config) override { config_.load(config, true); saveConfig(); - reloadConfig(); + populateConfig(); } void saveConfig() { fcitx::safeSaveAsIni(config_, "conf/anthy.conf"); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 387dbd6..ccd94c5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,3 +3,10 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_executable(teststylefile teststylefile.cpp) target_link_libraries(teststylefile stylefile Fcitx5::Core) add_test(teststylefile teststylefile) + +add_subdirectory(addon) +add_subdirectory(inputmethod) +add_executable(testanthy testanthy.cpp) +target_link_libraries(testanthy Fcitx5::Core Fcitx5::Module::TestFrontend) +add_dependencies(testanthy anthy copy-addon copy-im) +add_test(testanthy testanthy) diff --git a/test/addon/CMakeLists.txt b/test/addon/CMakeLists.txt new file mode 100644 index 0000000..062dafb --- /dev/null +++ b/test/addon/CMakeLists.txt @@ -0,0 +1,2 @@ +add_custom_target(copy-addon DEPENDS anthy-addon.conf.in-fmt) +add_custom_command(TARGET copy-addon COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/src/anthy-addon.conf ${CMAKE_CURRENT_BINARY_DIR}/anthy.conf) diff --git a/test/inputmethod/CMakeLists.txt b/test/inputmethod/CMakeLists.txt new file mode 100644 index 0000000..0f1c073 --- /dev/null +++ b/test/inputmethod/CMakeLists.txt @@ -0,0 +1,2 @@ +add_custom_target(copy-im DEPENDS anthy.conf.in-fmt) +add_custom_command(TARGET copy-im COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/src/anthy.conf ${CMAKE_CURRENT_BINARY_DIR}/anthy.conf) diff --git a/test/testanthy.cpp b/test/testanthy.cpp new file mode 100644 index 0000000..75c0052 --- /dev/null +++ b/test/testanthy.cpp @@ -0,0 +1,67 @@ +/* + * SPDX-FileCopyrightText: 2021-2021 CSSlayer + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + */ +#include "testdir.h" +#include "testfrontend_public.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace fcitx; + +void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) { + dispatcher->schedule([dispatcher, instance]() { + auto *anthy = instance->addonManager().addon("anthy", true); + FCITX_ASSERT(anthy); + + auto defaultGroup = instance->inputMethodManager().currentGroup(); + defaultGroup.inputMethodList().clear(); + defaultGroup.inputMethodList().push_back(InputMethodGroupItem("anthy")); + defaultGroup.setDefaultInputMethod(""); + instance->inputMethodManager().setGroup(defaultGroup); + auto *testfrontend = instance->addonManager().addon("testfrontend"); + auto uuid = + testfrontend->call("testapp"); + auto ic = instance->inputContextManager().findByUUID(uuid); + + RawConfig config; + config.setValueByPath("General/TypingMethod", "Nicola"); + anthy->setConfig(config); + + testfrontend->call(uuid, Key("Muhenkan"), + false); + testfrontend->call(uuid, Key("a"), false); + testfrontend->call(uuid, Key("a"), true); + testfrontend->call(uuid, Key("Muhenkan"), + true); + + instance->exit(); + }); +} + +int main() { + setupTestingEnvironment(TESTING_BINARY_DIR, {TESTING_BINARY_DIR "/src"}, + {TESTING_BINARY_DIR "/test"}); + // fcitx::Log::setLogRule("default=5,table=5,libime-table=5"); + char arg0[] = "testanthy"; + char arg1[] = "--disable=all"; + char arg2[] = "--enable=testim,testfrontend,anthy,testui"; + char *argv[] = {arg0, arg1, arg2}; + fcitx::Log::setLogRule("default=5,anthy=5"); + Instance instance(FCITX_ARRAY_SIZE(argv), argv); + instance.addonManager().registerDefaultLoader(nullptr); + EventDispatcher dispatcher; + dispatcher.attach(&instance.eventLoop()); + scheduleEvent(&dispatcher, &instance); + instance.exec(); + + return 0; +}