1818#include < fcitx-utils/macros.h>
1919#include < fcitx-utils/standardpaths.h>
2020#include < fcitx-utils/testing.h>
21+ #include < fcitx/addoninstance.h>
2122#include < fcitx/addonmanager.h>
23+ #include < fcitx/inputcontext.h>
2224#include < fcitx/inputmethodgroup.h>
2325#include < fcitx/inputmethodmanager.h>
2426#include < fcitx/inputpanel.h>
@@ -54,7 +56,21 @@ void findAndSelectCandidate(InputContext *ic, std::string_view word) {
5456 candList->candidate (findCandidateOrDie (ic, word)).select (ic);
5557}
5658
57- void testBasic (Instance *instance) {
59+ void sendControlSpace (AddonInstance *testfrontend, InputContext *ic) {
60+ for (int i = 0 ; i < 2 ; i++) {
61+ testfrontend->call <ITestFrontend::keyEvent>(ic->uuid (),
62+ Key (" Control_L" ), false );
63+ testfrontend->call <ITestFrontend::keyEvent>(
64+ ic->uuid (), Key (" Control+space" ), false );
65+ testfrontend->call <ITestFrontend::keyEvent>(ic->uuid (),
66+ Key (" Control+space" ), true );
67+ testfrontend->call <ITestFrontend::keyEvent>(
68+ ic->uuid (), Key (" Control+Control_L" ), true );
69+ ic->reset ();
70+ }
71+ }
72+
73+ void setup (Instance *instance) {
5874 instance->eventDispatcher ().schedule ([instance]() {
5975 auto *pinyin = instance->addonManager ().addon (" pinyin" , true );
6076 FCITX_ASSERT (pinyin);
@@ -64,17 +80,27 @@ void testBasic(Instance *instance) {
6480 InputMethodGroupItem (" keyboard-us" ));
6581 defaultGroup.inputMethodList ().push_back (
6682 InputMethodGroupItem (" pinyin" ));
83+ defaultGroup.inputMethodList ().push_back (
84+ InputMethodGroupItem (" shuangpin" ));
6785 defaultGroup.setDefaultInputMethod (" " );
6886 instance->inputMethodManager ().setGroup (std::move (defaultGroup));
87+ });
88+ }
89+
90+ void testBasic (Instance *instance) {
91+ instance->eventDispatcher ().schedule ([instance]() {
92+ auto *pinyin = instance->addonManager ().addon (" pinyin" );
6993 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
7094 auto uuid =
7195 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
7296 testfrontend->call <ITestFrontend::pushCommitExpectation>(" 俺" );
7397 testfrontend->call <ITestFrontend::pushCommitExpectation>(" ni" );
7498 testfrontend->call <ITestFrontend::pushCommitExpectation>(" ni" );
7599 testfrontend->call <ITestFrontend::pushCommitExpectation>(" 你hao" );
76- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
77- false );
100+ auto *ic = instance->inputContextManager ().findByUUID (uuid);
101+ FCITX_ASSERT (ic);
102+ instance->setCurrentInputMethod (ic, " pinyin" , true );
103+
78104 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" a" ), false );
79105 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" `" ), false );
80106 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" n" ), false );
@@ -98,8 +124,6 @@ void testBasic(Instance *instance) {
98124 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" h" ), false );
99125 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" a" ), false );
100126 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
101- auto *ic = instance->inputContextManager ().findByUUID (uuid);
102- FCITX_ASSERT (ic);
103127 // Make a partial selection, we do search because the data might change.
104128 findAndSelectCandidate (ic, " 你" );
105129 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Return" ), false );
@@ -111,10 +135,7 @@ void testBasic(Instance *instance) {
111135 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" h" ), false );
112136 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" a" ), false );
113137 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
114- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
115- false );
116- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
117- false );
138+ sendControlSpace (testfrontend, ic);
118139
119140 testfrontend->call <ITestFrontend::pushCommitExpectation>(" 你hao" );
120141 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" n" ), false );
@@ -124,10 +145,7 @@ void testBasic(Instance *instance) {
124145 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
125146 // Make a partial selection, we do search because the data might change.
126147 findAndSelectCandidate (ic, " 你" );
127- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
128- false );
129- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
130- false );
148+ sendControlSpace (testfrontend, ic);
131149
132150 RawConfig config;
133151 config.setValueByPath (" SwitchInputMethodBehavior" ,
@@ -141,10 +159,7 @@ void testBasic(Instance *instance) {
141159 auto sentence =
142160 ic->inputPanel ().candidateList ()->candidate (0 ).text ().toString ();
143161 testfrontend->call <ITestFrontend::pushCommitExpectation>(sentence);
144- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
145- false );
146- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
147- false );
162+ sendControlSpace (testfrontend, ic);
148163
149164 config.setValueByPath (" SwitchInputMethodBehavior" , " Clear" );
150165 pinyin->setConfig (config);
@@ -153,10 +168,7 @@ void testBasic(Instance *instance) {
153168 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" h" ), false );
154169 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" a" ), false );
155170 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
156- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
157- false );
158- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
159- false );
171+ sendControlSpace (testfrontend, ic);
160172 });
161173}
162174
@@ -165,9 +177,9 @@ void testSelectByChar(Instance *instance) {
165177 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
166178 auto uuid =
167179 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
168-
169- testfrontend-> call <ITestFrontend::keyEvent>(uuid, Key ( " Control+space " ),
170- false );
180+ auto *ic = instance-> inputContextManager (). findByUUID (uuid);
181+ FCITX_ASSERT (ic);
182+ instance-> setCurrentInputMethod (ic, " pinyin " , true );
171183
172184 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" n" ), false );
173185 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" i" ), false );
@@ -187,8 +199,6 @@ void testSelectByChar(Instance *instance) {
187199 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" g" ), false );
188200
189201 testfrontend->call <ITestFrontend::pushCommitExpectation>(" 你好主病" );
190- auto *ic = instance->inputContextManager ().findByUUID (uuid);
191- FCITX_ASSERT (ic);
192202 findAndSelectCandidate (ic, " 你好" );
193203 auto candidateIdx = findCandidateOrDie (ic, " 公主" );
194204 ic->inputPanel ().candidateList ()->toBulkCursor ()->setGlobalCursorIndex (
@@ -204,9 +214,9 @@ void testUppercase(Instance *instance) {
204214 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
205215 auto uuid =
206216 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
207-
208- testfrontend-> call <ITestFrontend::keyEvent>(uuid, Key ( " Control+space " ),
209- false );
217+ auto *ic = instance-> inputContextManager (). findByUUID (uuid);
218+ FCITX_ASSERT (ic);
219+ instance-> setCurrentInputMethod (ic, " pinyin " , true );
210220
211221 testfrontend->call <ITestFrontend::pushCommitExpectation>(" Apple" );
212222
@@ -234,17 +244,15 @@ void testForget(Instance *instance) {
234244 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
235245 auto uuid =
236246 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
237-
238- testfrontend-> call <ITestFrontend::keyEvent>(uuid, Key ( " Control+space " ),
239- false );
247+ auto *ic = instance-> inputContextManager (). findByUUID (uuid);
248+ FCITX_ASSERT (ic);
249+ instance-> setCurrentInputMethod (ic, " pinyin " , true );
240250
241251 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" n" ), false );
242252 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" i" ), false );
243253 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" h" ), false );
244254 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" a" ), false );
245255 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
246- auto *ic = instance->inputContextManager ().findByUUID (uuid);
247- FCITX_ASSERT (ic);
248256 auto *candidateList = ic->inputPanel ().candidateList ().get ();
249257 const auto &cand = candidateList->candidate (0 );
250258 auto *actionable = candidateList->toActionable ();
@@ -265,9 +273,8 @@ void testActionInStrokeFilter(Instance *instance) {
265273 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
266274 auto *ic = instance->inputContextManager ().findByUUID (uuid);
267275 FCITX_ASSERT (ic);
276+ instance->setCurrentInputMethod (ic, " pinyin" , true );
268277
269- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
270- false );
271278 // Target ppp for 彡
272279 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" p" ), false );
273280 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" p" ), false );
@@ -298,9 +305,9 @@ void testPin(Instance *instance) {
298305 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
299306 auto uuid =
300307 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
301-
302- testfrontend-> call <ITestFrontend::keyEvent>(uuid, Key ( " Control+space " ),
303- false );
308+ auto *ic = instance-> inputContextManager (). findByUUID (uuid);
309+ FCITX_ASSERT (ic);
310+ instance-> setCurrentInputMethod (ic, " pinyin " , true );
304311
305312 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" t" ), false );
306313 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" o" ), false );
@@ -309,8 +316,6 @@ void testPin(Instance *instance) {
309316 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" y" ), false );
310317 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" i" ), false );
311318 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" n" ), false );
312- auto *ic = instance->inputContextManager ().findByUUID (uuid);
313- FCITX_ASSERT (ic);
314319 auto index1 = findCandidateOrDie (ic, " 同音" );
315320 auto index2 = findCandidateOrDie (ic, " 痛饮" );
316321 const auto oldIndex1 = index1;
@@ -374,16 +379,14 @@ void testQuickPhraseTrigger(Instance *instance) {
374379 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
375380 auto uuid =
376381 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
377-
378- testfrontend-> call <ITestFrontend::keyEvent>(uuid, Key ( " Control+space " ),
379- false );
382+ auto *ic = instance-> inputContextManager (). findByUUID (uuid);
383+ FCITX_ASSERT (ic);
384+ instance-> setCurrentInputMethod (ic, " pinyin " , true );
380385
381386 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" w" ), false );
382387 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" w" ), false );
383388 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" w" ), false );
384389 testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" ." ), false );
385- auto *ic = instance->inputContextManager ().findByUUID (uuid);
386- FCITX_ASSERT (ic);
387390 FCITX_ASSERT (ic->inputPanel ().preedit ().toString () == " www." );
388391
389392 ic->reset ();
@@ -428,14 +431,37 @@ void testQuickPhraseTrigger(Instance *instance) {
428431 });
429432}
430433
434+ void testVQuickPhraseTrigger (Instance *instance) {
435+ instance->eventDispatcher ().schedule ([instance]() {
436+ auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
437+ auto uuid =
438+ testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
439+
440+ auto *ic = instance->inputContextManager ().findByUUID (uuid);
441+ FCITX_ASSERT (ic);
442+ instance->setCurrentInputMethod (ic, " pinyin" , true );
443+
444+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" v" ), false );
445+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" ." ), false );
446+ FCITX_ASSERT (ic->inputPanel ().preedit ().toString () == " v." );
447+
448+ instance->setCurrentInputMethod (ic, " shuangpin" , true );
449+ ic->reset ();
450+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" V" ), false );
451+ testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" ." ), false );
452+ FCITX_ASSERT (ic->inputPanel ().preedit ().toString () == " V." );
453+ });
454+ }
455+
431456void testPunctuation (Instance *instance) {
432457 instance->eventDispatcher ().schedule ([instance]() {
433458 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
434459 auto uuid =
435460 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
461+ auto *ic = instance->inputContextManager ().findByUUID (uuid);
462+ FCITX_ASSERT (ic);
463+ instance->setCurrentInputMethod (ic, " pinyin" , true );
436464
437- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
438- false );
439465 testfrontend->call <ITestFrontend::pushCommitExpectation>(" 。" );
440466 FCITX_ASSERT (testfrontend->call <ITestFrontend::sendKeyEvent>(
441467 uuid, Key (" ." ), false ));
@@ -468,9 +494,10 @@ void testPunctuationPart2(Instance *instance) {
468494 auto *testfrontend = instance->addonManager ().addon (" testfrontend" );
469495 auto uuid =
470496 testfrontend->call <ITestFrontend::createInputContext>(" testapp" );
497+ auto *ic = instance->inputContextManager ().findByUUID (uuid);
498+ FCITX_ASSERT (ic);
499+ instance->setCurrentInputMethod (ic, " pinyin" , true );
471500
472- testfrontend->call <ITestFrontend::keyEvent>(uuid, Key (" Control+space" ),
473- false );
474501 FCITX_ASSERT (!testfrontend->call <ITestFrontend::sendKeyEvent>(
475502 uuid, Key (" 1" ), false ));
476503 FCITX_ASSERT (!testfrontend->call <ITestFrontend::sendKeyEvent>(
@@ -505,13 +532,15 @@ int main() {
505532 fcitx::Log::setLogRule (" default=5,pinyin=5" );
506533 Instance instance (FCITX_ARRAY_SIZE (argv), argv);
507534 instance.addonManager ().registerDefaultLoader (nullptr );
535+ setup (&instance);
508536 testBasic (&instance);
509537 testSelectByChar (&instance);
510538 testUppercase (&instance);
511539 testForget (&instance);
512540 testActionInStrokeFilter (&instance);
513541 testPin (&instance);
514542 testQuickPhraseTrigger (&instance);
543+ testVQuickPhraseTrigger (&instance);
515544 testPunctuation (&instance);
516545 instance.exec ();
517546 endTestEvent.reset ();
0 commit comments