Skip to content

Commit

Permalink
Add test case for #21
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Jun 12, 2024
1 parent a9c1b79 commit e45da4c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ find_package(ECM REQUIRED 1.0.0)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

find_package(PkgConfig REQUIRED)
find_package(Fcitx5Core 5.1.6 REQUIRED)
find_package(Fcitx5Core 5.1.9 REQUIRED)
find_package(Fcitx5Module REQUIRED COMPONENTS TestFrontend)
find_package(Gettext REQUIRED)
include(FeatureSummary)
Expand Down
104 changes: 92 additions & 12 deletions test/testchewing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
*/
#include "testdir.h"
#include "testfrontend_public.h"
#include <fcitx-config/rawconfig.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>
#include <string>
#include <string_view>

using namespace fcitx;

void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
dispatcher->schedule([instance]() {
void testBasic(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *chewing = instance->addonManager().addon("chewing", true);
FCITX_ASSERT(chewing);
auto defaultGroup = instance->inputMethodManager().currentGroup();
Expand Down Expand Up @@ -114,8 +120,40 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {

instance->deactivate();
});
}

dispatcher->schedule([instance]() {
void testBackspaceWithBuffer(Instance *instance) {
instance->eventDispatcher().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(FcitxKey_BackSpace), false));
auto text = ic->inputPanel().preedit().toString();
FCITX_ASSERT(text.empty());

instance->deactivate();
});
}

void testBackspaceWhenBufferEmpty(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *chewing = instance->addonManager().addon("chewing", true);
FCITX_ASSERT(chewing);
RawConfig config;
Expand Down Expand Up @@ -150,8 +188,50 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {

instance->deactivate();
});
}

void testBackspaceWithBopomofo(Instance *instance) {
instance->eventDispatcher().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));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key(FcitxKey_BackSpace), false));
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key(FcitxKey_BackSpace), false));
auto text = ic->inputPanel().preedit().toString();
FCITX_ASSERT(text.empty());
FCITX_ASSERT(!testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key(FcitxKey_BackSpace), false));

instance->deactivate();
});
}

dispatcher->schedule([instance]() {
void testCommitPreedit(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *chewing = instance->addonManager().addon("chewing", true);
FCITX_ASSERT(chewing);
RawConfig config;
Expand Down Expand Up @@ -188,11 +268,6 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {

instance->deactivate();
});

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

int main() {
Expand All @@ -206,9 +281,14 @@ int main() {
fcitx::Log::setLogRule("default=5,chewing=5");
Instance instance(FCITX_ARRAY_SIZE(argv), argv);
instance.addonManager().registerDefaultLoader(nullptr);
EventDispatcher dispatcher;
dispatcher.attach(&instance.eventLoop());
scheduleEvent(&dispatcher, &instance);

testBasic(&instance);
testBackspaceWhenBufferEmpty(&instance);
testBackspaceWithBuffer(&instance);
testBackspaceWithBopomofo(&instance);
testCommitPreedit(&instance);

instance.eventDispatcher().schedule([&instance]() { instance.exit(); });
instance.exec();

return 0;
Expand Down

0 comments on commit e45da4c

Please sign in to comment.