TreeNodeTest.cs 8.1 KB

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