TreeViewTests.cs 25 KB

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