summaryrefslogtreecommitdiff
path: root/tests/unicode/unicode_ucis/test_unicode_ucis.cpp
blob: acc6329f857b4272bc982fd58297b5a7b66a1fc2 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2023 QMK
// SPDX-License-Identifier: GPL-2.0-or-later

#include "keyboard_report_util.hpp"
#include "keycode.h"
#include "test_common.hpp"
#include "test_keymap_key.hpp"

using testing::_;

// clang-format off
const ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE(
    UCIS_SYM("qmk", 0x03A8) // Ψ
);
// clang-format on

class UnicodeUCIS : public TestFixture {};

TEST_F(UnicodeUCIS, matches_sequence) {
    TestDriver driver;

    auto key_q     = KeymapKey(0, 0, 0, KC_Q);
    auto key_m     = KeymapKey(0, 1, 0, KC_M);
    auto key_k     = KeymapKey(0, 2, 0, KC_K);
    auto key_enter = KeymapKey(0, 3, 0, KC_ENTER);

    set_keymap({key_q, key_m, key_k, key_enter});

    EXPECT_UNICODE(driver, 0x2328); // ⌨
    ucis_start();

    EXPECT_EQ(ucis_active(), true);
    EXPECT_EQ(ucis_count(), 0);

    EXPECT_REPORT(driver, (KC_Q));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_q);
    EXPECT_EQ(ucis_count(), 1);

    EXPECT_REPORT(driver, (KC_M));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_m);
    EXPECT_EQ(ucis_count(), 2);

    EXPECT_REPORT(driver, (KC_K));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_k);
    EXPECT_EQ(ucis_count(), 3);

    EXPECT_REPORT(driver, (KC_BACKSPACE)).Times(4);
    EXPECT_EMPTY_REPORT(driver).Times(4);
    EXPECT_UNICODE(driver, 0x03A8);
    tap_key(key_enter);

    EXPECT_EQ(ucis_active(), false);

    VERIFY_AND_CLEAR(driver);
}

TEST_F(UnicodeUCIS, cancels_sequence) {
    TestDriver driver;

    auto key_q      = KeymapKey(0, 0, 0, KC_Q);
    auto key_m      = KeymapKey(0, 1, 0, KC_M);
    auto key_k      = KeymapKey(0, 2, 0, KC_K);
    auto key_escape = KeymapKey(0, 3, 0, KC_ESCAPE);

    set_keymap({key_q, key_m, key_k, key_escape});

    EXPECT_UNICODE(driver, 0x2328); // ⌨
    ucis_start();

    EXPECT_EQ(ucis_active(), true);
    EXPECT_EQ(ucis_count(), 0);

    EXPECT_REPORT(driver, (KC_Q));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_q);
    EXPECT_EQ(ucis_count(), 1);

    EXPECT_REPORT(driver, (KC_M));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_m);
    EXPECT_EQ(ucis_count(), 2);

    EXPECT_REPORT(driver, (KC_K));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_k);
    EXPECT_EQ(ucis_count(), 3);

    EXPECT_NO_REPORT(driver);
    tap_key(key_escape);

    EXPECT_EQ(ucis_active(), false);

    VERIFY_AND_CLEAR(driver);
}

TEST_F(UnicodeUCIS, matches_sequence_with_corrected_typo) {
    TestDriver driver;

    auto key_q         = KeymapKey(0, 0, 0, KC_Q);
    auto key_m         = KeymapKey(0, 1, 0, KC_M);
    auto key_j         = KeymapKey(0, 2, 0, KC_J);
    auto key_k         = KeymapKey(0, 3, 0, KC_K);
    auto key_backspace = KeymapKey(0, 4, 0, KC_BACKSPACE);
    auto key_enter     = KeymapKey(0, 5, 0, KC_ENTER);

    set_keymap({key_q, key_m, key_j, key_k, key_backspace, key_enter});

    EXPECT_UNICODE(driver, 0x2328); // ⌨
    ucis_start();

    EXPECT_EQ(ucis_active(), true);
    EXPECT_EQ(ucis_count(), 0);

    EXPECT_REPORT(driver, (KC_Q));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_q);
    EXPECT_EQ(ucis_count(), 1);

    EXPECT_REPORT(driver, (KC_M));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_m);
    EXPECT_EQ(ucis_count(), 2);

    EXPECT_REPORT(driver, (KC_J));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_j);
    EXPECT_EQ(ucis_count(), 3);

    EXPECT_REPORT(driver, (KC_BACKSPACE));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_backspace);
    EXPECT_EQ(ucis_count(), 2);

    EXPECT_REPORT(driver, (KC_K));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_k);
    EXPECT_EQ(ucis_count(), 3);

    EXPECT_REPORT(driver, (KC_BACKSPACE)).Times(4);
    EXPECT_EMPTY_REPORT(driver).Times(4);
    EXPECT_UNICODE(driver, 0x03A8);
    tap_key(key_enter);

    EXPECT_EQ(ucis_active(), false);

    VERIFY_AND_CLEAR(driver);
}

TEST_F(UnicodeUCIS, does_not_match_longer_sequence) {
    TestDriver driver;

    auto key_q     = KeymapKey(0, 0, 0, KC_Q);
    auto key_m     = KeymapKey(0, 1, 0, KC_M);
    auto key_k     = KeymapKey(0, 2, 0, KC_K);
    auto key_enter = KeymapKey(0, 3, 0, KC_ENTER);

    set_keymap({key_q, key_m, key_k, key_enter});

    EXPECT_UNICODE(driver, 0x2328); // ⌨
    ucis_start();

    EXPECT_EQ(ucis_active(), true);
    EXPECT_EQ(ucis_count(), 0);

    EXPECT_REPORT(driver, (KC_Q));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_q);
    EXPECT_EQ(ucis_count(), 1);

    EXPECT_REPORT(driver, (KC_M));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_m);
    EXPECT_EQ(ucis_count(), 2);

    EXPECT_REPORT(driver, (KC_K));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_k);
    EXPECT_EQ(ucis_count(), 3);

    EXPECT_REPORT(driver, (KC_K));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_k);
    EXPECT_EQ(ucis_count(), 4);

    EXPECT_NO_REPORT(driver);
    tap_key(key_enter);

    EXPECT_EQ(ucis_active(), false);

    VERIFY_AND_CLEAR(driver);
}

TEST_F(UnicodeUCIS, does_not_match_shorter_sequence) {
    TestDriver driver;

    auto key_q     = KeymapKey(0, 0, 0, KC_Q);
    auto key_enter = KeymapKey(0, 1, 0, KC_ENTER);

    set_keymap({key_q, key_enter});

    EXPECT_UNICODE(driver, 0x2328); // ⌨
    ucis_start();

    EXPECT_EQ(ucis_active(), true);
    EXPECT_EQ(ucis_count(), 0);

    EXPECT_REPORT(driver, (KC_Q));
    EXPECT_EMPTY_REPORT(driver);
    tap_key(key_q);
    EXPECT_EQ(ucis_count(), 1);

    EXPECT_NO_REPORT(driver);
    tap_key(key_enter);

    EXPECT_EQ(ucis_active(), false);

    VERIFY_AND_CLEAR(driver);
}