ScrollTests.cs 17 KB

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