TreeViewTests.cs 28 KB

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