TreeViewTests.cs 18 KB

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