Browse Source

Merge branch 'master' into pool-deferred-renderer-state

pull/3775/head
Steven Kirk 6 years ago
committed by GitHub
parent
commit
2a6c36a6d5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      azure-pipelines.yml
  2. 7
      src/Avalonia.Controls/AutoCompleteBox.cs
  3. 7
      src/Avalonia.Controls/Calendar/DatePicker.cs
  4. 7
      src/Avalonia.Controls/ComboBox.cs
  5. 57
      src/Avalonia.Controls/Primitives/Popup.cs
  6. 33
      src/Avalonia.Controls/Primitives/PopupClosedEventArgs.cs
  7. 12
      src/Avalonia.Controls/Primitives/PopupRoot.cs
  8. 48
      src/Avalonia.Controls/Window.cs
  9. 60
      src/Avalonia.Controls/WindowBase.cs
  10. 2
      src/Avalonia.Input/Raw/RawPointerEventArgs.cs
  11. 22
      src/Avalonia.X11/X11Window.cs
  12. 10
      src/Avalonia.X11/XI2Manager.cs
  13. 7
      tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
  14. 82
      tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
  15. 81
      tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
  16. 139
      tests/Avalonia.Controls.UnitTests/WindowTests.cs
  17. 108
      tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs
  18. 111
      tests/Avalonia.UnitTests/MockWindowingPlatform.cs

2
azure-pipelines.yml

@ -68,7 +68,7 @@ jobs:
inputs: inputs:
script: | script: |
brew update brew update
brew install castxml brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/8a004a91a7fcd3f6620d5b01b6541ff0a640ffba/Formula/castxml.rb
- task: CmdLine@2 - task: CmdLine@2
displayName: 'Install Nuke' displayName: 'Install Nuke'

7
src/Avalonia.Controls/AutoCompleteBox.cs

