SliderTests.cs 15 KB

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