2
0

AbsoluteLayoutTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Xml.Linq;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. //using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.ViewTests {
  11. public class AbsoluteLayoutTests {
  12. readonly ITestOutputHelper output;
  13. public AbsoluteLayoutTests (ITestOutputHelper output)
  14. {
  15. this.output = output;
  16. }
  17. [Fact]
  18. public void AbsoluteLayout_Constructor ()
  19. {
  20. var frame = new Rect (1, 2, 3, 4);
  21. var v = new View (frame);
  22. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  23. Assert.Equal (frame, v.Frame);
  24. Assert.Equal (new Rect(0, 0, frame.Width, frame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  25. Assert.Null (v.X);
  26. Assert.Null (v.Y);
  27. Assert.Null (v.Height);
  28. Assert.Null (v.Width);
  29. v = new View (frame, "v");
  30. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  31. Assert.Equal (frame, v.Frame);
  32. Assert.Equal (new Rect (0, 0, frame.Width, frame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  33. Assert.Null (v.X);
  34. Assert.Null (v.Y);
  35. Assert.Null (v.Height);
  36. Assert.Null (v.Width);
  37. v = new View (frame.X, frame.Y, "v");
  38. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  39. // BUGBUG: v2 - I think the default size should be 0,0
  40. Assert.Equal (new Rect(frame.X, frame.Y, 1, 1), v.Frame);
  41. Assert.Equal (new Rect (0, 0, 1, 1), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  42. Assert.Null (v.X);
  43. Assert.Null (v.Y);
  44. Assert.Null (v.Height);
  45. Assert.Null (v.Width);
  46. }
  47. [Fact]
  48. public void AbsoluteLayout_Change_Frame ()
  49. {
  50. var frame = new Rect (1, 2, 3, 4);
  51. var newFrame = new Rect (1, 2, 30, 40);
  52. var v = new View (frame);
  53. v.Frame = newFrame;
  54. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  55. Assert.Equal (newFrame, v.Frame);
  56. Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  57. Assert.Null (v.X);
  58. Assert.Null (v.Y);
  59. Assert.Null (v.Height);
  60. Assert.Null (v.Width);
  61. v = new View (frame.X, frame.Y, "v");
  62. v.Frame = newFrame;
  63. Assert.Equal (newFrame, v.Frame);
  64. Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  65. Assert.Null (v.X);
  66. Assert.Null (v.Y);
  67. Assert.Null (v.Height);
  68. Assert.Null (v.Width);
  69. newFrame = new Rect (10, 20, 30, 40);
  70. v = new View (frame);
  71. v.Frame = newFrame;
  72. Assert.Equal (newFrame, v.Frame);
  73. Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  74. Assert.Null (v.X);
  75. Assert.Null (v.Y);
  76. Assert.Null (v.Height);
  77. Assert.Null (v.Width);
  78. v = new View (frame.X, frame.Y, "v");
  79. v.Frame = newFrame;
  80. Assert.Equal (newFrame, v.Frame);
  81. Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  82. Assert.Null (v.X);
  83. Assert.Null (v.Y);
  84. Assert.Null (v.Height);
  85. Assert.Null (v.Width);
  86. }
  87. [Fact]
  88. public void AbsoluteLayout_Change_Height_or_Width_Absolute ()
  89. {
  90. var frame = new Rect (1, 2, 3, 4);
  91. var newFrame = new Rect (1, 2, 30, 40);
  92. var v = new View (frame);
  93. v.Height = newFrame.Height;
  94. v.Width = newFrame.Width;
  95. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  96. Assert.Equal (newFrame, v.Frame);
  97. Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  98. Assert.Null (v.X);
  99. Assert.Null (v.Y);
  100. Assert.Equal ($"Absolute({newFrame.Height})", v.Height.ToString());
  101. Assert.Equal ($"Absolute({newFrame.Width})", v.Width.ToString ());
  102. }
  103. [Fact]
  104. public void AbsoluteLayout_Change_Height_or_Width_NotAbsolute ()
  105. {
  106. var v = new View (Rect.Empty);
  107. v.Height = Dim.Fill ();
  108. v.Width = Dim.Fill ();
  109. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  110. }
  111. [Fact]
  112. public void AbsoluteLayout_Change_Height_or_Width_Null ()
  113. {
  114. var v = new View (Rect.Empty);
  115. v.Height = null;
  116. v.Width = null;
  117. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  118. }
  119. [Fact]
  120. public void AbsoluteLayout_Change_X_or_Y_Absolute ()
  121. {
  122. var frame = new Rect (1, 2, 3, 4);
  123. var newFrame = new Rect (10, 20, 3, 4);
  124. var v = new View (frame);
  125. v.X = newFrame.X;
  126. v.Y = newFrame.Y;
  127. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  128. Assert.Equal (newFrame, v.Frame);
  129. Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout
  130. Assert.Equal ($"Absolute({newFrame.X})", v.X.ToString ());
  131. Assert.Equal ($"Absolute({newFrame.Y})", v.Y.ToString ());
  132. Assert.Null (v.Height);
  133. Assert.Null (v.Width);
  134. }
  135. [Fact]
  136. public void AbsoluteLayout_Change_X_or_Y_NotAbsolute ()
  137. {
  138. var v = new View (Rect.Empty);
  139. v.X = Pos.Center ();
  140. v.Y = Pos.Center ();
  141. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  142. }
  143. [Fact]
  144. public void AbsoluteLayout_Change_X_or_Y_Null ()
  145. {
  146. var v = new View (Rect.Empty);
  147. v.X = null;
  148. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  149. v = new View (Rect.Empty);
  150. v.X = Pos.Center ();
  151. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  152. v.X = null;
  153. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  154. v = new View (Rect.Empty);
  155. v.Y = null;
  156. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  157. v = new View (Rect.Empty);
  158. v.Y = Pos.Center ();
  159. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  160. v.Y = null;
  161. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  162. }
  163. [Fact]
  164. public void AbsoluteLayout_Change_X_Y_Height_Width_Absolute ()
  165. {
  166. var v = new View (Rect.Empty);
  167. v.X = 1;
  168. v.Y = 2;
  169. v.Height = 3;
  170. v.Width = 4;
  171. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  172. v = new View (Rect.Empty);
  173. v.X = Pos.Center ();
  174. v.Y = Pos.Center ();
  175. v.Width = Dim.Fill ();
  176. v.Height = Dim.Fill ();
  177. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  178. // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte?
  179. v.X = null;
  180. v.Y = null;
  181. v.Height = null;
  182. v.Width = null;
  183. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed??
  184. v = new View (Rect.Empty);
  185. v.X = Pos.Center ();
  186. v.Y = Pos.Center ();
  187. v.Width = Dim.Fill ();
  188. v.Height = Dim.Fill ();
  189. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  190. // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte?
  191. v.X = 1;
  192. v.Y = null;
  193. v.Height = null;
  194. v.Width = null;
  195. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed??
  196. v = new View (Rect.Empty);
  197. v.X = Pos.Center ();
  198. v.Y = Pos.Center ();
  199. v.Width = Dim.Fill ();
  200. v.Height = Dim.Fill ();
  201. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  202. // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte?
  203. v.X = null;
  204. v.Y = 2;
  205. v.Height = null;
  206. v.Width = null;
  207. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed??
  208. v = new View (Rect.Empty);
  209. v.X = Pos.Center ();
  210. v.Y = Pos.Center ();
  211. v.Width = Dim.Fill ();
  212. v.Height = Dim.Fill ();
  213. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  214. // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte?
  215. v.X = null;
  216. v.Y = null;
  217. v.Height = 3;
  218. v.Width = null;
  219. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed??
  220. v = new View (Rect.Empty);
  221. v.X = Pos.Center ();
  222. v.Y = Pos.Center ();
  223. v.Width = Dim.Fill ();
  224. v.Height = Dim.Fill ();
  225. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  226. // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte?
  227. v.X = null;
  228. v.Y = null;
  229. v.Height = null;
  230. v.Width = 4;
  231. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed??
  232. }
  233. [Fact]
  234. public void AbsoluteLayout_Change_X_Y_Height_Width_Null ()
  235. {
  236. var v = new View (Rect.Empty);
  237. v.X = null;
  238. v.Y = null;
  239. v.Height = null;
  240. v.Width = null;
  241. Assert.True (v.LayoutStyle == LayoutStyle.Absolute);
  242. v = new View (Rect.Empty);
  243. v.X = Pos.Center ();
  244. v.Y = Pos.Center ();
  245. v.Width = Dim.Fill ();
  246. v.Height = Dim.Fill ();
  247. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle
  248. // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte?
  249. v.X = null;
  250. v.Y = null;
  251. v.Height = null;
  252. v.Width = null;
  253. Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed??
  254. }
  255. [Fact]
  256. public void AbsoluteLayout_Layout ()
  257. {
  258. var superRect = new Rect (0, 0, 100, 100);
  259. var super = new View (superRect, "super");
  260. Assert.True (super.LayoutStyle == LayoutStyle.Absolute);
  261. var v1 = new View () {
  262. X = 0,
  263. Y = 0,
  264. Width = 10,
  265. Height = 10
  266. };
  267. // BUGBUG: v2 - This should be LayoutStyle.Absolute
  268. Assert.True (v1.LayoutStyle == LayoutStyle.Computed);
  269. var v2 = new View () {
  270. X = 10,
  271. Y = 10,
  272. Width = 10,
  273. Height = 10
  274. };
  275. // BUGBUG: v2 - This should be LayoutStyle.Absolute
  276. Assert.True (v1.LayoutStyle == LayoutStyle.Computed);
  277. super.Add (v1, v2);
  278. super.LayoutSubviews ();
  279. Assert.Equal (new Rect (0, 0, 10, 10), v1.Frame);
  280. Assert.Equal (new Rect (10, 10, 10, 10), v2.Frame);
  281. }
  282. }
  283. }