ScrollViewTests.cs 37 KB

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