Browse Source

XML Tree now supports removing elements, inserting and removing text nodes.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2111 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 19 years ago
parent
commit
27a2e4a0a8
  1. 2
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddAttributeDialog.cs
  2. 26
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddChildTextNodeCommand.cs
  3. 2
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddElementDialog.cs
  4. 30
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/IAddAttributeDialog.cs
  5. 30
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/IAddElementDialog.cs
  6. 35
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/IXmlTreeView.cs
  7. 27
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/InsertTextNodeAfterCommand.cs
  8. 27
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/InsertTextNodeBeforeCommand.cs
  9. 26
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/RemoveElementCommand.cs
  10. 26
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/RemoveTextNodeCommand.cs
  11. 11
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTextTreeNode.cs
  12. 89
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeEditor.cs
  13. 180
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs
  14. 172
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewControl.cs
  15. 37
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin
  16. 7
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj
  17. 10
      src/AddIns/DisplayBindings/XmlEditor/Test/Parser/QualifiedNameTestFixture.cs
  18. 19
      src/AddIns/DisplayBindings/XmlEditor/Test/Paths/NoElementPathTestFixture.cs
  19. 91
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddChildTextNodeTestFixture.cs
  20. 24
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddElementsToTreeControlTestFixture.cs
  21. 8
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/GetXmlAttributePropertyDescriptorTestFixture.cs
  22. 133
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/InsertTextNodeAfterTestFixture.cs
  23. 133
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/InsertTextNodeBeforeTestFixture.cs
  24. 400
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MenuCommandsTestFixture.cs
  25. 93
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MouseDownTestFixture.cs
  26. 119
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/OwnerStatusTestFixture.cs
  27. 97
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementTestFixture.cs
  28. 90
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementsFromTreeControlTestFixture.cs
  29. 90
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodeTestFixture.cs
  30. 105
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodesFromTreeControlTestFixture.cs
  31. 25
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RootNodeAddedToTreeControlTestFixture.cs
  32. 11
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/TextNodeTextChangedTestFixture.cs
  33. 27
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlAttributeTypeDescriptorTestFixture.cs
  34. 8
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTextTreeNodeTextTests.cs
  35. 334
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTestFixture.cs
  36. 29
      src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeView.cs
  37. 125
      src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeViewContainerControl.cs
  38. 65
      src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockAddAttributeDialog.cs
  39. 65
      src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockAddElementDialog.cs
  40. 96
      src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockXmlTreeView.cs
  41. 16
      src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj
  42. 13
      src/AddIns/DisplayBindings/XmlEditor/Test/app.config
  43. 6
      src/AddIns/DisplayBindings/XmlEditor/XmlEditor.sln

2
src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddAttributeDialog.cs

@ -13,7 +13,7 @@ using ICSharpCode.SharpDevelop.Gui.XmlForms; @@ -13,7 +13,7 @@ using ICSharpCode.SharpDevelop.Gui.XmlForms;
namespace ICSharpCode.XmlEditor
{
public class AddAttributeDialog : BaseSharpDevelopForm
public class AddAttributeDialog : BaseSharpDevelopForm, IAddAttributeDialog
{
ListBox attributesListBox;
Button okButton;

26
src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddChildTextNodeCommand.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.Core;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Adds a new text node to selected element.
/// </summary>
public class AddChildTextNodeCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.AppendChildTextNode();
}
}
}
}

2
src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddElementDialog.cs

@ -13,7 +13,7 @@ using ICSharpCode.SharpDevelop.Gui.XmlForms; @@ -13,7 +13,7 @@ using ICSharpCode.SharpDevelop.Gui.XmlForms;
namespace ICSharpCode.XmlEditor
{
public class AddElementDialog : BaseSharpDevelopForm
public class AddElementDialog : BaseSharpDevelopForm, IAddElementDialog
{
ListBox elementsListBox;
Button okButton;

30
src/AddIns/DisplayBindings/XmlEditor/Project/Src/IAddAttributeDialog.cs

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows.Forms;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Interface for the AddAttributeDialog.
/// </summary>
public interface IAddAttributeDialog : IDisposable
{
/// <summary>
/// The attribute names that should be added. These are the
/// attribute names that the user selected in the dialog when
/// it was closed.
/// </summary>
string[] AttributeNames {get;}
/// <summary>
/// Shows the dialog.
/// </summary>
DialogResult ShowDialog();
}
}

30
src/AddIns/DisplayBindings/XmlEditor/Project/Src/IAddElementDialog.cs

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows.Forms;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Interface for the AddElementDialog.
/// </summary>
public interface IAddElementDialog : IDisposable
{
/// <summary>
/// The element names that should be added. These are the
/// element names that the user selected in the dialog when
/// it was closed.
/// </summary>
string[] ElementNames {get;}
/// <summary>
/// Shows the dialog.
/// </summary>
DialogResult ShowDialog();
}
}

35
src/AddIns/DisplayBindings/XmlEditor/Project/Src/IXmlTreeView.cs

@ -104,5 +104,40 @@ namespace ICSharpCode.XmlEditor @@ -104,5 +104,40 @@ namespace ICSharpCode.XmlEditor
/// element.
/// </summary>
void InsertElementAfter(XmlElement element);
/// <summary>
/// Removes the specified element from the tree.
/// </summary>
void RemoveElement(XmlElement element);
/// <summary>
/// Appends a new child text node to the currently selected
/// element.
/// </summary>
void AppendChildTextNode(XmlText textNode);
/// <summary>
/// Inserts a new child text node before the currently
/// selected node.
/// </summary>
void InsertTextNodeBefore(XmlText textNode);
/// <summary>
/// Inserts a new child text node after the currently
/// selected node.
/// </summary>
void InsertTextNodeAfter(XmlText textNode);
/// <summary>
/// Removes the currently selected text node.
/// </summary>
void RemoveTextNode(XmlText textNode);
/// <summary>
/// Informs the xml tree view that the text node
/// has changed and the corresponding tree node
/// needs to be updated.
/// </summary>
void UpdateTextNode(XmlText textNode);
}
}

27
src/AddIns/DisplayBindings/XmlEditor/Project/Src/InsertTextNodeAfterCommand.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.Core;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Inserts a new text node to after the selected node.
/// </summary>
public class InsertTextNodeAfterCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.InsertTextNodeAfter();
}
}
}
}

27
src/AddIns/DisplayBindings/XmlEditor/Project/Src/InsertTextNodeBeforeCommand.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.Core;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Inserts a new text node to before the selected node.
/// </summary>
public class InsertTextNodeBeforeCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.InsertTextNodeBefore();
}
}
}
}

26
src/AddIns/DisplayBindings/XmlEditor/Project/Src/RemoveElementCommand.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.Core;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Removes the selected element from the XML tree.
/// </summary>
public class RemoveElementCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.RemoveElement();
}
}
}
}

26
src/AddIns/DisplayBindings/XmlEditor/Project/Src/RemoveTextNodeCommand.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.Core;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Removes the currently selected text node from the XML tree.
/// </summary>
public class RemoveTextNodeCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.RemoveTextNode();
}
}
}
}

11
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTextTreeNode.cs

@ -22,7 +22,7 @@ namespace ICSharpCode.XmlEditor @@ -22,7 +22,7 @@ namespace ICSharpCode.XmlEditor
this.xmlText = xmlText;
ImageKey = XmlTextTreeNodeImageKey;
SelectedImageKey = ImageKey;
Text = GetDisplayText(xmlText.InnerText);
Update();
}
public XmlText XmlText {
@ -31,6 +31,15 @@ namespace ICSharpCode.XmlEditor @@ -31,6 +31,15 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Updates the display text based on changes in the
/// XmlText associated with this node.
/// </summary>
public void Update()
{
Text = GetDisplayText(xmlText.InnerText);
}
/// <summary>
/// Gets the text to display for this tree node.
/// </summary>

89
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeEditor.cs

@ -131,6 +131,7 @@ namespace ICSharpCode.XmlEditor @@ -131,6 +131,7 @@ namespace ICSharpCode.XmlEditor
if (textNode != null) {
view.IsDirty = true;
textNode.Value = view.TextContent;
view.UpdateTextNode(textNode);
}
}
@ -200,6 +201,94 @@ namespace ICSharpCode.XmlEditor @@ -200,6 +201,94 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Removes the currently selected element.
/// </summary>
public void RemoveElement()
{
XmlElement selectedElement = view.SelectedElement;
if (selectedElement != null) {
XmlNode parentNode = selectedElement.ParentNode;
parentNode.RemoveChild(selectedElement);
view.IsDirty = true;
view.RemoveElement(selectedElement);
}
}
/// <summary>
/// Removes the currently select text node.
/// </summary>
public void RemoveTextNode()
{
XmlText textNode = view.SelectedTextNode;
if (textNode != null) {
XmlNode parentNode = textNode.ParentNode;
parentNode.RemoveChild(textNode);
view.IsDirty = true;
view.RemoveTextNode(textNode);
}
}
/// <summary>
/// Adds a child text node to the current selected element.
/// </summary>
public void AddChildTextNode()
{
XmlElement selectedElement = view.SelectedElement;
if (selectedElement != null) {
XmlText textNode = document.CreateTextNode(String.Empty);
selectedElement.AppendChild(textNode);
view.IsDirty = true;
view.AppendChildTextNode(textNode);
}
}
/// <summary>
/// Inserts a text node before the currently selected node.
/// </summary>
public void InsertTextNodeBefore()
{
// Get the currently selected text node or element.
XmlNode selectedNode = view.SelectedTextNode;
if (selectedNode == null) {
selectedNode = view.SelectedElement;
}
// Insert the text node before the selected node.
if (selectedNode != null) {
XmlElement parentElement = selectedNode.ParentNode as XmlElement;
if (parentElement != null) {
XmlText textNode = document.CreateTextNode(String.Empty);
parentElement.InsertBefore(textNode, selectedNode);
view.IsDirty = true;
view.InsertTextNodeBefore(textNode);
}
}
}
/// <summary>
/// Inserts a text node after the currently selected node.
/// </summary>
public void InsertTextNodeAfter()
{
// Get the currently selected text node or element.
XmlNode selectedNode = view.SelectedTextNode;
if (selectedNode == null) {
selectedNode = view.SelectedElement;
}
// Insert the text node after the selected node.
if (selectedNode != null) {
XmlElement parentElement = selectedNode.ParentNode as XmlElement;
if (parentElement != null) {
XmlText textNode = document.CreateTextNode(String.Empty);
parentElement.InsertAfter(textNode, selectedNode);
view.IsDirty = true;
view.InsertTextNodeAfter(textNode);
}
}
}
/// <summary>
/// Gets the missing attributes for the specified element based
/// on its associated schema.

180
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs

