Merge remote branch 'upstream/master'
[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] // bug 661753
182                 public void TestTreeNodeClone ()
183                 {
184                         TreeNode orig = new TreeNode ();
185
186                         orig.Nodes.Add ("Node1");
187                         orig.Nodes.Add ("Node2");
188                         orig.Nodes.Add ("Node3");
189
190                         orig.Checked = true;
191                         orig.ImageIndex = 4;
192                         orig.Name = "MyName";
193                         orig.SelectedImageIndex = 3;
194                         orig.StateImageIndex = 8;
195                         orig.Tag = new object ();
196                         orig.Text = "MyText";
197                         orig.ToolTipText = "MyToolTipText";
198
199                         orig.ContextMenu = new ContextMenu ();
200                         orig.ContextMenu.Name = "MyContextMenu";
201                         orig.ContextMenu.MenuItems.Add (new MenuItem ("MenuItem1"));
202                         orig.ContextMenu.MenuItems.Add (new MenuItem ("MenuItem2"));
203
204                         orig.ContextMenuStrip = new ContextMenuStrip ();
205                         orig.ContextMenuStrip.Name = "MyContextMenuStrip";
206                         orig.ContextMenuStrip.Items.Add ("ToolStripText");
207                         orig.ContextMenuStrip.Items.Add (new SystemDrawingNamespace.Bitmap (1, 1));
208
209                         TreeNode clone = orig.Clone () as TreeNode;
210
211                         Assert.AreEqual (orig.Nodes[0].Name, clone.Nodes[0].Name, "#01");
212                         Assert.AreEqual (orig.Nodes[1].Name, clone.Nodes[1].Name, "#02");
213                         Assert.AreEqual (orig.Nodes[2].Name, clone.Nodes[2].Name, "#03");
214                         Assert.AreNotEqual (orig.Nodes[0], clone.Nodes[0], "#04");
215                         Assert.AreNotEqual (orig.Nodes[1], clone.Nodes[1], "#05");
216                         Assert.AreNotEqual (orig.Nodes[2], clone.Nodes[2], "#06");
217
218                         Assert.AreEqual (orig.Checked, clone.Checked, "#07");
219                         Assert.AreEqual (orig.ImageIndex, clone.ImageIndex, "#08");
220                         Assert.AreEqual (orig.Name, clone.Name, "#09");
221                         Assert.AreEqual (orig.SelectedImageIndex, clone.SelectedImageIndex, "#10");
222                         Assert.AreEqual (orig.StateImageIndex, clone.StateImageIndex, "#11");
223                         Assert.AreEqual (orig.Tag, clone.Tag, "#12");
224                         Assert.AreEqual (orig.Text, clone.Text, "#13");
225                         Assert.AreEqual (orig.ToolTipText, clone.ToolTipText, "#14");
226                         Assert.AreEqual (orig.ContextMenu, clone.ContextMenu, "#15");
227                         Assert.AreEqual (orig.ContextMenuStrip, clone.ContextMenuStrip, "#16");
228                 }
229
230                 [Test] // bug 661753
231                 public void TestTreeNodeClone2 ()
232                 {
233                         // Cannot test ImageIndex and ImageKey properties at the same time,
234                         // as one excludes the other. So this method is for Keys only.
235
236                         TreeNode orig = new TreeNode ();
237
238                         orig.ImageKey = "MyImageKey";
239                         orig.SelectedImageKey = "MySelectedImageKey";
240                         orig.StateImageKey = "MyStateImageKey";
241
242                         TreeNode clone = orig.Clone () as TreeNode;
243
244                         Assert.AreEqual (orig.ImageKey, clone.ImageKey, "#01");
245                         Assert.AreEqual (orig.SelectedImageKey, clone.SelectedImageKey, "#02");
246                         Assert.AreEqual (orig.StateImageKey, clone.StateImageKey, "#03");
247                 }
248
249                 [Test]
250                 public void SingleNodeIndexTest ()
251                 {
252                         TreeNode tn_1 = new TreeNode ("A");
253                         Assert.AreEqual (0, tn_1.Index, "#1");
254                         TreeView tv = new TreeView ();
255                         tv.Nodes.Add (tn_1);
256                         Assert.AreEqual (0, tn_1.Index, "#2");
257                 }
258
259                 [Test]
260                 public void EndEditTest ()
261                 {
262                         TreeNode node1 = new TreeNode ("A");
263                         TreeNode node2 = new TreeNode ("B");
264
265                         Form f = new Form ();
266                         TreeView tv = new TreeView ();
267                         tv.LabelEdit = true;
268                         tv.Parent = f;
269                         tv.Nodes.Add (node1);
270                         tv.Nodes.Add (node2);
271
272                         f.Show ();
273
274                         // EndEdit called on a different node
275                         node1.BeginEdit ();
276                         Assert.AreEqual (true, node1.IsEditing, "#1");
277                         node2.EndEdit (false);
278                         Assert.AreEqual (false, node1.IsEditing, "#2");
279
280                         node1.BeginEdit ();
281                         Assert.AreEqual (true, node1.IsEditing, "#3");
282                         node2.EndEdit (true);
283                         Assert.AreEqual (false, node1.IsEditing, "#4");
284
285                         f.Dispose ();
286                 }
287                 
288 #if NET_2_0
289                 [Test]
290                 public void PropertyName ()
291                 {
292                         TreeNode tn = new TreeNode ();
293                         
294                         Assert.AreEqual (string.Empty, tn.Name, "A1");
295                         
296                         tn.Name = "Monkey";
297                         Assert.AreEqual ("Monkey", tn.Name, "A2");
298         
299                         tn.Name = null;
300                         Assert.AreEqual (string.Empty, tn.Name, "A3");
301                 }
302                 
303                 [Test]
304                 public void PropertyLevel ()
305                 {
306                         TreeNode tn = new TreeNode ();
307                         
308                         Assert.AreEqual (0, tn.Level, "A1");
309                         
310                         TreeView tv = new TreeView ();
311                         tv.Nodes.Add (tn);
312
313                         Assert.AreEqual (0, tn.Level, "A2");
314
315                         TreeNode tn1 = new TreeNode ();
316                         tn.Nodes.Add (tn1);
317
318                         Assert.AreEqual (1, tn1.Level, "A3");
319
320                         TreeNode tn2 = new TreeNode ();
321                         tn1.Nodes.Add (tn2);
322
323                         Assert.AreEqual (2, tn2.Level, "A4");
324                 }
325                 
326                 [Test]
327                 public void PropertyToolTipText ()
328                 {
329                         TreeNode tn = new TreeNode ("test");
330                         
331                         Assert.AreEqual (string.Empty, tn.ToolTipText, "A1");
332                         
333                         tn.ToolTipText = "Woo";
334
335                         Assert.AreEqual ("Woo", tn.ToolTipText, "A2");
336                 }
337                 
338 #if NET_2_0
339                 [Test]
340                 public void ImageKeyIndex ()
341                 {
342                         TreeNode tn = new TreeNode ("Test");
343
344                         Assert.AreEqual (-1, tn.ImageIndex, "A1");
345                         Assert.AreEqual (string.Empty, tn.ImageKey, "A2");
346                         
347                         tn.ImageIndex = 2;
348
349                         Assert.AreEqual (2, tn.ImageIndex, "A3");
350                         Assert.AreEqual (string.Empty, tn.ImageKey, "A4");
351                         
352                         tn.ImageKey = "b";
353
354                         Assert.AreEqual (-1, tn.ImageIndex, "A5");
355                         Assert.AreEqual ("b", tn.ImageKey, "A6");
356                         
357                         tn.ImageIndex = 2;
358
359                         Assert.AreEqual (2, tn.ImageIndex, "A7");
360                         Assert.AreEqual (string.Empty, tn.ImageKey, "A8");
361                 }
362 #endif
363
364                 //[Test]
365                 //public void MethodSerialize ()
366                 //{
367                 //        PublicTreeNode tn = new PublicTreeNode ("test");
368
369                 //        SerializationInfo si = new SerializationInfo (tn.GetType (), new BinaryFormatter ());
370                 //        StreamingContext sc = new StreamingContext ();
371                         
372                 //        tn.PublicSerialize (si, sc);
373                 //}
374                 
375                 private class PublicTreeNode : TreeNode
376                 {
377                         public PublicTreeNode (string text) : base (text) {}
378                         
379                         public void PublicSerialize (SerializationInfo si, StreamingContext context)
380                         {
381                                 base.Serialize (si, context);
382                         }
383                 }
384 #endif
385         }
386 }