RadioGroupTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. // ReSharper disable AccessToModifiedClosure
  4. namespace Terminal.Gui.ViewsTests;
  5. public class RadioGroupTests (ITestOutputHelper output)
  6. {
  7. [Fact]
  8. public void Constructors_Defaults ()
  9. {
  10. var rg = new RadioGroup ();
  11. Assert.True (rg.CanFocus);
  12. Assert.Empty (rg.RadioLabels);
  13. Assert.Equal (Rectangle.Empty, rg.Frame);
  14. Assert.Equal (0, rg.SelectedItem);
  15. rg = new () { RadioLabels = new [] { "Test" } };
  16. Assert.True (rg.CanFocus);
  17. Assert.Single (rg.RadioLabels);
  18. Assert.Equal (0, rg.SelectedItem);
  19. rg = new ()
  20. {
  21. X = 1,
  22. Y = 2,
  23. Width = 20,
  24. Height = 5,
  25. RadioLabels = new [] { "Test" }
  26. };
  27. Assert.True (rg.CanFocus);
  28. Assert.Single (rg.RadioLabels);
  29. Assert.Equal (new (1, 2, 20, 5), rg.Frame);
  30. Assert.Equal (0, rg.SelectedItem);
  31. rg = new () { X = 1, Y = 2, RadioLabels = new [] { "Test" } };
  32. var view = new View { Width = 30, Height = 40 };
  33. view.Add (rg);
  34. view.BeginInit ();
  35. view.EndInit ();
  36. view.LayoutSubViews ();
  37. Assert.True (rg.CanFocus);
  38. Assert.Single (rg.RadioLabels);
  39. Assert.Equal (new (1, 2, 6, 1), rg.Frame);
  40. Assert.Equal (0, rg.SelectedItem);
  41. }
  42. [Fact]
  43. public void Initialize_SelectedItem_With_Minus_One ()
  44. {
  45. var rg = new RadioGroup { RadioLabels = new [] { "Test" }, SelectedItem = -1 };
  46. Application.Top = new ();
  47. Application.Top.Add (rg);
  48. rg.SetFocus ();
  49. Assert.Equal (-1, rg.SelectedItem);
  50. Application.RaiseKeyDownEvent (Key.Space);
  51. Assert.Equal (0, rg.SelectedItem);
  52. Application.Top.Dispose ();
  53. }
  54. [Fact]
  55. public void HotKeyBindings_Are_Added_Correctly ()
  56. {
  57. var rg = new RadioGroup { RadioLabels = new [] { "_Left", "_Right" } };
  58. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (Key.L));
  59. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (Key.R));
  60. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (Key.L.WithShift));
  61. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (Key.L.WithAlt));
  62. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (Key.R.WithShift));
  63. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (Key.R.WithAlt));
  64. }
  65. [Fact]
  66. public void Commands_HasFocus ()
  67. {
  68. Application.Navigation = new ();
  69. var rg = new RadioGroup
  70. {
  71. Id = "rg",
  72. RadioLabels = ["Test", "New Test"]
  73. };
  74. Application.Top = new ();
  75. Application.Top.Add (rg);
  76. rg.SetFocus ();
  77. Assert.Equal (Orientation.Vertical, rg.Orientation);
  78. var selectedItemChangedCount = 0;
  79. rg.SelectedItemChanged += (s, e) => selectedItemChangedCount++;
  80. var selectingCount = 0;
  81. rg.Selecting += (s, e) => selectingCount++;
  82. var acceptedCount = 0;
  83. rg.Accepting += (s, e) => acceptedCount++;
  84. // By default the first item is selected
  85. Assert.Equal (0, rg.SelectedItem);
  86. Assert.Equal (0, selectedItemChangedCount);
  87. Assert.Equal (0, selectingCount);
  88. Assert.Equal (0, acceptedCount);
  89. Assert.Equal (Key.Empty, rg.HotKey);
  90. // With HasFocus
  91. // Test up/down without Select
  92. Assert.False (Application.RaiseKeyDownEvent (Key.CursorUp)); // Should not change (should focus prev view if there was one, which there isn't)
  93. Assert.Equal (0, rg.SelectedItem);
  94. Assert.Equal (0, rg.Cursor);
  95. Assert.Equal (0, selectedItemChangedCount);
  96. Assert.Equal (0, selectingCount);
  97. Assert.Equal (0, acceptedCount);
  98. Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
  99. Assert.Equal (0, rg.SelectedItem); // Cursor changed, but selection didnt
  100. Assert.Equal (1, rg.Cursor);
  101. Assert.Equal (0, selectedItemChangedCount);
  102. Assert.Equal (0, selectingCount);
  103. Assert.Equal (0, acceptedCount);
  104. Assert.False (Application.RaiseKeyDownEvent (Key.CursorDown)); // Should not change selection (should focus next view if there was one, which there isn't)
  105. Assert.Equal (0, rg.SelectedItem);
  106. Assert.Equal (1, rg.Cursor);
  107. Assert.Equal (0, selectedItemChangedCount);
  108. Assert.Equal (0, selectingCount);
  109. Assert.Equal (0, acceptedCount);
  110. // Test Select (Space) when Cursor != SelectedItem - Should select cursor
  111. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  112. Assert.Equal (1, rg.SelectedItem);
  113. Assert.Equal (1, rg.Cursor);
  114. Assert.Equal (1, selectedItemChangedCount);
  115. Assert.Equal (1, selectingCount);
  116. Assert.Equal (0, acceptedCount);
  117. // Test Select (Space) when Cursor == SelectedItem - Should cycle
  118. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  119. Assert.Equal (0, rg.SelectedItem);
  120. Assert.Equal (0, rg.Cursor);
  121. Assert.Equal (2, selectedItemChangedCount);
  122. Assert.Equal (2, selectingCount);
  123. Assert.Equal (0, acceptedCount);
  124. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  125. Assert.Equal (1, rg.SelectedItem);
  126. Assert.Equal (1, rg.Cursor);
  127. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  128. Assert.Equal (0, rg.SelectedItem);
  129. Assert.Equal (0, rg.Cursor);
  130. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  131. Assert.Equal (1, rg.SelectedItem);
  132. Assert.Equal (1, rg.Cursor);
  133. Assert.True (Application.RaiseKeyDownEvent (Key.Home));
  134. Assert.Equal (1, rg.SelectedItem);
  135. Assert.Equal (0, rg.Cursor);
  136. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  137. Assert.Equal (0, rg.SelectedItem);
  138. Assert.Equal (0, rg.Cursor);
  139. Assert.True (Application.RaiseKeyDownEvent (Key.End));
  140. Assert.Equal (0, rg.SelectedItem);
  141. Assert.Equal (1, rg.Cursor);
  142. Assert.True (Application.RaiseKeyDownEvent (Key.Space));
  143. Assert.Equal (1, rg.SelectedItem);
  144. Assert.Equal (1, rg.Cursor);
  145. Assert.Equal (7, selectedItemChangedCount);
  146. Assert.Equal (7, selectingCount);
  147. Assert.Equal (0, acceptedCount);
  148. // Test HotKey
  149. // Selected == Cursor (1) - Advance state and raise Select event - DO NOT raise Accept
  150. rg.HotKey = Key.L;
  151. Assert.Equal (Key.L, rg.HotKey);
  152. Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
  153. Assert.Equal (0, rg.SelectedItem);
  154. Assert.Equal (0, rg.Cursor);
  155. Assert.Equal (8, selectedItemChangedCount);
  156. Assert.Equal (8, selectingCount);
  157. Assert.Equal (0, acceptedCount);
  158. // Make Selected != Cursor
  159. Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
  160. Assert.Equal (0, rg.SelectedItem);
  161. Assert.Equal (1, rg.Cursor);
  162. // Selected != Cursor - Raise HotKey event - Since we're focused, this should just advance
  163. Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
  164. Assert.Equal (1, rg.SelectedItem);
  165. Assert.Equal (1, rg.Cursor);
  166. Assert.Equal (9, selectedItemChangedCount);
  167. Assert.Equal (9, selectingCount);
  168. Assert.Equal (0, acceptedCount);
  169. Application.ResetState (true);
  170. }
  171. [Fact]
  172. public void HotKey_HasFocus_False ()
  173. {
  174. Application.Navigation = new ();
  175. var rg = new RadioGroup { RadioLabels = ["Test", "New Test"] };
  176. Application.Top = new ();
  177. // With !HasFocus
  178. View otherView = new () { Id = "otherView", CanFocus = true };
  179. Label label = new ()
  180. {
  181. Id = "label",
  182. Title = "_R"
  183. };
  184. Application.Top.Add (label, rg, otherView);
  185. otherView.SetFocus ();
  186. var selectedItemChangedCount = 0;
  187. rg.SelectedItemChanged += (s, e) => selectedItemChangedCount++;
  188. var selectCount = 0;
  189. rg.Selecting += (s, e) => selectCount++;
  190. var acceptCount = 0;
  191. rg.Accepting += (s, e) => acceptCount++;
  192. // By default the first item is selected
  193. Assert.Equal (0, rg.SelectedItem);
  194. Assert.Equal (Orientation.Vertical, rg.Orientation);
  195. Assert.Equal (0, selectedItemChangedCount);
  196. Assert.Equal (0, selectCount);
  197. Assert.Equal (0, acceptCount);
  198. Assert.Equal (Key.Empty, rg.HotKey);
  199. Assert.False (rg.HasFocus);
  200. // Test HotKey
  201. // Selected (0) == Cursor (0) - SetFocus
  202. rg.HotKey = Key.L;
  203. Assert.Equal (Key.L, rg.HotKey);
  204. Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
  205. Assert.True (rg.HasFocus);
  206. Assert.Equal (0, rg.SelectedItem);
  207. Assert.Equal (0, rg.Cursor);
  208. Assert.Equal (0, selectedItemChangedCount);
  209. Assert.Equal (0, selectCount);
  210. Assert.Equal (0, acceptCount);
  211. // Make Selected != Cursor
  212. Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
  213. Assert.Equal (0, rg.SelectedItem);
  214. Assert.Equal (1, rg.Cursor);
  215. otherView.SetFocus ();
  216. // Selected != Cursor - SetFocus
  217. Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
  218. Assert.True (rg.HasFocus);
  219. Assert.Equal (0, rg.SelectedItem);
  220. Assert.Equal (1, rg.Cursor);
  221. Assert.Equal (0, selectedItemChangedCount);
  222. Assert.Equal (0, selectCount);
  223. Assert.Equal (0, acceptCount);
  224. Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
  225. Assert.True (rg.HasFocus);
  226. Assert.Equal (1, rg.SelectedItem);
  227. Assert.Equal (1, rg.Cursor);
  228. Assert.Equal (1, selectedItemChangedCount);
  229. Assert.Equal (1, selectCount);
  230. Assert.Equal (0, acceptCount);
  231. Application.ResetState (true);
  232. }
  233. [Fact]
  234. public void HotKeys_HasFocus_False_Does_Not_SetFocus_Selects ()
  235. {
  236. Application.Navigation = new ();
  237. var rg = new RadioGroup { RadioLabels = ["Item _A", "Item _B"] };
  238. Application.Top = new ();
  239. // With !HasFocus
  240. View otherView = new () { Id = "otherView", CanFocus = true };
  241. Label label = new ()
  242. {
  243. Id = "label",
  244. Title = "_R"
  245. };
  246. Application.Top.Add (label, rg, otherView);
  247. otherView.SetFocus ();
  248. var selectedItemChangedCount = 0;
  249. rg.SelectedItemChanged += (s, e) => selectedItemChangedCount++;
  250. var selectCount = 0;
  251. rg.Selecting += (s, e) => selectCount++;
  252. var acceptCount = 0;
  253. rg.Accepting += (s, e) => acceptCount++;
  254. // By default the first item is selected
  255. Assert.Equal (0, rg.SelectedItem);
  256. Assert.Equal (Orientation.Vertical, rg.Orientation);
  257. Assert.Equal (0, selectedItemChangedCount);
  258. Assert.Equal (0, selectCount);
  259. Assert.Equal (0, acceptCount);
  260. Assert.Equal (Key.Empty, rg.HotKey);
  261. Assert.False (rg.HasFocus);
  262. // Test RadioTitem.HotKey - Should never SetFocus
  263. // Selected (0) == Cursor (0)
  264. Assert.True (Application.RaiseKeyDownEvent (Key.A));
  265. Assert.False (rg.HasFocus);
  266. Assert.Equal (0, rg.SelectedItem);
  267. Assert.Equal (0, rg.Cursor);
  268. Assert.Equal (0, selectedItemChangedCount);
  269. Assert.Equal (0, selectCount);
  270. Assert.Equal (0, acceptCount);
  271. rg.SetFocus ();
  272. // Make Selected != Cursor
  273. Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
  274. Assert.Equal (0, rg.SelectedItem);
  275. Assert.Equal (1, rg.Cursor);
  276. otherView.SetFocus ();
  277. // Selected != Cursor
  278. Assert.True (Application.RaiseKeyDownEvent (Key.A));
  279. Assert.False (rg.HasFocus);
  280. Assert.Equal (0, rg.SelectedItem);
  281. Assert.Equal (1, rg.Cursor);
  282. Assert.Equal (0, selectedItemChangedCount);
  283. Assert.Equal (0, selectCount);
  284. Assert.Equal (0, acceptCount);
  285. // Selected != Cursor - Should not set focus
  286. Assert.True (Application.RaiseKeyDownEvent (Key.B));
  287. Assert.False (rg.HasFocus);
  288. Assert.Equal (1, rg.SelectedItem);
  289. Assert.Equal (1, rg.Cursor);
  290. Assert.Equal (1, selectedItemChangedCount);
  291. Assert.Equal (1, selectCount);
  292. Assert.Equal (0, acceptCount);
  293. Assert.True (Application.RaiseKeyDownEvent (Key.B));
  294. Assert.False (rg.HasFocus);
  295. Assert.Equal (1, rg.SelectedItem);
  296. Assert.Equal (1, rg.Cursor);
  297. Assert.Equal (1, selectedItemChangedCount);
  298. Assert.Equal (1, selectCount);
  299. Assert.Equal (0, acceptCount);
  300. Application.ResetState (true);
  301. }
  302. [Fact]
  303. public void HotKeys_HasFocus_True_Selects ()
  304. {
  305. var rg = new RadioGroup { RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"] };
  306. Application.Top = new ();
  307. Application.Top.Add (rg);
  308. rg.SetFocus ();
  309. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (KeyCode.L));
  310. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
  311. Assert.NotEmpty (rg.HotKeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
  312. Assert.True (Application.RaiseKeyDownEvent (Key.T));
  313. Assert.Equal (2, rg.SelectedItem);
  314. Assert.True (Application.RaiseKeyDownEvent (Key.L));
  315. Assert.Equal (0, rg.SelectedItem);
  316. Assert.True (Application.RaiseKeyDownEvent (Key.J));
  317. Assert.Equal (3, rg.SelectedItem);
  318. Assert.True (Application.RaiseKeyDownEvent (Key.R));
  319. Assert.Equal (1, rg.SelectedItem);
  320. Assert.True (Application.RaiseKeyDownEvent (Key.T.WithAlt));
  321. Assert.Equal (2, rg.SelectedItem);
  322. Assert.True (Application.RaiseKeyDownEvent (Key.L.WithAlt));
  323. Assert.Equal (0, rg.SelectedItem);
  324. Assert.True (Application.RaiseKeyDownEvent (Key.J.WithAlt));
  325. Assert.Equal (3, rg.SelectedItem);
  326. Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
  327. Assert.Equal (1, rg.SelectedItem);
  328. Application.Top.Remove (rg);
  329. var superView = new View ();
  330. superView.Add (rg);
  331. Assert.True (superView.NewKeyDownEvent (Key.T));
  332. Assert.Equal (2, rg.SelectedItem);
  333. Assert.True (superView.NewKeyDownEvent (Key.L));
  334. Assert.Equal (0, rg.SelectedItem);
  335. Assert.True (superView.NewKeyDownEvent (Key.J));
  336. Assert.Equal (3, rg.SelectedItem);
  337. Assert.True (superView.NewKeyDownEvent (Key.R));
  338. Assert.Equal (1, rg.SelectedItem);
  339. Assert.True (superView.NewKeyDownEvent (Key.T.WithAlt));
  340. Assert.Equal (2, rg.SelectedItem);
  341. Assert.True (superView.NewKeyDownEvent (Key.L.WithAlt));
  342. Assert.Equal (0, rg.SelectedItem);
  343. Assert.True (superView.NewKeyDownEvent (Key.J.WithAlt));
  344. Assert.Equal (3, rg.SelectedItem);
  345. Assert.True (superView.NewKeyDownEvent (Key.R.WithAlt));
  346. Assert.Equal (1, rg.SelectedItem);
  347. Application.Top.Dispose ();
  348. }
  349. [Fact]
  350. public void HotKey_SetsFocus ()
  351. {
  352. var superView = new View
  353. {
  354. CanFocus = true
  355. };
  356. superView.Add (new View { CanFocus = true });
  357. var group = new RadioGroup
  358. {
  359. Title = "Radio_Group",
  360. RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"]
  361. };
  362. superView.Add (group);
  363. Assert.False (group.HasFocus);
  364. Assert.Equal (0, group.SelectedItem);
  365. group.NewKeyDownEvent (Key.G.WithAlt);
  366. Assert.Equal (0, group.SelectedItem);
  367. Assert.True (group.HasFocus);
  368. }
  369. [Fact]
  370. public void HotKey_No_SelectedItem_Selects_First ()
  371. {
  372. var superView = new View
  373. {
  374. CanFocus = true
  375. };
  376. superView.Add (new View { CanFocus = true });
  377. var group = new RadioGroup
  378. {
  379. Title = "Radio_Group",
  380. RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"]
  381. };
  382. group.SelectedItem = -1;
  383. superView.Add (group);
  384. Assert.False (group.HasFocus);
  385. Assert.Equal (-1, group.SelectedItem);
  386. group.NewKeyDownEvent (Key.G.WithAlt);
  387. Assert.Equal (0, group.SelectedItem);
  388. Assert.False (group.HasFocus);
  389. }
  390. [Fact]
  391. public void HotKeys_Does_Not_SetFocus ()
  392. {
  393. var superView = new View
  394. {
  395. CanFocus = true
  396. };
  397. superView.Add (new View { CanFocus = true });
  398. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  399. superView.Add (group);
  400. Assert.False (group.HasFocus);
  401. Assert.Equal (0, group.SelectedItem);
  402. group.NewKeyDownEvent (Key.R);
  403. Assert.Equal (1, group.SelectedItem);
  404. Assert.False (group.HasFocus);
  405. }
  406. [Fact]
  407. public void HotKey_Command_Does_Not_Accept ()
  408. {
  409. var group = new RadioGroup { RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"] };
  410. var accepted = false;
  411. group.Accepting += OnAccept;
  412. group.InvokeCommand (Command.HotKey);
  413. Assert.False (accepted);
  414. return;
  415. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  416. }
  417. [Fact]
  418. public void Accept_Command_Fires_Accept ()
  419. {
  420. var group = new RadioGroup { RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"] };
  421. var accepted = false;
  422. group.Accepting += OnAccept;
  423. group.InvokeCommand (Command.Accept);
  424. Assert.True (accepted);
  425. return;
  426. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  427. }
  428. [Fact]
  429. [AutoInitShutdown]
  430. public void Orientation_Width_Height_Vertical_Horizontal_Space ()
  431. {
  432. var rg = new RadioGroup { RadioLabels = ["Test", "New Test 你"] };
  433. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  434. win.Add (rg);
  435. var top = new Toplevel ();
  436. top.Add (win);
  437. Application.Begin (top);
  438. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  439. Assert.Equal (Orientation.Vertical, rg.Orientation);
  440. Assert.Equal (2, rg.RadioLabels.Length);
  441. Assert.Equal (0, rg.X);
  442. Assert.Equal (0, rg.Y);
  443. Assert.Equal (13, rg.Frame.Width);
  444. Assert.Equal (2, rg.Frame.Height);
  445. var expected = @$"
  446. ┌────────────────────────────┐
  447. │{Glyphs.Selected} Test │
  448. │{Glyphs.UnSelected} New Test 你 │
  449. │ │
  450. └────────────────────────────┘
  451. ";
  452. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  453. Assert.Equal (new (0, 0, 30, 5), pos);
  454. rg.Orientation = Orientation.Horizontal;
  455. Application.LayoutAndDraw ();
  456. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  457. Assert.Equal (2, rg.HorizontalSpace);
  458. Assert.Equal (0, rg.X);
  459. Assert.Equal (0, rg.Y);
  460. Assert.Equal (21, rg.Frame.Width);
  461. Assert.Equal (1, rg.Frame.Height);
  462. expected = @$"
  463. ┌────────────────────────────┐
  464. │{Glyphs.Selected} Test {Glyphs.UnSelected} New Test 你 │
  465. │ │
  466. │ │
  467. └────────────────────────────┘
  468. ";
  469. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  470. Assert.Equal (new (0, 0, 30, 5), pos);
  471. rg.HorizontalSpace = 4;
  472. Application.LayoutAndDraw ();
  473. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  474. Assert.Equal (4, rg.HorizontalSpace);
  475. Assert.Equal (0, rg.X);
  476. Assert.Equal (0, rg.Y);
  477. Assert.Equal (23, rg.Frame.Width);
  478. Assert.Equal (1, rg.Frame.Height);
  479. expected = @$"
  480. ┌────────────────────────────┐
  481. │{Glyphs.Selected} Test {Glyphs.UnSelected} New Test 你 │
  482. │ │
  483. │ │
  484. └────────────────────────────┘
  485. ";
  486. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  487. Assert.Equal (new (0, 0, 30, 5), pos);
  488. top.Dispose ();
  489. }
  490. [Fact]
  491. public void SelectedItemChanged_Event ()
  492. {
  493. int previousSelectedItem = -1;
  494. int selectedItem = -1;
  495. var rg = new RadioGroup { RadioLabels = ["Test", "New Test"] };
  496. rg.SelectedItemChanged += (s, e) =>
  497. {
  498. previousSelectedItem = e.PreviousSelectedItem;
  499. selectedItem = e.SelectedItem;
  500. };
  501. rg.SelectedItem = 1;
  502. Assert.Equal (0, previousSelectedItem);
  503. Assert.Equal (selectedItem, rg.SelectedItem);
  504. }
  505. #region Mouse Tests
  506. [Fact]
  507. [SetupFakeDriver]
  508. public void Mouse_Click ()
  509. {
  510. var radioGroup = new RadioGroup
  511. {
  512. RadioLabels = ["_1", "_2"]
  513. };
  514. Assert.True (radioGroup.CanFocus);
  515. var selectedItemChanged = 0;
  516. radioGroup.SelectedItemChanged += (s, e) => selectedItemChanged++;
  517. var selectingCount = 0;
  518. radioGroup.Selecting += (s, e) => selectingCount++;
  519. var acceptedCount = 0;
  520. radioGroup.Accepting += (s, e) => acceptedCount++;
  521. Assert.Equal (Orientation.Vertical, radioGroup.Orientation);
  522. radioGroup.HasFocus = true;
  523. Assert.True (radioGroup.HasFocus);
  524. Assert.Equal (0, radioGroup.SelectedItem);
  525. Assert.Equal (0, selectedItemChanged);
  526. Assert.Equal (0, selectingCount);
  527. Assert.Equal (0, acceptedCount);
  528. // Click on the first item, which is already selected
  529. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  530. Assert.Equal (0, radioGroup.SelectedItem);
  531. Assert.Equal (0, selectedItemChanged);
  532. Assert.Equal (0, selectingCount);
  533. Assert.Equal (0, acceptedCount);
  534. // Click on the second item
  535. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  536. Assert.Equal (1, radioGroup.SelectedItem);
  537. Assert.Equal (1, selectedItemChanged);
  538. Assert.Equal (1, selectingCount);
  539. Assert.Equal (0, acceptedCount);
  540. // Click on the first item
  541. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  542. Assert.Equal (0, radioGroup.SelectedItem);
  543. Assert.Equal (2, selectedItemChanged);
  544. Assert.Equal (2, selectingCount);
  545. Assert.Equal (0, acceptedCount);
  546. }
  547. [Fact]
  548. [SetupFakeDriver]
  549. public void Mouse_DoubleClick_Accepts ()
  550. {
  551. var radioGroup = new RadioGroup
  552. {
  553. RadioLabels = ["_1", "__2"]
  554. };
  555. Assert.True (radioGroup.CanFocus);
  556. var selectedItemChanged = 0;
  557. radioGroup.SelectedItemChanged += (s, e) => selectedItemChanged++;
  558. var selectingCount = 0;
  559. radioGroup.Selecting += (s, e) => selectingCount++;
  560. var acceptedCount = 0;
  561. var handleAccepted = false;
  562. radioGroup.Accepting += (s, e) =>
  563. {
  564. acceptedCount++;
  565. e.Cancel = handleAccepted;
  566. };
  567. Assert.True (radioGroup.DoubleClickAccepts);
  568. Assert.Equal (Orientation.Vertical, radioGroup.Orientation);
  569. radioGroup.HasFocus = true;
  570. Assert.True (radioGroup.HasFocus);
  571. Assert.Equal (0, radioGroup.SelectedItem);
  572. Assert.Equal (0, selectedItemChanged);
  573. Assert.Equal (0, selectingCount);
  574. Assert.Equal (0, acceptedCount);
  575. // NOTE: Drivers ALWAYS generate a Button1Clicked event before Button1DoubleClicked
  576. // NOTE: We need to do the same
  577. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  578. Assert.False (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
  579. Assert.Equal (0, radioGroup.SelectedItem);
  580. Assert.Equal (0, selectedItemChanged);
  581. Assert.Equal (0, selectingCount);
  582. Assert.Equal (1, acceptedCount);
  583. // single click twice
  584. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  585. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  586. Assert.Equal (1, radioGroup.SelectedItem);
  587. Assert.Equal (1, selectedItemChanged);
  588. Assert.Equal (1, selectingCount);
  589. Assert.Equal (1, acceptedCount);
  590. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  591. Assert.False (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1DoubleClicked }));
  592. Assert.Equal (1, radioGroup.SelectedItem);
  593. Assert.Equal (1, selectedItemChanged);
  594. Assert.Equal (1, selectingCount);
  595. Assert.Equal (2, acceptedCount);
  596. View superView = new () { Id = "superView", CanFocus = true };
  597. superView.Add (radioGroup);
  598. superView.SetFocus ();
  599. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  600. Assert.Equal (0, radioGroup.SelectedItem);
  601. Assert.Equal (2, selectedItemChanged);
  602. Assert.Equal (2, selectingCount);
  603. Assert.Equal (2, acceptedCount);
  604. var superViewAcceptCount = 0;
  605. superView.Accepting += (s, a) =>
  606. {
  607. superViewAcceptCount++;
  608. a.Cancel = true;
  609. };
  610. Assert.Equal (0, superViewAcceptCount);
  611. // By handling the event, we're cancelling it. So the radio group should not change.
  612. handleAccepted = true;
  613. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  614. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
  615. Assert.Equal (0, radioGroup.SelectedItem);
  616. Assert.Equal (2, selectedItemChanged);
  617. Assert.Equal (2, selectingCount);
  618. Assert.Equal (3, acceptedCount);
  619. Assert.Equal (0, superViewAcceptCount);
  620. handleAccepted = false;
  621. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  622. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
  623. Assert.Equal (0, radioGroup.SelectedItem);
  624. Assert.Equal (2, selectedItemChanged);
  625. Assert.Equal (2, selectingCount);
  626. Assert.Equal (4, acceptedCount);
  627. Assert.Equal (1, superViewAcceptCount); // Accept bubbles up to superview
  628. radioGroup.DoubleClickAccepts = false;
  629. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  630. Assert.False (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1DoubleClicked }));
  631. }
  632. #endregion Mouse Tests
  633. }