SetRelativeLayoutTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Text;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewTests;
  6. public class SetRelativeLayoutTests {
  7. readonly ITestOutputHelper _output;
  8. public SetRelativeLayoutTests (ITestOutputHelper output) => _output = output;
  9. [Fact]
  10. public void Null_Pos_Is_Same_As_PosAbsolute0 ()
  11. {
  12. var view = new View () {
  13. X = null,
  14. Y = null,
  15. };
  16. // Default layout style is Computed
  17. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  18. Assert.Null (view.X);
  19. Assert.Null (view.Y);
  20. view.BeginInit(); view.EndInit();
  21. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  22. Assert.Null (view.X);
  23. Assert.Null (view.Y);
  24. view.SetRelativeLayout(new Rect(5, 5, 10, 10));
  25. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  26. Assert.Null (view.X);
  27. Assert.Null (view.Y);
  28. Assert.Equal (0, view.Frame.X);
  29. Assert.Equal (0, view.Frame.Y);
  30. }
  31. [Theory]
  32. [InlineData (1, 1)]
  33. [InlineData (0, 0)]
  34. public void NonNull_Pos (int pos, int expectedPos)
  35. {
  36. var view = new View () {
  37. X = pos,
  38. Y = pos,
  39. };
  40. // Default layout style is Computed
  41. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  42. Assert.NotNull (view.X);
  43. Assert.NotNull (view.Y);
  44. view.BeginInit (); view.EndInit ();
  45. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  46. Assert.NotNull (view.X);
  47. Assert.NotNull (view.Y);
  48. view.SetRelativeLayout (new Rect (5, 5, 10, 10));
  49. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  50. Assert.NotNull (view.X);
  51. Assert.NotNull (view.Y);
  52. Assert.Equal (expectedPos, view.Frame.X);
  53. Assert.Equal (expectedPos, view.Frame.Y);
  54. }
  55. [Fact]
  56. public void Null_Dim_Is_Same_As_DimFill0 ()
  57. {
  58. var view = new View () {
  59. Width = null,
  60. Height = null,
  61. };
  62. // Default layout style is Computed
  63. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  64. Assert.Null (view.Width);
  65. Assert.Null (view.Height);
  66. view.BeginInit (); view.EndInit ();
  67. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  68. Assert.Null (view.Width);
  69. Assert.Null (view.Height);
  70. view.SetRelativeLayout (new Rect (5, 5, 10, 10));
  71. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  72. Assert.Null (view.Width);
  73. Assert.Null (view.Height);
  74. Assert.Equal (0, view.Frame.X);
  75. Assert.Equal (0, view.Frame.Y);
  76. Assert.Equal (10, view.Frame.Width);
  77. Assert.Equal (10, view.Frame.Height);
  78. view.Width = Dim.Fill (0);
  79. view.Height = Dim.Fill (0);
  80. view.SetRelativeLayout (new Rect (5, 5, 10, 10));
  81. Assert.Equal (10, view.Frame.Width);
  82. Assert.Equal (10, view.Frame.Height);
  83. }
  84. [Theory]
  85. [InlineData(1, 1)]
  86. [InlineData (0, 0)]
  87. public void NonNull_Dim (int dim, int expectedDim)
  88. {
  89. var view = new View () {
  90. Width = dim,
  91. Height = dim,
  92. };
  93. // Default layout style is Computed
  94. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  95. Assert.NotNull (view.Width);
  96. Assert.NotNull (view.Height);
  97. view.BeginInit (); view.EndInit ();
  98. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  99. Assert.NotNull (view.Width);
  100. Assert.NotNull (view.Height);
  101. view.SetRelativeLayout (new Rect (5, 5, 10, 10));
  102. Assert.Equal (view.LayoutStyle, LayoutStyle.Computed);
  103. Assert.NotNull (view.Width);
  104. Assert.NotNull (view.Height);
  105. Assert.Equal (0, view.Frame.X);
  106. Assert.Equal (0, view.Frame.Y);
  107. // BUGBUG: Width == null is same as Dim.Absolute (0) (or should be). Thus this is a bug.
  108. Assert.Equal (expectedDim, view.Frame.Width);
  109. Assert.Equal (expectedDim, view.Frame.Height);
  110. }
  111. [Fact] [TestRespondersDisposed]
  112. public void PosCombine_Center_Plus_Absolute ()
  113. {
  114. var superView = new View () {
  115. AutoSize = false,
  116. Width = 10,
  117. Height = 10
  118. };
  119. var testView = new View () {
  120. AutoSize = false,
  121. X = Pos.Center (),
  122. Y = Pos.Center (),
  123. Width = 1,
  124. Height = 1
  125. };
  126. superView.Add (testView);
  127. testView.SetRelativeLayout (superView.Frame);
  128. Assert.Equal (4, testView.Frame.X);
  129. Assert.Equal (4, testView.Frame.Y);
  130. testView = new View () {
  131. AutoSize = false,
  132. X = Pos.Center () + 1,
  133. Y = Pos.Center () + 1,
  134. Width = 1,
  135. Height = 1
  136. };
  137. superView.Add (testView);
  138. testView.SetRelativeLayout (superView.Frame);
  139. Assert.Equal (5, testView.Frame.X);
  140. Assert.Equal (5, testView.Frame.Y);
  141. testView = new View () {
  142. AutoSize = false,
  143. X = 1 + Pos.Center (),
  144. Y = 1 + Pos.Center (),
  145. Width = 1,
  146. Height = 1
  147. };
  148. superView.Add (testView);
  149. testView.SetRelativeLayout (superView.Frame);
  150. Assert.Equal (5, testView.Frame.X);
  151. Assert.Equal (5, testView.Frame.Y);
  152. testView = new View () {
  153. AutoSize = false,
  154. X = 1 + Pos.Percent (50),
  155. Y = Pos.Percent (50) + 1,
  156. Width = 1,
  157. Height = 1
  158. };
  159. superView.Add (testView);
  160. testView.SetRelativeLayout (superView.Frame);
  161. Assert.Equal (6, testView.Frame.X);
  162. Assert.Equal (6, testView.Frame.Y);
  163. testView = new View () {
  164. AutoSize = false,
  165. X = Pos.Percent (10) + Pos.Percent (40),
  166. Y = Pos.Percent (10) + Pos.Percent (40),
  167. Width = 1,
  168. Height = 1
  169. };
  170. superView.Add (testView);
  171. testView.SetRelativeLayout (superView.Frame);
  172. Assert.Equal (5, testView.Frame.X);
  173. Assert.Equal (5, testView.Frame.Y);
  174. testView = new View () {
  175. AutoSize = false,
  176. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  177. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  178. Width = 1,
  179. Height = 1
  180. };
  181. superView.Add (testView);
  182. testView.SetRelativeLayout (superView.Frame);
  183. Assert.Equal (5, testView.Frame.X);
  184. Assert.Equal (5, testView.Frame.Y);
  185. testView = new View () {
  186. AutoSize = false,
  187. X = Pos.Left (testView),
  188. Y = Pos.Left (testView),
  189. Width = 1,
  190. Height = 1
  191. };
  192. superView.Add (testView);
  193. testView.SetRelativeLayout (superView.Frame);
  194. Assert.Equal (5, testView.Frame.X);
  195. Assert.Equal (5, testView.Frame.Y);
  196. testView = new View () {
  197. AutoSize = false,
  198. X = 1 + Pos.Left (testView),
  199. Y = Pos.Top (testView) + 1,
  200. Width = 1,
  201. Height = 1
  202. };
  203. superView.Add (testView);
  204. testView.SetRelativeLayout (superView.Frame);
  205. Assert.Equal (6, testView.Frame.X);
  206. Assert.Equal (6, testView.Frame.Y);
  207. superView.Dispose ();
  208. }
  209. }