pointing_tests

This commit is contained in:
Dasky 2024-10-20 14:19:50 +01:00
parent c6b2553470
commit 97efc9340a
12 changed files with 531 additions and 0 deletions

View File

@ -21,8 +21,10 @@ $(TEST_OUTPUT)_SRC := \
$(SRC) \
$(QUANTUM_PATH)/keymap_introspection.c \
tests/test_common/matrix.c \
tests/test_common/pointing_device_driver.c \
tests/test_common/test_driver.cpp \
tests/test_common/keyboard_report_util.cpp \
tests/test_common/mouse_report_util.cpp \
tests/test_common/keycode_util.cpp \
tests/test_common/keycode_table.cpp \
tests/test_common/test_fixture.cpp \

6
tests/pointing/config.h Normal file
View File

@ -0,0 +1,6 @@
// Copyright 2024 Dasky (@daskygit)
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "test_common.h"

View File

@ -0,0 +1,6 @@
// Copyright 2024 Dasky (@daskygit)
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "test_common.h"

View File

@ -0,0 +1,5 @@
POINTING_DEVICE_ENABLE = no
MOUSEKEY_ENABLE = yes
POINTING_DEVICE_DRIVER = custom

View File

@ -0,0 +1,65 @@
// Copyright 2024 Dasky (@daskygit)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "gtest/gtest.h"
#include "mouse_report_util.hpp"
#include "test_common.hpp"
#include "test_pointing_device_driver.h"
using testing::_;
struct MouseKeyExpectations {
int16_t x;
int16_t y;
int16_t h;
int16_t v;
uint16_t button_mask;
};
class Pointing : public TestFixture {};
class PointingKeycodesParametrizedTestFixture : public ::testing::WithParamInterface<std::pair<KeymapKey, MouseKeyExpectations>>, public Pointing {};
TEST_P(PointingKeycodesParametrizedTestFixture, PointingMouseKeysViaPointingDriver) {
TestDriver driver;
KeymapKey mouse_key = GetParam().first;
MouseKeyExpectations expectations = GetParam().second;
set_keymap({mouse_key});
EXPECT_MOUSE_REPORT(driver, (expectations.x, expectations.y, expectations.h, expectations.v, expectations.button_mask));
mouse_key.press();
run_one_scan_loop();
EXPECT_EMPTY_MOUSE_REPORT(driver);
mouse_key.release();
run_one_scan_loop();
EXPECT_NO_MOUSE_REPORT(driver);
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
// clang-format off
INSTANTIATE_TEST_CASE_P(
PointingMouseKeysOnlyTests,
PointingKeycodesParametrizedTestFixture,
::testing::Values(
// Key , X, Y, H, V, Buttons Mask
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_1}, MouseKeyExpectations{ 0, 0, 0, 0, 1}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_2}, MouseKeyExpectations{ 0, 0, 0, 0, 2}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_3}, MouseKeyExpectations{ 0, 0, 0, 0, 4}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_4}, MouseKeyExpectations{ 0, 0, 0, 0, 8}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_5}, MouseKeyExpectations{ 0, 0, 0, 0, 16}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_6}, MouseKeyExpectations{ 0, 0, 0, 0, 32}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_7}, MouseKeyExpectations{ 0, 0, 0, 0, 64}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_8}, MouseKeyExpectations{ 0, 0, 0, 0, 128}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_UP}, MouseKeyExpectations{ 0,-8, 0, 0, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_DOWN}, MouseKeyExpectations{ 0, 8, 0, 0, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_LEFT}, MouseKeyExpectations{-8, 0, 0, 0, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_CURSOR_RIGHT}, MouseKeyExpectations{ 8, 0, 0, 0, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_UP}, MouseKeyExpectations{ 0, 0, 0, 1, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_DOWN}, MouseKeyExpectations{ 0, 0, 0,-1, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_LEFT}, MouseKeyExpectations{ 0, 0,-1, 0, 0}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_WHEEL_RIGHT}, MouseKeyExpectations{ 0, 0, 1, 0, 0})
));
// clang-format on

4
tests/pointing/test.mk Normal file
View File

@ -0,0 +1,4 @@
POINTING_DEVICE_ENABLE = yes
MOUSEKEY_ENABLE = no
POINTING_DEVICE_DRIVER = custom

View File

