ScrollViewTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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.False (sv.AutoHideScrollBars);
  178. Assert.True (sv.ShowHorizontalScrollIndicator);
  179. Assert.True (sv.ShowVerticalScrollIndicator);
  180. sv.Draw ();
  181. TestHelpers.AssertDriverContentsAre (@"
  182. ◄├─────┤►
  183. ", output);
  184. sv.ShowHorizontalScrollIndicator = false;
  185. sv.ShowVerticalScrollIndicator = true;
  186. Assert.False (sv.AutoHideScrollBars);
  187. Assert.False (sv.ShowHorizontalScrollIndicator);
  188. Assert.True (sv.ShowVerticalScrollIndicator);
  189. sv.Draw ();
  190. TestHelpers.AssertDriverContentsAre (@"
  191. ", output);
  192. sv.ShowHorizontalScrollIndicator = true;
  193. sv.ShowVerticalScrollIndicator = false;
  194. Assert.False (sv.AutoHideScrollBars);
  195. Assert.True (sv.ShowHorizontalScrollIndicator);
  196. Assert.False (sv.ShowVerticalScrollIndicator);
  197. sv.Draw ();
  198. TestHelpers.AssertDriverContentsAre (@"
  199. ◄├─────┤►
  200. ", output);
  201. sv.ShowHorizontalScrollIndicator = false;
  202. sv.ShowVerticalScrollIndicator = false;
  203. Assert.False (sv.AutoHideScrollBars);
  204. Assert.False (sv.ShowHorizontalScrollIndicator);
  205. Assert.False (sv.ShowVerticalScrollIndicator);
  206. sv.Draw ();
  207. TestHelpers.AssertDriverContentsAre (@"
  208. ", output);
  209. }
  210. [Fact, AutoInitShutdown]
  211. public void AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  212. {
  213. var sv = new ScrollView {
  214. Width = 10,
  215. Height = 10
  216. };
  217. Application.Top.Add (sv);
  218. Application.Begin (Application.Top);
  219. Assert.True (sv.AutoHideScrollBars);
  220. Assert.False (sv.ShowHorizontalScrollIndicator);
  221. Assert.False (sv.ShowVerticalScrollIndicator);
  222. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  223. sv.AutoHideScrollBars = false;
  224. sv.ShowHorizontalScrollIndicator = true;
  225. sv.ShowVerticalScrollIndicator = true;
  226. sv.LayoutSubviews ();
  227. sv.Draw ();
  228. TestHelpers.AssertDriverContentsWithFrameAre (@"
  229. ◄├─────┤►
  230. ", output);
  231. }
  232. [Fact, AutoInitShutdown]
  233. public void ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  234. {
  235. var sv = new ScrollView {
  236. Width = 10,
  237. Height = 10,
  238. ContentSize = new Size (50, 50)
  239. };
  240. Application.Top.Add (sv);
  241. Application.Begin (Application.Top);
  242. Assert.Equal (50, sv.ContentSize.Width);
  243. Assert.Equal (50, sv.ContentSize.Height);
  244. Assert.True (sv.AutoHideScrollBars);
  245. Assert.True (sv.ShowHorizontalScrollIndicator);
  246. Assert.True (sv.ShowVerticalScrollIndicator);
  247. TestHelpers.AssertDriverContentsWithFrameAre (@"
  248. ◄├┤░░░░░►
  249. ", output);
  250. }
  251. [Fact, AutoInitShutdown]
  252. public void ContentOffset_ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  253. {
  254. var sv = new ScrollView {
  255. Width = 10,
  256. Height = 10,
  257. ContentSize = new Size (50, 50),
  258. ContentOffset = new Point (25, 25)
  259. };
  260. Application.Top.Add (sv);
  261. Application.Begin (Application.Top);
  262. Assert.Equal (-25, sv.ContentOffset.X);
  263. Assert.Equal (-25, sv.ContentOffset.Y);
  264. Assert.Equal (50, sv.ContentSize.Width);
  265. Assert.Equal (50, sv.ContentSize.Height);
  266. Assert.True (sv.AutoHideScrollBars);
  267. Assert.True (sv.ShowHorizontalScrollIndicator);
  268. Assert.True (sv.ShowVerticalScrollIndicator);
  269. TestHelpers.AssertDriverContentsWithFrameAre (@"
  270. ◄░░░├─┤░►
  271. ", output);
  272. }
  273. // BUGBUG: v2 - I can't figure out what this test is trying to test and it fails in weird ways
  274. // Disabling for now
  275. //[Fact, AutoInitShutdown]
  276. //public void Frame_And_Labels_Does_Not_Overspill_ScrollView ()
  277. //{
  278. // var sv = new ScrollView {
  279. // X = 3,
  280. // Y = 3,
  281. // Width = 10,
  282. // Height = 10,
  283. // ContentSize = new Size (50, 50)
  284. // };
  285. // for (int i = 0; i < 8; i++) {
  286. // sv.Add (new CustomButton ("█", $"Button {i}", 20, 3) { Y = i * 3 });
  287. // }
  288. // Application.Top.Add (sv);
  289. // Application.Begin (Application.Top);
  290. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  291. // █████████▲
  292. // ██████But┬
  293. // █████████┴
  294. // ┌────────░
  295. // │ But░
  296. // └────────░
  297. // ┌────────░
  298. // │ But░
  299. // └────────▼
  300. // ◄├┤░░░░░► ", output);
  301. // sv.ContentOffset = new Point (5, 5);
  302. // sv.LayoutSubviews ();
  303. // Application.Refresh ();
  304. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  305. // ─────────▲
  306. // ─────────┬
  307. // Button 2│
  308. // ─────────┴
  309. // ─────────░
  310. // Button 3░
  311. // ─────────░
  312. // ─────────░
  313. // Button 4▼
  314. // ◄├─┤░░░░► ", output);
  315. //}
  316. //private class CustomButton : FrameView {
  317. // private Label labelFill;
  318. // private Label labelText;
  319. // public CustomButton (string fill, string text, int width, int height) : base ()
  320. // {
  321. // Width = width;
  322. // Height = height;
  323. // labelFill = new Label () { AutoSize = false, X = Pos.Center (), Y = Pos.Center (), Width = Dim.Fill (), Height = Dim.Fill (), Visible = false };
  324. // labelFill.LayoutComplete += (s, e) => {
  325. // var fillText = new System.Text.StringBuilder ();
  326. // for (int i = 0; i < labelFill.Bounds.Height; i++) {
  327. // if (i > 0) {
  328. // fillText.AppendLine ("");
  329. // }
  330. // for (int j = 0; j < labelFill.Bounds.Width; j++) {
  331. // fillText.Append (fill);
  332. // }
  333. // }
  334. // labelFill.Text = fillText;
  335. // };
  336. // labelText = new Label (text) { X = Pos.Center (), Y = Pos.Center () };
  337. // Add (labelFill, labelText);
  338. // CanFocus = true;
  339. // }
  340. // public override bool OnEnter (View view)
  341. // {
  342. // Border.BorderStyle = BorderStyle.None;
  343. // Border.DrawMarginFrame = false;
  344. // labelFill.Visible = true;
  345. // view = this;
  346. // return base.OnEnter (view);
  347. // }
  348. // public override bool OnLeave (View view)
  349. // {
  350. // Border.BorderStyle = BorderStyle.Single;
  351. // Border.DrawMarginFrame = true;
  352. // labelFill.Visible = false;
  353. // if (view == null)
  354. // view = this;
  355. // return base.OnLeave (view);
  356. // }
  357. //}
  358. // BUGBUG: Broke this test with #2483 - @bdisp I need your help figuring out why
  359. // [Fact, AutoInitShutdown]
  360. // public void Clear_Window_Inside_ScrollView ()
  361. // {
  362. // var topLabel = new Label ("At 15,0") { X = 15 };
  363. // var sv = new ScrollView {
  364. // X = 3,
  365. // Y = 3,
  366. // Width = 10,
  367. // Height = 10,
  368. // ContentSize = new Size (23, 23),
  369. // KeepContentAlwaysInViewport = false
  370. // };
  371. // var bottomLabel = new Label ("At 15,15") { X = 15, Y = 15 };
  372. // Application.Top.Add (topLabel, sv, bottomLabel);
  373. // Application.Begin (Application.Top);
  374. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  375. // At 15,0
  376. // ▲
  377. // ┬
  378. // ┴
  379. // ░
  380. // ░
  381. // ░
  382. // ░
  383. // ░
  384. // ▼
  385. // ◄├┤░░░░░►
  386. // At 15,15", output);
  387. // var attributes = new Attribute [] {
  388. // Colors.TopLevel.Normal,
  389. // Colors.TopLevel.Focus,
  390. // Colors.Base.Normal
  391. // };
  392. // TestHelpers.AssertDriverColorsAre (@"
  393. //00000000000000000000000
  394. //00000000000000000000000
  395. //00000000000000000000000
  396. //00000000000010000000000
  397. //00000000000010000000000
  398. //00000000000010000000000
  399. //00000000000010000000000
  400. //00000000000010000000000
  401. //00000000000010000000000
  402. //00000000000010000000000
  403. //00000000000010000000000
  404. //00000000000010000000000
  405. //00011111111110000000000
  406. //00000000000000000000000
  407. //00000000000000000000000
  408. //00000000000000000000000", attributes);
  409. // sv.Add (new Window { X = 3, Y = 3, Width = 20, Height = 20 });
  410. // Application.Refresh ();
  411. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  412. // At 15,0
  413. // ▲
  414. // ┬
  415. // ┴
  416. // ┌─────░
  417. // │ ░
  418. // │ ░
  419. // │ ░
  420. // │ ░
  421. // │ ▼
  422. // ◄├┤░░░░░►
  423. // At 15,15", output);
  424. // TestHelpers.AssertDriverColorsAre (@"
  425. //00000000000000000000000
  426. //00000000000000000000000
  427. //00000000000000000000000
  428. //00000000000010000000000
  429. //00000000000010000000000
  430. //00000000000010000000000
  431. //00000022222210000000000
  432. //00000022222210000000000
  433. //00000022222210000000000
  434. //00000022222210000000000
  435. //00000022222210000000000
  436. //00000022222210000000000
  437. //00011111111110000000000
  438. //00000000000000000000000
  439. //00000000000000000000000
  440. //00000000000000000000000", attributes);
  441. // sv.ContentOffset = new Point (20, 20);
  442. // Application.Refresh ();
  443. // TestHelpers.AssertDriverContentsWithFrameAre (@"
  444. // At 15,0
  445. // │ ▲
  446. // │ ░
  447. // ──┘ ░
  448. // ░
  449. // ░
  450. // ┬
  451. // │
  452. // ┴
  453. // ▼
  454. // ◄░░░░├─┤►
  455. // At 15,15", output);
  456. // TestHelpers.AssertDriverColorsAre (@"
  457. //00000000000000000000000
  458. //00000000000000000000000
  459. //00000000000000000000000
  460. //00022200000010000000000
  461. //00022200000010000000000
  462. //00022200000010000000000
  463. //00000000000010000000000
  464. //00000000000010000000000
  465. //00000000000010000000000
  466. //00000000000010000000000
  467. //00000000000010000000000
  468. //00000000000010000000000
  469. //00011111111110000000000
  470. //00000000000000000000000
  471. //00000000000000000000000
  472. //00000000000000000000000", attributes);
  473. // }
  474. [Fact, AutoInitShutdown]
  475. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  476. {
  477. var rule = "0123456789";
  478. var size = new Size (40, 40);
  479. var view = new View (new Rect (Point.Empty, size));
  480. view.Add (new Label (rule.Repeat (size.Width / rule.Length)) { AutoSize = false, Width = Dim.Fill () });
  481. view.Add (new Label (rule.Repeat (size.Height / rule.Length), TextDirection.TopBottom_LeftRight) { Height = Dim.Fill (), AutoSize = false });
  482. view.Add (new Label (1, 1, "[ Press me! ]"));
  483. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  484. ContentSize = size,
  485. ShowHorizontalScrollIndicator = true,
  486. ShowVerticalScrollIndicator = true
  487. };
  488. scrollView.Add (view);
  489. var win = new Window (new Rect (1, 1, 20, 14));
  490. win.Add (scrollView);
  491. Application.Top.Add (win);
  492. Application.Begin (Application.Top);
  493. var expected = @"
  494. ┌──────────────────┐
  495. │ │
  496. │ 01234567890123▲ │
  497. │ 1[ Press me! ]┬ │
  498. │ 2 │ │
  499. │ 3 ┴ │
  500. │ 4 ░ │
  501. │ 5 ░ │
  502. │ 6 ░ │
  503. │ 7 ░ │
  504. │ 8 ▼ │
  505. │ ◄├───┤░░░░░░░► │
  506. │ │
  507. └──────────────────┘
  508. "
  509. ;
  510. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  511. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  512. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  513. Application.Top.Draw ();
  514. expected = @"
  515. ┌──────────────────┐
  516. │ │
  517. │ 12345678901234▲ │
  518. │ [ Press me! ] ┬ │
  519. │ │ │
  520. │ ┴ │
  521. │ ░ │
  522. │ ░ │
  523. │ ░ │
  524. │ ░ │
  525. │ ▼ │
  526. │ ◄├───┤░░░░░░░► │
  527. │ │
  528. └──────────────────┘
  529. "
  530. ;
  531. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  532. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  533. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  534. Application.Top.Draw ();
  535. expected = @"
  536. ┌──────────────────┐
  537. │ │
  538. │ 23456789012345▲ │
  539. │ Press me! ] ┬ │
  540. │ │ │
  541. │ ┴ │
  542. │ ░ │
  543. │ ░ │
  544. │ ░ │
  545. │ ░ │
  546. │ ▼ │
  547. │ ◄├────┤░░░░░░► │
  548. │ │
  549. └──────────────────┘
  550. "
  551. ;
  552. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  553. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  554. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  555. Application.Top.Draw ();
  556. expected = @"
  557. ┌──────────────────┐
  558. │ │
  559. │ 34567890123456▲ │
  560. │ Press me! ] ┬ │
  561. │ │ │
  562. │ ┴ │
  563. │ ░ │
  564. │ ░ │
  565. │ ░ │
  566. │ ░ │
  567. │ ▼ │
  568. │ ◄├────┤░░░░░░► │
  569. │ │
  570. └──────────────────┘
  571. "
  572. ;
  573. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  574. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  575. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  576. Application.Top.Draw ();
  577. expected = @"
  578. ┌──────────────────┐
  579. │ │
  580. │ 45678901234567▲ │
  581. │ ress me! ] ┬ │
  582. │ │ │
  583. │ ┴ │
  584. │ ░ │
  585. │ ░ │
  586. │ ░ │
  587. │ ░ │
  588. │ ▼ │
  589. │ ◄░├───┤░░░░░░► │
  590. │ │
  591. └──────────────────┘
  592. "
  593. ;
  594. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  595. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  596. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  597. Application.Top.Draw ();
  598. expected = @"
  599. ┌──────────────────┐
  600. │ │
  601. │ 56789012345678▲ │
  602. │ ess me! ] ┬ │
  603. │ │ │
  604. │ ┴ │
  605. │ ░ │
  606. │ ░ │
  607. │ ░ │
  608. │ ░ │
  609. │ ▼ │
  610. │ ◄░├────┤░░░░░► │
  611. │ │
  612. └──────────────────┘
  613. "
  614. ;
  615. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  616. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  617. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  618. Application.Top.Draw ();
  619. expected = @"
  620. ┌──────────────────┐
  621. │ │
  622. │ 67890123456789▲ │
  623. │ ss me! ] ┬ │
  624. │ │ │
  625. │ ┴ │
  626. │ ░ │
  627. │ ░ │
  628. │ ░ │
  629. │ ░ │
  630. │ ▼ │
  631. │ ◄░├────┤░░░░░► │
  632. │ │
  633. └──────────────────┘
  634. "
  635. ;
  636. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  637. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  638. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorRight)));
  639. Application.Top.Draw ();
  640. expected = @"
  641. ┌──────────────────┐
  642. │ │
  643. │ 78901234567890▲ │
  644. │ s me! ] ┬ │
  645. │ │ │
  646. │ ┴ │
  647. │ ░ │
  648. │ ░ │
  649. │ ░ │
  650. │ ░ │
  651. │ ▼ │
  652. │ ◄░░├───┤░░░░░► │
  653. │ │
  654. └──────────────────┘
  655. ";
  656. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  657. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  658. Assert.True (scrollView.OnKeyDown (new (KeyCode.CtrlMask | KeyCode.End)));
  659. Application.Top.Draw ();
  660. expected = @"
  661. ┌──────────────────┐
  662. │ │
  663. │ 67890123456789▲ │
  664. │ ┬ │
  665. │ │ │
  666. │ ┴ │
  667. │ ░ │
  668. │ ░ │
  669. │ ░ │
  670. │ ░ │
  671. │ ▼ │
  672. │ ◄░░░░░░░├───┤► │
  673. │ │
  674. └──────────────────┘
  675. ";
  676. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  677. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  678. Assert.True (scrollView.OnKeyDown (new (KeyCode.CtrlMask | KeyCode.Home)));
  679. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorDown)));
  680. Application.Top.Draw ();
  681. expected = @"
  682. ┌──────────────────┐
  683. │ │
  684. │ 1[ Press me! ]▲ │
  685. │ 2 ┬ │
  686. │ 3 │ │
  687. │ 4 ┴ │
  688. │ 5 ░ │
  689. │ 6 ░ │
  690. │ 7 ░ │
  691. │ 8 ░ │
  692. │ 9 ▼ │
  693. │ ◄├───┤░░░░░░░► │
  694. │ │
  695. └──────────────────┘
  696. ";
  697. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  698. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  699. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorDown)));
  700. Application.Top.Draw ();
  701. expected = @"
  702. ┌──────────────────┐
  703. │ │
  704. │ 2 ▲ │
  705. │ 3 ┬ │
  706. │ 4 │ │
  707. │ 5 ┴ │
  708. │ 6 ░ │
  709. │ 7 ░ │
  710. │ 8 ░ │
  711. │ 9 ░ │
  712. │ 0 ▼ │
  713. │ ◄├───┤░░░░░░░► │
  714. │ │
  715. └──────────────────┘
  716. ";
  717. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  718. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  719. Assert.True (scrollView.OnKeyDown (new (KeyCode.CursorDown)));
  720. Application.Top.Draw ();
  721. expected = @"
  722. ┌──────────────────┐
  723. │ │
  724. │ 3 ▲ │
  725. │ 4 ┬ │
  726. │ 5 │ │
  727. │ 6 ┴ │
  728. │ 7 ░ │
  729. │ 8 ░ │
  730. │ 9 ░ │
  731. │ 0 ░ │
  732. │ 1 ▼ │
  733. │ ◄├───┤░░░░░░░► │
  734. │ │
  735. └──────────────────┘
  736. ";
  737. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  738. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  739. Assert.True (scrollView.OnKeyDown (new (KeyCode.End)));
  740. Application.Top.Draw ();
  741. expected = @"
  742. ┌──────────────────┐
  743. │ │
  744. │ 1 ▲ │
  745. │ 2 ░ │
  746. │ 3 ░ │
  747. │ 4 ░ │
  748. │ 5 ░ │
  749. │ 6 ░ │
  750. │ 7 ┬ │
  751. │ 8 ┴ │
  752. │ 9 ▼ │
  753. │ ◄├───┤░░░░░░░► │
  754. │ │
  755. └──────────────────┘
  756. ";
  757. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  758. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  759. }
  760. [Fact, AutoInitShutdown]
  761. public void Remove_Added_View_Is_Allowed ()
  762. {
  763. var sv = new ScrollView () {
  764. Width = 20,
  765. Height = 20,
  766. ContentSize = new Size (100, 100)
  767. };
  768. sv.Add (new View () { Width = Dim.Fill (), Height = Dim.Fill (50), Id = "View1" },
  769. new View () { Y = 51, Width = Dim.Fill (), Height = Dim.Fill (), Id = "View2" });
  770. Application.Top.Add (sv);
  771. Application.Begin (Application.Top);
  772. Assert.Equal (4, sv.Subviews.Count);
  773. Assert.Equal (2, sv.Subviews [0].Subviews.Count);
  774. sv.Remove (sv.Subviews [0].Subviews [1]);
  775. Assert.Equal (4, sv.Subviews.Count);
  776. Assert.Single (sv.Subviews [0].Subviews);
  777. Assert.Equal ("View1", sv.Subviews [0].Subviews [0].Id);
  778. }
  779. }
  780. }