File: testfrontend.cpp

package info (click to toggle)
fcitx5 5.1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,356 kB
  • sloc: cpp: 75,670; sh: 1,770; xml: 1,545; python: 1,052; ansic: 71; makefile: 11
file content (109 lines) | stat: -rw-r--r-- 3,199 bytes parent folder | download | duplicates (3)
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
/*
 * SPDX-FileCopyrightText: 2020-2020 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */
#include "testfrontend.h"
#include "fcitx/addonfactory.h"
#include "fcitx/addonmanager.h"
#include "fcitx/inputcontextmanager.h"
#include "fcitx/inputpanel.h"

namespace fcitx {

class TestInputContext : public InputContext {
public:
    TestInputContext(TestFrontend *frontend,
                     InputContextManager &inputContextManager,
                     const std::string &program)
        : InputContext(inputContextManager, program), frontend_(frontend) {
        created();
    }
    ~TestInputContext() { destroy(); }

    const char *frontend() const override { return "testfrontend"; }

    void commitStringImpl(const std::string &text) override {
        FCITX_INFO() << "Commit: " << text;
        frontend_->commitString(text);
    }
    void forwardKeyImpl(const ForwardKeyEvent &key) override {
        FCITX_INFO() << "ForwardKey: " << key.key();
    }
    void deleteSurroundingTextImpl(int offset, unsigned int size) override {
        FCITX_INFO() << "DeleteSurrounding: " << offset << " " << size;
    }

    void updatePreeditImpl() override {
        FCITX_INFO() << "Update preedit: "
                     << inputPanel().clientPreedit().toString();
    }

private:
    TestFrontend *frontend_;
};

TestFrontend::TestFrontend(Instance *instance) : instance_(instance) {}

TestFrontend::~TestFrontend() {
    FCITX_ASSERT(commitExpectation_.empty()) << commitExpectation_;
}

ICUUID
TestFrontend::createInputContext(const std::string &program) {
    auto *ic =
        new TestInputContext(this, instance_->inputContextManager(), program);
    return ic->uuid();
}

void TestFrontend::destroyInputContext(ICUUID uuid) {
    auto *ic = instance_->inputContextManager().findByUUID(uuid);
    delete ic;
}

bool TestFrontend::sendKeyEvent(ICUUID uuid, const Key &key, bool isRelease) {
    auto *ic = instance_->inputContextManager().findByUUID(uuid);
    if (!ic) {
        return false;
    }
    if (!ic->hasFocus()) {
        ic->focusIn();
    }
    KeyEvent keyEvent(ic, key, isRelease);
    auto result = ic->keyEvent(keyEvent);
    FCITX_INFO() << "KeyEvent key: " << key.toString()
                 << " isRelease: " << isRelease
                 << " accepted: " << keyEvent.accepted();
    return result;
}

void TestFrontend::keyEvent(ICUUID uuid, const Key &key, bool isRelease) {
    sendKeyEvent(uuid, key, isRelease);
}

void TestFrontend::commitString(const std::string &expect) {
    if (!checkExpectation_) {
        return;
    }
    FCITX_ASSERT(!commitExpectation_.empty() &&
                 expect == commitExpectation_.front())
        << "commitString: " << expect;
    commitExpectation_.pop_front();
}

void TestFrontend::pushCommitExpectation(std::string expect) {
    checkExpectation_ = true;
    commitExpectation_.push_back(std::move(expect));
}

class TestFrontendFactory : public AddonFactory {
public:
    AddonInstance *create(AddonManager *manager) override {
        return new TestFrontend(manager->instance());
    }
};

} // namespace fcitx

FCITX_ADDON_FACTORY_V2(testfrontend, fcitx::TestFrontendFactory);