Browse Source

Prevent null Panel children

pull/10256/head
Julien Lebosquain 4 years ago
parent
commit
97aa613bba
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 18
      src/Avalonia.Controls/Controls.cs
  2. 8
      tests/Avalonia.Controls.UnitTests/PanelTests.cs

18
src/Avalonia.Controls/Controls.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia.Collections; using Avalonia.Collections;
@ -13,7 +14,7 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
public Controls() public Controls()
{ {
ResetBehavior = ResetBehavior.Remove; Configure();
} }
/// <summary> /// <summary>
@ -21,9 +22,22 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
/// <param name="items">The initial items in the collection.</param> /// <param name="items">The initial items in the collection.</param>
public Controls(IEnumerable<Control> items) public Controls(IEnumerable<Control> items)
: base(items) {
Configure();
AddRange(items); // virtual member call in ctor, ok for our current implementation
}
private void Configure()
{ {
ResetBehavior = ResetBehavior.Remove; ResetBehavior = ResetBehavior.Remove;
Validate = item =>
{
if (item is null)
{
throw new ArgumentNullException(nameof(item),
$"A null control cannot be added to a {nameof(Controls)} collection.");
}
};
} }
} }
} }

8
tests/Avalonia.Controls.UnitTests/PanelTests.cs

@ -1,3 +1,4 @@
using System;
using System.Linq; using System.Linq;
using Avalonia.LogicalTree; using Avalonia.LogicalTree;
using Avalonia.Media; using Avalonia.Media;
@ -133,5 +134,12 @@ namespace Avalonia.Controls.UnitTests
renderer.Verify(x => x.AddDirty(target), Times.Once); renderer.Verify(x => x.AddDirty(target), Times.Once);
} }
[Fact]
public void Adding_Null_Child_Should_Throw()
{
var panel = new Panel();
Assert.Throws<ArgumentNullException>(() => panel.Children.Add(null!));
}
} }
} }

Loading…
Cancel
Save