SubviewTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. public class SubviewTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public SubviewTests (ITestOutputHelper output) { _output = output; }
  9. [Fact]
  10. [AutoInitShutdown]
  11. public void GetTopSuperView_Test ()
  12. {
  13. var v1 = new View ();
  14. var fv1 = new FrameView ();
  15. fv1.Add (v1);
  16. var tf1 = new TextField ();
  17. var w1 = new Window ();
  18. w1.Add (fv1, tf1);
  19. var top1 = new Toplevel ();
  20. top1.Add (w1);
  21. var v2 = new View ();
  22. var fv2 = new FrameView ();
  23. fv2.Add (v2);
  24. var tf2 = new TextField ();
  25. var w2 = new Window ();
  26. w2.Add (fv2, tf2);
  27. var top2 = new Toplevel ();
  28. top2.Add (w2);
  29. Assert.Equal (top1, v1.GetTopSuperView ());
  30. Assert.Equal (top2, v2.GetTopSuperView ());
  31. v1.Dispose ();
  32. fv1.Dispose ();
  33. tf1.Dispose ();
  34. w1.Dispose ();
  35. top1.Dispose ();
  36. v2.Dispose ();
  37. fv2.Dispose ();
  38. tf2.Dispose ();
  39. w2.Dispose ();
  40. top2.Dispose ();
  41. }
  42. [Fact]
  43. [TestRespondersDisposed]
  44. public void Initialized_Event_Comparing_With_Added_Event ()
  45. {
  46. Application.Init (new FakeDriver ());
  47. var top = new Toplevel { Id = "0" }; // Frame: 0, 0, 80, 25; Viewport: 0, 0, 80, 25
  48. var winAddedToTop = new Window
  49. {
  50. Id = "t", Width = Dim.Fill (), Height = Dim.Fill ()
  51. }; // Frame: 0, 0, 80, 25; Viewport: 0, 0, 78, 23
  52. var v1AddedToWin = new View
  53. {
  54. Id = "v1", Width = Dim.Fill (), Height = Dim.Fill ()
  55. }; // Frame: 1, 1, 78, 23 (because Windows has a border)
  56. var v2AddedToWin = new View
  57. {
  58. Id = "v2", Width = Dim.Fill (), Height = Dim.Fill ()
  59. }; // Frame: 1, 1, 78, 23 (because Windows has a border)
  60. var svAddedTov1 = new View
  61. {
  62. Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill ()
  63. }; // Frame: 1, 1, 78, 23 (same as it's superview v1AddedToWin)
  64. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  65. winAddedToTop.Added += (s, e) =>
  66. {
  67. Assert.Equal (e.SuperView.Frame.Width, winAddedToTop.Frame.Width);
  68. Assert.Equal (e.SuperView.Frame.Height, winAddedToTop.Frame.Height);
  69. };
  70. v1AddedToWin.Added += (s, e) =>
  71. {
  72. Assert.Equal (e.SuperView.Frame.Width, v1AddedToWin.Frame.Width);
  73. Assert.Equal (e.SuperView.Frame.Height, v1AddedToWin.Frame.Height);
  74. };
  75. v2AddedToWin.Added += (s, e) =>
  76. {
  77. Assert.Equal (e.SuperView.Frame.Width, v2AddedToWin.Frame.Width);
  78. Assert.Equal (e.SuperView.Frame.Height, v2AddedToWin.Frame.Height);
  79. };
  80. svAddedTov1.Added += (s, e) =>
  81. {
  82. Assert.Equal (e.SuperView.Frame.Width, svAddedTov1.Frame.Width);
  83. Assert.Equal (e.SuperView.Frame.Height, svAddedTov1.Frame.Height);
  84. };
  85. top.Initialized += (s, e) =>
  86. {
  87. tc++;
  88. Assert.Equal (1, tc);
  89. Assert.Equal (1, wc);
  90. Assert.Equal (1, v1c);
  91. Assert.Equal (1, v2c);
  92. Assert.Equal (1, sv1c);
  93. Assert.True (top.CanFocus);
  94. Assert.True (winAddedToTop.CanFocus);
  95. Assert.False (v1AddedToWin.CanFocus);
  96. Assert.False (v2AddedToWin.CanFocus);
  97. Assert.False (svAddedTov1.CanFocus);
  98. Application.LayoutAndDraw ();
  99. };
  100. winAddedToTop.Initialized += (s, e) =>
  101. {
  102. wc++;
  103. Assert.Equal (top.Viewport.Width, winAddedToTop.Frame.Width);
  104. Assert.Equal (top.Viewport.Height, winAddedToTop.Frame.Height);
  105. };
  106. v1AddedToWin.Initialized += (s, e) =>
  107. {
  108. v1c++;
  109. // Top.Frame: 0, 0, 80, 25; Top.Viewport: 0, 0, 80, 25
  110. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  111. // in no way should the v1AddedToWin.Frame be the same as the Top.Frame/Viewport
  112. // as it is a subview of winAddedToTop, which has a border!
  113. //Assert.Equal (top.Viewport.Width, v1AddedToWin.Frame.Width);
  114. //Assert.Equal (top.Viewport.Height, v1AddedToWin.Frame.Height);
  115. };
  116. v2AddedToWin.Initialized += (s, e) =>
  117. {
  118. v2c++;
  119. // Top.Frame: 0, 0, 80, 25; Top.Viewport: 0, 0, 80, 25
  120. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  121. // in no way should the v2AddedToWin.Frame be the same as the Top.Frame/Viewport
  122. // as it is a subview of winAddedToTop, which has a border!
  123. //Assert.Equal (top.Viewport.Width, v2AddedToWin.Frame.Width);
  124. //Assert.Equal (top.Viewport.Height, v2AddedToWin.Frame.Height);
  125. };
  126. svAddedTov1.Initialized += (s, e) =>
  127. {
  128. sv1c++;
  129. // Top.Frame: 0, 0, 80, 25; Top.Viewport: 0, 0, 80, 25
  130. // BUGBUG: This is wrong, it should be 78, 23. This test has always been broken.
  131. // in no way should the svAddedTov1.Frame be the same as the Top.Frame/Viewport
  132. // because sv1AddedTov1 is a subview of v1AddedToWin, which is a subview of
  133. // winAddedToTop, which has a border!
  134. //Assert.Equal (top.Viewport.Width, svAddedTov1.Frame.Width);
  135. //Assert.Equal (top.Viewport.Height, svAddedTov1.Frame.Height);
  136. Assert.False (svAddedTov1.CanFocus);
  137. //Assert.Throws<InvalidOperationException> (() => svAddedTov1.CanFocus = true);
  138. Assert.False (svAddedTov1.CanFocus);
  139. };
  140. v1AddedToWin.Add (svAddedTov1);
  141. winAddedToTop.Add (v1AddedToWin, v2AddedToWin);
  142. top.Add (winAddedToTop);
  143. Application.Iteration += (s, a) =>
  144. {
  145. Application.LayoutAndDraw ();
  146. top.Running = false;
  147. };
  148. Application.Run (top);
  149. top.Dispose ();
  150. Application.Shutdown ();
  151. Assert.Equal (1, tc);
  152. Assert.Equal (1, wc);
  153. Assert.Equal (1, v1c);
  154. Assert.Equal (1, v2c);
  155. Assert.Equal (1, sv1c);
  156. Assert.True (top.CanFocus);
  157. Assert.True (winAddedToTop.CanFocus);
  158. Assert.False (v1AddedToWin.CanFocus);
  159. Assert.False (v2AddedToWin.CanFocus);
  160. Assert.False (svAddedTov1.CanFocus);
  161. v1AddedToWin.CanFocus = true;
  162. Assert.False (svAddedTov1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  163. }
  164. [Fact]
  165. [TestRespondersDisposed]
  166. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  167. {
  168. Application.Init (new FakeDriver ());
  169. var t = new Toplevel { Id = "0" };
  170. var w = new Window { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  171. var v1 = new View { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  172. var v2 = new View { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  173. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  174. t.Initialized += (s, e) =>
  175. {
  176. tc++;
  177. Assert.Equal (1, tc);
  178. Assert.Equal (1, wc);
  179. Assert.Equal (1, v1c);
  180. Assert.Equal (1, v2c);
  181. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  182. Assert.True (t.CanFocus);
  183. Assert.True (w.CanFocus);
  184. Assert.False (v1.CanFocus);
  185. Assert.False (v2.CanFocus);
  186. Application.LayoutAndDraw ();
  187. };
  188. w.Initialized += (s, e) =>
  189. {
  190. wc++;
  191. Assert.Equal (t.Viewport.Width, w.Frame.Width);
  192. Assert.Equal (t.Viewport.Height, w.Frame.Height);
  193. };
  194. v1.Initialized += (s, e) =>
  195. {
  196. v1c++;
  197. //Assert.Equal (t.Viewport.Width, v1.Frame.Width);
  198. //Assert.Equal (t.Viewport.Height, v1.Frame.Height);
  199. };
  200. v2.Initialized += (s, e) =>
  201. {
  202. v2c++;
  203. //Assert.Equal (t.Viewport.Width, v2.Frame.Width);
  204. //Assert.Equal (t.Viewport.Height, v2.Frame.Height);
  205. };
  206. w.Add (v1, v2);
  207. t.Add (w);
  208. Application.Iteration += (s, a) =>
  209. {
  210. var sv1 = new View { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  211. sv1.Initialized += (s, e) =>
  212. {
  213. sv1c++;
  214. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  215. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  216. Assert.False (sv1.CanFocus);
  217. //Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  218. Assert.False (sv1.CanFocus);
  219. };
  220. v1.Add (sv1);
  221. Application.LayoutAndDraw ();
  222. t.Running = false;
  223. };
  224. Application.Run (t);
  225. t.Dispose ();
  226. Application.Shutdown ();
  227. Assert.Equal (1, tc);
  228. Assert.Equal (1, wc);
  229. Assert.Equal (1, v1c);
  230. Assert.Equal (1, v2c);
  231. Assert.Equal (1, sv1c);
  232. Assert.True (t.CanFocus);
  233. Assert.True (w.CanFocus);
  234. Assert.False (v1.CanFocus);
  235. Assert.False (v2.CanFocus);
  236. }}