summaryrefslogtreecommitdiffstats
path: root/tests/autocorrect/test_autocorrect.cpp
blob: 509c1c9ea410dbd1f67360f3d5f04c207d6ca19b (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
// Copyright 2021 Christopher Courtney, aka Drashna Jael're  (@drashna) <drashna@live.com>
// SPDX-License-Identifier: GPL-2.0-or-later

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

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::InSequence;

class AutoCorrect : public TestFixture {
   public:
    void SetUp() override {
        autocorrect_enable();
    }
    // Convenience function to tap `key`.
    void TapKey(KeymapKey key) {
        key.press();
        run_one_scan_loop();
        key.release();
        run_one_scan_loop();
    }

    // Taps in order each key in `keys`.
    template <typename... Ts>
    void TapKeys(Ts... keys) {
        for (KeymapKey key : {keys...}) {
            TapKey(key);
        }
    }
};

// Test that verifies enable/disable/toggling works
TEST_F(AutoCorrect, OnOffToggle) {
    TestDriver driver;

    EXPECT_EQ(autocorrect_is_enabled(), true);

    autocorrect_disable();
    EXPECT_EQ(autocorrect_is_enabled(), false);
    autocorrect_disable();
    EXPECT_EQ(autocorrect_is_enabled(), false);

    autocorrect_enable();
    EXPECT_EQ(autocorrect_is_enabled(), true);
    autocorrect_enable();
    EXPECT_EQ(autocorrect_is_enabled(), true);

    autocorrect_toggle();
    EXPECT_EQ(autocorrect_is_enabled(), false);
    autocorrect_toggle();
    EXPECT_EQ(autocorrect_is_enabled(), true);

    testing::Mock::VerifyAndClearExpectations(&driver);
}

// Test that typing "fales" autocorrects to "false"
TEST_F(AutoCorrect, fales_to_false_autocorrection) {
    TestDriver driver;
    auto       key_f = KeymapKey(0, 0, 0, KC_F);
    auto       key_a = KeymapKey(0, 1, 0, KC_A);
    auto       key_l = KeymapKey(0, 2, 0, KC_L);
    auto       key_e = KeymapKey(0, 3, 0, KC_E);
    auto       key_s = KeymapKey(0, 4, 0, KC_S);

    set_keymap({key_f, key_a, key_l, key_e, key_s});

    // Allow any number of empty reports.
    EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(AnyNumber());
    { // Expect the following reports in this order.
        InSequence s;
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_F)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_L)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_BACKSPACE)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_S)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E)));
    }

    TapKeys(key_f, key_a, key_l, key_e, key_s);

    testing::Mock::VerifyAndClearExpectations(&driver);
}

// Test that typing "fales" doesn't autocorrect if disabled
TEST_F(AutoCorrect, fales_disabled_autocorrect) {
    TestDriver driver;
    auto       key_f = KeymapKey(0, 0, 0, KC_F);
    auto       key_a = KeymapKey(0, 1, 0, KC_A);
    auto       key_l = KeymapKey(0, 2, 0, KC_L);
    auto       key_e = KeymapKey(0, 3, 0, KC_E);
    auto       key_s = KeymapKey(0, 4, 0, KC_S);

    set_keymap({key_f, key_a, key_l, key_e, key_s});

    // Allow any number of empty reports.
    EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(AnyNumber());
    { // Expect the following reports in this order.
        InSequence s;
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_F)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_L)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_S)));
    }

    autocorrect_disable();
    TapKeys(key_f, key_a, key_l, key_e, key_s);
    autocorrect_enable();

    testing::Mock::VerifyAndClearExpectations(&driver);
}

// Test that typing "falsify" doesn't autocorrect if disabled
TEST_F(AutoCorrect, falsify_should_not_autocorrect) {
    TestDriver driver;
    auto       key_f = KeymapKey(0, 0, 0, KC_F);
    auto       key_a = KeymapKey(0, 1, 0, KC_A);
    auto       key_l = KeymapKey(0, 2, 0, KC_L);
    auto       key_s = KeymapKey(0, 3, 0, KC_S);
    auto       key_i = KeymapKey(0, 4, 0, KC_I);
    auto       key_y = KeymapKey(0, 5, 0, KC_Y);

    set_keymap({key_f, key_a, key_l, key_s, key_i, key_y});

    // Allow any number of empty reports.
    EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(AnyNumber());
    { // Expect the following reports in this order.
        InSequence s;
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_F)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_L)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_S)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_I)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_F)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_Y)));
    }

    TapKeys(key_f, key_a, key_l, key_s, key_i, key_f, key_y);

    testing::Mock::VerifyAndClearExpectations(&driver);
}

// Test that  typing "ture" autocorrect to "true"
TEST_F(AutoCorrect, ture_to_true_autocorrect) {
    TestDriver driver;
    auto       key_t_code = KeymapKey(0, 0, 0, KC_T);
    auto       key_r      = KeymapKey(0, 1, 0, KC_R);
    auto       key_u      = KeymapKey(0, 2, 0, KC_U);
    auto       key_e      = KeymapKey(0, 3, 0, KC_E);
    auto       key_space  = KeymapKey(0, 4, 0, KC_SPACE);

    set_keymap({key_t_code, key_r, key_u, key_e, key_space});

    // Allow any number of empty reports.
    EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(AnyNumber());
    { // Expect the following reports in this order.
        InSequence s;
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_SPACE)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_T)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_U)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_R)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_BACKSPACE))).Times(2);
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_R)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_U)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E)));
    }

    TapKeys(key_space, key_t_code, key_u, key_r, key_e);

    testing::Mock::VerifyAndClearExpectations(&driver);
}

// Test that  typing "overture" does not autocorrect
TEST_F(AutoCorrect, overture_should_not_autocorrect) {
    TestDriver driver;
    auto       key_t_code = KeymapKey(0, 0, 0, KC_T);
    auto       key_r      = KeymapKey(0, 1, 0, KC_R);
    auto       key_u      = KeymapKey(0, 2, 0, KC_U);
    auto       key_e      = KeymapKey(0, 3, 0, KC_E);
    auto       key_o      = KeymapKey(0, 4, 0, KC_O);
    auto       key_v      = KeymapKey(0, 5, 0, KC_V);

    set_keymap({key_t_code, key_r, key_u, key_e, key_o, key_v});

    // Allow any number of empty reports.
    EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(AnyNumber());
    { // Expect the following reports in this order.
        InSequence s;
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_O)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_V)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_R)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_T)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_U)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_R)));
        EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E)));
    }

    TapKeys(key_o, key_v, key_e, key_r, key_t_code, key_u, key_r, key_e);

    testing::Mock::VerifyAndClearExpectations(&driver);
}