ScrollBarTests.cs 15 KB

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