TreeViewTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Xunit;
  8. namespace UnitTests {
  9. public class TreeViewTests
  10. {
  11. #region Test Setup Methods
  12. class Factory
  13. {
  14. public Car[] Cars {get;set;}
  15. };
  16. class Car {
  17. };
  18. private TreeView CreateTree()
  19. {
  20. return CreateTree(out _, out _, out _);
  21. }
  22. private TreeView CreateTree(out Factory factory1, out Car car1, out Car car2)
  23. {
  24. car1 = new Car();
  25. car2 = new Car();
  26. factory1 = new Factory()
  27. {
  28. Cars = new []{car1 ,car2}
  29. };
  30. var tree = new TreeView();
  31. tree.ChildrenGetter = (s)=> s is Factory f ? f.Cars: null;
  32. tree.AddObject(factory1);
  33. return tree;
  34. }
  35. #endregion
  36. /// <summary>
  37. /// Tests that <see cref="TreeView.Expand(object)"/> and <see cref="TreeView.IsExpanded(object)"/> are consistent
  38. /// </summary>
  39. [Fact]
  40. public void IsExpanded_TrueAfterExpand()
  41. {
  42. var tree = CreateTree(out Factory f, out _, out _);
  43. Assert.False(tree.IsExpanded(f));
  44. tree.Expand(f);
  45. Assert.True(tree.IsExpanded(f));
  46. tree.Collapse(f);
  47. Assert.False(tree.IsExpanded(f));
  48. }
  49. /// <summary>
  50. /// 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)
  51. /// </summary>
  52. [Fact]
  53. public void IsExpanded_FalseIfCannotExpand()
  54. {
  55. var tree = CreateTree(out Factory f, out Car c, out _);
  56. // expose the car by expanding the factory
  57. tree.Expand(f);
  58. // car is not expanded
  59. Assert.False(tree.IsExpanded(c));
  60. //try to expand the car (should have no effect because cars have no children)
  61. tree.Expand(c);
  62. Assert.False(tree.IsExpanded(c));
  63. // should also be ignored
  64. tree.Collapse(c);
  65. Assert.False(tree.IsExpanded(c));
  66. }
  67. /// <summary>
  68. /// Tests illegal ranges for <see cref="TreeView.ScrollOffset"/>
  69. /// </summary>
  70. [Fact]
  71. public void ScrollOffset_CannotBeNegative()
  72. {
  73. var tree = CreateTree();
  74. Assert.Equal(0,tree.ScrollOffset);
  75. tree.ScrollOffset = -100;
  76. Assert.Equal(0,tree.ScrollOffset);
  77. tree.ScrollOffset = 10;
  78. Assert.Equal(10,tree.ScrollOffset);
  79. }
  80. /// <summary>
  81. /// Tests <see cref="TreeView.GetScrollOffsetOf(object)"/> for objects that are as yet undiscovered by the tree
  82. /// </summary>
  83. [Fact]
  84. public void GetScrollOffsetOf_MinusOneForUnRevealed()
  85. {
  86. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  87. // to start with the tree is collapsed and only knows about the root object
  88. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  89. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  90. Assert.Equal(-1,tree.GetScrollOffsetOf(c2));
  91. // reveal it by expanding the root object
  92. tree.Expand(f);
  93. // tree now knows about children
  94. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  95. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  96. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  97. // after collapsing the root node again
  98. tree.Collapse(f);
  99. // tree no longer knows about the locations of these objects
  100. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  101. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  102. Assert.Equal(-1,tree.GetScrollOffsetOf(c2));
  103. }
  104. /// <summary>
  105. /// 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)"/>
  106. /// </summary>
  107. [Fact]
  108. public void RefreshObject_ChildRemoved()
  109. {
  110. var tree = CreateTree(out Factory f, out Car c1, out Car c2);
  111. //reveal it by expanding the root object
  112. tree.Expand(f);
  113. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  114. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  115. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  116. // Factory now no longer makes Car c1 (only c2)
  117. f.Cars = new Car[]{c2};
  118. // Tree does not know this yet
  119. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  120. Assert.Equal(1,tree.GetScrollOffsetOf(c1));
  121. Assert.Equal(2,tree.GetScrollOffsetOf(c2));
  122. // If the user has selected the node c1
  123. tree.SelectedObject = c1;
  124. // When we refresh the tree
  125. tree.RefreshObject(f);
  126. // Now tree knows that factory has only one child node c2
  127. Assert.Equal(0,tree.GetScrollOffsetOf(f));
  128. Assert.Equal(-1,tree.GetScrollOffsetOf(c1));
  129. Assert.Equal(1,tree.GetScrollOffsetOf(c2));
  130. // The old selection was c1 which is now gone so selection should default to the parent of that branch (the factory)
  131. Assert.Equal(f,tree.SelectedObject);
  132. }
  133. }
  134. }