keyboard stuff
1// Copyright 2024 Dasky (@daskygit)
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include "gtest/gtest.h"
5#include "mouse_report_util.hpp"
6#include "test_common.hpp"
7
8using testing::_;
9
10struct MouseKeyExpectations {
11 int16_t x;
12 int16_t y;
13 int16_t h;
14 int16_t v;
15 uint16_t button_mask;
16};
17
18class Mousekey : public TestFixture {};
19class MousekeyParametrized : public ::testing::WithParamInterface<std::pair<KeymapKey, MouseKeyExpectations>>, public Mousekey {};
20
21TEST_F(Mousekey, SendMouseNotCalledWhenNoKeyIsPressed) {
22 TestDriver driver;
23 EXPECT_NO_MOUSE_REPORT(driver);
24 run_one_scan_loop();
25}
26
27TEST_F(Mousekey, PressAndHoldCursorUpIsCorrectlyReported) {
28 TestDriver driver;
29 KeymapKey mouse_key = KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_UP};
30
31 set_keymap({mouse_key});
32
33 EXPECT_MOUSE_REPORT(driver, (0, -8, 0, 0, 0));
34 mouse_key.press();
35 run_one_scan_loop();
36
37 EXPECT_MOUSE_REPORT(driver, (0, -2, 0, 0, 0));
38 idle_for(MOUSEKEY_INTERVAL);
39
40 EXPECT_EMPTY_MOUSE_REPORT(driver);
41 mouse_key.release();
42 run_one_scan_loop();
43
44 VERIFY_AND_CLEAR(driver);
45}
46
47TEST_F(Mousekey, PressAndHoldButtonOneCorrectlyReported) {
48 TestDriver driver;
49 KeymapKey mouse_key = KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_1};
50
51 set_keymap({mouse_key});
52
53 EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 1));
54 mouse_key.press();
55 run_one_scan_loop();
56
57 idle_for(MOUSEKEY_INTERVAL);
58
59 EXPECT_EMPTY_MOUSE_REPORT(driver);
60 mouse_key.release();
61 run_one_scan_loop();
62
63 VERIFY_AND_CLEAR(driver);
64}
65
66TEST_P(MousekeyParametrized, PressAndReleaseIsCorrectlyReported) {
67 TestDriver driver;
68 KeymapKey mouse_key = GetParam().first;
69 MouseKeyExpectations expectations = GetParam().second;
70
71 set_keymap({mouse_key});
72
73 EXPECT_MOUSE_REPORT(driver, (expectations.x, expectations.y, expectations.h, expectations.v, expectations.button_mask));
74 mouse_key.press();
75 run_one_scan_loop();
76
77 EXPECT_EMPTY_MOUSE_REPORT(driver);
78 mouse_key.release();
79 run_one_scan_loop();
80
81 EXPECT_NO_MOUSE_REPORT(driver);
82 run_one_scan_loop();
83
84 VERIFY_AND_CLEAR(driver);
85}
86// clang-format off
87INSTANTIATE_TEST_CASE_P(
88 Keys,
89 MousekeyParametrized,
90 ::testing::Values(
91 // Key , X, Y, H, V, Buttons Mask
92 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_1}, MouseKeyExpectations{ 0, 0, 0, 0, 1}),
93 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_2}, MouseKeyExpectations{ 0, 0, 0, 0, 2}),
94 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_3}, MouseKeyExpectations{ 0, 0, 0, 0, 4}),
95 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_4}, MouseKeyExpectations{ 0, 0, 0, 0, 8}),
96 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_5}, MouseKeyExpectations{ 0, 0, 0, 0, 16}),
97 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_6}, MouseKeyExpectations{ 0, 0, 0, 0, 32}),
98 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_7}, MouseKeyExpectations{ 0, 0, 0, 0, 64}),
99 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_8}, MouseKeyExpectations{ 0, 0, 0, 0, 128}),
100 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_UP}, MouseKeyExpectations{ 0,-8, 0, 0, 0}),
101 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_DOWN}, MouseKeyExpectations{ 0, 8, 0, 0, 0}),
102 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_LEFT}, MouseKeyExpectations{-8, 0, 0, 0, 0}),
103 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_RIGHT}, MouseKeyExpectations{ 8, 0, 0, 0, 0}),
104 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_UP}, MouseKeyExpectations{ 0, 0, 0, 1, 0}),
105 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_DOWN}, MouseKeyExpectations{ 0, 0, 0,-1, 0}),
106 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_LEFT}, MouseKeyExpectations{ 0, 0,-1, 0, 0}),
107 std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_RIGHT}, MouseKeyExpectations{ 0, 0, 1, 0, 0})
108 ));
109// clang-format on