From 4b3a456fd0528f283fdc8f79921d791e8039e13d Mon Sep 17 00:00:00 2001 From: mrward Date: Sun, 6 Feb 2011 11:16:58 +0000 Subject: [PATCH] Fix null reference when XmlTreeView not created and clipboard handler methods called. --- .../XmlEditor/Project/Src/XmlTreeView.cs | 5 +- .../Src/XmlTreeViewContainerControl.cs | 22 ++++--- .../AddElementsToTreeControlTestFixture.cs | 6 +- ...ditCommentNodesInTreeControlTestFixture.cs | 2 +- .../Test/Tree/MenuCommandsTestFixture.cs | 6 +- .../Test/Tree/OwnerStatusTestFixture.cs | 10 +-- .../Tree/PasteInTreeControlTestFixture.cs | 7 +- ...emoveElementsFromTreeControlTestFixture.cs | 7 +- ...moveTextNodesFromTreeControlTestFixture.cs | 8 ++- .../RootNodeAddedToTreeControlTestFixture.cs | 13 ++-- ...ixture.cs => XmlTreeViewContainerTests.cs} | 26 +++++--- .../DerivedXmlTreeViewContainerControl.cs | 66 +++++-------------- .../XmlEditor/Test/XmlEditor.Tests.csproj | 2 +- 13 files changed, 81 insertions(+), 99 deletions(-) rename src/AddIns/DisplayBindings/XmlEditor/Test/Tree/{XmlTreeViewContainerTestFixture.cs => XmlTreeViewContainerTests.cs} (96%) diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs index 0090ca5f65..1fbafd1049 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs @@ -32,7 +32,7 @@ namespace ICSharpCode.XmlEditor this.defaultSchema = defaultSchema; this.TabPageText = "${res:ICSharpCode.XmlEditor.XmlTreeView.Title}"; - this.treeViewContainer = new XmlTreeViewContainerControl(); + this.treeViewContainer = new XmlTreeViewContainerControl(schemas, defaultSchema); this.treeViewContainer.DirtyChanged += TreeViewContainerDirtyChanged; treeViewContainer.AttributesGrid.ContextMenuStrip = MenuService.CreateContextMenu(treeViewContainer, "/AddIns/XmlEditor/XmlTree/AttributesGrid/ContextMenu"); treeViewContainer.TreeView.ContextMenuStrip = MenuService.CreateContextMenu(treeViewContainer, "/AddIns/XmlEditor/XmlTree/ContextMenu"); @@ -133,7 +133,8 @@ namespace ICSharpCode.XmlEditor protected override void LoadFromPrimary() { IFileDocumentProvider provider = this.PrimaryViewContent as IFileDocumentProvider; - treeViewContainer.LoadXml(provider.GetDocumentForFile(this.PrimaryFile).Text, schemas, defaultSchema); + IDocument document = provider.GetDocumentForFile(this.PrimaryFile); + treeViewContainer.LoadXml(document.Text); XmlView view = XmlView.ForFile(this.PrimaryFile); if (view != null) { XmlView.CheckIsWellFormed(view.TextEditor); diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs index 50dedef5f4..a9d5668e5d 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs @@ -38,9 +38,15 @@ namespace ICSharpCode.XmlEditor public event EventHandler DirtyChanged; public XmlTreeViewContainerControl() + : this(new XmlSchemaCompletionCollection(), null) + { + } + + public XmlTreeViewContainerControl(XmlSchemaCompletionCollection schemas, XmlSchemaCompletion defaultSchema) { InitializeComponent(); InitImages(); + editor = new XmlTreeEditor(this, schemas, defaultSchema); } /// @@ -135,23 +141,23 @@ namespace ICSharpCode.XmlEditor IsErrorMessageTextBoxVisible = true; } - /// - /// Displays the specified xml as a tree. - /// - public void LoadXml(string xml, XmlSchemaCompletionCollection schemas, XmlSchemaCompletion defaultSchema) + public void LoadXml(string xml) { textBox.Clear(); IsAttributesGridVisible = true; ClearAttributes(); - editor = new XmlTreeEditor(this, schemas, defaultSchema); editor.LoadXml(xml); - // Expand document element node. + ExpandRootDocumentElementNode(); + } + + void ExpandRootDocumentElementNode() + { if (xmlElementTreeView.Nodes.Count > 0) { xmlElementTreeView.Nodes[0].Expand(); - } - } + } + } /// /// Gets or sets the xml document to be shown in this diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddElementsToTreeControlTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddElementsToTreeControlTestFixture.cs index 090bb327bf..fe6e599021 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddElementsToTreeControlTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddElementsToTreeControlTestFixture.cs @@ -7,6 +7,7 @@ using NUnit.Framework; using System; using System.Windows.Forms; using System.Xml; +using XmlEditor.Tests.Utils; namespace XmlEditor.Tests.Tree { @@ -19,13 +20,12 @@ namespace XmlEditor.Tests.Tree [SetUp] public void SetUpFixture() { - using (XmlTreeViewContainerControl treeViewContainer = new XmlTreeViewContainerControl()) { - treeViewContainer.LoadXml("", new XmlSchemaCompletionCollection(), null); + using (DerivedXmlTreeViewContainerControl treeViewContainer = new DerivedXmlTreeViewContainerControl()) { + treeViewContainer.LoadXml(""); doc = treeViewContainer.Document; XmlTreeViewControl treeView = treeViewContainer.TreeView; - //treeView.DocumentElement = doc.DocumentElement; rootNode = (XmlElementTreeNode)treeView.Nodes[0]; // No node selected in treeview - adding a child diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/EditCommentNodesInTreeControlTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/EditCommentNodesInTreeControlTestFixture.cs index 826fea7ff6..b06eb68628 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/EditCommentNodesInTreeControlTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/EditCommentNodesInTreeControlTestFixture.cs @@ -35,7 +35,7 @@ namespace XmlEditor.Tests.Tree { treeViewContainer = new DerivedXmlTreeViewContainerControl(); string xml = ""; - treeViewContainer.LoadXml(xml, new XmlSchemaCompletionCollection(), null); + treeViewContainer.LoadXml(xml); doc = treeViewContainer.Document; treeView = treeViewContainer.TreeView; diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MenuCommandsTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MenuCommandsTestFixture.cs index 1ee7fed637..fb1943c895 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MenuCommandsTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MenuCommandsTestFixture.cs @@ -30,11 +30,7 @@ namespace XmlEditor.Tests.Tree public void Init() { treeViewContainer = new DerivedXmlTreeViewContainerControl(); - - XmlSchemaCompletion xhtmlSchema = new XmlSchemaCompletion(ResourceManager.ReadXhtmlStrictSchema()); - XmlSchemaCompletionCollection schemas = new XmlSchemaCompletionCollection(); - - treeViewContainer.LoadXml("", schemas, null); + treeViewContainer.LoadXml(""); doc = treeViewContainer.Document; treeView = treeViewContainer.TreeView; diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/OwnerStatusTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/OwnerStatusTestFixture.cs index ac61e55cbd..c29df7c9e1 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/OwnerStatusTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/OwnerStatusTestFixture.cs @@ -16,7 +16,7 @@ namespace XmlEditor.Tests.Tree [TestFixture] public class OwnerStatusTestFixture { - XmlTreeViewContainerControl treeViewContainer; + DerivedXmlTreeViewContainerControl treeViewContainer; XmlTreeViewControl treeView; XmlDocument doc; XmlElementTreeNode htmlTreeNode; @@ -28,12 +28,8 @@ namespace XmlEditor.Tests.Tree [SetUp] public void Init() { - treeViewContainer = new XmlTreeViewContainerControl(); - - XmlSchemaCompletion xhtmlSchema = new XmlSchemaCompletion(ResourceManager.ReadXhtmlStrictSchema()); - XmlSchemaCompletionCollection schemas = new XmlSchemaCompletionCollection(); - - treeViewContainer.LoadXml("

Text

", schemas, null); + treeViewContainer = new DerivedXmlTreeViewContainerControl(); + treeViewContainer.LoadXml("

Text

"); doc = treeViewContainer.Document; treeView = treeViewContainer.TreeView; diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/PasteInTreeControlTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/PasteInTreeControlTestFixture.cs index d5ab5cb797..255899a164 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/PasteInTreeControlTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/PasteInTreeControlTestFixture.cs @@ -6,6 +6,7 @@ using System.Xml; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.XmlEditor; using NUnit.Framework; +using XmlEditor.Tests.Utils; namespace XmlEditor.Tests.Tree { @@ -16,7 +17,7 @@ namespace XmlEditor.Tests.Tree public class PasteInTreeControlTestFixture { XmlDocument doc; - XmlTreeViewContainerControl treeViewContainerControl; + DerivedXmlTreeViewContainerControl treeViewContainerControl; XmlTreeViewControl treeView; IClipboardHandler clipboardHandler; XmlElementTreeNode htmlTreeNode; @@ -33,9 +34,9 @@ namespace XmlEditor.Tests.Tree [SetUp] public void SetUp() { - treeViewContainerControl = new XmlTreeViewContainerControl(); + treeViewContainerControl = new DerivedXmlTreeViewContainerControl(); treeView = treeViewContainerControl.TreeView; - treeViewContainerControl.LoadXml(GetXml(), new XmlSchemaCompletionCollection(), null); + treeViewContainerControl.LoadXml(GetXml()); doc = treeViewContainerControl.Document; clipboardHandler = treeViewContainerControl as IClipboardHandler; diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementsFromTreeControlTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementsFromTreeControlTestFixture.cs index 78add5b00c..06fd5795e5 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementsFromTreeControlTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementsFromTreeControlTestFixture.cs @@ -6,6 +6,7 @@ using System.Xml; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.XmlEditor; using NUnit.Framework; +using XmlEditor.Tests.Utils; namespace XmlEditor.Tests.Tree { @@ -13,15 +14,15 @@ namespace XmlEditor.Tests.Tree public class RemoveElementsFromTreeControlTestFixture { XmlDocument doc; - XmlTreeViewContainerControl treeViewContainerControl; + DerivedXmlTreeViewContainerControl treeViewContainerControl; XmlTreeViewControl treeView; [SetUp] public void SetUp() { - treeViewContainerControl = new XmlTreeViewContainerControl(); + treeViewContainerControl = new DerivedXmlTreeViewContainerControl(); treeView = treeViewContainerControl.TreeView; - treeViewContainerControl.LoadXml("", new XmlSchemaCompletionCollection(), null); + treeViewContainerControl.LoadXml(""); doc = treeViewContainerControl.Document; } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodesFromTreeControlTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodesFromTreeControlTestFixture.cs index f2cf811f82..84264724f6 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodesFromTreeControlTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodesFromTreeControlTestFixture.cs @@ -4,9 +4,11 @@ using System; using System.Windows.Forms; using System.Xml; + using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.XmlEditor; using NUnit.Framework; +using XmlEditor.Tests.Utils; namespace XmlEditor.Tests.Tree { @@ -14,7 +16,7 @@ namespace XmlEditor.Tests.Tree public class RemoveTextNodesFromTreeControlTestFixture { XmlDocument doc; - XmlTreeViewContainerControl treeViewContainerControl; + DerivedXmlTreeViewContainerControl treeViewContainerControl; XmlTreeViewControl treeView; XmlElementTreeNode topElementTreeNode; XmlElementTreeNode childElementTreeNode; @@ -23,10 +25,10 @@ namespace XmlEditor.Tests.Tree [SetUp] public void SetUp() { - treeViewContainerControl = new XmlTreeViewContainerControl(); + treeViewContainerControl = new DerivedXmlTreeViewContainerControl(); treeView = treeViewContainerControl.TreeView; string xml = "texttext"; - treeViewContainerControl.LoadXml(xml, new XmlSchemaCompletionCollection(), null); + treeViewContainerControl.LoadXml(xml); doc = treeViewContainerControl.Document; ExtTreeNode rootNode = (ExtTreeNode)treeView.Nodes[0]; diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RootNodeAddedToTreeControlTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RootNodeAddedToTreeControlTestFixture.cs index 6b901a3074..870d03d59b 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RootNodeAddedToTreeControlTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RootNodeAddedToTreeControlTestFixture.cs @@ -1,13 +1,15 @@ // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) -using ICSharpCode.SharpDevelop.Gui; -using ICSharpCode.XmlEditor; -using NUnit.Framework; using System; using System.Windows.Forms; using System.Xml; +using ICSharpCode.SharpDevelop.Gui; +using ICSharpCode.XmlEditor; +using NUnit.Framework; +using XmlEditor.Tests.Utils; + namespace XmlEditor.Tests.Tree { [TestFixture] @@ -23,9 +25,8 @@ namespace XmlEditor.Tests.Tree [TestFixtureSetUp] public void SetUpFixture() { - using (XmlTreeViewContainerControl treeViewContainer = new XmlTreeViewContainerControl()) { - - treeViewContainer.LoadXml("", new XmlSchemaCompletionCollection(), null); + using (DerivedXmlTreeViewContainerControl treeViewContainer = new DerivedXmlTreeViewContainerControl()) { + treeViewContainer.LoadXml(""); doc = treeViewContainer.Document; XmlTreeViewControl treeView = treeViewContainer.TreeView; diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTests.cs similarity index 96% rename from src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTestFixture.cs rename to src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTests.cs index f92e455ed4..e01efc38b1 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTests.cs @@ -17,13 +17,12 @@ namespace XmlEditor.Tests.Tree /// tests do not really fit into any other test fixture. ///
[TestFixture] - public class XmlTreeViewContainerTestFixture + public class XmlTreeViewContainerTests { XmlDocument doc; XmlTreeViewControl treeView; DerivedXmlTreeViewContainerControl treeViewContainer; RichTextBox textBox; - XmlSchemaCompletionCollection schemas; PropertyGrid attributesGrid; SplitContainer splitContainer; RichTextBox errorMessageTextBox; @@ -43,11 +42,8 @@ namespace XmlEditor.Tests.Tree { treeViewContainer = new DerivedXmlTreeViewContainerControl(); treeViewContainer.DirtyChanged += TreeViewContainerDirtyChanged; - - XmlSchemaCompletion xhtmlSchema = new XmlSchemaCompletion(ResourceManager.ReadXhtmlStrictSchema()); - schemas = new XmlSchemaCompletionCollection(); - treeViewContainer.LoadXml("text", schemas, null); + treeViewContainer.LoadXml("text"); doc = treeViewContainer.Document; treeView = treeViewContainer.TreeView; @@ -62,6 +58,11 @@ namespace XmlEditor.Tests.Tree attributesGrid = (PropertyGrid)splitContainer.Panel2.Controls["attributesGrid"]; } + void CreateTreeViewContainer() + { + treeViewContainer = new DerivedXmlTreeViewContainerControl(); + } + [TearDown] public void TearDown() { @@ -126,7 +127,7 @@ namespace XmlEditor.Tests.Tree public void TextBoxClearedAfterLoadXml() { treeViewContainer.ShowTextContent("test"); - treeViewContainer.LoadXml("", schemas, null); + treeViewContainer.LoadXml(""); Assert.AreEqual(String.Empty, textBox.Text); AttributesGridOnTop(); @@ -148,7 +149,7 @@ namespace XmlEditor.Tests.Tree Assert.IsNotNull(attributesGrid.SelectedObject); // Loading new xml should clear the attributes grid. - treeViewContainer.LoadXml("", schemas, null); + treeViewContainer.LoadXml(""); Assert.IsNull(attributesGrid.SelectedObject, "Should be no SelectedObject in the attributes grid after loading new xml."); @@ -436,5 +437,14 @@ namespace XmlEditor.Tests.Tree { dirtyChanged = true; } + + [Test] + public void EnableDelete_XmlNotLoadedIntoEditor_DoesNotThrowNullReferenceException() + { + CreateTreeViewContainer(); + + bool result = false; + Assert.DoesNotThrow(() => result = treeViewContainer.EnableDelete); + } } } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeViewContainerControl.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeViewContainerControl.cs index d81edaa4c3..d4ea8e6170 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeViewContainerControl.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeViewContainerControl.cs @@ -17,55 +17,23 @@ namespace XmlEditor.Tests.Utils ///
public class DerivedXmlTreeViewContainerControl : XmlTreeViewContainerControl { - List addElementDialogElementNamesReturned = new List(); - DialogResult addElementDialogResult = DialogResult.OK; - List addAttributeDialogAttributeNamesReturned = new List(); - DialogResult addAttributeDialogResult = DialogResult.OK; + public List AddElementDialogElementNamesReturned = new List(); + public DialogResult AddElementDialogResult = DialogResult.OK; + public List AddAttributeDialogAttributeNamesReturned = new List(); + public DialogResult AddAttributeDialogResult = DialogResult.OK; + public XmlSchemaCompletionCollection Schemas; - /// - /// This is the list of element names that will be returned from - /// the mock AddElementDialog. - /// - public List AddElementDialogElementNamesReturned { - get { - return addElementDialogElementNamesReturned; - } - } - - /// - /// Gets or sets the dialog result for the AddElementDialog. - /// - public DialogResult AddElementDialogResult { - get { - return addElementDialogResult; - } - set { - addElementDialogResult = value; - } - } - - /// - /// Gets the list of attribute names that will be returned - /// from the mock AddAttributeDialog. - /// - public List AddAttributeDialogAttributeNamesReturned { - get { - return addAttributeDialogAttributeNamesReturned; - } + public DerivedXmlTreeViewContainerControl() + : this(new XmlSchemaCompletionCollection()) + { } - /// - /// Gets or sets the dialog result for the AddAttributeDialog. - /// - public DialogResult AddAttributeDialogResult { - get { - return addAttributeDialogResult; - } - set { - addAttributeDialogResult = value; - } + public DerivedXmlTreeViewContainerControl(XmlSchemaCompletionCollection schemas) + : base(schemas, null) + { + this.Schemas = schemas; } - + /// /// Allows us to call the XmlTreeViewContainerControl's /// TextBoxChanged method to fake the user typing in text @@ -128,8 +96,8 @@ namespace XmlEditor.Tests.Utils protected override IAddXmlNodeDialog CreateAddElementDialog(string[] elementNames) { MockAddXmlNodeDialog dialog = new MockAddXmlNodeDialog(); - dialog.SetNamesToReturn(addElementDialogElementNamesReturned.ToArray()); - dialog.SetDialogResult(addElementDialogResult); + dialog.SetNamesToReturn(AddElementDialogElementNamesReturned.ToArray()); + dialog.SetDialogResult(AddElementDialogResult); return dialog; } @@ -139,8 +107,8 @@ namespace XmlEditor.Tests.Utils protected override IAddXmlNodeDialog CreateAddAttributeDialog(string[] attributeNames) { MockAddXmlNodeDialog dialog = new MockAddXmlNodeDialog(); - dialog.SetNamesToReturn(addAttributeDialogAttributeNamesReturned.ToArray()); - dialog.SetDialogResult(addAttributeDialogResult); + dialog.SetNamesToReturn(AddAttributeDialogAttributeNamesReturned.ToArray()); + dialog.SetDialogResult(AddAttributeDialogResult); return dialog; } } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj b/src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj index bba244b017..20c2779bed 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj @@ -211,7 +211,7 @@ - +