Pos.ViewTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Pos;
  3. namespace Terminal.Gui.LayoutTests;
  4. public class PosViewTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Fact]
  8. public void PosView_Equal ()
  9. {
  10. var view1 = new View ();
  11. var view2 = new View ();
  12. Pos pos1 = Left (view1);
  13. Pos pos2 = Left (view1);
  14. Assert.Equal (pos1, pos2);
  15. pos2 = Left (view2);
  16. Assert.NotEqual (pos1, pos2);
  17. pos2 = Right (view1);
  18. Assert.NotEqual (pos1, pos2);
  19. }
  20. // TODO: Test Left, Top, Right bottom Equal
  21. /// <summary>Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations</summary>
  22. [Fact]
  23. [TestRespondersDisposed]
  24. public void PosView_Side_SetsValue ()
  25. {
  26. string side; // used in format string
  27. var testRect = Rectangle.Empty;
  28. var testInt = 0;
  29. Pos pos;
  30. // Pos.Left
  31. side = "Left";
  32. testInt = 0;
  33. testRect = Rectangle.Empty;
  34. pos = Left (new ());
  35. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  36. pos = Left (new () { Frame = testRect });
  37. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  38. testRect = new (1, 2, 3, 4);
  39. pos = Left (new () { Frame = testRect });
  40. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  41. // Pos.Left(win) + 0
  42. pos = Left (new () { Frame = testRect }) + testInt;
  43. Assert.Equal (
  44. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  45. pos.ToString ()
  46. );
  47. testInt = 1;
  48. // Pos.Left(win) +1
  49. pos = Left (new () { Frame = testRect }) + testInt;
  50. Assert.Equal (
  51. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  52. pos.ToString ()
  53. );
  54. testInt = -1;
  55. // Pos.Left(win) -1
  56. pos = Left (new () { Frame = testRect }) - testInt;
  57. Assert.Equal (
  58. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  59. pos.ToString ()
  60. );
  61. // Pos.X
  62. side = "Left";
  63. testInt = 0;
  64. testRect = Rectangle.Empty;
  65. pos = X (new ());
  66. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  67. pos = X (new () { Frame = testRect });
  68. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  69. testRect = new (1, 2, 3, 4);
  70. pos = X (new () { Frame = testRect });
  71. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  72. // Pos.X(win) + 0
  73. pos = X (new () { Frame = testRect }) + testInt;
  74. Assert.Equal (
  75. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  76. pos.ToString ()
  77. );
  78. testInt = 1;
  79. // Pos.X(win) +1
  80. pos = X (new () { Frame = testRect }) + testInt;
  81. Assert.Equal (
  82. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  83. pos.ToString ()
  84. );
  85. testInt = -1;
  86. // Pos.X(win) -1
  87. pos = X (new () { Frame = testRect }) - testInt;
  88. Assert.Equal (
  89. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  90. pos.ToString ()
  91. );
  92. // Pos.Top
  93. side = "Top";
  94. testInt = 0;
  95. testRect = Rectangle.Empty;
  96. pos = Top (new ());
  97. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  98. pos = Top (new () { Frame = testRect });
  99. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  100. testRect = new (1, 2, 3, 4);
  101. pos = Top (new () { Frame = testRect });
  102. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  103. // Pos.Top(win) + 0
  104. pos = Top (new () { Frame = testRect }) + testInt;
  105. Assert.Equal (
  106. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  107. pos.ToString ()
  108. );
  109. testInt = 1;
  110. // Pos.Top(win) +1
  111. pos = Top (new () { Frame = testRect }) + testInt;
  112. Assert.Equal (
  113. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  114. pos.ToString ()
  115. );
  116. testInt = -1;
  117. // Pos.Top(win) -1
  118. pos = Top (new () { Frame = testRect }) - testInt;
  119. Assert.Equal (
  120. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  121. pos.ToString ()
  122. );
  123. // Pos.Y
  124. side = "Top";
  125. testInt = 0;
  126. testRect = Rectangle.Empty;
  127. pos = Y (new ());
  128. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  129. pos = Y (new () { Frame = testRect });
  130. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  131. testRect = new (1, 2, 3, 4);
  132. pos = Y (new () { Frame = testRect });
  133. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  134. // Pos.Y(win) + 0
  135. pos = Y (new () { Frame = testRect }) + testInt;
  136. Assert.Equal (
  137. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  138. pos.ToString ()
  139. );
  140. testInt = 1;
  141. // Pos.Y(win) +1
  142. pos = Y (new () { Frame = testRect }) + testInt;
  143. Assert.Equal (
  144. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  145. pos.ToString ()
  146. );
  147. testInt = -1;
  148. // Pos.Y(win) -1
  149. pos = Y (new () { Frame = testRect }) - testInt;
  150. Assert.Equal (
  151. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  152. pos.ToString ()
  153. );
  154. // Pos.Bottom
  155. side = "Bottom";
  156. testRect = Rectangle.Empty;
  157. testInt = 0;
  158. pos = Bottom (new ());
  159. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  160. pos = Bottom (new () { Frame = testRect });
  161. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  162. testRect = new (1, 2, 3, 4);
  163. pos = Bottom (new () { Frame = testRect });
  164. Assert.Equal ($"View(Side={side},Target=View(){testRect})", pos.ToString ());
  165. // Pos.Bottom(win) + 0
  166. pos = Bottom (new () { Frame = testRect }) + testInt;
  167. Assert.Equal (
  168. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  169. pos.ToString ()
  170. );
  171. testInt = 1;
  172. // Pos.Bottom(win) +1
  173. pos = Bottom (new () { Frame = testRect }) + testInt;
  174. Assert.Equal (
  175. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  176. pos.ToString ()
  177. );
  178. testInt = -1;
  179. // Pos.Bottom(win) -1
  180. pos = Bottom (new () { Frame = testRect }) - testInt;
  181. Assert.Equal (
  182. $"Combine(View(Side={side},Target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  183. pos.ToString ()
  184. );
  185. #if DEBUG_IDISPOSABLE
  186. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  187. Responder.Instances.Clear ();
  188. #endif
  189. }
  190. [Fact]
  191. public void PosView_Side_SetToNull_Throws ()
  192. {
  193. Pos pos = Left (null);
  194. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  195. pos = X (null);
  196. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  197. pos = Top (null);
  198. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  199. pos = Y (null);
  200. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  201. pos = Bottom (null);
  202. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  203. pos = Right (null);
  204. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  205. }
  206. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  207. // TODO: A new test that calls SetRelativeLayout directly is needed.
  208. [Fact]
  209. [TestRespondersDisposed]
  210. public void Subtract_Operator ()
  211. {
  212. Application.Init (new FakeDriver ());
  213. var top = new Toplevel ();
  214. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  215. var field = new TextField { X = 0, Y = 0, Width = 20 };
  216. var count = 20;
  217. List<View> listViews = new ();
  218. for (var i = 0; i < count; i++)
  219. {
  220. field.Text = $"View {i}";
  221. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  222. view.Add (view2);
  223. Assert.Equal ($"View {i}", view2.Text);
  224. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  225. listViews.Add (view2);
  226. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  227. field.Y += 1;
  228. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  229. }
  230. field.KeyDown += (s, k) =>
  231. {
  232. if (k.KeyCode == KeyCode.Enter)
  233. {
  234. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  235. view.Remove (listViews [count - 1]);
  236. listViews [count - 1].Dispose ();
  237. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  238. field.Y -= 1;
  239. count--;
  240. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  241. }
  242. };
  243. Application.Iteration += (s, a) =>
  244. {
  245. while (count > 0)
  246. {
  247. field.NewKeyDownEvent (new (KeyCode.Enter));
  248. }
  249. Application.RequestStop ();
  250. };
  251. var win = new Window ();
  252. win.Add (view);
  253. win.Add (field);
  254. top.Add (win);
  255. Application.Run (top);
  256. top.Dispose ();
  257. Assert.Equal (0, count);
  258. // Shutdown must be called to safely clean up Application if Init has been called
  259. Application.Shutdown ();
  260. }
  261. }