ScrollSliderTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using System.Diagnostics;
  2. using Microsoft.VisualStudio.TestPlatform.Utilities;
  3. using Xunit.Abstractions;
  4. using static Unix.Terminal.Delegates;
  5. namespace Terminal.Gui.ViewsTests;
  6. public class ScrollSliderTests (ITestOutputHelper output)
  7. {
  8. [Fact]
  9. public void Constructor_Initializes_Correctly ()
  10. {
  11. var scrollSlider = new ScrollSlider ();
  12. Assert.False (scrollSlider.CanFocus);
  13. Assert.Equal (Orientation.Vertical, scrollSlider.Orientation);
  14. Assert.Equal (TextDirection.TopBottom_LeftRight, scrollSlider.TextDirection);
  15. Assert.Equal (Alignment.Center, scrollSlider.TextAlignment);
  16. Assert.Equal (Alignment.Center, scrollSlider.VerticalTextAlignment);
  17. scrollSlider.Layout ();
  18. Assert.Equal (0, scrollSlider.Frame.X);
  19. Assert.Equal (0, scrollSlider.Frame.Y);
  20. Assert.Equal (1, scrollSlider.Size);
  21. }
  22. [Fact]
  23. public void OnOrientationChanged_Sets_Size_To_1 ()
  24. {
  25. var scrollSlider = new ScrollSlider ();
  26. scrollSlider.Orientation = Orientation.Horizontal;
  27. Assert.Equal (1, scrollSlider.Size);
  28. }
  29. [Fact]
  30. public void OnOrientationChanged_Sets_Position_To_0 ()
  31. {
  32. View super = new View ()
  33. {
  34. Id = "super",
  35. Width = 10,
  36. Height = 10
  37. };
  38. var scrollSlider = new ScrollSlider ()
  39. {
  40. };
  41. super.Add (scrollSlider);
  42. scrollSlider.Layout ();
  43. scrollSlider.Position = 1;
  44. scrollSlider.Orientation = Orientation.Horizontal;
  45. Assert.Equal (0, scrollSlider.Position);
  46. }
  47. [Fact]
  48. public void OnOrientationChanged_Updates_TextDirection_And_TextAlignment ()
  49. {
  50. var scrollSlider = new ScrollSlider ();
  51. scrollSlider.Orientation = Orientation.Horizontal;
  52. Assert.Equal (TextDirection.LeftRight_TopBottom, scrollSlider.TextDirection);
  53. Assert.Equal (Alignment.Center, scrollSlider.TextAlignment);
  54. Assert.Equal (Alignment.Center, scrollSlider.VerticalTextAlignment);
  55. }
  56. [Theory]
  57. [CombinatorialData]
  58. public void Size_Clamps_To_SuperView_Viewport ([CombinatorialRange (-1, 6, 1)] int sliderSize, Orientation orientation)
  59. {
  60. var super = new View
  61. {
  62. Id = "super",
  63. Width = 5,
  64. Height = 5
  65. };
  66. var scrollSlider = new ScrollSlider
  67. {
  68. Orientation = orientation,
  69. };
  70. super.Add (scrollSlider);
  71. scrollSlider.Layout ();
  72. scrollSlider.Size = sliderSize;
  73. scrollSlider.Layout ();
  74. Assert.True (scrollSlider.Size > 0);
  75. Assert.True (scrollSlider.Size <= 5);
  76. }
  77. [Theory]
  78. [CombinatorialData]
  79. public void Size_Clamps_To_ViewportDimensions ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (-1, 12, 1)] int sliderSize, Orientation orientation)
  80. {
  81. var scrollSlider = new ScrollSlider
  82. {
  83. Orientation = orientation,
  84. ViewportDimension = dimension,
  85. Size = sliderSize,
  86. };
  87. scrollSlider.Layout ();
  88. Assert.True (scrollSlider.Size > 0);
  89. Assert.True (scrollSlider.Size <= dimension);
  90. }
  91. [Theory]
  92. [CombinatorialData]
  93. public void ViewportDimensions_Clamps_0_To_Dimension ([CombinatorialRange (0, 10, 1)] int dimension, Orientation orientation)
  94. {
  95. var scrollSlider = new ScrollSlider
  96. {
  97. Orientation = orientation,
  98. ViewportDimension = dimension,
  99. };
  100. Assert.InRange (scrollSlider.ViewportDimension, 1, 10);
  101. View super = new ()
  102. {
  103. Id = "super",
  104. Height = dimension,
  105. Width = dimension,
  106. };
  107. scrollSlider = new ScrollSlider
  108. {
  109. Orientation = orientation,
  110. };
  111. super.Add (scrollSlider);
  112. super.Layout ();
  113. Assert.InRange (scrollSlider.ViewportDimension, 1, 10);
  114. scrollSlider.ViewportDimension = dimension;
  115. Assert.InRange (scrollSlider.ViewportDimension, 1, 10);
  116. }
  117. [Theory]
  118. [CombinatorialData]
  119. public void ClampPosition_Clamps_To_Viewport_Minus_Size ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-2, 6, 2)] int sliderPosition, Orientation orientation)
  120. {
  121. var scrollSlider = new ScrollSlider
  122. {
  123. Orientation = orientation,
  124. ViewportDimension = dimension,
  125. Size = sliderSize,
  126. };
  127. int clampedPosition = scrollSlider.ClampPosition (sliderPosition);
  128. Assert.InRange (clampedPosition, 0, dimension - sliderSize);
  129. View super = new ()
  130. {
  131. Id = "super",
  132. Height = dimension,
  133. Width = dimension,
  134. };
  135. scrollSlider = new ScrollSlider
  136. {
  137. Orientation = orientation,
  138. Size = sliderSize,
  139. };
  140. super.Add (scrollSlider);
  141. super.Layout ();
  142. clampedPosition = scrollSlider.ClampPosition (sliderPosition);
  143. Assert.InRange (clampedPosition, 0, dimension - sliderSize);
  144. }
  145. [Theory]
  146. [CombinatorialData]
  147. public void Position_Clamps_To_SuperView_Viewport ([CombinatorialRange (0, 5, 1)] int sliderSize, [CombinatorialRange (-2, 6, 2)] int sliderPosition, Orientation orientation)
  148. {
  149. var super = new View
  150. {
  151. Id = "super",
  152. Width = 5,
  153. Height = 5
  154. };
  155. var scrollSlider = new ScrollSlider
  156. {
  157. Orientation = orientation,
  158. };
  159. super.Add (scrollSlider);
  160. scrollSlider.Size = sliderSize;
  161. scrollSlider.Layout ();
  162. scrollSlider.Position = sliderPosition;
  163. Assert.True (scrollSlider.Position <= 5);
  164. }
  165. [Theory]
  166. [SetupFakeDriver]
  167. [InlineData (
  168. 3,
  169. 10,
  170. 1,
  171. 0,
  172. Orientation.Vertical,
  173. @"
  174. ┌───┐
  175. │███│
  176. │ │
  177. │ │
  178. │ │
  179. │ │
  180. │ │
  181. │ │
  182. │ │
  183. │ │
  184. │ │
  185. └───┘")]
  186. [InlineData (
  187. 10,
  188. 1,
  189. 3,
  190. 0,
  191. Orientation.Horizontal,
  192. @"
  193. ┌──────────┐
  194. │███ │
  195. └──────────┘")]
  196. [InlineData (
  197. 3,
  198. 10,
  199. 3,
  200. 0,
  201. Orientation.Vertical,
  202. @"
  203. ┌───┐
  204. │███│
  205. │███│
  206. │███│
  207. │ │
  208. │ │
  209. │ │
  210. │ │
  211. │ │
  212. │ │
  213. │ │
  214. └───┘")]
  215. [InlineData (
  216. 3,
  217. 10,
  218. 5,
  219. 0,
  220. Orientation.Vertical,
  221. @"
  222. ┌───┐
  223. │███│
  224. │███│
  225. │███│
  226. │███│
  227. │███│
  228. │ │
  229. │ │
  230. │ │
  231. │ │
  232. │ │
  233. └───┘")]
  234. [InlineData (
  235. 3,
  236. 10,
  237. 5,
  238. 1,
  239. Orientation.Vertical,
  240. @"
  241. ┌───┐
  242. │ │
  243. │███│
  244. │███│
  245. │███│
  246. │███│
  247. │███│
  248. │ │
  249. │ │
  250. │ │
  251. │ │
  252. └───┘")]
  253. [InlineData (
  254. 3,
  255. 10,
  256. 5,
  257. 4,
  258. Orientation.Vertical,
  259. @"
  260. ┌───┐
  261. │ │
  262. │ │
  263. │ │
  264. │ │
  265. │███│
  266. │███│
  267. │███│
  268. │███│
  269. │███│
  270. │ │
  271. └───┘")]
  272. [InlineData (
  273. 3,
  274. 10,
  275. 5,
  276. 5,
  277. Orientation.Vertical,
  278. @"
  279. ┌───┐
  280. │ │
  281. │ │
  282. │ │
  283. │ │
  284. │ │
  285. │███│
  286. │███│
  287. │███│
  288. │███│
  289. │███│
  290. └───┘")]
  291. [InlineData (
  292. 3,
  293. 10,
  294. 5,
  295. 6,
  296. Orientation.Vertical,
  297. @"
  298. ┌───┐
  299. │ │
  300. │ │
  301. │ │
  302. │ │
  303. │ │
  304. │███│
  305. │███│
  306. │███│
  307. │███│
  308. │███│
  309. └───┘")]
  310. [InlineData (
  311. 3,
  312. 10,
  313. 10,
  314. 0,
  315. Orientation.Vertical,
  316. @"
  317. ┌───┐
  318. │███│
  319. │███│
  320. │███│
  321. │███│
  322. │███│
  323. │███│
  324. │███│
  325. │███│
  326. │███│
  327. │███│
  328. └───┘")]
  329. [InlineData (
  330. 3,
  331. 10,
  332. 10,
  333. 5,
  334. Orientation.Vertical,
  335. @"
  336. ┌───┐
  337. │███│
  338. │███│
  339. │███│
  340. │███│
  341. │███│
  342. │███│
  343. │███│
  344. │███│
  345. │███│
  346. │███│
  347. └───┘")]
  348. [InlineData (
  349. 3,
  350. 10,
  351. 11,
  352. 0,
  353. Orientation.Vertical,
  354. @"
  355. ┌───┐
  356. │███│
  357. │███│
  358. │███│
  359. │███│
  360. │███│
  361. │███│
  362. │███│
  363. │███│
  364. │███│
  365. │███│
  366. └───┘")]
  367. [InlineData (
  368. 10,
  369. 3,
  370. 5,
  371. 0,
  372. Orientation.Horizontal,
  373. @"
  374. ┌──────────┐
  375. │█████ │
  376. │█████ │
  377. │█████ │
  378. └──────────┘")]
  379. [InlineData (
  380. 10,
  381. 3,
  382. 5,
  383. 1,
  384. Orientation.Horizontal,
  385. @"
  386. ┌──────────┐
  387. │ █████ │
  388. │ █████ │
  389. │ █████ │
  390. └──────────┘")]
  391. [InlineData (
  392. 10,
  393. 3,
  394. 5,
  395. 4,
  396. Orientation.Horizontal,
  397. @"
  398. ┌──────────┐
  399. │ █████ │
  400. │ █████ │
  401. │ █████ │
  402. └──────────┘")]
  403. [InlineData (
  404. 10,
  405. 3,
  406. 5,
  407. 5,
  408. Orientation.Horizontal,
  409. @"
  410. ┌──────────┐
  411. │ █████│
  412. │ █████│
  413. │ █████│
  414. └──────────┘")]
  415. [InlineData (
  416. 10,
  417. 3,
  418. 5,
  419. 6,
  420. Orientation.Horizontal,
  421. @"
  422. ┌──────────┐
  423. │ █████│
  424. │ █████│
  425. │ █████│
  426. └──────────┘")]
  427. [InlineData (
  428. 10,
  429. 3,
  430. 10,
  431. 0,
  432. Orientation.Horizontal,
  433. @"
  434. ┌──────────┐
  435. │██████████│
  436. │██████████│
  437. │██████████│
  438. └──────────┘")]
  439. [InlineData (
  440. 10,
  441. 3,
  442. 10,
  443. 5,
  444. Orientation.Horizontal,
  445. @"
  446. ┌──────────┐
  447. │██████████│
  448. │██████████│
  449. │██████████│
  450. └──────────┘")]
  451. [InlineData (
  452. 10,
  453. 3,
  454. 11,
  455. 0,
  456. Orientation.Horizontal,
  457. @"
  458. ┌──────────┐
  459. │██████████│
  460. │██████████│
  461. │██████████│
  462. └──────────┘")]
  463. public void Draws_Correctly (int superViewportWidth, int superViewportHeight, int sliderSize, int position, Orientation orientation, string expected)
  464. {
  465. var super = new Window
  466. {
  467. Id = "super",
  468. Width = superViewportWidth + 2,
  469. Height = superViewportHeight + 2
  470. };
  471. var scrollSlider = new ScrollSlider
  472. {
  473. Orientation = orientation,
  474. Size = sliderSize,
  475. Position = position,
  476. };
  477. Assert.Equal (sliderSize, scrollSlider.Size);
  478. super.Add (scrollSlider);
  479. super.Layout ();
  480. super.Draw ();
  481. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  482. }
  483. [Fact]
  484. public void ShowPercent_True_ShowsPercentage ()
  485. {
  486. View super = new ()
  487. {
  488. Id = "super",
  489. Width = 10,
  490. Height = 10
  491. };
  492. ScrollSlider scrollSlider = new ()
  493. {
  494. Id = "scrollSlider",
  495. Height = 10,
  496. Width = 10,
  497. };
  498. super.Add (scrollSlider);
  499. scrollSlider.ShowPercent = true;
  500. Assert.True (scrollSlider.ShowPercent);
  501. super.Draw ();
  502. Assert.Contains ("0%", scrollSlider.Text);
  503. }
  504. }