SliderTests.cs 14 KB

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