44 * SPDX-License-Identifier: LGPL-2.1-or-later
55 *
66 */
7+ #include < memory>
8+ #include < string>
79#include " fcitx-utils/eventdispatcher.h"
10+ #include " fcitx-utils/handlertable.h"
11+ #include " fcitx-utils/key.h"
12+ #include " fcitx-utils/keysym.h"
13+ #include " fcitx-utils/log.h"
14+ #include " fcitx-utils/macros.h"
815#include " fcitx-utils/testing.h"
916#include " fcitx/addonmanager.h"
1017#include " fcitx/instance.h"
18+ #include " fcitx/userinterface.h"
1119#include " quickphrase_public.h"
1220#include " testdir.h"
1321#include " testfrontend_public.h"
@@ -16,8 +24,8 @@ using namespace fcitx;
1624
1725std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>> handle;
1826
19- void scheduleEvent (EventDispatcher *dispatcher, Instance *instance) {
20- dispatcher-> schedule ([instance]() {
27+ void testInit ( Instance *instance) {
28+ instance-> eventDispatcher (). schedule ([instance]() {
2129 auto *quickphrase = instance->addonManager ().addon (" quickphrase" , true );
2230 handle = quickphrase->call <IQuickPhrase::addProvider>(
2331 [](InputContext *, const std::string &text,
@@ -31,7 +39,10 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
3139 });
3240 FCITX_ASSERT (quickphrase);
3341 });
34- dispatcher->schedule ([dispatcher, instance]() {
42+ }
43+
44+ void testBasic (Instance *instance) {
45+ instance->eventDispatcher ().schedule ([instance]() {
3546 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
3647 for (const auto *expectation :
3748 {" TEST" , " abc" , " abcd" , " DEF" , " abcd" , " DEF1" , " test1" , " CALLBACK" ,
@@ -150,12 +161,36 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
150161 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" u" ), false );
151162 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" t" ), false );
152163 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
164+ });
165+ }
153166
154- dispatcher->schedule ([dispatcher, instance]() {
155- handle.reset ();
156- dispatcher->detach ();
157- instance->exit ();
158- });
167+ void testProviderV2 (Instance *instance) {
168+ instance->eventDispatcher ().schedule ([instance]() {
169+ auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
170+ testfrontend->call <ITestFrontend::pushCommitExpectation>(" PROVIDERV2" );
171+ auto uuid =
172+ testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
173+ testfrontend->call <ITestFrontend::keyEvent>(
174+ uuid, Key (FcitxKey_BackSpace), false );
175+ auto *quickphrase = instance->addonManager ().addon (" quickphrase" , true );
176+ auto handleV2 = quickphrase->call <IQuickPhrase::addProviderV2>(
177+ [](InputContext *, const std::string &text,
178+ const QuickPhraseAddCandidateCallbackV2 &callback) {
179+ FCITX_INFO () << " Quickphrase text: " << text;
180+ if (text == " PVD" ) {
181+ callback (" PROVIDERV2" , " V2" , " COMMENT" ,
182+ QuickPhraseAction::Commit);
183+ return false ;
184+ }
185+ return true ;
186+ });
187+
188+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Super+grave" ),
189+ false );
190+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" P" ), false );
191+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" V" ), false );
192+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" D" ), false );
193+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" 1" ), false );
159194 });
160195}
161196
@@ -172,9 +207,13 @@ int main() {
172207 char *argv[] = {arg0, arg1, arg2};
173208 Instance instance (FCITX_ARRAY_SIZE (argv), argv);
174209 instance.addonManager ().registerDefaultLoader (nullptr );
175- EventDispatcher dispatcher;
176- dispatcher.attach (&instance.eventLoop ());
177- scheduleEvent (&dispatcher, &instance);
210+ testInit (&instance);
211+ testBasic (&instance);
212+ testProviderV2 (&instance);
213+ instance.eventDispatcher ().schedule ([&instance]() {
214+ handle.reset ();
215+ instance.exit ();
216+ });
178217 instance.exec ();
179218 return 0 ;
180219}
0 commit comments