ScrollViewTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using NStack;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests {
  6. public class ScrollViewTests {
  7. readonly ITestOutputHelper output;
  8. public ScrollViewTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. [Fact]
  13. public void Constructors_Defaults ()
  14. {
  15. var sv = new ScrollView ();
  16. Assert.Equal (LayoutStyle.Computed, sv.LayoutStyle);
  17. Assert.True (sv.CanFocus);
  18. Assert.Equal (new Rect (0, 0, 0, 0), sv.Frame);
  19. Assert.Equal (Rect.Empty, sv.Frame);
  20. Assert.Null (sv.X);
  21. Assert.Null (sv.Y);
  22. Assert.Null (sv.Width);
  23. Assert.Null (sv.Height);
  24. Assert.Equal (Point.Empty, sv.ContentOffset);
  25. Assert.Equal (Size.Empty, sv.ContentSize);
  26. Assert.True (sv.AutoHideScrollBars);
  27. Assert.True (sv.KeepContentAlwaysInViewport);
  28. sv = new ScrollView (new Rect (1, 2, 20, 10));
  29. Assert.Equal (LayoutStyle.Absolute, sv.LayoutStyle);
  30. Assert.True (sv.CanFocus);
  31. Assert.Equal (new Rect (1, 2, 20, 10), sv.Frame);
  32. Assert.Null (sv.X);
  33. Assert.Null (sv.Y);
  34. Assert.Null (sv.Width);
  35. Assert.Null (sv.Height);
  36. Assert.Equal (Point.Empty, sv.ContentOffset);
  37. Assert.Equal (Size.Empty, sv.ContentSize);
  38. Assert.True (sv.AutoHideScrollBars);
  39. Assert.True (sv.KeepContentAlwaysInViewport);
  40. }
  41. [Fact]
  42. public void Adding_Views ()
  43. {
  44. var sv = new ScrollView (new Rect (0, 0, 20, 10)) {
  45. ContentSize = new Size (30, 20)
  46. };
  47. sv.Add (new View () { Width = 10, Height = 5 },
  48. new View () { X = 12, Y = 7, Width = 10, Height = 5 });
  49. Assert.Equal (new Size (30, 20), sv.ContentSize);
  50. Assert.Equal (2, sv.Subviews [0].Subviews.Count);
  51. }
  52. [Fact]
  53. public void KeyBindings_Command ()
  54. {
  55. var sv = new ScrollView (new Rect (0, 0, 20, 10)) {
  56. ContentSize = new Size (40, 20)
  57. };
  58. sv.Add (new View () { Width = 20, Height = 5 },
  59. new View () { X = 22, Y = 7, Width = 10, Height = 5 });
  60. sv.BeginInit (); sv.EndInit ();
  61. Assert.True (sv.KeepContentAlwaysInViewport);
  62. Assert.True (sv.AutoHideScrollBars);
  63. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  64. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  65. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  66. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  67. Assert.Equal (new Point (0, -1), sv.ContentOffset);
  68. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  69. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  70. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp, new KeyModifiers ())));
  71. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  72. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  73. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  74. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  75. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  76. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  77. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  78. Assert.True (sv.ProcessKey (new KeyEvent ((Key)'v' | Key.AltMask, new KeyModifiers ())));
  79. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  80. Assert.True (sv.ProcessKey (new KeyEvent (Key.V | Key.CtrlMask, new KeyModifiers ())));
  81. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  82. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  83. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  84. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  85. Assert.Equal (new Point (-1, -10), sv.ContentOffset);
  86. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  87. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  88. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp | Key.CtrlMask, new KeyModifiers ())));
  89. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  90. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  91. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  92. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  93. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  94. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  95. Assert.Equal (new Point (-20, 0), sv.ContentOffset);
  96. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  97. Assert.Equal (new Point (-20, 0), sv.ContentOffset);
  98. Assert.True (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  99. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  100. Assert.False (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  101. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  102. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  103. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  104. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  105. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  106. Assert.True (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  107. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  108. Assert.False (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  109. Assert.Equal (new Point (-20, -10), sv.ContentOffset);
  110. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  111. Assert.Equal (new Point (-20, 0), sv.ContentOffset);
  112. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  113. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  114. sv.KeepContentAlwaysInViewport = false;
  115. Assert.False (sv.KeepContentAlwaysInViewport);
  116. Assert.True (sv.AutoHideScrollBars);
  117. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  118. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  119. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  120. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  121. Assert.Equal (new Point (0, -1), sv.ContentOffset);
  122. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  123. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  124. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp, new KeyModifiers ())));
  125. Assert.Equal (new Point (0, 0), sv.ContentOffset);
  126. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  127. Assert.Equal (new Point (0, -10), sv.ContentOffset);
  128. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  129. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  130. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  131. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  132. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  133. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  134. Assert.True (sv.ProcessKey (new KeyEvent ((Key)'v' | Key.AltMask, new KeyModifiers ())));
  135. Assert.Equal (new Point (0, -9), sv.ContentOffset);
  136. Assert.True (sv.ProcessKey (new KeyEvent (Key.V | Key.CtrlMask, new KeyModifiers ())));
  137. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  138. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  139. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  140. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  141. Assert.Equal (new Point (-1, -19), sv.ContentOffset);
  142. Assert.True (sv.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  143. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  144. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageUp | Key.CtrlMask, new KeyModifiers ())));
  145. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  146. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  147. Assert.Equal (new Point (-20, -19), sv.ContentOffset);
  148. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  149. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  150. Assert.False (sv.ProcessKey (new KeyEvent (Key.PageDown | Key.CtrlMask, new KeyModifiers ())));
  151. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  152. Assert.False (sv.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  153. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  154. Assert.True (sv.ProcessKey (new KeyEvent (Key.PageUp | Key.CtrlMask, new KeyModifiers ())));
  155. Assert.Equal (new Point (-19, -19), sv.ContentOffset);
  156. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  157. Assert.Equal (new Point (-19, 0), sv.ContentOffset);
  158. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  159. Assert.Equal (new Point (-19, 0), sv.ContentOffset);
  160. Assert.True (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  161. Assert.Equal (new Point (-19, -19), sv.ContentOffset);
  162. Assert.False (sv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  163. Assert.Equal (new Point (-19, -19), sv.ContentOffset);
  164. Assert.True (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  165. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  166. Assert.False (sv.ProcessKey (new KeyEvent (Key.Home | Key.CtrlMask, new KeyModifiers ())));
  167. Assert.Equal (new Point (0, -19), sv.ContentOffset);
  168. Assert.True (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  169. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  170. Assert.False (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
  171. Assert.Equal (new Point (-39, -19), sv.ContentOffset);
  172. }
  173. [Fact, AutoInitShutdown]
  174. public void AutoHideScrollBars_False_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  175. {
  176. var sv = new ScrollView {
  177. Width = 10,
  178. Height = 10,
  179. AutoHideScrollBars = false
  180. };
  181. sv.ShowHorizontalScrollIndicator = true;
  182. sv.ShowVerticalScrollIndicator = true;
  183. Application.Top.Add (sv);
  184. Application.Begin (Application.Top);
  185. Assert.False (sv.AutoHideScrollBars);
  186. Assert.True (sv.ShowHorizontalScrollIndicator);
  187. Assert.True (sv.ShowVerticalScrollIndicator);
  188. sv.Redraw (sv.Bounds);
  189. TestHelpers.AssertDriverContentsAre (@"
  190. ◄├─────┤►
  191. ", output);
  192. sv.ShowHorizontalScrollIndicator = false;
  193. sv.ShowVerticalScrollIndicator = true;
  194. Assert.False (sv.AutoHideScrollBars);
  195. Assert.False (sv.ShowHorizontalScrollIndicator);
  196. Assert.True (sv.ShowVerticalScrollIndicator);
  197. sv.Redraw (sv.Bounds);
  198. TestHelpers.AssertDriverContentsAre (@"
  199. ", output);
  200. sv.ShowHorizontalScrollIndicator = true;
  201. sv.ShowVerticalScrollIndicator = false;
  202. Assert.False (sv.AutoHideScrollBars);
  203. Assert.True (sv.ShowHorizontalScrollIndicator);
  204. Assert.False (sv.ShowVerticalScrollIndicator);
  205. sv.Redraw (sv.Bounds);
  206. TestHelpers.AssertDriverContentsAre (@"
  207. ◄├─────┤►
  208. ", output);
  209. sv.ShowHorizontalScrollIndicator = false;
  210. sv.ShowVerticalScrollIndicator = false;
  211. Assert.False (sv.AutoHideScrollBars);
  212. Assert.False (sv.ShowHorizontalScrollIndicator);
  213. Assert.False (sv.ShowVerticalScrollIndicator);
  214. sv.Redraw (sv.Bounds);
  215. TestHelpers.AssertDriverContentsAre (@"
  216. ", output);
  217. }
  218. [Fact, AutoInitShutdown]
  219. public void AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  220. {
  221. var sv = new ScrollView {
  222. Width = 10,
  223. Height = 10
  224. };
  225. Application.Top.Add (sv);
  226. Application.Begin (Application.Top);
  227. Assert.True (sv.AutoHideScrollBars);
  228. Assert.False (sv.ShowHorizontalScrollIndicator);
  229. Assert.False (sv.ShowVerticalScrollIndicator);
  230. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  231. sv.AutoHideScrollBars = false;
  232. sv.ShowHorizontalScrollIndicator = true;
  233. sv.ShowVerticalScrollIndicator = true;
  234. sv.LayoutSubviews ();
  235. sv.Redraw (sv.Bounds);
  236. TestHelpers.AssertDriverContentsWithFrameAre (@"
  237. ◄├─────┤►
  238. ", output);
  239. }
  240. [Fact, AutoInitShutdown]
  241. public void ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  242. {
  243. var sv = new ScrollView {
  244. Width = 10,
  245. Height = 10,
  246. ContentSize = new Size (50, 50)
  247. };
  248. Application.Top.Add (sv);
  249. Application.Begin (Application.Top);
  250. Assert.Equal (50, sv.ContentSize.Width);
  251. Assert.Equal (50, sv.ContentSize.Height);
  252. Assert.True (sv.AutoHideScrollBars);
  253. Assert.True (sv.ShowHorizontalScrollIndicator);
  254. Assert.True (sv.ShowVerticalScrollIndicator);
  255. TestHelpers.AssertDriverContentsWithFrameAre (@"
  256. ◄├┤░░░░░►
  257. ", output);
  258. }
  259. [Fact, AutoInitShutdown]
  260. public void ContentOffset_ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  261. {
  262. var sv = new ScrollView {
  263. Width = 10,
  264. Height = 10,
  265. ContentSize = new Size (50, 50),
  266. ContentOffset = new Point (25, 25)
  267. };
  268. Application.Top.Add (sv);
  269. Application.Begin (Application.Top);
  270. Assert.Equal (-25, sv.ContentOffset.X);
  271. Assert.Equal (-25, sv.ContentOffset.Y);
  272. Assert.Equal (50, sv.ContentSize.Width);
  273. Assert.Equal (50, sv.ContentSize.Height);
  274. Assert.True (sv.AutoHideScrollBars);
  275. Assert.True (sv.ShowHorizontalScrollIndicator);
  276. Assert.True (sv.ShowVerticalScrollIndicator);
  277. TestHelpers.AssertDriverContentsWithFrameAre (@"
  278. ◄░░░├─┤░►
  279. ", output);
  280. }
  281. // BUGBUG: v2 - I can't figure out what this test is trying to test and it fails in weird ways
  282. // Disabling for now
  283. //[Fact, AutoInitShutdown]
  284. //public void Frame_And_Labels_Does_Not_Overspill_ScrollView ()
  285. //{
  286. // var sv = new ScrollView {
  287. // X = 3,
  288. // Y = 3,
  289. // Width = 10,
  290. // Height = 10,
  291. // ContentSize = new Size (50, 50)
  292. // };
  293. // for (int i = 0; i < 8; i++) {
  294. // sv.Add (new CustomButton ("█", $"Button {i}", 20, 3) { Y = i * 3 });
  295. // }
  296. // Application.Top.Add (sv);
  297. // Application.Begin (Application.Top);
  298. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  299. // █████████▲
  300. // ██████But┬
  301. // █████████┴
  302. // ┌────────░
  303. // │ But░
  304. // └────────░
  305. // ┌────────░
  306. // │ But░
  307. // └────────▼
  308. // ◄├┤░░░░░► ", output);
  309. // sv.ContentOffset = new Point (5, 5);
  310. // sv.LayoutSubviews ();
  311. // Application.Refresh ();
  312. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  313. // ─────────▲
  314. // ─────────┬
  315. // Button 2│
  316. // ─────────┴
  317. // ─────────░
  318. // Button 3░
  319. // ─────────░
  320. // ─────────░
  321. // Button 4▼
  322. // ◄├─┤░░░░► ", output);
  323. //}
  324. //private class CustomButton : FrameView {
  325. // private Label labelFill;
  326. // private Label labelText;
  327. // public CustomButton (string fill, ustring text, int width, int height) : base ()
  328. // {
  329. // Width = width;
  330. // Height = height;
  331. // labelFill = new Label () { AutoSize = false, X = Pos.Center (), Y = Pos.Center (), Width = Dim.Fill (), Height = Dim.Fill (), Visible = false };
  332. // labelFill.LayoutComplete += (s, e) => {
  333. // var fillText = new System.Text.StringBuilder ();
  334. // for (int i = 0; i < labelFill.Bounds.Height; i++) {
  335. // if (i > 0) {
  336. // fillText.AppendLine ("");
  337. // }
  338. // for (int j = 0; j < labelFill.Bounds.Width; j++) {
  339. // fillText.Append (fill);
  340. // }
  341. // }
  342. // labelFill.Text = fillText.ToString ();
  343. // };
  344. // labelText = new Label (text) { X = Pos.Center (), Y = Pos.Center () };
  345. // Add (labelFill, labelText);
  346. // CanFocus = true;
  347. // }
  348. // public override bool OnEnter (View view)
  349. // {
  350. // Border.BorderStyle = BorderStyle.None;
  351. // Border.DrawMarginFrame = false;
  352. // labelFill.Visible = true;
  353. // view = this;
  354. // return base.OnEnter (view);
  355. // }
  356. // public override bool OnLeave (View view)
  357. // {
  358. // Border.BorderStyle = BorderStyle.Single;
  359. // Border.DrawMarginFrame = true;
  360. // labelFill.Visible = false;
  361. // if (view == null)
  362. // view = this;
  363. // return base.OnLeave (view);
  364. // }
  365. //}
  366. [Fact, AutoInitShutdown]
  367. public void Clear_Window_Inside_ScrollView ()
  368. {
  369. var topLabel = new Label ("At 15,0") { X = 15 };
  370. var sv = new ScrollView {
  371. X = 3,
  372. Y = 3,
  373. Width = 10,
  374. Height = 10,
  375. ContentSize = new Size (23, 23),
  376. KeepContentAlwaysInViewport = false
  377. };
  378. var bottomLabel = new Label ("At 15,15") { X = 15, Y = 15 };
  379. Application.Top.Add (topLabel, sv, bottomLabel);
  380. Application.Begin (Application.Top);
  381. TestHelpers.AssertDriverContentsWithFrameAre (@"
  382. At 15,0
  383. ◄├┤░░░░░►
  384. At 15,15", output);
  385. var attributes = new Attribute [] {
  386. Colors.TopLevel.Normal,
  387. Colors.TopLevel.Focus,
  388. Colors.Base.Normal
  389. };
  390. TestHelpers.AssertDriverColorsAre (@"
  391. 00000000000000000000000
  392. 00000000000000000000000
  393. 00000000000000000000000
  394. 00000000000010000000000
  395. 00000000000010000000000
  396. 00000000000010000000000
  397. 00000000000010000000000
  398. 00000000000010000000000
  399. 00000000000010000000000
  400. 00000000000010000000000
  401. 00000000000010000000000
  402. 00000000000010000000000
  403. 00011111111110000000000
  404. 00000000000000000000000
  405. 00000000000000000000000
  406. 00000000000000000000000", attributes);
  407. sv.Add (new Window ("1") { X = 3, Y = 3, Width = 20, Height = 20 });
  408. Application.Refresh ();
  409. TestHelpers.AssertDriverContentsWithFrameAre (@"
  410. At 15,0
  411. ┌┤1├──░
  412. │ ░
  413. │ ░
  414. │ ░
  415. │ ░
  416. │ ▼
  417. ◄├┤░░░░░►
  418. At 15,15", output);
  419. TestHelpers.AssertDriverColorsAre (@"
  420. 00000000000000000000000
  421. 00000000000000000000000
  422. 00000000000000000000000
  423. 00000000000010000000000
  424. 00000000000010000000000
  425. 00000000000010000000000
  426. 00000022222210000000000
  427. 00000022222210000000000
  428. 00000022222210000000000
  429. 00000022222210000000000
  430. 00000022222210000000000
  431. 00000022222210000000000
  432. 00011111111110000000000
  433. 00000000000000000000000
  434. 00000000000000000000000
  435. 00000000000000000000000", attributes);
  436. sv.ContentOffset = new Point (20, 20);
  437. Application.Refresh ();
  438. TestHelpers.AssertDriverContentsWithFrameAre (@"
  439. At 15,0
  440. │ ▲
  441. │ ░
  442. ──┘ ░
  443. ◄░░░░├─┤►
  444. At 15,15", output);
  445. TestHelpers.AssertDriverColorsAre (@"
  446. 00000000000000000000000
  447. 00000000000000000000000
  448. 00000000000000000000000
  449. 00022200000010000000000
  450. 00022200000010000000000
  451. 00022200000010000000000
  452. 00000000000010000000000
  453. 00000000000010000000000
  454. 00000000000010000000000
  455. 00000000000010000000000
  456. 00000000000010000000000
  457. 00000000000010000000000
  458. 00011111111110000000000
  459. 00000000000000000000000
  460. 00000000000000000000000
  461. 00000000000000000000000", attributes);
  462. }
  463. [Fact, AutoInitShutdown]
  464. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  465. {
  466. var rule = "0123456789";
  467. var size = new Size (40, 40);
  468. var view = new View (new Rect (Point.Empty, size));
  469. view.Add (new Label (rule.Repeat (size.Width / rule.Length)) { AutoSize = false, Width = Dim.Fill () });
  470. view.Add (new Label (rule.Repeat (size.Height / rule.Length), TextDirection.TopBottom_LeftRight) { Height = Dim.Fill (), AutoSize = false });
  471. view.Add (new Button (1, 1, "Press me!"));
  472. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  473. ContentSize = size,
  474. ShowHorizontalScrollIndicator = true,
  475. ShowVerticalScrollIndicator = true
  476. };
  477. scrollView.Add (view);
  478. var win = new Window (new Rect (1, 1, 20, 14), "");
  479. win.Add (scrollView);
  480. Application.Top.Add (win);
  481. Application.Begin (Application.Top);
  482. var expected = @"
  483. ┌──────────────────┐
  484. │ │
  485. │ 01234567890123▲ │
  486. │ 1[ Press me! ]┬ │
  487. │ 2 │ │
  488. │ 3 ┴ │
  489. │ 4 ░ │
  490. │ 5 ░ │
  491. │ 6 ░ │
  492. │ 7 ░ │
  493. │ 8 ▼ │
  494. │ ◄├───┤░░░░░░░► │
  495. │ │
  496. └──────────────────┘
  497. "
  498. ;
  499. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  500. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  501. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  502. Application.Top.Redraw (Application.Top.Bounds);
  503. expected = @"
  504. ┌──────────────────┐
  505. │ │
  506. │ 12345678901234▲ │
  507. │ [ Press me! ] ┬ │
  508. │ │ │
  509. │ ┴ │
  510. │ ░ │
  511. │ ░ │
  512. │ ░ │
  513. │ ░ │
  514. │ ▼ │
  515. │ ◄├───┤░░░░░░░► │
  516. │ │
  517. └──────────────────┘
  518. "
  519. ;
  520. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  521. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  522. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  523. Application.Top.Redraw (Application.Top.Bounds);
  524. expected = @"
  525. ┌──────────────────┐
  526. │ │
  527. │ 23456789012345▲ │
  528. │ Press me! ] ┬ │
  529. │ │ │
  530. │ ┴ │
  531. │ ░ │
  532. │ ░ │
  533. │ ░ │
  534. │ ░ │
  535. │ ▼ │
  536. │ ◄├────┤░░░░░░► │
  537. │ │
  538. └──────────────────┘
  539. "
  540. ;
  541. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  542. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  543. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  544. Application.Top.Redraw (Application.Top.Bounds);
  545. expected = @"
  546. ┌──────────────────┐
  547. │ │
  548. │ 34567890123456▲ │
  549. │ Press me! ] ┬ │
  550. │ │ │
  551. │ ┴ │
  552. │ ░ │
  553. │ ░ │
  554. │ ░ │
  555. │ ░ │
  556. │ ▼ │
  557. │ ◄├────┤░░░░░░► │
  558. │ │
  559. └──────────────────┘
  560. "
  561. ;
  562. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  563. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  564. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  565. Application.Top.Redraw (Application.Top.Bounds);
  566. expected = @"
  567. ┌──────────────────┐
  568. │ │
  569. │ 45678901234567▲ │
  570. │ ress me! ] ┬ │
  571. │ │ │
  572. │ ┴ │
  573. │ ░ │
  574. │ ░ │
  575. │ ░ │
  576. │ ░ │
  577. │ ▼ │
  578. │ ◄░├───┤░░░░░░► │
  579. │ │
  580. └──────────────────┘
  581. "
  582. ;
  583. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  584. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  585. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  586. Application.Top.Redraw (Application.Top.Bounds);
  587. expected = @"
  588. ┌──────────────────┐
  589. │ │
  590. │ 56789012345678▲ │
  591. │ ess me! ] ┬ │
  592. │ │ │
  593. │ ┴ │
  594. │ ░ │
  595. │ ░ │
  596. │ ░ │
  597. │ ░ │
  598. │ ▼ │
  599. │ ◄░├────┤░░░░░► │
  600. │ │
  601. └──────────────────┘
  602. "
  603. ;
  604. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  605. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  606. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  607. Application.Top.Redraw (Application.Top.Bounds);
  608. expected = @"
  609. ┌──────────────────┐
  610. │ │
  611. │ 67890123456789▲ │
  612. │ ss me! ] ┬ │
  613. │ │ │
  614. │ ┴ │
  615. │ ░ │
  616. │ ░ │
  617. │ ░ │
  618. │ ░ │
  619. │ ▼ │
  620. │ ◄░├────┤░░░░░► │
  621. │ │
  622. └──────────────────┘
  623. "
  624. ;
  625. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  626. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  627. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  628. Application.Top.Redraw (Application.Top.Bounds);
  629. expected = @"
  630. ┌──────────────────┐
  631. │ │
  632. │ 78901234567890▲ │
  633. │ s me! ] ┬ │
  634. │ │ │
  635. │ ┴ │
  636. │ ░ │
  637. │ ░ │
  638. │ ░ │
  639. │ ░ │
  640. │ ▼ │
  641. │ ◄░░├───┤░░░░░► │
  642. │ │
  643. └──────────────────┘
  644. ";
  645. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  646. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  647. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.End, new KeyModifiers ())));
  648. Application.Top.Redraw (Application.Top.Bounds);
  649. expected = @"
  650. ┌──────────────────┐
  651. │ │
  652. │ 67890123456789▲ │
  653. │ ┬ │
  654. │ │ │
  655. │ ┴ │
  656. │ ░ │
  657. │ ░ │
  658. │ ░ │
  659. │ ░ │
  660. │ ▼ │
  661. │ ◄░░░░░░░├───┤► │
  662. │ │
  663. └──────────────────┘
  664. ";
  665. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  666. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  667. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  668. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  669. Application.Top.Redraw (Application.Top.Bounds);
  670. expected = @"
  671. ┌──────────────────┐
  672. │ │
  673. │ 1[ Press me! ]▲ │
  674. │ 2 ┬ │
  675. │ 3 │ │
  676. │ 4 ┴ │
  677. │ 5 ░ │
  678. │ 6 ░ │
  679. │ 7 ░ │
  680. │ 8 ░ │
  681. │ 9 ▼ │
  682. │ ◄├───┤░░░░░░░► │
  683. │ │
  684. └──────────────────┘
  685. ";
  686. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  687. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  688. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  689. Application.Top.Redraw (Application.Top.Bounds);
  690. expected = @"
  691. ┌──────────────────┐
  692. │ │
  693. │ 2 ▲ │
  694. │ 3 ┬ │
  695. │ 4 │ │
  696. │ 5 ┴ │
  697. │ 6 ░ │
  698. │ 7 ░ │
  699. │ 8 ░ │
  700. │ 9 ░ │
  701. │ 0 ▼ │
  702. │ ◄├───┤░░░░░░░► │
  703. │ │
  704. └──────────────────┘
  705. ";
  706. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  707. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  708. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  709. Application.Top.Redraw (Application.Top.Bounds);
  710. expected = @"
  711. ┌──────────────────┐
  712. │ │
  713. │ 3 ▲ │
  714. │ 4 ┬ │
  715. │ 5 │ │
  716. │ 6 ┴ │
  717. │ 7 ░ │
  718. │ 8 ░ │
  719. │ 9 ░ │
  720. │ 0 ░ │
  721. │ 1 ▼ │
  722. │ ◄├───┤░░░░░░░► │
  723. │ │
  724. └──────────────────┘
  725. ";
  726. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  727. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  728. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  729. Application.Top.Redraw (Application.Top.Bounds);
  730. expected = @"
  731. ┌──────────────────┐
  732. │ │
  733. │ 1 ▲ │
  734. │ 2 ░ │
  735. │ 3 ░ │
  736. │ 4 ░ │
  737. │ 5 ░ │
  738. │ 6 ░ │
  739. │ 7 ┬ │
  740. │ 8 ┴ │
  741. │ 9 ▼ │
  742. │ ◄├───┤░░░░░░░► │
  743. │ │
  744. └──────────────────┘
  745. ";
  746. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  747. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  748. }
  749. }
  750. }