ScrollSliderTests.cs 11 KB

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