@ -1630,7 +1630,7 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
/// <param name="sender">The source object.</param> /// <param name="sender">The source object.</param>
/// <param name="e">The event data.</param> /// <param name="e">The event data.</param>
private void DropDownPopup_Closed(object sender, EventArgs e) private void DropDownPopup_Closed(object sender, PopupClosedEventArgs e)
{ {
// Force the drop down dependency property to be false. // Force the drop down dependency property to be false.
if (IsDropDownOpen) if (IsDropDownOpen)
@ -1638,6 +1638,11 @@ namespace Avalonia.Controls
IsDropDownOpen = false; IsDropDownOpen = false;
} }
if (e.CloseEvent is PointerEventArgs pointerEvent)
{
pointerEvent.Handled = true;
}
// Fire the DropDownClosed event // Fire the DropDownClosed event
if (_popupHasOpened) if (_popupHasOpened)
{ {

7
src/Avalonia.Controls/Calendar/DatePicker.cs

@ -895,12 +895,17 @@ namespace Avalonia.Controls
_ignoreButtonClick = false; _ignoreButtonClick = false;
} }
} }
private void PopUp_Closed(object sender, EventArgs e) private void PopUp_Closed(object sender, PopupClosedEventArgs e)
{ {
IsDropDownOpen = false; IsDropDownOpen = false;
if(!_isPopupClosing) if(!_isPopupClosing)
{ {
if (e.CloseEvent is PointerEventArgs pointerEvent)
{
pointerEvent.Handled = true;
}
_isPopupClosing = true; _isPopupClosing = true;
Threading.Dispatcher.UIThread.InvokeAsync(() => _isPopupClosing = false); Threading.Dispatcher.UIThread.InvokeAsync(() => _isPopupClosing = false);
} }

7
src/Avalonia.Controls/ComboBox.cs

@ -242,11 +242,16 @@ namespace Avalonia.Controls
} }
} }
private void PopupClosed(object sender, EventArgs e) private void PopupClosed(object sender, PopupClosedEventArgs e)
{ {
_subscriptionsOnOpen?.Dispose(); _subscriptionsOnOpen?.Dispose();
_subscriptionsOnOpen = null; _subscriptionsOnOpen = null;
if (e.CloseEvent is PointerEventArgs pointerEvent)
{
pointerEvent.Handled = true;
}
if (CanFocus(this)) if (CanFocus(this))
{ {
Focus(); Focus();

57
src/Avalonia.Controls/Primitives/Popup.cs

@ -95,7 +95,7 @@ namespace Avalonia.Controls.Primitives
/// <summary> /// <summary>
/// Raised when the popup closes. /// Raised when the popup closes.
/// </summary> /// </summary>
public event EventHandler? Closed; public event EventHandler<PopupClosedEventArgs>? Closed;
/// <summary> /// <summary>
/// Raised when the popup opens. /// Raised when the popup opens.
@ -270,7 +270,7 @@ namespace Avalonia.Controls.Primitives
if (parentPopupRoot?.Parent is Popup popup) if (parentPopupRoot?.Parent is Popup popup)
{ {
DeferCleanup(SubscribeToEventHandler<Popup, EventHandler>(popup, ParentClosed, DeferCleanup(SubscribeToEventHandler<Popup, EventHandler<PopupClosedEventArgs>>(popup, ParentClosed,
(x, handler) => x.Closed += handler, (x, handler) => x.Closed += handler,
(x, handler) => x.Closed -= handler)); (x, handler) => x.Closed -= handler));
} }
@ -306,28 +306,7 @@ namespace Avalonia.Controls.Primitives
/// <summary> /// <summary>
/// Closes the popup. /// Closes the popup.
/// </summary> /// </summary>
public void Close() public void Close() => CloseCore(null);
{
if (_openState is null)
{
using (BeginIgnoringIsOpen())
{
IsOpen = false;
}
return;
}
_openState.Dispose();
_openState = null;
using (BeginIgnoringIsOpen())
{
IsOpen = false;
}
Closed?.Invoke(this, EventArgs.Empty);
}
/// <summary> /// <summary>
/// Measures the control. /// Measures the control.
@ -389,22 +368,44 @@ namespace Avalonia.Controls.Primitives
} }
} }
private void CloseCore(EventArgs? closeEvent)
{
if (_openState is null)
{
using (BeginIgnoringIsOpen())
{
IsOpen = false;
}
return;
}
_openState.Dispose();
_openState = null;
using (BeginIgnoringIsOpen())
{
IsOpen = false;
}
Closed?.Invoke(this, new PopupClosedEventArgs(closeEvent));
}
private void ListenForNonClientClick(RawInputEventArgs e) private void ListenForNonClientClick(RawInputEventArgs e)
{ {
var mouse = e as RawPointerEventArgs; var mouse = e as RawPointerEventArgs;
if (!StaysOpen && mouse?.Type == RawPointerEventType.NonClientLeftButtonDown) if (!StaysOpen && mouse?.Type == RawPointerEventType.NonClientLeftButtonDown)
{ {
Close(); CloseCore(e);
} }
} }
private void PointerPressedOutside(object sender, PointerPressedEventArgs e) private void PointerPressedOutside(object sender, PointerPressedEventArgs e)
{ {
if (!StaysOpen && !IsChildOrThis((IVisual)e.Source)) if (!StaysOpen && e.Source is IVisual v && !IsChildOrThis(v))
{ {
Close(); CloseCore(e);
e.Handled = true;
} }
} }

33
src/Avalonia.Controls/Primitives/PopupClosedEventArgs.cs

@ -0,0 +1,33 @@
using System;
using Avalonia.Interactivity;
#nullable enable
namespace Avalonia.Controls.Primitives
{
/// <summary>
/// Holds data for the <see cref="Popup.Closed"/> event.
/// </summary>
public class PopupClosedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="PopupClosedEventArgs"/> class.
/// </summary>
/// <param name="closeEvent"></param>
public PopupClosedEventArgs(EventArgs? closeEvent)
{
CloseEvent = closeEvent;
}
/// <summary>
/// Gets the event that closed the popup, if any.
/// </summary>
/// <remarks>
/// If <see cref="Popup.StaysOpen"/> is false, then this property will hold details of the
/// interaction that caused the popup to close if the close was caused by e.g. a pointer press
/// outside the popup. It can be used to mark the event as handled if the event should not
/// be propagated.
/// </remarks>
public EventArgs? CloseEvent { get; }
}
}

12
src/Avalonia.Controls/Primitives/PopupRoot.cs

