TreeViewTests.cs 24 KB

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