Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/Uno.Foundation/Extensibility/ApiExtensibility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Uno.Foundation.Extensibility;
#nullable enable

using System.Diagnostics.CodeAnalysis;

namespace Uno.Foundation.Extensibility;

/// <summary>
/// Registry for API existensibility providers, used to provide optional
Expand Down Expand Up @@ -60,8 +64,9 @@ public static void Register<TOwner>(Type type, Func<TOwner, object> builder)
/// <typeparam name="T">A registered type</typeparam>
/// <param name="owner">An optional owner to be passed to the extension constructor</param>
/// <param name="instance">The instance if the creation was successful</param>
/// <returns>True if the creation suceeded, otherwise False.</returns>
public static bool CreateInstance<T>(object owner, out T instance) where T : class
/// <returns>True if the creation succeeded, otherwise False.</returns>
public static bool CreateInstance<T>(object owner, [NotNullWhen(true)] out T? instance)
where T : class
{
lock (_gate)
{
Expand All @@ -76,4 +81,17 @@ public static bool CreateInstance<T>(object owner, out T instance) where T : cla

return false;
}

internal static T CreateInstance<T>(object owner)
where T : class
{
if (CreateInstance<T>(owner, out var instance))
{
return instance;
}
else
{
throw new InvalidOperationException($"Unable to find {typeof(T)} extension");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void Initialize()
{
_isDispatcherThread = true;

ApiExtensibility.Register(typeof(Uno.ApplicationModel.Core.ICoreApplicationExtension), o => _coreApplicationExtension);
ApiExtensibility.Register(typeof(Uno.ApplicationModel.Core.ICoreApplicationExtension), o => _coreApplicationExtension!);
ApiExtensibility.Register(typeof(Windows.UI.Core.ICoreWindowExtension), o => new CoreWindowExtension(o));
ApiExtensibility.Register(typeof(Windows.UI.ViewManagement.IApplicationViewExtension), o => new ApplicationViewExtension(o));
ApiExtensibility.Register<Application>(typeof(Uno.UI.Xaml.IApplicationExtension), o => new ApplicationExtension(o));
Expand Down
200 changes: 200 additions & 0 deletions src/Uno.UI.RuntimeTests/Helpers/UITestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Input.Preview.Injection;
using Windows.UI.Xaml;
using Private.Infrastructure;

namespace Uno.UI.RuntimeTests.Tests.Uno_UI_Xaml_Core;

// Note: This file contains a bunch of helpers that are expected to be moved to the test engine among the pointer injection work

public static class UITestHelper
{
public static async Task<Windows.Foundation.Rect> Load(FrameworkElement element)
{
TestServices.WindowHelper.WindowContent = element;
await TestServices.WindowHelper.WaitForLoaded(element);
element.UpdateLayout();
await TestServices.WindowHelper.WaitForIdle();

return element.GetAbsoluteBounds();
}
}

public static class InputInjectorExtensions
{
public static Finger GetFinger(this InputInjector injector, uint id = 42)
=> new(injector, id);
}

public interface IInjectedPointer
{
void Press(Point position);

void MoveTo(Point position);

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

void Release();
}

public static class FrameworkElementExtensions
{
public static Rect GetAbsoluteBounds(this FrameworkElement element)
=> element.TransformToVisual(null).TransformBounds(new Rect(0, 0, element.ActualWidth, element.ActualHeight));
}

public static class PointExtensions
{
public static Point Offset(this Point point, double xAndY)
=> new(point.X + xAndY, point.Y + xAndY);

public static Point Offset(this Point point, double x, double y)
=> new(point.X + x, point.Y + y);
}

public static class InjectedPointerExtensions
{
public static void Press(this IInjectedPointer pointer, double x, double y)
=> pointer.Press(new(x, 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)
{
pointer.Press(from);
pointer.MoveTo(to);
pointer.Release();
}
}

public class Finger : IInjectedPointer, IDisposable
{
private const int _defaultMoveSteps = 10;

private readonly InputInjector _injector;
private readonly uint _id;

private Point? _currentPosition;

public Finger(InputInjector injector, uint id)
{
_injector = injector;
_id = id;

_injector.InitializeTouchInjection(InjectedInputVisualizationMode.Default);
}

public void Press(Point position)
{
if (_currentPosition is null)
{
Inject(GetPress(_id, position));
_currentPosition = position;
}
}

void IInjectedPointer.MoveTo(Point position) => MoveTo(position);
public void MoveTo(Point position, int steps = _defaultMoveSteps)
{
if (_currentPosition is { } current)
{
Inject(GetMove(current, position, steps));
_currentPosition = position;
}
}

void IInjectedPointer.MoveBy(double deltaX, double deltaY) => MoveBy(deltaX, deltaY);
public void MoveBy(double deltaX, double deltaY, int steps = _defaultMoveSteps)
{
if (_currentPosition is { } current)
{
MoveTo(current.Offset(deltaX, deltaY), steps);
}
}

public void Release()
{
if (_currentPosition is { } current)
{
Inject(GetRelease(current));
_currentPosition = null;
}
}

public void Dispose()
{
Release();
_injector.UninitializeTouchInjection();
}

public static InjectedInputTouchInfo GetPress(uint id, Point position)
=> new()
{
PointerInfo = new()
{
PointerId = id,
PixelLocation = new()
{
PositionX = (int)position.X,
PositionY = (int)position.Y
},
PointerOptions = InjectedInputPointerOptions.New
| InjectedInputPointerOptions.FirstButton
| InjectedInputPointerOptions.PointerDown
| InjectedInputPointerOptions.InContact
}
};

public static IEnumerable<InjectedInputTouchInfo> GetMove(Point fromPosition, Point toPosition, int steps = _defaultMoveSteps)
{
var stepX = (toPosition.X - fromPosition.X) / steps;
var stepY = (toPosition.Y - fromPosition.Y) / steps;
for (var step = 0; step <= steps; step++)
{
yield return new()
{
PointerInfo = new()
{
PixelLocation = new()
{
PositionX = (int)(fromPosition.X + step * stepX),
PositionY = (int)(fromPosition.Y + step * stepY)
},
PointerOptions = InjectedInputPointerOptions.Update
| InjectedInputPointerOptions.FirstButton
| InjectedInputPointerOptions.InContact
| InjectedInputPointerOptions.InRange
}
};
}
}

public static InjectedInputTouchInfo GetRelease(Point position)
=> new()
{
PointerInfo = new()
{
PixelLocation =
{
PositionX = (int)position.X,
PositionY = (int)position.Y
},
PointerOptions = InjectedInputPointerOptions.FirstButton
| InjectedInputPointerOptions.PointerUp
}
};

private void Inject(IEnumerable<InjectedInputTouchInfo> infos)
=> _injector.InjectTouchInput(infos);

private void Inject(params InjectedInputTouchInfo[] infos)
=> _injector.InjectTouchInput(infos);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#if __SKIA__
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Appointments;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Input.Preview.Injection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Uno.UI.RuntimeTests.Tests.Uno_UI_Xaml_Core;

[TestClass]
[RunsOnUIThread]
public class Given_InputManager
{
[TestMethod]
#if !__SKIA__
[Ignore("Pointer injection supported only on skia for now.")]
#endif
public async Task When_VisibilityChangesWhileDispatching_Then_RecomputeOriginalSource()
{
if (Private.Infrastructure.TestServices.WindowHelper.IsXamlIsland)
{
Assert.Inconclusive("Pointer injection is not supported yet on XamlIsland");
return;
}

Border col1, col2;
var ui = new Grid
{
Width = 200,
Height = 200,
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
},
Children =
{
(col1 = new Border { Background = new SolidColorBrush(Colors.DeepPink) }.GridColumn(0)),
(col2 = new Border { Background = new SolidColorBrush(Colors.DeepSkyBlue) }.GridColumn(1)),
}
};

var position = await UITestHelper.Load(ui);

var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
using var finger = injector.GetFinger();

var failed = false;
col1.PointerExited += (snd, args) => col2.Visibility = Visibility.Collapsed;
col2.PointerEntered += (snd, args) => failed = true;

injector.GetFinger().Drag(position.Location.Offset(10), position.Location.Offset(180, 10));

Assert.AreEqual(Visibility.Collapsed, col2.Visibility, "The visibility should have been changed when the pointer left the col1.");
Assert.IsFalse(failed, "The pointer should not have been dispatched to the col2 as it has been set to visibility collapsed.");
}
}
#endif
Loading