@ -117,20 +117,14 @@ namespace Avalonia.Controls.Primitives
}); });
} }
/// <summary> protected override sealed Size ArrangeSetBounds(Size size)
/// Carries out the arrange pass of the window.
/// </summary>
/// <param name="finalSize">The final window size.</param>
/// <returns>The <paramref name="finalSize"/> parameter unchanged.</returns>
protected override Size ArrangeOverride(Size finalSize)
{ {
using (BeginAutoSizing()) using (BeginAutoSizing())
{ {
_positionerParameters.Size = finalSize; _positionerParameters.Size = size;
UpdatePosition(); UpdatePosition();
return ClientSize;
} }
return base.ArrangeOverride(PlatformImpl?.ClientSize ?? default(Size));
} }
} }
} }

48
src/Avalonia.Controls/Window.cs

@ -313,22 +313,7 @@ namespace Avalonia.Controls
/// Should be called from left mouse button press event handler /// Should be called from left mouse button press event handler
/// </summary> /// </summary>
public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e) => PlatformImpl?.BeginResizeDrag(edge, e); public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e) => PlatformImpl?.BeginResizeDrag(edge, e);
/// <summary>
/// Carries out the arrange pass of the window.
/// </summary>
/// <param name="finalSize">The final window size.</param>
/// <returns>The <paramref name="finalSize"/> parameter unchanged.</returns>
protected override Size ArrangeOverride(Size finalSize)
{
using (BeginAutoSizing())
{
PlatformImpl?.Resize(finalSize);
}
return base.ArrangeOverride(PlatformImpl?.ClientSize ?? default(Size));
}
/// <inheritdoc/> /// <inheritdoc/>
Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize; Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize;
@ -450,6 +435,19 @@ namespace Avalonia.Controls
EnsureInitialized(); EnsureInitialized();
IsVisible = true; 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); LayoutManager.ExecuteInitialLayoutPass(this);
using (BeginAutoSizing()) using (BeginAutoSizing())
@ -569,31 +567,30 @@ namespace Avalonia.Controls
} }
} }
/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
var sizeToContent = SizeToContent; var sizeToContent = SizeToContent;
var clientSize = ClientSize; var clientSize = ClientSize;
var constraint = availableSize; var constraint = clientSize;
if ((sizeToContent & SizeToContent.Width) != 0) if (sizeToContent.HasFlagCustom(SizeToContent.Width))
{ {
constraint = constraint.WithWidth(double.PositiveInfinity); constraint = constraint.WithWidth(double.PositiveInfinity);
} }
if ((sizeToContent & SizeToContent.Height) != 0) if (sizeToContent.HasFlagCustom(SizeToContent.Height))
{ {
constraint = constraint.WithHeight(double.PositiveInfinity); constraint = constraint.WithHeight(double.PositiveInfinity);
} }
var result = base.MeasureOverride(constraint); var result = base.MeasureOverride(constraint);
if ((sizeToContent & SizeToContent.Width) == 0) if (!sizeToContent.HasFlagCustom(SizeToContent.Width))
{ {
result = result.WithWidth(clientSize.Width); result = result.WithWidth(clientSize.Width);
} }
if ((sizeToContent & SizeToContent.Height) == 0) if (!sizeToContent.HasFlagCustom(SizeToContent.Height))
{ {
result = result.WithHeight(clientSize.Height); result = result.WithHeight(clientSize.Height);
} }
@ -601,6 +598,15 @@ namespace Avalonia.Controls
return result; return result;
} }
protected sealed override Size ArrangeSetBounds(Size size)
{
using (BeginAutoSizing())
{
PlatformImpl?.Resize(size);
return ClientSize;
}
}
protected sealed override void HandleClosed() protected sealed override void HandleClosed()
{ {
RaiseEvent(new RoutedEventArgs(WindowClosedEvent)); RaiseEvent(new RoutedEventArgs(WindowClosedEvent));

60
src/Avalonia.Controls/WindowBase.cs

@ -224,16 +224,66 @@ namespace Avalonia.Controls
/// <param name="clientSize">The new client size.</param> /// <param name="clientSize">The new client size.</param>
protected override void HandleResized(Size clientSize) protected override void HandleResized(Size clientSize)
{ {
if (!AutoSizing) Width = clientSize.Width;
{ Height = clientSize.Height;
Width = clientSize.Width;
Height = clientSize.Height;
}
ClientSize = clientSize; ClientSize = clientSize;
LayoutManager.ExecuteLayoutPass(); LayoutManager.ExecuteLayoutPass();
Renderer?.Resized(clientSize); Renderer?.Resized(clientSize);
} }
/// <summary>
/// Overrides the core measure logic for windows.
/// </summary>
/// <param name="availableSize">The available size.</param>
/// <returns>The measured size.</returns>
/// <remarks>
/// 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.
/// </remarks>
protected override Size MeasureCore(Size availableSize)
{
ApplyStyling();
ApplyTemplate();
var constraint = availableSize;
if (!double.IsNaN(Width))
{
constraint = constraint.WithWidth(Width);
}
if (!double.IsNaN(Height))
{
constraint = constraint.WithHeight(Height);
}
return MeasureOverride(constraint);
}
/// <summary>
/// Overrides the core arrange logic for windows.
/// </summary>
/// <param name="finalRect">The final arrange rect.</param>
/// <remarks>
/// 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.
/// </remarks>
protected override void ArrangeCore(Rect finalRect)
{
var constraint = ArrangeSetBounds(finalRect.Size);
var arrangeSize = ArrangeOverride(constraint);
Bounds = new Rect(arrangeSize);
}
/// <summary>
/// Called durung the arrange pass to set the size of the window.
/// </summary>
/// <param name="size">The requested size of the window.</param>
/// <returns>The actual size of the window.</returns>
protected virtual Size ArrangeSetBounds(Size size) => size;
/// <summary> /// <summary>
/// Handles a window position change notification from /// Handles a window position change notification from
/// <see cref="IWindowBaseImpl.PositionChanged"/>. /// <see cref="IWindowBaseImpl.PositionChanged"/>.

2
src/Avalonia.Input/Raw/RawPointerEventArgs.cs

@ -63,7 +63,7 @@ namespace Avalonia.Input.Raw
/// <summary> /// <summary>
/// Gets the type of the event. /// Gets the type of the event.
/// </summary> /// </summary>
public RawPointerEventType Type { get; private set; } public RawPointerEventType Type { get; set; }
/// <summary> /// <summary>
/// Gets the input modifiers. /// Gets the input modifiers.

22
src/Avalonia.X11/X11Window.cs

@ -649,7 +649,27 @@ namespace Avalonia.X11
ScheduleInput(args); ScheduleInput(args);
} }
public void ScheduleInput(RawInputEventArgs args) public void ScheduleXI2Input(RawInputEventArgs args)
{
if (args is RawPointerEventArgs pargs)
{
if ((pargs.Type == RawPointerEventType.TouchBegin
|| pargs.Type == RawPointerEventType.TouchUpdate
|| pargs.Type == RawPointerEventType.LeftButtonDown
|| pargs.Type == RawPointerEventType.RightButtonDown
|| pargs.Type == RawPointerEventType.MiddleButtonDown
|| pargs.Type == RawPointerEventType.NonClientLeftButtonDown)
&& ActivateTransientChildIfNeeded())
return;
if (pargs.Type == RawPointerEventType.TouchEnd
&& ActivateTransientChildIfNeeded())
pargs.Type = RawPointerEventType.TouchCancel;
}
ScheduleInput(args);
}
private void ScheduleInput(RawInputEventArgs args)
{ {
if (args is RawPointerEventArgs mouse) if (args is RawPointerEventArgs mouse)
mouse.Position = mouse.Position / Scaling; mouse.Position = mouse.Position / Scaling;

10
src/Avalonia.X11/XI2Manager.cs

@ -196,7 +196,7 @@ namespace Avalonia.X11
(ev.Type == XiEventType.XI_TouchUpdate ? (ev.Type == XiEventType.XI_TouchUpdate ?
RawPointerEventType.TouchUpdate : RawPointerEventType.TouchUpdate :
RawPointerEventType.TouchEnd); RawPointerEventType.TouchEnd);
client.ScheduleInput(new RawTouchEventArgs(client.TouchDevice, client.ScheduleXI2Input(new RawTouchEventArgs(client.TouchDevice,
ev.Timestamp, client.InputRoot, type, ev.Position, ev.Modifiers, ev.Detail)); ev.Timestamp, client.InputRoot, type, ev.Position, ev.Modifiers, ev.Detail));
return; return;
} }
@ -230,10 +230,10 @@ namespace Avalonia.X11
} }
if (scrollDelta != default) if (scrollDelta != default)
client.ScheduleInput(new RawMouseWheelEventArgs(client.MouseDevice, ev.Timestamp, client.ScheduleXI2Input(new RawMouseWheelEventArgs(client.MouseDevice, ev.Timestamp,
client.InputRoot, ev.Position, scrollDelta, ev.Modifiers)); client.InputRoot, ev.Position, scrollDelta, ev.Modifiers));
if (_pointerDevice.HasMotion(ev)) if (_pointerDevice.HasMotion(ev))
client.ScheduleInput(new RawPointerEventArgs(client.MouseDevice, ev.Timestamp, client.InputRoot, client.ScheduleXI2Input(new RawPointerEventArgs(client.MouseDevice, ev.Timestamp, client.InputRoot,
RawPointerEventType.Move, ev.Position, ev.Modifiers)); RawPointerEventType.Move, ev.Position, ev.Modifiers));
} }
@ -250,7 +250,7 @@ namespace Avalonia.X11
_ => (RawPointerEventType?)null _ => (RawPointerEventType?)null
}; };
if (type.HasValue) if (type.HasValue)
client.ScheduleInput(new RawPointerEventArgs(client.MouseDevice, ev.Timestamp, client.InputRoot, client.ScheduleXI2Input(new RawPointerEventArgs(client.MouseDevice, ev.Timestamp, client.InputRoot,
type.Value, ev.Position, ev.Modifiers)); type.Value, ev.Position, ev.Modifiers));
} }
@ -313,7 +313,7 @@ namespace Avalonia.X11
interface IXI2Client interface IXI2Client
{ {
IInputRoot InputRoot { get; } IInputRoot InputRoot { get; }
void ScheduleInput(RawInputEventArgs args); void ScheduleXI2Input(RawInputEventArgs args);
IMouseDevice MouseDevice { get; } IMouseDevice MouseDevice { get; }
TouchDevice TouchDevice { get; } TouchDevice TouchDevice { get; }
} }

