Browse Source

Clarify/fix unit test.

Now all of the other popup tests pass as well.
pull/11502/head
Steven Kirk 3 years ago
parent
commit
40515fc798
  1. 82
      tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
  2. 11
      tests/Avalonia.UnitTests/MockWindowingPlatform.cs

82
tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs

@ -1080,6 +1080,10 @@ namespace Avalonia.Controls.UnitTests.Primitives
[Fact] [Fact]
public void GetPosition_On_Control_In_Popup_Called_From_Parent_Should_Return_Valid_Coordinates() public void GetPosition_On_Control_In_Popup_Called_From_Parent_Should_Return_Valid_Coordinates()
{ {
// This test only applies when using a PopupRoot host and not an overlay popup.
if (UsePopupHost)
return;
using (CreateServices()) using (CreateServices())
{ {
var popupContent = new Border() { Height = 100, Width = 100, Background = Brushes.Red }; var popupContent = new Border() { Height = 100, Width = 100, Background = Brushes.Red };
@ -1088,18 +1092,18 @@ namespace Avalonia.Controls.UnitTests.Primitives
var popupParent = new Border { Child = popup }; var popupParent = new Border { Child = popup };
var root = PreparedWindow(popupParent); var root = PreparedWindow(popupParent);
var raised = 0;
root.LayoutManager.ExecuteInitialLayoutPass();
popup.Open(); popup.Open();
root.LayoutManager.ExecuteLayoutPass();
var pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
var pts = popupContent.PointToScreen(new Point(10, 10)); // Verify that the popup is positioned at 40,40 as descibed by the Horizontal/
// VerticalOffset: 10,10 becomes 50,50 in screen coordinates.
Assert.Equal(new PixelPoint(50, 50), popupContent.PointToScreen(new Point(10, 10)));
Assert.Equal(new PixelPoint(50, 50), pts); // The popup parent is positioned at 0,0 in screen coordinates so client and
// screen coordinates are the same.
Assert.Equal(new PixelPoint(10, 10), popupParent.PointToScreen(new Point(10, 10)));
// The event will be raised on the popup content at 50,50 (90,90 in screen coordinates)
var pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
var ev = new PointerPressedEventArgs( var ev = new PointerPressedEventArgs(
popupContent, popupContent,
pointer, pointer,
@ -1109,19 +1113,28 @@ namespace Avalonia.Controls.UnitTests.Primitives
new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonPressed), new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonPressed),
KeyModifiers.None); KeyModifiers.None);
Point pointRelativeToWindowContent = default; var contentRaised = 0;
var parentRaised = 0;
popupParent.AddHandler(Button.PointerPressedEvent, (s, e) => // The event is raised on the popup content in popup coordinates.
popupContent.AddHandler(Button.PointerPressedEvent, (s, e) =>
{ {
++raised; ++contentRaised;
Assert.Equal(new Point(50, 50), e.GetPosition(popupContent));
});
pointRelativeToWindowContent = e.GetPosition(popupParent); // The event is raised on the parent in root coordinates (which in this case are
// the same as screen coordinates).
popupParent.AddHandler(Button.PointerPressedEvent, (s, e) =>
{
++parentRaised;
Assert.Equal(new Point(90, 90), e.GetPosition(popupParent));
}); });
popupContent.RaiseEvent(ev); popupContent.RaiseEvent(ev);
Assert.Equal(1, raised); Assert.Equal(1, contentRaised);
Assert.Equal(new Point(90, 90), pointRelativeToWindowContent); Assert.Equal(1, parentRaised);
} }
} }
@ -1146,28 +1159,16 @@ namespace Avalonia.Controls.UnitTests.Primitives
private IDisposable CreateServices() private IDisposable CreateServices()
{ {
return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform: return UnitTestApplication.Start(TestServices.StyledWindow.With(
new MockWindowingPlatform(() => MockWindowingPlatform.CreateWindowMock().Object, windowingPlatform: CreateMockWindowingPlatform()));
x =>
{
if(UsePopupHost)
return null;
return MockWindowingPlatform.CreatePopupMock(x).Object;
})));
} }
private IDisposable CreateServicesWithFocus() private IDisposable CreateServicesWithFocus()
{ {
return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform: return UnitTestApplication.Start(TestServices.StyledWindow.With(
new MockWindowingPlatform(null, windowingPlatform: CreateMockWindowingPlatform(),
x => focusManager: new FocusManager(),
{ keyboardDevice: () => new KeyboardDevice()));
if (UsePopupHost)
return null;
return MockWindowingPlatform.CreatePopupMock(x).Object;
}),
focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice()));
} }
@ -1184,6 +1185,23 @@ namespace Avalonia.Controls.UnitTests.Primitives
KeyModifiers.None); KeyModifiers.None);
} }
private MockWindowingPlatform CreateMockWindowingPlatform()
{
return new MockWindowingPlatform(() =>
{
var mock = MockWindowingPlatform.CreateWindowMock();
mock.Setup(x => x.CreatePopup()).Returns(() =>
{
if (UsePopupHost)
return null;
return MockWindowingPlatform.CreatePopupMock(mock.Object).Object;
});
return mock.Object;
}, null);
}
private static Window PreparedWindow(object content = null) private static Window PreparedWindow(object content = null)
{ {
var w = new Window { Content = content }; var w = new Window { Content = content };

11
tests/Avalonia.UnitTests/MockWindowingPlatform.cs

@ -24,7 +24,6 @@ namespace Avalonia.UnitTests
public static Mock<IWindowImpl> CreateWindowMock(double initialWidth = 800, double initialHeight = 600) public static Mock<IWindowImpl> CreateWindowMock(double initialWidth = 800, double initialHeight = 600)
{ {
var windowImpl = new Mock<IWindowImpl>(); var windowImpl = new Mock<IWindowImpl>();
var position = new PixelPoint();
var clientSize = new Size(initialWidth, initialHeight); var clientSize = new Size(initialWidth, initialHeight);
windowImpl.SetupAllProperties(); windowImpl.SetupAllProperties();
@ -35,7 +34,6 @@ namespace Avalonia.UnitTests
windowImpl.Setup(x => x.DesktopScaling).Returns(1); windowImpl.Setup(x => x.DesktopScaling).Returns(1);
windowImpl.Setup(x => x.RenderScaling).Returns(1); windowImpl.Setup(x => x.RenderScaling).Returns(1);
windowImpl.Setup(x => x.Screen).Returns(CreateScreenMock().Object); windowImpl.Setup(x => x.Screen).Returns(CreateScreenMock().Object);
windowImpl.Setup(x => x.Position).Returns(() => position);
windowImpl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null); windowImpl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null);
@ -51,15 +49,15 @@ namespace Avalonia.UnitTests
windowImpl.Setup(x => x.Move(It.IsAny<PixelPoint>())).Callback<PixelPoint>(x => windowImpl.Setup(x => x.Move(It.IsAny<PixelPoint>())).Callback<PixelPoint>(x =>
{ {
position = x; windowImpl.Setup(x => x.Position).Returns(x);
windowImpl.Object.PositionChanged?.Invoke(x); windowImpl.Object.PositionChanged?.Invoke(x);
}); });
windowImpl.Setup(x => x.PointToScreen(It.IsAny<Point>())) windowImpl.Setup(x => x.PointToScreen(It.IsAny<Point>()))
.Returns<Point>((point) => PixelPoint.FromPoint(point, 1) + position); .Returns<Point>((point) => PixelPoint.FromPoint(point, 1) + windowImpl.Object.Position);
windowImpl.Setup(x => x.PointToClient(It.IsAny<PixelPoint>())) windowImpl.Setup(x => x.PointToClient(It.IsAny<PixelPoint>()))
.Returns<PixelPoint>(point => (point - position).ToPoint(1)); .Returns<PixelPoint>(point => (point - windowImpl.Object.Position).ToPoint(1));
windowImpl.Setup(x => x.Resize(It.IsAny<Size>(), It.IsAny<WindowResizeReason>())) windowImpl.Setup(x => x.Resize(It.IsAny<Size>(), It.IsAny<WindowResizeReason>()))
.Callback<Size, WindowResizeReason>((x, y) => .Callback<Size, WindowResizeReason>((x, y) =>
@ -79,9 +77,6 @@ namespace Avalonia.UnitTests
windowImpl.Object.Activated?.Invoke(); windowImpl.Object.Activated?.Invoke();
}); });
windowImpl.Setup(x => x.PointToScreen(It.IsAny<Point>()))
.Returns((Point p) => PixelPoint.FromPoint(p, 1D) + position);
return windowImpl; return windowImpl;
} }

Loading…
Cancel
Save