2008-11-24 Jonathan Pobst <monkey@jpobst.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 using System.Runtime.Serialization;
6 using System.Runtime.Serialization.Formatters.Binary;
7
8 namespace MonoTests.System.Windows.Forms {
9
10         
11         [TestFixture]
12         public class TreeNodeTest : TestHelper {
13
14                 [Test]
15                 public void EmptyCtorTest ()
16                 {
17                         TreeNode tn = new TreeNode ();
18                         Assert.AreEqual ("", tn.Text, "#1");
19                         Assert.AreEqual (0, tn.Nodes.Count, "#2");
20                         Assert.AreEqual (-1, tn.ImageIndex, "#3");
21                         Assert.AreEqual (-1, tn.SelectedImageIndex, "#4");
22
23                         // Set simple properties
24                         tn.Text = null;
25                         Assert.AreEqual ("", tn.Text, "#5");
26                         tn.ImageIndex = 67;
27                         Assert.AreEqual (67, tn.ImageIndex, "#6");
28                         tn.SelectedImageIndex = 99;
29                         Assert.AreEqual (99, tn.SelectedImageIndex, "#7");
30                 }
31
32                 [Test]
33                 public void CtorTest () {
34                         TreeNode tn = new TreeNode ("label1");
35                 
36                         Assert.AreEqual ("label1", tn.Text);
37                         Assert.AreEqual (0, tn.Nodes.Count);
38                         Assert.AreEqual (-1, tn.ImageIndex, "II");
39                         Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
40
41                         Assert.IsNull (tn.FirstNode);
42                         Assert.IsNull (tn.LastNode);
43                         Assert.AreEqual ("", new TreeNode (null).Text);
44                 }
45
46                 [Test]
47                 public void CtorTest2 ()
48                 {
49                         TreeNode tn = new TreeNode ("a1", new TreeNode[] { new TreeNode ("aa1"), new TreeNode ("aa2") } );
50
51                         Assert.AreEqual ("a1", tn.Text);
52                         Assert.AreEqual (-1, tn.ImageIndex, "II");
53                         Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
54
55                         Assert.AreEqual ("aa1", tn.Nodes [0].Text, "#1");
56                         Assert.AreEqual ("aa2", tn.Nodes [1].Text, "#2");
57                         Assert.AreSame (tn.FirstNode, tn.Nodes [0], "#3");
58                         Assert.AreSame (tn.LastNode, tn.Nodes [1], "#4");
59                 }
60
61                 [Test]
62                 public void CtorTest3 ()
63                 {
64                         TreeNode tn = new TreeNode ("a", 5, 9);
65
66                         Assert.AreEqual ("a", tn.Text);
67                         Assert.IsNotNull (tn.Nodes);
68                         Assert.AreEqual (5, tn.ImageIndex);
69                         Assert.AreEqual (9, tn.SelectedImageIndex);
70                         Assert.AreEqual ("", new TreeNode (null, 0, 0).Text);
71                 }
72
73                 [Test, ExpectedException (typeof (ArgumentNullException))]
74                 public void CtorException1 ()
75                 {
76                         new TreeNode ("", 1, 1, null);
77                 }
78
79                 [Test, ExpectedException (typeof (ArgumentNullException))]
80                 public void CtorException2 () {
81                         new TreeNode ("tt", null);
82                 }
83
84                 [Test]
85                 public void Traverse ()
86                 {
87                         TreeNode tn_1 = new TreeNode ("1");
88                         TreeNode tn_2 = new TreeNode ("2");
89                         TreeNode tn_3 = new TreeNode ("3");
90                         TreeNode tn = new TreeNode ("lev1");
91                         tn.Nodes.Add (tn_1);
92                         Assert.AreSame (tn, tn_1.Parent, "#1");
93                         Assert.IsNull (tn_1.NextNode, "#2");
94                         Assert.AreEqual (0, tn_1.Parent.Index, "#3");
95                         tn.Nodes.Add (tn_2);
96                         Assert.IsNull (tn_1.NextNode.NextNode, "#33");
97                         tn.Nodes.Add (tn_3);
98                         Assert.AreEqual (2, tn_3.Index, "#4");
99
100                         Assert.AreEqual (3, tn.Nodes.Count, "#5");
101                         Assert.AreSame (tn_2, tn_2.NextNode.PrevNode, "#6");
102                         Assert.IsNull (tn_1.PrevNode, "#7");
103                 }
104
105                 [Test]
106                 public void ExpandCollapseLeafTest ()
107                 {
108                         // Leaf nodes should keep its expanded state
109                         TreeNode tn_1 = new TreeNode ();
110                         Assert.IsFalse (tn_1.IsExpanded, "#1");
111
112                         tn_1.Expand ();
113                         Assert.IsTrue (tn_1.IsExpanded, "#2");
114                 }
115
116                 [Test]
117                 public void FullPathException ()
118                 {
119                         try {
120                                 string s = new TreeNode ("").FullPath;
121                                 Assert.Fail ("#1");
122                                 // Prevent CS0219, will never write anything
123                                 // due to previous statement throwing an
124                                 // exception
125                                 Console.WriteLine (s);
126 #if NET_2_0
127                         } catch (InvalidOperationException ex) {
128                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
129                                 Assert.IsNotNull (ex.Message, "#3");
130                                 Assert.IsNull (ex.InnerException, "#4");
131                         }
132 #else
133                         } catch (Exception ex) {
134                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
135                                 Assert.IsNotNull (ex.Message, "#3");
136                                 Assert.IsNull (ex.InnerException, "#4");
137                         }
138 #endif
139                 }
140
141                 [Test]
142                 public void FullPathTest ()
143                 {
144                         TreeNode tn_1 = new TreeNode ("A");
145                         TreeNode tn_2 = new TreeNode ("B");
146                         tn_2.Nodes.Add (tn_1);
147
148                         TreeView tv = new TreeView ();
149                         tv.Nodes.Add (tn_1);
150                         tv.Nodes [0].Nodes.Add (tn_2);
151
152                         Assert.AreEqual ("A", tn_1.FullPath, "#1");
153                         Assert.AreEqual ("A", tv.Nodes[0].FullPath, "#2");
154                         Assert.AreEqual (@"A\B", tn_2.FullPath, "#3");
155                         tv.PathSeparator = "_separator_";
156                         Assert.AreEqual ("A_separator_B", tn_2.FullPath, "#4");
157                 }
158
159                 [Test]
160                 public void CloneTest ()
161                 {
162                         TreeNode orig = new TreeNode ("text", 2, 3, new TreeNode [] { new TreeNode ("child", 22, 33) });
163                         orig.Tag = FlatStyle.Flat;
164                         orig.Checked = true;
165                         orig.BackColor = SystemDrawingNamespace.Color.AliceBlue;
166                         orig.ForeColor = SystemDrawingNamespace.Color.Beige;
167
168                         TreeNode clone = (TreeNode)orig.Clone ();
169                         Assert.AreEqual ("text", clone.Text, "#1");
170                         Assert.AreEqual (2, clone.ImageIndex, "#2");
171                         Assert.AreEqual (3, clone.SelectedImageIndex, "#3");
172                         Assert.AreEqual (1, clone.Nodes.Count, "#4");
173                         Assert.AreEqual (FlatStyle.Flat, clone.Tag, "#5");
174                         Assert.IsTrue (clone.Checked, "#6");
175                         Assert.AreEqual ("child", clone.Nodes [0].Text, "#10");
176                         Assert.AreEqual (22, clone.Nodes [0].ImageIndex, "#11");
177                         Assert.AreEqual (SystemDrawingNamespace.Color.AliceBlue, clone.BackColor, "#12");
178                         Assert.AreEqual (SystemDrawingNamespace.Color.Beige, clone.ForeColor, "#13");
179                 }
180
181                 [Test]
182                 public void SingleNodeIndexTest ()
183                 {
184                         TreeNode tn_1 = new TreeNode ("A");
185                         Assert.AreEqual (0, tn_1.Index, "#1");
186                         TreeView tv = new TreeView ();
187                         tv.Nodes.Add (tn_1);
188                         Assert.AreEqual (0, tn_1.Index, "#2");
189                 }
190
191                 [Test]
192                 public void EndEditTest ()
193                 {
194                         TreeNode node1 = new TreeNode ("A");
195                         TreeNode node2 = new TreeNode ("B");
196
197                         Form f = new Form ();
198                         TreeView tv = new TreeView ();
199                         tv.LabelEdit = true;
200                         tv.Parent = f;
201                         tv.Nodes.Add (node1);
202                         tv.Nodes.Add (node2);
203
204                         f.Show ();
205
206                         // EndEdit called on a different node
207                         node1.BeginEdit ();
208                         Assert.AreEqual (true, node1.IsEditing, "#1");
209                         node2.EndEdit (false);
210                         Assert.AreEqual (false, node1.IsEditing, "#2");
211
212                         node1.BeginEdit ();
213                         Assert.AreEqual (true, node1.IsEditing, "#3");
214                         node2.EndEdit (true);
215                         Assert.AreEqual (false, node1.IsEditing, "#4");
216
217                         f.Dispose ();
218                 }
219                 
220 #if NET_2_0
221                 [Test]
222                 public void PropertyName ()
223                 {
224                         TreeNode tn = new TreeNode ();
225                         
226                         Assert.AreEqual (string.Empty, tn.Name, "A1");
227                         
228                         tn.Name = "Monkey";
229                         Assert.AreEqual ("Monkey", tn.Name, "A2");
230         
231                         tn.Name = null;
232                         Assert.AreEqual (string.Empty, tn.Name, "A3");
233                 }
234                 
235                 [Test]
236                 public void PropertyLevel ()
237                 {
238                         TreeNode tn = new TreeNode ();
239                         
240                         Assert.AreEqual (0, tn.Level, "A1");
241                         
242                         TreeView tv = new TreeView ();
243                         tv.Nodes.Add (tn);
244
245                         Assert.AreEqual (0, tn.Level, "A2");
246
247                         TreeNode tn1 = new TreeNode ();
248                         tn.Nodes.Add (tn1);
249
250                         Assert.AreEqual (1, tn1.Level, "A3");
251
252                         TreeNode tn2 = new TreeNode ();
253                         tn1.Nodes.Add (tn2);
254
255                         Assert.AreEqual (2, tn2.Level, "A4");
256                 }
257                 
258                 [Test]
259                 public void PropertyToolTipText ()
260                 {
261                         TreeNode tn = new TreeNode ("test");
262                         
263                         Assert.AreEqual (string.Empty, tn.ToolTipText, "A1");
264                         
265                         tn.ToolTipText = "Woo";
266
267                         Assert.AreEqual ("Woo", tn.ToolTipText, "A2");
268                 }
269                 
270 #if NET_2_0
271                 [Test]
272                 public void ImageKeyIndex ()
273                 {
274                         TreeNode tn = new TreeNode ("Test");
275
276                         Assert.AreEqual (-1, tn.ImageIndex, "A1");
277                         Assert.AreEqual (string.Empty, tn.ImageKey, "A2");
278                         
279                         tn.ImageIndex = 2;
280
281                         Assert.AreEqual (2, tn.ImageIndex, "A3");
282                         Assert.AreEqual (string.Empty, tn.ImageKey, "A4");
283                         
284                         tn.ImageKey = "b";
285
286                         Assert.AreEqual (-1, tn.ImageIndex, "A5");
287                         Assert.AreEqual ("b", tn.ImageKey, "A6");
288                         
289                         tn.ImageIndex = 2;
290
291                         Assert.AreEqual (2, tn.ImageIndex, "A7");
292                         Assert.AreEqual (string.Empty, tn.ImageKey, "A8");
293                 }
294 #endif
295
296                 //[Test]
297                 //public void MethodSerialize ()
298                 //{
299                 //        PublicTreeNode tn = new PublicTreeNode ("test");
300
301                 //        SerializationInfo si = new SerializationInfo (tn.GetType (), new BinaryFormatter ());
302                 //        StreamingContext sc = new StreamingContext ();
303                         
304                 //        tn.PublicSerialize (si, sc);
305                 //}
306                 
307                 private class PublicTreeNode : TreeNode
308                 {
309                         public PublicTreeNode (string text) : base (text) {}
310                         
311                         public void PublicSerialize (SerializationInfo si, StreamingContext context)
312                         {
313                                 base.Serialize (si, context);
314                         }
315                 }
316 #endif
317         }
318 }