7
tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs

@ -209,16 +209,17 @@ namespace Avalonia.Controls.UnitTests
screenImpl.Setup(x => x.ScreenCount).Returns(1); screenImpl.Setup(x => x.ScreenCount).Returns(1);
screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) }); 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); 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); windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
var services = TestServices.StyledWindow.With( var services = TestServices.StyledWindow.With(
inputManager: new InputManager(), inputManager: new InputManager(),
windowImpl: windowImpl.Object, windowImpl: windowImpl.Object,
windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, () => popupImpl.Object)); windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object));
return UnitTestApplication.Start(services); return UnitTestApplication.Start(services);
} }

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

@ -4,6 +4,7 @@ using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.LogicalTree; using Avalonia.LogicalTree;
using Avalonia.Platform;
using Avalonia.Styling; using Avalonia.Styling;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -172,9 +173,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<PopupRoot>((parent, scope) => Template = new FuncControlTemplate<PopupRoot>((parent, scope) =>
new ContentPresenter new ContentPresenter
@ -217,5 +284,16 @@ namespace Avalonia.Controls.UnitTests.Primitives
Popup = (Popup)this.GetVisualChildren().Single(); 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);
}
}
} }
} }

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

@ -298,13 +298,6 @@ namespace Avalonia.Controls.UnitTests.Primitives
} }
} }
Window PreparedWindow(object content = null)
{
var w = new Window {Content = content};
w.ApplyTemplate();
return w;
}
[Fact] [Fact]
public void DataContextBeginUpdate_Should_Not_Be_Called_For_Controls_That_Dont_Inherit() public void DataContextBeginUpdate_Should_Not_Be_Called_For_Controls_That_Dont_Inherit()
{ {
@ -351,18 +344,88 @@ namespace Avalonia.Controls.UnitTests.Primitives
} }
} }
[Fact]
public void StaysOpen_False_Should_Not_Handle_Closing_Click()
{
using (CreateServices())
{
var window = PreparedWindow();
var target = new Popup()
{
PlacementTarget = window ,
StaysOpen = false,
};
target.Open();
var e = CreatePointerPressedEventArgs(window);
window.RaiseEvent(e);
Assert.False(e.Handled);
}
}
[Fact]
public void Should_Pass_Closing_Click_To_Closed_Event()
{
using (CreateServices())
{
var window = PreparedWindow();
var target = new Popup()
{
PlacementTarget = window,
StaysOpen = false,
};
target.Open();
var press = CreatePointerPressedEventArgs(window);
var raised = 0;
target.Closed += (s, e) =>
{
Assert.Same(press, e.CloseEvent);
++raised;
};
window.RaiseEvent(press);
Assert.Equal(1, raised);
}
}
private IDisposable CreateServices() private IDisposable CreateServices()
{ {
return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform: return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform:
new MockWindowingPlatform(null, new MockWindowingPlatform(null,
() => x =>
{ {
if(UsePopupHost) if(UsePopupHost)
return null; return null;
return MockWindowingPlatform.CreatePopupMock().Object; return MockWindowingPlatform.CreatePopupMock(x).Object;
}))); })));
} }
private PointerPressedEventArgs CreatePointerPressedEventArgs(Window source)
{
var pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
return new PointerPressedEventArgs(
source,
pointer,
source,
default,
0,
new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonPressed),
KeyModifiers.None);
}
private Window PreparedWindow(object content = null)
{
var w = new Window { Content = content };
w.ApplyTemplate();
return w;
}
private static IControl PopupContentControlTemplate(PopupContentControl control, INameScope scope) private static IControl PopupContentControlTemplate(PopupContentControl control, INameScope scope)
{ {
return new Popup return new Popup

139
tests/Avalonia.Controls.UnitTests/WindowTests.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Layout;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.Rendering; using Avalonia.Rendering;
using Avalonia.UnitTests; using Avalonia.UnitTests;
@ -355,6 +356,27 @@ namespace Avalonia.Controls.UnitTests
} }
} }
[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] [Fact]
public void Child_Should_Be_Measured_With_Infinity_If_SizeToContent_Is_WidthAndHeight() public void Child_Should_Be_Measured_With_Infinity_If_SizeToContent_Is_WidthAndHeight()
{ {
@ -375,6 +397,123 @@ namespace Avalonia.Controls.UnitTests
} }
} }
[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<Size>())).Callback<Size>(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<IRenderer> renderer) private IWindowImpl CreateImpl(Mock<IRenderer> renderer)
{ {
return Mock.Of<IWindowImpl>(x => return Mock.Of<IWindowImpl>(x =>

108
tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs

@ -1,25 +1,12 @@
using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using Moq;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Presenters; using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates; 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.Styling;
using Avalonia.Themes.Default; using Avalonia.UnitTests;
using Avalonia.VisualTree; using Avalonia.VisualTree;
using Xunit; using Xunit;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using Avalonia.Controls.UnitTests;
using Avalonia.UnitTests;
namespace Avalonia.Layout.UnitTests namespace Avalonia.Layout.UnitTests
{ {
@ -28,10 +15,8 @@ namespace Avalonia.Layout.UnitTests
[Fact] [Fact]
public void Grandchild_Size_Changed() public void Grandchild_Size_Changed()
{ {
using (var context = AvaloniaLocator.EnterScope()) using (UnitTestApplication.Start(TestServices.StyledWindow))
{ {
RegisterServices();
Border border; Border border;
TextBlock textBlock; TextBlock textBlock;
@ -55,7 +40,6 @@ namespace Avalonia.Layout.UnitTests
}; };
window.Show(); window.Show();
window.LayoutManager.ExecuteInitialLayoutPass(window);
Assert.Equal(new Size(400, 400), border.Bounds.Size); Assert.Equal(new Size(400, 400), border.Bounds.Size);
textBlock.Width = 200; textBlock.Width = 200;
@ -68,10 +52,8 @@ namespace Avalonia.Layout.UnitTests
[Fact] [Fact]
public void Test_ScrollViewer_With_TextBlock() public void Test_ScrollViewer_With_TextBlock()
{ {
using (var context = AvaloniaLocator.EnterScope()) using (UnitTestApplication.Start(TestServices.StyledWindow))
{ {
RegisterServices();
ScrollViewer scrollViewer; ScrollViewer scrollViewer;
TextBlock textBlock; TextBlock textBlock;
@ -79,7 +61,6 @@ namespace Avalonia.Layout.UnitTests
{ {
Width = 800, Width = 800,
Height = 600, Height = 600,
SizeToContent = SizeToContent.WidthAndHeight,
Content = scrollViewer = new ScrollViewer Content = scrollViewer = new ScrollViewer
{ {
Width = 200, Width = 200,
@ -99,7 +80,6 @@ namespace Avalonia.Layout.UnitTests
window.Resources["ScrollBarThickness"] = 10.0; window.Resources["ScrollBarThickness"] = 10.0;
window.Show(); window.Show();
window.LayoutManager.ExecuteInitialLayoutPass(window);
Assert.Equal(new Size(800, 600), window.Bounds.Size); Assert.Equal(new Size(800, 600), window.Bounds.Size);
Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size); Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size);
@ -131,87 +111,5 @@ namespace Avalonia.Layout.UnitTests
{ {
return v.Bounds.Position; 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<FormattedTextLine> GetLines() => new FormattedTextLine[0];
public TextHitTestResult HitTestPoint(Point point) => new TextHitTestResult();
public Rect HitTestTextPosition(int index) => new Rect();
public IEnumerable<Rect> HitTestTextRange(int index, int length) => new Rect[0];
public Size Measure() => Constraint;
}
private void RegisterServices()
{
var globalStyles = new Mock<IGlobalStyles>();
var globalStylesResources = globalStyles.As<IResourceNode>();
var outObj = (object)10;
globalStylesResources.Setup(x => x.TryGetResource("FontSizeNormal", out outObj)).Returns(true);
var renderInterface = new Mock<IPlatformRenderInterface>();
renderInterface.Setup(x =>
x.CreateFormattedText(
It.IsAny<string>(),
It.IsAny<Typeface>(),
It.IsAny<double>(),
It.IsAny<TextAlignment>(),
It.IsAny<TextWrapping>(),
It.IsAny<Size>(),
It.IsAny<IReadOnlyList<FormattedTextStyleSpan>>()))
.Returns(new FormattedTextMock("TEST"));
var streamGeometry = new Mock<IStreamGeometryImpl>();
streamGeometry.Setup(x =>
x.Open())
.Returns(new Mock<IStreamGeometryContextImpl>().Object);
renderInterface.Setup(x =>
x.CreateStreamGeometry())
.Returns(streamGeometry.Object);
var windowImpl = new Mock<IWindowImpl>();
Size clientSize = default(Size);
windowImpl.SetupGet(x => x.ClientSize).Returns(() => clientSize);
windowImpl.Setup(x => x.Resize(It.IsAny<Size>())).Callback<Size>(s => clientSize = s);
windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1024, 1024));
windowImpl.SetupGet(x => x.Scaling).Returns(1);
AvaloniaLocator.CurrentMutable
.Bind<IStandardCursorFactory>().ToConstant(new CursorFactoryMock())
.Bind<IAssetLoader>().ToConstant(new AssetLoader())
.Bind<IInputManager>().ToConstant(new Mock<IInputManager>().Object)
.Bind<IGlobalStyles>().ToConstant(globalStyles.Object)
.Bind<IRuntimePlatform>().ToConstant(new AppBuilder().RuntimePlatform)
.Bind<IPlatformRenderInterface>().ToConstant(renderInterface.Object)
.Bind<IStyler>().ToConstant(new Styler())
.Bind<IFontManagerImpl>().ToConstant(new MockFontManagerImpl())
.Bind<ITextShaperImpl>().ToConstant(new MockTextShaperImpl())
.Bind<IWindowingPlatform>().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);
}
} }
} }

111
tests/Avalonia.UnitTests/MockWindowingPlatform.cs

@ -8,65 +8,118 @@ namespace Avalonia.UnitTests
{ {
public class MockWindowingPlatform : IWindowingPlatform public class MockWindowingPlatform : IWindowingPlatform
{ {
private static readonly Size s_screenSize = new Size(1280, 1024);
private readonly Func<IWindowImpl> _windowImpl; private readonly Func<IWindowImpl> _windowImpl;
private readonly Func<IPopupImpl> _popupImpl; private readonly Func<IWindowBaseImpl, IPopupImpl> _popupImpl;
public MockWindowingPlatform(Func<IWindowImpl> windowImpl = null, Func<IPopupImpl> popupImpl = null ) public MockWindowingPlatform(
Func<IWindowImpl> windowImpl = null,
Func<IWindowBaseImpl, IPopupImpl> popupImpl = null )
{ {
_windowImpl = windowImpl; _windowImpl = windowImpl;
_popupImpl = popupImpl; _popupImpl = popupImpl;
} }
public static Mock<IWindowImpl> CreateWindowMock(Func<IPopupImpl> popupImpl = null) public static Mock<IWindowImpl> CreateWindowMock()
{ {
var win = Mock.Of<IWindowImpl>(x => x.Scaling == 1); var windowImpl = new Mock<IWindowImpl>();
var mock = Mock.Get(win); var position = new PixelPoint();
mock.Setup(x => x.Show()).Callback(() => 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(() =>
{ {
mock.Object.Activated?.Invoke(); return CreatePopupMock(windowImpl.Object).Object;
}); });
mock.Setup(x => x.CreatePopup()).Returns(() =>
windowImpl.Setup(x => x.Dispose()).Callback(() =>
{ {
if (popupImpl != null) windowImpl.Object.Closed?.Invoke();
return popupImpl(); });
return CreatePopupMock().Object;
windowImpl.Setup(x => x.Move(It.IsAny<PixelPoint>())).Callback<PixelPoint>(x =>
{
position = x;
windowImpl.Object.PositionChanged?.Invoke(x);
});
windowImpl.Setup(x => x.Resize(It.IsAny<Size>())).Callback<Size>(x =>
{
clientSize = x.Constrain(s_screenSize);
windowImpl.Object.Resized?.Invoke(clientSize);
}); });
mock.Setup(x => x.Dispose()).Callback(() =>
windowImpl.Setup(x => x.Show()).Callback(() =>
{ {
mock.Object.Closed?.Invoke(); windowImpl.Object.Activated?.Invoke();
}); });
PixelPoint pos = default;
mock.SetupGet(x => x.Position).Returns(() => pos); return windowImpl;
mock.Setup(x => x.Move(It.IsAny<PixelPoint>())).Callback(new Action<PixelPoint>(np => pos = np));
SetupToplevel(mock);
return mock;
} }
static void SetupToplevel<T>(Mock<T> mock) where T : class, ITopLevelImpl public static Mock<IPopupImpl> CreatePopupMock(IWindowBaseImpl parent)
{ {
mock.SetupGet(x => x.MouseDevice).Returns(new MouseDevice()); var popupImpl = new Mock<IPopupImpl>();
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<IPopupImpl> CreatePopupMock() public static Mock<IScreenImpl> CreateScreenMock()
{ {
var positioner = Mock.Of<IPopupPositioner>(); var screenImpl = new Mock<IScreenImpl>();
var popup = Mock.Of<IPopupImpl>(x => x.Scaling == 1); var bounds = new PixelRect(0, 0, (int)s_screenSize.Width, (int)s_screenSize.Height);
var mock = Mock.Get(popup); var screen = new Screen(96, bounds, bounds, true);
mock.SetupGet(x => x.PopupPositioner).Returns(positioner); screenImpl.Setup(x => x.AllScreens).Returns(new[] { screen });
SetupToplevel(mock); screenImpl.Setup(x => x.ScreenCount).Returns(1);
return screenImpl;
return mock;
} }
public IWindowImpl CreateWindow() 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() public IEmbeddableWindowImpl CreateEmbeddableWindow()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private static void SetupToplevel<T>(Mock<T> mock) where T : class, ITopLevelImpl
{
mock.SetupGet(x => x.MouseDevice).Returns(new MouseDevice());
}
} }
} }

Loading…
Cancel
Save