1 /*
2  * SPDX-FileCopyrightText: 2020~2020 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #include "testdir.h"
8 #include "testfrontend_public.h"
9 #include <fcitx-utils/eventdispatcher.h>
10 #include <fcitx-utils/log.h>
11 #include <fcitx-utils/standardpath.h>
12 #include <fcitx-utils/testing.h>
13 #include <fcitx/addonmanager.h>
14 #include <fcitx/inputmethodmanager.h>
15 #include <fcitx/inputpanel.h>
16 #include <fcitx/instance.h>
17 #include <iostream>
18 #include <thread>
19 
20 using namespace fcitx;
21 
scheduleEvent(EventDispatcher * dispatcher,Instance * instance)22 void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
23     dispatcher->schedule([instance]() {
24         auto *table = instance->addonManager().addon("table", true);
25         FCITX_ASSERT(table);
26     });
27     dispatcher->schedule([dispatcher, instance]() {
28         auto defaultGroup = instance->inputMethodManager().currentGroup();
29         defaultGroup.inputMethodList().clear();
30         defaultGroup.inputMethodList().push_back(
31             InputMethodGroupItem("keyboard-us"));
32         defaultGroup.inputMethodList().push_back(InputMethodGroupItem("erbi"));
33         defaultGroup.setDefaultInputMethod("");
34         instance->inputMethodManager().setGroup(defaultGroup);
35         auto *testfrontend = instance->addonManager().addon("testfrontend");
36         testfrontend->call<ITestFrontend::pushCommitExpectation>("萌");
37         testfrontend->call<ITestFrontend::pushCommitExpectation>("豚");
38         testfrontend->call<ITestFrontend::pushCommitExpectation>("萌豚");
39         testfrontend->call<ITestFrontend::pushCommitExpectation>("萌豚");
40         testfrontend->call<ITestFrontend::pushCommitExpectation>("mbA");
41         auto uuid =
42             testfrontend->call<ITestFrontend::createInputContext>("testapp");
43         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("Control+space"),
44                                                     false);
45         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("m"), false);
46         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("b"), false);
47         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("t"), false);
48         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("d"), false);
49         auto ic = instance->inputContextManager().findByUUID(uuid);
50         // Check no candidate.
51         FCITX_ASSERT(!ic->inputPanel().candidateList());
52         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key(FcitxKey_Escape),
53                                                     false);
54 
55         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("m"), false);
56         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("b"), false);
57         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("s"), false);
58         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("d"), false);
59         ic->updateUserInterface(fcitx::UserInterfaceComponent::InputPanel,
60                                 true);
61         // This t trigger auto commit.
62         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("t"), false);
63         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("d"), false);
64         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("k"), false);
65         // This comma trigger only match commit.
66         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key(","), false);
67 
68         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("m"), false);
69         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("b"), false);
70         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("t"), false);
71         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("d"), false);
72         // Auto phrase does not do auto select.
73         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("1"), false);
74 
75         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("m"), false);
76         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("b"), false);
77         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("t"), false);
78         // This trigger auto select because it's now user phrase.
79         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("d"), false);
80         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("m"), false);
81         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("b"), false);
82         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("A"), false);
83         testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("Return"), false);
84 
85         dispatcher->schedule([dispatcher, instance]() {
86             dispatcher->detach();
87             instance->exit();
88         });
89     });
90 }
91 
runInstance()92 void runInstance() {}
93 
main()94 int main() {
95     setupTestingEnvironment(TESTING_BINARY_DIR,
96                             {TESTING_BINARY_DIR "/modules/pinyinhelper",
97                              TESTING_BINARY_DIR "/modules/punctuation",
98                              TESTING_BINARY_DIR "/im/table"},
99                             {TESTING_BINARY_DIR "/test",
100                              TESTING_BINARY_DIR "/modules",
101                              StandardPath::fcitxPath("pkgdatadir")});
102     // fcitx::Log::setLogRule("default=5,table=5,libime-table=5");
103     char arg0[] = "testtable";
104     char arg1[] = "--disable=all";
105     char arg2[] = "--enable=testim,testfrontend,table,quickphrase,punctuation,"
106                   "pinyinhelper";
107     char *argv[] = {arg0, arg1, arg2};
108     Instance instance(FCITX_ARRAY_SIZE(argv), argv);
109     instance.addonManager().registerDefaultLoader(nullptr);
110     EventDispatcher dispatcher;
111     dispatcher.attach(&instance.eventLoop());
112     std::thread thread(scheduleEvent, &dispatcher, &instance);
113     instance.exec();
114     thread.join();
115 
116     return 0;
117 }
118