merge -r 60814:60815
[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, ExpectedException (typeof (Exception))]
104                 public void FullPathException ()
105                 {
106                         string s = new TreeNode ("").FullPath;
107                         // Prevent CS0219, will never write anything
108                         // due to previous statement throwing Exception
109                         Console.WriteLine(s);
110                 }
111
112                 [Test]
113                 public void FullPathTest ()
114                 {
115                         TreeNode tn_1 = new TreeNode ("A");
116                         TreeNode tn_2 = new TreeNode ("B");
117                         tn_2.Nodes.Add (tn_1);
118
119                         TreeView tv = new TreeView ();
120                         tv.Nodes.Add (tn_1);
121                         tv.Nodes [0].Nodes.Add (tn_2);
122
123                         Assert.AreEqual ("A", tn_1.FullPath, "#1");
124                         Assert.AreEqual ("A", tv.Nodes[0].FullPath, "#2");
125                         Assert.AreEqual (@"A\B", tn_2.FullPath, "#3");
126                         tv.PathSeparator = "_separator_";
127                         Assert.AreEqual ("A_separator_B", tn_2.FullPath, "#4");
128                 }
129
130                 [Test]
131                 public void CloneTest ()
132                 {
133                         TreeNode orig = new TreeNode ("text", 2, 3, new TreeNode [] { new TreeNode ("child", 22, 33) });
134                         orig.Tag = FlatStyle.Flat;
135                         orig.Checked = true;
136                         orig.BackColor = SystemDrawingNamespace.Color.AliceBlue;
137                         orig.ForeColor = SystemDrawingNamespace.Color.Beige;
138
139                         TreeNode clone = (TreeNode)orig.Clone ();
140                         Assert.AreEqual ("text", clone.Text, "#1");
141                         Assert.AreEqual (2, clone.ImageIndex, "#2");
142                         Assert.AreEqual (3, clone.SelectedImageIndex, "#3");
143                         Assert.AreEqual (1, clone.Nodes.Count, "#4");
144                         Assert.AreEqual (FlatStyle.Flat, clone.Tag, "#5");
145                         Assert.IsTrue (clone.Checked, "#6");
146                         Assert.AreEqual ("child", clone.Nodes [0].Text, "#10");
147                         Assert.AreEqual (22, clone.Nodes [0].ImageIndex, "#11");
148                         Assert.AreEqual (SystemDrawingNamespace.Color.AliceBlue, clone.BackColor, "#12");
149                         Assert.AreEqual (SystemDrawingNamespace.Color.Beige, clone.ForeColor, "#13");
150                 }
151
152                 [Test]
153                 public void SingleNodeIndexTest ()
154                 {
155                         TreeNode tn_1 = new TreeNode ("A");
156                         Assert.AreEqual (0, tn_1.Index, "#1");
157                         TreeView tv = new TreeView ();
158                         tv.Nodes.Add (tn_1);
159                         Assert.AreEqual (0, tn_1.Index, "#2");
160                 }
161         }
162 }