ScrollViewTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
  367. // [Fact, AutoInitShutdown]
  368. // public void Clear_Window_Inside_ScrollView ()
  369. // {
  370. // var topLabel = new Label ("At 15,0") { X = 15 };
  371. // var sv = new ScrollView {
  372. // X = 3,
  373. // Y = 3,
  374. // Width = 10,
  375. // Height = 10,
  376. // ContentSize = new Size (23, 23),
  377. // KeepContentAlwaysInViewport = false
  378. // };
  379. // var bottomLabel = new Label ("At 15,15") { X = 15, Y = 15 };
  380. // Application.Top.Add (topLabel, sv, bottomLabel);
  381. // Application.Begin (Application.Top);
  382. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  383. // At 15,0
  384. // ▲
  385. // ┬
  386. // ┴
  387. // ░
  388. // ░
  389. // ░
  390. // ░
  391. // ░
  392. // ▼
  393. // ◄├┤░░░░░►
  394. // At 15,15", output);
  395. // var attributes = new Attribute [] {
  396. // Colors.TopLevel.Normal,
  397. // Colors.TopLevel.Focus,
  398. // Colors.Base.Normal
  399. // };
  400. // TestHelpers.AssertDriverColorsAre (@"
  401. //00000000000000000000000
  402. //00000000000000000000000
  403. //00000000000000000000000
  404. //00000000000010000000000
  405. //00000000000010000000000
  406. //00000000000010000000000
  407. //00000000000010000000000
  408. //00000000000010000000000
  409. //00000000000010000000000
  410. //00000000000010000000000
  411. //00000000000010000000000
  412. //00000000000010000000000
  413. //00011111111110000000000
  414. //00000000000000000000000
  415. //00000000000000000000000
  416. //00000000000000000000000", attributes);
  417. // sv.Add (new Window { X = 3, Y = 3, Width = 20, Height = 20 });
  418. // Application.Refresh ();
  419. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  420. // At 15,0
  421. // ▲
  422. // ┬
  423. // ┴
  424. // ┌─────░
  425. // │ ░
  426. // │ ░
  427. // │ ░
  428. // │ ░
  429. // │ ▼
  430. // ◄├┤░░░░░►
  431. // At 15,15", output);
  432. // TestHelpers.AssertDriverColorsAre (@"
  433. //00000000000000000000000
  434. //00000000000000000000000
  435. //00000000000000000000000
  436. //00000000000010000000000
  437. //00000000000010000000000
  438. //00000000000010000000000
  439. //00000022222210000000000
  440. //00000022222210000000000
  441. //00000022222210000000000
  442. //00000022222210000000000
  443. //00000022222210000000000
  444. //00000022222210000000000
  445. //00011111111110000000000
  446. //00000000000000000000000
  447. //00000000000000000000000
  448. //00000000000000000000000", attributes);
  449. // sv.ContentOffset = new Point (20, 20);
  450. // Application.Refresh ();
  451. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  452. // At 15,0
  453. // │ ▲
  454. // │ ░
  455. // ──┘ ░
  456. // ░
  457. // ░
  458. // ┬
  459. // │
  460. // ┴
  461. // ▼
  462. // ◄░░░░├─┤►
  463. // At 15,15", output);
  464. // TestHelpers.AssertDriverColorsAre (@"
  465. //00000000000000000000000
  466. //00000000000000000000000
  467. //00000000000000000000000
  468. //00022200000010000000000
  469. //00022200000010000000000
  470. //00022200000010000000000
  471. //00000000000010000000000
  472. //00000000000010000000000
  473. //00000000000010000000000
  474. //00000000000010000000000
  475. //00000000000010000000000
  476. //00000000000010000000000
  477. //00011111111110000000000
  478. //00000000000000000000000
  479. //00000000000000000000000
  480. //00000000000000000000000", attributes);
  481. // }
  482. [Fact, AutoInitShutdown]
  483. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  484. {
  485. var rule = "0123456789";
  486. var size = new Size (40, 40);
  487. var view = new View (new Rect (Point.Empty, size));
  488. view.Add (new Label (rule.Repeat (size.Width / rule.Length)) { AutoSize = false, Width = Dim.Fill () });
  489. view.Add (new Label (rule.Repeat (size.Height / rule.Length), TextDirection.TopBottom_LeftRight) { Height = Dim.Fill (), AutoSize = false });
  490. view.Add (new Button (1, 1, "Press me!"));
  491. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  492. ContentSize = size,
  493. ShowHorizontalScrollIndicator = true,
  494. ShowVerticalScrollIndicator = true
  495. };
  496. scrollView.Add (view);
  497. var win = new Window (new Rect (1, 1, 20, 14));
  498. win.Add (scrollView);
  499. Application.Top.Add (win);
  500. Application.Begin (Application.Top);
  501. var expected = @"
  502. ┌──────────────────┐
  503. │ │
  504. │ 01234567890123▲ │
  505. │ 1[ Press me! ]┬ │
  506. │ 2 │ │
  507. │ 3 ┴ │
  508. │ 4 ░ │
  509. │ 5 ░ │
  510. │ 6 ░ │
  511. │ 7 ░ │
  512. │ 8 ▼ │
  513. │ ◄├───┤░░░░░░░► │
  514. │ │
  515. └──────────────────┘
  516. "
  517. ;
  518. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  519. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  520. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  521. Application.Top.Redraw (Application.Top.Bounds);
  522. expected = @"
  523. ┌──────────────────┐
  524. │ │
  525. │ 12345678901234▲ │
  526. │ [ Press me! ] ┬ │
  527. │ │ │
  528. │ ┴ │
  529. │ ░ │
  530. │ ░ │
  531. │ ░ │
  532. │ ░ │
  533. │ ▼ │
  534. │ ◄├───┤░░░░░░░► │
  535. │ │
  536. └──────────────────┘
  537. "
  538. ;
  539. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  540. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  541. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  542. Application.Top.Redraw (Application.Top.Bounds);
  543. expected = @"
  544. ┌──────────────────┐
  545. │ │
  546. │ 23456789012345▲ │
  547. │ Press me! ] ┬ │
  548. │ │ │
  549. │ ┴ │
  550. │ ░ │
  551. │ ░ │
  552. │ ░ │
  553. │ ░ │
  554. │ ▼ │
  555. │ ◄├────┤░░░░░░► │
  556. │ │
  557. └──────────────────┘
  558. "
  559. ;
  560. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  561. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  562. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  563. Application.Top.Redraw (Application.Top.Bounds);
  564. expected = @"
  565. ┌──────────────────┐
  566. │ │
  567. │ 34567890123456▲ │
  568. │ Press me! ] ┬ │
  569. │ │ │
  570. │ ┴ │
  571. │ ░ │
  572. │ ░ │
  573. │ ░ │
  574. │ ░ │
  575. │ ▼ │
  576. │ ◄├────┤░░░░░░► │
  577. │ │
  578. └──────────────────┘
  579. "
  580. ;
  581. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  582. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  583. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  584. Application.Top.Redraw (Application.Top.Bounds);
  585. expected = @"
  586. ┌──────────────────┐
  587. │ │
  588. │ 45678901234567▲ │
  589. │ ress me! ] ┬ │
  590. │ │ │
  591. │ ┴ │
  592. │ ░ │
  593. │ ░ │
  594. │ ░ │
  595. │ ░ │
  596. │ ▼ │
  597. │ ◄░├───┤░░░░░░► │
  598. │ │
  599. └──────────────────┘
  600. "
  601. ;
  602. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  603. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  604. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  605. Application.Top.Redraw (Application.Top.Bounds);
  606. expected = @"
  607. ┌──────────────────┐
  608. │ │
  609. │ 56789012345678▲ │
  610. │ ess me! ] ┬ │
  611. │ │ │
  612. │ ┴ │
  613. │ ░ │
  614. │ ░ │
  615. │ ░ │
  616. │ ░ │
  617. │ ▼ │
  618. │ ◄░├────┤░░░░░► │
  619. │ │
  620. └──────────────────┘
  621. "
  622. ;
  623. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  624. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  625. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  626. Application.Top.Redraw (Application.Top.Bounds);
  627. expected = @"
  628. ┌──────────────────┐
  629. │ │
  630. │ 67890123456789▲ │
  631. │ ss me! ] ┬ │
  632. │ │ │
  633. │ ┴ │
  634. │ ░ │
  635. │ ░ │
  636. │ ░ │
  637. │ ░ │
  638. │ ▼ │
  639. │ ◄░├────┤░░░░░► │
  640. │ │
  641. └──────────────────┘
  642. "
  643. ;
  644. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  645. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  646. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  647. Application.Top.Redraw (Application.Top.Bounds);
  648. expected = @"
  649. ┌──────────────────┐
  650. │ │
  651. │ 78901234567890▲ │
  652. │ s me! ] ┬ │
  653. │ │ │
  654. │ ┴ │
  655. │ ░ │
  656. │ ░ │
  657. │ ░ │
  658. │ ░ │
  659. │ ▼ │
  660. │ ◄░░├───┤░░░░░► │
  661. │ │
  662. └──────────────────┘
  663. ";
  664. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  665. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  666. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.End, new KeyModifiers ())));
  667. Application.Top.Redraw (Application.Top.Bounds);
  668. expected = @"
  669. ┌──────────────────┐
  670. │ │
  671. │ 67890123456789▲ │
  672. │ ┬ │
  673. │ │ │
  674. │ ┴ │
  675. │ ░ │
  676. │ ░ │
  677. │ ░ │
  678. │ ░ │
  679. │ ▼ │
  680. │ ◄░░░░░░░├───┤► │
  681. │ │
  682. └──────────────────┘
  683. ";
  684. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  685. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  686. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  687. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  688. Application.Top.Redraw (Application.Top.Bounds);
  689. expected = @"
  690. ┌──────────────────┐
  691. │ │
  692. │ 1[ Press me! ]▲ │
  693. │ 2 ┬ │
  694. │ 3 │ │
  695. │ 4 ┴ │
  696. │ 5 ░ │
  697. │ 6 ░ │
  698. │ 7 ░ │
  699. │ 8 ░ │
  700. │ 9 ▼ │
  701. │ ◄├───┤░░░░░░░► │
  702. │ │
  703. └──────────────────┘
  704. ";
  705. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  706. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  707. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  708. Application.Top.Redraw (Application.Top.Bounds);
  709. expected = @"
  710. ┌──────────────────┐
  711. │ │
  712. │ 2 ▲ │
  713. │ 3 ┬ │
  714. │ 4 │ │
  715. │ 5 ┴ │
  716. │ 6 ░ │
  717. │ 7 ░ │
  718. │ 8 ░ │
  719. │ 9 ░ │
  720. │ 0 ▼ │
  721. │ ◄├───┤░░░░░░░► │
  722. │ │
  723. └──────────────────┘
  724. ";
  725. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  726. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  727. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  728. Application.Top.Redraw (Application.Top.Bounds);
  729. expected = @"
  730. ┌──────────────────┐
  731. │ │
  732. │ 3 ▲ │
  733. │ 4 ┬ │
  734. │ 5 │ │
  735. │ 6 ┴ │
  736. │ 7 ░ │
  737. │ 8 ░ │
  738. │ 9 ░ │
  739. │ 0 ░ │
  740. │ 1 ▼ │
  741. │ ◄├───┤░░░░░░░► │
  742. │ │
  743. └──────────────────┘
  744. ";
  745. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  746. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  747. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  748. Application.Top.Redraw (Application.Top.Bounds);
  749. expected = @"
  750. ┌──────────────────┐
  751. │ │
  752. │ 1 ▲ │
  753. │ 2 ░ │
  754. │ 3 ░ │
  755. │ 4 ░ │
  756. │ 5 ░ │
  757. │ 6 ░ │
  758. │ 7 ┬ │
  759. │ 8 ┴ │
  760. │ 9 ▼ │
  761. │ ◄├───┤░░░░░░░► │
  762. │ │
  763. └──────────────────┘
  764. ";
  765. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  766. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  767. }
  768. }
  769. }