diff --git a/Avalonia.sln b/Avalonia.sln
index 7a2f785bb9..7ad4a699ec 100644
--- a/Avalonia.sln
+++ b/Avalonia.sln
@@ -201,18 +201,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Controls.DataGrid"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Dialogs", "src\Avalonia.Dialogs\Avalonia.Dialogs.csproj", "{4D55985A-1EE2-4F25-AD39-6EA8BC04F8FB}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avalonia.FreeDesktop", "src\Avalonia.FreeDesktop\Avalonia.FreeDesktop.csproj", "{4D36CEC8-53F2-40A5-9A37-79AAE356E2DA}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.FreeDesktop", "src\Avalonia.FreeDesktop\Avalonia.FreeDesktop.csproj", "{4D36CEC8-53F2-40A5-9A37-79AAE356E2DA}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avalonia.Controls.DataGrid.UnitTests", "tests\Avalonia.Controls.DataGrid.UnitTests\Avalonia.Controls.DataGrid.UnitTests.csproj", "{351337F5-D66F-461B-A957-4EF60BDB4BA6}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Controls.DataGrid.UnitTests", "tests\Avalonia.Controls.DataGrid.UnitTests\Avalonia.Controls.DataGrid.UnitTests.csproj", "{351337F5-D66F-461B-A957-4EF60BDB4BA6}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Shared\RenderHelpers\RenderHelpers.projitems*{3c4c0cb4-0c0f-4450-a37b-148c84ff905f}*SharedItemsImports = 13
- src\Shared\RenderHelpers\RenderHelpers.projitems*{3e908f67-5543-4879-a1dc-08eace79b3cd}*SharedItemsImports = 4
+ src\Shared\RenderHelpers\RenderHelpers.projitems*{3e908f67-5543-4879-a1dc-08eace79b3cd}*SharedItemsImports = 5
src\Shared\PlatformSupport\PlatformSupport.projitems*{4488ad85-1495-4809-9aa4-ddfe0a48527e}*SharedItemsImports = 4
src\Shared\PlatformSupport\PlatformSupport.projitems*{7b92af71-6287-4693-9dcb-bd5b6e927e23}*SharedItemsImports = 4
- src\Shared\RenderHelpers\RenderHelpers.projitems*{7d2d3083-71dd-4cc9-8907-39a0d86fb322}*SharedItemsImports = 4
- tests\Avalonia.RenderTests\Avalonia.RenderTests.projitems*{dabfd304-d6a4-4752-8123-c2ccf7ac7831}*SharedItemsImports = 4
+ src\Shared\RenderHelpers\RenderHelpers.projitems*{7d2d3083-71dd-4cc9-8907-39a0d86fb322}*SharedItemsImports = 5
+ src\Shared\PlatformSupport\PlatformSupport.projitems*{88060192-33d5-4932-b0f9-8bd2763e857d}*SharedItemsImports = 5
src\Shared\PlatformSupport\PlatformSupport.projitems*{e4d9629c-f168-4224-3f51-a5e482ffbc42}*SharedItemsImports = 13
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/src/Avalonia.Base/EnumExtensions.cs b/src/Avalonia.Base/EnumExtensions.cs
new file mode 100644
index 0000000000..d847ab2faa
--- /dev/null
+++ b/src/Avalonia.Base/EnumExtensions.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace Avalonia
+{
+ ///
+ /// Provides extension methods for enums.
+ ///
+ public static class EnumExtensions
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static unsafe bool HasFlagCustom(this T value, T flag) where T : unmanaged, Enum
+ {
+ var intValue = *(int*)&value;
+ var intFlag = *(int*)&flag;
+
+ return (intValue & intFlag) == intFlag;
+ }
+ }
+}
diff --git a/src/Avalonia.Controls/Primitives/PopupRoot.cs b/src/Avalonia.Controls/Primitives/PopupRoot.cs
index 74a6d288f4..b3769091c9 100644
--- a/src/Avalonia.Controls/Primitives/PopupRoot.cs
+++ b/src/Avalonia.Controls/Primitives/PopupRoot.cs
@@ -118,20 +118,14 @@ namespace Avalonia.Controls.Primitives
});
}
- ///
- /// Carries out the arrange pass of the window.
- ///
- /// The final window size.
- /// The parameter unchanged.
- protected override Size ArrangeOverride(Size finalSize)
+ protected override sealed Size ArrangeSetBounds(Size size)
{
using (BeginAutoSizing())
{
- _positionerParameters.Size = finalSize;
+ _positionerParameters.Size = size;
UpdatePosition();
+ return ClientSize;
}
-
- return base.ArrangeOverride(PlatformImpl?.ClientSize ?? default(Size));
}
}
}
diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs
index 0c37858223..ca64ec5e50 100644
--- a/src/Avalonia.Controls/Window.cs
+++ b/src/Avalonia.Controls/Window.cs
@@ -268,22 +268,7 @@ namespace Avalonia.Controls
/// Should be called from left mouse button press event handler
///
public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e) => PlatformImpl?.BeginResizeDrag(edge, e);
-
- ///
- /// Carries out the arrange pass of the window.
- ///
- /// The final window size.
- /// The parameter unchanged.
- protected override Size ArrangeOverride(Size finalSize)
- {
- using (BeginAutoSizing())
- {
- PlatformImpl?.Resize(finalSize);
- }
- return base.ArrangeOverride(PlatformImpl?.ClientSize ?? default(Size));
- }
-
///
Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize;
@@ -405,6 +390,19 @@ namespace Avalonia.Controls
EnsureInitialized();
IsVisible = true;
+
+ var initialSize = new Size(
+ double.IsNaN(Width) ? ClientSize.Width : Width,
+ double.IsNaN(Height) ? ClientSize.Height : Height);
+
+ if (initialSize != ClientSize)
+ {
+ using (BeginAutoSizing())
+ {
+ PlatformImpl?.Resize(initialSize);
+ }
+ }
+
LayoutManager.ExecuteInitialLayoutPass(this);
using (BeginAutoSizing())
@@ -524,31 +522,30 @@ namespace Avalonia.Controls
}
}
- ///
protected override Size MeasureOverride(Size availableSize)
{
var sizeToContent = SizeToContent;
var clientSize = ClientSize;
- Size constraint = clientSize;
+ var constraint = clientSize;
- if ((sizeToContent & SizeToContent.Width) != 0)
+ if (sizeToContent.HasFlagCustom(SizeToContent.Width))
{
constraint = constraint.WithWidth(double.PositiveInfinity);
}
- if ((sizeToContent & SizeToContent.Height) != 0)
+ if (sizeToContent.HasFlagCustom(SizeToContent.Height))
{
constraint = constraint.WithHeight(double.PositiveInfinity);
}
var result = base.MeasureOverride(constraint);
- if ((sizeToContent & SizeToContent.Width) == 0)
+ if (!sizeToContent.HasFlagCustom(SizeToContent.Width))
{
result = result.WithWidth(clientSize.Width);
}
- if ((sizeToContent & SizeToContent.Height) == 0)
+ if (!sizeToContent.HasFlagCustom(SizeToContent.Height))
{
result = result.WithHeight(clientSize.Height);
}
@@ -556,6 +553,15 @@ namespace Avalonia.Controls
return result;
}
+ protected sealed override Size ArrangeSetBounds(Size size)
+ {
+ using (BeginAutoSizing())
+ {
+ PlatformImpl?.Resize(size);
+ return ClientSize;
+ }
+ }
+
protected sealed override void HandleClosed()
{
RaiseEvent(new RoutedEventArgs(WindowClosedEvent));
diff --git a/src/Avalonia.Controls/WindowBase.cs b/src/Avalonia.Controls/WindowBase.cs
index 196110edf7..77ae382ae5 100644
--- a/src/Avalonia.Controls/WindowBase.cs
+++ b/src/Avalonia.Controls/WindowBase.cs
@@ -224,16 +224,65 @@ namespace Avalonia.Controls
/// The new client size.
protected override void HandleResized(Size clientSize)
{
- if (!AutoSizing)
- {
- Width = clientSize.Width;
- Height = clientSize.Height;
- }
+ Width = clientSize.Width;
+ Height = clientSize.Height;
ClientSize = clientSize;
LayoutManager.ExecuteLayoutPass();
Renderer?.Resized(clientSize);
}
+ ///
+ /// Overrides the core measure logic for windows.
+ ///
+ /// The available size.
+ /// The measured size.
+ ///
+ /// The layout logic for top-level windows is different than for other controls because
+ /// they don't have a parent, meaning that many layout properties handled by the default
+ /// MeasureCore (such as margins and alignment) make no sense.
+ ///
+ protected override Size MeasureCore(Size availableSize)
+ {
+ ApplyTemplate();
+
+ var constraint = availableSize;
+
+ if (!double.IsNaN(Width))
+ {
+ constraint = constraint.WithWidth(Width);
+ }
+
+ if (!double.IsNaN(Height))
+ {
+ constraint = constraint.WithHeight(Height);
+ }
+
+ return MeasureOverride(constraint);
+ }
+
+ ///
+ /// Overrides the core arrange logic for windows.
+ ///
+ /// The final arrange rect.
+ ///
+ /// The layout logic for top-level windows is different than for other controls because
+ /// they don't have a parent, meaning that many layout properties handled by the default
+ /// ArrangeCore (such as margins and alignment) make no sense.
+ ///
+ protected override void ArrangeCore(Rect finalRect)
+ {
+ var constraint = ArrangeSetBounds(finalRect.Size);
+ var arrangeSize = ArrangeOverride(constraint);
+ Bounds = new Rect(arrangeSize);
+ }
+
+ ///
+ /// Called durung the arrange pass to set the size of the window.
+ ///
+ /// The requested size of the window.
+ /// The actual size of the window.
+ protected virtual Size ArrangeSetBounds(Size size) => size;
+
///
/// Handles a window position change notification from
/// .
diff --git a/src/Avalonia.X11/X11ImmediateRendererProxy.cs b/src/Avalonia.X11/X11ImmediateRendererProxy.cs
index f7bc7f9711..59a9021743 100644
--- a/src/Avalonia.X11/X11ImmediateRendererProxy.cs
+++ b/src/Avalonia.X11/X11ImmediateRendererProxy.cs
@@ -55,11 +55,6 @@ namespace Avalonia.X11
return _renderer.HitTest(p, root, filter);
}
- public IVisual HitTestFirst(Point p, IVisual root, Func filter)
- {
- return _renderer.HitTestFirst(p, root, filter);
- }
-
public void RecalculateChildren(IVisual visual)
{
_renderer.RecalculateChildren(visual);
diff --git a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
index f44f89e91f..041f97f42a 100644
--- a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
@@ -200,16 +200,17 @@ namespace Avalonia.Controls.UnitTests
screenImpl.Setup(x => x.ScreenCount).Returns(1);
screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) });
- popupImpl = MockWindowingPlatform.CreatePopupMock();
+ var windowImpl = MockWindowingPlatform.CreateWindowMock();
+ popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object);
popupImpl.SetupGet(x => x.Scaling).Returns(1);
+ windowImpl.Setup(x => x.CreatePopup()).Returns(popupImpl.Object);
- var windowImpl = MockWindowingPlatform.CreateWindowMock(() => popupImpl.Object);
windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
var services = TestServices.StyledWindow.With(
inputManager: new InputManager(),
windowImpl: windowImpl.Object,
- windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, () => popupImpl.Object));
+ windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object));
return UnitTestApplication.Start(services);
}
diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
index f75f6fcf91..9221415a28 100644
--- a/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
@@ -7,6 +7,7 @@ using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.LogicalTree;
+using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
@@ -175,9 +176,75 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
}
- private PopupRoot CreateTarget(TopLevel popupParent)
+ [Fact]
+ public void Child_Should_Be_Measured_With_Infinity()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new ChildControl();
+ var window = new Window();
+ var target = CreateTarget(window);
+
+ target.Content = child;
+ target.Show();
+
+ Assert.Equal(Size.Infinity, child.MeasureSize);
+ }
+ }
+
+ [Fact]
+ public void Child_Should_Be_Measured_With_Width_Height_When_Set()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new ChildControl();
+ var window = new Window();
+ var target = CreateTarget(window);
+
+ target.Width = 500;
+ target.Height = 600;
+ target.Content = child;
+ target.Show();
+
+ Assert.Equal(new Size(500, 600), child.MeasureSize);
+ }
+ }
+
+ [Fact]
+ public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window_Size()
+ {
+ // Issue #3784.
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var window = new Window();
+ var popupImpl = MockWindowingPlatform.CreatePopupMock(window.PlatformImpl);
+
+ popupImpl.Setup(x => x.ClientSize).Returns(new Size(400, 480));
+
+ var child = new Canvas
+ {
+ Width = 400,
+ Height = 800,
+ };
+
+ var target = CreateTarget(window, popupImpl.Object);
+ target.Content = child;
+
+ target.Show();
+
+ Assert.Equal(new Size(400, 480), target.Bounds.Size);
+
+ // Issue #3784 causes this to be (0, 160) which makes no sense as Window has no
+ // parent control to be offset against.
+ Assert.Equal(new Point(0, 0), target.Bounds.Position);
+ }
+ }
+
+ private PopupRoot CreateTarget(TopLevel popupParent, IPopupImpl impl = null)
{
- var result = new PopupRoot(popupParent, popupParent.PlatformImpl.CreatePopup())
+ impl ??= popupParent.PlatformImpl.CreatePopup();
+
+ var result = new PopupRoot(popupParent, impl)
{
Template = new FuncControlTemplate((parent, scope) =>
new ContentPresenter
@@ -220,5 +287,16 @@ namespace Avalonia.Controls.UnitTests.Primitives
Popup = (Popup)this.GetVisualChildren().Single();
}
}
+
+ private class ChildControl : Control
+ {
+ public Size MeasureSize { get; private set; }
+
+ protected override Size MeasureOverride(Size availableSize)
+ {
+ MeasureSize = availableSize;
+ return base.MeasureOverride(availableSize);
+ }
+ }
}
}
diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
index 7cb9fccee8..5ec5c2b2f9 100644
--- a/tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
@@ -332,11 +332,11 @@ namespace Avalonia.Controls.UnitTests.Primitives
{
return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform:
new MockWindowingPlatform(null,
- () =>
+ x =>
{
if(UsePopupHost)
return null;
- return MockWindowingPlatform.CreatePopupMock().Object;
+ return MockWindowingPlatform.CreatePopupMock(x).Object;
})));
}
diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs
index 0508edd92f..3dff81ba13 100644
--- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs
@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
+using Avalonia.Layout;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.UnitTests;
@@ -341,6 +342,184 @@ namespace Avalonia.Controls.UnitTests
}
}
+ [Fact]
+ public void Child_Should_Be_Measured_With_Width_And_Height_If_SizeToContent_Is_Manual()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new ChildControl();
+ var target = new Window
+ {
+ Width = 100,
+ Height = 50,
+ SizeToContent = SizeToContent.Manual,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(new Size(100, 50), child.MeasureSize);
+ }
+ }
+
+ [Fact]
+ public void Child_Should_Be_Measured_With_ClientSize_If_SizeToContent_Is_Manual_And_No_Width_Height_Specified()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var windowImpl = MockWindowingPlatform.CreateWindowMock();
+ windowImpl.Setup(x => x.ClientSize).Returns(new Size(550, 450));
+
+ var child = new ChildControl();
+ var target = new Window(windowImpl.Object)
+ {
+ SizeToContent = SizeToContent.Manual,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(new Size(550, 450), child.MeasureSize);
+ }
+ }
+
+ [Fact]
+ public void Child_Should_Be_Measured_With_Infinity_If_SizeToContent_Is_WidthAndHeight()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new ChildControl();
+ var target = new Window
+ {
+ Width = 100,
+ Height = 50,
+ SizeToContent = SizeToContent.WidthAndHeight,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(Size.Infinity, child.MeasureSize);
+ }
+ }
+
+ [Fact]
+ public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window_Size()
+ {
+ // Issue #3784.
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var windowImpl = MockWindowingPlatform.CreateWindowMock();
+ var clientSize = new Size(200, 200);
+ var maxClientSize = new Size(480, 480);
+
+ windowImpl.Setup(x => x.Resize(It.IsAny())).Callback(size =>
+ {
+ clientSize = size.Constrain(maxClientSize);
+ windowImpl.Object.Resized?.Invoke(clientSize);
+ });
+
+ windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
+
+ var child = new Canvas
+ {
+ Width = 400,
+ Height = 800,
+ };
+ var target = new Window(windowImpl.Object)
+ {
+ SizeToContent = SizeToContent.WidthAndHeight,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(new Size(400, 480), target.Bounds.Size);
+
+ // Issue #3784 causes this to be (0, 160) which makes no sense as Window has no
+ // parent control to be offset against.
+ Assert.Equal(new Point(0, 0), target.Bounds.Position);
+ }
+ }
+
+ [Fact]
+ public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAndHeight()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new Canvas
+ {
+ Width = 400,
+ Height = 800,
+ };
+
+ var target = new Window()
+ {
+ SizeToContent = SizeToContent.WidthAndHeight,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(400, target.Width);
+ Assert.Equal(800, target.Height);
+ }
+ }
+
+ [Fact]
+ public void SizeToContent_Should_Not_Be_Lost_On_Show()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new Canvas
+ {
+ Width = 400,
+ Height = 800,
+ };
+
+ var target = new Window()
+ {
+ SizeToContent = SizeToContent.WidthAndHeight,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
+ }
+ }
+
+ [Fact]
+ public void Width_Height_Should_Be_Updated_When_SizeToContent_Is_WidthAndHeight()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var child = new Canvas
+ {
+ Width = 400,
+ Height = 800,
+ };
+
+ var target = new Window()
+ {
+ SizeToContent = SizeToContent.WidthAndHeight,
+ Content = child
+ };
+
+ target.Show();
+
+ Assert.Equal(400, target.Width);
+ Assert.Equal(800, target.Height);
+
+ child.Width = 410;
+ target.LayoutManager.ExecuteLayoutPass();
+
+ Assert.Equal(410, target.Width);
+ Assert.Equal(800, target.Height);
+ Assert.Equal(SizeToContent.WidthAndHeight, target.SizeToContent);
+ }
+ }
+
private IWindowImpl CreateImpl(Mock renderer)
{
return Mock.Of(x =>
diff --git a/tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs b/tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs
index 6cf38b6121..dcc29a9716 100644
--- a/tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs
+++ b/tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs
@@ -1,28 +1,12 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using System.Diagnostics;
-using System.IO;
using System.Linq;
-using Moq;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
-using Avalonia.Diagnostics;
-using Avalonia.Input;
-using Avalonia.Platform;
-using Avalonia.Rendering;
-using Avalonia.Shared.PlatformSupport;
using Avalonia.Styling;
-using Avalonia.Themes.Default;
+using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Xunit;
-using Avalonia.Media;
-using System;
-using System.Collections.Generic;
-using Avalonia.Controls.UnitTests;
-using Avalonia.UnitTests;
namespace Avalonia.Layout.UnitTests
{
@@ -31,10 +15,8 @@ namespace Avalonia.Layout.UnitTests
[Fact]
public void Grandchild_Size_Changed()
{
- using (var context = AvaloniaLocator.EnterScope())
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
{
- RegisterServices();
-
Border border;
TextBlock textBlock;
@@ -58,7 +40,6 @@ namespace Avalonia.Layout.UnitTests
};
window.Show();
- window.LayoutManager.ExecuteInitialLayoutPass(window);
Assert.Equal(new Size(400, 400), border.Bounds.Size);
textBlock.Width = 200;
@@ -71,10 +52,8 @@ namespace Avalonia.Layout.UnitTests
[Fact]
public void Test_ScrollViewer_With_TextBlock()
{
- using (var context = AvaloniaLocator.EnterScope())
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
{
- RegisterServices();
-
ScrollViewer scrollViewer;
TextBlock textBlock;
@@ -82,7 +61,6 @@ namespace Avalonia.Layout.UnitTests
{
Width = 800,
Height = 600,
- SizeToContent = SizeToContent.WidthAndHeight,
Content = scrollViewer = new ScrollViewer
{
Width = 200,
@@ -102,7 +80,6 @@ namespace Avalonia.Layout.UnitTests
window.Resources["ScrollBarThickness"] = 10.0;
window.Show();
- window.LayoutManager.ExecuteInitialLayoutPass(window);
Assert.Equal(new Size(800, 600), window.Bounds.Size);
Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size);
@@ -134,84 +111,5 @@ namespace Avalonia.Layout.UnitTests
{
return v.Bounds.Position;
}
-
- class FormattedTextMock : IFormattedTextImpl
- {
- public FormattedTextMock(string text)
- {
- Text = text;
- }
-
- public Size Constraint { get; set; }
-
- public string Text { get; }
-
- public Rect Bounds => Rect.Empty;
-
- public void Dispose()
- {
- }
-
- public IEnumerable GetLines() => new FormattedTextLine[0];
-
- public TextHitTestResult HitTestPoint(Point point) => new TextHitTestResult();
-
- public Rect HitTestTextPosition(int index) => new Rect();
-
- public IEnumerable HitTestTextRange(int index, int length) => new Rect[0];
-
- public Size Measure() => Constraint;
- }
-
- private void RegisterServices()
- {
- var globalStyles = new Mock();
- var globalStylesResources = globalStyles.As();
- var outObj = (object)10;
- globalStylesResources.Setup(x => x.TryGetResource("FontSizeNormal", out outObj)).Returns(true);
-
- var renderInterface = new Mock();
- renderInterface.Setup(x =>
- x.CreateFormattedText(
- It.IsAny(),
- It.IsAny(),
- It.IsAny(),
- It.IsAny(),
- It.IsAny(),
- It.IsAny>()))
- .Returns(new FormattedTextMock("TEST"));
-
- var streamGeometry = new Mock();
- streamGeometry.Setup(x =>
- x.Open())
- .Returns(new Mock().Object);
-
- renderInterface.Setup(x =>
- x.CreateStreamGeometry())
- .Returns(streamGeometry.Object);
-
- var windowImpl = new Mock();
-
- Size clientSize = default(Size);
-
- windowImpl.SetupGet(x => x.ClientSize).Returns(() => clientSize);
- windowImpl.Setup(x => x.Resize(It.IsAny())).Callback(s => clientSize = s);
- windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1024, 1024));
- windowImpl.SetupGet(x => x.Scaling).Returns(1);
-
- AvaloniaLocator.CurrentMutable
- .Bind().ToConstant(new CursorFactoryMock())
- .Bind().ToConstant(new AssetLoader())
- .Bind().ToConstant(new Mock().Object)
- .Bind().ToConstant(globalStyles.Object)
- .Bind().ToConstant(new AppBuilder().RuntimePlatform)
- .Bind().ToConstant(renderInterface.Object)
- .Bind().ToConstant(new Styler())
- .Bind().ToConstant(new Avalonia.Controls.UnitTests.WindowingPlatformMock(() => windowImpl.Object));
-
- var theme = new DefaultTheme();
- globalStyles.Setup(x => x.IsStylesInitialized).Returns(true);
- globalStyles.Setup(x => x.Styles).Returns(theme);
- }
}
}
diff --git a/tests/Avalonia.UnitTests/MockWindowingPlatform.cs b/tests/Avalonia.UnitTests/MockWindowingPlatform.cs
index a6701ef655..b8b7512c9e 100644
--- a/tests/Avalonia.UnitTests/MockWindowingPlatform.cs
+++ b/tests/Avalonia.UnitTests/MockWindowingPlatform.cs
@@ -8,61 +8,118 @@ namespace Avalonia.UnitTests
{
public class MockWindowingPlatform : IWindowingPlatform
{
+ private static readonly Size s_screenSize = new Size(1280, 1024);
private readonly Func _windowImpl;
- private readonly Func _popupImpl;
+ private readonly Func _popupImpl;
- public MockWindowingPlatform(Func windowImpl = null, Func popupImpl = null )
+ public MockWindowingPlatform(
+ Func windowImpl = null,
+ Func popupImpl = null )
{
_windowImpl = windowImpl;
_popupImpl = popupImpl;
}
- public static Mock CreateWindowMock(Func popupImpl = null)
+ public static Mock CreateWindowMock()
{
- var win = Mock.Of(x => x.Scaling == 1);
- var mock = Mock.Get(win);
- mock.Setup(x => x.CreatePopup()).Returns(() =>
+ var windowImpl = new Mock();
+ var position = new PixelPoint();
+ var clientSize = new Size(800, 600);
+
+ windowImpl.SetupAllProperties();
+ windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
+ windowImpl.Setup(x => x.Scaling).Returns(1);
+ windowImpl.Setup(x => x.Screen).Returns(CreateScreenMock().Object);
+ windowImpl.Setup(x => x.Position).Returns(() => position);
+ SetupToplevel(windowImpl);
+
+ windowImpl.Setup(x => x.CreatePopup()).Returns(() =>
+ {
+ return CreatePopupMock(windowImpl.Object).Object;
+ });
+
+ windowImpl.Setup(x => x.Dispose()).Callback(() =>
{
- if (popupImpl != null)
- return popupImpl();
- return CreatePopupMock().Object;
+ windowImpl.Object.Closed?.Invoke();
+ });
+ windowImpl.Setup(x => x.Move(It.IsAny())).Callback(x =>
+ {
+ position = x;
+ windowImpl.Object.PositionChanged?.Invoke(x);
});
- mock.Setup(x => x.Dispose()).Callback(() =>
+
+ windowImpl.Setup(x => x.Resize(It.IsAny())).Callback(x =>
{
- mock.Object.Closed?.Invoke();
+ clientSize = x.Constrain(s_screenSize);
+ windowImpl.Object.Resized?.Invoke(clientSize);
});
- PixelPoint pos = default;
- mock.SetupGet(x => x.Position).Returns(() => pos);
- mock.Setup(x => x.Move(It.IsAny())).Callback(new Action(np => pos = np));
- SetupToplevel(mock);
- return mock;
+
+ windowImpl.Setup(x => x.Show()).Callback(() =>
+ {
+ windowImpl.Object.Activated?.Invoke();
+ });
+
+ return windowImpl;
}
- static void SetupToplevel(Mock mock) where T : class, ITopLevelImpl
+ public static Mock CreatePopupMock(IWindowBaseImpl parent)
{
- mock.SetupGet(x => x.MouseDevice).Returns(new MouseDevice());
+ var popupImpl = new Mock();
+
+ var positionerHelper = new ManagedPopupPositionerPopupImplHelper(parent, (pos, size, scale) =>
+ {
+ popupImpl.Object.PositionChanged?.Invoke(pos);
+ popupImpl.Object.Resized?.Invoke(size);
+ });
+
+ var positioner = new ManagedPopupPositioner(positionerHelper);
+
+ popupImpl.Setup(x => x.Scaling).Returns(1);
+ popupImpl.Setup(x => x.PopupPositioner).Returns(positioner);
+
+ SetupToplevel(popupImpl);
+
+ return popupImpl;
}
- public static Mock CreatePopupMock()
+ public static Mock CreateScreenMock()
{
- var positioner = Mock.Of();
- var popup = Mock.Of(x => x.Scaling == 1);
- var mock = Mock.Get(popup);
- mock.SetupGet(x => x.PopupPositioner).Returns(positioner);
- SetupToplevel(mock);
-
- return mock;
+ var screenImpl = new Mock();
+ var bounds = new PixelRect(0, 0, (int)s_screenSize.Width, (int)s_screenSize.Height);
+ var screen = new Screen(96, bounds, bounds, true);
+ screenImpl.Setup(x => x.AllScreens).Returns(new[] { screen });
+ screenImpl.Setup(x => x.ScreenCount).Returns(1);
+ return screenImpl;
}
public IWindowImpl CreateWindow()
{
- return _windowImpl?.Invoke() ?? CreateWindowMock(_popupImpl).Object;
+ if (_windowImpl is object)
+ {
+ return _windowImpl();
+ }
+ else
+ {
+ var mock = CreateWindowMock();
+
+ if (_popupImpl is object)
+ {
+ mock.Setup(x => x.CreatePopup()).Returns(() => _popupImpl(mock.Object));
+ }
+
+ return mock.Object;
+ }
}
public IEmbeddableWindowImpl CreateEmbeddableWindow()
{
throw new NotImplementedException();
}
+
+ private static void SetupToplevel(Mock mock) where T : class, ITopLevelImpl
+ {
+ mock.SetupGet(x => x.MouseDevice).Returns(new MouseDevice());
+ }
}
}