SliderTests.cs 15 KB

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