ScrollViewTests.cs 30 KB

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