@ -0,0 +1,174 @@
// Copyright 2024 Dasky (@daskygit)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "gtest/gtest.h"
#include "mouse_report_util.hpp"
#include "test_common.hpp"
#include "test_pointing_device_driver.h"
#include "mousekey.h"
using testing::_;
struct MouseKeyExpectations {
int16_t x;
int16_t y;
int16_t h;
int16_t v;
uint16_t button_mask;
};
class Pointing : public TestFixture {};
class PointingKeycodeButtonsParametrizedTestFixture : public ::testing::WithParamInterface<std::pair<KeymapKey, MouseKeyExpectations>>, public Pointing {};
TEST_F(Pointing, SendMouseIsNotCalledWithNoInput) {
TestDriver driver;
EXPECT_NO_MOUSE_REPORT(driver);
run_one_scan_loop();
}
TEST_F(Pointing, Xnegative) {
TestDriver driver;
set_x(-10);
EXPECT_MOUSE_REPORT(driver, (-10, 0, 0, 0, 0));
run_one_scan_loop();
clear_movement();
// EXPECT_EMPTY_MOUSE_REPORT(driver); // This should probably generate an empty report.
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_F(Pointing, Xpositive) {
TestDriver driver;
set_x(10);
EXPECT_MOUSE_REPORT(driver, (10, 0, 0, 0, 0));
run_one_scan_loop();
clear_movement();
// EXPECT_EMPTY_MOUSE_REPORT(driver); // This should probably generate an empty report.
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_F(Pointing, Ynegative) {
TestDriver driver;
set_y(-20);
EXPECT_MOUSE_REPORT(driver, (0, -20, 0, 0, 0));
run_one_scan_loop();
clear_movement();
// EXPECT_EMPTY_MOUSE_REPORT(driver); // This should probably generate an empty report.
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_F(Pointing, Ypositive) {
TestDriver driver;
set_y(20);
EXPECT_MOUSE_REPORT(driver, (0, 20, 0, 0, 0));
run_one_scan_loop();
clear_movement();
// EXPECT_EMPTY_MOUSE_REPORT(driver); // This should probably generate an empty report.
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_F(Pointing, XandY) {
TestDriver driver;
set_x(-50);
set_y(100);
EXPECT_MOUSE_REPORT(driver, (-50, 100, 0, 0, 0));
run_one_scan_loop();
clear_movement();
// EXPECT_EMPTY_MOUSE_REPORT(driver); // This should probably generate an empty report.
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_F(Pointing, CorrectButtonIsReportedWhenPressed) {
TestDriver driver;
EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 1));
press_button(POINTING_DEVICE_BUTTON1);
run_one_scan_loop();
EXPECT_EMPTY_MOUSE_REPORT(driver);
release_button(POINTING_DEVICE_BUTTON1);
run_one_scan_loop();
EXPECT_NO_MOUSE_REPORT(driver);
run_one_scan_loop();
clear_all_buttons();
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_F(Pointing, CorrectButtonIsReportedWhenKeyPressed) {
TestDriver driver;
auto key = KeymapKey(0, 0, 0, KC_MS_BTN1);
set_keymap({key});
EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 1));
key.press();
run_one_scan_loop();
EXPECT_EMPTY_MOUSE_REPORT(driver);
key.release();
run_one_scan_loop();
EXPECT_NO_MOUSE_REPORT(driver);
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
TEST_P(PointingKeycodeButtonsParametrizedTestFixture, PointingMouseKeysViaPointingDriver) {
TestDriver driver;
KeymapKey mouse_key = GetParam().first;
MouseKeyExpectations expectations = GetParam().second;
set_keymap({mouse_key});
EXPECT_MOUSE_REPORT(driver, (expectations.x, expectations.y, expectations.h, expectations.v, expectations.button_mask));
mouse_key.press();
run_one_scan_loop();
EXPECT_EMPTY_MOUSE_REPORT(driver);
mouse_key.release();
run_one_scan_loop();
EXPECT_NO_MOUSE_REPORT(driver);
run_one_scan_loop();
VERIFY_AND_CLEAR(driver);
}
// clang-format off
INSTANTIATE_TEST_CASE_P(
PointingMouseKeysTests,
PointingKeycodeButtonsParametrizedTestFixture,
::testing::Values(
// Key , X, Y, H, V, Buttons Mask
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_1}, MouseKeyExpectations{0, 0, 0, 0, 1}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_2}, MouseKeyExpectations{0, 0, 0, 0, 2}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_3}, MouseKeyExpectations{0, 0, 0, 0, 4}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_4}, MouseKeyExpectations{0, 0, 0, 0, 8}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_5}, MouseKeyExpectations{0, 0, 0, 0, 16}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_6}, MouseKeyExpectations{0, 0, 0, 0, 32}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_7}, MouseKeyExpectations{0, 0, 0, 0, 64}),
std::make_pair(KeymapKey{0, 0, 0, QK_MOUSE_BUTTON_8}, MouseKeyExpectations{0, 0, 0, 0, 128})
));
// clang-format on

