ClearViewportTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #nullable enable
  2. using Moq;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewTests;
  6. [Trait ("Category", "Output")]
  7. public class ClearViewportTests (ITestOutputHelper output)
  8. {
  9. public class TestableView : View
  10. {
  11. public bool TestOnClearingViewport () { return OnClearingViewport (); }
  12. public int OnClearingViewportCalled { get; set; }
  13. public bool CancelOnClearingViewport { get; set; }
  14. protected override bool OnClearingViewport ()
  15. {
  16. OnClearingViewportCalled++;
  17. return CancelOnClearingViewport;
  18. }
  19. public int OnClearedViewportCalled { get; set; }
  20. protected override void OnClearedViewport () { OnClearedViewportCalled++; }
  21. }
  22. [Fact]
  23. public void DoClearViewport_ViewportIsTransparent_DoesNotClear ()
  24. {
  25. // Arrange
  26. Mock<TestableView> view = new () { CallBase = true };
  27. view.Object.ViewportSettings = ViewportSettings.Transparent;
  28. // Act
  29. view.Object.DoClearViewport ();
  30. // Assert
  31. Assert.Equal (0, view.Object.OnClearingViewportCalled);
  32. Assert.Equal (0, view.Object.OnClearedViewportCalled);
  33. }
  34. [Fact]
  35. public void DoClearViewport_OnClearingViewportReturnsTrue_DoesNotClear ()
  36. {
  37. // Arrange
  38. Mock<TestableView> view = new () { CallBase = true };
  39. view.Object.CancelOnClearingViewport = true;
  40. // Act
  41. view.Object.DoClearViewport ();
  42. // Assert
  43. Assert.Equal (0, view.Object.OnClearedViewportCalled);
  44. }
  45. [Fact]
  46. public void DoClearViewport_ClearingViewportEventCancelled_DoesNotClear ()
  47. {
  48. // Arrange
  49. Mock<TestableView> view = new () { CallBase = true };
  50. view.Object.ClearingViewport += (sender, e) => e.Cancel = true;
  51. // Act
  52. view.Object.DoClearViewport ();
  53. // Assert
  54. Assert.Equal (0, view.Object.OnClearedViewportCalled);
  55. }
  56. [Fact]
  57. public void DoClearViewport_ClearsViewport ()
  58. {
  59. // Arrange
  60. Mock<TestableView> view = new () { CallBase = true };
  61. // Act
  62. view.Object.DoClearViewport ();
  63. // Assert
  64. Assert.Equal (1, view.Object.OnClearedViewportCalled);
  65. }
  66. [Fact]
  67. public void DoClearViewport_RaisesClearingViewportEvent ()
  68. {
  69. // Arrange
  70. Mock<TestableView> view = new () { CallBase = true };
  71. var eventRaised = false;
  72. view.Object.ClearingViewport += (sender, e) => eventRaised = true;
  73. // Act
  74. view.Object.DoClearViewport ();
  75. // Assert
  76. Assert.True (eventRaised);
  77. }
  78. [Fact]
  79. [SetupFakeDriver]
  80. public void Clear_ClearsEntireViewport ()
  81. {
  82. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  83. var view = new View
  84. {
  85. Text = "X",
  86. X = 1, Y = 1,
  87. Width = 3, Height = 3,
  88. BorderStyle = LineStyle.Single
  89. };
  90. superView.Add (view);
  91. superView.BeginInit ();
  92. superView.EndInit ();
  93. superView.LayoutSubViews ();
  94. superView.Draw ();
  95. DriverAssert.AssertDriverContentsWithFrameAre (
  96. @"
  97. ┌─┐
  98. │X│
  99. └─┘",
  100. output);
  101. // On Draw exit the view is excluded from the clip, so this will do nothing.
  102. view.ClearViewport ();
  103. DriverAssert.AssertDriverContentsWithFrameAre (
  104. @"
  105. ┌─┐
  106. │X│
  107. └─┘",
  108. output);
  109. View.SetClipToScreen ();
  110. view.ClearViewport ();
  111. DriverAssert.AssertDriverContentsWithFrameAre (
  112. @"
  113. ┌─┐
  114. │ │
  115. └─┘",
  116. output);
  117. }
  118. [Fact]
  119. [SetupFakeDriver]
  120. public void Clear_WithClearVisibleContentOnly_ClearsVisibleContentOnly ()
  121. {
  122. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  123. var view = new View
  124. {
  125. Text = "X",
  126. X = 1, Y = 1,
  127. Width = 3, Height = 3,
  128. BorderStyle = LineStyle.Single,
  129. ViewportSettings = ViewportSettings.ClearContentOnly
  130. };
  131. superView.Add (view);
  132. superView.BeginInit ();
  133. superView.EndInit ();
  134. superView.LayoutSubViews ();
  135. superView.Draw ();
  136. DriverAssert.AssertDriverContentsWithFrameAre (
  137. @"
  138. ┌─┐
  139. │X│
  140. └─┘",
  141. output);
  142. View.SetClipToScreen ();
  143. view.ClearViewport ();
  144. DriverAssert.AssertDriverContentsWithFrameAre (
  145. @"
  146. ┌─┐
  147. │ │
  148. └─┘",
  149. output);
  150. }
  151. [Fact]
  152. [AutoInitShutdown]
  153. public void Clear_Viewport_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  154. {
  155. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  156. view.DrawingContent += (s, e) =>
  157. {
  158. Region? savedClip = view.AddViewportToClip ();
  159. for (var row = 0; row < view.Viewport.Height; row++)
  160. {
  161. Application.Driver?.Move (1, row + 1);
  162. for (var col = 0; col < view.Viewport.Width; col++)
  163. {
  164. Application.Driver?.AddStr ($"{col}");
  165. }
  166. }
  167. View.SetClip (savedClip);
  168. e.Cancel = true;
  169. };
  170. var top = new Toplevel ();
  171. top.Add (view);
  172. Application.Begin (top);
  173. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  174. var expected = @"
  175. ┌──────────────────┐
  176. │012345678910111213│
  177. │012345678910111213│
  178. │012345678910111213│
  179. │012345678910111213│
  180. │012345678910111213│
  181. │012345678910111213│
  182. │012345678910111213│
  183. │012345678910111213│
  184. └──────────────────┘
  185. "
  186. ;
  187. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  188. Assert.Equal (new (0, 0, 20, 10), pos);
  189. view.FillRect (view.Viewport);
  190. expected = @"
  191. ┌──────────────────┐
  192. │ │
  193. │ │
  194. │ │
  195. │ │
  196. │ │
  197. │ │
  198. │ │
  199. │ │
  200. └──────────────────┘
  201. "
  202. ;
  203. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  204. top.Dispose ();
  205. }
  206. [Fact]
  207. [AutoInitShutdown]
  208. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  209. {
  210. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  211. view.DrawingContent += (s, e) =>
  212. {
  213. Region? savedClip = view.AddViewportToClip ();
  214. for (var row = 0; row < view.Viewport.Height; row++)
  215. {
  216. Application.Driver?.Move (1, row + 1);
  217. for (var col = 0; col < view.Viewport.Width; col++)
  218. {
  219. Application.Driver?.AddStr ($"{col}");
  220. }
  221. }
  222. View.SetClip (savedClip);
  223. e.Cancel = true;
  224. };
  225. var top = new Toplevel ();
  226. top.Add (view);
  227. Application.Begin (top);
  228. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  229. var expected = @"
  230. ┌──────────────────┐
  231. │012345678910111213│
  232. │012345678910111213│
  233. │012345678910111213│
  234. │012345678910111213│
  235. │012345678910111213│
  236. │012345678910111213│
  237. │012345678910111213│
  238. │012345678910111213│
  239. └──────────────────┘
  240. "
  241. ;
  242. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  243. Assert.Equal (new (0, 0, 20, 10), pos);
  244. view.FillRect (view.Viewport);
  245. expected = @"
  246. ┌──────────────────┐
  247. │ │
  248. │ │
  249. │ │
  250. │ │
  251. │ │
  252. │ │
  253. │ │
  254. │ │
  255. └──────────────────┘
  256. ";
  257. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  258. top.Dispose ();
  259. }
  260. [Theory]
  261. [AutoInitShutdown (configLocation: ConfigLocations.Default)]
  262. [InlineData (true)]
  263. [InlineData (false)]
  264. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  265. {
  266. var root = new View { Width = 20, Height = 10, ColorScheme = Colors.ColorSchemes ["Base"] };
  267. string text = new ('c', 100);
  268. View v = label
  269. // Label has Width/Height == AutoSize, so Frame.Size will be (100, 1)
  270. ? new Label { Text = text }
  271. // TextView has Width/Height == (Dim.Fill, 1), so Frame.Size will be 20 (width of root), 1
  272. : new TextView { Width = Dim.Fill (), Height = 1, Text = text };
  273. root.Add (v);
  274. var top = new Toplevel ();
  275. top.Add (root);
  276. RunState runState = Application.Begin (top);
  277. Application.RunIteration (ref runState);
  278. if (label)
  279. {
  280. Assert.False (v.CanFocus);
  281. Assert.Equal (new (0, 0, text.Length, 1), v.Frame);
  282. }
  283. else
  284. {
  285. Assert.True (v.CanFocus);
  286. Assert.Equal (new (0, 0, 20, 1), v.Frame);
  287. }
  288. DriverAssert.AssertDriverContentsWithFrameAre (
  289. @"
  290. cccccccccccccccccccc",
  291. output
  292. );
  293. Attribute [] attributes =
  294. {
  295. Colors.ColorSchemes ["TopLevel"]!.Normal,
  296. Colors.ColorSchemes ["Base"]!.Normal,
  297. Colors.ColorSchemes ["Base"]!.Focus
  298. };
  299. if (label)
  300. {
  301. DriverAssert.AssertDriverAttributesAre (
  302. @"
  303. 111111111111111111110
  304. 111111111111111111110",
  305. output,
  306. Application.Driver,
  307. attributes
  308. );
  309. }
  310. else
  311. {
  312. DriverAssert.AssertDriverAttributesAre (
  313. @"
  314. 222222222222222222220
  315. 111111111111111111110",
  316. output,
  317. Application.Driver,
  318. attributes
  319. );
  320. }
  321. if (label)
  322. {
  323. root.CanFocus = true;
  324. v.CanFocus = true;
  325. Assert.True (v.HasFocus);
  326. v.SetFocus ();
  327. Assert.True (v.HasFocus);
  328. Application.LayoutAndDraw ();
  329. DriverAssert.AssertDriverAttributesAre (
  330. @"
  331. 222222222222222222220
  332. 111111111111111111110",
  333. output,
  334. Application.Driver,
  335. attributes
  336. );
  337. }
  338. Application.End (runState);
  339. top.Dispose ();
  340. }
  341. }