Repository navigation
Expand file tree
/
Copy pathinput_router_ut.cpp
More file actions
167 lines (142 loc) · 4.65 KB
/
Copy pathinput_router_ut.cpp
File metadata and controls
167 lines (142 loc) · 4.65 KB
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
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "input_router.h"
#include "composer.h"
#include "input_sink.h"
#include <std/mem/obj_pool.h>
#include <std/tst/ut.h>
using namespace stl;
namespace {
struct CaptureSink final: public InputSink {
bool key(const KeyInput& input) override;
bool text(const TextInput& input) override;
bool pointerMotion(const PointerMotionInput& input) override;
bool pointerButton(const PointerButtonInput& input) override;
bool scroll(const ScrollInput& input) override;
void focus(bool focused) override;
void pointerPresence(bool present) override;
void flush() override;
bool consume = false;
bool unlinkOnKey = false;
KeyInput lastKey;
TextInput lastText;
PointerMotionInput lastMotion;
PointerButtonInput lastButton;
ScrollInput lastScroll;
size_t keys = 0;
size_t texts = 0;
size_t motions = 0;
size_t buttons = 0;
size_t scrolls = 0;
size_t focuses = 0;
size_t presences = 0;
size_t flushes = 0;
bool focused = false;
bool present = false;
};
}
bool CaptureSink::key(const KeyInput& input) {
lastKey = input;
++keys;
if (unlinkOnKey) {
unlink();
}
return consume;
}
bool CaptureSink::text(const TextInput& input) {
lastText = input;
++texts;
return consume;
}
bool CaptureSink::pointerMotion(const PointerMotionInput& input) {
lastMotion = input;
++motions;
return consume;
}
bool CaptureSink::pointerButton(const PointerButtonInput& input) {
lastButton = input;
++buttons;
return consume;
}
bool CaptureSink::scroll(const ScrollInput& input) {
lastScroll = input;
++scrolls;
return consume;
}
void CaptureSink::focus(bool focused_) {
focused = focused_;
++focuses;
}
void CaptureSink::pointerPresence(bool present_) {
present = present_;
++presences;
}
void CaptureSink::flush() {
++flushes;
}
STD_TEST_SUITE(InputRouter) {
STD_TEST(StopsAtFirstConsumingSink) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
CaptureSink first;
CaptureSink second;
first.consume = true;
composer.inputSinks.pushBack(&first);
composer.inputSinks.pushBack(&second);
STD_INSIST(composer.input->key({InputKey::Enter}));
STD_INSIST(first.keys == 1);
STD_INSIST(second.keys == 0);
}
STD_TEST(ContinuesAfterSinkRemovesItself) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
CaptureSink removing;
CaptureSink trailing;
removing.unlinkOnKey = true;
composer.inputSinks.pushBack(&removing);
composer.inputSinks.pushBack(&trailing);
STD_INSIST(!composer.input->key({InputKey::Enter}));
STD_INSIST(removing.keys == 1);
STD_INSIST(trailing.keys == 1);
composer.input->key({InputKey::Enter});
STD_INSIST(removing.keys == 1);
STD_INSIST(trailing.keys == 2);
}
STD_TEST(RoutesEveryInputShape) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
CaptureSink sink;
composer.inputSinks.pushBack(&sink);
composer.input->text({0x20ac, InputAlt});
composer.input->pointerMotion({12, 34, InputShift});
composer.input->pointerButton({PointerButton::Middle, true, 12, 34, InputControl, 1.5});
composer.input->scroll({1.25, -2.5, 12, 34, InputSuper});
STD_INSIST(sink.texts == 1);
STD_INSIST(sink.lastText.codepoint == 0x20ac);
STD_INSIST(sink.motions == 1);
STD_INSIST(sink.lastMotion.pixelY == 34);
STD_INSIST(sink.buttons == 1);
STD_INSIST(sink.lastButton.button == PointerButton::Middle);
STD_INSIST(sink.scrolls == 1);
STD_INSIST(sink.lastScroll.y == -2.5);
}
STD_TEST(BroadcastsStateAndFlushEvents) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
CaptureSink first;
CaptureSink second;
composer.inputSinks.pushBack(&first);
composer.inputSinks.pushBack(&second);
composer.input->focus(true);
composer.input->pointerPresence(true);
composer.input->flush();
STD_INSIST(first.focuses == 1 && second.focuses == 1);
STD_INSIST(first.focused && second.focused);
STD_INSIST(first.presences == 1 && second.presences == 1);
STD_INSIST(first.present && second.present);
STD_INSIST(first.flushes == 1 && second.flushes == 1);
}
}