View File

@ -0,0 +1,66 @@
/* Copyright 2017 Fred Sundvik
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "mouse_report_util.hpp"
#include <cstdint>
#include <vector>
#include <algorithm>
using namespace testing;
bool operator==(const report_mouse_t& lhs, const report_mouse_t& rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y && lhs.h == rhs.h && lhs.v == rhs.v && lhs.buttons == rhs.buttons;
}
std::ostream& operator<<(std::ostream& os, const report_mouse_t& report) {
os << std::setw(10) << std::left << "mouse report: ";
if (report.x == 0 && report.y == 0 && report.h == 0 && report.v == 0 && report.buttons == 0) {
return os << "empty" << std::endl;
}
os << "(";
os << (int)report.x << ", ";
os << (int)report.y << ", ";
os << (int)report.h << ", ";
os << (int)report.v << ", ";
os << (int)report.buttons;
os << ")";
return os << std::endl;
}
MouseReportMatcher::MouseReportMatcher(int16_t x, int16_t y, int8_t h, int8_t v, uint8_t button_mask) {
memset(&m_report, 0, sizeof(report_mouse_t));
m_report.x = x;
m_report.y = y;
m_report.h = h;
m_report.v = v;
m_report.buttons = button_mask;
}
bool MouseReportMatcher::MatchAndExplain(report_mouse_t& report, MatchResultListener* listener) const {
return m_report == report;
}
void MouseReportMatcher::DescribeTo(::std::ostream* os) const {
*os << "is equal to " << m_report;
}
void MouseReportMatcher::DescribeNegationTo(::std::ostream* os) const {
*os << "is not equal to " << m_report;
}

View File

@ -0,0 +1,38 @@
/* Copyright 2017 Fred Sundvik
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "report.h"
#include <ostream>
#include "gmock/gmock.h"
bool operator==(const report_mouse_t& lhs, const report_mouse_t& rhs);
std::ostream& operator<<(std::ostream& stream, const report_mouse_t& value);
class MouseReportMatcher : public testing::MatcherInterface<report_mouse_t&> {
public:
MouseReportMatcher(int16_t x, int16_t y, int8_t h, int8_t v, uint8_t button_mask);
virtual bool MatchAndExplain(report_mouse_t& report, testing::MatchResultListener* listener) const override;
virtual void DescribeTo(::std::ostream* os) const override;
virtual void DescribeNegationTo(::std::ostream* os) const override;
private:
report_mouse_t m_report;
};
inline testing::Matcher<report_mouse_t&> MouseReport(int16_t x, int16_t y, int8_t h, int8_t v, uint8_t button_mask) {
return testing::MakeMatcher(new MouseReportMatcher(x, y, h, v, button_mask));
}

View File

@ -0,0 +1,93 @@
// Copyright 2024 Dasky (@daskygit)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "report.h"
#include "test_pointing_device_driver.h"
#include <string.h>
typedef struct {
bool state;
bool set;
} test_buttons_t;
static report_mouse_t test_report = {0};
static uint16_t test_cpi = {0};
static test_buttons_t test_button_events[8] = {0};
void pointing_device_driver_init(void) {}
report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) {
test_report.buttons = 0;
test_report.buttons = mouse_report.buttons; // buttons must currently be preserved by pointing device driver
for (uint8_t i = 0; i < 8; i++) {
if (test_button_events[i].set) {
test_button_events[i].set = false;
if (test_button_events[i].state) {
test_report.buttons |= 1 << (i);
} else {
test_report.buttons &= ~(1 << (i));
}
}
}
return test_report;
}
__attribute__((weak)) uint16_t pointing_device_driver_get_cpi(void) {
return test_cpi;
}
__attribute__((weak)) void pointing_device_driver_set_cpi(uint16_t cpi) {
test_cpi = cpi;
}
void press_button(uint8_t btn) {
test_button_events[btn].set = true;
test_button_events[btn].state = true;
}
void release_button(uint8_t btn) {
test_button_events[btn].set = true;
test_button_events[btn].state = false;
}
void clear_all_buttons(void) {
for (uint8_t i = 0; i < 8; i++) {
test_button_events[i].set = true;
test_button_events[i].state = false;
}
}
void set_x(int16_t x) {
test_report.x = x;
}
void clear_x(void) {
set_x(0);
}
void set_y(int16_t y) {
test_report.y = y;
}
void clear_y(void) {
set_y(0);
}
void set_h(int8_t h) {
test_report.h = h;
}
void clear_h(void) {
set_h(0);
}
void set_v(int8_t v) {
test_report.v = v;
}
void clear_v(void) {
set_v(0);
}
void clear_movement(void) {
set_x(0);
set_y(0);
set_h(0);
set_v(0);
}

View File

@ -66,6 +66,25 @@ class TestDriver {
*/
#define EXPECT_REPORT(driver, report) EXPECT_CALL((driver), send_keyboard_mock(KeyboardReport report))
/**
* @brief Sets gmock expectation that a mouse report of `report` will be sent.
* For this macro to parse correctly, the `report` arg must be surrounded by
* parentheses ( ). For instance,
*
* // Expect that a report of "X:-10 Y:0 H:0 V:10 BTN:1 " is sent to the host.
* EXPECT_REPORT(driver, (-10, 0, 0, 0, 1));
*
* is shorthand for
*
* EXPECT_CALL(driver, send_mouse_mock(MouseReport(-10, 0, 0, 0, 1)));
*
* It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
* allow only single report to be sent:
*
* EXPECT_REPORT(driver, (-10, 0, 0, 0, 1)).Times(1);
*/
#define EXPECT_MOUSE_REPORT(driver, report) EXPECT_CALL((driver), send_mouse_mock(MouseReport report))
/**
* @brief Sets gmock expectation that Unicode `code_point` is sent with UNICODE_MODE_LINUX input
* mode. For instance for U+2013,
@ -87,6 +106,15 @@ class TestDriver {
*/
#define EXPECT_EMPTY_REPORT(driver) EXPECT_REPORT(driver, ())
/**
* @brief Sets gmock expectation that a empty keyboard report will be sent.
* It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_MOUSE_REPORT, for instance,
* allow any number of empty reports with:
*
* EXPECT_EMPTY_MOUSE_REPORT(driver).Times(AnyNumber());
*/
#define EXPECT_EMPTY_MOUSE_REPORT(driver) EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 0))
/**
* @brief Sets gmock expectation that a keyboard report will be sent, without matching its content.
* It is possible to use .Times() and other gmock APIS with EXPECT_ANY_REPORT, for instance,
@ -96,11 +124,25 @@ class TestDriver {
*/
#define EXPECT_ANY_REPORT(driver) EXPECT_CALL((driver), send_keyboard_mock(_))
/**
* @brief Sets gmock expectation that a mouse report will be sent, without matching its content.
* It is possible to use .Times() and other gmock APIS with EXPECT_ANY_MOUSE_REPORT, for instance,
* allow a single arbitrary report with:
*
* EXPECT_ANY_MOUSE_REPORT(driver).Times(1);
*/
#define EXPECT_ANY_MOUSE_REPORT(driver) EXPECT_CALL((driver), send_mouse_mock(_))
/**
* @brief Sets gmock expectation that no keyboard report will be sent at all.
*/
#define EXPECT_NO_REPORT(driver) EXPECT_ANY_REPORT(driver).Times(0)
/**
* @brief Sets gmock expectation that no keyboard report will be sent at all.
*/
#define EXPECT_NO_MOUSE_REPORT(driver) EXPECT_ANY_MOUSE_REPORT(driver).Times(0)
/** @brief Tests whether keycode `actual` is equal to `expected`. */
#define EXPECT_KEYCODE_EQ(actual, expected) EXPECT_THAT((actual), KeycodeEq((expected)))

View File

@ -0,0 +1,30 @@
// Copyright 2024 Dasky (@daskygit)
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void press_button(uint8_t btn);
void release_button(uint8_t btn);
void clear_all_buttons(void);
void set_x(int16_t x);
void clear_x(void);
void set_y(int16_t y);
void clear_y(void);
void set_h(int8_t h);
void clear_h(void);
void set_v(int8_t v);
void clear_v(void);
void clear_movement(void);
#ifdef __cplusplus
}
#endif