Fixed WinFormsTest System.Drawing namespace reference
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / TreeNodeTest.cs
index d269db9c72089af9adfe746bcdc9ac8f33508075..731e2cbc5baeb0b3df5ac746b97dbc2909f62e77 100644 (file)
@@ -2,12 +2,14 @@ using System;
 using NUnit.Framework;
 using System.Windows.Forms;
 using SystemDrawingNamespace = System.Drawing;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
 
 namespace MonoTests.System.Windows.Forms {
 
        
        [TestFixture]
-       public class TreeNodeTest {
+       public class TreeNodeTest : TestHelper {
 
                [Test]
                public void EmptyCtorTest ()
@@ -100,13 +102,40 @@ namespace MonoTests.System.Windows.Forms {
                        Assert.IsNull (tn_1.PrevNode, "#7");
                }
 
-               [Test, ExpectedException (typeof (Exception))]
+               [Test]
+               public void ExpandCollapseLeafTest ()
+               {
+                       // Leaf nodes should keep its expanded state
+                       TreeNode tn_1 = new TreeNode ();
+                       Assert.IsFalse (tn_1.IsExpanded, "#1");
+
+                       tn_1.Expand ();
+                       Assert.IsTrue (tn_1.IsExpanded, "#2");
+               }
+
+               [Test]
                public void FullPathException ()
                {
-                       string s = new TreeNode ("").FullPath;
-                       // Prevent CS0219, will never write anything
-                       // due to previous statement throwing Exception
-                       Console.WriteLine(s);
+                       try {
+                               string s = new TreeNode ("").FullPath;
+                               Assert.Fail ("#1");
+                               // Prevent CS0219, will never write anything
+                               // due to previous statement throwing an
+                               // exception
+                               Console.WriteLine (s);
+#if NET_2_0
+                       } catch (InvalidOperationException ex) {
+                               Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
+                               Assert.IsNotNull (ex.Message, "#3");
+                               Assert.IsNull (ex.InnerException, "#4");
+                       }
+#else
+                       } catch (Exception ex) {
+                               Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
+                               Assert.IsNotNull (ex.Message, "#3");
+                               Assert.IsNull (ex.InnerException, "#4");
+                       }
+#endif
                }
 
                [Test]
@@ -149,6 +178,74 @@ namespace MonoTests.System.Windows.Forms {
                        Assert.AreEqual (SystemDrawingNamespace.Color.Beige, clone.ForeColor, "#13");
                }
 
+               [Test] // bug 661753
+               public void TestTreeNodeClone ()
+               {
+                       TreeNode orig = new TreeNode ();
+
+                       orig.Nodes.Add ("Node1");
+                       orig.Nodes.Add ("Node2");
+                       orig.Nodes.Add ("Node3");
+
+                       orig.Checked = true;
+                       orig.ImageIndex = 4;
+                       orig.Name = "MyName";
+                       orig.SelectedImageIndex = 3;
+                       orig.StateImageIndex = 8;
+                       orig.Tag = new object ();
+                       orig.Text = "MyText";
+                       orig.ToolTipText = "MyToolTipText";
+
+                       orig.ContextMenu = new ContextMenu ();
+                       orig.ContextMenu.Name = "MyContextMenu";
+                       orig.ContextMenu.MenuItems.Add (new MenuItem ("MenuItem1"));
+                       orig.ContextMenu.MenuItems.Add (new MenuItem ("MenuItem2"));
+
+                       orig.ContextMenuStrip = new ContextMenuStrip ();
+                       orig.ContextMenuStrip.Name = "MyContextMenuStrip";
+                       orig.ContextMenuStrip.Items.Add ("ToolStripText");
+                       orig.ContextMenuStrip.Items.Add (new SystemDrawingNamespace.Bitmap (1, 1));
+
+                       TreeNode clone = orig.Clone () as TreeNode;
+
+                       Assert.AreEqual (orig.Nodes[0].Name, clone.Nodes[0].Name, "#01");
+                       Assert.AreEqual (orig.Nodes[1].Name, clone.Nodes[1].Name, "#02");
+                       Assert.AreEqual (orig.Nodes[2].Name, clone.Nodes[2].Name, "#03");
+                       Assert.AreNotEqual (orig.Nodes[0], clone.Nodes[0], "#04");
+                       Assert.AreNotEqual (orig.Nodes[1], clone.Nodes[1], "#05");
+                       Assert.AreNotEqual (orig.Nodes[2], clone.Nodes[2], "#06");
+
+                       Assert.AreEqual (orig.Checked, clone.Checked, "#07");
+                       Assert.AreEqual (orig.ImageIndex, clone.ImageIndex, "#08");
+                       Assert.AreEqual (orig.Name, clone.Name, "#09");
+                       Assert.AreEqual (orig.SelectedImageIndex, clone.SelectedImageIndex, "#10");
+                       Assert.AreEqual (orig.StateImageIndex, clone.StateImageIndex, "#11");
+                       Assert.AreEqual (orig.Tag, clone.Tag, "#12");
+                       Assert.AreEqual (orig.Text, clone.Text, "#13");
+                       Assert.AreEqual (orig.ToolTipText, clone.ToolTipText, "#14");
+                       Assert.AreEqual (orig.ContextMenu, clone.ContextMenu, "#15");
+                       Assert.AreEqual (orig.ContextMenuStrip, clone.ContextMenuStrip, "#16");
+               }
+
+               [Test] // bug 661753
+               public void TestTreeNodeClone2 ()
+               {
+                       // Cannot test ImageIndex and ImageKey properties at the same time,
+                       // as one excludes the other. So this method is for Keys only.
+
+                       TreeNode orig = new TreeNode ();
+
+                       orig.ImageKey = "MyImageKey";
+                       orig.SelectedImageKey = "MySelectedImageKey";
+                       orig.StateImageKey = "MyStateImageKey";
+
+                       TreeNode clone = orig.Clone () as TreeNode;
+
+                       Assert.AreEqual (orig.ImageKey, clone.ImageKey, "#01");
+                       Assert.AreEqual (orig.SelectedImageKey, clone.SelectedImageKey, "#02");
+                       Assert.AreEqual (orig.StateImageKey, clone.StateImageKey, "#03");
+               }
+
                [Test]
                public void SingleNodeIndexTest ()
                {
@@ -158,5 +255,132 @@ namespace MonoTests.System.Windows.Forms {
                        tv.Nodes.Add (tn_1);
                        Assert.AreEqual (0, tn_1.Index, "#2");
                }
+
+               [Test]
+               public void EndEditTest ()
+               {
+                       TreeNode node1 = new TreeNode ("A");
+                       TreeNode node2 = new TreeNode ("B");
+
+                       Form f = new Form ();
+                       TreeView tv = new TreeView ();
+                       tv.LabelEdit = true;
+                       tv.Parent = f;
+                       tv.Nodes.Add (node1);
+                       tv.Nodes.Add (node2);
+
+                       f.Show ();
+
+                       // EndEdit called on a different node
+                       node1.BeginEdit ();
+                       Assert.AreEqual (true, node1.IsEditing, "#1");
+                       node2.EndEdit (false);
+                       Assert.AreEqual (false, node1.IsEditing, "#2");
+
+                       node1.BeginEdit ();
+                       Assert.AreEqual (true, node1.IsEditing, "#3");
+                       node2.EndEdit (true);
+                       Assert.AreEqual (false, node1.IsEditing, "#4");
+
+                       f.Dispose ();
+               }
+               
+#if NET_2_0
+               [Test]
+               public void PropertyName ()
+               {
+                       TreeNode tn = new TreeNode ();
+                       
+                       Assert.AreEqual (string.Empty, tn.Name, "A1");
+                       
+                       tn.Name = "Monkey";
+                       Assert.AreEqual ("Monkey", tn.Name, "A2");
+       
+                       tn.Name = null;
+                       Assert.AreEqual (string.Empty, tn.Name, "A3");
+               }
+               
+               [Test]
+               public void PropertyLevel ()
+               {
+                       TreeNode tn = new TreeNode ();
+                       
+                       Assert.AreEqual (0, tn.Level, "A1");
+                       
+                       TreeView tv = new TreeView ();
+                       tv.Nodes.Add (tn);
+
+                       Assert.AreEqual (0, tn.Level, "A2");
+
+                       TreeNode tn1 = new TreeNode ();
+                       tn.Nodes.Add (tn1);
+
+                       Assert.AreEqual (1, tn1.Level, "A3");
+
+                       TreeNode tn2 = new TreeNode ();
+                       tn1.Nodes.Add (tn2);
+
+                       Assert.AreEqual (2, tn2.Level, "A4");
+               }
+               
+               [Test]
+               public void PropertyToolTipText ()
+               {
+                       TreeNode tn = new TreeNode ("test");
+                       
+                       Assert.AreEqual (string.Empty, tn.ToolTipText, "A1");
+                       
+                       tn.ToolTipText = "Woo";
+
+                       Assert.AreEqual ("Woo", tn.ToolTipText, "A2");
+               }
+               
+#if NET_2_0
+               [Test]
+               public void ImageKeyIndex ()
+               {
+                       TreeNode tn = new TreeNode ("Test");
+
+                       Assert.AreEqual (-1, tn.ImageIndex, "A1");
+                       Assert.AreEqual (string.Empty, tn.ImageKey, "A2");
+                       
+                       tn.ImageIndex = 2;
+
+                       Assert.AreEqual (2, tn.ImageIndex, "A3");
+                       Assert.AreEqual (string.Empty, tn.ImageKey, "A4");
+                       
+                       tn.ImageKey = "b";
+
+                       Assert.AreEqual (-1, tn.ImageIndex, "A5");
+                       Assert.AreEqual ("b", tn.ImageKey, "A6");
+                       
+                       tn.ImageIndex = 2;
+
+                       Assert.AreEqual (2, tn.ImageIndex, "A7");
+                       Assert.AreEqual (string.Empty, tn.ImageKey, "A8");
+               }
+#endif
+
+               //[Test]
+               //public void MethodSerialize ()
+               //{
+               //        PublicTreeNode tn = new PublicTreeNode ("test");
+
+               //        SerializationInfo si = new SerializationInfo (tn.GetType (), new BinaryFormatter ());
+               //        StreamingContext sc = new StreamingContext ();
+                       
+               //        tn.PublicSerialize (si, sc);
+               //}
+               
+               private class PublicTreeNode : TreeNode
+               {
+                       public PublicTreeNode (string text) : base (text) {}
+                       
+                       public void PublicSerialize (SerializationInfo si, StreamingContext context)
+                       {
+                               base.Serialize (si, context);
+                       }
+               }
+#endif
        }
 }