@ -33,7 +33,8 @@ namespace ICSharpCode.XmlEditor @@ -33,7 +33,8 @@ namespace ICSharpCode.XmlEditor
Nothing = 0,
ElementSelected = 1,
RootElementSelected = 2,
AttributeSelected = 4
AttributeSelected = 4,
TextNodeSelected = 8
}
public event EventHandler DirtyChanged;
@ -44,6 +45,9 @@ namespace ICSharpCode.XmlEditor @@ -44,6 +45,9 @@ namespace ICSharpCode.XmlEditor
InitImages();
}
/// <summary>
/// Gets the current XmlTreeViewContainerControlState.
/// </summary>
public Enum InternalState {
get {
XmlTreeViewContainerControlState state = XmlTreeViewContainerControlState.Nothing;
@ -56,6 +60,9 @@ namespace ICSharpCode.XmlEditor @@ -56,6 +60,9 @@ namespace ICSharpCode.XmlEditor
if (SelectedAttribute != null) {
state |= XmlTreeViewContainerControlState.AttributeSelected;
}
if (SelectedTextNode != null) {
state = XmlTreeViewContainerControlState.TextNodeSelected;
}
return state;
}
}
@ -248,7 +255,7 @@ namespace ICSharpCode.XmlEditor @@ -248,7 +255,7 @@ namespace ICSharpCode.XmlEditor
/// <returns>The attributes selected by the user.</returns>
public string[] SelectNewAttributes(string[] attributes)
{
using (AddAttributeDialog addAttributeDialog = new AddAttributeDialog(attributes)) {
using (IAddAttributeDialog addAttributeDialog = CreateAddAttributeDialog(attributes)) {
if (addAttributeDialog.ShowDialog() == DialogResult.OK) {
return addAttributeDialog.AttributeNames;
}
@ -273,7 +280,7 @@ namespace ICSharpCode.XmlEditor @@ -273,7 +280,7 @@ namespace ICSharpCode.XmlEditor
/// <returns>The elements selected by the user.</returns>
public string[] SelectNewElements(string[] elements)
{
using (AddElementDialog addElementDialog = new AddElementDialog(elements)) {
using (IAddElementDialog addElementDialog = CreateAddElementDialog(elements)) {
if (addElementDialog.ShowDialog() == DialogResult.OK) {
return addElementDialog.ElementNames;
}
@ -331,6 +338,100 @@ namespace ICSharpCode.XmlEditor @@ -331,6 +338,100 @@ namespace ICSharpCode.XmlEditor
xmlElementTreeView.InsertElementAfter(element);
}
/// <summary>
/// Removes the selected element.
/// </summary>
public void RemoveElement()
{
editor.RemoveElement();
}
/// <summary>
/// Removes the specified element from the tree.
/// </summary>
public void RemoveElement(XmlElement element)
{
xmlElementTreeView.RemoveElement(element);
}
/// <summary>
/// Appends a new text node to the currently selected
/// element.
/// </summary>
public void AppendChildTextNode(XmlText textNode)
{
xmlElementTreeView.AppendChildTextNode(textNode);
}
/// <summary>
/// Appends a new text node to the currently selected
/// element.
/// </summary>
public void AppendChildTextNode()
{
editor.AddChildTextNode();
}
/// <summary>
/// Inserts a new text node before the currently selected
/// node.
/// </summary>
public void InsertTextNodeBefore()
{
editor.InsertTextNodeBefore();
}
/// <summary>
/// Inserts a new text node before the currently selected
/// node.
/// </summary>
public void InsertTextNodeBefore(XmlText textNode)
{
xmlElementTreeView.InsertTextNodeBefore(textNode);
}
/// <summary>
/// Inserts a new text node after the currently selected
/// node.
/// </summary>
public void InsertTextNodeAfter()
{
editor.InsertTextNodeAfter();
}
/// <summary>
/// Inserts a new text node after the currently selected
/// node.
/// </summary>
public void InsertTextNodeAfter(XmlText textNode)
{
xmlElementTreeView.InsertTextNodeAfter(textNode);
}
/// <summary>
/// Removes the currently selected text node.
/// </summary>
public void RemoveTextNode()
{
editor.RemoveTextNode();
}
/// <summary>
/// Removes the currently selected text node.
/// </summary>
public void RemoveTextNode(XmlText textNode)
{
xmlElementTreeView.RemoveTextNode(textNode);
}
/// <summary>
/// Updates the corresponding tree node's text.
/// </summary>
public void UpdateTextNode(XmlText textNode)
{
xmlElementTreeView.UpdateTextNode(textNode);
}
/// <summary>
/// Disposes resources used by the control.
/// </summary>
@ -345,6 +446,26 @@ namespace ICSharpCode.XmlEditor @@ -345,6 +446,26 @@ namespace ICSharpCode.XmlEditor
base.Dispose(disposing);
}
/// <summary>
/// Creates a new AddElementDialog.
/// </summary>
/// <param name="elementNames">The element names to be listed in the
/// dialog.</param>
protected virtual IAddElementDialog CreateAddElementDialog(string[] elementNames)
{
return new AddElementDialog(elementNames);
}
/// <summary>
/// Creates a new AddAttributeDialog.
/// </summary>
/// <param name="attributeNames">The attribute names to be listed in the
/// dialog.</param>
protected virtual IAddAttributeDialog CreateAddAttributeDialog(string[] attributeNames)
{
return new AddAttributeDialog(attributeNames);
}
#region Forms Designer generated code
/// <summary>
@ -462,22 +583,22 @@ namespace ICSharpCode.XmlEditor @@ -462,22 +583,22 @@ namespace ICSharpCode.XmlEditor
#endregion
/// <summary>
/// Creates an image list that will be used for the XmlTreeViewControl.
/// This method is protected only so we can easily test
/// what happens when this method is called. Triggering
/// a TextChanged event is difficult to do from unit tests.
/// You can trigger it it by setting the textBox's Rtf property.
/// </summary>
void InitImages()
protected void TextBoxTextChanged(object sender, EventArgs e)
{
if (components == null) {
components = new Container();
}
ImageList images = new ImageList(components);
Image xmlElementImage = Image.FromStream(GetType().Assembly.GetManifestResourceStream("ICSharpCode.XmlEditor.Resources.XmlElementTreeNodeIcon.png"));
images.Images.Add(XmlElementTreeNode.XmlElementTreeNodeImageKey, xmlElementImage);
Image xmlTextImage = Image.FromStream(GetType().Assembly.GetManifestResourceStream("ICSharpCode.XmlEditor.Resources.XmlTextTreeNodeIcon.png"));
images.Images.Add(XmlTextTreeNode.XmlTextTreeNodeImageKey, xmlTextImage);
xmlElementTreeView.ImageList = images;
bool previousIsDirty = dirty;
editor.TextContentChanged();
OnXmlChanged(previousIsDirty);
}
void XmlElementTreeViewAfterSelect(object sender, TreeViewEventArgs e)
/// <summary>
/// This method is protected so we can test it.
/// </summary>
protected void XmlElementTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
if (xmlElementTreeView.IsTextNodeSelected) {
editor.SelectedTextNodeChanged();
@ -486,20 +607,34 @@ namespace ICSharpCode.XmlEditor @@ -486,20 +607,34 @@ namespace ICSharpCode.XmlEditor
}
}
void TextBoxTextChanged(object sender, EventArgs e)
/// <summary>
/// This method is protected so we can test it.
/// </summary>
protected void AttributesGridPropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
bool previousIsDirty = dirty;
editor.TextContentChanged();
editor.AttributeValueChanged();
OnXmlChanged(previousIsDirty);
}
void AttributesGridPropertyValueChanged(object s, PropertyValueChangedEventArgs e)
/// <summary>
/// Creates an image list that will be used for the XmlTreeViewControl.
/// </summary>
void InitImages()
{
bool previousIsDirty = dirty;
editor.AttributeValueChanged();
OnXmlChanged(previousIsDirty);
if (components == null) {
components = new Container();
}
ImageList images = new ImageList(components);
Image xmlElementImage = Image.FromStream(typeof(XmlTreeViewContainerControl).Assembly.GetManifestResourceStream("ICSharpCode.XmlEditor.Resources.XmlElementTreeNodeIcon.png"));
images.Images.Add(XmlElementTreeNode.XmlElementTreeNodeImageKey, xmlElementImage);
Image xmlTextImage = Image.FromStream(typeof(XmlTreeViewContainerControl).Assembly.GetManifestResourceStream("ICSharpCode.XmlEditor.Resources.XmlTextTreeNodeIcon.png"));
images.Images.Add(XmlTextTreeNode.XmlTextTreeNodeImageKey, xmlTextImage);
xmlElementTreeView.ImageList = images;
}
/// <summary>
/// Raises the dirty changed event if the dirty flag has changed.
/// </summary>
@ -545,9 +680,6 @@ namespace ICSharpCode.XmlEditor @@ -545,9 +680,6 @@ namespace ICSharpCode.XmlEditor
/// Gets or sets whether the text node text box is visible.
/// </summary>
bool IsTextBoxVisible {
get {
return textBoxVisible;
}
set {
textBoxVisible = value;
if (value) {

172
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewControl.cs

@ -35,6 +35,9 @@ namespace ICSharpCode.XmlEditor @@ -35,6 +35,9 @@ namespace ICSharpCode.XmlEditor
{
}
/// <summary>
/// Gets or sets the root element currently being displayed.
/// </summary>
[Browsable(false)]
public XmlElement DocumentElement {
get {
@ -53,6 +56,9 @@ namespace ICSharpCode.XmlEditor @@ -53,6 +56,9 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Gets the selected element in the tree.
/// </summary>
public XmlElement SelectedElement {
get {
XmlElementTreeNode xmlElementTreeNode = SelectedElementNode;
@ -63,12 +69,18 @@ namespace ICSharpCode.XmlEditor @@ -63,12 +69,18 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Determines whether an element is selected in the tree.
/// </summary>
public bool IsElementSelected {
get {
return SelectedElement != null;
}
}
/// <summary>
/// Gets the selected text node in the tree.
/// </summary>
public XmlText SelectedTextNode {
get {
XmlTextTreeNode xmlTextTreeNode = SelectedNode as XmlTextTreeNode;
@ -80,6 +92,9 @@ namespace ICSharpCode.XmlEditor @@ -80,6 +92,9 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Determines whether a text node is selected in the tree.
/// </summary>
public bool IsTextNodeSelected {
get {
return SelectedTextNode != null;
@ -115,6 +130,19 @@ namespace ICSharpCode.XmlEditor @@ -115,6 +130,19 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Appends a new child text node to the currently selected element.
/// </summary>
public void AppendChildTextNode(XmlText textNode)
{
XmlElementTreeNode selectedNode = SelectedElementNode;
if (selectedNode != null) {
XmlTextTreeNode newNode = new XmlTextTreeNode(textNode);
newNode.AddTo(selectedNode);
selectedNode.Expand();
}
}
/// <summary>
/// Inserts a new element node before the currently selected
/// node.
@ -133,6 +161,80 @@ namespace ICSharpCode.XmlEditor @@ -133,6 +161,80 @@ namespace ICSharpCode.XmlEditor
InsertElement(element, InsertionMode.After);
}
/// <summary>
/// Removes the specified element from the tree.
/// </summary>
public void RemoveElement(XmlElement element)
{
XmlElementTreeNode selectedElementTreeNode = SelectedNode as XmlElementTreeNode;
if (selectedElementTreeNode != null && selectedElementTreeNode.XmlElement == element) {
// Remove selected tree node.
selectedElementTreeNode.Remove();
} else {
XmlElementTreeNode elementTreeNode = FindElementNode(element, Nodes);
if (elementTreeNode != null) {
elementTreeNode.Remove();
}
}
}
/// <summary>
/// Removes the specified text node from the tree.
/// </summary>
public void RemoveTextNode(XmlText textNode)
{
XmlTextTreeNode selectedTextTreeNode = SelectedNode as XmlTextTreeNode;
if (selectedTextTreeNode != null && selectedTextTreeNode.XmlText == textNode) {
selectedTextTreeNode.Remove();
} else {
XmlTextTreeNode textTreeNode = FindTextNode(textNode, Nodes);
if (textTreeNode != null) {
textTreeNode.Remove();
}
}
}
/// <summary>
/// Inserts a text node before the currently selected
/// node.
/// </summary>
public void InsertTextNodeBefore(XmlText textNode)
{
InsertTextNode(textNode, InsertionMode.Before);
}
/// <summary>
/// Inserts a text node after the currently selected
/// node.
/// </summary>
public void InsertTextNodeAfter(XmlText textNode)
{
InsertTextNode(textNode, InsertionMode.After);
}
/// <summary>
/// Updates the corresponding tree node's text based on
/// the textNode's value.
/// </summary>
public void UpdateTextNode(XmlText textNode)
{
XmlTextTreeNode selectedTextTreeNode = SelectedNode as XmlTextTreeNode;
if (selectedTextTreeNode != null && selectedTextTreeNode.XmlText == textNode) {
selectedTextTreeNode.Update();
} else {
XmlTextTreeNode textTreeNode = FindTextNode(textNode, Nodes);
if (textTreeNode != null) {
textTreeNode.Update();
}
}
}
/// <summary>
/// If no node is selected after a mouse click then we make
/// sure the AfterSelect event is fired. Standard behaviour is
/// for the AfterSelect event not to be fired when the user
/// deselects all tree nodes.
/// </summary>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
@ -141,6 +243,9 @@ namespace ICSharpCode.XmlEditor @@ -141,6 +243,9 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Displays the document in the xml tree.
/// </summary>
void ShowDocumentElement()
{
Nodes.Clear();
@ -150,6 +255,9 @@ namespace ICSharpCode.XmlEditor @@ -150,6 +255,9 @@ namespace ICSharpCode.XmlEditor
}
}
/// <summary>
/// Returns the selected xml element tree node.
/// </summary>
XmlElementTreeNode SelectedElementNode {
get {
return SelectedNode as XmlElementTreeNode;
@ -173,5 +281,69 @@ namespace ICSharpCode.XmlEditor @@ -173,5 +281,69 @@ namespace ICSharpCode.XmlEditor
newNode.Insert(index, parentNode);
}
}
/// <summary>
/// Inserts a new text node either before or after the
/// currently selected node.
/// </summary>
void InsertTextNode(XmlText textNode, InsertionMode insertionMode)
{
ExtTreeNode selectedNode = (ExtTreeNode)SelectedNode;
if (selectedNode != null) {
XmlElementTreeNode parentNode = (XmlElementTreeNode)selectedNode.Parent;
XmlTextTreeNode newNode = new XmlTextTreeNode(textNode);
int index = parentNode.Nodes.IndexOf(selectedNode);
if (insertionMode == InsertionMode.After) {
index++;
}
newNode.Insert(index, parentNode);
}
}
/// <summary>
/// Looks at all the nodes in the tree view and returns the
/// tree node that represents the specified element.
/// </summary>
XmlElementTreeNode FindElementNode(XmlElement element, TreeNodeCollection nodes)
{
foreach (ExtTreeNode node in nodes) {
XmlElementTreeNode elementTreeNode = node as XmlElementTreeNode;
if (elementTreeNode != null) {
if (elementTreeNode.XmlElement == element) {
return elementTreeNode;
}
// Look for a match in the element's child nodes.
XmlElementTreeNode childElementTreeNode = FindElementNode(element, elementTreeNode.Nodes);
if (childElementTreeNode != null) {
return childElementTreeNode;
}
}
}
return null;
}
/// <summary>
/// Looks at all the nodes in the tree view and returns the
/// tree node that represents the specified text node.
/// </summary>
XmlTextTreeNode FindTextNode(XmlText textNode, TreeNodeCollection nodes)
{
foreach (ExtTreeNode node in nodes) {
XmlTextTreeNode textTreeNode = node as XmlTextTreeNode;
if (textTreeNode != null) {
if (textTreeNode.XmlText == textNode) {
return textTreeNode;
}
} else {
// Look for a match in the node's child nodes.
XmlTextTreeNode childTextTreeNode = FindTextNode(textNode, node.Nodes);
if (childTextTreeNode != null) {
return childTextTreeNode;
}
}
}
return null;
}
}
}

37
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin

@ -217,5 +217,42 @@ @@ -217,5 +217,42 @@
label = "${res:ICSharpCode.XmlEditor.XmlTreeView.AddChildElementMenuLabel}"
class = "ICSharpCode.XmlEditor.AddChildElementCommand"/>
</Condition>
<ComplexCondition action = "Disable">
<Or>
<Condition name = "Ownerstate" ownerstate = "TextNodeSelected"/>
<And>
<Condition name = "Ownerstate" ownerstate = "ElementSelected"/>
<Not>
<Condition name = "Ownerstate" ownerstate = "RootElementSelected"/>
</Not>
</And>
</Or>
<MenuItem id = "TextNodeCommandsSeparator" type = "Separator"/>
<MenuItem id = "InsertTextNodeBefore"
label = "${res:ICSharpCode.XmlEditor.XmlTreeView.InsertTextNodeBeforeMenuLabel}"
class = "ICSharpCode.XmlEditor.InsertTextNodeBeforeCommand"/>
<MenuItem id = "InsertTextNodeAfter"
label = "${res:ICSharpCode.XmlEditor.XmlTreeView.InsertTextNodeAfterMenuLabel}"
class = "ICSharpCode.XmlEditor.InsertTextNodeAfterCommand"/>
</ComplexCondition>
<Condition name = "Ownerstate" ownerstate = "ElementSelected" action = "Disable">
<MenuItem id = "AddChildTextNode"
label = "${res:ICSharpCode.XmlEditor.XmlTreeView.AddChildTextNodeAfterMenuLabel}"
class = "ICSharpCode.XmlEditor.AddChildTextNodeCommand"/>
</Condition>
<Condition name = "Ownerstate" ownerstate = "TextNodeSelected" action = "Exclude">
<MenuItem id = "RemoveTextNodeSeparator" type = "Separator"/>
<MenuItem id = "RemoveTextNode"
label = "${res:XML.MainMenu.EditMenu.Delete}"
icon = "Icons.16x16.DeleteIcon"
class = "ICSharpCode.XmlEditor.RemoveTextNodeCommand"/>
</Condition>
<Condition name = "Ownerstate" ownerstate = "ElementSelected" action = "Exclude">
<MenuItem id = "RemoveElementSeparator" type = "Separator"/>
<MenuItem id = "RemoveElement"
label = "${res:XML.MainMenu.EditMenu.Delete}"
icon = "Icons.16x16.DeleteIcon"
class = "ICSharpCode.XmlEditor.RemoveElementCommand"/>
</Condition>
</Path>
</AddIn>

7
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

@ -54,6 +54,13 @@ @@ -54,6 +54,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\AddChildTextNodeCommand.cs" />
<Compile Include="Src\IAddAttributeDialog.cs" />
<Compile Include="Src\IAddElementDialog.cs" />
<Compile Include="Src\InsertTextNodeAfterCommand.cs" />
<Compile Include="Src\InsertTextNodeBeforeCommand.cs" />
<Compile Include="Src\RemoveElementCommand.cs" />
<Compile Include="Src\RemoveTextNodeCommand.cs" />
<Compile Include="Src\XmlEditorControl.cs">
<SubType>UserControl</SubType>
</Compile>

10
src/AddIns/DisplayBindings/XmlEditor/Test/Parser/QualifiedNameTestFixture.cs

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.Xml;
namespace XmlEditor.Tests.Parser
{
@ -61,5 +62,14 @@ namespace XmlEditor.Tests.Parser @@ -61,5 +62,14 @@ namespace XmlEditor.Tests.Parser
Assert.IsFalse(name1 == name2, "Should not be the same.");
}
[Test]
public void HashCodeTest()
{
QualifiedName name1 = new QualifiedName("foo", "http://foo.com", "f");
XmlQualifiedName xmlQualifiedName = new XmlQualifiedName("foo", "http://foo.com");
Assert.AreEqual(name1.GetHashCode(), xmlQualifiedName.GetHashCode());
}
}
}

19
src/AddIns/DisplayBindings/XmlEditor/Test/Paths/NoElementPathTestFixture.cs

@ -37,6 +37,12 @@ namespace XmlEditor.Tests.Paths @@ -37,6 +37,12 @@ namespace XmlEditor.Tests.Paths
Assert.IsTrue(newPath.Equals(path), "Should be equal.");
}
[Test]
public void EqualsItself()
{
Assert.IsTrue(path.Equals(path), "Should be equal.");
}
[Test]
public void NotEqual()
{
@ -46,11 +52,24 @@ namespace XmlEditor.Tests.Paths @@ -46,11 +52,24 @@ namespace XmlEditor.Tests.Paths
Assert.IsFalse(newPath.Equals(path), "Should not be equal.");
}
[Test]
public void NotEqualToADifferentType()
{
Object o = new Object();
Assert.IsFalse(path.Equals(o), "Should not be equal.");
}
[Test]
public void Compact()
{
path.Compact();
Equality();
}
[Test]
public void HashCode()
{
Assert.AreEqual(path.Elements.GetHashCode(), path.GetHashCode());
}
}
}

91
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddChildTextNodeTestFixture.cs

@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.IO;
using System.Xml;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Checks that a text node is added by the XmlTreeEditor as
/// a child of the selected element.
/// </summary>
[TestFixture]
public class AddChildTextNodeTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement paragraphElement;
[SetUp]
public void Init()
{
base.InitFixture();
paragraphElement = (XmlElement)editor.Document.SelectSingleNode("/html/body/p");
mockXmlTreeView.SelectedElement = paragraphElement;
editor.AddChildTextNode();
}
[Test]
public void ParagraphElementHasChildNodes()
{
Assert.AreEqual(1, paragraphElement.ChildNodes.Count);
}
[Test]
public void NewTextNodeAdded()
{
XmlNode node = paragraphElement.FirstChild;
Assert.IsInstanceOfType(typeof(XmlText), node);
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
[Test]
public void AddChildTextNodeWhenNoElementSelected()
{
mockXmlTreeView.SelectedElement = null;
mockXmlTreeView.IsDirty = false;
editor.AddChildTextNode();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
[Test]
public void TextNodeAddedToView()
{
Assert.AreEqual(1, mockXmlTreeView.ChildTextNodesAdded.Count);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body>\r\n" +
"\t\t<p/>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

24
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddElementsToTreeControlTestFixture.cs

@ -23,40 +23,44 @@ namespace XmlEditor.Tests.Tree @@ -23,40 +23,44 @@ namespace XmlEditor.Tests.Tree
[SetUp]
public void SetUpFixture()
{
doc = new XmlDocument();
doc.LoadXml("<root/>");
using (XmlTreeViewControl treeView = new XmlTreeViewControl()) {
treeView.DocumentElement = doc.DocumentElement;
using (XmlTreeViewContainerControl treeViewContainer = new XmlTreeViewContainerControl()) {
XmlCompletionDataProvider completionDataProvider = new XmlCompletionDataProvider(new XmlSchemaCompletionDataCollection(), null, String.Empty);
treeViewContainer.LoadXml("<root/>", completionDataProvider);
doc = treeViewContainer.Document;
XmlTreeViewControl treeView = treeViewContainer.TreeView;
//treeView.DocumentElement = doc.DocumentElement;
rootNode = (XmlElementTreeNode)treeView.Nodes[0];
// No node selected in treeview - adding a child
// node should do nothing.
treeView.SelectedNode = null;
XmlElement testElement = doc.CreateElement("test");
treeView.AppendChildElement(testElement);
treeViewContainer.AppendChildElement(testElement);
treeView.SelectedNode = rootNode;
XmlElement childElement = doc.CreateElement("child");
treeView.AppendChildElement(childElement);
treeViewContainer.AppendChildElement(childElement);
// No node selected in treeview - inserting a node
// node should do nothing.
treeView.SelectedNode = null;
treeView.AppendChildElement(testElement);
treeViewContainer.AppendChildElement(testElement);
XmlElementTreeNode childNode = (XmlElementTreeNode)rootNode.Nodes[0];
treeView.SelectedNode = childNode;
XmlElement beforeElement = doc.CreateElement("before");
treeView.InsertElementBefore(beforeElement);
treeViewContainer.InsertElementBefore(beforeElement);
// No node selected in treeview - inserting a node
// node should do nothing.
treeView.SelectedNode = null;
treeView.AppendChildElement(testElement);
treeViewContainer.AppendChildElement(testElement);
treeView.SelectedNode = childNode;
XmlElement afterElement = doc.CreateElement("after");
treeView.InsertElementAfter(afterElement);
treeViewContainer.InsertElementAfter(afterElement);
}
}

8
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/GetXmlAttributePropertyDescriptorTestFixture.cs

@ -93,5 +93,13 @@ namespace XmlEditor.Tests.Tree @@ -93,5 +93,13 @@ namespace XmlEditor.Tests.Tree
Assert.AreEqual("new value", (String)firstAttributePropertyDescriptor.GetValue(null));
Assert.AreEqual("new value", firstAttribute.Value);
}
[Test]
public void ResetValueDoesNothing()
{
firstAttributePropertyDescriptor.SetValue(null, "new value");
firstAttributePropertyDescriptor.ResetValue(null);
Assert.AreEqual("new value", firstAttribute.Value);
}
}
}

133
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/InsertTextNodeAfterTestFixture.cs

@ -0,0 +1,133 @@ @@ -0,0 +1,133 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.IO;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that a text node is inserted after the selected node
/// by the XmlTreeEditor.
/// </summary>
[TestFixture]
public class InsertTextNodeAfterTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement paragraphElement;
XmlText textNode;
[SetUp]
public void Init()
{
base.InitFixture();
paragraphElement = (XmlElement)editor.Document.SelectSingleNode("/html/body/p");
textNode = (XmlText)paragraphElement.SelectSingleNode("text()");
mockXmlTreeView.SelectedTextNode = textNode;
editor.InsertTextNodeAfter();
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
[Test]
public void ParagraphNodeHasTwoChildNodes()
{
Assert.AreEqual(2, paragraphElement.ChildNodes.Count);
}
[Test]
public void TextNodeInserted()
{
XmlText lastTextNode = (XmlText)paragraphElement.LastChild;
Assert.AreEqual(String.Empty, lastTextNode.Value);
}
/// <summary>
/// Makes sure that nothing happens if we try to insert a
/// text node when a text node is not already selected.
/// </summary>
[Test]
public void NoNodeSelected()
{
mockXmlTreeView.SelectedTextNode = null;
mockXmlTreeView.IsDirty = false;
editor.InsertTextNodeAfter();
ParagraphNodeHasTwoChildNodes();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
[Test]
public void TextNodeAddedToView()
{
Assert.AreEqual(1, mockXmlTreeView.TextNodesInsertedAfter.Count);
}
/// <summary>
/// Tests that we can insert a text node after the
/// an element if it is not the root element.
/// </summary>
[Test]
public void ElementSelected()
{
mockXmlTreeView.SelectedTextNode = null;
mockXmlTreeView.SelectedElement = paragraphElement;
mockXmlTreeView.IsDirty = false;
editor.InsertTextNodeAfter();
XmlElement bodyElement = (XmlElement)paragraphElement.ParentNode;
Assert.AreEqual(2, bodyElement.ChildNodes.Count);
Assert.IsInstanceOfType(typeof(XmlText), bodyElement.LastChild);
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Tests that we cannot insert a text node after the
/// root element.
/// </summary>
[Test]
public void RootElementSelected()
{
mockXmlTreeView.SelectedTextNode = null;
mockXmlTreeView.SelectedElement = editor.Document.DocumentElement;
mockXmlTreeView.IsDirty = false;
editor.InsertTextNodeAfter();
ParagraphNodeHasTwoChildNodes();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body>\r\n" +
"\t\t<p>text</p>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

133
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/InsertTextNodeBeforeTestFixture.cs

@ -0,0 +1,133 @@ @@ -0,0 +1,133 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.IO;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that a text node is inserted before the selected node
/// by the XmlTreeEditor.
/// </summary>
[TestFixture]
public class InsertTextNodeBeforeTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement paragraphElement;
XmlText textNode;
[SetUp]
public void Init()
{
base.InitFixture();
paragraphElement = (XmlElement)editor.Document.SelectSingleNode("/html/body/p");
textNode = (XmlText)paragraphElement.SelectSingleNode("text()");
mockXmlTreeView.SelectedTextNode = textNode;
editor.InsertTextNodeBefore();
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
[Test]
public void ParagraphNodeHasTwoChildNodes()
{
Assert.AreEqual(2, paragraphElement.ChildNodes.Count);
}
[Test]
public void TextNodeInserted()
{
XmlText firstTextNode = (XmlText)paragraphElement.ChildNodes[0];
Assert.AreEqual(String.Empty, firstTextNode.Value);
}
/// <summary>
/// Makes sure that nothing happens if we try to insert a
/// text node when a text node is not already selected.
/// </summary>
[Test]
public void NoNodeSelected()
{
mockXmlTreeView.SelectedTextNode = null;
mockXmlTreeView.IsDirty = false;
editor.InsertTextNodeBefore();
ParagraphNodeHasTwoChildNodes();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
[Test]
public void TextNodeAddedToView()
{
Assert.AreEqual(1, mockXmlTreeView.TextNodesInsertedBefore.Count);
}
/// <summary>
/// Tests that we can insert a text node before the
/// an element if it is not the root element.
/// </summary>
[Test]
public void ElementSelected()
{
mockXmlTreeView.SelectedTextNode = null;
mockXmlTreeView.SelectedElement = paragraphElement;
mockXmlTreeView.IsDirty = false;
editor.InsertTextNodeBefore();
XmlElement bodyElement = (XmlElement)paragraphElement.ParentNode;
Assert.AreEqual(2, bodyElement.ChildNodes.Count);
Assert.IsInstanceOfType(typeof(XmlText), bodyElement.FirstChild);
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Tests that we cannot insert a text node before the
/// root element.
/// </summary>
[Test]
public void RootElementSelected()
{
mockXmlTreeView.SelectedTextNode = null;
mockXmlTreeView.SelectedElement = editor.Document.DocumentElement;
mockXmlTreeView.IsDirty = false;
editor.InsertTextNodeBefore();
ParagraphNodeHasTwoChildNodes();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body>\r\n" +
"\t\t<p>text</p>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

400
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MenuCommandsTestFixture.cs

@ -0,0 +1,400 @@ @@ -0,0 +1,400 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests the various menu commands that can be used on the
/// Xml Tree (e.g. InsertElementBeforeCommand).
/// </summary>
[TestFixture]
public class MenuCommandsTestFixture
{
DerivedXmlTreeViewContainerControl treeViewContainer;
XmlDocument doc;
XmlTreeViewControl treeView;
XmlElement bodyElement;
XmlElementTreeNode htmlTreeNode;
XmlElementTreeNode bodyTreeNode;
[SetUp]
public void Init()
{
treeViewContainer = new DerivedXmlTreeViewContainerControl();
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
XmlSchemaCompletionData xhtmlSchema = new XmlSchemaCompletionData(reader);
XmlSchemaCompletionDataCollection schemas = new XmlSchemaCompletionDataCollection();
XmlCompletionDataProvider provider = new XmlCompletionDataProvider(schemas, xhtmlSchema, String.Empty);
treeViewContainer.LoadXml("<html><body></body></html>", provider);
doc = treeViewContainer.Document;
treeView = treeViewContainer.TreeView;
htmlTreeNode = (XmlElementTreeNode)treeView.Nodes[0];
htmlTreeNode.Expanding();
bodyTreeNode = (XmlElementTreeNode)htmlTreeNode.Nodes[0];
bodyElement = (XmlElement)doc.SelectSingleNode("/html/body");
}
[TearDown]
public void TearDown()
{
if (treeViewContainer != null) {
treeViewContainer.Dispose();
}
}
/// <summary>
/// Nothing should happen if the owner is the XmlTreeViewContainerControl.
/// </summary>
[Test]
public void InsertElementBeforeWithUnknownOwner()
{
InsertElementBeforeCommand command = new InsertElementBeforeCommand();
command.Owner = this;
command.Run();
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void InsertElementBeforeWithNullOwner()
{
InsertElementBeforeCommand command = new InsertElementBeforeCommand();
command.Run();
}
/// <summary>
/// Here we produce dummy data from our mock AddElementDialog
/// and check that the InsertElementBeforeCommand inserts
/// the correct element
/// </summary>
[Test]
public void InsertElementBefore()
{
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.AddElementDialogElementNamesReturned.Add("head");
InsertElementBeforeCommand command = new InsertElementBeforeCommand();
command.Owner = treeViewContainer;
command.Run();
XmlElement headElement = (XmlElement)doc.SelectSingleNode("/html/head");
Assert.IsNotNull(headElement, "Expected a new head element to be inserted.");
Assert.AreSame(headElement, doc.DocumentElement.FirstChild);
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void InsertElementAfterWithNullOwner()
{
InsertElementAfterCommand command = new InsertElementAfterCommand();
command.Run();
}
[Test]
public void InsertElementAfter()
{
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.AddElementDialogElementNamesReturned.Add("afterBody");
InsertElementAfterCommand command = new InsertElementAfterCommand();
command.Owner = treeViewContainer;
command.Run();
XmlElement newElement = (XmlElement)doc.SelectSingleNode("/html/afterBody");
Assert.IsNotNull(newElement, "Expected a new element to be inserted.");
Assert.AreSame(newElement, doc.DocumentElement.ChildNodes[1]);
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void AddChildElementWithNullOwner()
{
AddChildElementCommand command = new AddChildElementCommand();
command.Run();
}
[Test]
public void AddChildElement()
{
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.AddElementDialogElementNamesReturned.Add("p");
AddChildElementCommand command = new AddChildElementCommand();
command.Owner = treeViewContainer;
command.Run();
XmlElement paragraphElement = (XmlElement)bodyElement.SelectSingleNode("p");
Assert.IsNotNull(paragraphElement, "Expected a new <p> element to be appended.");
}
/// <summary>
/// Tests the XmlTreeViewContainer.SelectNewElements methods
/// returns an empty string array when the AddElementDialog
/// is cancelled.
/// </summary>
[Test]
public void AddElementDialogCancelled()
{
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.AddElementDialogElementNamesReturned.Add("p");
treeViewContainer.AddElementDialogResult = DialogResult.Cancel;
string[] elements = treeViewContainer.SelectNewElements(new string[] {"a"});
Assert.AreEqual(0, elements.Length);
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void AddAttributeWithNullOwner()
{
AddAttributeCommand command = new AddAttributeCommand();
command.Run();
}
[Test]
public void AddAttribute()
{
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.AddAttributeDialogAttributeNamesReturned.Add("class");
AddAttributeCommand command = new AddAttributeCommand();
command.Owner = treeViewContainer;
command.Run();
Assert.IsTrue(bodyElement.HasAttribute("class"));
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void RemoveAttributeWithNullOwner()
{
RemoveAttributeCommand command = new RemoveAttributeCommand();
command.Run();
}
[Test]
public void RemoveAttribute()
{
AddAttribute();
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.ShowAttributes(treeView.SelectedElement.Attributes);
Assert.IsNotNull(treeViewContainer.AttributesGrid.SelectedGridItem,
"Sanity check - should have a grid item selected.");
RemoveAttributeCommand command = new RemoveAttributeCommand();
command.Owner = treeViewContainer;
command.Run();
Assert.IsFalse(bodyElement.HasAttribute("class"));
}
/// <summary>
/// Tests the XmlTreeViewContainer.SelectNewAttributes methods
/// returns an empty string array when the AddAttributeDialog
/// is cancelled.
/// </summary>
[Test]
public void AddAttributeDialogCancelled()
{
treeView.SelectedNode = bodyTreeNode;
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.AddAttributeDialogAttributeNamesReturned.Add("class");
treeViewContainer.AddAttributeDialogResult = DialogResult.Cancel;
AddAttributeCommand command = new AddAttributeCommand();
string[] attributes = treeViewContainer.SelectNewAttributes(new string[] {"a"});
Assert.AreEqual(0, attributes.Length);
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void AddChildTextNodeWithNullOwner()
{
AddChildTextNodeCommand command = new AddChildTextNodeCommand();
command.Run();
}
[Test]
public void AddChildTextNode()
{
treeView.SelectedNode = bodyTreeNode;
AddChildTextNodeCommand command = new AddChildTextNodeCommand();
command.Owner = treeViewContainer;
command.Run();
XmlText textNode = bodyElement.SelectSingleNode("text()") as XmlText;
Assert.IsNotNull(textNode, "Expected a new text node element to be appended.");
XmlTextTreeNode textTreeNode = bodyTreeNode.Nodes[0] as XmlTextTreeNode;
Assert.IsNotNull(textTreeNode);
}
/// <summary>
/// Makes sure that nothing happens if we try to add a text
/// node to the currently selected element node when there
/// is no selected element node.
/// </summary>
[Test]
public void AddChildTextNodeWhenNoElementSelected()
{
treeView.SelectedNode = null;
XmlText newTextNode = doc.CreateTextNode(String.Empty);
treeView.AppendChildTextNode(newTextNode);
XmlText textNode = bodyElement.SelectSingleNode("text()") as XmlText;
Assert.IsNull(textNode);
}
/// <summary>
/// Nothing should happen if the owner is null.
/// </summary>
[Test]
public void InsertTextNodeBeforeWithNullOwner()
{
InsertTextNodeBeforeCommand command = new InsertTextNodeBeforeCommand();
command.Run();
}
[Test]
public void InsertTextNodeBefore()
{
AddChildTextNode();
XmlText textNode = bodyElement.SelectSingleNode("text()") as XmlText;
textNode.Value = "Original";
XmlTextTreeNode textTreeNode = bodyTreeNode.Nodes[0] as XmlTextTreeNode;
treeView.SelectedNode = textTreeNode;
InsertTextNodeBeforeCommand command = new InsertTextNodeBeforeCommand();
command.Owner = treeViewContainer;
command.Run();
XmlTextTreeNode insertedTextTreeNode = bodyTreeNode.Nodes[0] as XmlTextTreeNode;
XmlText insertedTextNode = bodyElement.FirstChild as XmlText;
Assert.IsNotNull(insertedTextTreeNode);
Assert.AreEqual(2, bodyTreeNode.Nodes.Count);
Assert.AreEqual(String.Empty, insertedTextTreeNode.XmlText.Value);
Assert.IsNotNull(insertedTextNode);
Assert.AreEqual(2, bodyElement.ChildNodes.Count);
Assert.AreEqual(String.Empty, insertedTextNode.Value);
}
/// <summary>
/// Makes sure that nothing happens if we try to add a text
/// node when there is no currently selected node.
/// </summary>
[Test]
public void InsertTextNodeWhenNoTreeNodeSelected()
{
treeView.SelectedNode = null;
XmlText newTextNode = doc.CreateTextNode(String.Empty);
treeView.InsertTextNodeBefore(newTextNode);
XmlText textNode = bodyElement.SelectSingleNode("text()") as XmlText;
Assert.IsNull(textNode);
}
[Test]
public void InsertTextNodeAfterWithNullOwner()
{
InsertTextNodeAfterCommand command = new InsertTextNodeAfterCommand();
command.Run();
}
[Test]
public void InsertTextNodeAfter()
{
AddChildTextNode();
XmlText textNode = bodyElement.SelectSingleNode("text()") as XmlText;
textNode.Value = "OriginalTextNode";
XmlTextTreeNode textTreeNode = bodyTreeNode.Nodes[0] as XmlTextTreeNode;
treeView.SelectedNode = textTreeNode;
InsertTextNodeAfterCommand command = new InsertTextNodeAfterCommand();
command.Owner = treeViewContainer;
command.Run();
XmlTextTreeNode insertedTextTreeNode = bodyTreeNode.LastNode as XmlTextTreeNode;
XmlText insertedTextNode = bodyElement.LastChild as XmlText;
Assert.IsNotNull(insertedTextTreeNode);
Assert.AreEqual(2, bodyTreeNode.Nodes.Count);
Assert.AreEqual(String.Empty, insertedTextTreeNode.XmlText.Value);
Assert.IsNotNull(insertedTextNode);
Assert.AreEqual(2, bodyElement.ChildNodes.Count);
Assert.AreEqual(String.Empty, insertedTextNode.Value);
}
/// <summary>
/// Expect nothing to happen since the ICommand.Owner is not
/// set.
/// </summary>
[Test]
public void RemoveElementCommandWithNullOwner()
{
RemoveElementCommand command = new RemoveElementCommand();
command.Run();
}
[Test]
public void RemoveRootElementUsingCommand()
{
treeView.SelectedNode = treeView.Nodes[0];
RemoveElementCommand command = new RemoveElementCommand();
command.Owner = treeViewContainer;
command.Run();
Assert.AreEqual(0, treeView.Nodes.Count);
Assert.IsTrue(treeViewContainer.IsDirty);
}
[Test]
public void RemoveTextNodeWithNullOwner()
{
RemoveTextNodeCommand command = new RemoveTextNodeCommand();
command.Run();
}
[Test]
public void RemoveTextNode()
{
AddChildTextNode();
treeView.SelectedNode = bodyTreeNode.Nodes[0];
RemoveTextNodeCommand command = new RemoveTextNodeCommand();
command.Owner = treeViewContainer;
command.Run();
Assert.IsFalse(bodyElement.HasChildNodes);
Assert.AreEqual(0, bodyTreeNode.Nodes.Count);
}
}
}

93
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/MouseDownTestFixture.cs

@ -0,0 +1,93 @@ @@ -0,0 +1,93 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that the XmlTreeView fires an AfterSelect event
/// when the user clicks into the XmlTreeView control but not
/// onto a node and deselects all nodes. The standard behaviour
/// of a tree control is to not fire an AfterSelect event in
/// this case.
/// </summary>
[TestFixture]
public class MouseDownTestFixture
{
DerivedXmlTreeViewControl treeView;
List<TreeViewEventArgs> treeViewEventArgs;
[SetUp]
public void SetUpFixture()
{
treeViewEventArgs = new List<TreeViewEventArgs>();
treeView = new DerivedXmlTreeViewControl();
treeView.Height = 100;
treeView.Nodes.Add(new ExtTreeNode());
treeView.AfterSelect += XmlTreeViewAfterSelect;
}
[TearDown]
public void TearDownFixture()
{
if (treeView != null) {
treeView.AfterSelect -= XmlTreeViewAfterSelect;
treeView.Dispose();
}
}
/// <summary>
/// Make sure the AfterSelect event is not fired twice and the
/// standard behaviour of the OnMouseDown method is preserved
/// in the normal case.
/// </summary>
[Test]
public void MouseDownWithNodeSelected()
{
treeView.SelectedNode = treeView.Nodes[0];
// Make sure the button click will select the first node
// so choose x=0, y=0.
MouseEventArgs e = new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0);
treeView.CallMouseDown(e);
Assert.IsNotNull(treeView.SelectedNode, "Sanity check: The mouse down call should not deselect the tree node.");
Assert.AreEqual(1, treeViewEventArgs.Count, "AfterSelect event should be fired once.");
}
[Test]
public void MouseDownWithNoNodeSelected()
{
treeView.SelectedNode = null;
// Make sure the mouse click will not select any
// tree node (x=0, y=99 - Height of control=100)
treeViewEventArgs.Clear();
MouseEventArgs e = new MouseEventArgs(MouseButtons.Left, 1, 0, 99, 0);
treeView.CallMouseDown(e);
Assert.IsNull(treeView.SelectedNode, "Sanity check: The mouse down call should not select a tree node");
Assert.AreEqual(1, treeViewEventArgs.Count, "AfterSelect event should be fired once.");
TreeViewEventArgs treeViewEventArg = treeViewEventArgs[0];
Assert.IsNull(treeViewEventArg.Node);
Assert.AreEqual(TreeViewAction.ByMouse, treeViewEventArg.Action);
}
void XmlTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
treeViewEventArgs.Add(e);
}
}
}

119
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/OwnerStatusTestFixture.cs

@ -0,0 +1,119 @@ @@ -0,0 +1,119 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests the XmlTreeViewContainerControl.OwnerState property.
/// </summary>
[TestFixture]
public class OwnerStatusTestFixture
{
XmlTreeViewContainerControl treeViewContainer;
XmlTreeViewControl treeView;
XmlDocument doc;
XmlElementTreeNode htmlTreeNode;
XmlElementTreeNode bodyTreeNode;
XmlElementTreeNode paraTreeNode;
XmlTextTreeNode textTreeNode;
[SetUp]
public void Init()
{
treeViewContainer = new XmlTreeViewContainerControl();
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
XmlSchemaCompletionData xhtmlSchema = new XmlSchemaCompletionData(reader);
XmlSchemaCompletionDataCollection schemas = new XmlSchemaCompletionDataCollection();
XmlCompletionDataProvider provider = new XmlCompletionDataProvider(schemas, xhtmlSchema, String.Empty);
treeViewContainer.LoadXml("<html><body class='a'><p>Text</p></body></html>", provider);
doc = treeViewContainer.Document;
treeView = treeViewContainer.TreeView;
htmlTreeNode = (XmlElementTreeNode)treeView.Nodes[0];
htmlTreeNode.Expanding();
bodyTreeNode = (XmlElementTreeNode)htmlTreeNode.Nodes[0];
bodyTreeNode.Expanding();
paraTreeNode = (XmlElementTreeNode)bodyTreeNode.Nodes[0];
paraTreeNode.Expanding();
textTreeNode = (XmlTextTreeNode)paraTreeNode.Nodes[0];
}
[TearDown]
public void TearDown()
{
if (treeViewContainer != null) {
treeViewContainer.Dispose();
}
}
[Test]
public void NothingSelected()
{
treeView.SelectedNode = null;
Assert.AreEqual(XmlTreeViewContainerControl.XmlTreeViewContainerControlState.Nothing,
treeViewContainer.InternalState,
"OwnerState should be Nothing.");
}
[Test]
public void RootElementSelected()
{
treeView.SelectedNode = htmlTreeNode;
Assert.AreEqual(XmlTreeViewContainerControl.XmlTreeViewContainerControlState.RootElementSelected | XmlTreeViewContainerControl.XmlTreeViewContainerControlState.ElementSelected,
treeViewContainer.InternalState,
"OwnerState should be RootElementSelected and ElementSelected.");
}
[Test]
public void BodyElementSelected()
{
treeView.SelectedNode = bodyTreeNode;
Assert.AreEqual(XmlTreeViewContainerControl.XmlTreeViewContainerControlState.ElementSelected,
treeViewContainer.InternalState,
"OwnerState should be ElementSelected.");
}
[Test]
public void ClassAttributeSelected()
{
treeView.SelectedNode = bodyTreeNode;
treeViewContainer.ShowAttributes(treeView.SelectedElement.Attributes);
Assert.IsNotNull(treeViewContainer.AttributesGrid.SelectedGridItem,
"Sanity check - should have a grid item selected.");
Assert.AreEqual(XmlTreeViewContainerControl.XmlTreeViewContainerControlState.ElementSelected | XmlTreeViewContainerControl.XmlTreeViewContainerControlState.AttributeSelected,
treeViewContainer.InternalState,
"OwnerState should be ElementSelected and AttributeSelected.");
}
[Test]
public void TextNodeSelected()
{
treeView.SelectedNode = textTreeNode;
Assert.AreEqual(XmlTreeViewContainerControl.XmlTreeViewContainerControlState.TextNodeSelected,
treeViewContainer.InternalState,
"OwnerState should be TextNodeSelected.");
}
}
}

97
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementTestFixture.cs

@ -0,0 +1,97 @@ @@ -0,0 +1,97 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.IO;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that an element is removed from the XML document by the
/// XmlTreeEditor.
/// </summary>
[TestFixture]
public class RemoveElementTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement bodyElement;
[SetUp]
public void Init()
{
base.InitFixture();
bodyElement = (XmlElement)editor.Document.SelectSingleNode("/html/body");
mockXmlTreeView.SelectedElement = bodyElement;
editor.RemoveElement();
}
[Test]
public void BodyElementRemoved()
{
Assert.IsNull(editor.Document.SelectSingleNode("/html/body"));
}
[Test]
public void BodyElementRemovedFromTree()
{
Assert.AreEqual(1, mockXmlTreeView.ElementsRemoved.Count);
Assert.AreSame(bodyElement, mockXmlTreeView.ElementsRemoved[0]);
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
[Test]
public void NoElementSelected()
{
mockXmlTreeView.SelectedElement = null;
mockXmlTreeView.IsDirty = false;
editor.RemoveElement();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
[Test]
public void RemoveRootElement()
{
XmlElement htmlElement = editor.Document.DocumentElement;
mockXmlTreeView.SelectedElement = htmlElement;
mockXmlTreeView.IsDirty = false;
editor.RemoveElement();
Assert.IsTrue(mockXmlTreeView.IsDirty);
Assert.IsNull(editor.Document.DocumentElement);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body id='body' title='abc'>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

90
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveElementsFromTreeControlTestFixture.cs

@ -0,0 +1,90 @@ @@ -0,0 +1,90 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Xml;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
namespace XmlEditor.Tests.Tree
{
[TestFixture]
public class RemoveElementsFromTreeControlTestFixture
{
XmlDocument doc;
XmlTreeViewContainerControl treeViewContainerControl;
XmlTreeViewControl treeView;
[SetUp]
public void SetUp()
{
XmlCompletionDataProvider completionDataProvider = new XmlCompletionDataProvider(new XmlSchemaCompletionDataCollection(), null, String.Empty);
treeViewContainerControl = new XmlTreeViewContainerControl();
treeView = treeViewContainerControl.TreeView;
treeViewContainerControl.LoadXml("<root><child></child></root>", completionDataProvider);
doc = treeViewContainerControl.Document;
}
[TearDown]
public void TearDown()
{
if (treeViewContainerControl != null) {
treeViewContainerControl.Dispose();
}
}
[Test]
public void IsDirty()
{
Assert.IsFalse(treeViewContainerControl.IsDirty);
}
[Test]
public void RootTreeNodesBeforeRemove()
{
Assert.AreEqual(1, treeView.Nodes.Count);
}
[Test]
public void RemoveSelectedRootElement()
{
treeView.SelectedNode = treeView.Nodes[0];
treeView.RemoveElement(doc.DocumentElement);
Assert.AreEqual(0, treeView.Nodes.Count);
}
[Test]
public void RemoveRootElementWhenNoTreeNodeSelected()
{
treeView.SelectedNode = null;
treeView.RemoveElement(doc.DocumentElement);
Assert.AreEqual(0, treeView.Nodes.Count);
}
[Test]
public void RemoveChildElement()
{
ExtTreeNode rootNode = (ExtTreeNode)treeView.Nodes[0];
rootNode.Expanding();
treeView.RemoveElement((XmlElement)doc.DocumentElement.FirstChild);
Assert.AreEqual(0, treeView.Nodes[0].Nodes.Count);
}
/// <summary>
/// Removing an element that does not exist in the
/// tree should not make the view dirty. Nothing should
/// happen at all.
/// </summary>
[Test]
public void RemoveUnknownElement()
{
XmlElement element = (XmlElement)doc.CreateElement("NewElement");
treeView.RemoveElement(element);
}
}
}

90
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodeTestFixture.cs

@ -0,0 +1,90 @@ @@ -0,0 +1,90 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.IO;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that a text node is removed from the XML document by the
/// XmlTreeEditor.
/// </summary>
[TestFixture]
public class RemoveTextNodeTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement paragraphElement;
[SetUp]
public void Init()
{
base.InitFixture();
paragraphElement = (XmlElement)editor.Document.SelectSingleNode("/html/body/p");
XmlText textNode = (XmlText)paragraphElement.FirstChild;
mockXmlTreeView.SelectedTextNode = textNode;
editor.RemoveTextNode();
}
[Test]
public void TextNodeRemovedFromDocument()
{
Assert.AreEqual(0, paragraphElement.ChildNodes.Count);
}
/// <summary>
/// Tests that the xml tree editor does not throw
/// an exception if we try to remove a text node when
/// no node is selected.
/// </summary>
[Test]
public void NoTextNodeSelected()
{
mockXmlTreeView.IsDirty = false;
mockXmlTreeView.SelectedTextNode = null;
editor.RemoveTextNode();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
[Test]
public void TextNodeRemovedFromView()
{
Assert.AreEqual(1, mockXmlTreeView.TextNodesRemoved.Count);
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<body>\r\n" +
"\t\t<p>some text here</p>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

105
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveTextNodesFromTreeControlTestFixture.cs

@ -0,0 +1,105 @@ @@ -0,0 +1,105 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows.Forms;
using System.Xml;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
namespace XmlEditor.Tests.Tree
{
[TestFixture]
public class RemoveTextNodesFromTreeControlTestFixture
{
XmlDocument doc;
XmlTreeViewContainerControl treeViewContainerControl;
XmlTreeViewControl treeView;
XmlElementTreeNode topElementTreeNode;
XmlElementTreeNode childElementTreeNode;
XmlTextTreeNode topTextTreeNode;
[SetUp]
public void SetUp()
{
XmlCompletionDataProvider completionDataProvider = new XmlCompletionDataProvider(new XmlSchemaCompletionDataCollection(), null, String.Empty);
treeViewContainerControl = new XmlTreeViewContainerControl();
treeView = treeViewContainerControl.TreeView;
treeViewContainerControl.LoadXml("<root><top>text</top><bottom><child>text</child></bottom></root>", completionDataProvider);
doc = treeViewContainerControl.Document;
ExtTreeNode rootNode = (ExtTreeNode)treeView.Nodes[0];
rootNode.Expanding();
topElementTreeNode = (XmlElementTreeNode)rootNode.Nodes[0];
topElementTreeNode.Expanding();
topTextTreeNode = (XmlTextTreeNode)topElementTreeNode.Nodes[0];
ExtTreeNode bottomNode = (ExtTreeNode)rootNode.Nodes[1];
bottomNode.Expanding();
childElementTreeNode = (XmlElementTreeNode)bottomNode.Nodes[0];
childElementTreeNode.Expanding();
}
[TearDown]
public void TearDown()
{
if (treeViewContainerControl != null) {
treeViewContainerControl.Dispose();
}
}
[Test]
public void RemoveTextNode()
{
treeView.SelectedNode = topTextTreeNode;
treeView.RemoveTextNode(topTextTreeNode.XmlText);
Assert.AreEqual(0, topElementTreeNode.Nodes.Count);
}
/// <summary>
/// Makes sure that nothing happens if we try to remove a text
/// node when there is no currently selected node.
/// </summary>
[Test]
public void RemoveTextNodeWhenNoTreeNodeSelected()
{
treeView.SelectedNode = null;
XmlText newTextNode = doc.CreateTextNode(String.Empty);
treeView.RemoveTextNode(newTextNode);
Assert.AreEqual(1, topElementTreeNode.Nodes.Count);
}
/// <summary>
/// Makes sure that nothing happens if we try to remove a text
/// node that is not in the tree.
/// </summary>
[Test]
public void RemoveUnknownTextNode()
{
treeView.SelectedNode = topElementTreeNode;
XmlText newTextNode = doc.CreateTextNode(String.Empty);
treeView.RemoveTextNode(newTextNode);
Assert.AreEqual(1, topElementTreeNode.Nodes.Count);
}
[Test]
public void RemoveKnownTextNodeWhenNothingSelected()
{
treeView.SelectedNode = null;
treeView.RemoveTextNode(topTextTreeNode.XmlText);
Assert.AreEqual(0, topElementTreeNode.Nodes.Count);
}
}
}

25
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RootNodeAddedToTreeControlTestFixture.cs

@ -22,22 +22,29 @@ namespace XmlEditor.Tests.Tree @@ -22,22 +22,29 @@ namespace XmlEditor.Tests.Tree
XmlDocument doc;
XmlElement initialElementSelected;
bool initialIsElementSelected;
XmlNode documentElement;
[TestFixtureSetUp]
public void SetUpFixture()
{
doc = new XmlDocument();
doc.LoadXml("<test/>");
using (XmlTreeViewControl treeView = new XmlTreeViewControl()) {
treeView.DocumentElement = doc.DocumentElement;
using (XmlTreeViewContainerControl treeViewContainer = new XmlTreeViewContainerControl()) {
XmlCompletionDataProvider completionDataProvider = new XmlCompletionDataProvider(new XmlSchemaCompletionDataCollection(), null, String.Empty);
treeViewContainer.LoadXml("<test/>", completionDataProvider);
doc = treeViewContainer.Document;
XmlTreeViewControl treeView = treeViewContainer.TreeView;
treeViewContainer.DocumentElement = doc.DocumentElement;
initialElementSelected = treeView.SelectedElement;
initialIsElementSelected = treeView.IsElementSelected;
// Set the document element again to make sure the existing node
// is removed.
doc.LoadXml("<root/>");
treeView.DocumentElement = null;
treeView.DocumentElement = doc.DocumentElement;
treeViewContainer.DocumentElement = null;
treeViewContainer.DocumentElement = doc.DocumentElement;
documentElement = treeViewContainer.DocumentElement;
rootNode = (XmlElementTreeNode)treeView.Nodes[0];
nodeCount = treeView.Nodes.Count;
@ -85,5 +92,11 @@ namespace XmlEditor.Tests.Tree @@ -85,5 +92,11 @@ namespace XmlEditor.Tests.Tree
{
Assert.IsTrue(Object.ReferenceEquals(rootNode.XmlElement, doc.DocumentElement));
}
[Test]
public void DocumentElement()
{
Assert.AreSame(doc.DocumentElement, documentElement);
}
}
}

11
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/TextNodeTextChangedTestFixture.cs

@ -74,6 +74,17 @@ namespace XmlEditor.Tests.Tree @@ -74,6 +74,17 @@ namespace XmlEditor.Tests.Tree
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Check that the Xml tree editor calls the XmlTreeView's
/// UpdateTextNode method.
/// </summary>
[Test]
public void TreeNodeTextUpdated()
{
Assert.AreEqual(1, mockXmlTreeView.TextNodesUpdated.Count);
Assert.AreEqual(textNode, mockXmlTreeView.TextNodesUpdated[0]);
}
protected override string GetXml()
{
return "<root>text</root>";

27
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlAttributeTypeDescriptorTestFixture.cs

@ -46,5 +46,32 @@ namespace XmlEditor.Tests.Tree @@ -46,5 +46,32 @@ namespace XmlEditor.Tests.Tree
{
Assert.IsTrue(Object.ReferenceEquals(typeDescriptor, typeDescriptor.GetPropertyOwner(null)));
}
[Test]
public void ComponentName()
{
Assert.IsNull(typeDescriptor.GetComponentName());
}
[Test]
public void DefaultEvent()
{
Assert.IsNull(typeDescriptor.GetDefaultEvent());
}
[Test]
public void Events()
{
Assert.IsNull(typeDescriptor.GetEvents());
Assert.IsNull(typeDescriptor.GetEvents(new Attribute[0]));
}
[Test]
public void NullAttributesCollection()
{
XmlAttributeTypeDescriptor typeDescriptor = new XmlAttributeTypeDescriptor(null);
PropertyDescriptorCollection properties = typeDescriptor.GetProperties();
Assert.AreEqual(0, properties.Count);
}
}
}

8
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTextTreeNodeTextTests.cs

@ -55,5 +55,13 @@ namespace XmlEditor.Tests.Tree @@ -55,5 +55,13 @@ namespace XmlEditor.Tests.Tree
XmlTextTreeNode node = new XmlTextTreeNode(text);
Assert.AreEqual("Test...", node.Text);
}
[Test]
public void EmptyLines()
{
XmlText text = doc.CreateTextNode("\r\n\r\n\r\n");
XmlTextTreeNode node = new XmlTextTreeNode(text);
Assert.AreEqual(String.Empty, node.Text);
}
}
}

334
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewContainerTestFixture.cs

@ -0,0 +1,334 @@ @@ -0,0 +1,334 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows.Forms;
using System.Xml;
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Various tests for the XmlTreeViewContainerControl. These
/// tests do not really fit into any other test fixture.
/// </summary>
[TestFixture]
public class XmlTreeViewContainerTestFixture
{
XmlDocument doc;
XmlTreeViewControl treeView;
DerivedXmlTreeViewContainerControl treeViewContainer;
RichTextBox textBox;
XmlCompletionDataProvider provider;
PropertyGrid attributesGrid;
SplitContainer splitContainer;
RichTextBox errorMessageTextBox;
bool dirtyChanged;
XmlElementTreeNode htmlTreeNode;
XmlTextTreeNode textTreeNode;
[SetUp]
public void Init()
{
treeViewContainer = new DerivedXmlTreeViewContainerControl();
treeViewContainer.DirtyChanged += TreeViewContainerDirtyChanged;
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
XmlSchemaCompletionData xhtmlSchema = new XmlSchemaCompletionData(reader);
XmlSchemaCompletionDataCollection schemas = new XmlSchemaCompletionDataCollection();
provider = new XmlCompletionDataProvider(schemas, xhtmlSchema, String.Empty);
treeViewContainer.LoadXml("<html id='a'>text<body></body></html>", provider);
doc = treeViewContainer.Document;
treeView = treeViewContainer.TreeView;
htmlTreeNode = (XmlElementTreeNode)treeView.Nodes[0];
htmlTreeNode.Expanding();
textTreeNode = (XmlTextTreeNode)htmlTreeNode.Nodes[0];
splitContainer = (SplitContainer)treeViewContainer.Controls["splitContainer"];
textBox = (RichTextBox)splitContainer.Panel2.Controls["textBox"];
errorMessageTextBox = (RichTextBox)splitContainer.Panel2.Controls["errorMessageTextBox"];
attributesGrid = (PropertyGrid)splitContainer.Panel2.Controls["attributesGrid"];
}
[TearDown]
public void TearDown()
{
if (treeViewContainer != null) {
treeViewContainer.DirtyChanged -= TreeViewContainerDirtyChanged;
treeViewContainer.Dispose();
}
}
[Test]
public void ErrorMessageTextBoxVisibleAtStart()
{
Assert.IsFalse(treeViewContainer.IsErrorMessageTextBoxVisible);
}
[Test]
public void TextBoxIsNotTabStopAtStart()
{
Assert.IsFalse(textBox.TabStop);
}
[Test]
public void TextBoxIsEmptyAtStart()
{
Assert.AreEqual(String.Empty, textBox.Text);
}
[Test]
public void AttributesGridIsTabStopAtStart()
{
Assert.IsTrue(attributesGrid.TabStop);
}
/// <summary>
/// Check that the XmlTreeViewContainer brought the
/// AttributesGrid to the front.
/// </summary>
[Test]
public void AttributesGridOnTop()
{
Assert.AreEqual(0, splitContainer.Panel2.Controls.IndexOf(attributesGrid),
"AttributesGrid is not on top");
}
/// <summary>
/// Checks that the text box shows the specified text and
/// is visible in the control.
/// </summary>
[Test]
public void ShowTextContent()
{
string text = "test";
treeViewContainer.ShowTextContent(text);
Assert.AreEqual(text, treeViewContainer.TextContent);
Assert.AreEqual(text, textBox.Text);
Assert.IsTrue(textBox.TabStop);
Assert.AreEqual(0, splitContainer.Panel2.Controls.IndexOf(textBox));
}
[Test]
public void TextBoxClearedAfterLoadXml()
{
treeViewContainer.ShowTextContent("test");
treeViewContainer.LoadXml("<html/>", provider);
Assert.AreEqual(String.Empty, textBox.Text);
AttributesGridOnTop();
}
[Test]
public void RootNodeExpanded()
{
Assert.IsTrue(treeView.Nodes[0].IsExpanded);
}
[Test]
public void AttributesClearedAfterLoadXml()
{
// Make sure some attributes are showing.
treeViewContainer.ShowAttributes(doc.DocumentElement.Attributes);
Assert.IsTrue(doc.DocumentElement.HasAttributes, "Sanity check that the root element has some attributes");
Assert.IsNotNull(attributesGrid.SelectedObject);
// Loading new xml should clear the attributes grid.
treeViewContainer.LoadXml("<html/>", provider);
Assert.IsNull(attributesGrid.SelectedObject,
"Should be no SelectedObject in the attributes grid after loading new xml.");
}
[Test]
public void ErrorMessageTextBoxNotTabStop()
{
Assert.IsFalse(errorMessageTextBox.TabStop);
}
[Test]
public void ErrorMessageTextBoxNotOnTop()
{
Assert.AreNotEqual(0, splitContainer.Panel2.Controls.IndexOf(errorMessageTextBox));
}
[Test]
public void ShowXmlNotWellFormed()
{
XmlException ex = new XmlException("Message");
treeViewContainer.ShowXmlIsNotWellFormedMessage(ex);
Assert.AreEqual(0, treeView.Nodes.Count, "TreeView should be cleared.");
Assert.AreEqual(ex.Message, treeViewContainer.ErrorMessage);
Assert.AreEqual(0, splitContainer.Panel2.Controls.IndexOf(errorMessageTextBox), "ErrorMessageTextBox should be on top");
Assert.AreEqual(ex.Message, errorMessageTextBox.Text);
Assert.IsTrue(errorMessageTextBox.TabStop);
Assert.IsFalse(attributesGrid.TabStop);
Assert.IsFalse(textBox.TabStop);
}
/// <summary>
/// Checks that the text box is not a tab stop after showing
/// an error.
/// </summary>
[Test]
public void ShowTextContentBeforeShowingError()
{
treeViewContainer.ShowTextContent("Test");
ShowXmlNotWellFormed();
}
[Test]
public void DirtyChanged()
{
treeViewContainer.IsDirty = true;
Assert.IsTrue(dirtyChanged);
}
[Test]
public void TextChanged()
{
// Select the text node.
treeView.SelectedNode = textTreeNode;
string newText = "changed text";
textBox.Text = newText;
// Make sure the dirty flag is changed by changing
// the text.
treeViewContainer.IsDirty = false;
dirtyChanged = false;
treeViewContainer.CallTextBoxTextChanged();
Assert.AreEqual(newText, textTreeNode.XmlText.Value);
Assert.AreEqual(newText, textTreeNode.Text, "Tree node text should be updated with new XmlText's value");
Assert.IsTrue(treeViewContainer.IsDirty);
Assert.IsTrue(dirtyChanged);
}
/// <summary>
/// Tests that when the XmlTreeView's UpdateTextNode method
/// is called we do not get a null exception if the
/// text node cannot be found in the tree.
/// </summary>
[Test]
public void UpdateUnknownTextNodeText()
{
// Select the text node.
treeView.SelectedNode = textTreeNode;
XmlText textNode = doc.CreateTextNode(String.Empty);
treeView.UpdateTextNode(textNode);
}
/// <summary>
/// Updates the text node when no text node is selected in the
/// tree.
/// </summary>
[Test]
public void UpdateTextNodeText()
{
treeView.SelectedNode = null;
textTreeNode.XmlText.Value = "New value";
treeView.UpdateTextNode(textTreeNode.XmlText);
Assert.AreEqual("New value", textTreeNode.Text);
}
/// <summary>
/// Check that the DirtyChanged event is not fired
/// </summary>
[Test]
public void TextChangedDirtyUnchanged()
{
// Select the text node.
treeView.SelectedNode = textTreeNode;
textBox.Text = "changed text";
// Make sure the dirty flag is changed by changing
// the text.
treeViewContainer.IsDirty = true;
dirtyChanged = false;
treeViewContainer.CallTextBoxTextChanged();
Assert.AreEqual("changed text", textTreeNode.XmlText.Value);
Assert.IsTrue(treeViewContainer.IsDirty);
Assert.IsFalse(dirtyChanged);
}
[Test]
public void AttributeValueChanged()
{
// Select the html node.
treeView.SelectedNode = htmlTreeNode;
treeViewContainer.ShowAttributes(doc.DocumentElement.Attributes);
Assert.IsNotNull(attributesGrid.SelectedGridItem);
treeViewContainer.IsDirty = false;
dirtyChanged = false;
treeViewContainer.CallAttributesGridPropertyValueChanged();
Assert.IsTrue(treeViewContainer.IsDirty);
Assert.IsTrue(dirtyChanged);
}
[Test]
public void AttributeValueChangedDirtyUnchanged()
{
// Select the html node.
treeView.SelectedNode = htmlTreeNode;
treeViewContainer.ShowAttributes(doc.DocumentElement.Attributes);
Assert.IsNotNull(attributesGrid.SelectedGridItem);
treeViewContainer.IsDirty = true;
dirtyChanged = false;
treeViewContainer.CallAttributesGridPropertyValueChanged();
Assert.IsTrue(treeViewContainer.IsDirty);
Assert.IsFalse(dirtyChanged);
}
[Test]
public void TextNodeSelected()
{
treeView.SelectedNode = textTreeNode;
treeViewContainer.ShowTextContent(String.Empty);
treeViewContainer.CallXmlElementTreeViewAfterSelect();
Assert.AreEqual("text", textBox.Text);
}
[Test]
public void HtmlElementNodeSelected()
{
treeView.SelectedNode = htmlTreeNode;
treeViewContainer.CallXmlElementTreeViewAfterSelect();
Assert.IsNotNull(attributesGrid.SelectedGridItem);
Assert.AreEqual("id", attributesGrid.SelectedGridItem.Label);
}
void TreeViewContainerDirtyChanged(object source, EventArgs e)
{
dirtyChanged = true;
}
}
}

29
src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeView.cs

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows.Forms;
using ICSharpCode.XmlEditor;
namespace XmlEditor.Tests.Utils
{
/// <summary>
/// Test utility class that has the XmlTreeViewControl as a base class
/// and gives access to protected methods.
/// </summary>
public class DerivedXmlTreeViewControl : XmlTreeViewControl
{
public DerivedXmlTreeViewControl()
{
}
public void CallMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
}
}
}

125
src/AddIns/DisplayBindings/XmlEditor/Test/Utils/DerivedXmlTreeViewContainerControl.cs

@ -0,0 +1,125 @@ @@ -0,0 +1,125 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ICSharpCode.XmlEditor;
namespace XmlEditor.Tests.Utils
{
/// <summary>
/// Derived version of the XmlTreeViewContainerControl which
/// allows us to override the code that shows the various dialogs
/// that ask for user input so we can fake the data allowing us
/// to test the class.
/// </summary>
public class DerivedXmlTreeViewContainerControl : XmlTreeViewContainerControl
{
List<string> addElementDialogElementNamesReturned = new List<string>();
DialogResult addElementDialogResult = DialogResult.OK;
List<string> addAttributeDialogAttributeNamesReturned = new List<string>();
DialogResult addAttributeDialogResult = DialogResult.OK;
/// <summary>
/// This is the list of element names that will be returned from
/// the mock AddElementDialog.
/// </summary>
public List<string> AddElementDialogElementNamesReturned {
get {
return addElementDialogElementNamesReturned;
}
}
/// <summary>
/// Gets or sets the dialog result for the AddElementDialog.
/// </summary>
public DialogResult AddElementDialogResult {
get {
return addElementDialogResult;
}
set {
addElementDialogResult = value;
}
}
/// <summary>
/// Gets the list of attribute names that will be returned
/// from the mock AddAttributeDialog.
/// </summary>
public List<string> AddAttributeDialogAttributeNamesReturned {
get {
return addAttributeDialogAttributeNamesReturned;
}
}
/// <summary>
/// Gets or sets the dialog result for the AddAttributeDialog.
/// </summary>
public DialogResult AddAttributeDialogResult {
get {
return addAttributeDialogResult;
}
set {
addAttributeDialogResult = value;
}
}
/// <summary>
/// Allows us to call the XmlTreeViewContainerControl's
/// TextBoxChanged method to fake the user typing in text
/// into the text box.
/// </summary>
public void CallTextBoxTextChanged()
{
base.TextBoxTextChanged(this, new EventArgs());
}
/// <summary>
/// Allows us to call the XmlTreeViewContainerControl's
/// AttributesGridPropertyValueChanged to fake the user
/// changing the property value.
/// </summary>
public void CallAttributesGridPropertyValueChanged()
{
base.AttributesGridPropertyValueChanged(this, new PropertyValueChangedEventArgs(null, null));
}
/// <summary>
/// Allows us to call the XmlTreeViewContainerControl's
/// XmlElementTreeViewAfterSelect to fake the user selecting
/// a tree node.
/// </summary>
public void CallXmlElementTreeViewAfterSelect()
{
base.XmlElementTreeViewAfterSelect(this, new TreeViewEventArgs(null, TreeViewAction.ByMouse));
}
/// <summary>
/// Returns a new MockAddElementDialog for testing.
/// </summary>
protected override IAddElementDialog CreateAddElementDialog(string[] elementNames)
{
MockAddElementDialog dialog = new MockAddElementDialog();
dialog.SetElementNamesToReturn(addElementDialogElementNamesReturned.ToArray());
dialog.SetDialogResult(addElementDialogResult);
return dialog;
}
/// <summary>
/// Returns a new MockAddAttributeDialog for testing.
/// </summary>
protected override IAddAttributeDialog CreateAddAttributeDialog(string[] attributeNames)
{
MockAddAttributeDialog dialog = new MockAddAttributeDialog();
dialog.SetAttributeNamesToReturn(addAttributeDialogAttributeNamesReturned.ToArray());
dialog.SetDialogResult(addAttributeDialogResult);
return dialog;
}
}
}

65
src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockAddAttributeDialog.cs

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ICSharpCode.XmlEditor;
namespace XmlEditor.Tests.Utils
{
/// <summary>
/// Mocks the AddAttributeDialog so we can test the
/// XmlTreeViewContainerControl class when it displays
/// the AddAttributeDialog.
/// </summary>
public class MockAddAttributeDialog : IAddAttributeDialog
{
DialogResult dialogResult = DialogResult.OK;
List<string> attributeNames = new List<string>();
/// <summary>
/// Specifies the attribute names to return from the
/// IAddAttributeDialog.AttributeNames property.
/// </summary>
public void SetAttributeNamesToReturn(string[] names)
{
attributeNames.Clear();
foreach (string name in names) {
attributeNames.Add(name);
}
}
/// <summary>
/// Specifies the dialog result to return from the
/// IAddAttributeDialog.ShowDialog method.
/// </summary>
public void SetDialogResult(DialogResult result)
{
dialogResult = result;
}
#region IAddAttributeDialog implementation
public string[] AttributeNames {
get {
return attributeNames.ToArray();
}
}
public DialogResult ShowDialog()
{
return dialogResult;
}
public void Dispose()
{
}
#endregion
}
}

65
src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockAddElementDialog.cs

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ICSharpCode.XmlEditor;
namespace XmlEditor.Tests.Utils
{
/// <summary>
/// Mocks the AddElementDialog so we can test the
/// XmlTreeViewContainerControl class when it displays
/// the AddElementDialog.
/// </summary>
public class MockAddElementDialog : IAddElementDialog
{
DialogResult dialogResult = DialogResult.OK;
List<string> elementNames = new List<string>();
/// <summary>
/// Specifies the element names to return from the
/// IAddElementDialog.ElementNames property.
/// </summary>
public void SetElementNamesToReturn(string[] names)
{
elementNames.Clear();
foreach (string name in names) {
elementNames.Add(name);
}
}
/// <summary>
/// Specifies the dialog result to return from the
/// IAddElementDialog.ShowDialog method.
/// </summary>
public void SetDialogResult(DialogResult result)
{
dialogResult = result;
}
#region IAddElementDialog implementation
public string[] ElementNames {
get {
return elementNames.ToArray();
}
}
public DialogResult ShowDialog()
{
return dialogResult;
}
public void Dispose()
{
}
#endregion
}
}

96
src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockXmlTreeView.cs

@ -32,6 +32,12 @@ namespace XmlEditor.Tests.Utils @@ -32,6 +32,12 @@ namespace XmlEditor.Tests.Utils
List<XmlElement> childElementsAdded = new List<XmlElement>();
List<XmlElement> elementsInsertedBefore = new List<XmlElement>();
List<XmlElement> elementsInsertedAfter = new List<XmlElement>();
List<XmlElement> elementsRemoved = new List<XmlElement>();
List<XmlText> childTextNodesAdded = new List<XmlText>();
List<XmlText> textNodesInsertedBefore = new List<XmlText>();
List<XmlText> textNodesInsertedAfter = new List<XmlText>();
List<XmlText> textNodesRemoved = new List<XmlText>();
List<XmlText> textNodesUpdated = new List<XmlText>();
public MockXmlTreeView()
{
@ -135,6 +141,36 @@ namespace XmlEditor.Tests.Utils @@ -135,6 +141,36 @@ namespace XmlEditor.Tests.Utils
elementsInsertedAfter.Add(element);
}
public void RemoveElement(XmlElement element)
{
elementsRemoved.Add(element);
}
public void AppendChildTextNode(XmlText textNode)
{
childTextNodesAdded.Add(textNode);
}
public void InsertTextNodeBefore(XmlText textNode)
{
textNodesInsertedBefore.Add(textNode);
}
public void InsertTextNodeAfter(XmlText textNode)
{
textNodesInsertedAfter.Add(textNode);
}
public void RemoveTextNode(XmlText textNode)
{
textNodesRemoved.Add(textNode);
}
public void UpdateTextNode(XmlText textNode)
{
textNodesUpdated.Add(textNode);
}
public string TextContent {
get {
return textContentDisplayed;
@ -261,5 +297,65 @@ namespace XmlEditor.Tests.Utils @@ -261,5 +297,65 @@ namespace XmlEditor.Tests.Utils
return elementsInsertedAfter;
}
}
/// <summary>
/// Returns the elements removed via the RemoveElement
/// method.
/// </summary>
public List<XmlElement> ElementsRemoved {
get {
return elementsRemoved;
}
}
/// <summary>
/// Returns the text nodes added via the AppendChildTextNode
/// method.
/// </summary>
public List<XmlText> ChildTextNodesAdded {
get {
return childTextNodesAdded;
}
}
/// <summary>
/// Returns the text nodes that were inserted via the
/// InsertTextNodeBefore method.
/// </summary>
public List<XmlText> TextNodesInsertedBefore {
get {
return textNodesInsertedBefore;
}
}
/// <summary>
/// Returns the text nodes that were inserted via the
/// InsertTextNodeAfter method.
/// </summary>
public List<XmlText> TextNodesInsertedAfter {
get {
return textNodesInsertedAfter;
}
}
/// <summary>
/// Returns the text nodes that were removed via the
/// RemoveTextNode method.
/// </summary>
public List<XmlText> TextNodesRemoved {
get {
return textNodesRemoved;
}
}
/// <summary>
/// Returns the text nodes that were updated via the
/// UpdateTextNode method.
/// </summary>
public List<XmlText> TextNodesUpdated {
get {
return textNodesUpdated;
}
}
}
}

16
src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj

@ -70,6 +70,21 @@ @@ -70,6 +70,21 @@
<Compile Include="Paths\TwoElementPathTestFixture.cs" />
<Compile Include="Schema\NestedChoiceTestFixture.cs" />
<Compile Include="Schema\ChildElementAttributesTestFixture.cs" />
<Compile Include="Tree\AddChildTextNodeTestFixture.cs" />
<Compile Include="Tree\InsertTextNodeAfterTestFixture.cs" />
<Compile Include="Tree\InsertTextNodeBeforeTestFixture.cs" />
<Compile Include="Tree\MenuCommandsTestFixture.cs" />
<Compile Include="Tree\MouseDownTestFixture.cs" />
<Compile Include="Tree\OwnerStatusTestFixture.cs" />
<Compile Include="Tree\RemoveElementsFromTreeControlTestFixture.cs" />
<Compile Include="Tree\RemoveElementTestFixture.cs" />
<Compile Include="Tree\RemoveTextNodesFromTreeControlTestFixture.cs" />
<Compile Include="Tree\RemoveTextNodeTestFixture.cs" />
<Compile Include="Tree\XmlTreeViewContainerTestFixture.cs" />
<Compile Include="Utils\DerivedXmlTreeView.cs" />
<Compile Include="Utils\DerivedXmlTreeViewContainerControl.cs" />
<Compile Include="Utils\MockAddAttributeDialog.cs" />
<Compile Include="Utils\MockAddElementDialog.cs" />
<Compile Include="Utils\ResourceManager.cs" />
<Compile Include="Schema\XhtmlStrictSchemaTestFixture.cs" />
<Compile Include="Schema\XsdSchemaTestFixture.cs" />
@ -143,6 +158,7 @@ @@ -143,6 +158,7 @@
<Compile Include="Tree\InsertElementBeforeTestFixture.cs" />
<Compile Include="Tree\AddElementsToTreeControlTestFixture.cs" />
<Compile Include="Tree\InsertElementAfterTestFixture.cs" />
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Schema\" />

13
src/AddIns/DisplayBindings/XmlEditor/Test/app.config

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
<configuration>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA" />
</TestRunner>
</NUnit>
</configuration>

6
src/AddIns/DisplayBindings/XmlEditor/XmlEditor.sln

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 9.00
# SharpDevelop 2.0.0.1590

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
# SharpDevelop 2.1.0.2075
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlEditor", "Project\XmlEditor.csproj", "{6B717BD1-CD5E-498C-A42E-9E6A4584DC48}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlEditor.Tests", "Test\XmlEditor.Tests.csproj", "{FC0FE702-A87D-4D70-A9B6-1ECCD611125F}"

Loading…
Cancel
Save