2007-03-28 Igor Zelmanovich <igorz@mainsoft.com>
[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 using SystemDrawingNamespace = System.Drawing;
5
6 namespace MonoTests.System.Windows.Forms {
7
8         
9         [TestFixture]
10         public class TreeNodeTest {
11
12                 [Test]
13                 public void EmptyCtorTest ()
14                 {
15                         TreeNode tn = new TreeNode ();
16                         Assert.AreEqual ("", tn.Text, "#1");
17                         Assert.AreEqual (0, tn.Nodes.Count, "#2");
18                         Assert.AreEqual (-1, tn.ImageIndex, "#3");
19                         Assert.AreEqual (-1, tn.SelectedImageIndex, "#4");
20
21                         // Set simple properties
22                         tn.Text = null;
23                         Assert.AreEqual ("", tn.Text, "#5");
24                         tn.ImageIndex = 67;
25                         Assert.AreEqual (67, tn.ImageIndex, "#6");
26                         tn.SelectedImageIndex = 99;
27                         Assert.AreEqual (99, tn.SelectedImageIndex, "#7");
28                 }
29
30                 [Test]
31                 public void CtorTest () {
32                         TreeNode tn = new TreeNode ("label1");
33                 
34                         Assert.AreEqual ("label1", tn.Text);
35                         Assert.AreEqual (0, tn.Nodes.Count);
36                         Assert.AreEqual (-1, tn.ImageIndex, "II");
37                         Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
38
39                         Assert.IsNull (tn.FirstNode);
40                         Assert.IsNull (tn.LastNode);
41                         Assert.AreEqual ("", new TreeNode (null).Text);
42                 }
43
44                 [Test]
45                 public void CtorTest2 ()
46                 {
47                         TreeNode tn = new TreeNode ("a1", new TreeNode[] { new TreeNode ("aa1"), new TreeNode ("aa2") } );
48
49                         Assert.AreEqual ("a1", tn.Text);
50                         Assert.AreEqual (-1, tn.ImageIndex, "II");
51                         Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
52
53                         Assert.AreEqual ("aa1", tn.Nodes [0].Text, "#1");
54                         Assert.AreEqual ("aa2", tn.Nodes [1].Text, "#2");
55                         Assert.AreSame (tn.FirstNode, tn.Nodes [0], "#3");
56                         Assert.AreSame (tn.LastNode, tn.Nodes [1], "#4");
57                 }
58
59                 [Test]
60                 public void CtorTest3 ()
61                 {
62                         TreeNode tn = new TreeNode ("a", 5, 9);
63
64                         Assert.AreEqual ("a", tn.Text);
65                         Assert.IsNotNull (tn.Nodes);
66                         Assert.AreEqual (5, tn.ImageIndex);
67                         Assert.AreEqual (9, tn.SelectedImageIndex);
68                         Assert.AreEqual ("", new TreeNode (null, 0, 0).Text);
69                 }
70
71                 [Test, ExpectedException (typeof (ArgumentNullException))]
72                 public void CtorException1 ()
73                 {
74                         new TreeNode ("", 1, 1, null);
75                 }
76
77                 [Test, ExpectedException (typeof (ArgumentNullException))]
78                 public void CtorException2 () {
79                         new TreeNode ("tt", null);
80                 }
81
82                 [Test]
83                 public void Traverse ()
84                 {
85                         TreeNode tn_1 = new TreeNode ("1");
86                         TreeNode tn_2 = new TreeNode ("2");
87                         TreeNode tn_3 = new TreeNode ("3");
88                         TreeNode tn = new TreeNode ("lev1");
89                         tn.Nodes.Add (tn_1);
90                         Assert.AreSame (tn, tn_1.Parent, "#1");
91                         Assert.IsNull (tn_1.NextNode, "#2");
92                         Assert.AreEqual (0, tn_1.Parent.Index, "#3");
93                         tn.Nodes.Add (tn_2);
94                         Assert.IsNull (tn_1.NextNode.NextNode, "#33");
95                         tn.Nodes.Add (tn_3);
96                         Assert.AreEqual (2, tn_3.Index, "#4");
97
98                         Assert.AreEqual (3, tn.Nodes.Count, "#5");
99                         Assert.AreSame (tn_2, tn_2.NextNode.PrevNode, "#6");
100                         Assert.IsNull (tn_1.PrevNode, "#7");
101                 }
102
103                 [Test]
104                 public void FullPathException ()
105                 {
106                         try {
107                                 string s = new TreeNode ("").FullPath;
108                                 Assert.Fail ("#1");
109                                 // Prevent CS0219, will never write anything
110                                 // due to previous statement throwing an
111                                 // exception
112                                 Console.WriteLine (s);
113 #if NET_2_0
114                         } catch (InvalidOperationException ex) {
115                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
116                                 Assert.IsNotNull (ex.Message, "#3");
117                                 Assert.IsNull (ex.InnerException, "#4");
118                         }
119 #else
120                         } catch (Exception ex) {
121                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
122                                 Assert.IsNotNull (ex.Message, "#3");
123                                 Assert.IsNull (ex.InnerException, "#4");
124                         }
125 #endif
126                 }
127
128                 [Test]
129                 public void FullPathTest ()
130                 {
131                         TreeNode tn_1 = new TreeNode ("A");
132                         TreeNode tn_2 = new TreeNode ("B");
133                         tn_2.Nodes.Add (tn_1);
134
135                         TreeView tv = new TreeView ();
136                         tv.Nodes.Add (tn_1);
137                         tv.Nodes [0].Nodes.Add (tn_2);
138
139                         Assert.AreEqual ("A", tn_1.FullPath, "#1");
140                         Assert.AreEqual ("A", tv.Nodes[0].FullPath, "#2");
141                         Assert.AreEqual (@"A\B", tn_2.FullPath, "#3");
142                         tv.PathSeparator = "_separator_";
143                         Assert.AreEqual ("A_separator_B", tn_2.FullPath, "#4");
144                 }
145
146                 [Test]
147                 public void CloneTest ()
148                 {
149                         TreeNode orig = new TreeNode ("text", 2, 3, new TreeNode [] { new TreeNode ("child", 22, 33) });
150                         orig.Tag = FlatStyle.Flat;
151                         orig.Checked = true;
152                         orig.BackColor = SystemDrawingNamespace.Color.AliceBlue;
153                         orig.ForeColor = SystemDrawingNamespace.Color.Beige;
154
155                         TreeNode clone = (TreeNode)orig.Clone ();
156                         Assert.AreEqual ("text", clone.Text, "#1");
157                         Assert.AreEqual (2, clone.ImageIndex, "#2");
158                         Assert.AreEqual (3, clone.SelectedImageIndex, "#3");
159                         Assert.AreEqual (1, clone.Nodes.Count, "#4");
160                         Assert.AreEqual (FlatStyle.Flat, clone.Tag, "#5");
161                         Assert.IsTrue (clone.Checked, "#6");
162                         Assert.AreEqual ("child", clone.Nodes [0].Text, "#10");
163                         Assert.AreEqual (22, clone.Nodes [0].ImageIndex, "#11");
164                         Assert.AreEqual (SystemDrawingNamespace.Color.AliceBlue, clone.BackColor, "#12");
165                         Assert.AreEqual (SystemDrawingNamespace.Color.Beige, clone.ForeColor, "#13");
166                 }
167
168                 [Test]
169                 public void SingleNodeIndexTest ()
170                 {
171                         TreeNode tn_1 = new TreeNode ("A");
172                         Assert.AreEqual (0, tn_1.Index, "#1");
173                         TreeView tv = new TreeView ();
174                         tv.Nodes.Add (tn_1);
175                         Assert.AreEqual (0, tn_1.Index, "#2");
176                 }
177                 
178 #if NET_2_0
179                 [Test]
180                 public void PropertyName ()
181                 {
182                         TreeNode tn = new TreeNode ();
183                         
184                         Assert.AreEqual (string.Empty, tn.Name, "A1");
185                         
186                         tn.Name = "Monkey";
187                         Assert.AreEqual ("Monkey", tn.Name, "A2");
188         
189                         tn.Name = null;
190                         Assert.AreEqual (string.Empty, tn.Name, "A3");
191                 }
192 #endif
193         }
194 }