ScrollTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class ScrollTests
  4. {
  5. public ScrollTests (ITestOutputHelper output) { _output = output; }
  6. private readonly ITestOutputHelper _output;
  7. [Theory]
  8. [AutoInitShutdown]
  9. [InlineData (
  10. 20,
  11. @"
  12. ░",
  13. @"
  14. ░",
  15. @"
  16. █",
  17. @"
  18. ░",
  19. @"
  20. █████░░░░░",
  21. @"
  22. ░░█████░░░",
  23. @"
  24. ░░░░░█████",
  25. @"
  26. ░░██░░░░░░")]
  27. [InlineData (
  28. 40,
  29. @"
  30. ░",
  31. @"
  32. ░",
  33. @"
  34. ░",
  35. @"
  36. ░",
  37. @"
  38. ██░░░░░░░░",
  39. @"
  40. ░██░░░░░░░",
  41. @"
  42. ░░██░░░░░░",
  43. @"
  44. ░█░░░░░░░░")]
  45. public void Changing_Position_Size_Orientation_Draws_Correctly (
  46. int size,
  47. string firstVertExpected,
  48. string middleVertExpected,
  49. string endVertExpected,
  50. string sizeVertExpected,
  51. string firstHoriExpected,
  52. string middleHoriExpected,
  53. string endHoriExpected,
  54. string sizeHoriExpected
  55. )
  56. {
  57. var scroll = new Scroll
  58. {
  59. Orientation = Orientation.Vertical,
  60. Size = size,
  61. Height = 10
  62. };
  63. var top = new Toplevel ();
  64. top.Add (scroll);
  65. Application.Begin (top);
  66. _ = TestHelpers.AssertDriverContentsWithFrameAre (firstVertExpected, _output);
  67. scroll.Position = 4;
  68. Application.Refresh ();
  69. _ = TestHelpers.AssertDriverContentsWithFrameAre (middleVertExpected, _output);
  70. scroll.Position = 10;
  71. Application.Refresh ();
  72. _ = TestHelpers.AssertDriverContentsWithFrameAre (endVertExpected, _output);
  73. scroll.Size = size * 2;
  74. Application.Refresh ();
  75. _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeVertExpected, _output);
  76. scroll.Orientation = Orientation.Horizontal;
  77. scroll.Width = 10;
  78. scroll.Height = 1;
  79. scroll.Position = 0;
  80. scroll.Size = size;
  81. Application.Refresh ();
  82. _ = TestHelpers.AssertDriverContentsWithFrameAre (firstHoriExpected, _output);
  83. scroll.Position = 4;
  84. Application.Refresh ();
  85. _ = TestHelpers.AssertDriverContentsWithFrameAre (middleHoriExpected, _output);
  86. scroll.Position = 10;
  87. Application.Refresh ();
  88. _ = TestHelpers.AssertDriverContentsWithFrameAre (endHoriExpected, _output);
  89. scroll.Size = size * 2;
  90. Application.Refresh ();
  91. _ = TestHelpers.AssertDriverContentsWithFrameAre (sizeHoriExpected, _output);
  92. }
  93. [Fact]
  94. public void Constructor_Defaults ()
  95. {
  96. var scroll = new Scroll ();
  97. Assert.True (scroll.WantContinuousButtonPressed);
  98. Assert.False (scroll.ClearOnVisibleFalse);
  99. Assert.False (scroll.CanFocus);
  100. Assert.Equal (Orientation.Vertical, scroll.Orientation);
  101. Assert.Equal (0, scroll.Size);
  102. Assert.Equal (0, scroll.Position);
  103. }
  104. [Theory]
  105. [AutoInitShutdown]
  106. [InlineData (
  107. Orientation.Vertical,
  108. 20,
  109. 10,
  110. 4,
  111. @"
  112. █",
  113. 0,
  114. @"
  115. ░")]
  116. [InlineData (
  117. Orientation.Vertical,
  118. 40,
  119. 10,
  120. 5,
  121. @"
  122. ░",
  123. 20,
  124. @"
  125. ░")]
  126. [InlineData (
  127. Orientation.Horizontal,
  128. 20,
  129. 10,
  130. 4,
  131. @"
  132. ░░░░░█████",
  133. 0,
  134. @"
  135. █████░░░░░")]
  136. [InlineData (
  137. Orientation.Horizontal,
  138. 40,
  139. 10,
  140. 5,
  141. @"
  142. ░░██░░░░░░",
  143. 20,
  144. @"
  145. ░░░░░██░░░")]
  146. public void Mouse_On_The_Container (Orientation orientation, int size, int position, int location, string output, int expectedPos, string expectedOut)
  147. {
  148. var scroll = new Scroll
  149. {
  150. Width = orientation == Orientation.Vertical ? 1 : 10,
  151. Height = orientation == Orientation.Vertical ? 10 : 1,
  152. Orientation = orientation, Size = size,
  153. Position = position
  154. };
  155. var top = new Toplevel ();
  156. top.Add (scroll);
  157. Application.Begin (top);
  158. _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
  159. Application.OnMouseEvent (
  160. new ()
  161. {
  162. Position = orientation == Orientation.Vertical ? new (0, location) : new Point (location, 0),
  163. Flags = MouseFlags.Button1Pressed
  164. });
  165. Assert.Equal (expectedPos, scroll.Position);
  166. Application.Refresh ();
  167. _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
  168. }
  169. [Theory]
  170. [AutoInitShutdown]
  171. [InlineData (
  172. Orientation.Vertical,
  173. 20,
  174. 10,
  175. 5,
  176. 5,
  177. @"
  178. █",
  179. MouseFlags.Button1Pressed,
  180. 10,
  181. @"
  182. █")]
  183. [InlineData (
  184. Orientation.Vertical,
  185. 40,
  186. 10,
  187. 3,
  188. 3,
  189. @"
  190. ░",
  191. MouseFlags.Button1Pressed,
  192. 10,
  193. @"
  194. ░")]
  195. [InlineData (
  196. Orientation.Horizontal,
  197. 20,
  198. 10,
  199. 5,
  200. 5,
  201. @"
  202. ░░░░░█████",
  203. MouseFlags.Button1Pressed,
  204. 10,
  205. @"
  206. ░░░░░█████")]
  207. [InlineData (
  208. Orientation.Horizontal,
  209. 40,
  210. 10,
  211. 3,
  212. 3,
  213. @"
  214. ░░██░░░░░░",
  215. MouseFlags.Button1Pressed,
  216. 10,
  217. @"
  218. ░░██░░░░░░")]
  219. [InlineData (
  220. Orientation.Vertical,
  221. 20,
  222. 10,
  223. 5,
  224. 4,
  225. @"
  226. █",
  227. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  228. 8,
  229. @"
  230. ░")]
  231. [InlineData (
  232. Orientation.Horizontal,
  233. 20,
  234. 10,
  235. 5,
  236. 4,
  237. @"
  238. ░░░░░█████",
  239. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  240. 8,
  241. @"
  242. ░░░░█████░")]
  243. [InlineData (
  244. Orientation.Vertical,
  245. 20,
  246. 10,
  247. 5,
  248. 6,
  249. @"
  250. █",
  251. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  252. 10,
  253. @"
  254. █")]
  255. [InlineData (
  256. Orientation.Horizontal,
  257. 20,
  258. 10,
  259. 5,
  260. 6,
  261. @"
  262. ░░░░░█████",
  263. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  264. 10,
  265. @"
  266. ░░░░░█████")]
  267. [InlineData (
  268. Orientation.Vertical,
  269. 40,
  270. 10,
  271. 2,
  272. 1,
  273. @"
  274. ░",
  275. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  276. 4,
  277. @"
  278. ░")]
  279. [InlineData (
  280. Orientation.Horizontal,
  281. 40,
  282. 10,
  283. 2,
  284. 1,
  285. @"
  286. ░░██░░░░░░",
  287. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  288. 4,
  289. @"
  290. ░██░░░░░░░")]
  291. [InlineData (
  292. Orientation.Vertical,
  293. 40,
  294. 10,
  295. 3,
  296. 4,
  297. @"
  298. ░",
  299. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  300. 12,
  301. @"
  302. ░")]
  303. [InlineData (
  304. Orientation.Horizontal,
  305. 40,
  306. 10,
  307. 3,
  308. 4,
  309. @"
  310. ░░██░░░░░░",
  311. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  312. 12,
  313. @"
  314. ░░░██░░░░░")]
  315. [InlineData (
  316. Orientation.Vertical,
  317. 40,
  318. 10,
  319. 2,
  320. 3,
  321. @"
  322. ░",
  323. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  324. 12,
  325. @"
  326. ░")]
  327. [InlineData (
  328. Orientation.Horizontal,
  329. 40,
  330. 10,
  331. 2,
  332. 3,
  333. @"
  334. ░░██░░░░░░",
  335. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  336. 12,
  337. @"
  338. ░░░██░░░░░")]
  339. [InlineData (
  340. Orientation.Vertical,
  341. 40,
  342. 10,
  343. 2,
  344. 4,
  345. @"
  346. ░",
  347. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  348. 16,
  349. @"
  350. ░")]
  351. [InlineData (
  352. Orientation.Horizontal,
  353. 40,
  354. 10,
  355. 2,
  356. 4,
  357. @"
  358. ░░██░░░░░░",
  359. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition,
  360. 16,
  361. @"
  362. ░░░░██░░░░")]
  363. public void Mouse_On_The_Slider (
  364. Orientation orientation,
  365. int size,
  366. int position,
  367. int startLocation,
  368. int endLocation,
  369. string output,
  370. MouseFlags mouseFlags,
  371. int expectedPos,
  372. string expectedOut
  373. )
  374. {
  375. var scroll = new Scroll
  376. {
  377. Width = orientation == Orientation.Vertical ? 1 : 10,
  378. Height = orientation == Orientation.Vertical ? 10 : 1,
  379. Orientation = orientation,
  380. Size = size, Position = position
  381. };
  382. var top = new Toplevel ();
  383. top.Add (scroll);
  384. Application.Begin (top);
  385. _ = TestHelpers.AssertDriverContentsWithFrameAre (output, _output);
  386. Assert.Null (Application.MouseGrabView);
  387. if (mouseFlags.HasFlag (MouseFlags.ReportMousePosition))
  388. {
  389. MouseFlags mf = mouseFlags & ~MouseFlags.ReportMousePosition;
  390. Application.OnMouseEvent (
  391. new ()
  392. {
  393. Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  394. Flags = mf
  395. });
  396. Application.OnMouseEvent (
  397. new ()
  398. {
  399. Position = orientation == Orientation.Vertical ? new (0, endLocation) : new (endLocation, 0),
  400. Flags = mouseFlags
  401. });
  402. }
  403. else
  404. {
  405. Assert.Equal (startLocation, endLocation);
  406. Application.OnMouseEvent (
  407. new ()
  408. {
  409. Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  410. Flags = mouseFlags
  411. });
  412. }
  413. Assert.Equal ("slider", Application.MouseGrabView?.Id);
  414. Assert.Equal (expectedPos, scroll.Position);
  415. Application.Refresh ();
  416. _ = TestHelpers.AssertDriverContentsWithFrameAre (expectedOut, _output);
  417. Application.OnMouseEvent (
  418. new ()
  419. {
  420. Position = orientation == Orientation.Vertical ? new (0, startLocation) : new (startLocation, 0),
  421. Flags = MouseFlags.Button1Released
  422. });
  423. Assert.Null (Application.MouseGrabView);
  424. }
  425. [Theory]
  426. [InlineData (Orientation.Vertical, 20, 10)]
  427. [InlineData (Orientation.Vertical, 40, 30)]
  428. public void Position_Cannot_Be_Negative_Nor_Greater_Than_Size_Minus_Frame_Length (Orientation orientation, int size, int expectedPos)
  429. {
  430. var scroll = new Scroll { Orientation = orientation, Height = 10, Size = size };
  431. Assert.Equal (0, scroll.Position);
  432. scroll.Position = -1;
  433. Assert.Equal (0, scroll.Position);
  434. scroll.Position = size;
  435. Assert.Equal (0, scroll.Position);
  436. scroll.Position = expectedPos;
  437. Assert.Equal (expectedPos, scroll.Position);
  438. }
  439. [Fact]
  440. public void PositionChanging_Cancelable_And_PositionChanged_Events ()
  441. {
  442. var changingCount = 0;
  443. var changedCount = 0;
  444. var scroll = new Scroll { Size = 10 };
  445. scroll.PositionChanging += (s, e) =>
  446. {
  447. if (changingCount == 0)
  448. {
  449. e.Cancel = true;
  450. }
  451. changingCount++;
  452. };
  453. scroll.PositionChanged += (s, e) => changedCount++;
  454. scroll.Position = 1;
  455. Assert.Equal (0, scroll.Position);
  456. Assert.Equal (1, changingCount);
  457. Assert.Equal (0, changedCount);
  458. scroll.Position = 1;
  459. Assert.Equal (1, scroll.Position);
  460. Assert.Equal (2, changingCount);
  461. Assert.Equal (1, changedCount);
  462. }
  463. [Fact]
  464. public void SizeChanged_Event ()
  465. {
  466. var count = 0;
  467. var scroll = new Scroll ();
  468. scroll.SizeChanged += (s, e) => count++;
  469. scroll.Size = 10;
  470. Assert.Equal (10, scroll.Size);
  471. Assert.Equal (1, count);
  472. }
  473. [Theory]
  474. [AutoInitShutdown]
  475. [InlineData (
  476. 3,
  477. 10,
  478. 1,
  479. Orientation.Vertical,
  480. @"
  481. ┌─┐
  482. │█│
  483. │█│
  484. │█│
  485. │░│
  486. │░│
  487. │░│
  488. │░│
  489. │░│
  490. └─┘")]
  491. [InlineData (
  492. 10,
  493. 3,
  494. 1,
  495. Orientation.Horizontal,
  496. @"
  497. ┌────────┐
  498. │███░░░░░│
  499. └────────┘")]
  500. [InlineData (
  501. 3,
  502. 10,
  503. 3,
  504. Orientation.Vertical,
  505. @"
  506. ┌───┐
  507. │███│
  508. │███│
  509. │███│
  510. │░░░│
  511. │░░░│
  512. │░░░│
  513. │░░░│
  514. │░░░│
  515. └───┘")]
  516. [InlineData (
  517. 10,
  518. 3,
  519. 3,
  520. Orientation.Horizontal,
  521. @"
  522. ┌────────┐
  523. │███░░░░░│
  524. │███░░░░░│
  525. │███░░░░░│
  526. └────────┘")]
  527. public void Vertical_Horizontal_Draws_Correctly (int sizeWidth, int sizeHeight, int widthHeight, Orientation orientation, string expected)
  528. {
  529. var super = new Window { Id = "super", Width = Dim.Fill (), Height = Dim.Fill () };
  530. var top = new Toplevel ();
  531. top.Add (super);
  532. var scroll = new Scroll
  533. {
  534. Orientation = orientation,
  535. Size = orientation == Orientation.Vertical ? sizeHeight * 2 : sizeWidth * 2,
  536. Width = orientation == Orientation.Vertical ? widthHeight : Dim.Fill (),
  537. Height = orientation == Orientation.Vertical ? Dim.Fill () : widthHeight
  538. };
  539. super.Add (scroll);
  540. Application.Begin (top);
  541. ((FakeDriver)Application.Driver).SetBufferSize (
  542. sizeWidth + (orientation == Orientation.Vertical ? widthHeight - 1 : 0),
  543. sizeHeight + (orientation == Orientation.Vertical ? 0 : widthHeight - 1));
  544. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  545. }
  546. }