| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- 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 : TestHelper {
- [Test]
- public void EmptyCtorTest ()
- {
- TreeNode tn = new TreeNode ();
- Assert.AreEqual ("", tn.Text, "#1");
- Assert.AreEqual (0, tn.Nodes.Count, "#2");
- Assert.AreEqual (-1, tn.ImageIndex, "#3");
- Assert.AreEqual (-1, tn.SelectedImageIndex, "#4");
- // Set simple properties
- tn.Text = null;
- Assert.AreEqual ("", tn.Text, "#5");
- tn.ImageIndex = 67;
- Assert.AreEqual (67, tn.ImageIndex, "#6");
- tn.SelectedImageIndex = 99;
- Assert.AreEqual (99, tn.SelectedImageIndex, "#7");
- }
- [Test]
- public void CtorTest () {
- TreeNode tn = new TreeNode ("label1");
-
- Assert.AreEqual ("label1", tn.Text);
- Assert.AreEqual (0, tn.Nodes.Count);
- Assert.AreEqual (-1, tn.ImageIndex, "II");
- Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
- Assert.IsNull (tn.FirstNode);
- Assert.IsNull (tn.LastNode);
- Assert.AreEqual ("", new TreeNode (null).Text);
- }
- [Test]
- public void CtorTest2 ()
- {
- TreeNode tn = new TreeNode ("a1", new TreeNode[] { new TreeNode ("aa1"), new TreeNode ("aa2") } );
- Assert.AreEqual ("a1", tn.Text);
- Assert.AreEqual (-1, tn.ImageIndex, "II");
- Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
- Assert.AreEqual ("aa1", tn.Nodes [0].Text, "#1");
- Assert.AreEqual ("aa2", tn.Nodes [1].Text, "#2");
- Assert.AreSame (tn.FirstNode, tn.Nodes [0], "#3");
- Assert.AreSame (tn.LastNode, tn.Nodes [1], "#4");
- }
- [Test]
- public void CtorTest3 ()
- {
- TreeNode tn = new TreeNode ("a", 5, 9);
- Assert.AreEqual ("a", tn.Text);
- Assert.IsNotNull (tn.Nodes);
- Assert.AreEqual (5, tn.ImageIndex);
- Assert.AreEqual (9, tn.SelectedImageIndex);
- Assert.AreEqual ("", new TreeNode (null, 0, 0).Text);
- }
- [Test, ExpectedException (typeof (ArgumentNullException))]
- public void CtorException1 ()
- {
- new TreeNode ("", 1, 1, null);
- }
- [Test, ExpectedException (typeof (ArgumentNullException))]
- public void CtorException2 () {
- new TreeNode ("tt", null);
- }
- [Test]
- public void Traverse ()
- {
- TreeNode tn_1 = new TreeNode ("1");
- TreeNode tn_2 = new TreeNode ("2");
- TreeNode tn_3 = new TreeNode ("3");
- TreeNode tn = new TreeNode ("lev1");
- tn.Nodes.Add (tn_1);
- Assert.AreSame (tn, tn_1.Parent, "#1");
- Assert.IsNull (tn_1.NextNode, "#2");
- Assert.AreEqual (0, tn_1.Parent.Index, "#3");
- tn.Nodes.Add (tn_2);
- Assert.IsNull (tn_1.NextNode.NextNode, "#33");
- tn.Nodes.Add (tn_3);
- Assert.AreEqual (2, tn_3.Index, "#4");
- Assert.AreEqual (3, tn.Nodes.Count, "#5");
- Assert.AreSame (tn_2, tn_2.NextNode.PrevNode, "#6");
- Assert.IsNull (tn_1.PrevNode, "#7");
- }
- [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 ()
- {
- 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]
- public void FullPathTest ()
- {
- TreeNode tn_1 = new TreeNode ("A");
- TreeNode tn_2 = new TreeNode ("B");
- tn_2.Nodes.Add (tn_1);
- TreeView tv = new TreeView ();
- tv.Nodes.Add (tn_1);
- tv.Nodes [0].Nodes.Add (tn_2);
- Assert.AreEqual ("A", tn_1.FullPath, "#1");
- Assert.AreEqual ("A", tv.Nodes[0].FullPath, "#2");
- Assert.AreEqual (@"A\B", tn_2.FullPath, "#3");
- tv.PathSeparator = "_separator_";
- Assert.AreEqual ("A_separator_B", tn_2.FullPath, "#4");
- }
- [Test]
- public void CloneTest ()
- {
- TreeNode orig = new TreeNode ("text", 2, 3, new TreeNode [] { new TreeNode ("child", 22, 33) });
- orig.Tag = FlatStyle.Flat;
- orig.Checked = true;
- orig.BackColor = SystemDrawingNamespace.Color.AliceBlue;
- orig.ForeColor = SystemDrawingNamespace.Color.Beige;
- TreeNode clone = (TreeNode)orig.Clone ();
- Assert.AreEqual ("text", clone.Text, "#1");
- Assert.AreEqual (2, clone.ImageIndex, "#2");
- Assert.AreEqual (3, clone.SelectedImageIndex, "#3");
- Assert.AreEqual (1, clone.Nodes.Count, "#4");
- Assert.AreEqual (FlatStyle.Flat, clone.Tag, "#5");
- Assert.IsTrue (clone.Checked, "#6");
- Assert.AreEqual ("child", clone.Nodes [0].Text, "#10");
- Assert.AreEqual (22, clone.Nodes [0].ImageIndex, "#11");
- Assert.AreEqual (SystemDrawingNamespace.Color.AliceBlue, clone.BackColor, "#12");
- Assert.AreEqual (SystemDrawingNamespace.Color.Beige, clone.ForeColor, "#13");
- }
- [Test]
- public void SingleNodeIndexTest ()
- {
- TreeNode tn_1 = new TreeNode ("A");
- Assert.AreEqual (0, tn_1.Index, "#1");
- TreeView tv = new TreeView ();
- 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
- }
- }
|