Browse Source

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

pull/16734/head
Tom Edwards 2 years ago
committed by GitHub
parent
commit
3b1c1f4234
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 16
      src/Avalonia.Base/Layout/Layoutable.cs
  2. 24
      tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs

16
src/Avalonia.Base/Layout/Layoutable.cs

@ -74,37 +74,37 @@ namespace Avalonia.Layout
/// 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.
@ -153,6 +153,10 @@ namespace Avalonia.Layout
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>

24
tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.UnitTests;
@ -387,6 +388,29 @@ namespace Avalonia.Base.UnitTests.Layout
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; }

Loading…
Cancel
Save