TreeViewTests.cs 26 KB

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