AbsoluteLayoutTests.cs 11 KB

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