Index: Core/Common/src/Core.Common.Gui/GuiCore.cs =================================================================== diff -u -r48a7db25af36c9f686e45e428840d3381f877371 -rb099f576f45f1a8fb167d965b357ba675105f4e4 --- Core/Common/src/Core.Common.Gui/GuiCore.cs (.../GuiCore.cs) (revision 48a7db25af36c9f686e45e428840d3381f877371) +++ Core/Common/src/Core.Common.Gui/GuiCore.cs (.../GuiCore.cs) (revision b099f576f45f1a8fb167d965b357ba675105f4e4) @@ -517,6 +517,12 @@ { selectionProviders.Remove(selectionProvider); selectionProvider.SelectionChanged -= OnSelectionChanged; + + // Clear the current selection if it's no longer applicable + if (Selection != null && !selectionProviders.Select(sp => sp.Selection).Any(s => Selection.Equals(s))) + { + Selection = null; + } } } Index: Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs =================================================================== diff -u -r650277f75faab8d29b4a07b738f0fd2a99ac7ed3 -rb099f576f45f1a8fb167d965b357ba675105f4e4 --- Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs (.../GuiCoreTest.cs) (revision 650277f75faab8d29b4a07b738f0fd2a99ac7ed3) +++ Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs (.../GuiCoreTest.cs) (revision b099f576f45f1a8fb167d965b357ba675105f4e4) @@ -1025,7 +1025,7 @@ [STAThread] public void GivenGuiWithoutSelection_WhenSelectionProviderAdded_ThenSelectionSynced() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1040,10 +1040,10 @@ // Precondition Assert.IsNull(gui.Selection); - // Call + // When gui.ViewHost.AddDocumentView(selectionProvider); - // Assert + // Then Assert.AreSame(selectionProvider.Selection, gui.Selection); } mocks.VerifyAll(); @@ -1053,7 +1053,7 @@ [STAThread] public void GivenGuiWithRandomSelection_WhenSelectionProviderAdded_ThenSelectionSynced() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1067,10 +1067,10 @@ gui.Selection = new object(); - // Call + // When gui.ViewHost.AddDocumentView(selectionProvider); - // Assert + // Then Assert.AreSame(selectionProvider.Selection, gui.Selection); } mocks.VerifyAll(); @@ -1080,7 +1080,7 @@ [STAThread] public void GivenGuiWithRandomSelection_WhenNonSelectionProviderAdded_ThenSelectionPreserved() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1094,10 +1094,10 @@ gui.Selection = selection; - // Call + // When gui.ViewHost.AddDocumentView(new TestView()); - // Assert + // Then Assert.AreSame(selection, gui.Selection); } mocks.VerifyAll(); @@ -1107,7 +1107,7 @@ [STAThread] public void GivenGuiWithRandomSelection_WhenSelectionChangedOnAddedSelectionProvider_ThenSelectionSynced() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1122,10 +1122,10 @@ gui.Selection = new object(); - // Call + // When selectionProvider.ChangeSelection(); - // Assert + // Then Assert.AreSame(selectionProvider.Selection, gui.Selection); } mocks.VerifyAll(); @@ -1135,7 +1135,7 @@ [STAThread] public void GivenGuiWithRandomSelection_WhenSelectionChangedOnRemovedSelectionProvider_ThenSelectionNoLongerSynced() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1152,10 +1152,10 @@ gui.Selection = selection; - // Call + // When selectionProvider.ChangeSelection(); - // Assert + // Then Assert.AreSame(selection, gui.Selection); } mocks.VerifyAll(); @@ -1165,7 +1165,7 @@ [STAThread] public void GivenGuiWithRandomSelection_WhenSelectionProviderBecomesActiveView_ThenSelectionSynced() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1181,10 +1181,10 @@ gui.Selection = new object(); - // Call + // When gui.ViewHost.SetFocusToView(selectionProvider); - // Assert + // Then Assert.AreSame(selectionProvider.Selection, gui.Selection); } mocks.VerifyAll(); @@ -1194,7 +1194,7 @@ [STAThread] public void GivenGuiWithRandomSelection_WhenNonSelectionProviderBecomesActiveView_ThenSelectionPreserved() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1210,20 +1210,78 @@ gui.Selection = selection; - // Call + // When gui.ViewHost.SetFocusToView(testView); - // Assert + // Then Assert.AreSame(selection, gui.Selection); } mocks.VerifyAll(); } [Test] [STAThread] + public void GivenGuiWithRandomSelection_WhenSelectionProviderRemoved_ThenSelectionPreserved() + { + // Given + var mocks = new MockRepository(); + var projectStore = mocks.Stub(); + var projectFactory = mocks.Stub(); + mocks.ReplayAll(); + + var testView = new TestView(); + var selection = new object(); + + using (var gui = new GuiCore(new MainWindow(), projectStore, projectFactory, new GuiCoreSettings())) + { + gui.Run(); + gui.ViewHost.AddDocumentView(testView); + + gui.Selection = selection; + + // When + gui.ViewHost.Remove(testView); + + // Then + Assert.AreSame(selection, gui.Selection); + } + mocks.VerifyAll(); + } + + [Test] + [STAThread] + public void GivenGuiWithSelectionFromSelectionProvider_WhenSelectionProviderRemoved_ThenSelectionCleared() + { + // Given + var mocks = new MockRepository(); + var projectStore = mocks.Stub(); + var projectFactory = mocks.Stub(); + mocks.ReplayAll(); + + var selectionProvider = new TestSelectionProvider(); + + using (var gui = new GuiCore(new MainWindow(), projectStore, projectFactory, new GuiCoreSettings())) + { + gui.Run(); + gui.ViewHost.AddDocumentView(selectionProvider); + + // Precondition + Assert.AreSame(selectionProvider.Selection, gui.Selection); + + // When + gui.ViewHost.Remove(selectionProvider); + + // Then + Assert.IsNull(gui.Selection); + } + mocks.VerifyAll(); + } + + [Test] + [STAThread] public void GivenGuiWithRandomSelection_WhenGuiDisposed_ThenSelectionNoLongerSynced() { - // Setup + // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectFactory = mocks.Stub(); @@ -1241,10 +1299,10 @@ // Precondition Assert.IsNull(gui.Selection); - // Call + // When selectionProvider.ChangeSelection(); - // Assert + // Then Assert.IsNull(gui.Selection); } mocks.VerifyAll();