Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
332 lines
15 KiB
C#
332 lines
15 KiB
C#
using System.Text.Json.Nodes;
|
||
using Febe.Browser.Gestures;
|
||
using Febe.Browser.Targets;
|
||
using Febe.Core.Abstractions;
|
||
using Febe.Core.Domain;
|
||
using Febe.Tests.Browser.TestSupport;
|
||
|
||
namespace Febe.Tests.Browser;
|
||
|
||
[TestClass]
|
||
public sealed class GestureTests
|
||
{
|
||
[TestMethod]
|
||
public async Task PointerTravelsSmoothlyAlongACurveInHumanTime()
|
||
{
|
||
for (var seed = 0; seed < 40; seed++)
|
||
{
|
||
var (gestures, cdp, clock) = Create(seed);
|
||
var start = new PagePoint(100, 700);
|
||
var target = new PagePoint(900, 200);
|
||
await gestures.MoveToAsync(start);
|
||
cdp.ClearSent();
|
||
var t0 = clock.Now;
|
||
|
||
await gestures.MoveToAsync(target);
|
||
|
||
var moves = cdp.Mouse("mouseMoved");
|
||
Assert.IsTrue(moves.All(m => m.Method == "Input.dispatchMouseEvent"));
|
||
var duration = (moves[^1].At - t0).TotalMilliseconds;
|
||
Assert.IsTrue(duration is >= 250 * 0.85 and <= 900 * 1.15, $"seed {seed}: travel took {duration:F0} ms.");
|
||
|
||
var intervals = moves.Zip(moves.Skip(1), (a, b) => (b.At - a.At).TotalMilliseconds).ToArray();
|
||
var medianInterval = intervals.Order().ElementAt(intervals.Length / 2);
|
||
Assert.IsTrue(medianInterval is >= 12 and <= 22, $"seed {seed}: about 60 events per second (median {medianInterval:F1} ms).");
|
||
|
||
Assert.AreEqual(target.X, moves[^1].X);
|
||
Assert.AreEqual(target.Y, moves[^1].Y);
|
||
|
||
// Never a jump: no step longer than a fraction of the distance.
|
||
var points = new[] { start }.Concat(moves.Select(m => new PagePoint(m.X, m.Y))).ToArray();
|
||
var steps = points.Zip(points.Skip(1), (a, b) => a.DistanceTo(b)).ToArray();
|
||
var distance = start.DistanceTo(target);
|
||
Assert.IsTrue(moves.Count >= 10, $"seed {seed}: only {moves.Count} pointer events.");
|
||
Assert.IsTrue(steps.Max() < distance / 5, $"seed {seed}: largest step {steps.Max():F0} px of {distance:F0}.");
|
||
|
||
// Eased: the first and last steps are shorter than the steps in the middle.
|
||
var middle = steps.Skip(steps.Length / 3).Take(steps.Length / 3).Average();
|
||
Assert.IsTrue(steps[0] < middle && steps[^1] < middle, $"seed {seed}: start and arrival are soft.");
|
||
|
||
// Slightly curved: off the straight line, but not by much.
|
||
var deviation = points.Max(p => DistanceFromLine(p, start, target));
|
||
Assert.IsTrue(deviation > 1 && deviation < distance * 0.15, $"seed {seed}: bend of {deviation:F1} px.");
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ClickLandsInTheCentralPartNeverTheExactCentreWithHumanTimings()
|
||
{
|
||
var box = new PageRect(400, 300, 120, 40);
|
||
for (var seed = 0; seed < 200; seed++)
|
||
{
|
||
var (gestures, cdp, _) = Create(seed);
|
||
var at = await gestures.ClickAsync(box);
|
||
|
||
var central = new PageRect(box.X + (box.Width * 0.2) - 0.5, box.Y + (box.Height * 0.2) - 0.5, (box.Width * 0.6) + 1, (box.Height * 0.6) + 1);
|
||
Assert.IsTrue(central.Contains(at), $"seed {seed}: {at} is outside the central part.");
|
||
Assert.AreNotEqual(box.Center, at, $"seed {seed}: exact centre.");
|
||
|
||
var events = cdp.Mouse();
|
||
var press = events.Single(e => e.Type == "mousePressed");
|
||
var release = events.Single(e => e.Type == "mouseReleased");
|
||
var lastMove = events.Last(e => e.Type == "mouseMoved");
|
||
Assert.AreEqual("left", press.Parameters!["button"]!.GetValue<string>());
|
||
Assert.AreEqual(at.X, press.X);
|
||
Assert.AreEqual(at.Y, press.Y);
|
||
|
||
var hover = (press.At - lastMove.At).TotalMilliseconds;
|
||
var hold = (release.At - press.At).TotalMilliseconds;
|
||
Assert.IsTrue(hover is >= 80 and <= 300, $"seed {seed}: hover {hover:F0} ms.");
|
||
Assert.IsTrue(hold is >= 50 and <= 140, $"seed {seed}: press-release {hold:F0} ms.");
|
||
Assert.IsTrue(events.IndexOf(press) > events.IndexOf(lastMove) && events.IndexOf(release) == events.Count - 1);
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ClickOnATinyTargetStaysInside()
|
||
{
|
||
var box = new PageRect(10, 10, 6, 6);
|
||
for (var seed = 0; seed < 100; seed++)
|
||
{
|
||
var (gestures, _, _) = Create(seed);
|
||
var at = await gestures.ClickAsync(box);
|
||
Assert.IsTrue(box.Contains(at), $"seed {seed}: {at} outside {box}.");
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task WheelBurstsSlowDown()
|
||
{
|
||
for (var seed = 0; seed < 100; seed++)
|
||
{
|
||
var (gestures, cdp, _) = Create(seed);
|
||
await gestures.MoveToAsync(new PagePoint(640, 450));
|
||
cdp.ClearSent();
|
||
|
||
var total = await gestures.ScrollBurstAsync();
|
||
|
||
var wheels = cdp.Mouse("mouseWheel");
|
||
Assert.IsTrue(wheels.Count is >= 3 and <= 8, $"seed {seed}: {wheels.Count} events.");
|
||
Assert.IsTrue(wheels.All(w => w.DeltaY is >= 80 and <= 140), $"seed {seed}: deltas out of range.");
|
||
Assert.IsTrue(wheels.Zip(wheels.Skip(1), (a, b) => b.DeltaY <= a.DeltaY).All(x => x), "Deltas shrink.");
|
||
var intervals = wheels.Zip(wheels.Skip(1), (a, b) => (b.At - a.At).TotalMilliseconds).ToArray();
|
||
Assert.IsTrue(intervals.All(i => i is >= 12 and <= 40), $"seed {seed}: intervals out of range.");
|
||
Assert.IsTrue(intervals.Zip(intervals.Skip(1), (a, b) => b >= a).All(x => x), "Intervals grow.");
|
||
Assert.IsTrue(wheels.All(w => w.X == 640 && w.Y == 450), "The wheel turns where the pointer is.");
|
||
Assert.AreEqual(wheels.Sum(w => w.DeltaY), total);
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task PausesBetweenBurstsAndReadingPausesFollowTheTable()
|
||
{
|
||
var (gestures, _, clock) = Create(11);
|
||
var readingAt = new List<int>();
|
||
var normal = new List<double>();
|
||
var reading = new List<double>();
|
||
for (var burst = 1; burst <= 400; burst++)
|
||
{
|
||
var before = clock.Now;
|
||
if (await gestures.PauseAfterBurstAsync())
|
||
{
|
||
readingAt.Add(burst);
|
||
reading.Add((clock.Now - before).TotalSeconds);
|
||
}
|
||
else
|
||
{
|
||
normal.Add((clock.Now - before).TotalSeconds);
|
||
}
|
||
}
|
||
|
||
var gaps = readingAt.Zip(readingAt.Skip(1), (a, b) => b - a).ToArray();
|
||
Assert.IsTrue(gaps.All(g => g is >= 6 and <= 10), "A reading pause every 6–10 bursts.");
|
||
Assert.IsTrue(normal.All(s => s is >= 0.4 and <= 4));
|
||
Assert.AreEqual(1.1, normal.Order().ElementAt(normal.Count / 2), 0.2);
|
||
Assert.IsTrue(reading.All(s => s is >= 3 and <= 20));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ScrollStopsWhenTheRecipeSaysSo()
|
||
{
|
||
var (gestures, cdp, _) = Create(3);
|
||
var calls = 0;
|
||
var bursts = await gestures.ScrollAsync(8, _ => ValueTask.FromResult(++calls < 2));
|
||
Assert.AreEqual(2, bursts);
|
||
Assert.IsTrue(cdp.Sent.All(c => c.Method == "Input.dispatchMouseEvent"), "Scrolling never uses Runtime.evaluate.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ScrollingSometimesGoesBackALittle()
|
||
{
|
||
var (gestures, cdp, _) = Create(5);
|
||
await gestures.ScrollAsync(200);
|
||
var up = cdp.Mouse("mouseWheel").Where(w => w.DeltaY < 0).ToList();
|
||
Assert.IsTrue(up.Count > 0, "Some small scroll backs happen.");
|
||
Assert.IsTrue(up.All(w => w.DeltaY is >= -100 and <= -40));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task WaitAfterLoadHasMedian22Seconds()
|
||
{
|
||
var (gestures, _, clock) = Create(21);
|
||
var waits = new List<double>();
|
||
for (var i = 0; i < 3000; i++)
|
||
{
|
||
var before = clock.Now;
|
||
await gestures.WaitAfterLoadAsync();
|
||
waits.Add((clock.Now - before).TotalSeconds);
|
||
}
|
||
|
||
Assert.IsTrue(waits.All(w => w is >= 1 and <= 6));
|
||
Assert.AreEqual(2.2, waits.Order().ElementAt(waits.Count / 2), 0.1);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task DistractionsHappenAboutEightPercentOfTheTime()
|
||
{
|
||
var (gestures, _, clock) = Create(31);
|
||
var lengths = new List<double>();
|
||
const int Trials = 5000;
|
||
for (var i = 0; i < Trials; i++)
|
||
{
|
||
var before = clock.Now;
|
||
if (await gestures.MaybeDistractionAsync())
|
||
{
|
||
lengths.Add((clock.Now - before).TotalSeconds);
|
||
}
|
||
}
|
||
|
||
Assert.AreEqual(0.08, lengths.Count / (double)Trials, 0.012);
|
||
Assert.IsTrue(lengths.All(l => l is >= 15 and <= 90));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task MoodAndPaceMultiplyEveryWait()
|
||
{
|
||
var cdp = new FakeCdpSession(new VirtualClock());
|
||
var clock = new VirtualClock();
|
||
var slow = new HumanGestures(cdp, clock, new SeededRandom(1), mood: 1.5, pace: GesturePace.Slow);
|
||
Assert.AreEqual(1.5 * 1.35, slow.Multiplier, 1e-9);
|
||
for (var i = 0; i < 500; i++)
|
||
{
|
||
var waited = await slow.WaitAsync(slow.Settings.AfterLoadMs);
|
||
Assert.IsTrue(waited.TotalSeconds >= 1 * slow.Multiplier - 1e-6 && waited.TotalSeconds <= 6 * slow.Multiplier + 1e-6);
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task NothingMovesWhileAlbertoIsDriving()
|
||
{
|
||
var clock = new VirtualClock();
|
||
var cdp = new FakeCdpSession(clock);
|
||
var driving = true;
|
||
var gestures = new HumanGestures(cdp, clock, new SeededRandom(1), userIsDriving: () => driving);
|
||
|
||
await Assert.ThrowsExactlyAsync<GestureInterruptedException>(() => gestures.ClickAsync(new PageRect(10, 10, 50, 20)));
|
||
await Assert.ThrowsExactlyAsync<GestureInterruptedException>(() => gestures.ScrollBurstAsync());
|
||
Assert.AreEqual(0, cdp.Sent.Count);
|
||
|
||
driving = false;
|
||
await gestures.ScrollBurstAsync();
|
||
Assert.IsTrue(cdp.Sent.Count > 0);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task KeyboardTypesThroughDispatchKeyEvent()
|
||
{
|
||
var clock = new VirtualClock();
|
||
var cdp = new FakeCdpSession(clock);
|
||
await Keyboard.TypeTextAsync(cdp, "Ab è", clock, new SeededRandom(1));
|
||
|
||
Assert.IsTrue(cdp.Sent.All(c => c.Method == "Input.dispatchKeyEvent"));
|
||
var downs = cdp.Sent.Where(c => c.Type == "keyDown").Select(c => c.Parameters!["text"]!.GetValue<string>()).ToArray();
|
||
CollectionAssert.AreEqual(new[] { "A", "b", " ", "è" }, downs);
|
||
Assert.AreEqual(4, cdp.Sent.Count(c => c.Type == "keyUp"));
|
||
|
||
Assert.AreEqual("\r", Keyboard.Special(SpecialKey.Enter).Text);
|
||
cdp.ClearSent();
|
||
await Keyboard.KeyDownAsync(cdp, Keyboard.ForText("a") with { Modifiers = KeyModifiers.Control });
|
||
Assert.AreEqual("rawKeyDown", cdp.Sent.Single().Type, "Shortcuts do not insert text.");
|
||
Assert.AreEqual("KeyStroke(…)", Keyboard.ForText("x").ToString(), "Keys never print themselves.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task TargetActionsClickOnlyWhatTheMapFinds()
|
||
{
|
||
var map = TargetMap.Parse("""{ "targets": { "avanti": { "role": "button", "name": "^avanti$" } } }""");
|
||
|
||
// Found and unobstructed: one click inside the element.
|
||
var (gestures, cdp, _) = Create(1);
|
||
cdp.Responder = (method, _) => method == "Runtime.evaluate" ? Matches((300, 200, 80, 30, true, true, true)) : null;
|
||
var result = await new TargetActions(new TargetResolver(cdp, map), gestures).ClickAsync("avanti");
|
||
Assert.IsTrue(result.Clicked);
|
||
Assert.IsTrue(new PageRect(300, 200, 80, 30).Contains(result.At!.Value));
|
||
Assert.AreEqual(1, cdp.Mouse("mousePressed").Count);
|
||
|
||
// Not found: no input at all, and a clear reason.
|
||
(gestures, cdp, _) = Create(2);
|
||
cdp.Responder = (method, _) => method == "Runtime.evaluate" ? Matches() : null;
|
||
result = await new TargetActions(new TargetResolver(cdp, map), gestures).ClickAsync("avanti");
|
||
Assert.IsFalse(result.Clicked);
|
||
Assert.AreEqual(TargetStatus.NotFound, result.Lookup.Status);
|
||
StringAssert.Contains(result.Lookup.Message, "non trovato");
|
||
Assert.AreEqual(0, cdp.Mouse().Count);
|
||
|
||
// Covered by something else: no click.
|
||
(gestures, cdp, _) = Create(3);
|
||
cdp.Responder = (method, _) => method == "Runtime.evaluate" ? Matches((300, 200, 80, 30, true, true, false)) : null;
|
||
result = await new TargetActions(new TargetResolver(cdp, map), gestures).ClickAsync("avanti");
|
||
Assert.AreEqual(TargetStatus.Obstructed, result.Lookup.Status);
|
||
Assert.AreEqual(0, cdp.Mouse("mousePressed").Count);
|
||
|
||
// Below the fold: wheel bursts until it is in view, then click.
|
||
(gestures, cdp, _) = Create(4);
|
||
var lookups = 0;
|
||
cdp.Responder = (method, _) => method != "Runtime.evaluate"
|
||
? null
|
||
: ++lookups < 3 ? Matches((300, 1500, 80, 30, true, false, false)) : Matches((300, 400, 80, 30, true, true, true));
|
||
result = await new TargetActions(new TargetResolver(cdp, map), gestures).ClickAsync("avanti");
|
||
Assert.IsTrue(result.Clicked);
|
||
Assert.IsTrue(cdp.Mouse("mouseWheel").Count >= 6, "Two bursts of at least three events.");
|
||
Assert.IsTrue(cdp.Mouse("mouseWheel").All(w => w.DeltaY > 0), "Scrolled down toward the target.");
|
||
}
|
||
|
||
private static JsonNode Matches(params (double X, double Y, double W, double H, bool Visible, bool InViewport, bool Unobstructed)[] items) =>
|
||
new JsonObject
|
||
{
|
||
["result"] = new JsonObject
|
||
{
|
||
["type"] = "object",
|
||
["value"] = new JsonObject
|
||
{
|
||
["viewportWidth"] = 1280,
|
||
["viewportHeight"] = 900,
|
||
["matches"] = new JsonArray([.. items.Select(i => (JsonNode)new JsonObject
|
||
{
|
||
["x"] = i.X,
|
||
["y"] = i.Y,
|
||
["width"] = i.W,
|
||
["height"] = i.H,
|
||
["visible"] = i.Visible,
|
||
["inViewport"] = i.InViewport,
|
||
["unobstructed"] = i.Unobstructed,
|
||
})]),
|
||
},
|
||
},
|
||
};
|
||
|
||
private static (HumanGestures Gestures, FakeCdpSession Cdp, VirtualClock Clock) Create(int seed)
|
||
{
|
||
var clock = new VirtualClock();
|
||
var cdp = new FakeCdpSession(clock);
|
||
return (new HumanGestures(cdp, clock, new SeededRandom(seed)), cdp, clock);
|
||
}
|
||
|
||
private static double DistanceFromLine(PagePoint p, PagePoint a, PagePoint b)
|
||
{
|
||
var dx = b.X - a.X;
|
||
var dy = b.Y - a.Y;
|
||
return Math.Abs((dy * p.X) - (dx * p.Y) + (b.X * a.Y) - (b.Y * a.X)) / Math.Sqrt((dx * dx) + (dy * dy));
|
||
}
|
||
}
|