TreeViewTests.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. namespace UnitTests {
  9. public class TreeViewTests
  10. {
  11. #region Test Setup Methods
  12. class Factory
  13. {
  14. public Car[] Cars {get;set;}
  15. };
  16. class Car {
  17. };
  18. private TreeView CreateTree()
  19. {
  20. return CreateTree(out _, out _, out _);
  21. }
  22. private TreeView CreateTree(out Factory factory1, out Car car1, out Car car2)
  23. {
  24. car1 = new Car();
  25. car2 = new Car();
  26. factory1 = new Factory()
  27. {
  28. Cars = new []{car1 ,car2}
  29. };
  30. var tree = new TreeView();
  31. tree.ChildrenGetter = (s)=> s is Factory f ? f.Cars: null;
  32. tree.AddObject(factory1);
  33. return tree;
  34. }
  35. #endregion
  36. /// <summary>
  37. /// Tests that <see cref="TreeView.Expand(object)"/> and <see cref="TreeView.IsExpanded(object)"/> are consistent
  38. /// </summary>
  39. [Fact]
  40. public void IsExpanded_TrueAfterExpand()
  41. {
  42. var tree = CreateTree(out Factory f, out _, out _);
  43. Assert.False(tree.IsExpanded(f));
  44. tree.Expand(f);
  45. Assert.True(tree.IsExpanded(f));
  46. tree.Collapse(f);
  47. Assert.False(tree.IsExpanded(f));
  48. }
  49. /// <summary>
  50. /// Tests that <see cref="TreeView.IsExpanded(object)"/> and <see cref="TreeView.Expand(object)"/> behaves correctly when an object cannot be expanded (because it has no children)
  51. /// </summary>
  52. [Fact]
  53. public void IsExpanded_FalseIfCannotExpand()
  54. {
  55. var tree = CreateTree(out Factory f, out Car c, out _);
  56. // expose the car by expanding the factory
  57. tree.Expand(f);
  58. // car is not expanded
  59. Assert.False(tree.IsExpanded(c));
  60. //try to expand the car (should have no effect because cars have no children)
  61. tree.Expand(c);
  62. Assert.False(tree.IsExpanded(c));
  63. // should also be ignored
  64. tree.Collapse(c);
  65. Assert.False(tree.IsExpanded(c));
  66. }
  67. /// <summary>
  68. /// Tests illegal ranges for <see cref="TreeView.ScrollOffset"/>
  69. /// </summary>
  70. [Fact]
  71. public void ScrollOffset_CannotBeNegative()
  72. {
  73. var tree = CreateTree();
  74. Assert.Equal(0,tree.ScrollOffset);
  75. tree.ScrollOffset = -100;
  76. Assert.Equal(0,tree.ScrollOffset);
  77. tree.ScrollOffset = 10;
  78. Assert.Equal(10,tree.ScrollOffset);
  79. }
  80. /// <summary>
  81. /// Tests <see cref="TreeView.GetScrollOffsetOf(object)"/> for objects that are as yet undiscovered by the tree
  82. /// </summary>
  83. [Fact]
  84. public void GetScrollOffsetOf_MinusOneForUnRevealed()
  85. {
  86. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  87. // to start with the tree is collapsed and only knows about the root object
  88. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  89. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  90. Assert.Equal(-1,tree.GetScrollOffsetOf(c2));
  91. // reveal it by expanding the root object
  92. tree.Expand(f);
  93. // tree now knows about children
  94. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  95. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  96. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  97. // after collapsing the root node again
  98. tree.Collapse(f);
  99. // tree no longer knows about the locations of these objects
  100. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  101. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  102. Assert.Equal(-1,tree.GetScrollOffsetOf(c2));
  103. }
  104. /// <summary>
  105. /// Simulates behind the scenes changes to an object (which children it has) and how to sync that into the tree using <see cref="TreeView.RefreshObject(object, bool)"/>
  106. /// </summary>
  107. [Fact]
  108. public void RefreshObject_ChildRemoved()
  109. {
  110. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  111. //reveal it by expanding the root object
  112. tree.Expand(f);
  113. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  114. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  115. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  116. // Factory now no longer makes Car c1 (only c2)
  117. f.Cars = new Car[]{c2};
  118. // Tree does not know this yet
  119. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  120. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  121. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  122. // If the user has selected the node c1
  123. tree.SelectedObject = c1;
  124. // When we refresh the tree
  125. tree.RefreshObject(f);
  126. // Now tree knows that factory has only one child node c2
  127. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  128. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  129. Assert.Equal(1,tree.GetScrollOffsetOf(c2));
  130. // The old selection was c1 which is now gone so selection should default to the parent of that branch (the factory)
  131. Assert.Equal(f,tree.SelectedObject);
  132. }
  133. [Fact]
  134. public void GetParent_ReturnsParentOnlyWhenExpanded()
  135. {
  136. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  137. Assert.Null(tree.GetParent(f));
  138. Assert.Null(tree.GetParent(c1));
  139. Assert.Null(tree.GetParent(c2));
  140. // now when we expand the factory we discover the cars
  141. tree.Expand(f);
  142. Assert.Null(tree.GetParent(f));
  143. Assert.Equal(f,tree.GetParent(c1));
  144. Assert.Equal(f,tree.GetParent(c2));
  145. tree.Collapse(f);
  146. Assert.Null(tree.GetParent(f));
  147. Assert.Null(tree.GetParent(c1));
  148. Assert.Null(tree.GetParent(c2));
  149. }
  150. [Fact]
  151. public void GetChildren_ReturnsChildrenOnlyWhenExpanded()
  152. {
  153. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  154. Assert.Empty(tree.GetChildren(f));
  155. Assert.Empty(tree.GetChildren(c1));
  156. Assert.Empty(tree.GetChildren(c2));
  157. // now when we expand the factory we discover the cars
  158. tree.Expand(f);
  159. Assert.Contains(c1,tree.GetChildren(f));
  160. Assert.Contains(c2,tree.GetChildren(f));
  161. Assert.Empty(tree.GetChildren(c1));
  162. Assert.Empty(tree.GetChildren(c2));
  163. tree.Collapse(f);
  164. Assert.Empty(tree.GetChildren(f));
  165. Assert.Empty(tree.GetChildren(c1));
  166. Assert.Empty(tree.GetChildren(c2));
  167. }
  168. /// <summary>
  169. /// Simulates behind the scenes changes to an object (which children it has) and how to sync that into the tree using <see cref="TreeView.RefreshObject(object, bool)"/>
  170. /// </summary>
  171. [Fact]
  172. public void RefreshObject_EqualityTest()
  173. {
  174. var obj1 = new EqualityTestObject(){Name="Bob",Age=1 };
  175. var obj2 = new EqualityTestObject(){Name="Bob",Age=2 };;
  176. string root = "root";
  177. var tree = new TreeView();
  178. tree.ChildrenGetter = (s)=> ReferenceEquals(s , root) ? new object[]{obj1 } : null;
  179. tree.AddObject(root);
  180. // Tree is not expanded so the root has no children yet
  181. Assert.Empty(tree.GetChildren(root));
  182. tree.Expand(root);
  183. // now that the tree is expanded we should get our child returned
  184. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj1,child)));
  185. // change the getter to return an Equal object (but not the same reference - obj2)
  186. tree.ChildrenGetter = (s)=> ReferenceEquals(s , root) ? new object[]{obj2 } : null;
  187. // tree has cached the knowledge of what children the root has so won't know about the change (we still get obj1)
  188. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj1,child)));
  189. // now that we refresh the root we should get the new child reference (obj2)
  190. tree.RefreshObject(root);
  191. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj2,child)));
  192. }
  193. /// <summary>
  194. /// Test object which considers for equality only <see cref="Name"/>
  195. /// </summary>
  196. private class EqualityTestObject
  197. {
  198. public string Name { get;set;}
  199. public int Age { get;set;}
  200. public override int GetHashCode ()
  201. {
  202. return Name?.GetHashCode()??base.GetHashCode ();
  203. }
  204. public override bool Equals (object obj)
  205. {
  206. return obj is EqualityTestObject eto && Equals(Name, eto.Name);
  207. }
  208. }
  209. }
  210. }