SliderTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. using System.Text;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class SliderOptionTests
  4. {
  5. [Fact]
  6. public void OnChanged_Should_Raise_ChangedEvent ()
  7. {
  8. // Arrange
  9. SliderOption<int> sliderOption = new ();
  10. var eventRaised = false;
  11. sliderOption.Changed += (sender, args) => eventRaised = true;
  12. // Act
  13. sliderOption.OnChanged (true);
  14. // Assert
  15. Assert.True (eventRaised);
  16. }
  17. [Fact]
  18. public void OnSet_Should_Raise_SetEvent ()
  19. {
  20. // Arrange
  21. SliderOption<int> sliderOption = new ();
  22. var eventRaised = false;
  23. sliderOption.Set += (sender, args) => eventRaised = true;
  24. // Act
  25. sliderOption.OnSet ();
  26. // Assert
  27. Assert.True (eventRaised);
  28. }
  29. [Fact]
  30. public void OnUnSet_Should_Raise_UnSetEvent ()
  31. {
  32. // Arrange
  33. SliderOption<int> sliderOption = new ();
  34. var eventRaised = false;
  35. sliderOption.UnSet += (sender, args) => eventRaised = true;
  36. // Act
  37. sliderOption.OnUnSet ();
  38. // Assert
  39. Assert.True (eventRaised);
  40. }
  41. [Fact]
  42. public void Slider_Option_Default_Constructor ()
  43. {
  44. SliderOption<int> o = new ();
  45. Assert.Null (o.Legend);
  46. Assert.Equal (default (Rune), o.LegendAbbr);
  47. Assert.Equal (default (int), o.Data);
  48. }
  49. [Fact]
  50. public void Slider_Option_Values_Constructor ()
  51. {
  52. SliderOption<int> o = new ("1 thousand", new ('y'), 1000);
  53. Assert.Equal ("1 thousand", o.Legend);
  54. Assert.Equal (new ('y'), o.LegendAbbr);
  55. Assert.Equal (1000, o.Data);
  56. }
  57. [Fact]
  58. public void SliderOption_ToString_WhenEmpty ()
  59. {
  60. SliderOption<object> sliderOption = new ();
  61. Assert.Equal ("{Legend=, LegendAbbr=\0, Data=}", sliderOption.ToString ());
  62. }
  63. [Fact]
  64. public void SliderOption_ToString_WhenPopulated_WithInt ()
  65. {
  66. SliderOption<int> sliderOption = new () { Legend = "Lord flibble", LegendAbbr = new ('l'), Data = 1 };
  67. Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data=1}", sliderOption.ToString ());
  68. }
  69. [Fact]
  70. public void SliderOption_ToString_WhenPopulated_WithSizeF ()
  71. {
  72. SliderOption<SizeF> sliderOption = new ()
  73. {
  74. Legend = "Lord flibble", LegendAbbr = new ('l'), Data = new (32, 11)
  75. };
  76. Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data={Width=32, Height=11}}", sliderOption.ToString ());
  77. }
  78. }
  79. public class SliderEventArgsTests
  80. {
  81. [Fact]
  82. public void Constructor_Sets_Cancel_Default_To_False ()
  83. {
  84. // Arrange
  85. Dictionary<int, SliderOption<int>> options = new ();
  86. var focused = 42;
  87. // Act
  88. SliderEventArgs<int> sliderEventArgs = new (options, focused);
  89. // Assert
  90. Assert.False (sliderEventArgs.Cancel);
  91. }
  92. [Fact]
  93. public void Constructor_Sets_Focused ()
  94. {
  95. // Arrange
  96. Dictionary<int, SliderOption<int>> options = new ();
  97. var focused = 42;
  98. // Act
  99. SliderEventArgs<int> sliderEventArgs = new (options, focused);
  100. // Assert
  101. Assert.Equal (focused, sliderEventArgs.Focused);
  102. }
  103. [Fact]
  104. public void Constructor_Sets_Options ()
  105. {
  106. // Arrange
  107. Dictionary<int, SliderOption<int>> options = new ();
  108. // Act
  109. SliderEventArgs<int> sliderEventArgs = new (options);
  110. // Assert
  111. Assert.Equal (options, sliderEventArgs.Options);
  112. }
  113. }
  114. public class SliderTests
  115. {
  116. [Fact]
  117. public void Constructor_Default ()
  118. {
  119. // Arrange & Act
  120. Slider<int> slider = new ();
  121. // Assert
  122. Assert.NotNull (slider);
  123. Assert.NotNull (slider.Options);
  124. Assert.Empty (slider.Options);
  125. Assert.Equal (Orientation.Horizontal, slider.Orientation);
  126. Assert.False (slider.AllowEmpty);
  127. Assert.True (slider.ShowLegends);
  128. Assert.False (slider.ShowEndSpacing);
  129. Assert.Equal (SliderType.Single, slider.Type);
  130. Assert.Equal (1, slider.MinimumInnerSpacing);
  131. Assert.True (slider.Width is DimAuto);
  132. Assert.True (slider.Height is DimAuto);
  133. Assert.Equal (0, slider.FocusedOption);
  134. }
  135. [Fact]
  136. public void Constructor_With_Options ()
  137. {
  138. // Arrange
  139. List<int> options = new () { 1, 2, 3 };
  140. // Act
  141. Slider<int> slider = new (options);
  142. slider.SetRelativeLayout (new (100, 100));
  143. // Assert
  144. // 0123456789
  145. // 1 2 3
  146. Assert.Equal (1, slider.MinimumInnerSpacing);
  147. Assert.Equal (new Size (5, 2), slider.GetContentSize ());
  148. Assert.Equal (new Size (5, 2), slider.Frame.Size);
  149. Assert.NotNull (slider);
  150. Assert.NotNull (slider.Options);
  151. Assert.Equal (options.Count, slider.Options.Count);
  152. }
  153. [Fact]
  154. public void MovePlus_Should_MoveFocusRight_When_OptionIsAvailable ()
  155. {
  156. // Arrange
  157. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  158. // Act
  159. bool result = slider.MovePlus ();
  160. // Assert
  161. Assert.True (result);
  162. Assert.Equal (1, slider.FocusedOption);
  163. }
  164. [Fact]
  165. public void MovePlus_Should_NotMoveFocusRight_When_AtEnd ()
  166. {
  167. // Arrange
  168. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  169. slider.FocusedOption = 3;
  170. // Act
  171. bool result = slider.MovePlus ();
  172. // Assert
  173. Assert.False (result);
  174. Assert.Equal (3, slider.FocusedOption);
  175. }
  176. [Fact]
  177. public void OnOptionFocused_Event_Cancelled ()
  178. {
  179. // Arrange
  180. Slider<int> slider = new (new () { 1, 2, 3 });
  181. var eventRaised = false;
  182. var cancel = false;
  183. slider.OptionFocused += (sender, args) => eventRaised = true;
  184. var newFocusedOption = 1;
  185. // Create args with cancel set to false
  186. cancel = false;
  187. SliderEventArgs<int> args =
  188. new (new (), newFocusedOption) { Cancel = cancel };
  189. Assert.Equal (0, slider.FocusedOption);
  190. // Act
  191. slider.OnOptionFocused (newFocusedOption, args);
  192. // Assert
  193. Assert.True (eventRaised); // Event should be raised
  194. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should change
  195. // Create args with cancel set to true
  196. cancel = true;
  197. args = new (new (), newFocusedOption)
  198. {
  199. Cancel = cancel
  200. };
  201. // Act
  202. slider.OnOptionFocused (2, args);
  203. // Assert
  204. Assert.True (eventRaised); // Event should be raised
  205. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should not change
  206. }
  207. [Fact]
  208. public void OnOptionFocused_Event_Raised ()
  209. {
  210. // Arrange
  211. Slider<int> slider = new (new () { 1, 2, 3 });
  212. var eventRaised = false;
  213. slider.OptionFocused += (sender, args) => eventRaised = true;
  214. var newFocusedOption = 1;
  215. SliderEventArgs<int> args = new (new (), newFocusedOption);
  216. // Act
  217. slider.OnOptionFocused (newFocusedOption, args);
  218. // Assert
  219. Assert.True (eventRaised);
  220. }
  221. [Fact]
  222. public void OnOptionsChanged_Event_Raised ()
  223. {
  224. // Arrange
  225. Slider<int> slider = new ();
  226. var eventRaised = false;
  227. slider.OptionsChanged += (sender, args) => eventRaised = true;
  228. // Act
  229. slider.OnOptionsChanged ();
  230. // Assert
  231. Assert.True (eventRaised);
  232. }
  233. [Fact]
  234. public void Set_Should_Not_UnSetFocusedOption_When_EmptyNotAllowed ()
  235. {
  236. // Arrange
  237. Slider<int> slider = new (new () { 1, 2, 3, 4 }) { AllowEmpty = false };
  238. Assert.NotEmpty (slider.GetSetOptions ());
  239. // Act
  240. bool result = slider.UnSetOption (slider.FocusedOption);
  241. // Assert
  242. Assert.False (result);
  243. Assert.NotEmpty (slider.GetSetOptions ());
  244. }
  245. // Add similar tests for other methods like MoveMinus, MoveStart, MoveEnd, Set, etc.
  246. [Fact]
  247. public void Set_Should_SetFocusedOption ()
  248. {
  249. // Arrange
  250. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  251. // Act
  252. slider.FocusedOption = 2;
  253. bool result = slider.Select ();
  254. // Assert
  255. Assert.True (result);
  256. Assert.Equal (2, slider.FocusedOption);
  257. Assert.Single (slider.GetSetOptions ());
  258. }
  259. [Fact]
  260. public void TryGetOptionByPosition_InvalidPosition_Failure ()
  261. {
  262. // Arrange
  263. Slider<int> slider = new (new () { 1, 2, 3 });
  264. var x = 10;
  265. var y = 10;
  266. var threshold = 2;
  267. int expectedOption = -1;
  268. // Act
  269. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  270. // Assert
  271. Assert.False (result);
  272. Assert.Equal (expectedOption, option);
  273. }
  274. [Theory]
  275. [InlineData (0, 0, 0, 1)]
  276. [InlineData (3, 0, 0, 2)]
  277. [InlineData (9, 0, 0, 4)]
  278. [InlineData (0, 0, 1, 1)]
  279. [InlineData (3, 0, 1, 2)]
  280. [InlineData (9, 0, 1, 4)]
  281. public void TryGetOptionByPosition_ValidPositionHorizontal_Success (int x, int y, int threshold, int expectedData)
  282. {
  283. // Arrange
  284. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  285. // 0123456789
  286. // 1234
  287. slider.MinimumInnerSpacing = 2;
  288. // 0123456789
  289. // 1--2--3--4
  290. // Arrange
  291. // Act
  292. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  293. // Assert
  294. Assert.True (result);
  295. Assert.Equal (expectedData, slider.Options [option].Data);
  296. }
  297. [Theory]
  298. [InlineData (0, 0, 0, 1)]
  299. [InlineData (0, 3, 0, 2)]
  300. [InlineData (0, 9, 0, 4)]
  301. [InlineData (0, 0, 1, 1)]
  302. [InlineData (0, 3, 1, 2)]
  303. [InlineData (0, 9, 1, 4)]
  304. public void TryGetOptionByPosition_ValidPositionVertical_Success (int x, int y, int threshold, int expectedData)
  305. {
  306. // Arrange
  307. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  308. slider.Orientation = Orientation.Vertical;
  309. // Set auto size to true to enable testing
  310. slider.MinimumInnerSpacing = 2;
  311. // 0 1
  312. // 1 |
  313. // 2 |
  314. // 3 2
  315. // 4 |
  316. // 5 |
  317. // 6 3
  318. // 7 |
  319. // 8 |
  320. // 9 4
  321. // Act
  322. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  323. // Assert
  324. Assert.True (result);
  325. Assert.Equal (expectedData, slider.Options [option].Data);
  326. }
  327. [Fact]
  328. public void TryGetPositionByOption_InvalidOption_Failure ()
  329. {
  330. // Arrange
  331. Slider<int> slider = new (new () { 1, 2, 3 });
  332. int option = -1;
  333. (int, int) expectedPosition = (-1, -1);
  334. // Act
  335. bool result = slider.TryGetPositionByOption (option, out (int x, int y) position);
  336. // Assert
  337. Assert.False (result);
  338. Assert.Equal (expectedPosition, position);
  339. }
  340. [Theory]
  341. [InlineData (0, 0, 0)]
  342. [InlineData (1, 3, 0)]
  343. [InlineData (3, 9, 0)]
  344. public void TryGetPositionByOption_ValidOptionHorizontal_Success (int option, int expectedX, int expectedY)
  345. {
  346. // Arrange
  347. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  348. // Set auto size to true to enable testing
  349. slider.MinimumInnerSpacing = 2;
  350. // 0123456789
  351. // 1--2--3--4
  352. // Act
  353. bool result = slider.TryGetPositionByOption (option, out (int x, int y) position);
  354. // Assert
  355. Assert.True (result);
  356. Assert.Equal (expectedX, position.x);
  357. Assert.Equal (expectedY, position.y);
  358. }
  359. [Theory]
  360. [InlineData (0, 0, 0)]
  361. [InlineData (1, 0, 3)]
  362. [InlineData (3, 0, 9)]
  363. public void TryGetPositionByOption_ValidOptionVertical_Success (int option, int expectedX, int expectedY)
  364. {
  365. // Arrange
  366. Slider<int> slider = new (new () { 1, 2, 3, 4 });
  367. slider.Orientation = Orientation.Vertical;
  368. // Set auto size to true to enable testing
  369. slider.MinimumInnerSpacing = 2;
  370. // Act
  371. bool result = slider.TryGetPositionByOption (option, out (int x, int y) position);
  372. // Assert
  373. Assert.True (result);
  374. Assert.Equal (expectedX, position.x);
  375. Assert.Equal (expectedY, position.y);
  376. }
  377. // https://github.com/gui-cs/Terminal.Gui/issues/3099
  378. [Fact]
  379. private void One_Option_Does_Not_Throw ()
  380. {
  381. // Arrange
  382. Slider<int> slider = new ();
  383. slider.BeginInit ();
  384. slider.EndInit ();
  385. // Act/Assert
  386. slider.Options = new () { new () };
  387. }
  388. [Fact]
  389. private void Set_Options_No_Legend_Throws ()
  390. {
  391. // Arrange
  392. Slider<int> slider = new ();
  393. // Act/Assert
  394. Assert.Throws<ArgumentNullException> (() => slider.Options = null);
  395. }
  396. [Fact]
  397. private void Set_Options_Throws_If_Null ()
  398. {
  399. // Arrange
  400. Slider<int> slider = new ();
  401. // Act/Assert
  402. Assert.Throws<ArgumentNullException> (() => slider.Options = null);
  403. }
  404. [Fact]
  405. private void DimAuto_Both_Respects_SuperView_ContentSize ()
  406. {
  407. View view = new ()
  408. {
  409. Width = Dim.Fill (),
  410. Height = Dim.Fill ()
  411. };
  412. List<object> options = new () { "01234", "01234" };
  413. Slider slider = new (options)
  414. {
  415. Orientation = Orientation.Vertical,
  416. Type = SliderType.Multiple,
  417. };
  418. view.Add (slider);
  419. view.BeginInit ();
  420. view.EndInit ();
  421. Size expectedSize = slider.Frame.Size;
  422. Assert.Equal (new (6, 3), expectedSize);
  423. view.SetContentSize (new (1, 1));
  424. view.LayoutSubviews ();
  425. slider.SetRelativeLayout (view.Viewport.Size);
  426. Assert.Equal (expectedSize, slider.Frame.Size);
  427. }
  428. [Fact]
  429. private void DimAuto_Width_Respects_SuperView_ContentSize ()
  430. {
  431. View view = new ()
  432. {
  433. Width = Dim.Fill (),
  434. Height = 10
  435. };
  436. List<object> options = new () { "01234", "01234" };
  437. Slider slider = new (options)
  438. {
  439. Orientation = Orientation.Vertical,
  440. Type = SliderType.Multiple,
  441. Height = 10
  442. };
  443. view.Add (slider);
  444. view.BeginInit ();
  445. view.EndInit ();
  446. Size expectedSize = slider.Frame.Size;
  447. Assert.Equal (new (6, 10), expectedSize);
  448. view.SetContentSize (new (1, 1));
  449. view.LayoutSubviews ();
  450. slider.SetRelativeLayout (view.Viewport.Size);
  451. Assert.Equal (expectedSize, slider.Frame.Size);
  452. }
  453. [Fact]
  454. private void DimAuto_Height_Respects_SuperView_ContentSize ()
  455. {
  456. View view = new ()
  457. {
  458. Width = 10,
  459. Height = Dim.Fill ()
  460. };
  461. List<object> options = new () { "01234", "01234" };
  462. Slider slider = new (options)
  463. {
  464. Orientation = Orientation.Vertical,
  465. Type = SliderType.Multiple,
  466. Width = 10,
  467. };
  468. view.Add (slider);
  469. view.BeginInit ();
  470. view.EndInit ();
  471. Size expectedSize = slider.Frame.Size;
  472. Assert.Equal (new (10, 3), expectedSize);
  473. view.SetContentSize (new (1, 1));
  474. view.LayoutSubviews ();
  475. slider.SetRelativeLayout (view.Viewport.Size);
  476. Assert.Equal (expectedSize, slider.Frame.Size);
  477. }
  478. // Add more tests for different scenarios and edge cases.
  479. }