File: testlua.cpp

package info (click to toggle)
fcitx5-lua 5.0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 380 kB
  • sloc: cpp: 1,229; sh: 21; makefile: 3
file content (156 lines) | stat: -rw-r--r-- 6,076 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * SPDX-FileCopyrightText: 2020~2020 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */
#include "luaaddon_public.h"
#include "testdir.h"
#include "testfrontend_public.h"
#include "testim_public.h"
#include <cassert>
#include <cstdint>
#include <cstring>
#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/standardpaths.h>
#include <fcitx-utils/testing.h>
#include <fcitx/addonmanager.h>
#include <fcitx/event.h>
#include <fcitx/inputmethodgroup.h>
#include <fcitx/inputmethodmanager.h>
#include <fcitx/instance.h>
#include <string>
#include <thread>

using namespace fcitx;

void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
    dispatcher->schedule([instance]() {
        auto *luaaddonloader =
            instance->addonManager().addon("luaaddonloader", true);
        FCITX_ASSERT(luaaddonloader);
        auto *luaaddon = instance->addonManager().addon("testlua");
        FCITX_ASSERT(luaaddon);
        auto *imeapi = instance->addonManager().addon("imeapi");
        FCITX_ASSERT(imeapi);
    });
    dispatcher->schedule([dispatcher, instance]() {
        // Setup the input method group with two input method
        auto groupName = instance->inputMethodManager().currentGroup();
        InputMethodGroup group(groupName);
        group.inputMethodList().push_back(InputMethodGroupItem("keyboard-us"));
        group.inputMethodList().push_back(InputMethodGroupItem("testim"));
        group.setDefaultInputMethod("testim");
        instance->inputMethodManager().setGroup(group);

        auto *testfrontend = instance->addonManager().addon("testfrontend");
        auto *testim = instance->addonManager().addon("testim");
        auto *luaaddon = instance->addonManager().addon("testlua");
        testim->call<ITestIM::setHandler>(
            [](const InputMethodEntry &, KeyEvent &keyEvent) {
                if (keyEvent.key().states() != KeyState::NoState ||
                    keyEvent.isRelease()) {
                    return;
                }
                auto s = Key::keySymToUTF8(keyEvent.key().sym());
                if (!s.empty()) {
                    keyEvent.inputContext()->commitString(s);
                    keyEvent.filterAndAccept();
                }
            });
        auto uuid =
            testfrontend->call<ITestFrontend::createInputContext>("testapp");
        auto *ic = instance->inputContextManager().findByUUID(uuid);
        FCITX_ASSERT(ic);

        // Test with the converter in test.lua
        testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
        testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("b"), false);
        testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("c"), false);
        testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("d"), false);

        // Test lua currentInputMethod
        auto ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(
            ic, "testInputMethod", RawConfig{});
        FCITX_INFO() << ret;
        assert(ret.value() == "keyboard-us");

        testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("Control+space"),
                                                    false);

        // Test lua currentProgram
        ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(ic, "testProgram",
                                                           RawConfig{});
        FCITX_INFO() << ret;
        assert(ret.value() == "testapp");

        // Test lua currentInputMethod after ctrl space
        ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(
            ic, "testInputMethod", RawConfig{});
        FCITX_INFO() << ret;
        assert(ret.value() == "testim");

        // Test with complex value with invokeLuaFunction
        FCITX_ASSERT(luaaddon);
        RawConfig config;
        config["A"].setValue("5");
        config["A"]["Q"].setValue("4");
        FCITX_INFO() << config;
        ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(ic, "testInvoke",
                                                           config);
        FCITX_INFO() << ret;
        assert(ret["B"]["E"]["F"].value() == "7");
        RawConfig strConfig;
        strConfig.setValue("ABC");
        ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(ic, "testInvoke",
                                                           strConfig);
        assert(ret.value() == "DEF");
        FCITX_INFO() << ret;

        std::string testString = "ABC测试𐐒DEF";
        strConfig.setValue(testString);
        ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(
            ic, "testUtf16Conversion", strConfig);
        uint16_t data[] = {0x41,   0x42, 0x43, 0x6d4b, 0x8bd5, 0xd801,
                           0xdc12, 0x44, 0x45, 0x46,   0x0};
        FCITX_ASSERT(ret.value().size() == sizeof(data));
        FCITX_ASSERT(memcmp(ret.value().data(), data, sizeof(data)) == 0);

        strConfig.setValue(ret.value());
        ret = luaaddon->call<ILuaAddon::invokeLuaFunction>(
            ic, "testUtf8Conversion", strConfig);
        FCITX_ASSERT(ret.value() == testString) << ret;

        dispatcher->detach();
        instance->exit();
    });
}

void runInstance() {}

int main() {
    setupTestingEnvironmentPath(
        TESTING_BINARY_DIR, {"bin"},
        {"test", TESTING_SOURCE_DIR "/test",
         StandardPaths::fcitxPath("pkgdatadir", "testing")});

    fcitx::Log::setLogRule("default=5,lua=5");
    char arg0[] = "testlua";
    char arg1[] = "--disable=all";
    char arg2[] = "--enable=testim,testfrontend,luaaddonloader,imeapi,testlua";
    char *argv[] = {arg0, arg1, arg2};
    Instance instance(FCITX_ARRAY_SIZE(argv), argv);
    instance.addonManager().registerDefaultLoader(nullptr);
    EventDispatcher dispatcher;
    dispatcher.attach(&instance.eventLoop());
    std::thread thread(scheduleEvent, &dispatcher, &instance);
    instance.exec();
    thread.join();

    return 0;
}