TreeViewTests.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests {
  6. public class TreeViewTests {
  7. readonly ITestOutputHelper output;
  8. public TreeViewTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. #region Test Setup Methods
  13. class Factory {
  14. public Car [] Cars { get; set; }
  15. public override string ToString ()
  16. {
  17. return "Factory";
  18. }
  19. };
  20. class Car {
  21. public string Name { get; set; }
  22. public override string ToString ()
  23. {
  24. return Name;
  25. }
  26. };
  27. private TreeView<object> CreateTree ()
  28. {
  29. return CreateTree (out _, out _, out _);
  30. }
  31. private TreeView<object> CreateTree (out Factory factory1, out Car car1, out Car car2)
  32. {
  33. car1 = new Car ();
  34. car2 = new Car ();
  35. factory1 = new Factory () {
  36. Cars = new [] { car1, car2 }
  37. };
  38. var tree = new TreeView<object> (new DelegateTreeBuilder<object> ((s) => s is Factory f ? f.Cars : null));
  39. tree.AddObject (factory1);
  40. return tree;
  41. }
  42. #endregion
  43. /// <summary>
  44. /// Tests that <see cref="TreeView.Expand(object)"/> and <see cref="TreeView.IsExpanded(object)"/> are consistent
  45. /// </summary>
  46. [Fact]
  47. public void IsExpanded_TrueAfterExpand ()
  48. {
  49. var tree = CreateTree (out Factory f, out _, out _);
  50. Assert.False (tree.IsExpanded (f));
  51. tree.Expand (f);
  52. Assert.True (tree.IsExpanded (f));
  53. tree.Collapse (f);
  54. Assert.False (tree.IsExpanded (f));
  55. }
  56. [Fact]
  57. public void EmptyTreeView_ContentSizes ()
  58. {
  59. var emptyTree = new TreeView ();
  60. Assert.Equal (0, emptyTree.ContentHeight);
  61. Assert.Equal (0, emptyTree.GetContentWidth (true));
  62. Assert.Equal (0, emptyTree.GetContentWidth (false));
  63. }
  64. [Fact]
  65. public void EmptyTreeViewGeneric_ContentSizes ()
  66. {
  67. var emptyTree = new TreeView<string> ();
  68. Assert.Equal (0, emptyTree.ContentHeight);
  69. Assert.Equal (0, emptyTree.GetContentWidth (true));
  70. Assert.Equal (0, emptyTree.GetContentWidth (false));
  71. }
  72. /// <summary>
  73. /// Tests that <see cref="TreeView.Expand(object)"/> results in a correct content height
  74. /// </summary>
  75. [Fact]
  76. public void ContentHeight_BiggerAfterExpand ()
  77. {
  78. var tree = CreateTree (out Factory f, out _, out _);
  79. Assert.Equal (1, tree.ContentHeight);
  80. tree.Expand (f);
  81. Assert.Equal (3, tree.ContentHeight);
  82. tree.Collapse (f);
  83. Assert.Equal (1, tree.ContentHeight);
  84. }
  85. [Fact]
  86. public void ContentWidth_BiggerAfterExpand ()
  87. {
  88. var tree = CreateTree (out Factory f, out Car car1, out _);
  89. tree.BeginInit (); tree.EndInit ();
  90. tree.Bounds = new Rect (0, 0, 10, 10);
  91. InitFakeDriver ();
  92. //-+Factory
  93. Assert.Equal (9, tree.GetContentWidth (true));
  94. car1.Name = "123456789";
  95. tree.Expand (f);
  96. //..├-123456789
  97. Assert.Equal (13, tree.GetContentWidth (true));
  98. tree.Collapse (f);
  99. //-+Factory
  100. Assert.Equal (9, tree.GetContentWidth (true));
  101. Application.Shutdown ();
  102. }
  103. [Fact]
  104. public void ContentWidth_VisibleVsAll ()
  105. {
  106. var tree = CreateTree (out Factory f, out Car car1, out Car car2);
  107. tree.BeginInit (); tree.EndInit ();
  108. // control only allows 1 row to be viewed at once
  109. tree.Bounds = new Rect (0, 0, 20, 1);
  110. InitFakeDriver ();
  111. //-+Factory
  112. Assert.Equal (9, tree.GetContentWidth (true));
  113. Assert.Equal (9, tree.GetContentWidth (false));
  114. car1.Name = "123456789";
  115. car2.Name = "12345678";
  116. tree.Expand (f);
  117. // Although expanded the bigger (longer) child node is not in the rendered area of the control
  118. Assert.Equal (9, tree.GetContentWidth (true));
  119. Assert.Equal (13, tree.GetContentWidth (false)); // If you ask for the global max width it includes the longer child
  120. // Now that we have scrolled down 1 row we should see the big child
  121. tree.ScrollOffsetVertical = 1;
  122. Assert.Equal (13, tree.GetContentWidth (true));
  123. Assert.Equal (13, tree.GetContentWidth (false));
  124. // Scroll down so only car2 is visible
  125. tree.ScrollOffsetVertical = 2;
  126. Assert.Equal (12, tree.GetContentWidth (true));
  127. Assert.Equal (13, tree.GetContentWidth (false));
  128. // Scroll way down (off bottom of control even)
  129. tree.ScrollOffsetVertical = 5;
  130. Assert.Equal (0, tree.GetContentWidth (true));
  131. Assert.Equal (13, tree.GetContentWidth (false));
  132. Application.Shutdown ();
  133. }
  134. /// <summary>
  135. /// 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)
  136. /// </summary>
  137. [Fact]
  138. public void IsExpanded_FalseIfCannotExpand ()
  139. {
  140. var tree = CreateTree (out Factory f, out Car c, out _);
  141. // expose the car by expanding the factory
  142. tree.Expand (f);
  143. // car is not expanded
  144. Assert.False (tree.IsExpanded (c));
  145. //try to expand the car (should have no effect because cars have no children)
  146. tree.Expand (c);
  147. Assert.False (tree.IsExpanded (c));
  148. // should also be ignored
  149. tree.Collapse (c);
  150. Assert.False (tree.IsExpanded (c));
  151. Application.Shutdown ();
  152. }
  153. /// <summary>
  154. /// Tests illegal ranges for <see cref="TreeView.ScrollOffset"/>
  155. /// </summary>
  156. [Fact]
  157. public void ScrollOffset_CannotBeNegative ()
  158. {
  159. var tree = CreateTree ();
  160. Assert.Equal (0, tree.ScrollOffsetVertical);
  161. tree.ScrollOffsetVertical = -100;
  162. Assert.Equal (0, tree.ScrollOffsetVertical);
  163. tree.ScrollOffsetVertical = 10;
  164. Assert.Equal (10, tree.ScrollOffsetVertical);
  165. }
  166. /// <summary>
  167. /// Tests <see cref="TreeView.GetScrollOffsetOf(object)"/> for objects that are as yet undiscovered by the tree
  168. /// </summary>
  169. [Fact]
  170. public void GetScrollOffsetOf_MinusOneForUnRevealed ()
  171. {
  172. var tree = CreateTree (out Factory f, out Car c1, out Car c2);
  173. // to start with the tree is collapsed and only knows about the root object
  174. Assert.Equal (0, tree.GetScrollOffsetOf (f));
  175. Assert.Equal (-1, tree.GetScrollOffsetOf (c1));
  176. Assert.Equal (-1, tree.GetScrollOffsetOf (c2));
  177. // reveal it by expanding the root object
  178. tree.Expand (f);
  179. // tree now knows about children
  180. Assert.Equal (0, tree.GetScrollOffsetOf (f));
  181. Assert.Equal (1, tree.GetScrollOffsetOf (c1));
  182. Assert.Equal (2, tree.GetScrollOffsetOf (c2));
  183. // after collapsing the root node again
  184. tree.Collapse (f);
  185. // tree no longer knows about the locations of these objects
  186. Assert.Equal (0, tree.GetScrollOffsetOf (f));
  187. Assert.Equal (-1, tree.GetScrollOffsetOf (c1));
  188. Assert.Equal (-1, tree.GetScrollOffsetOf (c2));
  189. }
  190. /// <summary>
  191. /// 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)"/>
  192. /// </summary>
  193. [Fact]
  194. public void RefreshObject_ChildRemoved ()
  195. {
  196. var tree = CreateTree (out Factory f, out Car c1, out Car c2);
  197. //reveal it by expanding the root object
  198. tree.Expand (f);
  199. Assert.Equal (0, tree.GetScrollOffsetOf (f));
  200. Assert.Equal (1, tree.GetScrollOffsetOf (c1));
  201. Assert.Equal (2, tree.GetScrollOffsetOf (c2));
  202. // Factory now no longer makes Car c1 (only c2)
  203. f.Cars = new Car [] { c2 };
  204. // Tree does not know this yet
  205. Assert.Equal (0, tree.GetScrollOffsetOf (f));
  206. Assert.Equal (1, tree.GetScrollOffsetOf (c1));
  207. Assert.Equal (2, tree.GetScrollOffsetOf (c2));
  208. // If the user has selected the node c1
  209. tree.SelectedObject = c1;
  210. // When we refresh the tree
  211. tree.RefreshObject (f);
  212. // Now tree knows that factory has only one child node c2
  213. Assert.Equal (0, tree.GetScrollOffsetOf (f));
  214. Assert.Equal (-1, tree.GetScrollOffsetOf (c1));
  215. Assert.Equal (1, tree.GetScrollOffsetOf (c2));
  216. // The old selection was c1 which is now gone so selection should default to the parent of that branch (the factory)
  217. Assert.Equal (f, tree.SelectedObject);
  218. }
  219. /// <summary>
  220. /// Tests that <see cref="TreeView.GetParent(object)"/> returns the parent object for
  221. /// Cars (Factories). Note that the method only works once the parent branch (Factory)
  222. /// is expanded to expose the child (Car)
  223. /// </summary>
  224. [Fact]
  225. public void GetParent_ReturnsParentOnlyWhenExpanded ()
  226. {
  227. var tree = CreateTree (out Factory f, out Car c1, out Car c2);
  228. Assert.Null (tree.GetParent (f));
  229. Assert.Null (tree.GetParent (c1));
  230. Assert.Null (tree.GetParent (c2));
  231. // now when we expand the factory we discover the cars
  232. tree.Expand (f);
  233. Assert.Null (tree.GetParent (f));
  234. Assert.Equal (f, tree.GetParent (c1));
  235. Assert.Equal (f, tree.GetParent (c2));
  236. tree.Collapse (f);
  237. Assert.Null (tree.GetParent (f));
  238. Assert.Null (tree.GetParent (c1));
  239. Assert.Null (tree.GetParent (c2));
  240. }
  241. /// <summary>
  242. /// Tests how the tree adapts to changes in the ChildrenGetter delegate during runtime
  243. /// when some branches are expanded and the new delegate returns children for a node that
  244. /// previously didn't have any children
  245. /// </summary>
  246. [Fact]
  247. public void RefreshObject_AfterChangingChildrenGetterDuringRuntime ()
  248. {
  249. var tree = CreateTree (out Factory f, out Car c1, out Car c2);
  250. string wheel = "Shiny Wheel";
  251. // Expand the Factory
  252. tree.Expand (f);
  253. // c1 cannot have children
  254. Assert.Equal (f, tree.GetParent (c1));
  255. // expanding it does nothing
  256. tree.Expand (c1);
  257. Assert.False (tree.IsExpanded (c1));
  258. // change the children getter so that now cars can have wheels
  259. tree.TreeBuilder = new DelegateTreeBuilder<object> ((o) =>
  260. // factories have cars
  261. o is Factory ? new object [] { c1, c2 }
  262. // cars have wheels
  263. : new object [] { wheel });
  264. // still cannot expand
  265. tree.Expand (c1);
  266. Assert.False (tree.IsExpanded (c1));
  267. tree.RefreshObject (c1);
  268. tree.Expand (c1);
  269. Assert.True (tree.IsExpanded (c1));
  270. Assert.Equal (wheel, tree.GetChildren (c1).FirstOrDefault ());
  271. }
  272. /// <summary>
  273. /// Same as <see cref="RefreshObject_AfterChangingChildrenGetterDuringRuntime"/> but
  274. /// uses <see cref="TreeView.RebuildTree()"/> instead of <see cref="TreeView.RefreshObject(object, bool)"/>
  275. /// </summary>
  276. [Fact]
  277. public void RebuildTree_AfterChangingChildrenGetterDuringRuntime ()
  278. {
  279. var tree = CreateTree (out Factory f, out Car c1, out Car c2);
  280. string wheel = "Shiny Wheel";
  281. // Expand the Factory
  282. tree.Expand (f);
  283. // c1 cannot have children
  284. Assert.Equal (f, tree.GetParent (c1));
  285. // expanding it does nothing
  286. tree.Expand (c1);
  287. Assert.False (tree.IsExpanded (c1));
  288. // change the children getter so that now cars can have wheels
  289. tree.TreeBuilder = new DelegateTreeBuilder<object> ((o) =>
  290. // factories have cars
  291. o is Factory ? new object [] { c1, c2 }
  292. // cars have wheels
  293. : new object [] { wheel });
  294. // still cannot expand
  295. tree.Expand (c1);
  296. Assert.False (tree.IsExpanded (c1));
  297. // Rebuild the tree
  298. tree.RebuildTree ();
  299. // Rebuild should not have collapsed any branches or done anything wierd
  300. Assert.True (tree.IsExpanded (f));
  301. tree.Expand (c1);
  302. Assert.True (tree.IsExpanded (c1));
  303. Assert.Equal (wheel, tree.GetChildren (c1).FirstOrDefault ());
  304. }
  305. /// <summary>
  306. /// Tests that <see cref="TreeView.GetChildren(object)"/> returns the child objects for
  307. /// the factory. Note that the method only works once the parent branch (Factory)
  308. /// is expanded to expose the child (Car)
  309. /// </summary>
  310. [Fact]
  311. public void GetChildren_ReturnsChildrenOnlyWhenExpanded ()
  312. {
  313. var tree = CreateTree (out Factory f, out Car c1, out Car c2);
  314. Assert.Empty (tree.GetChildren (f));
  315. Assert.Empty (tree.GetChildren (c1));
  316. Assert.Empty (tree.GetChildren (c2));
  317. // now when we expand the factory we discover the cars
  318. tree.Expand (f);
  319. Assert.Contains (c1, tree.GetChildren (f));
  320. Assert.Contains (c2, tree.GetChildren (f));
  321. Assert.Empty (tree.GetChildren (c1));
  322. Assert.Empty (tree.GetChildren (c2));
  323. tree.Collapse (f);
  324. Assert.Empty (tree.GetChildren (f));
  325. Assert.Empty (tree.GetChildren (c1));
  326. Assert.Empty (tree.GetChildren (c2));
  327. }
  328. [Fact]
  329. public void TreeNode_WorksWithoutDelegate ()
  330. {
  331. var tree = new TreeView ();
  332. var root = new TreeNode ("Root");
  333. root.Children.Add (new TreeNode ("Leaf1"));
  334. root.Children.Add (new TreeNode ("Leaf2"));
  335. tree.AddObject (root);
  336. tree.Expand (root);
  337. Assert.Equal (2, tree.GetChildren (root).Count ());
  338. }
  339. [Fact]
  340. public void MultiSelect_GetAllSelectedObjects ()
  341. {
  342. var tree = new TreeView ();
  343. TreeNode l1;
  344. TreeNode l2;
  345. TreeNode l3;
  346. TreeNode l4;
  347. var root = new TreeNode ("Root");
  348. root.Children.Add (l1 = new TreeNode ("Leaf1"));
  349. root.Children.Add (l2 = new TreeNode ("Leaf2"));
  350. root.Children.Add (l3 = new TreeNode ("Leaf3"));
  351. root.Children.Add (l4 = new TreeNode ("Leaf4"));
  352. tree.AddObject (root);
  353. tree.MultiSelect = true;
  354. tree.Expand (root);
  355. Assert.Empty (tree.GetAllSelectedObjects ());
  356. tree.SelectedObject = root;
  357. Assert.Single (tree.GetAllSelectedObjects (), root);
  358. // move selection down 1
  359. tree.AdjustSelection (1, false);
  360. Assert.Single (tree.GetAllSelectedObjects (), l1);
  361. // expand selection down 2 (e.g. shift down twice)
  362. tree.AdjustSelection (1, true);
  363. tree.AdjustSelection (1, true);
  364. Assert.Equal (3, tree.GetAllSelectedObjects ().Count ());
  365. Assert.Contains (l1, tree.GetAllSelectedObjects ());
  366. Assert.Contains (l2, tree.GetAllSelectedObjects ());
  367. Assert.Contains (l3, tree.GetAllSelectedObjects ());
  368. tree.Collapse (root);
  369. // No selected objects since the root was collapsed
  370. Assert.Empty (tree.GetAllSelectedObjects ());
  371. }
  372. [Fact]
  373. public void ObjectActivated_Called ()
  374. {
  375. var tree = CreateTree (out Factory f, out Car car1, out _);
  376. InitFakeDriver ();
  377. object activated = null;
  378. bool called = false;
  379. // register for the event
  380. tree.ObjectActivated += (s, e) => {
  381. activated = e.ActivatedObject;
  382. called = true;
  383. };
  384. Assert.False (called);
  385. // no object is selected yet so no event should happen
  386. tree.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  387. Assert.Null (activated);
  388. Assert.False (called);
  389. // down to select factory
  390. tree.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  391. tree.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  392. Assert.True (called);
  393. Assert.Same (f, activated);
  394. Application.Shutdown ();
  395. }
  396. [Fact]
  397. public void GoTo_OnlyAppliesToExposedObjects ()
  398. {
  399. var tree = CreateTree (out Factory f, out Car car1, out _);
  400. tree.BeginInit (); tree.EndInit ();
  401. // Make tree bounds 1 in height so that EnsureVisible always requires updating scroll offset
  402. tree.Bounds = new Rect (0, 0, 50, 1);
  403. Assert.Null (tree.SelectedObject);
  404. Assert.Equal (0, tree.ScrollOffsetVertical);
  405. // car 1 is not yet exposed
  406. tree.GoTo (car1);
  407. Assert.Null (tree.SelectedObject);
  408. Assert.Equal (0, tree.ScrollOffsetVertical);
  409. tree.Expand (f);
  410. // Car1 is now exposed by expanding the factory
  411. tree.GoTo (car1);
  412. Assert.Equal (car1, tree.SelectedObject);
  413. Assert.Equal (1, tree.ScrollOffsetVertical);
  414. }
  415. [Fact]
  416. public void GoToEnd_ShouldNotFailOnEmptyTreeView ()
  417. {
  418. var tree = new TreeView ();
  419. var exception = Record.Exception (() => tree.GoToEnd ());
  420. Assert.Null (exception);
  421. }
  422. [Fact]
  423. public void ObjectActivated_CustomKey ()
  424. {
  425. var tree = CreateTree (out Factory f, out Car car1, out _);
  426. InitFakeDriver ();
  427. tree.ObjectActivationKey = Key.Delete;
  428. object activated = null;
  429. bool called = false;
  430. // register for the event
  431. tree.ObjectActivated += (s, e) => {
  432. activated = e.ActivatedObject;
  433. called = true;
  434. };
  435. Assert.False (called);
  436. // no object is selected yet so no event should happen
  437. tree.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  438. Assert.Null (activated);
  439. Assert.False (called);
  440. // down to select factory
  441. tree.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  442. tree.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  443. // Enter is not the activation key in this unit test
  444. Assert.Null (activated);
  445. Assert.False (called);
  446. // Delete is the activation key in this test so should result in activation occurring
  447. tree.ProcessKey (new KeyEvent (Key.Delete, new KeyModifiers ()));
  448. Assert.True (called);
  449. Assert.Same (f, activated);
  450. Application.Shutdown ();
  451. }
  452. [Fact]
  453. public void ObjectActivationButton_DoubleClick ()
  454. {
  455. var tree = CreateTree (out Factory f, out Car car1, out _);
  456. InitFakeDriver ();
  457. object activated = null;
  458. bool called = false;
  459. // register for the event
  460. tree.ObjectActivated += (s, e) => {
  461. activated = e.ActivatedObject;
  462. called = true;
  463. };
  464. Assert.False (called);
  465. // double click triggers activation
  466. tree.MouseEvent (new MouseEvent () { Y = 0, Flags = MouseFlags.Button1DoubleClicked });
  467. Assert.True (called);
  468. Assert.Same (f, activated);
  469. Assert.Same (f, tree.SelectedObject);
  470. Application.Shutdown ();
  471. }
  472. [Fact]
  473. public void ObjectActivationButton_SetToNull ()
  474. {
  475. var tree = CreateTree (out Factory f, out Car car1, out _);
  476. InitFakeDriver ();
  477. // disable activation
  478. tree.ObjectActivationButton = null;
  479. object activated = null;
  480. bool called = false;
  481. // register for the event
  482. tree.ObjectActivated += (s, e) => {
  483. activated = e.ActivatedObject;
  484. called = true;
  485. };
  486. Assert.False (called);
  487. // double click does nothing because we changed button to null
  488. tree.MouseEvent (new MouseEvent () { Y = 0, Flags = MouseFlags.Button1DoubleClicked });
  489. Assert.False (called);
  490. Assert.Null (activated);
  491. Assert.Null (tree.SelectedObject);
  492. Application.Shutdown ();
  493. }
  494. [Fact]
  495. public void ObjectActivationButton_RightClick ()
  496. {
  497. var tree = CreateTree (out Factory f, out Car car1, out _);
  498. InitFakeDriver ();
  499. tree.ObjectActivationButton = MouseFlags.Button2Clicked;
  500. tree.ExpandAll ();
  501. object activated = null;
  502. bool called = false;
  503. // register for the event
  504. tree.ObjectActivated += (s, e) => {
  505. activated = e.ActivatedObject;
  506. called = true;
  507. };
  508. Assert.False (called);
  509. // double click does nothing because we changed button binding to right click
  510. tree.MouseEvent (new MouseEvent () { Y = 1, Flags = MouseFlags.Button1DoubleClicked });
  511. Assert.Null (activated);
  512. Assert.False (called);
  513. tree.MouseEvent (new MouseEvent () { Y = 1, Flags = MouseFlags.Button2Clicked });
  514. Assert.True (called);
  515. Assert.Same (car1, activated);
  516. Assert.Same (car1, tree.SelectedObject);
  517. Application.Shutdown ();
  518. }
  519. /// <summary>
  520. /// 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)"/>
  521. /// </summary>
  522. [Fact]
  523. public void RefreshObject_EqualityTest ()
  524. {
  525. var obj1 = new EqualityTestObject () { Name = "Bob", Age = 1 };
  526. var obj2 = new EqualityTestObject () { Name = "Bob", Age = 2 }; ;
  527. string root = "root";
  528. var tree = new TreeView<object> ();
  529. tree.TreeBuilder = new DelegateTreeBuilder<object> ((s) => ReferenceEquals (s, root) ? new object [] { obj1 } : null);
  530. tree.AddObject (root);
  531. // Tree is not expanded so the root has no children yet
  532. Assert.Empty (tree.GetChildren (root));
  533. tree.Expand (root);
  534. // now that the tree is expanded we should get our child returned
  535. Assert.Equal (1, tree.GetChildren (root).Count (child => ReferenceEquals (obj1, child)));
  536. // change the getter to return an Equal object (but not the same reference - obj2)
  537. tree.TreeBuilder = new DelegateTreeBuilder<object> ((s) => ReferenceEquals (s, root) ? new object [] { obj2 } : null);
  538. // tree has cached the knowledge of what children the root has so won't know about the change (we still get obj1)
  539. Assert.Equal (1, tree.GetChildren (root).Count (child => ReferenceEquals (obj1, child)));
  540. // now that we refresh the root we should get the new child reference (obj2)
  541. tree.RefreshObject (root);
  542. Assert.Equal (1, tree.GetChildren (root).Count (child => ReferenceEquals (obj2, child)));
  543. }
  544. [Fact, AutoInitShutdown]
  545. public void TestGetObjectOnRow ()
  546. {
  547. var tv = new TreeView { Width = 20, Height = 10 };
  548. tv.BeginInit (); tv.EndInit ();
  549. var n1 = new TreeNode ("normal");
  550. var n1_1 = new TreeNode ("pink");
  551. var n1_2 = new TreeNode ("normal");
  552. n1.Children.Add (n1_1);
  553. n1.Children.Add (n1_2);
  554. var n2 = new TreeNode ("pink");
  555. tv.AddObject (n1);
  556. tv.AddObject (n2);
  557. tv.Expand (n1);
  558. tv.ColorScheme = new ColorScheme ();
  559. tv.LayoutSubviews ();
  560. tv.Draw ();
  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.Draw ();
  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.LayoutSubviews ();
  599. tv.Draw ();
  600. TestHelpers.AssertDriverContentsAre (
  601. @"├-normal
  602. │ ├─pink
  603. │ └─normal
  604. └─pink
  605. ", output);
  606. Assert.Equal (0, tv.GetObjectRow (n1));
  607. Assert.Equal (1, tv.GetObjectRow (n1_1));
  608. Assert.Equal (2, tv.GetObjectRow (n1_2));
  609. Assert.Equal (3, tv.GetObjectRow (n2));
  610. tv.Collapse (n1);
  611. tv.LayoutSubviews ();
  612. tv.Draw ();
  613. TestHelpers.AssertDriverContentsAre (
  614. @"├+normal
  615. └─pink
  616. ", output);
  617. Assert.Equal (0, tv.GetObjectRow (n1));
  618. Assert.Null (tv.GetObjectRow (n1_1));
  619. Assert.Null (tv.GetObjectRow (n1_2));
  620. Assert.Equal (1, tv.GetObjectRow (n2));
  621. // scroll down 1
  622. tv.ScrollOffsetVertical = 1;
  623. tv.LayoutSubviews ();
  624. tv.Draw ();
  625. TestHelpers.AssertDriverContentsAre (
  626. @"└─pink
  627. ", output);
  628. Assert.Equal (-1, tv.GetObjectRow (n1));
  629. Assert.Null (tv.GetObjectRow (n1_1));
  630. Assert.Null (tv.GetObjectRow (n1_2));
  631. Assert.Equal (0, tv.GetObjectRow (n2));
  632. }
  633. [Fact, AutoInitShutdown]
  634. public void TestTreeViewColor ()
  635. {
  636. var tv = new TreeView { Width = 20, Height = 10 };
  637. var n1 = new TreeNode ("normal");
  638. var n1_1 = new TreeNode ("pink");
  639. var n1_2 = new TreeNode ("normal");
  640. n1.Children.Add (n1_1);
  641. n1.Children.Add (n1_2);
  642. var n2 = new TreeNode ("pink");
  643. tv.AddObject (n1);
  644. tv.AddObject (n2);
  645. tv.Expand (n1);
  646. var pink = new Attribute (Color.Magenta, Color.Black);
  647. var hotpink = new Attribute (Color.BrightMagenta, Color.Black);
  648. tv.ColorScheme = new ColorScheme ();
  649. tv.LayoutSubviews ();
  650. tv.Draw ();
  651. // Normal drawing of the tree view
  652. TestHelpers.AssertDriverContentsAre (
  653. @"├-normal
  654. │ ├─pink
  655. │ └─normal
  656. └─pink
  657. ", output);
  658. // Should all be the same color
  659. TestHelpers.AssertDriverColorsAre (
  660. @"00000000
  661. 00000000
  662. 0000000000
  663. 000000
  664. ",
  665. new [] { tv.ColorScheme.Normal, pink });
  666. // create a new color scheme
  667. var pinkScheme = new ColorScheme {
  668. Normal = pink,
  669. Focus = hotpink
  670. };
  671. // and a delegate that uses the pink color scheme
  672. // for nodes "pink"
  673. tv.ColorGetter = (n) => n.Text.Equals ("pink") ? pinkScheme : null;
  674. // redraw now that the custom color
  675. // delegate is registered
  676. tv.Draw ();
  677. // Same text
  678. TestHelpers.AssertDriverContentsAre (
  679. @"├-normal
  680. │ ├─pink
  681. │ └─normal
  682. └─pink
  683. ", output);
  684. // but now the item (only not lines) appear
  685. // in pink when they are the word "pink"
  686. TestHelpers.AssertDriverColorsAre (
  687. @"00000000
  688. 00001111
  689. 0000000000
  690. 001111
  691. ",
  692. new [] { tv.ColorScheme.Normal, pink });
  693. }
  694. [Fact, AutoInitShutdown]
  695. public void TestBottomlessTreeView_MaxDepth_5 ()
  696. {
  697. var tv = new TreeView<string> () { Width = 20, Height = 10 };
  698. tv.TreeBuilder = new DelegateTreeBuilder<string> (
  699. (s) => new [] { (int.Parse (s) + 1).ToString () }
  700. );
  701. tv.AddObject ("1");
  702. tv.ColorScheme = new ColorScheme ();
  703. tv.LayoutSubviews ();
  704. tv.Draw ();
  705. // Nothing expanded
  706. TestHelpers.AssertDriverContentsAre (
  707. @"└+1
  708. ", output);
  709. tv.MaxDepth = 5;
  710. tv.ExpandAll ();
  711. tv.Draw ();
  712. // Normal drawing of the tree view
  713. TestHelpers.AssertDriverContentsAre (
  714. @"
  715. └-1
  716. └-2
  717. └-3
  718. └-4
  719. └-5
  720. └─6
  721. ", output);
  722. Assert.False (tv.CanExpand ("6"));
  723. Assert.False (tv.IsExpanded ("6"));
  724. tv.Collapse ("6");
  725. Assert.False (tv.CanExpand ("6"));
  726. Assert.False (tv.IsExpanded ("6"));
  727. tv.Collapse ("5");
  728. Assert.True (tv.CanExpand ("5"));
  729. Assert.False (tv.IsExpanded ("5"));
  730. tv.Draw ();
  731. // Normal drawing of the tree view
  732. TestHelpers.AssertDriverContentsAre (
  733. @"
  734. └-1
  735. └-2
  736. └-3
  737. └-4
  738. └+5
  739. ", output);
  740. }
  741. [Fact, AutoInitShutdown]
  742. public void TestBottomlessTreeView_MaxDepth_3 ()
  743. {
  744. var tv = new TreeView<string> () { Width = 20, Height = 10 };
  745. tv.TreeBuilder = new DelegateTreeBuilder<string> (
  746. (s) => new [] { (int.Parse (s) + 1).ToString () }
  747. );
  748. tv.AddObject ("1");
  749. tv.ColorScheme = new ColorScheme ();
  750. tv.LayoutSubviews ();
  751. tv.Draw ();
  752. // Nothing expanded
  753. TestHelpers.AssertDriverContentsAre (
  754. @"└+1
  755. ", output);
  756. tv.MaxDepth = 3;
  757. tv.ExpandAll ();
  758. tv.Draw ();
  759. // Normal drawing of the tree view
  760. TestHelpers.AssertDriverContentsAre (
  761. @"
  762. └-1
  763. └-2
  764. └-3
  765. └─4
  766. ", output);
  767. }
  768. [Fact, AutoInitShutdown]
  769. public void TestTreeView_DrawLineEvent ()
  770. {
  771. var tv = new TreeView { Width = 20, Height = 10 };
  772. var eventArgs = new List<DrawTreeViewLineEventArgs<ITreeNode>> ();
  773. tv.DrawLine += (s, e) => {
  774. eventArgs.Add (e);
  775. };
  776. var n1 = new TreeNode ("root one");
  777. var n1_1 = new TreeNode ("leaf 1");
  778. var n1_2 = new TreeNode ("leaf 2");
  779. n1.Children.Add (n1_1);
  780. n1.Children.Add (n1_2);
  781. var n2 = new TreeNode ("root two");
  782. tv.AddObject (n1);
  783. tv.AddObject (n2);
  784. tv.Expand (n1);
  785. tv.ColorScheme = new ColorScheme ();
  786. tv.LayoutSubviews ();
  787. tv.Draw ();
  788. // Normal drawing of the tree view
  789. TestHelpers.AssertDriverContentsAre (
  790. @"
  791. ├-root one
  792. │ ├─leaf 1
  793. │ └─leaf 2
  794. └─root two
  795. ", output);
  796. Assert.Equal (4, eventArgs.Count ());
  797. Assert.Equal (0, eventArgs [0].Y);
  798. Assert.Equal (1, eventArgs [1].Y);
  799. Assert.Equal (2, eventArgs [2].Y);
  800. Assert.Equal (3, eventArgs [3].Y);
  801. Assert.All (eventArgs, ea => Assert.Equal (ea.Tree, tv));
  802. Assert.All (eventArgs, ea => Assert.False (ea.Handled));
  803. Assert.Equal ("├-root one", eventArgs [0].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  804. Assert.Equal ("│ ├─leaf 1", eventArgs [1].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  805. Assert.Equal ("│ └─leaf 2", eventArgs [2].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  806. Assert.Equal ("└─root two", eventArgs [3].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  807. Assert.Equal (1, eventArgs [0].IndexOfExpandCollapseSymbol);
  808. Assert.Equal (3, eventArgs [1].IndexOfExpandCollapseSymbol);
  809. Assert.Equal (3, eventArgs [2].IndexOfExpandCollapseSymbol);
  810. Assert.Equal (1, eventArgs [3].IndexOfExpandCollapseSymbol);
  811. Assert.Equal (2, eventArgs [0].IndexOfModelText);
  812. Assert.Equal (4, eventArgs [1].IndexOfModelText);
  813. Assert.Equal (4, eventArgs [2].IndexOfModelText);
  814. Assert.Equal (2, eventArgs [3].IndexOfModelText);
  815. Assert.Equal ("root one", eventArgs [0].Model.Text);
  816. Assert.Equal ("leaf 1", eventArgs [1].Model.Text);
  817. Assert.Equal ("leaf 2", eventArgs [2].Model.Text);
  818. Assert.Equal ("root two", eventArgs [3].Model.Text);
  819. }
  820. [Fact, AutoInitShutdown]
  821. public void TestTreeView_DrawLineEvent_WithScrolling ()
  822. {
  823. var tv = new TreeView { Width = 20, Height = 10 };
  824. var eventArgs = new List<DrawTreeViewLineEventArgs<ITreeNode>> ();
  825. tv.DrawLine += (s, e) => {
  826. eventArgs.Add (e);
  827. };
  828. tv.ScrollOffsetHorizontal = 3;
  829. tv.ScrollOffsetVertical = 1;
  830. var n1 = new TreeNode ("root one");
  831. var n1_1 = new TreeNode ("leaf 1");
  832. var n1_2 = new TreeNode ("leaf 2");
  833. n1.Children.Add (n1_1);
  834. n1.Children.Add (n1_2);
  835. var n2 = new TreeNode ("root two");
  836. tv.AddObject (n1);
  837. tv.AddObject (n2);
  838. tv.Expand (n1);
  839. tv.ColorScheme = new ColorScheme ();
  840. tv.LayoutSubviews ();
  841. tv.Draw ();
  842. // Normal drawing of the tree view
  843. TestHelpers.AssertDriverContentsAre (
  844. @"
  845. ─leaf 1
  846. ─leaf 2
  847. oot two
  848. ", output);
  849. Assert.Equal (3, eventArgs.Count ());
  850. Assert.Equal (0, eventArgs [0].Y);
  851. Assert.Equal (1, eventArgs [1].Y);
  852. Assert.Equal (2, eventArgs [2].Y);
  853. Assert.All (eventArgs, ea => Assert.Equal (ea.Tree, tv));
  854. Assert.All (eventArgs, ea => Assert.False (ea.Handled));
  855. Assert.Equal ("─leaf 1", eventArgs [0].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  856. Assert.Equal ("─leaf 2", eventArgs [1].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  857. Assert.Equal ("oot two", eventArgs [2].RuneCells.Aggregate ("", (s, n) => s += n.Rune).TrimEnd ());
  858. Assert.Equal (0, eventArgs [0].IndexOfExpandCollapseSymbol);
  859. Assert.Equal (0, eventArgs [1].IndexOfExpandCollapseSymbol);
  860. Assert.Null (eventArgs [2].IndexOfExpandCollapseSymbol);
  861. Assert.Equal (1, eventArgs [0].IndexOfModelText);
  862. Assert.Equal (1, eventArgs [1].IndexOfModelText);
  863. Assert.Equal (-1, eventArgs [2].IndexOfModelText);
  864. Assert.Equal ("leaf 1", eventArgs [0].Model.Text);
  865. Assert.Equal ("leaf 2", eventArgs [1].Model.Text);
  866. Assert.Equal ("root two", eventArgs [2].Model.Text);
  867. }
  868. [Fact, AutoInitShutdown]
  869. public void TestTreeView_DrawLineEvent_Handled ()
  870. {
  871. var tv = new TreeView { Width = 20, Height = 10 };
  872. tv.DrawLine += (s, e) => {
  873. if(e.Model.Text.Equals("leaf 1")) {
  874. e.Handled = true;
  875. for (int i = 0; i < 10; i++) {
  876. e.Tree.AddRune (i,e.Y,new System.Text.Rune('F'));
  877. }
  878. }
  879. };
  880. var n1 = new TreeNode ("root one");
  881. var n1_1 = new TreeNode ("leaf 1");
  882. var n1_2 = new TreeNode ("leaf 2");
  883. n1.Children.Add (n1_1);
  884. n1.Children.Add (n1_2);
  885. var n2 = new TreeNode ("root two");
  886. tv.AddObject (n1);
  887. tv.AddObject (n2);
  888. tv.Expand (n1);
  889. tv.ColorScheme = new ColorScheme ();
  890. tv.LayoutSubviews ();
  891. tv.Draw ();
  892. // Normal drawing of the tree view
  893. TestHelpers.AssertDriverContentsAre (
  894. @"
  895. ├-root one
  896. FFFFFFFFFF
  897. │ └─leaf 2
  898. └─root two
  899. ", output);
  900. }
  901. [Fact, AutoInitShutdown]
  902. public void TestTreeView_Filter ()
  903. {
  904. var tv = new TreeView { Width = 20, Height = 10 };
  905. var n1 = new TreeNode ("root one");
  906. var n1_1 = new TreeNode ("leaf 1");
  907. var n1_2 = new TreeNode ("leaf 2");
  908. n1.Children.Add (n1_1);
  909. n1.Children.Add (n1_2);
  910. var n2 = new TreeNode ("root two");
  911. tv.AddObject (n1);
  912. tv.AddObject (n2);
  913. tv.Expand (n1);
  914. tv.ColorScheme = new ColorScheme ();
  915. tv.LayoutSubviews ();
  916. tv.Draw ();
  917. // Normal drawing of the tree view
  918. TestHelpers.AssertDriverContentsAre (
  919. @"
  920. ├-root one
  921. │ ├─leaf 1
  922. │ └─leaf 2
  923. └─root two
  924. ", output);
  925. var filter = new TreeViewTextFilter<ITreeNode> (tv);
  926. tv.Filter = filter;
  927. // matches nothing
  928. filter.Text = "asdfjhasdf";
  929. tv.Draw ();
  930. // Normal drawing of the tree view
  931. TestHelpers.AssertDriverContentsAre (
  932. @"", output);
  933. // Matches everything
  934. filter.Text = "root";
  935. tv.Draw ();
  936. TestHelpers.AssertDriverContentsAre (
  937. @"
  938. ├-root one
  939. │ ├─leaf 1
  940. │ └─leaf 2
  941. └─root two
  942. ", output);
  943. // Matches 2 leaf nodes
  944. filter.Text = "leaf";
  945. tv.Draw ();
  946. TestHelpers.AssertDriverContentsAre (
  947. @"
  948. ├-root one
  949. │ ├─leaf 1
  950. │ └─leaf 2
  951. ", output);
  952. // Matches 1 leaf nodes
  953. filter.Text = "leaf 1";
  954. tv.Draw ();
  955. TestHelpers.AssertDriverContentsAre (
  956. @"
  957. ├-root one
  958. │ ├─leaf 1
  959. ", output);
  960. }
  961. [Fact, AutoInitShutdown]
  962. public void DesiredCursorVisibility_MultiSelect ()
  963. {
  964. var tv = new TreeView { Width = 20, Height = 10 };
  965. var n1 = new TreeNode ("normal");
  966. var n2 = new TreeNode ("pink");
  967. tv.AddObject (n1);
  968. tv.AddObject (n2);
  969. Application.Top.Add (tv);
  970. Application.Begin (Application.Top);
  971. Assert.True (tv.MultiSelect);
  972. Assert.True (tv.HasFocus);
  973. Assert.Equal (CursorVisibility.Invisible, tv.DesiredCursorVisibility);
  974. tv.SelectAll ();
  975. tv.DesiredCursorVisibility = CursorVisibility.Default;
  976. Application.Refresh ();
  977. Application.Driver.GetCursorVisibility (out CursorVisibility visibility);
  978. Assert.Equal (CursorVisibility.Default, tv.DesiredCursorVisibility);
  979. Assert.Equal (CursorVisibility.Default, visibility);
  980. }
  981. /// <summary>
  982. /// Test object which considers for equality only <see cref="Name"/>
  983. /// </summary>
  984. private class EqualityTestObject {
  985. public string Name { get; set; }
  986. public int Age { get; set; }
  987. public override int GetHashCode ()
  988. {
  989. return Name?.GetHashCode () ?? base.GetHashCode ();
  990. }
  991. public override bool Equals (object obj)
  992. {
  993. return obj is EqualityTestObject eto && Equals (Name, eto.Name);
  994. }
  995. }
  996. private void InitFakeDriver ()
  997. {
  998. var driver = new FakeDriver ();
  999. Application.Init (driver);
  1000. driver.Init (() => { });
  1001. }
  1002. }
  1003. }