Skip to content

Commit

Permalink
chore: Adjust for interaction tracker test
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 13, 2025
1 parent dd1ea84 commit f280423
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/Uno.UI.RuntimeTests/Helpers/UITestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public interface IInjectedPointer
{
void Press(Point position);

void MoveTo(Point position, uint? steps = null);
void MoveTo(Point position, uint? steps = null, uint? stepOffsetInMilliseconds = null);

void MoveBy(double deltaX = 0, double deltaY = 0);

Expand Down Expand Up @@ -383,17 +383,18 @@ public static void Press(this IInjectedPointer pointer, double x, double y)
public static void MoveTo(this IInjectedPointer pointer, double x, double y)
=> pointer.MoveTo(new(x, y));

public static void Drag(this IInjectedPointer pointer, Point from, Point to)
public static void Drag(this IInjectedPointer pointer, Point from, Point to, uint? steps = null, uint? stepOffsetInMilliseconds = null)
{
pointer.Press(from);
pointer.MoveTo(to);
pointer.MoveTo(to, steps, stepOffsetInMilliseconds);
pointer.Release();
}
}

public partial class Finger : IInjectedPointer, IDisposable
{
private const uint _defaultMoveSteps = 10;
private const uint _defaultStepOffsetInMilliseconds = 1;

private readonly InputInjector _injector;
private readonly uint _id;
Expand All @@ -417,12 +418,13 @@ public void Press(Point position)
}
}

void IInjectedPointer.MoveTo(Point position, uint? steps) => MoveTo(position, steps ?? _defaultMoveSteps);
public void MoveTo(Point position, uint steps = _defaultMoveSteps)
void IInjectedPointer.MoveTo(Point position, uint? steps, uint? stepOffsetInMilliseconds) =>
MoveTo(position, steps ?? _defaultMoveSteps, stepOffsetInMilliseconds ?? _defaultStepOffsetInMilliseconds);
public void MoveTo(Point position, uint steps = _defaultMoveSteps, uint stepOffsetInMilliseconds = _defaultStepOffsetInMilliseconds)
{
if (_currentPosition is { } current)
{
Inject(GetMove(current, position, steps));
Inject(GetMove(current, position, steps, stepOffsetInMilliseconds));
_currentPosition = position;
}
}
Expand Down Expand Up @@ -465,7 +467,7 @@ public static InjectedInputTouchInfo GetPress(uint id, Point position)
}
};

public static IEnumerable<InjectedInputTouchInfo> GetMove(Point fromPosition, Point toPosition, uint steps = _defaultMoveSteps)
public static IEnumerable<InjectedInputTouchInfo> GetMove(Point fromPosition, Point toPosition, uint steps = _defaultMoveSteps, uint stepOffsetInMilliseconds = _defaultStepOffsetInMilliseconds)
{
steps += 1; // We need to send at least the final location, but steps refers to the number of intermediate points

Expand All @@ -477,7 +479,7 @@ public static IEnumerable<InjectedInputTouchInfo> GetMove(Point fromPosition, Po
{
PointerInfo = new()
{
TimeOffsetInMilliseconds = 1,
TimeOffsetInMilliseconds = stepOffsetInMilliseconds,
PixelLocation = At(fromPosition.X + step * stepX, fromPosition.Y + step * stepY),
PointerOptions = InjectedInputPointerOptions.Update
| InjectedInputPointerOptions.FirstButton
Expand Down Expand Up @@ -620,10 +622,10 @@ public void ReleaseAny()
}

public void MoveBy(double deltaX, double deltaY)
=> Inject(GetMoveBy(deltaX, deltaY));
=> Inject(GetMoveBy(deltaX, deltaY, 1));

public void MoveTo(Point position, uint? steps = null)
=> Inject(GetMoveTo(position.X, position.Y, steps));
public void MoveTo(Point position, uint? steps = null, uint? stepOffsetInMilliseconds = null)
=> Inject(GetMoveTo(position.X, position.Y, steps, stepOffsetInMilliseconds));

public void WheelUp() => Wheel(ScrollContentPresenter.ScrollViewerDefaultMouseWheelDelta);
public void WheelDown() => Wheel(-ScrollContentPresenter.ScrollViewerDefaultMouseWheelDelta);
Expand All @@ -633,14 +635,16 @@ public void MoveTo(Point position, uint? steps = null)
public void Wheel(double delta, bool isHorizontal = false, uint steps = 1)
=> Inject(GetWheel(delta, isHorizontal, steps));

private IEnumerable<InjectedInputMouseInfo> GetMoveTo(double x, double y, uint? steps)
private IEnumerable<InjectedInputMouseInfo> GetMoveTo(double x, double y, uint? steps, uint? stepOffsetInMilliseconds = null)
{
var x0 = Current.X;
var y0 = Current.Y;
var deltaX = x - x0;
var deltaY = y - y0;

steps ??= (uint)Math.Min(Math.Max(Math.Abs(deltaX), Math.Abs(deltaY)), 512);
stepOffsetInMilliseconds ??= 1;

if (steps is 0)
{
yield break;
Expand All @@ -658,7 +662,7 @@ private IEnumerable<InjectedInputMouseInfo> GetMoveTo(double x, double y, uint?
var newPositionX = (int)Math.Round(x0 + i * stepX);
var newPositionY = (int)Math.Round(y0 + i * stepY);

yield return GetMoveBy(newPositionX - prevPositionX, newPositionY - prevPositionY);
yield return GetMoveBy(newPositionX - prevPositionX, newPositionY - prevPositionY, stepOffsetInMilliseconds.Value);

prevPositionX = newPositionX;
prevPositionY = newPositionY;
Expand Down Expand Up @@ -694,12 +698,12 @@ private static InjectedInputMouseInfo GetRightPress()
MouseOptions = InjectedInputMouseOptions.RightDown,
};

private static InjectedInputMouseInfo GetMoveBy(double deltaX, double deltaY)
private static InjectedInputMouseInfo GetMoveBy(double deltaX, double deltaY, uint stepOffsetInMilliseconds)
=> new()
{
DeltaX = (int)deltaX,
DeltaY = (int)deltaY,
TimeOffsetInMilliseconds = 1,
TimeOffsetInMilliseconds = stepOffsetInMilliseconds,
MouseOptions = InjectedInputMouseOptions.MoveNoCoalesce,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public async Task When_UserInteraction()

var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
var finger = injector.GetFinger();
finger.Drag(new(position.Left + 50, position.Top + 50), new(position.Left + 100, position.Top + 50));
finger.Drag(new(position.Left + 50, position.Top + 50), new(position.Left + 100, position.Top + 50), stepOffsetInMilliseconds: 0);

string logs = await WaitTrackerLogs(tracker);
var helper = new TrackerAssertHelper(logs);
Expand Down

0 comments on commit f280423

Please sign in to comment.