merge -r 53370:58178
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / TreeNodeTest.cs
1 using System;
2 using NUnit.Framework;
3 using System.Windows.Forms;
4
5 [TestFixture]
6 public class TreeNodeTest {
7
8         [Test]
9         public void EmptyCtorTest ()
10         {
11                 TreeNode tn = new TreeNode ();
12                 Assert.AreEqual ("", tn.Text, "#1");
13                 Assert.AreEqual (0, tn.Nodes.Count, "#2");
14                 Assert.AreEqual (-1, tn.ImageIndex, "#3");
15                 Assert.AreEqual (-1, tn.SelectedImageIndex, "#4");
16
17                 // Set simple properties
18                 tn.Text = null;
19                 Assert.AreEqual ("", tn.Text, "#5");
20                 tn.ImageIndex = 67;
21                 Assert.AreEqual (67, tn.ImageIndex, "#6");
22                 tn.SelectedImageIndex = 99;
23                 Assert.AreEqual (99, tn.SelectedImageIndex, "#7");
24         }
25
26         [Test]
27         public void CtorTest () {
28                 TreeNode tn = new TreeNode ("label1");
29                 
30                 Assert.AreEqual ("label1", tn.Text);
31                 Assert.AreEqual (0, tn.Nodes.Count);
32                 Assert.AreEqual (-1, tn.ImageIndex, "II");
33                 Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
34
35                 Assert.IsNull (tn.FirstNode);
36                 Assert.IsNull (tn.LastNode);
37                 Assert.AreEqual ("", new TreeNode (null).Text);
38         }
39
40         [Test]
41         public void CtorTest2 ()
42         {
43                 TreeNode tn = new TreeNode ("a1", new TreeNode[] { new TreeNode ("aa1"), new TreeNode ("aa2") } );
44
45                 Assert.AreEqual ("a1", tn.Text);
46                 Assert.AreEqual (-1, tn.ImageIndex, "II");
47                 Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
48
49                 Assert.AreEqual ("aa1", tn.Nodes [0].Text, "#1");
50                 Assert.AreEqual ("aa2", tn.Nodes [1].Text, "#2");
51                 Assert.AreSame (tn.FirstNode, tn.Nodes [0], "#3");
52                 Assert.AreSame (tn.LastNode, tn.Nodes [1], "#4");
53         }
54
55         [Test]
56         public void CtorTest3 ()
57         {
58                 TreeNode tn = new TreeNode ("a", 5, 9);
59
60                 Assert.AreEqual ("a", tn.Text);
61                 Assert.IsNotNull (tn.Nodes);
62                 Assert.AreEqual (5, tn.ImageIndex);
63                 Assert.AreEqual (9, tn.SelectedImageIndex);
64                 Assert.AreEqual ("", new TreeNode (null, 0, 0).Text);
65         }
66
67         [Test, ExpectedException (typeof (ArgumentNullException))]
68         public void CtorException1 ()
69         {
70                 new TreeNode ("", 1, 1, null);
71         }
72
73         [Test, ExpectedException (typeof (ArgumentNullException))]
74         public void CtorException2 () {
75                 new TreeNode ("tt", null);
76         }
77
78         [Test]
79         public void Traverse ()
80         {
81                 TreeNode tn_1 = new TreeNode ("1");
82                 TreeNode tn_2 = new TreeNode ("2");
83                 TreeNode tn_3 = new TreeNode ("3");
84                 TreeNode tn = new TreeNode ("lev1");
85                 tn.Nodes.Add (tn_1);
86                 Assert.AreSame (tn, tn_1.Parent, "#1");
87                 Assert.IsNull (tn_1.NextNode, "#2");
88                 Assert.AreEqual (0, tn_1.Parent.Index, "#3");
89                 tn.Nodes.Add (tn_2);
90                 Assert.IsNull (tn_1.NextNode.NextNode, "#33");
91                 tn.Nodes.Add (tn_3);
92                 Assert.AreEqual (2, tn_3.Index, "#4");
93
94                 Assert.AreEqual (3, tn.Nodes.Count, "#5");
95                 Assert.AreSame (tn_2, tn_2.NextNode.PrevNode, "#6");
96                 Assert.IsNull (tn_1.PrevNode, "#7");
97         }
98
99         [Test, ExpectedException (typeof (Exception))]
100         public void FullPathException ()
101         {
102                 string s = new TreeNode ("").FullPath;
103         }
104
105         [Test]
106         public void FullPathTest ()
107         {
108                 TreeNode tn_1 = new TreeNode ("A");
109                 TreeNode tn_2 = new TreeNode ("B");
110                 tn_2.Nodes.Add (tn_1);
111
112                 TreeView tv = new TreeView ();
113                 tv.Nodes.Add (tn_1);
114                 tv.Nodes [0].Nodes.Add (tn_2);
115
116                 Assert.AreEqual ("A", tn_1.FullPath, "#1");
117                 Assert.AreEqual ("A", tv.Nodes[0].FullPath, "#2");
118                 Assert.AreEqual (@"A\B", tn_2.FullPath, "#3");
119                 tv.PathSeparator = "_separator_";
120                 Assert.AreEqual ("A_separator_B", tn_2.FullPath, "#4");
121         }
122
123         [Test]
124         public void CloneTest ()
125         {
126                 TreeNode orig = new TreeNode ("text", 2, 3, new TreeNode [] { new TreeNode ("child", 22, 33) });
127                 orig.Tag = FlatStyle.Flat;
128                 orig.Checked = true;
129                 orig.BackColor = System.Drawing.Color.AliceBlue;
130                 orig.ForeColor = System.Drawing.Color.Beige;
131
132                 TreeNode clone = (TreeNode)orig.Clone ();
133                 Assert.AreEqual ("text", clone.Text, "#1");
134                 Assert.AreEqual (2, clone.ImageIndex, "#2");
135                 Assert.AreEqual (3, clone.SelectedImageIndex, "#3");
136                 Assert.AreEqual (1, clone.Nodes.Count, "#4");
137                 Assert.AreEqual (FlatStyle.Flat, clone.Tag, "#5");
138                 Assert.IsTrue (clone.Checked, "#6");
139                 Assert.AreEqual ("child", clone.Nodes [0].Text, "#10");
140                 Assert.AreEqual (22, clone.Nodes [0].ImageIndex, "#11");
141                 Assert.AreEqual (System.Drawing.Color.AliceBlue, clone.BackColor, "#12");
142                 Assert.AreEqual (System.Drawing.Color.Beige, clone.ForeColor, "#13");
143         }
144
145 }