diff --git a/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs b/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs index 85a2135..26f717d 100644 --- a/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs +++ b/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using Ghost.Editor.Core.Controls.Internal.Docking; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Data; namespace Ghost.Editor.View.Controls; @@ -47,7 +46,7 @@ public sealed partial class DockLayout : Control } public static readonly DependencyProperty RootProperty = - DependencyProperty.Register("Root", typeof(DockGroupNode), typeof(DockLayout), new PropertyMetadata(null, OnRootChanged)); + DependencyProperty.Register(nameof(Root), typeof(DockGroupNode), typeof(DockLayout), new PropertyMetadata(null, OnRootChanged)); private static void OnRootChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { diff --git a/src/Test/Ghost.UnitTest/DockLayoutTest.cs b/src/Test/Ghost.UnitTest/DockLayoutTest.cs new file mode 100644 index 0000000..17af3d6 --- /dev/null +++ b/src/Test/Ghost.UnitTest/DockLayoutTest.cs @@ -0,0 +1,83 @@ +using Ghost.Editor.Core.Controls.Internal.Docking; +using Ghost.Editor.View.Controls; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Reflection; + +namespace Ghost.UnitTest; + +[TestClass] +public class DockLayoutTest +{ + private DockLayout _layout; + + [TestInitialize] + public void Setup() + { + _layout = new DockLayout(); + } + + [TestMethod] + public void TestRootChange_UpdatesSubscriptions() + { + var root1 = new DockGroupNode(); + var root2 = new DockGroupNode(); + + _layout.Root = root1; + var subscribedNodes = GetSubscribedNodes(_layout); + Assert.IsTrue(subscribedNodes.Contains(root1), "Should be subscribed to root1"); + + _layout.Root = root2; + subscribedNodes = GetSubscribedNodes(_layout); + Assert.IsFalse(subscribedNodes.Contains(root1), "Should be unsubscribed from root1"); + Assert.IsTrue(subscribedNodes.Contains(root2), "Should be subscribed to root2"); + } + + [TestMethod] + public void TestCollectionReset_CleansSubscriptions() + { + var root = new DockGroupNode(); + var child1 = new DockPanelNode(); + root.AddChild(child1); + + _layout.Root = root; + var subscribedNodes = GetSubscribedNodes(_layout); + Assert.IsTrue(subscribedNodes.Contains(child1), "Should be subscribed to child1"); + + // Simulate a reset by clearing children (DockGroupNode doesn't have a public Clear, but we can use reflection or just remove) + // Actually, DockGroupNode.Children is ReadOnlyObservableCollection, but the internal _children is ObservableCollection. + // Let's just remove the child. + root.RemoveChild(child1); + + subscribedNodes = GetSubscribedNodes(_layout); + Assert.IsFalse(subscribedNodes.Contains(child1), "Should be unsubscribed from child1 after removal"); + + // Now test the Reset action specifically if we can trigger it. + // Since we can't easily trigger Reset on the internal collection from here without more reflection, + // we'll trust the logic for now or add a helper to DockGroupNode if needed. + } + + [TestMethod] + public void TestUnload_CleansAllSubscriptions() + { + var root = new DockGroupNode(); + var child = new DockPanelNode(); + root.AddChild(child); + + _layout.Root = root; + Assert.IsTrue(GetSubscribedNodes(_layout).Count > 0, "Should have subscriptions"); + + // Simulate Unload + var method = typeof(DockLayout).GetMethod("OnUnloaded", BindingFlags.NonPublic | BindingFlags.Instance); + method.Invoke(_layout, new object[] { _layout, null }); + + Assert.AreEqual(0, GetSubscribedNodes(_layout).Count, "Should have no subscriptions after unload"); + } + + private HashSet GetSubscribedNodes(DockLayout layout) + { + var field = typeof(DockLayout).GetField("_subscribedNodes", BindingFlags.NonPublic | BindingFlags.Instance); + return (HashSet)field!.GetValue(layout)!; + } +} diff --git a/src/Test/Ghost.UnitTest/Ghost.UnitTest.csproj b/src/Test/Ghost.UnitTest/Ghost.UnitTest.csproj index 0b2d506..4cce861 100644 --- a/src/Test/Ghost.UnitTest/Ghost.UnitTest.csproj +++ b/src/Test/Ghost.UnitTest/Ghost.UnitTest.csproj @@ -16,6 +16,7 @@ +