Modules/GraphicApplication/tests/InputRecordingTests.cpp
agent 6c32207f69 Scroll overscroll as a translated rubber band with one model for every input
Touchpad, finger drag and pinch pans share one path: the content moves first,
then a single RubberBand per axis takes the travel past a bound with
UIScrollView resistance, translated and never scaled, capped at a quarter of
the view. Lifting springs a pulled edge back with the Android edge spring;
otherwise the view flings with the velocity of the last 100 ms, and a fling
that reaches a bound carries a little past it before springing back. Wheel
notches coast and stop at the bound.

Touchpad scroll-end is routed to the view that took the scroll, and the coast
speed is measured on the platform's own event timestamps, so a late or
stalled frame no longer drops the inertia of a short flick. Fling friction is
0.02 rather than Android's 0.015 for a slightly shorter coast.
2026-11-02 11:00:51 +03:00

145 lines
4.9 KiB
C++

#include "InputRecording.hpp"
#include "UnitTest++/UnitTest++.h"
#include <vector>
using namespace tp;
namespace {
struct ManualClock {
double now = 0;
InputRecorder::Clock clock() {
return [this]() { return now; };
}
};
struct Collector : InputSink {
std::vector<InputEvent> events;
void post(const InputEvent& event) override { events.push_back(event); }
};
InputRecording penStroke() {
ManualClock time;
InputRecorder recorder(time.clock());
recorder.setWindowSize({ 1600, 1000 });
PointerSample pen;
pen.device = PointerDevice::Stylus;
pen.pressure = 0.25f;
pen.tilt = { 0.1f, -0.2f };
pen.timeMs = 77;
time.now = 1000;
recorder.post(KeyChanged{ Key::LeftShift, KeyAction::Press });
time.now = 1010;
recorder.post(PointerSampled{ pen });
recorder.post(PointerMoved{ { 10, 20 } });
recorder.post(PointerButtonChanged{ PointerButton::Left, true });
time.now = 1012.5;
recorder.post(PointerMoved{ { 30, 40 } });
time.now = 1030;
recorder.post(PointerButtonChanged{ PointerButton::Left, false });
time.now = 1200;
recorder.post(KeyChanged{ Key::LeftShift, KeyAction::Release });
return recorder.recording();
}
InputRecording roundTripped(const InputRecording& recording) {
const std::optional<InputRecording> parsed = InputRecording::parse(recording.serialize());
return parsed ? *parsed : InputRecording{};
}
}
SUITE(InputRecording) {
TEST(RecorderTimesEventsFromTheFirstOne) {
const InputRecording recording = penStroke();
REQUIRE CHECK_EQUAL(7u, recording.events.size());
CHECK_EQUAL(0.0, recording.events[0].timeMs);
CHECK_EQUAL(12.5, recording.events[4].timeMs);
CHECK_EQUAL(200.0, recording.durationMs());
}
TEST(ExplicitClockStartTimesTheFirstEventFromThere) {
ManualClock time;
InputRecorder recorder(time.clock());
time.now = 500;
recorder.startClockNow();
time.now = 740;
recorder.post(PointerMoved{ { 1, 1 } });
CHECK_EQUAL(240.0, recorder.recording().events.front().timeMs);
}
TEST(SerializedRecordingParsesBackIdentically) {
const InputRecording recording = penStroke();
const InputRecording parsed = roundTripped(recording);
CHECK_EQUAL(1600.0f, parsed.windowSize.x);
CHECK(recording.events == parsed.events);
}
TEST(EveryEventKindRoundTrips) {
ManualClock time;
InputRecorder recorder(time.clock());
TouchGesture pinch;
pinch.phase = GesturePhase::Update;
pinch.center = { 100, 50 };
pinch.pan = { 2, -1 };
pinch.scale = 1.25f;
pinch.rotation = 0.1f;
pinch.tapFingers = 2;
recorder.post(Scrolled{ { 0, 2 }, ScrollSource::Wheel });
recorder.post(Scrolled{ { 0, -3 }, ScrollSource::Touchpad, 123456 });
recorder.post(ScrollEnded{ 123470 });
recorder.post(KeyChanged{ Key::A, KeyAction::Repeat });
recorder.post(TextEntered{ "a b%\xd0\x96" });
recorder.post(GestureChanged{ pinch });
recorder.post(KeyboardFocusChanged{ true });
recorder.post(PointerLeft{});
recorder.post(PointerButtonChanged{ PointerButton::Forward, true });
CHECK(roundTripped(recorder.recording()).events == recorder.recording().events);
}
TEST(ScrollLinesWithoutATimestampStillParse) {
const std::optional<InputRecording> recording = InputRecording::parse(
std::string(InputRecording::kHeader) + "\n1.0 scroll touchpad 0 -0.5\n2.0 scroll-end\n"
);
REQUIRE CHECK(recording.has_value());
REQUIRE CHECK_EQUAL(2u, recording->events.size());
CHECK((recording->events[0].event == InputEvent{ Scrolled{ { 0.0f, -0.5f }, ScrollSource::Touchpad, 0 } }));
CHECK((recording->events[1].event == InputEvent{ ScrollEnded{ 0 } }));
}
TEST(ParseRejectsForeignText) {
CHECK(!InputRecording::parse("hello\n").has_value());
CHECK(!InputRecording::parse(std::string(InputRecording::kHeader) + "\n1.0 teleport 3\n").has_value());
CHECK(!InputRecording::parse(std::string(InputRecording::kHeader) + "\n1.0 button sideways 1\n").has_value());
}
TEST(ReplayPostsEventsInOrder) {
const InputRecording recording = penStroke();
Collector collector;
recording.replayInto(collector);
REQUIRE CHECK_EQUAL(recording.events.size(), collector.events.size());
for (size_t index = 0; index < recording.events.size(); index++) {
CHECK(recording.events[index].event == collector.events[index]);
}
}
TEST(SliceKeepsThePointerActivityAndRebasesTime) {
const InputRecording sliced = penStroke().slicedToPointerActivity();
REQUIRE CHECK_EQUAL(5u, sliced.events.size());
CHECK(std::holds_alternative<PointerSampled>(sliced.events.front().event));
CHECK_EQUAL(0.0, sliced.events.front().timeMs);
CHECK(std::holds_alternative<PointerButtonChanged>(sliced.events.back().event));
CHECK_EQUAL(20.0, sliced.events.back().timeMs);
}
TEST(TranslateMovesOnlyPositions) {
const InputRecording moved = penStroke().translated({ 5, -5 });
CHECK_EQUAL(15.0f, std::get<PointerMoved>(moved.events[2].event).position.x);
CHECK_EQUAL(15.0f, std::get<PointerMoved>(moved.events[2].event).position.y);
CHECK_EQUAL(0.25f, std::get<PointerSampled>(moved.events[1].event).sample.pressure);
}
}