aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_keymap.cpp
blob: 0fb4f57fab0f9083846033125507cbb0cb0f6bee (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
 * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <QtTest>
#include <QTemporaryDir>
#include <QSettings>
#include "keymap.h"

class TestKeyMap : public QObject
{
    Q_OBJECT
private slots:
    void defaultsAreLoaded();
    void iniOverridesDefault();
    void iniAddsNewBinding();
    void chordSequenceParses();
    void unknownActionIsReported();
    void invalidSequenceIsReported();
    void collidingOverridesAreReported();
    void bareCapitalMatchesShiftedPress();
    void userBindingWinsOverDefaultInMenus();
    void defaultsDoNotCollide();
    void everyDefaultIsAKnownAction();
    void everyDefaultParses();
};

void TestKeyMap::everyDefaultParses()
{
    // A default that does not parse is a dead binding, the failure mode
    // bareCapitalMatchesShiftedPress() covers for user-written keys.
    //
    // This deliberately does NOT try to decide which keys a keyboard can
    // deliver. Whether a symbol needs Shift is a layout property, not a Qt
    // one: Ctrl++ is exactly what the '+' key emits on an Italian layout and
    // is unreachable on a US one, and QTest::keyClick() cannot reproduce
    // either faithfully. A test asserting reachability from synthetic input
    // would encode one layout's habits as a rule for all of them.
    for (const auto &binding : KeyMap::defaultBindings()) {
        const QKeySequence sequence = KeyMap::normalizeSequence(binding.first);
        QVERIFY2(!sequence.isEmpty(),
                 qPrintable(QStringLiteral("default '%1' for %2 does not parse")
                                .arg(binding.first, binding.second)));
    }
}

void TestKeyMap::defaultsAreLoaded()
{
    KeyMap map;
    map.loadDefaults();
    QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("Ctrl+J"))),
             QStringLiteral("next_thread"));
    QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("Ctrl+E"))),
             QStringLiteral("archive"));
}

void TestKeyMap::bareCapitalMatchesShiftedPress()
{
    // Typing a capital produces Shift+<key>, but QKeySequence::fromString()
    // discards the case of a bare letter: "N" and "n" both parse to plain
    // Key_N, which no keypress can ever produce. A user who writes "N = flag"
    // would get a binding that silently never fires. Normalizing a bare
    // capital to Shift+<key> is what they meant.
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("N"), QStringLiteral("flag"));
        s.endGroup();
    }

    KeyMap map;
    map.loadDefaults();
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    // The sequence a real Shift+N keypress produces.
    QKeyEvent press(QEvent::KeyPress, Qt::Key_N, Qt::ShiftModifier);
    QCOMPARE(map.actionFor(QKeySequence(press.keyCombination())),
             QStringLiteral("flag"));

    // A lowercase binding stays unshifted, so the two remain distinguishable.
    QVERIFY(map.warnings().isEmpty());

    // "y" and "Y" are two different keys, not a collision: the second would
    // have silently displaced the first before normalization.
    QTemporaryDir caseDir;
    const QString casePath = caseDir.filePath(QStringLiteral("case.conf"));
    {
        QSettings s(casePath, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("y"), QStringLiteral("archive"));
        s.setValue(QStringLiteral("Y"), QStringLiteral("delete"));
        s.endGroup();
    }
    KeyMap caseMap;
    QSettings caseSettings(casePath, QSettings::IniFormat);
    caseMap.loadOverrides(caseSettings);

    QKeyEvent lower(QEvent::KeyPress, Qt::Key_Y, Qt::NoModifier);
    QKeyEvent upper(QEvent::KeyPress, Qt::Key_Y, Qt::ShiftModifier);
    QCOMPARE(caseMap.actionFor(QKeySequence(lower.keyCombination())),
             QStringLiteral("archive"));
    QCOMPARE(caseMap.actionFor(QKeySequence(upper.keyCombination())),
             QStringLiteral("delete"));
    QVERIFY(caseMap.warnings().isEmpty());
}

void TestKeyMap::userBindingWinsOverDefaultInMenus()
{
    // loadOverrides() adds a binding without removing the default, so two
    // sequences reach 'archive'. sequenceFor() is what the menus and the
    // shortcut reference display: it must show the user's, not the built-in
    // one they were trying to replace.
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("Ctrl+Alt+A"), QStringLiteral("archive"));
        s.endGroup();
    }

    KeyMap map;
    map.loadDefaults();
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    QCOMPARE(map.sequenceFor(QStringLiteral("archive")),
             QKeySequence(QStringLiteral("Ctrl+Alt+A")));

    // The default still fires; it is only no longer the advertised one.
    QCOMPARE(map.actionFor(KeyMap::defaultSequenceFor(QStringLiteral("archive"))),
             QStringLiteral("archive"));

    // An action the user left alone still shows its default.
    QCOMPARE(map.sequenceFor(QStringLiteral("delete")),
             KeyMap::defaultSequenceFor(QStringLiteral("delete")));
}

