PanelViewTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. namespace Terminal.Gui.Views {
  8. public class PanelViewTests {
  9. [Fact]
  10. public void Constructor_Defaults ()
  11. {
  12. var pv = new PanelView ();
  13. Assert.False (pv.CanFocus);
  14. Assert.False (pv.Visible);
  15. Assert.False (pv.UsePanelFrame);
  16. Assert.Null (pv.Child);
  17. pv = new PanelView (new Label ("This is a test."));
  18. Assert.False (pv.CanFocus);
  19. Assert.True (pv.Visible);
  20. Assert.False (pv.UsePanelFrame);
  21. Assert.NotNull (pv.Child);
  22. Assert.NotNull (pv.Border);
  23. Assert.Null (pv.Child.Border);
  24. }
  25. [Fact]
  26. public void Child_Sets_To_Null_Remove_From_Subviews_PanelView ()
  27. {
  28. var pv = new PanelView (new Label ("This is a test."));
  29. Assert.NotNull (pv.Child);
  30. Assert.Equal (1, pv.Subviews[0].Subviews.Count);
  31. pv.Child = null;
  32. Assert.Null (pv.Child);
  33. Assert.Equal (0, pv.Subviews [0].Subviews.Count);
  34. }
  35. [Fact]
  36. public void Add_View_Also_Sets_Child ()
  37. {
  38. var pv = new PanelView ();
  39. Assert.Null (pv.Child);
  40. Assert.Equal (0, pv.Subviews [0].Subviews.Count);
  41. pv.Add (new Label ("This is a test."));
  42. Assert.NotNull (pv.Child);
  43. Assert.Equal (1, pv.Subviews [0].Subviews.Count);
  44. }
  45. [Fact]
  46. public void Add_More_Views_Remove_Last_Child_Before__Only_One_Is_Allowed ()
  47. {
  48. var pv = new PanelView (new Label ("This is a test."));
  49. Assert.NotNull (pv.Child);
  50. Assert.Equal (1, pv.Subviews [0].Subviews.Count);
  51. Assert.IsType<Label> (pv.Child);
  52. pv.Add (new TextField ("This is a test."));
  53. Assert.NotNull (pv.Child);
  54. Assert.Equal (1, pv.Subviews [0].Subviews.Count);
  55. Assert.IsNotType<Label> (pv.Child);
  56. Assert.IsType<TextField> (pv.Child);
  57. }
  58. [Fact]
  59. public void Remove_RemoveAll_View_Also_Sets_Child_To_Null ()
  60. {
  61. var pv = new PanelView (new Label ("This is a test."));
  62. Assert.NotNull (pv.Child);
  63. Assert.Equal (1, pv.Subviews [0].Subviews.Count);
  64. pv.Remove (pv.Child);
  65. Assert.Null (pv.Child);
  66. Assert.Equal (0, pv.Subviews [0].Subviews.Count);
  67. pv = new PanelView (new Label ("This is a test."));
  68. Assert.NotNull (pv.Child);
  69. Assert.Equal (1, pv.Subviews [0].Subviews.Count);
  70. pv.RemoveAll ();
  71. Assert.Null (pv.Child);
  72. Assert.Equal (0, pv.Subviews [0].Subviews.Count);
  73. }
  74. [Fact]
  75. [AutoInitShutdown]
  76. public void AdjustContainer_Without_Border ()
  77. {
  78. var top = Application.Top;
  79. var win = new Window ();
  80. var pv = new PanelView (new Label ("This is a test."));
  81. win.Add (pv);
  82. top.Add (win);
  83. Application.Begin (top);
  84. Assert.Equal (new Rect (0, 0, 15, 1), pv.Frame);
  85. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  86. }
  87. [Fact]
  88. [AutoInitShutdown]
  89. public void AdjustContainer_With_Border_Absolute_Values ()
  90. {
  91. var top = Application.Top;
  92. var win = new Window ();
  93. var pv = new PanelView (new Label ("This is a test.") {
  94. Border = new Border () {
  95. BorderStyle = BorderStyle.Double,
  96. BorderThickness = new Thickness (1, 2, 3, 4),
  97. Padding = new Thickness (1, 2, 3, 4)
  98. }
  99. });
  100. win.Add (pv);
  101. top.Add (win);
  102. Application.Begin (top);
  103. Assert.Equal (new Rect (0, 0, 25, 15), pv.Frame);
  104. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  105. }
  106. [Fact]
  107. [AutoInitShutdown]
  108. public void AdjustContainer_With_Border_Computed_Values ()
  109. {
  110. var top = Application.Top;
  111. var win = new Window ();
  112. var pv = new PanelView (new TextView () {
  113. Width = Dim.Fill (),
  114. Height = Dim.Fill (),
  115. Border = new Border () {
  116. BorderStyle = BorderStyle.Double,
  117. BorderThickness = new Thickness (1, 2, 3, 4),
  118. Padding = new Thickness (1, 2, 3, 4)
  119. }
  120. });
  121. var pv1 = new PanelView (new TextView () {
  122. Width = Dim.Fill (1),
  123. Height = Dim.Fill (1),
  124. Border = new Border () {
  125. BorderStyle = BorderStyle.Double,
  126. BorderThickness = new Thickness (1, 2, 3, 4),
  127. Padding = new Thickness (1, 2, 3, 4)
  128. }
  129. });
  130. var pv2 = new PanelView (new TextView () {
  131. Width = Dim.Fill (2),
  132. Height = Dim.Fill (2),
  133. Border = new Border () {
  134. BorderStyle = BorderStyle.Double,
  135. BorderThickness = new Thickness (1, 2, 3, 4),
  136. Padding = new Thickness (1, 2, 3, 4)
  137. }
  138. });
  139. win.Add (pv, pv1, pv2);
  140. top.Add (win);
  141. Application.Begin (top);
  142. Assert.Equal (new Rect (0, 0, 78, 23), pv.Frame);
  143. Assert.Equal (new Rect (0, 0, 68, 9), pv.Child.Frame);
  144. Assert.Equal (new Rect (0, 0, 77, 22), pv1.Frame);
  145. Assert.Equal (new Rect (0, 0, 65, 6), pv1.Child.Frame);
  146. Assert.Equal (new Rect (0, 0, 76, 21), pv2.Frame);
  147. Assert.Equal (new Rect (0, 0, 62, 3), pv2.Child.Frame);
  148. }
  149. [Fact]
  150. [AutoInitShutdown]
  151. public void UsePanelFrame_False_PanelView_Always_Respect_The_Child_Upper_Left_Corner_Position_And_Size ()
  152. {
  153. var top = Application.Top;
  154. var win = new Window ();
  155. var pv = new PanelView (new Label ("This is a test.")) {
  156. X = 2,
  157. Y = 4,
  158. Width = 20,
  159. Height = 10
  160. };
  161. var pv1 = new PanelView (new TextField (3, 4, 15, "This is a test.")) {
  162. X = 2,
  163. Y = 4,
  164. Width = 20,
  165. Height = 10
  166. };
  167. var pv2 = new PanelView (new TextView () {
  168. X = 5,
  169. Y = 6,
  170. Width = Dim.Fill (),
  171. Height = Dim.Fill ()
  172. }) {
  173. X = 2,
  174. Y = 4,
  175. Width = 20,
  176. Height = 10
  177. };
  178. win.Add (pv, pv1, pv2);
  179. top.Add (win);
  180. Application.Begin (top);
  181. Assert.Equal (new Rect (0, 0, 15, 1), pv.Frame);
  182. Assert.Equal (new Rect (0, 0, 15, 1), pv.Child.Frame);
  183. Assert.Equal (new Rect (3, 4, 15, 1), pv1.Frame);
  184. Assert.Equal (new Rect (0, 0, 15, 1), pv1.Child.Frame);
  185. Assert.Equal (new Rect (5, 6, 73, 17), pv2.Frame);
  186. Assert.Equal (new Rect (0, 0, 73, 17), pv2.Child.Frame);
  187. }
  188. [Fact]
  189. [AutoInitShutdown]
  190. public void UsePanelFrame_True_PanelView_Position_And_Size_Are_Used ()
  191. {
  192. var top = Application.Top;
  193. var win = new Window ();
  194. var pv = new PanelView (new TextView () {
  195. X = 2,
  196. Y = 4,
  197. Width = 20,
  198. Height = 10
  199. }) {
  200. X = 5,
  201. Y = 6,
  202. Width = Dim.Fill (),
  203. Height = Dim.Fill (),
  204. UsePanelFrame = true
  205. };
  206. var pv1 = new PanelView (new TextView () {
  207. X = 5,
  208. Y = 6,
  209. Width = Dim.Fill (),
  210. Height = Dim.Fill ()
  211. }) {
  212. X = 2,
  213. Y = 4,
  214. Width = 20,
  215. Height = 10,
  216. UsePanelFrame = true
  217. };
  218. win.Add (pv, pv1);
  219. top.Add (win);
  220. Application.Begin (top);
  221. Assert.Equal (new Rect (5, 6, 73, 17), pv.Frame);
  222. Assert.Equal (new Rect (0, 0, 20, 10), pv.Child.Frame);
  223. Assert.Equal (new Rect (2, 4, 20, 10), pv1.Frame);
  224. Assert.Equal (new Rect (0, 0, 20, 10), pv1.Child.Frame);
  225. }
  226. }
  227. }