150 lines
9.4 KiB
C++
150 lines
9.4 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// What a pointer did, from its phases and their times: taps, long presses, drags and flings; a list's scrolling and
|
||
|
|
// what keeps a field in view; the compositor's event times on the host's clock; and the frames owed to the font atlas.
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro.Ui;
|
||
|
|
|
||
|
|
#include "../Support/Check.inc"
|
||
|
|
|
||
|
|
using namespace Nostro::Ui;
|
||
|
|
using Test::Check;
|
||
|
|
using Test::Equal;
|
||
|
|
using Kind = GestureEvent::Kind;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
void Gestures() {
|
||
|
|
GestureDetector g;
|
||
|
|
Check(!g.Pressed() && !g.LongPressDue(), "nothing is pressed at first");
|
||
|
|
Check(g.Feed(PointerPhase::Move, {5, 5}, 0).kind == Kind::None && g.Feed(PointerPhase::Up, {5, 5}, 1).kind == Kind::None, "a move or a release without a press is nothing");
|
||
|
|
g.Feed(PointerPhase::Down, {10, 10}, 0);
|
||
|
|
Check(g.Pressed() && g.LongPressDue() == std::optional<std::int64_t>(500), "a press waits for its long-press time");
|
||
|
|
Check(g.Feed(PointerPhase::Move, {13, 12}, 50).kind == Kind::None, "movement within the slop is not a drag");
|
||
|
|
const GestureEvent tap = g.Feed(PointerPhase::Up, {13, 12}, 120);
|
||
|
|
Check(tap.kind == Kind::Tap && tap.at == Point{10, 10} && !g.Pressed(), "a short press is a tap at the press position");
|
||
|
|
|
||
|
|
g.Feed(PointerPhase::Down, {10, 10}, 1000);
|
||
|
|
Check(g.Timeout(1400).kind == Kind::None, "the timer before the long-press time decides nothing");
|
||
|
|
const GestureEvent held = g.Timeout(1500);
|
||
|
|
Check(held.kind == Kind::LongPress && held.at == Point{10, 10} && !g.LongPressDue(), "the timer at the long-press time is a long press");
|
||
|
|
Check(g.Feed(PointerPhase::Move, {40, 40}, 1600).kind == Kind::None, "movement after a long press is not a drag");
|
||
|
|
Check(g.Feed(PointerPhase::Up, {40, 40}, 1700).kind == Kind::Release, "the release after a long press is a plain release");
|
||
|
|
|
||
|
|
g.Feed(PointerPhase::Down, {100, 100}, 2000);
|
||
|
|
const GestureEvent start = g.Feed(PointerPhase::Move, {100, 120}, 2016);
|
||
|
|
Check(start.kind == Kind::DragStart && start.at == Point{100, 100} && start.delta == Point{0, 20}, "movement past the slop starts a drag with the whole movement as its delta");
|
||
|
|
const GestureEvent drag = g.Feed(PointerPhase::Move, {100, 150}, 2032);
|
||
|
|
Check(drag.kind == Kind::Drag && drag.delta == Point{0, 30} && drag.at == Point{100, 150}, "a drag reports the movement since the event before");
|
||
|
|
Check(g.Dragging() && !g.LongPressDue(), "dragging, and no long press to wait for");
|
||
|
|
const GestureEvent fling = g.Feed(PointerPhase::Up, {100, 200}, 2048);
|
||
|
|
Check(fling.kind == Kind::Fling && fling.velocity.y > 1000 && fling.velocity.x == 0 && fling.at == Point{100, 200}, "a fast release is a fling with a velocity in pixels per second");
|
||
|
|
Check(g.Timeout(2500).kind == Kind::None && !g.Dragging(), "the timer after a drag decides nothing");
|
||
|
|
|
||
|
|
g.Feed(PointerPhase::Down, {100, 100}, 3000);
|
||
|
|
g.Feed(PointerPhase::Move, {100, 150}, 3016);
|
||
|
|
g.Feed(PointerPhase::Move, {100, 200}, 3032);
|
||
|
|
Check(g.Feed(PointerPhase::Up, {100, 200}, 3500).kind == Kind::Release, "a release after the finger was held still is not a fling, however fast it moved before");
|
||
|
|
g.Feed(PointerPhase::Down, {0, 0}, 3000);
|
||
|
|
g.Feed(PointerPhase::Move, {0, 30}, 3100);
|
||
|
|
g.Feed(PointerPhase::Move, {0, 31}, 3900);
|
||
|
|
Check(g.Feed(PointerPhase::Up, {0, 31}, 4000).kind == Kind::Release, "a slow release after a drag is a release, not a fling");
|
||
|
|
g.Feed(PointerPhase::Down, {0, 0}, 4500);
|
||
|
|
g.Feed(PointerPhase::Move, {30, 0}, 4500);
|
||
|
|
Check(g.Feed(PointerPhase::Up, {30, 0}, 4500).kind == Kind::Release, "a drag and its release in one instant have no speed to fling with");
|
||
|
|
|
||
|
|
g.Feed(PointerPhase::Down, {0, 0}, 5000);
|
||
|
|
g.Feed(PointerPhase::Cancel, {0, 0}, 5010);
|
||
|
|
Check(!g.Pressed() && g.Feed(PointerPhase::Up, {0, 0}, 5020).kind == Kind::None, "a cancel forgets the press");
|
||
|
|
g.Feed(PointerPhase::Down, {5, 5}, 6000);
|
||
|
|
const GestureEvent late = g.Feed(PointerPhase::Up, {5, 5}, 6600);
|
||
|
|
Check(late.kind == Kind::LongPress && late.at == Point{5, 5}, "a release past the long-press time with no timer fired, the press and its release dispatched together behind a stall, is the long press");
|
||
|
|
g.Feed(PointerPhase::Down, {5, 5}, 7000);
|
||
|
|
const GestureEvent stalled = g.Feed(PointerPhase::Move, {40, 40}, 7600);
|
||
|
|
Check(stalled.kind == Kind::LongPress && stalled.at == Point{5, 5}, "a move past the long-press time with no timer fired is the long press first: the finger was held that long before it moved");
|
||
|
|
Check(g.Feed(PointerPhase::Up, {40, 40}, 7700).kind == Kind::Release, "and its release a plain release, no drag");
|
||
|
|
|
||
|
|
// A press on what has no long press, the host's word before the Down: held as long, it is still a tap.
|
||
|
|
g.longPress = false;
|
||
|
|
g.Feed(PointerPhase::Down, {5, 5}, 8000);
|
||
|
|
Check(!g.LongPressDue() && g.Timeout(8600).kind == Kind::None, "without a long press the timer past the time gives nothing");
|
||
|
|
Check(g.Feed(PointerPhase::Move, {6, 5}, 8700).kind == Kind::None, "nor does a move within the slop");
|
||
|
|
const GestureEvent slow = g.Feed(PointerPhase::Up, {6, 5}, 9500);
|
||
|
|
Check(slow.kind == Kind::Tap && slow.at == Point{5, 5}, "and the release is the tap, however late");
|
||
|
|
g.Feed(PointerPhase::Down, {5, 5}, 10000);
|
||
|
|
Check(g.Feed(PointerPhase::Move, {5, 40}, 10800).kind == Kind::DragStart, "a finger that slides off past the time drags, as one that slides off before it");
|
||
|
|
Check(g.Feed(PointerPhase::Up, {5, 40}, 11500).kind == Kind::Release, "and its release taps nothing");
|
||
|
|
g.longPress = true;
|
||
|
|
g.Feed(PointerPhase::Down, {5, 5}, 12000);
|
||
|
|
Check(g.Timeout(12500).kind == Kind::LongPress, "a press the host gives a long press has it as before");
|
||
|
|
g.Feed(PointerPhase::Up, {5, 5}, 12600);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scrolling() {
|
||
|
|
ScrollState s;
|
||
|
|
s.content = 1000;
|
||
|
|
s.viewport = 400;
|
||
|
|
s.ScrollBy(100);
|
||
|
|
Check(s.offset == 100 && s.Max() == 600, "scroll by, and the furthest it goes");
|
||
|
|
s.ScrollBy(5000);
|
||
|
|
Equal(s.offset, 600.0f, "the offset stops at the end");
|
||
|
|
s.ScrollBy(-5000);
|
||
|
|
Equal(s.offset, 0.0f, "and at the start");
|
||
|
|
s.Fling(1000);
|
||
|
|
Check(s.Tick(0.1f) && s.offset > 0 && s.velocity < 1000, "a fling moves the content and slows down");
|
||
|
|
while(s.Tick(0.1f)) {
|
||
|
|
}
|
||
|
|
Check(s.velocity == 0 && s.offset > 0 && s.offset <= 600, "a fling comes to rest within the extent");
|
||
|
|
s.offset = 0;
|
||
|
|
s.Fling(-500);
|
||
|
|
Check(!s.Tick(0.1f) && s.offset == 0, "a fling against the edge stops at once");
|
||
|
|
s.Fling(10);
|
||
|
|
Check(!s.Tick(0.1f) && s.velocity == 0, "one too slow to see is none");
|
||
|
|
s.content = 300;
|
||
|
|
s.offset = 250;
|
||
|
|
s.Clamp();
|
||
|
|
Equal(s.offset, 0.0f, "content shorter than the viewport does not scroll");
|
||
|
|
|
||
|
|
// A field that got the focus, in a form of 1000 pixels shown 400 at a time.
|
||
|
|
s.content = 1000;
|
||
|
|
s.offset = 0;
|
||
|
|
Check(!s.EnsureVisible(100, 148, 16) && s.offset == 0, "what is in view stays where it is");
|
||
|
|
Check(s.EnsureVisible(500, 548, 16) && s.offset == 164, "what is below comes up to the bottom edge, the margin clear of it");
|
||
|
|
s.Fling(900);
|
||
|
|
Check(s.EnsureVisible(100, 148, 16) && s.offset == 84 && s.velocity == 0, "what is above comes down to the top edge, and a fling under it stops");
|
||
|
|
Check(s.EnsureVisible(980, 1000, 16) && s.offset == 600, "the margin gives way at the content's end");
|
||
|
|
Check(s.EnsureVisible(200, 900, 16) && s.offset == 184, "what is taller than the room shows its top");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Clock() {
|
||
|
|
EventClock clock;
|
||
|
|
Equal(clock.At(0, 5000), std::int64_t{5000}, "an event without a time happened now");
|
||
|
|
Equal(clock.At(1000, 5000), std::int64_t{5000}, "the first stamped event too");
|
||
|
|
Equal(clock.At(1016, 5040), std::int64_t{5016}, "the next one when its stamp says, not when it was dispatched: it waited 24 ms");
|
||
|
|
Equal(clock.At(1100, 5095), std::int64_t{5095}, "one dispatched sooner than any before shows the clocks are nearer: the difference is the smallest seen");
|
||
|
|
Equal(clock.At(1200, 5300), std::int64_t{5195}, "and the later ones go by it");
|
||
|
|
EventClock wrapping;
|
||
|
|
wrapping.At(0xFFFFFFF0u, 100);
|
||
|
|
Equal(wrapping.At(0x10u, 132), std::int64_t{132}, "a stamp that wrapped at 2^32 is 32 ms on, not 4 billion back");
|
||
|
|
Equal(wrapping.At(0x8u, 140), std::int64_t{124}, "and one before it is before it");
|
||
|
|
}
|
||
|
|
|
||
|
|
void AtlasFrames() {
|
||
|
|
FrameWait wait{.patience = 4};
|
||
|
|
Check(!wait.Again(0), "no glyph waits: no frame owed");
|
||
|
|
Check(wait.Again(5) && wait.Again(5) && wait.Again(5) && wait.Again(5), "glyphs waiting: a frame is owed at the first sighting and at three gainless ones after it");
|
||
|
|
Check(!wait.Again(5), "the fourth gainless frame ends it: the screen needs more cells than the atlas holds");
|
||
|
|
wait.Reset();
|
||
|
|
Check(wait.Again(5) && wait.Again(3) && wait.Again(3) && wait.Again(1) && !wait.Again(0), "a gain starts the count over; none waiting ends it");
|
||
|
|
Check(wait.Again(2) && wait.Again(7) && wait.Again(7) && wait.Again(7) && wait.Again(7) && !wait.Again(7), "a changed count, as a screen change makes, starts the count over");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
Gestures();
|
||
|
|
Scrolling();
|
||
|
|
Clock();
|
||
|
|
AtlasFrames();
|
||
|
|
return Test::Result();
|
||
|
|
}
|