TreeViewTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 GoToEnd_ShouldNotFailOnEmptyTreeView ()
  419. {
  420. var tree = new TreeView ();
  421. var exception = Record.Exception (() => tree.GoToEnd ());
  422. Assert.Null (exception);
  423. }
  424. [Fact]
  425. public void ObjectActivated_CustomKey ()
  426. {
  427. var tree = CreateTree (out Factory f, out Car car1, out _);
  428. InitFakeDriver ();
  429. tree.ObjectActivationKey = Key.Delete;
  430. object activated = null;
  431. bool called = false;
  432. // register for the event
  433. tree.ObjectActivated += (s) => {
  434. activated = s.ActivatedObject;
  435. called = true;
  436. };
  437. Assert.False (called);
  438. // no object is selected yet so no event should happen
  439. tree.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  440. Assert.Null (activated);
  441. Assert.False (called);
  442. // down to select factory
  443. tree.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  444. tree.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  445. // Enter is not the activation key in this unit test
  446. Assert.Null (activated);
  447. Assert.False (called);
  448. // Delete is the activation key in this test so should result in activation occurring
  449. tree.ProcessKey (new KeyEvent (Key.Delete, new KeyModifiers ()));
  450. Assert.True (called);
  451. Assert.Same (f, activated);
  452. Application.Shutdown ();
  453. }
  454. [Fact]
  455. public void ObjectActivationButton_DoubleClick ()
  456. {
  457. var tree = CreateTree (out Factory f, out Car car1, out _);
  458. InitFakeDriver ();
  459. object activated = null;
  460. bool called = false;
  461. // register for the event
  462. tree.ObjectActivated += (s) => {
  463. activated = s.ActivatedObject;
  464. called = true;
  465. };
  466. Assert.False (called);
  467. // double click triggers activation
  468. tree.MouseEvent (new MouseEvent () { Y = 0, Flags = MouseFlags.Button1DoubleClicked });
  469. Assert.True (called);
  470. Assert.Same (f, activated);
  471. Assert.Same (f, tree.SelectedObject);
  472. Application.Shutdown ();
  473. }
  474. [Fact]
  475. public void ObjectActivationButton_SetToNull ()
  476. {
  477. var tree = CreateTree (out Factory f, out Car car1, out _);
  478. InitFakeDriver ();
  479. // disable activation
  480. tree.ObjectActivationButton = null;
  481. object activated = null;
  482. bool called = false;
  483. // register for the event
  484. tree.ObjectActivated += (s) => {
  485. activated = s.ActivatedObject;
  486. called = true;
  487. };
  488. Assert.False (called);
  489. // double click does nothing because we changed button to null
  490. tree.MouseEvent (new MouseEvent () { Y = 0, Flags = MouseFlags.Button1DoubleClicked });
  491. Assert.False (called);
  492. Assert.Null (activated);
  493. Assert.Null (tree.SelectedObject);
  494. Application.Shutdown ();
  495. }
  496. [Fact]
  497. public void ObjectActivationButton_RightClick ()
  498. {
  499. var tree = CreateTree (out Factory f, out Car car1, out _);
  500. InitFakeDriver ();
  501. tree.ObjectActivationButton = MouseFlags.Button2Clicked;
  502. tree.ExpandAll ();
  503. object activated = null;
  504. bool called = false;
  505. // register for the event
  506. tree.ObjectActivated += (s) => {
  507. activated = s.ActivatedObject;
  508. called = true;
  509. };
  510. Assert.False (called);
  511. // double click does nothing because we changed button binding to right click
  512. tree.MouseEvent (new MouseEvent () { Y = 1, Flags = MouseFlags.Button1DoubleClicked });
  513. Assert.Null (activated);
  514. Assert.False (called);
  515. tree.MouseEvent (new MouseEvent () { Y = 1, Flags = MouseFlags.Button2Clicked });
  516. Assert.True (called);
  517. Assert.Same (car1, activated);
  518. Assert.Same (car1, tree.SelectedObject);
  519. Application.Shutdown ();
  520. }
  521. /// <summary>
  522. /// 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)"/>
  523. /// </summary>
  524. [Fact]
  525. public void RefreshObject_EqualityTest ()
  526. {
  527. var obj1 = new EqualityTestObject () { Name = "Bob", Age = 1 };
  528. var obj2 = new EqualityTestObject () { Name = "Bob", Age = 2 }; ;
  529. string root = "root";
  530. var tree = new TreeView<object> ();
  531. tree.TreeBuilder = new DelegateTreeBuilder<object> ((s) => ReferenceEquals (s, root) ? new object [] { obj1 } : null);
  532. tree.AddObject (root);
  533. // Tree is not expanded so the root has no children yet
  534. Assert.Empty (tree.GetChildren (root));
  535. tree.Expand (root);
  536. // now that the tree is expanded we should get our child returned
  537. Assert.Equal (1, tree.GetChildren (root).Count (child => ReferenceEquals (obj1, child)));
  538. // change the getter to return an Equal object (but not the same reference - obj2)
  539. tree.TreeBuilder = new DelegateTreeBuilder<object> ((s) => ReferenceEquals (s, root) ? new object [] { obj2 } : null);
  540. // tree has cached the knowledge of what children the root has so won't know about the change (we still get obj1)
  541. Assert.Equal (1, tree.GetChildren (root).Count (child => ReferenceEquals (obj1, child)));
  542. // now that we refresh the root we should get the new child reference (obj2)
  543. tree.RefreshObject (root);
  544. Assert.Equal (1, tree.GetChildren (root).Count (child => ReferenceEquals (obj2, child)));
  545. }
  546. [Fact, AutoInitShutdown]
  547. public void TestGetObjectOnRow ()
  548. {
  549. var tv = new TreeView { Width = 20, Height = 10 };
  550. var n1 = new TreeNode ("normal");
  551. var n1_1 = new TreeNode ("pink");
  552. var n1_2 = new TreeNode ("normal");
  553. n1.Children.Add (n1_1);
  554. n1.Children.Add (n1_2);
  555. var n2 = new TreeNode ("pink");
  556. tv.AddObject (n1);
  557. tv.AddObject (n2);
  558. tv.Expand (n1);
  559. tv.ColorScheme = new ColorScheme ();
  560. tv.Redraw (tv.Bounds);
  561. TestHelpers.AssertDriverContentsAre (
  562. @"├-normal
  563. │ ├─pink
  564. │ └─normal
  565. └─pink
  566. ", output);
  567. Assert.Same (n1, tv.GetObjectOnRow (0));
  568. Assert.Same (n1_1, tv.GetObjectOnRow (1));
  569. Assert.Same (n1_2, tv.GetObjectOnRow (2));
  570. Assert.Same (n2, tv.GetObjectOnRow (3));
  571. Assert.Null (tv.GetObjectOnRow (4));
  572. tv.Collapse (n1);
  573. tv.Redraw (tv.Bounds);
  574. TestHelpers.AssertDriverContentsAre (
  575. @"├+normal
  576. └─pink
  577. ", output);
  578. Assert.Same (n1, tv.GetObjectOnRow (0));
  579. Assert.Same (n2, tv.GetObjectOnRow (1));
  580. Assert.Null (tv.GetObjectOnRow (2));
  581. Assert.Null (tv.GetObjectOnRow (3));
  582. Assert.Null (tv.GetObjectOnRow (4));
  583. }
  584. [Fact, AutoInitShutdown]
  585. public void TestGetObjectRow ()
  586. {
  587. var tv = new TreeView { Width = 20, Height = 10 };
  588. var n1 = new TreeNode ("normal");
  589. var n1_1 = new TreeNode ("pink");
  590. var n1_2 = new TreeNode ("normal");
  591. n1.Children.Add (n1_1);
  592. n1.Children.Add (n1_2);
  593. var n2 = new TreeNode ("pink");
  594. tv.AddObject (n1);
  595. tv.AddObject (n2);
  596. tv.Expand (n1);
  597. tv.ColorScheme = new ColorScheme ();
  598. tv.Redraw (tv.Bounds);
  599. TestHelpers.AssertDriverContentsAre (
  600. @"├-normal
  601. │ ├─pink
  602. │ └─normal
  603. └─pink
  604. ", output);
  605. Assert.Equal (0, tv.GetObjectRow (n1));
  606. Assert.Equal (1, tv.GetObjectRow (n1_1));
  607. Assert.Equal (2, tv.GetObjectRow (n1_2));
  608. Assert.Equal (3, tv.GetObjectRow (n2));
  609. tv.Collapse (n1);
  610. tv.Redraw (tv.Bounds);
  611. TestHelpers.AssertDriverContentsAre (
  612. @"├+normal
  613. └─pink
  614. ", output);
  615. Assert.Equal (0, tv.GetObjectRow (n1));
  616. Assert.Null (tv.GetObjectRow (n1_1));
  617. Assert.Null (tv.GetObjectRow (n1_2));
  618. Assert.Equal (1, tv.GetObjectRow (n2));
  619. // scroll down 1
  620. tv.ScrollOffsetVertical = 1;
  621. tv.Redraw (tv.Bounds);
  622. TestHelpers.AssertDriverContentsAre (
  623. @"└─pink
  624. ", output);
  625. Assert.Equal (-1, tv.GetObjectRow (n1));
  626. Assert.Null (tv.GetObjectRow (n1_1));
  627. Assert.Null (tv.GetObjectRow (n1_2));
  628. Assert.Equal (0, tv.GetObjectRow (n2));
  629. }
  630. [Fact, AutoInitShutdown]
  631. public void TestTreeViewColor ()
  632. {
  633. var tv = new TreeView { Width = 20, Height = 10 };
  634. var n1 = new TreeNode ("normal");
  635. var n1_1 = new TreeNode ("pink");
  636. var n1_2 = new TreeNode ("normal");
  637. n1.Children.Add (n1_1);
  638. n1.Children.Add (n1_2);
  639. var n2 = new TreeNode ("pink");
  640. tv.AddObject (n1);
  641. tv.AddObject (n2);
  642. tv.Expand (n1);
  643. var pink = new Attribute (Color.Magenta, Color.Black);
  644. var hotpink = new Attribute (Color.BrightMagenta, Color.Black);
  645. tv.ColorScheme = new ColorScheme ();
  646. tv.Redraw (tv.Bounds);
  647. // Normal drawing of the tree view
  648. TestHelpers.AssertDriverContentsAre(
  649. @"├-normal
  650. │ ├─pink
  651. │ └─normal
  652. └─pink
  653. ", output);
  654. // Should all be the same color
  655. TestHelpers.AssertDriverColorsAre(
  656. @"00000000
  657. 00000000
  658. 0000000000
  659. 000000
  660. ",
  661. new [] { tv.ColorScheme.Normal, pink });
  662. // create a new color scheme
  663. var pinkScheme = new ColorScheme {
  664. Normal = pink,
  665. Focus = hotpink
  666. };
  667. // and a delegate that uses the pink color scheme
  668. // for nodes "pink"
  669. tv.ColorGetter = (n) => n.Text.Equals ("pink") ? pinkScheme : null;
  670. // redraw now that the custom color
  671. // delegate is registered
  672. tv.Redraw (tv.Bounds);
  673. // Same text
  674. TestHelpers.AssertDriverContentsAre(
  675. @"├-normal
  676. │ ├─pink
  677. │ └─normal
  678. └─pink
  679. ", output);
  680. // but now the item (only not lines) appear
  681. // in pink when they are the word "pink"
  682. TestHelpers.AssertDriverColorsAre(
  683. @"00000000
  684. 00001111
  685. 0000000000
  686. 001111
  687. ",
  688. new [] { tv.ColorScheme.Normal, pink });
  689. }
  690. [Fact, AutoInitShutdown]
  691. public void DesiredCursorVisibility_MultiSelect ()
  692. {
  693. var tv = new TreeView { Width = 20, Height = 10 };
  694. var n1 = new TreeNode ("normal");
  695. var n2 = new TreeNode ("pink");
  696. tv.AddObject (n1);
  697. tv.AddObject (n2);
  698. Application.Top.Add (tv);
  699. Application.Begin (Application.Top);
  700. Assert.True (tv.MultiSelect);
  701. Assert.True (tv.HasFocus);
  702. Assert.Equal (CursorVisibility.Invisible, tv.DesiredCursorVisibility);
  703. tv.SelectAll ();
  704. tv.DesiredCursorVisibility = CursorVisibility.Default;
  705. Application.Refresh ();
  706. Application.Driver.GetCursorVisibility (out CursorVisibility visibility);
  707. Assert.Equal (CursorVisibility.Default, tv.DesiredCursorVisibility);
  708. Assert.Equal (CursorVisibility.Default, visibility);
  709. }
  710. /// <summary>
  711. /// Test object which considers for equality only <see cref="Name"/>
  712. /// </summary>
  713. private class EqualityTestObject {
  714. public string Name { get; set; }
  715. public int Age { get; set; }
  716. public override int GetHashCode ()
  717. {
  718. return Name?.GetHashCode () ?? base.GetHashCode ();
  719. }
  720. public override bool Equals (object obj)
  721. {
  722. return obj is EqualityTestObject eto && Equals (Name, eto.Name);
  723. }
  724. }
  725. private void InitFakeDriver ()
  726. {
  727. var driver = new FakeDriver ();
  728. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  729. driver.Init (() => { });
  730. }
  731. }
  732. }