TreeViewTests.cs 33 KB

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