Browse Source

Fix NRE in VisualNode.SortChildren.

`_children` may be null. In addition if there are < 2 children, there's no sorting to be done.
Steven Kirk 6 years ago
parent
commit
cdb486fe23

+ 5 - 0
src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs

@@ -179,6 +179,11 @@ namespace Avalonia.Rendering.SceneGraph
         /// <param name="scene">The scene that the node is a part of.</param>
         public void SortChildren(Scene scene)
         {
+            if (_children == null || _children.Count <= 1)
+            {
+                return;
+            }
+
             var keys = new List<long>();
 
             for (var i = 0; i < Visual.VisualChildren.Count; ++i)

+ 9 - 0
tests/Avalonia.Visuals.UnitTests/Rendering/SceneGraph/VisualNodeTests.cs

@@ -92,5 +92,14 @@ namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
             Assert.Same(node1.DrawOperations[0].Item, node2.DrawOperations[0].Item);
             Assert.NotSame(node1.DrawOperations[0], node2.DrawOperations[0]);
         }
+
+        [Fact]
+        public void SortChildren_Does_Not_Throw_On_Null_Children()
+        {
+            var node = new VisualNode(Mock.Of<IVisual>(), null);
+            var scene = new Scene(Mock.Of<IVisual>());
+
+            node.SortChildren(scene);
+        }
     }
 }