TreeNodeTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using NUnit.Framework;
  3. using System.Windows.Forms;
  4. [TestFixture]
  5. public class TreeNodeTest {
  6. [Test]
  7. public void EmptyCtorTest ()
  8. {
  9. TreeNode tn = new TreeNode ();
  10. Assert.AreEqual ("", tn.Text, "#1");
  11. Assert.AreEqual (0, tn.Nodes.Count, "#2");
  12. Assert.AreEqual (-1, tn.ImageIndex, "#3");
  13. Assert.AreEqual (-1, tn.SelectedImageIndex, "#4");
  14. // Set simple properties
  15. tn.Text = null;
  16. Assert.AreEqual ("", tn.Text, "#5");
  17. tn.ImageIndex = 67;
  18. Assert.AreEqual (67, tn.ImageIndex, "#6");
  19. tn.SelectedImageIndex = 99;
  20. Assert.AreEqual (99, tn.SelectedImageIndex, "#7");
  21. }
  22. [Test]
  23. public void CtorTest () {
  24. TreeNode tn = new TreeNode ("label1");
  25. Assert.AreEqual ("label1", tn.Text);
  26. Assert.AreEqual (0, tn.Nodes.Count);
  27. Assert.AreEqual (-1, tn.ImageIndex, "II");
  28. Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
  29. Assert.IsNull (tn.FirstNode);
  30. Assert.IsNull (tn.LastNode);
  31. Assert.AreEqual ("", new TreeNode (null).Text);
  32. }
  33. [Test]
  34. public void CtorTest2 ()
  35. {
  36. TreeNode tn = new TreeNode ("a1", new TreeNode[] { new TreeNode ("aa1"), new TreeNode ("aa2") } );
  37. Assert.AreEqual ("a1", tn.Text);
  38. Assert.AreEqual (-1, tn.ImageIndex, "II");
  39. Assert.AreEqual (-1, tn.SelectedImageIndex, "SI");
  40. Assert.AreEqual ("aa1", tn.Nodes [0].Text, "#1");
  41. Assert.AreEqual ("aa2", tn.Nodes [1].Text, "#2");
  42. Assert.AreSame (tn.FirstNode, tn.Nodes [0], "#3");
  43. Assert.AreSame (tn.LastNode, tn.Nodes [1], "#4");
  44. }
  45. [Test]
  46. public void CtorTest3 ()
  47. {
  48. TreeNode tn = new TreeNode ("a", 5, 9);
  49. Assert.AreEqual ("a", tn.Text);
  50. Assert.IsNotNull (tn.Nodes);
  51. Assert.AreEqual (5, tn.ImageIndex);
  52. Assert.AreEqual (9, tn.SelectedImageIndex);
  53. Assert.AreEqual ("", new TreeNode (null, 0, 0).Text);
  54. }
  55. [Test, ExpectedException (typeof (ArgumentNullException))]
  56. public void CtorException1 ()
  57. {
  58. new TreeNode ("", 1, 1, null);
  59. }
  60. [Test, ExpectedException (typeof (ArgumentNullException))]
  61. public void CtorException2 () {
  62. new TreeNode ("tt", null);
  63. }
  64. [Test]
  65. public void Traverse ()
  66. {
  67. TreeNode tn_1 = new TreeNode ("1");
  68. TreeNode tn_2 = new TreeNode ("2");
  69. TreeNode tn_3 = new TreeNode ("3");
  70. TreeNode tn = new TreeNode ("lev1");
  71. tn.Nodes.Add (tn_1);
  72. Assert.AreSame (tn, tn_1.Parent, "#1");
  73. Assert.IsNull (tn_1.NextNode, "#2");
  74. Assert.AreEqual (0, tn_1.Parent.Index, "#3");
  75. tn.Nodes.Add (tn_2);
  76. Assert.IsNull (tn_1.NextNode.NextNode, "#33");
  77. tn.Nodes.Add (tn_3);
  78. Assert.AreEqual (2, tn_3.Index, "#4");
  79. Assert.AreEqual (3, tn.Nodes.Count, "#5");
  80. Assert.AreSame (tn_2, tn_2.NextNode.PrevNode, "#6");
  81. Assert.IsNull (tn_1.PrevNode, "#7");
  82. }
  83. [Test, ExpectedException (typeof (Exception))]
  84. public void FullPathException ()
  85. {
  86. string s = new TreeNode ("").FullPath;
  87. }
  88. [Test]
  89. public void FullPathTest ()
  90. {
  91. TreeNode tn_1 = new TreeNode ("A");
  92. TreeNode tn_2 = new TreeNode ("B");
  93. tn_2.Nodes.Add (tn_1);
  94. TreeView tv = new TreeView ();
  95. tv.Nodes.Add (tn_1);
  96. tv.Nodes [0].Nodes.Add (tn_2);
  97. Assert.AreEqual ("A", tn_1.FullPath, "#1");
  98. Assert.AreEqual ("A", tv.Nodes[0].FullPath, "#2");
  99. Assert.AreEqual (@"A\B", tn_2.FullPath, "#3");
  100. tv.PathSeparator = "_separator_";
  101. Assert.AreEqual ("A_separator_B", tn_2.FullPath, "#4");
  102. }
  103. [Test]
  104. public void CloneTest ()
  105. {
  106. TreeNode orig = new TreeNode ("text", 2, 3, new TreeNode [] { new TreeNode ("child", 22, 33) });
  107. orig.Tag = FlatStyle.Flat;
  108. orig.Checked = true;
  109. orig.BackColor = System.Drawing.Color.AliceBlue;
  110. orig.ForeColor = System.Drawing.Color.Beige;
  111. TreeNode clone = (TreeNode)orig.Clone ();
  112. Assert.AreEqual ("text", clone.Text, "#1");
  113. Assert.AreEqual (2, clone.ImageIndex, "#2");
  114. Assert.AreEqual (3, clone.SelectedImageIndex, "#3");
  115. Assert.AreEqual (1, clone.Nodes.Count, "#4");
  116. Assert.AreEqual (FlatStyle.Flat, clone.Tag, "#5");
  117. Assert.IsTrue (clone.Checked, "#6");
  118. Assert.AreEqual ("child", clone.Nodes [0].Text, "#10");
  119. Assert.AreEqual (22, clone.Nodes [0].ImageIndex, "#11");
  120. Assert.AreEqual (System.Drawing.Color.AliceBlue, clone.BackColor, "#12");
  121. Assert.AreEqual (System.Drawing.Color.Beige, clone.ForeColor, "#13");
  122. }
  123. }