Skip to content

Commit

Permalink
test: AppWindow properties should be in physical units
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Nov 1, 2024
1 parent 181a98f commit 2bd3a12
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,94 @@ namespace Uno.UI.RuntimeTests.Tests.Microsoft_UI_Windowing;
[RunsOnUIThread]
public class Given_AppWindow
{
#if HAS_UNO
[TestMethod]
public void When_Size_Applies_Scale()
{
var appWindow = TestServices.WindowHelper.CurrentTestWindow.AppWindow;
var nativeWrapper = TestServices.WindowHelper.CurrentTestWindow.NativeWindowWrapper;
var originalRasterizationScale = nativeWrapper.RasterizationScale;
var originalSize = appWindow.Size;

try
{
var newRasterizationScale = originalRasterizationScale * 2;
nativeWrapper.RasterizationScale = newRasterizationScale;
var newSize = appWindow.Size;
Assert.AreEqual(originalSize.Width * 2, newSize.Width, 1);
Assert.AreEqual(originalSize.Height * 2, newSize.Height, 1);
}
finally
{
nativeWrapper.RasterizationScale = originalRasterizationScale;
}
}

[TestMethod]
public void When_Resize_Applies_Scale()
{
AssertPositioningAndSizingSupport();

var appWindow = TestServices.WindowHelper.CurrentTestWindow.AppWindow;
var nativeWrapper = TestServices.WindowHelper.CurrentTestWindow.NativeWindowWrapper;
var originalRasterizationScale = nativeWrapper.RasterizationScale;
var originalSize = appWindow.Size;
var newSize = new SizeInt32() { Width = 100, Height = 100 };
try
{
appWindow.Resize(newSize);
var newSize = appWindow.Size;
Assert.AreEqual(100, newSize.Width, 1);
Assert.AreEqual(100, newSize.Height, 1);
var newRasterizationScale = originalRasterizationScale * 2;
nativeWrapper.RasterizationScale = newRasterizationScale;
var newSizeScaled = appWindow.Size;
Assert.AreEqual(newSize.Width * 2, newSizeScaled.Width, 1);
Assert.AreEqual(newSize.Height * 2, newSizeScaled.Height, 1);
}
finally
{
nativeWrapper.RasterizationScale = originalRasterizationScale;
appWindow.Resize(originalSize);
}
}

#if __SKIA__
[TestMethod]
public void When_Position_Applies_Scale()
{
AssertPositioningAndSizingSupport();

var appWindow = TestServices.WindowHelper.CurrentTestWindow.AppWindow;
var nativeWrapper = TestServices.WindowHelper.CurrentTestWindow.NativeWindowWrapper;
var originalRasterizationScale = nativeWrapper.RasterizationScale;
var originalPosition = appWindow.Position;

var newPosition = new PointInt32() { X = 10, Y = 10 };

try
{
appWindow.Move(newPosition);
var newPosition = appWindow.Position;
Assert.AreEqual(10, newPosition.X, 1);
Assert.AreEqual(10, newPosition.Y, 1);

var newRasterizationScale = originalRasterizationScale * 2;
nativeWrapper.RasterizationScale = newRasterizationScale;
var newPositionScaled = appWindow.Position;

Assert.AreEqual(newPosition.X * 2, newPositionScaled.X, 1);
Assert.AreEqual(newPosition.Y * 2, newPositionScaled.Y, 1);
}
finally
{
nativeWrapper.RasterizationScale = originalRasterizationScale;
appWindow.Move(originalPosition);
}
}
#endif
#endif

#if __SKIA__ || WINDOWS
[TestMethod]
public async Task When_Resize()
Expand Down Expand Up @@ -147,32 +235,6 @@ void OnChanged(AppWindow s, AppWindowChangedEventArgs e)
await TestServices.RunOnUIThread(() => newWindow.Close());
}
}

private void AssertPositioningAndSizingSupport()
{
if (!OperatingSystem.IsLinux() &&
!OperatingSystem.IsWindows() &&
!OperatingSystem.IsMacOS() ||
TestServices.WindowHelper.IsXamlIsland ||
IsGtk())
{
Assert.Inconclusive("This test only supported on Windows and Linux apps currently.");
}
}

private bool IsGtk()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in assemblies)
{
if (a.GetName().Name == "GtkSharp")
{
return true;
}
}

return false;
}
#endif
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BaseWindowImplementation(Window window)

protected Window Window { get; }

protected INativeWindowWrapper? NativeWindowWrapper { get; private set; }
public INativeWindowWrapper? NativeWindowWrapper { get; private set; }

public abstract CoreWindow? CoreWindow { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal interface IWindowImplementation

object? NativeWindow { get; }

INativeWindowWrapper? NativeWindowWrapper { get; }

event WindowActivatedEventHandler? Activated;

event TypedEventHandler<object, WindowEventArgs>? Closed;
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Window/Native/INativeWindowWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal interface INativeWindowWrapper : INativeAppWindow

CoreWindowActivationState ActivationState { get; }

float RasterizationScale { get; }
float RasterizationScale { get; set; }

event EventHandler<Size>? SizeChanged;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ private void RaiseContentIslandStateChanged(ContentIslandStateChangedEventArgs a
{
XamlRoot?.VisualTree.ContentRoot.CompositionContent?.RaiseStateChanged(args);
}

public virtual void Move(PointInt32 position)
{
}
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Window/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ public static Window? Current

internal object? NativeWindow => _windowImplementation.NativeWindow;

internal INativeWindowWrapper NativeWindowWrapper => _windowImplementation.NativeWindowWrapper;

/// <summary>
/// This is run when Application.Current is set and the UI framework is ready to construct
/// visual elements (this is important for eample for Andorid where trying to construct
Expand Down

0 comments on commit 2bd3a12

Please sign in to comment.