// // // // // $Revision$ // 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 { /// /// Tests the various menu commands that can be used on the /// Xml Tree (e.g. InsertElementBeforeCommand). /// [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("", 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(); } } /// /// Nothing should happen if the owner is the XmlTreeViewContainerControl. /// [Test] public void InsertElementBeforeWithUnknownOwner() { InsertElementBeforeCommand command = new InsertElementBeforeCommand(); command.Owner = this; command.Run(); } /// /// Nothing should happen if the owner is null. /// [Test] public void InsertElementBeforeWithNullOwner() { InsertElementBeforeCommand command = new InsertElementBeforeCommand(); command.Run(); } /// /// Here we produce dummy data from our mock AddElementDialog /// and check that the InsertElementBeforeCommand inserts /// the correct element /// [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); } /// /// Nothing should happen if the owner is null. /// [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]); } /// /// Nothing should happen if the owner is null. /// [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

element to be appended."); } ///

/// Tests the XmlTreeViewContainer.SelectNewElements methods /// returns an empty string array when the AddElementDialog /// is cancelled. /// [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); } /// /// Nothing should happen if the owner is null. /// [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")); } /// /// Nothing should happen if the owner is null. /// [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")); } /// /// Tests the XmlTreeViewContainer.SelectNewAttributes methods /// returns an empty string array when the AddAttributeDialog /// is cancelled. /// [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); } /// /// Nothing should happen if the owner is null. /// [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); } /// /// 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. /// [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); } /// /// Nothing should happen if the owner is null. /// [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); } /// /// Makes sure that nothing happens if we try to add a text /// node when there is no currently selected node. /// [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); } /// /// Expect nothing to happen since the ICommand.Owner is not /// set. /// [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); } } }