void TestKeyMap::defaultsDoNotCollide()
{
    // Two defaults on one sequence means one of them is unreachable, and the
    // QHash would silently keep whichever was inserted last.
    KeyMap map;
    map.loadDefaults();

    QSet<QString> actions;
    for (const QString &action : KeyMap::knownActions()) {
        const QKeySequence seq = map.defaultSequenceFor(action);
        if (seq.isEmpty())
            continue;  // Not every action carries a default.
        QVERIFY2(map.actionFor(seq) == action,
                 qPrintable(QStringLiteral("default '%1' for '%2' resolves to '%3'")
                                .arg(seq.toString(), action, map.actionFor(seq))));
        actions.insert(action);
    }
    QVERIFY(!actions.isEmpty());
}

void TestKeyMap::everyDefaultIsAKnownAction()
{
    // A default bound to a name loadOverrides() would reject as unknown.
    KeyMap map;
    map.loadDefaults();
    const QStringList known = KeyMap::knownActions();
    for (const QString &action : known)
        QVERIFY(!action.isEmpty());

    for (const QString &action : map.defaultActions()) {
        QVERIFY2(known.contains(action),
                 qPrintable(QStringLiteral("default binds unknown action '%1'")
                                .arg(action)));
    }
}

void TestKeyMap::iniOverridesDefault()
{
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("Ctrl+J"), QStringLiteral("archive"));
        s.endGroup();
    }

    KeyMap map;
    map.loadDefaults();
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("Ctrl+J"))),
             QStringLiteral("archive"));
    // An untouched default survives.
    QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("Ctrl+K"))),
             QStringLiteral("prev_thread"));
}

void TestKeyMap::iniAddsNewBinding()
{
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("Ctrl+Shift+A"), QStringLiteral("archive"));
        s.endGroup();
    }

    KeyMap map;
    map.loadDefaults();
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("Ctrl+Shift+A"))),
             QStringLiteral("archive"));
}

void TestKeyMap::chordSequenceParses()
{
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("g,i"), QStringLiteral("focus_query"));
        s.endGroup();
    }

    KeyMap map;
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    const QKeySequence chord = QKeySequence::fromString(QStringLiteral("g,i"));
    QCOMPARE(chord.count(), 2);
    QCOMPARE(map.actionFor(chord), QStringLiteral("focus_query"));
}

void TestKeyMap::unknownActionIsReported()
{
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("z"), QStringLiteral("no_such_action"));
        s.endGroup();
    }

    KeyMap map;
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    // Reported, not fatal, and not bound.
    QCOMPARE(map.warnings().size(), 1);
    QVERIFY(map.warnings().first().contains(QStringLiteral("no_such_action")));
    QVERIFY(map.actionFor(QKeySequence(QStringLiteral("z"))).isEmpty());
}

void TestKeyMap::invalidSequenceIsReported()
{
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("t.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("NotAKey++"), QStringLiteral("archive"));
        s.endGroup();
    }

    KeyMap map;
    QSettings s(path, QSettings::IniFormat);
    map.loadOverrides(s);

    QCOMPARE(map.warnings().size(), 1);
}

void TestKeyMap::collidingOverridesAreReported()
{
    // Two spellings of one sequence. "Ctrl+Y" and "ctrl+y" parse identically,
    // so binding both in [keys] is a genuine collision that must not silently
    // drop one.
    //
    // Note "y" and "Y" are NOT a collision any more: normalizeSequence()
    // rewrites a bare capital to Shift+Y, which is the key a user actually
    // presses, leaving the two distinct. Before that they both folded to
    // plain Key_Y and one was lost.
    {
        QTemporaryDir dir;
        const QString path = dir.filePath(QStringLiteral("t.conf"));
        {
            QSettings s(path, QSettings::IniFormat);
            s.beginGroup(QStringLiteral("keys"));
            s.setValue(QStringLiteral("Ctrl+Y"), QStringLiteral("archive"));
            s.setValue(QStringLiteral("ctrl+y"), QStringLiteral("delete"));
            s.endGroup();
        }

        KeyMap map;
        QSettings s(path, QSettings::IniFormat);
        map.loadOverrides(s);

        QCOMPARE(map.warnings().size(), 1);
        QVERIFY(map.warnings().first().contains(QStringLiteral("archive")));
        QVERIFY(map.warnings().first().contains(QStringLiteral("delete")));
    }

    // Overriding a default is not a collision: loadDefaults() puts 'j' in
    // the map first, then the override pass rebinds the same 'j'. That must
    // stay silent (regression check for iniOverridesDefault's scenario).
    {
        QTemporaryDir dir;
        const QString path = dir.filePath(QStringLiteral("t.conf"));
        {
            QSettings s(path, QSettings::IniFormat);
            s.beginGroup(QStringLiteral("keys"));
            s.setValue(QStringLiteral("Ctrl+J"), QStringLiteral("archive"));
            s.endGroup();
        }

        KeyMap map;
        map.loadDefaults();
        QSettings s(path, QSettings::IniFormat);
        map.loadOverrides(s);

        QCOMPARE(map.warnings().size(), 0);
    }
}

QTEST_MAIN(TestKeyMap)
#include "test_keymap.moc"