Modules/GraphicApplication/tests/InputStateTests.cpp
agent 36e4da0472 GraphicApplication: track modifier keys in the input state
KeyModifiers (shift, control, alt, super; either side of the keyboard counts)
and a ModifierTracker that follows modifier presses in event order. InputState
keeps one, so consumers read the modifiers held at the end of the frame, and
walk events() with their own tracker when they need per-event modifiers.
Losing keyboard focus releases them.
2026-10-30 11:15:17 +03:00

180 lines
5.7 KiB
C++

#include "InputState.hpp"
#include "UnitTest++/UnitTest++.h"
using namespace tp;
SUITE(InputState) {
TEST(AllInputKindsArriveInOneOrderedBatch) {
InputQueue queue;
TouchGesture gesture;
gesture.scale = 1.5f;
queue.post(TextEntered{ "a" });
queue.post(KeyChanged{ Key::A, KeyAction::Repeat });
queue.post(GestureChanged{ gesture });
queue.post(KeyboardFocusChanged{ false });
CHECK_EQUAL(4u, queue.pendingEventCount());
queue.applyPendingEvents();
CHECK_EQUAL(0u, queue.pendingEventCount());
const std::vector<InputEvent>& events = queue.state().events();
REQUIRE CHECK_EQUAL(4u, events.size());
CHECK(std::get<TextEntered>(events[0]).utf8 == "a");
CHECK(std::get<KeyChanged>(events[1]) == (KeyChanged{ Key::A, KeyAction::Repeat }));
CHECK_CLOSE(1.5f, std::get<GestureChanged>(events[2]).gesture.scale, 0.0001f);
CHECK(!std::get<KeyboardFocusChanged>(events[3]).focused);
}
TEST(PressAndReleaseInOneBatchRemainVisible) {
InputQueue queue;
queue.post(PointerButtonChanged{ PointerButton::Left, true });
queue.post(PointerButtonChanged{ PointerButton::Left, false });
queue.applyPendingEvents();
const ButtonState& left = queue.state().button(PointerButton::Left);
CHECK(left.pressed);
CHECK(left.released);
CHECK(!left.down);
queue.applyPendingEvents();
CHECK(!queue.state().button(PointerButton::Left).pressed);
CHECK(!queue.state().button(PointerButton::Left).released);
}
TEST(KeysAndPointerButtonsAreSeparateStates) {
InputQueue queue;
queue.post(KeyChanged{ Key::A, KeyAction::Press });
queue.applyPendingEvents();
CHECK(queue.state().key(Key::A).down);
CHECK(!queue.state().button(PointerButton::Left).down);
}
TEST(PointerAndScrollDescribeTheWholeBatch) {
InputQueue queue;
queue.post(PointerMoved{ { 10, 20 } });
queue.post(PointerMoved{ { 15, 25 } });
queue.post(Scrolled{ { 1, 2 }, ScrollSource::Wheel });
queue.post(Scrolled{ { 3, 4 }, ScrollSource::Touchpad });
queue.applyPendingEvents();
const InputState& state = queue.state();
CHECK_CLOSE(15, state.pointer().x, 0.0001f);
CHECK_CLOSE(25, state.pointer().y, 0.0001f);
CHECK_CLOSE(15, state.pointerDelta().x, 0.0001f);
CHECK_CLOSE(4, state.scroll().x, 0.0001f);
CHECK_CLOSE(6, state.scroll().y, 0.0001f);
CHECK(state.isContinuousScroll());
CHECK(state.isPointerInside());
queue.applyPendingEvents();
CHECK_CLOSE(0, queue.state().pointerDelta().x, 0.0001f);
CHECK_CLOSE(0, queue.state().scroll().y, 0.0001f);
CHECK(!queue.state().isContinuousScroll());
}
TEST(PointerLeftIsAnExplicitEventThatReleasesHeldButtons) {
InputQueue queue;
queue.post(PointerMoved{ { 5, 5 } });
queue.post(PointerButtonChanged{ PointerButton::Right, true });
queue.applyPendingEvents();
CHECK(queue.state().button(PointerButton::Right).down);
queue.post(PointerLeft{});
queue.applyPendingEvents();
CHECK(!queue.state().isPointerInside());
CHECK(queue.state().button(PointerButton::Right).released);
const std::vector<InputEvent>& events = queue.state().events();
REQUIRE CHECK_EQUAL(2u, events.size());
CHECK(std::holds_alternative<PointerButtonChanged>(events[0]));
CHECK(std::holds_alternative<PointerLeft>(events[1]));
}
TEST(FocusLossReleasesHeldKeysBeforeTheFocusEvent) {
InputQueue queue;
queue.post(KeyChanged{ Key::A, KeyAction::Press });
queue.applyPendingEvents();
CHECK(queue.state().key(Key::A).down);
queue.post(KeyboardFocusChanged{ false });
queue.applyPendingEvents();
CHECK(!queue.state().key(Key::A).down);
CHECK(queue.state().key(Key::A).released);
const std::vector<InputEvent>& events = queue.state().events();
REQUIRE CHECK_EQUAL(2u, events.size());
CHECK(std::get<KeyChanged>(events[0]).action == KeyAction::Release);
CHECK(!std::get<KeyboardFocusChanged>(events[1]).focused);
}
TEST(ViewOffsetsThePointerAndCanHideButtons) {
InputQueue queue;
queue.post(PointerMoved{ { 30, 40 } });
queue.post(PointerButtonChanged{ PointerButton::Left, true });
queue.applyPendingEvents();
const InputView view(queue.state(), { 10, 10 });
CHECK_CLOSE(20, view.getPointer().x, 0.0001f);
CHECK(view.isDown(PointerButton::Left));
const InputView hidden = view.withButtons(InputView::Buttons::Hidden);
CHECK(!hidden.isDown(PointerButton::Left));
CHECK_CLOSE(0, hidden.getPointerPressure(), 0.0001f);
CHECK_CLOSE(30, hidden.relativeTo({ 0, 0 }).getPointer().x, 0.0001f);
}
}
SUITE(KeyModifiers) {
TEST(EitherSideOfTheKeyboardCounts) {
ModifierTracker tracker;
tracker.press(Key::RightControl);
CHECK(tracker.current().control);
tracker.press(Key::LeftControl);
tracker.release(Key::RightControl);
CHECK(tracker.current().control);
tracker.release(Key::LeftControl);
CHECK(tracker.current().none());
}
TEST(OtherKeysAreNotModifiers) {
ModifierTracker tracker;
tracker.press(Key::A);
CHECK(tracker.current().none());
CHECK(!isModifierKey(Key::A));
CHECK(isModifierKey(Key::RightSuper));
}
TEST(StateHoldsModifiersAtTheEndOfTheFrame) {
InputQueue queue;
queue.post(KeyChanged{ Key::LeftShift, KeyAction::Press });
queue.post(KeyChanged{ Key::LeftAlt, KeyAction::Press });
queue.post(KeyChanged{ Key::LeftAlt, KeyAction::Release });
queue.applyPendingEvents();
CHECK(queue.state().modifiers() == KeyModifiers::shiftOnly());
}
TEST(ModifiersPersistAcrossFrames) {
InputQueue queue;
queue.post(KeyChanged{ Key::LeftControl, KeyAction::Press });
queue.applyPendingEvents();
queue.applyPendingEvents();
CHECK(queue.state().modifiers().control);
}
TEST(LosingFocusReleasesModifiers) {
InputQueue queue;
queue.post(KeyChanged{ Key::LeftControl, KeyAction::Press });
queue.applyPendingEvents();
queue.post(KeyboardFocusChanged{ false });
queue.applyPendingEvents();
CHECK(queue.state().modifiers().none());
}
}