TreeViewTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /// <summary>
  134. /// Tests that <see cref="TreeView.GetParent(object)"/> returns the parent object for
  135. /// Cars (Factories). Note that the method only works once the parent branch (Factory)
  136. /// is expanded to expose the child (Car)
  137. /// </summary>
  138. [Fact]
  139. public void GetParent_ReturnsParentOnlyWhenExpanded()
  140. {
  141. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  142. Assert.Null(tree.GetParent(f));
  143. Assert.Null(tree.GetParent(c1));
  144. Assert.Null(tree.GetParent(c2));
  145. // now when we expand the factory we discover the cars
  146. tree.Expand(f);
  147. Assert.Null(tree.GetParent(f));
  148. Assert.Equal(f,tree.GetParent(c1));
  149. Assert.Equal(f,tree.GetParent(c2));
  150. tree.Collapse(f);
  151. Assert.Null(tree.GetParent(f));
  152. Assert.Null(tree.GetParent(c1));
  153. Assert.Null(tree.GetParent(c2));
  154. }
  155. /// <summary>
  156. /// Tests how the tree adapts to changes in the ChildrenGetter delegate during runtime
  157. /// when some branches are expanded and the new delegate returns children for a node that
  158. /// previously didn't have any children
  159. /// </summary>
  160. [Fact]
  161. public void RefreshObject_AfterChangingChildrenGetterDuringRuntime()
  162. {
  163. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  164. string wheel = "Shiny Wheel";
  165. // Expand the Factory
  166. tree.Expand(f);
  167. // c1 cannot have children
  168. Assert.Equal(f,tree.GetParent(c1));
  169. // expanding it does nothing
  170. tree.Expand(c1);
  171. Assert.False(tree.IsExpanded(c1));
  172. // change the children getter so that now cars can have wheels
  173. tree.ChildrenGetter = (o)=>
  174. // factories have cars
  175. o is Factory ? new object[]{c1,c2}
  176. // cars have wheels
  177. : new object[]{wheel };
  178. // still cannot expand
  179. tree.Expand(c1);
  180. Assert.False(tree.IsExpanded(c1));
  181. tree.RefreshObject(c1);
  182. tree.Expand(c1);
  183. Assert.True(tree.IsExpanded(c1));
  184. Assert.Equal(wheel,tree.GetChildren(c1).FirstOrDefault());
  185. }
  186. /// <summary>
  187. /// Same as <see cref="RefreshObject_AfterChangingChildrenGetterDuringRuntime"/> but
  188. /// uses <see cref="TreeView.RebuildTree()"/> instead of <see cref="TreeView.RefreshObject(object, bool)"/>
  189. /// </summary>
  190. [Fact]
  191. public void RebuildTree_AfterChangingChildrenGetterDuringRuntime()
  192. {
  193. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  194. string wheel = "Shiny Wheel";
  195. // Expand the Factory
  196. tree.Expand(f);
  197. // c1 cannot have children
  198. Assert.Equal(f,tree.GetParent(c1));
  199. // expanding it does nothing
  200. tree.Expand(c1);
  201. Assert.False(tree.IsExpanded(c1));
  202. // change the children getter so that now cars can have wheels
  203. tree.ChildrenGetter = (o)=>
  204. // factories have cars
  205. o is Factory ? new object[]{c1,c2}
  206. // cars have wheels
  207. : new object[]{wheel };
  208. // still cannot expand
  209. tree.Expand(c1);
  210. Assert.False(tree.IsExpanded(c1));
  211. // Rebuild the tree
  212. tree.RebuildTree();
  213. // Rebuild should not have collapsed any branches or done anything wierd
  214. Assert.True(tree.IsExpanded(f));
  215. tree.Expand(c1);
  216. Assert.True(tree.IsExpanded(c1));
  217. Assert.Equal(wheel,tree.GetChildren(c1).FirstOrDefault());
  218. }
  219. /// <summary>
  220. /// Tests that <see cref="TreeView.GetChildren(object)"/> returns the child objects for
  221. /// the factory. Note that the method only works once the parent branch (Factory)
  222. /// is expanded to expose the child (Car)
  223. /// </summary>
  224. [Fact]
  225. public void GetChildren_ReturnsChildrenOnlyWhenExpanded()
  226. {
  227. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  228. Assert.Empty(tree.GetChildren(f));
  229. Assert.Empty(tree.GetChildren(c1));
  230. Assert.Empty(tree.GetChildren(c2));
  231. // now when we expand the factory we discover the cars
  232. tree.Expand(f);
  233. Assert.Contains(c1,tree.GetChildren(f));
  234. Assert.Contains(c2,tree.GetChildren(f));
  235. Assert.Empty(tree.GetChildren(c1));
  236. Assert.Empty(tree.GetChildren(c2));
  237. tree.Collapse(f);
  238. Assert.Empty(tree.GetChildren(f));
  239. Assert.Empty(tree.GetChildren(c1));
  240. Assert.Empty(tree.GetChildren(c2));
  241. }
  242. /// <summary>
  243. /// 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)"/>
  244. /// </summary>
  245. [Fact]
  246. public void RefreshObject_EqualityTest()
  247. {
  248. var obj1 = new EqualityTestObject(){Name="Bob",Age=1 };
  249. var obj2 = new EqualityTestObject(){Name="Bob",Age=2 };;
  250. string root = "root";
  251. var tree = new TreeView();
  252. tree.ChildrenGetter = (s)=> ReferenceEquals(s , root) ? new object[]{obj1 } : null;
  253. tree.AddObject(root);
  254. // Tree is not expanded so the root has no children yet
  255. Assert.Empty(tree.GetChildren(root));
  256. tree.Expand(root);
  257. // now that the tree is expanded we should get our child returned
  258. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj1,child)));
  259. // change the getter to return an Equal object (but not the same reference - obj2)
  260. tree.ChildrenGetter = (s)=> ReferenceEquals(s , root) ? new object[]{obj2 } : null;
  261. // tree has cached the knowledge of what children the root has so won't know about the change (we still get obj1)
  262. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj1,child)));
  263. // now that we refresh the root we should get the new child reference (obj2)
  264. tree.RefreshObject(root);
  265. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj2,child)));
  266. }
  267. /// <summary>
  268. /// Test object which considers for equality only <see cref="Name"/>
  269. /// </summary>
  270. private class EqualityTestObject
  271. {
  272. public string Name { get;set;}
  273. public int Age { get;set;}
  274. public override int GetHashCode ()
  275. {
  276. return Name?.GetHashCode()??base.GetHashCode ();
  277. }
  278. public override bool Equals (object obj)
  279. {
  280. return obj is EqualityTestObject eto && Equals(Name, eto.Name);
  281. }
  282. }
  283. }
  284. }