TreeViewTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. public override string ToString ()
  16. {
  17. return "Factory";
  18. }
  19. };
  20. class Car {
  21. public string Name{get;set;}
  22. public override string ToString ()
  23. {
  24. return Name;
  25. }
  26. };
  27. private TreeView<object> CreateTree()
  28. {
  29. return CreateTree(out _, out _, out _);
  30. }
  31. private TreeView<object> CreateTree(out Factory factory1, out Car car1, out Car car2)
  32. {
  33. car1 = new Car();
  34. car2 = new Car();
  35. factory1 = new Factory()
  36. {
  37. Cars = new []{car1 ,car2}
  38. };
  39. var tree = new TreeView<object>(new DelegateTreeBuilder<object>((s)=> s is Factory f ? f.Cars: null));
  40. tree.AddObject(factory1);
  41. return tree;
  42. }
  43. #endregion
  44. /// <summary>
  45. /// Tests that <see cref="TreeView.Expand(object)"/> and <see cref="TreeView.IsExpanded(object)"/> are consistent
  46. /// </summary>
  47. [Fact]
  48. public void IsExpanded_TrueAfterExpand()
  49. {
  50. var tree = CreateTree(out Factory f, out _, out _);
  51. Assert.False(tree.IsExpanded(f));
  52. tree.Expand(f);
  53. Assert.True(tree.IsExpanded(f));
  54. tree.Collapse(f);
  55. Assert.False(tree.IsExpanded(f));
  56. }
  57. [Fact]
  58. public void EmptyTreeView_ContentSizes()
  59. {
  60. var emptyTree = new TreeView();
  61. Assert.Equal(0,emptyTree.ContentHeight);
  62. Assert.Equal(0,emptyTree.GetContentWidth(true));
  63. Assert.Equal(0,emptyTree.GetContentWidth(false));
  64. }
  65. [Fact]
  66. public void EmptyTreeViewGeneric_ContentSizes()
  67. {
  68. var emptyTree = new TreeView<string>();
  69. Assert.Equal(0,emptyTree.ContentHeight);
  70. Assert.Equal(0,emptyTree.GetContentWidth(true));
  71. Assert.Equal(0,emptyTree.GetContentWidth(false));
  72. }
  73. /// <summary>
  74. /// Tests that <see cref="TreeView.Expand(object)"/> results in a correct content height
  75. /// </summary>
  76. [Fact]
  77. public void ContentHeight_BiggerAfterExpand()
  78. {
  79. var tree = CreateTree(out Factory f, out _, out _);
  80. Assert.Equal(1,tree.ContentHeight);
  81. tree.Expand(f);
  82. Assert.Equal(3,tree.ContentHeight);
  83. tree.Collapse(f);
  84. Assert.Equal(1,tree.ContentHeight);
  85. }
  86. [Fact]
  87. public void ContentWidth_BiggerAfterExpand()
  88. {
  89. var tree = CreateTree(out Factory f, out Car car1, out _);
  90. tree.Bounds = new Rect(0,0,10,10);
  91. var driver = new FakeDriver ();
  92. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  93. driver.Init (() => { });
  94. //-+Factory
  95. Assert.Equal(9,tree.GetContentWidth(true));
  96. car1.Name = "123456789";
  97. tree.Expand(f);
  98. //..├-123456789
  99. Assert.Equal(13,tree.GetContentWidth(true));
  100. tree.Collapse(f);
  101. //-+Factory
  102. Assert.Equal(9,tree.GetContentWidth(true));
  103. }
  104. [Fact]
  105. public void ContentWidth_VisibleVsAll()
  106. {
  107. var tree = CreateTree(out Factory f, out Car car1, out Car car2);
  108. // control only allows 1 row to be viewed at once
  109. tree.Bounds = new Rect(0,0,20,1);
  110. var driver = new FakeDriver ();
  111. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  112. driver.Init (() => { });
  113. //-+Factory
  114. Assert.Equal(9,tree.GetContentWidth(true));
  115. Assert.Equal(9,tree.GetContentWidth(false));
  116. car1.Name = "123456789";
  117. car2.Name = "12345678";
  118. tree.Expand(f);
  119. // Although expanded the bigger (longer) child node is not in the rendered area of the control
  120. Assert.Equal(9,tree.GetContentWidth(true));
  121. Assert.Equal(13,tree.GetContentWidth(false)); // If you ask for the global max width it includes the longer child
  122. // Now that we have scrolled down 1 row we should see the big child
  123. tree.ScrollOffsetVertical = 1;
  124. Assert.Equal(13,tree.GetContentWidth(true));
  125. Assert.Equal(13,tree.GetContentWidth(false));
  126. // Scroll down so only car2 is visible
  127. tree.ScrollOffsetVertical = 2;
  128. Assert.Equal(12,tree.GetContentWidth(true));
  129. Assert.Equal(13,tree.GetContentWidth(false));
  130. // Scroll way down (off bottom of control even)
  131. tree.ScrollOffsetVertical = 5;
  132. Assert.Equal(0,tree.GetContentWidth(true));
  133. Assert.Equal(13,tree.GetContentWidth(false));
  134. }
  135. /// <summary>
  136. /// 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)
  137. /// </summary>
  138. [Fact]
  139. public void IsExpanded_FalseIfCannotExpand()
  140. {
  141. var tree = CreateTree(out Factory f, out Car c, out _);
  142. // expose the car by expanding the factory
  143. tree.Expand(f);
  144. // car is not expanded
  145. Assert.False(tree.IsExpanded(c));
  146. //try to expand the car (should have no effect because cars have no children)
  147. tree.Expand(c);
  148. Assert.False(tree.IsExpanded(c));
  149. // should also be ignored
  150. tree.Collapse(c);
  151. Assert.False(tree.IsExpanded(c));
  152. }
  153. /// <summary>
  154. /// Tests illegal ranges for <see cref="TreeView.ScrollOffset"/>
  155. /// </summary>
  156. [Fact]
  157. public void ScrollOffset_CannotBeNegative()
  158. {
  159. var tree = CreateTree();
  160. Assert.Equal(0,tree.ScrollOffsetVertical);
  161. tree.ScrollOffsetVertical = -100;
  162. Assert.Equal(0,tree.ScrollOffsetVertical);
  163. tree.ScrollOffsetVertical = 10;
  164. Assert.Equal(10,tree.ScrollOffsetVertical);
  165. }
  166. /// <summary>
  167. /// Tests <see cref="TreeView.GetScrollOffsetOf(object)"/> for objects that are as yet undiscovered by the tree
  168. /// </summary>
  169. [Fact]
  170. public void GetScrollOffsetOf_MinusOneForUnRevealed()
  171. {
  172. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  173. // to start with the tree is collapsed and only knows about the root object
  174. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  175. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  176. Assert.Equal(-1,tree.GetScrollOffsetOf(c2));
  177. // reveal it by expanding the root object
  178. tree.Expand(f);
  179. // tree now knows about children
  180. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  181. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  182. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  183. // after collapsing the root node again
  184. tree.Collapse(f);
  185. // tree no longer knows about the locations of these objects
  186. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  187. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  188. Assert.Equal(-1,tree.GetScrollOffsetOf(c2));
  189. }
  190. /// <summary>
  191. /// 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)"/>
  192. /// </summary>
  193. [Fact]
  194. public void RefreshObject_ChildRemoved()
  195. {
  196. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  197. //reveal it by expanding the root object
  198. tree.Expand(f);
  199. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  200. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  201. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  202. // Factory now no longer makes Car c1 (only c2)
  203. f.Cars = new Car[]{c2};
  204. // Tree does not know this yet
  205. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  206. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  207. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  208. // If the user has selected the node c1
  209. tree.SelectedObject = c1;
  210. // When we refresh the tree
  211. tree.RefreshObject(f);
  212. // Now tree knows that factory has only one child node c2
  213. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  214. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  215. Assert.Equal(1,tree.GetScrollOffsetOf(c2));
  216. // The old selection was c1 which is now gone so selection should default to the parent of that branch (the factory)
  217. Assert.Equal(f,tree.SelectedObject);
  218. }
  219. /// <summary>
  220. /// Tests that <see cref="TreeView.GetParent(object)"/> returns the parent object for
  221. /// Cars (Factories). 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 GetParent_ReturnsParentOnlyWhenExpanded()
  226. {
  227. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  228. Assert.Null(tree.GetParent(f));
  229. Assert.Null(tree.GetParent(c1));
  230. Assert.Null(tree.GetParent(c2));
  231. // now when we expand the factory we discover the cars
  232. tree.Expand(f);
  233. Assert.Null(tree.GetParent(f));
  234. Assert.Equal(f,tree.GetParent(c1));
  235. Assert.Equal(f,tree.GetParent(c2));
  236. tree.Collapse(f);
  237. Assert.Null(tree.GetParent(f));
  238. Assert.Null(tree.GetParent(c1));
  239. Assert.Null(tree.GetParent(c2));
  240. }
  241. /// <summary>
  242. /// Tests how the tree adapts to changes in the ChildrenGetter delegate during runtime
  243. /// when some branches are expanded and the new delegate returns children for a node that
  244. /// previously didn't have any children
  245. /// </summary>
  246. [Fact]
  247. public void RefreshObject_AfterChangingChildrenGetterDuringRuntime()
  248. {
  249. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  250. string wheel = "Shiny Wheel";
  251. // Expand the Factory
  252. tree.Expand(f);
  253. // c1 cannot have children
  254. Assert.Equal(f,tree.GetParent(c1));
  255. // expanding it does nothing
  256. tree.Expand(c1);
  257. Assert.False(tree.IsExpanded(c1));
  258. // change the children getter so that now cars can have wheels
  259. tree.TreeBuilder = new DelegateTreeBuilder<object>((o)=>
  260. // factories have cars
  261. o is Factory ? new object[]{c1,c2}
  262. // cars have wheels
  263. : new object[]{wheel });
  264. // still cannot expand
  265. tree.Expand(c1);
  266. Assert.False(tree.IsExpanded(c1));
  267. tree.RefreshObject(c1);
  268. tree.Expand(c1);
  269. Assert.True(tree.IsExpanded(c1));
  270. Assert.Equal(wheel,tree.GetChildren(c1).FirstOrDefault());
  271. }
  272. /// <summary>
  273. /// Same as <see cref="RefreshObject_AfterChangingChildrenGetterDuringRuntime"/> but
  274. /// uses <see cref="TreeView.RebuildTree()"/> instead of <see cref="TreeView.RefreshObject(object, bool)"/>
  275. /// </summary>
  276. [Fact]
  277. public void RebuildTree_AfterChangingChildrenGetterDuringRuntime()
  278. {
  279. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  280. string wheel = "Shiny Wheel";
  281. // Expand the Factory
  282. tree.Expand(f);
  283. // c1 cannot have children
  284. Assert.Equal(f,tree.GetParent(c1));
  285. // expanding it does nothing
  286. tree.Expand(c1);
  287. Assert.False(tree.IsExpanded(c1));
  288. // change the children getter so that now cars can have wheels
  289. tree.TreeBuilder = new DelegateTreeBuilder<object>((o)=>
  290. // factories have cars
  291. o is Factory ? new object[]{c1,c2}
  292. // cars have wheels
  293. : new object[]{wheel });
  294. // still cannot expand
  295. tree.Expand(c1);
  296. Assert.False(tree.IsExpanded(c1));
  297. // Rebuild the tree
  298. tree.RebuildTree();
  299. // Rebuild should not have collapsed any branches or done anything wierd
  300. Assert.True(tree.IsExpanded(f));
  301. tree.Expand(c1);
  302. Assert.True(tree.IsExpanded(c1));
  303. Assert.Equal(wheel,tree.GetChildren(c1).FirstOrDefault());
  304. }
  305. /// <summary>
  306. /// Tests that <see cref="TreeView.GetChildren(object)"/> returns the child objects for
  307. /// the factory. Note that the method only works once the parent branch (Factory)
  308. /// is expanded to expose the child (Car)
  309. /// </summary>
  310. [Fact]
  311. public void GetChildren_ReturnsChildrenOnlyWhenExpanded()
  312. {
  313. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  314. Assert.Empty(tree.GetChildren(f));
  315. Assert.Empty(tree.GetChildren(c1));
  316. Assert.Empty(tree.GetChildren(c2));
  317. // now when we expand the factory we discover the cars
  318. tree.Expand(f);
  319. Assert.Contains(c1,tree.GetChildren(f));
  320. Assert.Contains(c2,tree.GetChildren(f));
  321. Assert.Empty(tree.GetChildren(c1));
  322. Assert.Empty(tree.GetChildren(c2));
  323. tree.Collapse(f);
  324. Assert.Empty(tree.GetChildren(f));
  325. Assert.Empty(tree.GetChildren(c1));
  326. Assert.Empty(tree.GetChildren(c2));
  327. }
  328. [Fact]
  329. public void TreeNode_WorksWithoutDelegate()
  330. {
  331. var tree = new TreeView();
  332. var root = new TreeNode("Root");
  333. root.Children.Add(new TreeNode("Leaf1"));
  334. root.Children.Add(new TreeNode("Leaf2"));
  335. tree.AddObject(root);
  336. tree.Expand(root);
  337. Assert.Equal(2,tree.GetChildren(root).Count());
  338. }
  339. [Fact]
  340. public void MultiSelect_GetAllSelectedObjects()
  341. {
  342. var tree = new TreeView();
  343. TreeNode l1;
  344. TreeNode l2;
  345. TreeNode l3;
  346. TreeNode l4;
  347. var root = new TreeNode("Root");
  348. root.Children.Add(l1 = new TreeNode("Leaf1"));
  349. root.Children.Add(l2 = new TreeNode("Leaf2"));
  350. root.Children.Add(l3 = new TreeNode("Leaf3"));
  351. root.Children.Add(l4 = new TreeNode("Leaf4"));
  352. tree.AddObject(root);
  353. tree.MultiSelect = true;
  354. tree.Expand(root);
  355. Assert.Empty(tree.GetAllSelectedObjects());
  356. tree.SelectedObject = root;
  357. Assert.Equal(1,tree.GetAllSelectedObjects().Count());
  358. Assert.Contains(root,tree.GetAllSelectedObjects());
  359. // move selection down 1
  360. tree.AdjustSelection(1,false);
  361. Assert.Equal(1,tree.GetAllSelectedObjects().Count());
  362. Assert.Contains(l1,tree.GetAllSelectedObjects());
  363. // expand selection down 2 (e.g. shift down twice)
  364. tree.AdjustSelection(1,true);
  365. tree.AdjustSelection(1,true);
  366. Assert.Equal(3,tree.GetAllSelectedObjects().Count());
  367. Assert.Contains(l1,tree.GetAllSelectedObjects());
  368. Assert.Contains(l2,tree.GetAllSelectedObjects());
  369. Assert.Contains(l3,tree.GetAllSelectedObjects());
  370. tree.Collapse(root);
  371. // No selected objects since the root was collapsed
  372. Assert.Empty(tree.GetAllSelectedObjects());
  373. }
  374. /// <summary>
  375. /// 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)"/>
  376. /// </summary>
  377. [Fact]
  378. public void RefreshObject_EqualityTest()
  379. {
  380. var obj1 = new EqualityTestObject(){Name="Bob",Age=1 };
  381. var obj2 = new EqualityTestObject(){Name="Bob",Age=2 };;
  382. string root = "root";
  383. var tree = new TreeView<object>();
  384. tree.TreeBuilder = new DelegateTreeBuilder<object>((s)=> ReferenceEquals(s , root) ? new object[]{obj1 } : null);
  385. tree.AddObject(root);
  386. // Tree is not expanded so the root has no children yet
  387. Assert.Empty(tree.GetChildren(root));
  388. tree.Expand(root);
  389. // now that the tree is expanded we should get our child returned
  390. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj1,child)));
  391. // change the getter to return an Equal object (but not the same reference - obj2)
  392. tree.TreeBuilder = new DelegateTreeBuilder<object>((s)=> ReferenceEquals(s , root) ? new object[]{obj2 } : null);
  393. // tree has cached the knowledge of what children the root has so won't know about the change (we still get obj1)
  394. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj1,child)));
  395. // now that we refresh the root we should get the new child reference (obj2)
  396. tree.RefreshObject(root);
  397. Assert.Equal(1,tree.GetChildren(root).Count(child=>ReferenceEquals(obj2,child)));
  398. }
  399. /// <summary>
  400. /// Test object which considers for equality only <see cref="Name"/>
  401. /// </summary>
  402. private class EqualityTestObject
  403. {
  404. public string Name { get;set;}
  405. public int Age { get;set;}
  406. public override int GetHashCode ()
  407. {
  408. return Name?.GetHashCode()??base.GetHashCode ();
  409. }
  410. public override bool Equals (object obj)
  411. {
  412. return obj is EqualityTestObject eto && Equals(Name, eto.Name);
  413. }
  414. }
  415. }
  416. }