2
0

TreeViewTests.cs 20 KB

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