Skip to content

Commit

Permalink
Add test basic case
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed May 27, 2024
1 parent 1699558 commit 6549b7e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
19 changes: 18 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ include(FeatureSummary)
include(GNUInstallDirs)
include(ECMUninstallTarget)

find_package(Fcitx5Core 5.0.2 REQUIRED)
find_package(Fcitx5Core 5.1.9 REQUIRED)
find_package(Gettext REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(Fcitx5Module REQUIRED COMPONENTS TestFrontend)

option(ENABLE_TEST "Build Test" On)
option(ENABLE_COVERAGE "Build the project with gcov support (Need ENABLE_TEST=On)" Off)

if (NOT DEFINED HANGUL_TARGET)
pkg_check_modules(Hangul IMPORTED_TARGET "libhangul>=0.0.12" REQUIRED)
Expand All @@ -26,6 +30,19 @@ add_subdirectory(src)
add_subdirectory(data)
add_subdirectory(list)

if (ENABLE_TEST)
enable_testing()
add_subdirectory(test)

if (ENABLE_COVERAGE)
add_custom_target(coverage
COMMAND "${CMAKE_CTEST_COMMAND}"
COMMAND lcov --gcov-tool "${GCOV_TOOL}" --no-external --capture --directory ./ -b "${CMAKE_CURRENT_SOURCE_DIR}" --output-file coverage.info
COMMAND genhtml coverage.info --output-directory "coverage_pages"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif()
endif ()

fcitx5_translate_desktop_file(org.fcitx.Fcitx5.Addon.Hangul.metainfo.xml.in
org.fcitx.Fcitx5.Addon.Hangul.metainfo.xml XML)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/org.fcitx.Fcitx5.Addon.Hangul.metainfo.xml" DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)
Expand Down
8 changes: 8 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
configure_file(testdir.h.in ${CMAKE_CURRENT_BINARY_DIR}/testdir.h @ONLY)
add_subdirectory(addon)
add_subdirectory(inputmethod)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(testhangul testhangul.cpp)
target_link_libraries(testhangul Fcitx5::Core Fcitx5::Module::TestFrontend)
add_dependencies(testhangul copy-addon copy-im)
add_test(testhangul testhangul)
2 changes: 2 additions & 0 deletions test/addon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_custom_target(copy-addon DEPENDS hangul-addon.conf.in-fmt)
add_custom_command(TARGET copy-addon COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/src/hangul-addon.conf ${CMAKE_CURRENT_BINARY_DIR}/hangul.conf)
2 changes: 2 additions & 0 deletions test/inputmethod/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_custom_target(copy-im DEPENDS hangul.conf.in-fmt)
add_custom_command(TARGET copy-im COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/src/hangul.conf ${CMAKE_CURRENT_BINARY_DIR}/hangul.conf)
12 changes: 12 additions & 0 deletions test/testdir.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* SPDX-FileCopyrightText: 2021~2021 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef _TEST_TESTDIR_H_
#define _TEST_TESTDIR_H_

#define TESTING_SOURCE_DIR "@CMAKE_SOURCE_DIR@"
#define TESTING_BINARY_DIR "@CMAKE_BINARY_DIR@"

#endif // _TEST_TESTDIR_H_
74 changes: 74 additions & 0 deletions test/testhangul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SPDX-FileCopyrightText: 2024-2024 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "testdir.h"
#include "testfrontend_public.h"
#include <fcitx-utils/eventdispatcher.h>
#include <fcitx-utils/key.h>
#include <fcitx-utils/keysym.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/standardpath.h>
#include <fcitx-utils/testing.h>
#include <fcitx/addonmanager.h>
#include <fcitx/inputmethodgroup.h>
#include <fcitx/inputmethodmanager.h>
#include <fcitx/inputpanel.h>
#include <fcitx/instance.h>

using namespace fcitx;

void scheduleEvent(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *hangul = instance->addonManager().addon("hangul", true);
FCITX_ASSERT(hangul);
auto defaultGroup = instance->inputMethodManager().currentGroup();
defaultGroup.inputMethodList().clear();
defaultGroup.inputMethodList().push_back(
InputMethodGroupItem("keyboard-us"));
defaultGroup.inputMethodList().push_back(
InputMethodGroupItem("hangul"));
defaultGroup.setDefaultInputMethod("");
instance->inputMethodManager().setGroup(defaultGroup);
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));

testfrontend->call<ITestFrontend::pushCommitExpectation>("");
testfrontend->call<ITestFrontend::pushCommitExpectation>("");
testfrontend->call<ITestFrontend::pushCommitExpectation>("");
FCITX_ASSERT(instance->inputMethod(ic) == "hangul");

FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("q"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key("Q"), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key(FcitxKey_Q, KeyState::CapsLock), false));
instance->deactivate();
});

instance->eventDispatcher().schedule([instance]() { instance->exit(); });
}

int main() {
setupTestingEnvironment(TESTING_BINARY_DIR, {TESTING_BINARY_DIR "/src"},
{TESTING_BINARY_DIR "/test"});
char arg0[] = "testhangul";
char arg1[] = "--disable=all";
char arg2[] = "--enable=testim,testfrontend,hangul";
char *argv[] = {arg0, arg1, arg2};
fcitx::Log::setLogRule("default=5,hangul=5");
Instance instance(FCITX_ARRAY_SIZE(argv), argv);
instance.addonManager().registerDefaultLoader(nullptr);
scheduleEvent(&instance);
instance.exec();

return 0;
}

0 comments on commit 6549b7e

Please sign in to comment.