Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate all width/height properties of Layoutable when they are set #15753

Merged
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
16 changes: 10 additions & 6 deletions src/Avalonia.Base/Layout/Layoutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,37 +74,37 @@ public class Layoutable : Visual
/// Defines the <see cref="Width"/> property.
/// </summary>
public static readonly StyledProperty<double> WidthProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(Width), double.NaN);
AvaloniaProperty.Register<Layoutable, double>(nameof(Width), double.NaN, validate: ValidateDimension);

/// <summary>
/// Defines the <see cref="Height"/> property.
/// </summary>
public static readonly StyledProperty<double> HeightProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(Height), double.NaN);
AvaloniaProperty.Register<Layoutable, double>(nameof(Height), double.NaN, validate: ValidateDimension);

/// <summary>
/// Defines the <see cref="MinWidth"/> property.
/// </summary>
public static readonly StyledProperty<double> MinWidthProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(MinWidth));
AvaloniaProperty.Register<Layoutable, double>(nameof(MinWidth), validate: ValidateMinimumDimension);

/// <summary>
/// Defines the <see cref="MaxWidth"/> property.
/// </summary>
public static readonly StyledProperty<double> MaxWidthProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(MaxWidth), double.PositiveInfinity);
AvaloniaProperty.Register<Layoutable, double>(nameof(MaxWidth), double.PositiveInfinity, validate: ValidateMaximumDimension);

/// <summary>
/// Defines the <see cref="MinHeight"/> property.
/// </summary>
public static readonly StyledProperty<double> MinHeightProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(MinHeight));
AvaloniaProperty.Register<Layoutable, double>(nameof(MinHeight), validate: ValidateMinimumDimension);

/// <summary>
/// Defines the <see cref="MaxHeight"/> property.
/// </summary>
public static readonly StyledProperty<double> MaxHeightProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(MaxHeight), double.PositiveInfinity);
AvaloniaProperty.Register<Layoutable, double>(nameof(MaxHeight), double.PositiveInfinity, validate: ValidateMaximumDimension);

/// <summary>
/// Defines the <see cref="Margin"/> property.
Expand Down Expand Up @@ -153,6 +153,10 @@ static Layoutable()
VerticalAlignmentProperty);
}

private static bool ValidateDimension(double value) => double.IsNaN(value) || ValidateMinimumDimension(value);
private static bool ValidateMinimumDimension(double value) => !double.IsPositiveInfinity(value) && ValidateMaximumDimension(value);
private static bool ValidateMaximumDimension(double value) => value >= 0;

/// <summary>
/// Occurs when the element's effective viewport changes.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.UnitTests;
Expand Down Expand Up @@ -387,6 +388,29 @@ public void Measuring_Invisible_Control_Should_Not_Invalidate_Parent_Measure()
Assert.Equal(default, child.DesiredSize);
}

[Fact]
public void Size_Properties_Reject_Invalid_Values()
{
var target = new Layoutable();

Assert.Multiple(() =>
{
SetShouldThrow([Layoutable.WidthProperty, Layoutable.HeightProperty], double.PositiveInfinity);
SetShouldThrow([Layoutable.WidthProperty, Layoutable.HeightProperty], -10);

SetShouldThrow([Layoutable.MinWidthProperty, Layoutable.MinHeightProperty], double.PositiveInfinity);
SetShouldThrow([Layoutable.MinWidthProperty, Layoutable.MinHeightProperty], -10);

SetShouldThrow([Layoutable.MaxWidthProperty, Layoutable.MaxHeightProperty], -10);

void SetShouldThrow(IEnumerable<StyledProperty<double>> properies, double value)
{
foreach (var prop in properies)
Assert.Throws<ArgumentException>(() => target.SetValue(prop, value));
}
});
}

private class TestLayoutable : Layoutable
{
public Size ArrangeSize { get; private set; }
Expand Down
Loading