SliderTests.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using Xunit;
  2. using Terminal.Gui;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Terminal.Gui.ViewsTests;
  9. public class SliderOptionTests {
  10. [Fact]
  11. public void OnSet_Should_Raise_SetEvent ()
  12. {
  13. // Arrange
  14. var sliderOption = new SliderOption<int> ();
  15. var eventRaised = false;
  16. sliderOption.Set += (sender, args) => eventRaised = true;
  17. // Act
  18. sliderOption.OnSet ();
  19. // Assert
  20. Assert.True (eventRaised);
  21. }
  22. [Fact]
  23. public void OnUnSet_Should_Raise_UnSetEvent ()
  24. {
  25. // Arrange
  26. var sliderOption = new SliderOption<int> ();
  27. var eventRaised = false;
  28. sliderOption.UnSet += (sender, args) => eventRaised = true;
  29. // Act
  30. sliderOption.OnUnSet ();
  31. // Assert
  32. Assert.True (eventRaised);
  33. }
  34. [Fact]
  35. public void OnChanged_Should_Raise_ChangedEvent ()
  36. {
  37. // Arrange
  38. var sliderOption = new SliderOption<int> ();
  39. var eventRaised = false;
  40. sliderOption.Changed += (sender, args) => eventRaised = true;
  41. // Act
  42. sliderOption.OnChanged (true);
  43. // Assert
  44. Assert.True (eventRaised);
  45. }
  46. }
  47. public class SliderEventArgsTests {
  48. [Fact]
  49. public void Constructor_Sets_Options ()
  50. {
  51. // Arrange
  52. var options = new Dictionary<int, SliderOption<int>> ();
  53. // Act
  54. var sliderEventArgs = new SliderEventArgs<int> (options);
  55. // Assert
  56. Assert.Equal (options, sliderEventArgs.Options);
  57. }
  58. [Fact]
  59. public void Constructor_Sets_Focused ()
  60. {
  61. // Arrange
  62. var options = new Dictionary<int, SliderOption<int>> ();
  63. var focused = 42;
  64. // Act
  65. var sliderEventArgs = new SliderEventArgs<int> (options, focused);
  66. // Assert
  67. Assert.Equal (focused, sliderEventArgs.Focused);
  68. }
  69. [Fact]
  70. public void Constructor_Sets_Cancel_Default_To_False ()
  71. {
  72. // Arrange
  73. var options = new Dictionary<int, SliderOption<int>> ();
  74. var focused = 42;
  75. // Act
  76. var sliderEventArgs = new SliderEventArgs<int> (options, focused);
  77. // Assert
  78. Assert.False (sliderEventArgs.Cancel);
  79. }
  80. }
  81. public class SliderTests {
  82. [Fact]
  83. public void Constructor_Default ()
  84. {
  85. // Arrange & Act
  86. var slider = new Slider<int> ();
  87. // Assert
  88. Assert.NotNull (slider);
  89. Assert.NotNull (slider.Options);
  90. Assert.Empty (slider.Options);
  91. Assert.Equal (Orientation.Horizontal, slider.Orientation);
  92. Assert.False (slider.AllowEmpty);
  93. Assert.True (slider.ShowLegends);
  94. Assert.False (slider.ShowSpacing);
  95. Assert.Equal (SliderType.Single, slider.Type);
  96. Assert.Equal (0, slider.InnerSpacing);
  97. Assert.False (slider.AutoSize);
  98. Assert.Equal (0, slider.FocusedOption);
  99. }
  100. [Fact]
  101. public void Constructor_With_Options ()
  102. {
  103. // Arrange
  104. var options = new List<int> { 1, 2, 3 };
  105. // Act
  106. var slider = new Slider<int> (options);
  107. // Assert
  108. Assert.NotNull (slider);
  109. Assert.NotNull (slider.Options);
  110. Assert.Equal (options.Count, slider.Options.Count);
  111. }
  112. [Fact]
  113. public void OnOptionsChanged_Event_Raised ()
  114. {
  115. // Arrange
  116. var slider = new Slider<int> ();
  117. bool eventRaised = false;
  118. slider.OptionsChanged += (sender, args) => eventRaised = true;
  119. // Act
  120. slider.OnOptionsChanged ();
  121. // Assert
  122. Assert.True (eventRaised);
  123. }
  124. [Fact]
  125. public void OnOptionFocused_Event_Raised ()
  126. {
  127. // Arrange
  128. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  129. bool eventRaised = false;
  130. slider.OptionFocused += (sender, args) => eventRaised = true;
  131. int newFocusedOption = 1;
  132. var args = new SliderEventArgs<int> (new Dictionary<int, SliderOption<int>> (), newFocusedOption);
  133. // Act
  134. slider.OnOptionFocused (newFocusedOption, args);
  135. // Assert
  136. Assert.True (eventRaised);
  137. }
  138. [Fact]
  139. public void OnOptionFocused_Event_Cancelled ()
  140. {
  141. // Arrange
  142. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  143. bool eventRaised = false;
  144. bool cancel = false;
  145. slider.OptionFocused += (sender, args) => eventRaised = true;
  146. int newFocusedOption = 1;
  147. // Create args with cancel set to false
  148. cancel = false;
  149. var args = new SliderEventArgs<int> (new Dictionary<int, SliderOption<int>> (), newFocusedOption) {
  150. Cancel = cancel
  151. };
  152. Assert.Equal (0, slider.FocusedOption);
  153. // Act
  154. slider.OnOptionFocused (newFocusedOption, args);
  155. // Assert
  156. Assert.True (eventRaised); // Event should be raised
  157. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should change
  158. // Create args with cancel set to true
  159. cancel = true;
  160. args = new SliderEventArgs<int> (new Dictionary<int, SliderOption<int>> (), newFocusedOption) {
  161. Cancel = cancel
  162. };
  163. // Act
  164. slider.OnOptionFocused (2, args);
  165. // Assert
  166. Assert.True (eventRaised); // Event should be raised
  167. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should not change
  168. }
  169. [Theory]
  170. [InlineData (0, 0, 0)]
  171. [InlineData (1, 3, 0)]
  172. [InlineData (3, 9, 0)]
  173. public void TryGetPositionByOption_ValidOptionHorizontal_Success (int option, int expectedX, int expectedY)
  174. {
  175. // Arrange
  176. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  177. slider.AutoSize = true; // Set auto size to true to enable testing
  178. slider.InnerSpacing = 2;
  179. // 0123456789
  180. // 1--2--3--4
  181. // Act
  182. bool result = slider.TryGetPositionByOption (option, out var position);
  183. // Assert
  184. Assert.True (result);
  185. Assert.Equal (expectedX, position.x);
  186. Assert.Equal (expectedY, position.y);
  187. }
  188. [Theory]
  189. [InlineData (0, 0, 0)]
  190. [InlineData (1, 0, 3)]
  191. [InlineData (3, 0, 9)]
  192. public void TryGetPositionByOption_ValidOptionVertical_Success (int option, int expectedX, int expectedY)
  193. {
  194. // Arrange
  195. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  196. slider.Orientation = Orientation.Vertical;
  197. slider.AutoSize = true; // Set auto size to true to enable testing
  198. slider.InnerSpacing = 2;
  199. // Act
  200. bool result = slider.TryGetPositionByOption (option, out var position);
  201. // Assert
  202. Assert.True (result);
  203. Assert.Equal (expectedX, position.x);
  204. Assert.Equal (expectedY, position.y);
  205. }
  206. [Fact]
  207. public void TryGetPositionByOption_InvalidOption_Failure ()
  208. {
  209. // Arrange
  210. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  211. int option = -1;
  212. var expectedPosition = (-1, -1);
  213. // Act
  214. bool result = slider.TryGetPositionByOption (option, out var position);
  215. // Assert
  216. Assert.False (result);
  217. Assert.Equal (expectedPosition, position);
  218. }
  219. [Theory]
  220. [InlineData (0, 0, 0, 1)]
  221. [InlineData (3, 0, 0, 2)]
  222. [InlineData (9, 0, 0, 4)]
  223. [InlineData (0, 0, 1, 1)]
  224. [InlineData (3, 0, 1, 2)]
  225. [InlineData (9, 0, 1, 4)]
  226. public void TryGetOptionByPosition_ValidPositionHorizontal_Success (int x, int y, int threshold, int expectedData)
  227. {
  228. // Arrange
  229. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  230. slider.AutoSize = true; // Set auto size to true to enable testing
  231. slider.InnerSpacing = 2;
  232. // 0123456789
  233. // 1--2--3--4
  234. // Arrange
  235. // Act
  236. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  237. // Assert
  238. Assert.True (result);
  239. Assert.Equal (expectedData, slider.Options [option].Data);
  240. }
  241. [Theory]
  242. [InlineData (0, 0, 0, 1)]
  243. [InlineData (0, 3, 0, 2)]
  244. [InlineData (0, 9, 0, 4)]
  245. [InlineData (0, 0, 1, 1)]
  246. [InlineData (0, 3, 1, 2)]
  247. [InlineData (0, 9, 1, 4)]
  248. public void TryGetOptionByPosition_ValidPositionVertical_Success (int x, int y, int threshold, int expectedData)
  249. {
  250. // Arrange
  251. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  252. slider.Orientation = Orientation.Vertical;
  253. slider.AutoSize = true; // Set auto size to true to enable testing
  254. slider.InnerSpacing = 2;
  255. // 0 1
  256. // 1 |
  257. // 2 |
  258. // 3 2
  259. // 4 |
  260. // 5 |
  261. // 6 3
  262. // 7 |
  263. // 8 |
  264. // 9 4
  265. slider.CalcSpacingConfig ();
  266. // Arrange
  267. // Act
  268. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  269. // Assert
  270. Assert.True (result);
  271. Assert.Equal (expectedData, slider.Options [option].Data);
  272. }
  273. [Fact]
  274. public void TryGetOptionByPosition_InvalidPosition_Failure ()
  275. {
  276. // Arrange
  277. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  278. int x = 10;
  279. int y = 10;
  280. int threshold = 2;
  281. int expectedOption = -1;
  282. // Act
  283. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  284. // Assert
  285. Assert.False (result);
  286. Assert.Equal (expectedOption, option);
  287. }
  288. [Fact]
  289. public void MovePlus_Should_MoveFocusRight_When_OptionIsAvailable ()
  290. {
  291. // Arrange
  292. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  293. slider.AutoSize = true;
  294. // Act
  295. bool result = slider.MovePlus ();
  296. // Assert
  297. Assert.True (result);
  298. Assert.Equal (1, slider.FocusedOption);
  299. }
  300. [Fact]
  301. public void MovePlus_Should_NotMoveFocusRight_When_AtEnd ()
  302. {
  303. // Arrange
  304. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  305. slider.AutoSize = true;
  306. slider.FocusedOption = 3;
  307. // Act
  308. bool result = slider.MovePlus ();
  309. // Assert
  310. Assert.False (result);
  311. Assert.Equal (3, slider.FocusedOption);
  312. }
  313. // Add similar tests for other methods like MoveMinus, MoveStart, MoveEnd, Set, etc.
  314. [Fact]
  315. public void Set_Should_SetFocusedOption ()
  316. {
  317. // Arrange
  318. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  319. slider.AutoSize = true;
  320. // Act
  321. slider.FocusedOption = 2;
  322. bool result = slider.Set ();
  323. // Assert
  324. Assert.True (result);
  325. Assert.Equal (2, slider.FocusedOption);
  326. Assert.Single (slider.GetSetOptions ());
  327. }
  328. [Fact]
  329. public void Set_Should_Not_UnSetFocusedOption_When_EmptyNotAllowed ()
  330. {
  331. // Arrange
  332. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 }) {
  333. AllowEmpty = false
  334. };
  335. slider.AutoSize = true;
  336. Assert.NotEmpty (slider.GetSetOptions ());
  337. // Act
  338. bool result = slider.UnSetOption (slider.FocusedOption);
  339. // Assert
  340. Assert.False (result);
  341. Assert.NotEmpty (slider.GetSetOptions());
  342. }
  343. // Add more tests for different scenarios and edge cases.
  344. }