TreeViewTests.cs 42 KB

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