SliderTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 (0, slider.InnerSpacing);
  131. Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Content), slider.Width);
  132. Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Content), slider.Height);
  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. // Assert
  143. Assert.NotNull (slider);
  144. Assert.NotNull (slider.Options);
  145. Assert.Equal (options.Count, slider.Options.Count);
  146. }
  147. [Fact]
  148. public void MovePlus_Should_MoveFocusRight_When_OptionIsAvailable ()
  149. {
  150. // Arrange
  151. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  152. slider.AutoSize = true;
  153. // Act
  154. bool result = slider.MovePlus ();
  155. // Assert
  156. Assert.True (result);
  157. Assert.Equal (1, slider.FocusedOption);
  158. }
  159. [Fact]
  160. public void MovePlus_Should_NotMoveFocusRight_When_AtEnd ()
  161. {
  162. // Arrange
  163. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  164. slider.AutoSize = true;
  165. slider.FocusedOption = 3;
  166. // Act
  167. bool result = slider.MovePlus ();
  168. // Assert
  169. Assert.False (result);
  170. Assert.Equal (3, slider.FocusedOption);
  171. }
  172. [Fact]
  173. public void OnOptionFocused_Event_Cancelled ()
  174. {
  175. // Arrange
  176. Slider<int> slider = new (new() { 1, 2, 3 });
  177. var eventRaised = false;
  178. var cancel = false;
  179. slider.OptionFocused += (sender, args) => eventRaised = true;
  180. var newFocusedOption = 1;
  181. // Create args with cancel set to false
  182. cancel = false;
  183. SliderEventArgs<int> args =
  184. new (new (), newFocusedOption) { Cancel = cancel };
  185. Assert.Equal (0, slider.FocusedOption);
  186. // Act
  187. slider.OnOptionFocused (newFocusedOption, args);
  188. // Assert
  189. Assert.True (eventRaised); // Event should be raised
  190. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should change
  191. // Create args with cancel set to true
  192. cancel = true;
  193. args = new (new (), newFocusedOption)
  194. {
  195. Cancel = cancel
  196. };
  197. // Act
  198. slider.OnOptionFocused (2, args);
  199. // Assert
  200. Assert.True (eventRaised); // Event should be raised
  201. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should not change
  202. }
  203. [Fact]
  204. public void OnOptionFocused_Event_Raised ()
  205. {
  206. // Arrange
  207. Slider<int> slider = new (new() { 1, 2, 3 });
  208. var eventRaised = false;
  209. slider.OptionFocused += (sender, args) => eventRaised = true;
  210. var newFocusedOption = 1;
  211. SliderEventArgs<int> args = new (new (), newFocusedOption);
  212. // Act
  213. slider.OnOptionFocused (newFocusedOption, args);
  214. // Assert
  215. Assert.True (eventRaised);
  216. }
  217. [Fact]
  218. public void OnOptionsChanged_Event_Raised ()
  219. {
  220. // Arrange
  221. Slider<int> slider = new ();
  222. var eventRaised = false;
  223. slider.OptionsChanged += (sender, args) => eventRaised = true;
  224. // Act
  225. slider.OnOptionsChanged ();
  226. // Assert
  227. Assert.True (eventRaised);
  228. }
  229. [Fact]
  230. public void Set_Should_Not_UnSetFocusedOption_When_EmptyNotAllowed ()
  231. {
  232. // Arrange
  233. Slider<int> slider = new (new() { 1, 2, 3, 4 }) { AllowEmpty = false };
  234. slider.AutoSize = true;
  235. Assert.NotEmpty (slider.GetSetOptions ());
  236. // Act
  237. bool result = slider.UnSetOption (slider.FocusedOption);
  238. // Assert
  239. Assert.False (result);
  240. Assert.NotEmpty (slider.GetSetOptions ());
  241. }
  242. // Add similar tests for other methods like MoveMinus, MoveStart, MoveEnd, Set, etc.
  243. [Fact]
  244. public void Set_Should_SetFocusedOption ()
  245. {
  246. // Arrange
  247. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  248. slider.AutoSize = true;
  249. // Act
  250. slider.FocusedOption = 2;
  251. bool result = slider.Set ();
  252. // Assert
  253. Assert.True (result);
  254. Assert.Equal (2, slider.FocusedOption);
  255. Assert.Single (slider.GetSetOptions ());
  256. }
  257. [Fact]
  258. public void TryGetOptionByPosition_InvalidPosition_Failure ()
  259. {
  260. // Arrange
  261. Slider<int> slider = new (new() { 1, 2, 3 });
  262. var x = 10;
  263. var y = 10;
  264. var threshold = 2;
  265. int expectedOption = -1;
  266. // Act
  267. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  268. // Assert
  269. Assert.False (result);
  270. Assert.Equal (expectedOption, option);
  271. }
  272. [Theory]
  273. [InlineData (0, 0, 0, 1)]
  274. [InlineData (3, 0, 0, 2)]
  275. [InlineData (9, 0, 0, 4)]
  276. [InlineData (0, 0, 1, 1)]
  277. [InlineData (3, 0, 1, 2)]
  278. [InlineData (9, 0, 1, 4)]
  279. public void TryGetOptionByPosition_ValidPositionHorizontal_Success (int x, int y, int threshold, int expectedData)
  280. {
  281. // Arrange
  282. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  283. slider.AutoSize = true; // Set auto size to true to enable testing
  284. slider.InnerSpacing = 2;
  285. // 0123456789
  286. // 1--2--3--4
  287. // Arrange
  288. // Act
  289. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  290. // Assert
  291. Assert.True (result);
  292. Assert.Equal (expectedData, slider.Options [option].Data);
  293. }
  294. [Theory]
  295. [InlineData (0, 0, 0, 1)]
  296. [InlineData (0, 3, 0, 2)]
  297. [InlineData (0, 9, 0, 4)]
  298. [InlineData (0, 0, 1, 1)]
  299. [InlineData (0, 3, 1, 2)]
  300. [InlineData (0, 9, 1, 4)]
  301. public void TryGetOptionByPosition_ValidPositionVertical_Success (int x, int y, int threshold, int expectedData)
  302. {
  303. // Arrange
  304. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  305. slider.Orientation = Orientation.Vertical;
  306. slider.AutoSize = true; // Set auto size to true to enable testing
  307. slider.InnerSpacing = 2;
  308. // 0 1
  309. // 1 |
  310. // 2 |
  311. // 3 2
  312. // 4 |
  313. // 5 |
  314. // 6 3
  315. // 7 |
  316. // 8 |
  317. // 9 4
  318. // Act
  319. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  320. // Assert
  321. Assert.True (result);
  322. Assert.Equal (expectedData, slider.Options [option].Data);
  323. }
  324. [Fact]
  325. public void TryGetPositionByOption_InvalidOption_Failure ()
  326. {
  327. // Arrange
  328. Slider<int> slider = new (new() { 1, 2, 3 });
  329. int option = -1;
  330. (int, int) expectedPosition = (-1, -1);
  331. // Act
  332. bool result = slider.TryGetPositionByOption (option, out (int x, int y) position);
  333. // Assert
  334. Assert.False (result);
  335. Assert.Equal (expectedPosition, position);
  336. }
  337. [Theory]
  338. [InlineData (0, 0, 0)]
  339. [InlineData (1, 3, 0)]
  340. [InlineData (3, 9, 0)]
  341. public void TryGetPositionByOption_ValidOptionHorizontal_Success (int option, int expectedX, int expectedY)
  342. {
  343. // Arrange
  344. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  345. slider.AutoSize = true; // Set auto size to true to enable testing
  346. slider.InnerSpacing = 2;
  347. // 0123456789
  348. // 1--2--3--4
  349. // Act
  350. bool result = slider.TryGetPositionByOption (option, out (int x, int y) position);
  351. // Assert
  352. Assert.True (result);
  353. Assert.Equal (expectedX, position.x);
  354. Assert.Equal (expectedY, position.y);
  355. }
  356. [Theory]
  357. [InlineData (0, 0, 0)]
  358. [InlineData (1, 0, 3)]
  359. [InlineData (3, 0, 9)]
  360. public void TryGetPositionByOption_ValidOptionVertical_Success (int option, int expectedX, int expectedY)
  361. {
  362. // Arrange
  363. Slider<int> slider = new (new() { 1, 2, 3, 4 });
  364. slider.Orientation = Orientation.Vertical;
  365. slider.AutoSize = true; // Set auto size to true to enable testing
  366. slider.InnerSpacing = 2;
  367. // Act
  368. bool result = slider.TryGetPositionByOption (option, out (int x, int y) position);
  369. // Assert
  370. Assert.True (result);
  371. Assert.Equal (expectedX, position.x);
  372. Assert.Equal (expectedY, position.y);
  373. }
  374. // https://github.com/gui-cs/Terminal.Gui/issues/3099
  375. [Fact]
  376. private void One_Option_Does_Not_Throw ()
  377. {
  378. // Arrange
  379. Slider<int> slider = new ();
  380. slider.BeginInit ();
  381. slider.EndInit ();
  382. // Act/Assert
  383. slider.Options = new() { new () };
  384. }
  385. [Fact]
  386. private void Set_Options_No_Legend_Throws ()
  387. {
  388. // Arrange
  389. Slider<int> slider = new ();
  390. // Act/Assert
  391. Assert.Throws<ArgumentNullException> (() => slider.Options = null);
  392. }
  393. [Fact]
  394. private void Set_Options_Throws_If_Null ()
  395. {
  396. // Arrange
  397. Slider<int> slider = new ();
  398. // Act/Assert
  399. Assert.Throws<ArgumentNullException> (() => slider.Options = null);
  400. }
  401. [Fact]
  402. private void DimAuto_Both_Respects_SuperView_ContentSize ()
  403. {
  404. View view = new ()
  405. {
  406. Width = Dim.Fill (),
  407. Height = Dim.Fill ()
  408. };
  409. List<object> options = new () { "01234", "01234" };
  410. Slider slider = new (options)
  411. {
  412. Orientation = Orientation.Vertical,
  413. Type = SliderType.Multiple,
  414. Width = Dim.Auto (Dim.DimAutoStyle.Content),
  415. Height = Dim.Auto (Dim.DimAutoStyle.Content)
  416. };
  417. view.Add (slider);
  418. view.BeginInit ();
  419. view.EndInit ();
  420. Size expectedSize = slider.Frame.Size;
  421. Assert.Equal (new (6, 2), expectedSize);
  422. view.ContentSize = new (1, 1);
  423. view.LayoutSubviews ();
  424. slider.SetRelativeLayout (view.Viewport.Size);
  425. Assert.Equal (new (1, 1), slider.Frame.Size);
  426. }
  427. [Fact]
  428. private void DimAuto_Width_Respects_SuperView_ContentSize ()
  429. {
  430. View view = new ()
  431. {
  432. Width = Dim.Fill (),
  433. Height = 10
  434. };
  435. List<object> options = new () { "01234", "01234" };
  436. Slider slider = new (options)
  437. {
  438. Orientation = Orientation.Vertical,
  439. Type = SliderType.Multiple,
  440. Width = Dim.Auto (Dim.DimAutoStyle.Content),
  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.ContentSize = new (1, 1);
  449. view.LayoutSubviews ();
  450. slider.SetRelativeLayout (view.Viewport.Size);
  451. Assert.Equal (new (1, 10), 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. Height = Dim.Auto (Dim.DimAutoStyle.Content)
  468. };
  469. view.Add (slider);
  470. view.BeginInit ();
  471. view.EndInit ();
  472. Size expectedSize = slider.Frame.Size;
  473. Assert.Equal (new (10, 2), expectedSize);
  474. view.ContentSize = new (1, 1);
  475. view.LayoutSubviews ();
  476. slider.SetRelativeLayout (view.Viewport.Size);
  477. Assert.Equal (new (10, 1), slider.Frame.Size);
  478. }
  479. // Add more tests for different scenarios and edge cases.
  480. }