Modules/GraphicApplication/tests/InputCoalescerTests.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

283 lines
9 KiB
C++

#include "InputCoalescer.hpp"
#include "UnitTest++/UnitTest++.h"
#include <cmath>
#include <string>
#include <vector>
using namespace tp;
namespace {
std::string point(Vec2F value) { return std::to_string((int) value.x) + "," + std::to_string((int) value.y); }
std::string describe(const InputEvent& event) {
return std::visit(
InputEventVisitor{
[](const PointerSampled& sampled) {
return "sample " + std::to_string((int) std::lround(sampled.sample.pressure * 100));
},
[](const PointerMoved& moved) { return "move " + point(moved.position); },
[](const PointerLeft&) { return std::string("leave"); },
[](const PointerButtonChanged& changed) {
return std::string(changed.pressed ? "press " : "release ") + std::to_string((int) changed.button);
},
[](const Scrolled& scrolled) {
return std::string(scrolled.source == ScrollSource::Touchpad ? "touchpad " : "scroll ") +
point(scrolled.delta);
},
[](const ScrollEnded&) { return std::string("scroll end"); },
[](const KeyChanged& changed) {
const char* actions[] = { "key down ", "key up ", "repeat " };
return actions[(int) changed.action] + std::to_string((int) changed.key);
},
[](const TextEntered& text) { return "text " + text.utf8; },
[](const KeyboardFocusChanged& focus) { return std::string(focus.focused ? "focus" : "blur"); },
[](const GestureChanged&) { return std::string("gesture"); },
},
event
);
}
class LoggingSink : public InputSink {
public:
void post(const InputEvent& event) override { log.push_back(describe(event)); }
std::vector<std::string> log;
};
std::vector<std::string> drain(InputCoalescer& coalescer) {
LoggingSink sink;
coalescer.deliver(sink);
return sink.log;
}
void move(InputCoalescer& coalescer, Vec2F position) { coalescer.post(PointerMoved{ position }); }
void scroll(InputCoalescer& coalescer, Vec2F delta, ScrollSource source = ScrollSource::Wheel) {
coalescer.post(Scrolled{ delta, source });
}
void button(InputCoalescer& coalescer, bool pressed) {
coalescer.post(PointerButtonChanged{ PointerButton::Left, pressed });
}
}
SUITE(InputCoalescerPolicies) {
TEST(OppositeScrollsThatCancelDeliverNothing) {
InputCoalescer coalescer;
scroll(coalescer, { 0, 1 });
scroll(coalescer, { 0, -1 });
CHECK(drain(coalescer).empty());
}
TEST(BurstOfMotionCollapsesToLatestPosition) {
InputCoalescer coalescer;
move(coalescer, { 1, 1 });
move(coalescer, { 2, 2 });
move(coalescer, { 3, 3 });
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(1u, log.size());
CHECK_EQUAL("move 3,3", log[0]);
CHECK_EQUAL(3u, coalescer.statistics().received);
CHECK_EQUAL(1u, coalescer.statistics().delivered);
}
TEST(EveryPolicyKeepsEachMotionSampleForDrawingApps) {
InputCoalescer coalescer;
coalescer.setPolicies({ DeliveryPolicy::Every, DeliveryPolicy::Accumulate });
move(coalescer, { 1, 1 });
move(coalescer, { 2, 2 });
move(coalescer, { 3, 3 });
CHECK_EQUAL(3u, drain(coalescer).size());
}
TEST(MotionNeverMergesAcrossButtons) {
InputCoalescer coalescer;
move(coalescer, { 1, 1 });
move(coalescer, { 5, 5 });
button(coalescer, true);
move(coalescer, { 6, 6 });
move(coalescer, { 9, 9 });
button(coalescer, false);
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(4u, log.size());
CHECK_EQUAL("move 5,5", log[0]);
CHECK_EQUAL("press 0", log[1]);
CHECK_EQUAL("move 9,9", log[2]);
CHECK_EQUAL("release 0", log[3]);
}
TEST(ScrollDeltasAddUpAndDoNotCrossKeys) {
InputCoalescer coalescer;
scroll(coalescer, { 0, 1 });
scroll(coalescer, { 0, 2 });
coalescer.post(KeyChanged{ Key::LeftControl, KeyAction::Press });
scroll(coalescer, { 0, -1 });
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(3u, log.size());
CHECK_EQUAL("scroll 0,3", log[0]);
CHECK_EQUAL("key down " + std::to_string((int) Key::LeftControl), log[1]);
CHECK_EQUAL("scroll 0,-1", log[2]);
}
TEST(TouchpadScrollsKeepTheirSourceAndEnd) {
InputCoalescer coalescer;
scroll(coalescer, { 0, 1 }, ScrollSource::Touchpad);
scroll(coalescer, { 0, 2 }, ScrollSource::Touchpad);
coalescer.post(ScrollEnded{});
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(2u, log.size());
CHECK_EQUAL("touchpad 0,3", log[0]);
CHECK_EQUAL("scroll end", log[1]);
}
TEST(MergedScrollsKeepTheLatestTimestamp) {
InputCoalescer coalescer;
coalescer.post(Scrolled{ { 0, 1 }, ScrollSource::Touchpad, 100 });
coalescer.post(Scrolled{ { 0, 2 }, ScrollSource::Touchpad, 108 });
class KeepingSink : public InputSink {
public:
void post(const InputEvent& event) override { events.push_back(event); }
std::vector<InputEvent> events;
};
KeepingSink sink;
coalescer.deliver(sink);
REQUIRE CHECK_EQUAL(1u, sink.events.size());
const auto* scrolled = std::get_if<Scrolled>(&sink.events[0]);
REQUIRE CHECK(scrolled != nullptr);
CHECK_EQUAL(108u, scrolled->timeMs);
}
TEST(WheelAndTouchpadScrollsAreNotMergedTogether) {
InputCoalescer coalescer;
scroll(coalescer, { 0, 1 }, ScrollSource::Wheel);
scroll(coalescer, { 0, 2 }, ScrollSource::Touchpad);
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(2u, log.size());
CHECK_EQUAL("scroll 0,1", log[0]);
CHECK_EQUAL("touchpad 0,2", log[1]);
}
TEST(ScrollsOnEitherSideOfAnEndAreNotMerged) {
InputCoalescer coalescer;
scroll(coalescer, { 0, 1 }, ScrollSource::Touchpad);
coalescer.post(ScrollEnded{});
scroll(coalescer, { 0, 1 }, ScrollSource::Touchpad);
CHECK_EQUAL(3u, drain(coalescer).size());
}
TEST(DiscreteEventsAreNeverMerged) {
InputCoalescer coalescer;
coalescer.post(KeyChanged{ Key::A, KeyAction::Press });
coalescer.post(KeyChanged{ Key::A, KeyAction::Release });
coalescer.post(KeyChanged{ Key::A, KeyAction::Press });
coalescer.post(KeyChanged{ Key::A, KeyAction::Repeat });
coalescer.post(KeyChanged{ Key::A, KeyAction::Repeat });
coalescer.post(TextEntered{ "a" });
coalescer.post(TextEntered{ "b" });
coalescer.post(KeyboardFocusChanged{ false });
coalescer.post(PointerLeft{});
CHECK_EQUAL(9u, drain(coalescer).size());
}
TEST(TapSeesEveryRawEventBeforeCoalescing) {
InputCoalescer coalescer;
LoggingSink tap;
coalescer.setTap(&tap);
move(coalescer, { 1, 1 });
move(coalescer, { 2, 2 });
coalescer.post(TextEntered{ "q" });
CHECK_EQUAL(3u, tap.log.size());
CHECK_EQUAL(2u, drain(coalescer).size());
}
TEST(SuccessiveGestureUpdatesAccumulate) {
InputCoalescer coalescer;
TouchGesture update;
update.scale = 2;
update.pan = { 1, 0 };
coalescer.post(GestureChanged{ update });
coalescer.post(GestureChanged{ update });
std::vector<InputEvent> delivered;
struct Collect : InputSink {
std::vector<InputEvent>& events;
explicit Collect(std::vector<InputEvent>& events) :
events(events) {}
void post(const InputEvent& event) override { events.push_back(event); }
} collect(delivered);
coalescer.deliver(collect);
REQUIRE CHECK_EQUAL(1u, delivered.size());
const TouchGesture& merged = std::get<GestureChanged>(delivered[0]).gesture;
CHECK_CLOSE(4.0f, merged.scale, 0.0001f);
CHECK_CLOSE(2.0f, merged.pan.x, 0.0001f);
}
TEST(DeliveryEmptiesTheQueueAndKeepsOrder) {
InputCoalescer coalescer;
coalescer.post(TextEntered{ "x" });
move(coalescer, { 4, 4 });
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(2u, log.size());
CHECK_EQUAL("text x", log[0]);
CHECK_EQUAL("move 4,4", log[1]);
CHECK(coalescer.isEmpty());
CHECK_EQUAL(0u, drain(coalescer).size());
}
}
SUITE(InputCoalescerPenSamples) {
PointerSample pen(halnf pressure) { return PointerSample{ PointerDevice::Stylus, StylusTip::Pen, pressure }; }
TEST(EachDeliveredMotionCarriesThePressureItWasRecordedWith) {
InputCoalescer coalescer;
coalescer.setPolicies({ DeliveryPolicy::Every, DeliveryPolicy::Accumulate });
coalescer.post(PointerSampled{ pen(0.25f) });
move(coalescer, { 1, 1 });
coalescer.post(PointerSampled{ pen(0.75f) });
move(coalescer, { 2, 2 });
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(4u, log.size());
CHECK_EQUAL("sample 25", log[0]);
CHECK_EQUAL("move 1,1", log[1]);
CHECK_EQUAL("sample 75", log[2]);
CHECK_EQUAL("move 2,2", log[3]);
}
TEST(LatestMotionKeepsTheNewestSample) {
InputCoalescer coalescer;
coalescer.post(PointerSampled{ pen(0.2f) });
move(coalescer, { 1, 1 });
coalescer.post(PointerSampled{ pen(0.9f) });
move(coalescer, { 4, 4 });
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(2u, log.size());
CHECK_EQUAL("sample 90", log[0]);
CHECK_EQUAL("move 4,4", log[1]);
}
TEST(UnchangedSampleIsNotRepeated) {
InputCoalescer coalescer;
coalescer.setPolicies({ DeliveryPolicy::Every, DeliveryPolicy::Accumulate });
coalescer.post(PointerSampled{ pen(0.5f) });
move(coalescer, { 1, 1 });
button(coalescer, true);
move(coalescer, { 2, 2 });
const std::vector<std::string> log = drain(coalescer);
REQUIRE CHECK_EQUAL(4u, log.size());
CHECK_EQUAL("sample 50", log[0]);
CHECK_EQUAL("move 2,2", log[3]);
}
}