RadioGroupTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. using System.ComponentModel;
  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. var superView = new View ();
  329. superView.Add (rg);
  330. Assert.True (superView.NewKeyDownEvent (Key.T));
  331. Assert.Equal (2, rg.SelectedItem);
  332. Assert.True (superView.NewKeyDownEvent (Key.L));
  333. Assert.Equal (0, rg.SelectedItem);
  334. Assert.True (superView.NewKeyDownEvent (Key.J));
  335. Assert.Equal (3, rg.SelectedItem);
  336. Assert.True (superView.NewKeyDownEvent (Key.R));
  337. Assert.Equal (1, rg.SelectedItem);
  338. Assert.True (superView.NewKeyDownEvent (Key.T.WithAlt));
  339. Assert.Equal (2, rg.SelectedItem);
  340. Assert.True (superView.NewKeyDownEvent (Key.L.WithAlt));
  341. Assert.Equal (0, rg.SelectedItem);
  342. Assert.True (superView.NewKeyDownEvent (Key.J.WithAlt));
  343. Assert.Equal (3, rg.SelectedItem);
  344. Assert.True (superView.NewKeyDownEvent (Key.R.WithAlt));
  345. Assert.Equal (1, rg.SelectedItem);
  346. Application.Top.Dispose ();
  347. }
  348. [Fact]
  349. public void HotKey_SetsFocus ()
  350. {
  351. var superView = new View
  352. {
  353. CanFocus = true
  354. };
  355. superView.Add (new View { CanFocus = true });
  356. var group = new RadioGroup
  357. {
  358. Title = "Radio_Group",
  359. RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"]
  360. };
  361. superView.Add (group);
  362. Assert.False (group.HasFocus);
  363. Assert.Equal (0, group.SelectedItem);
  364. group.NewKeyDownEvent (Key.G.WithAlt);
  365. Assert.Equal (0, group.SelectedItem);
  366. Assert.True (group.HasFocus);
  367. }
  368. [Fact]
  369. public void HotKey_No_SelectedItem_Selects_First ()
  370. {
  371. var superView = new View
  372. {
  373. CanFocus = true
  374. };
  375. superView.Add (new View { CanFocus = true });
  376. var group = new RadioGroup
  377. {
  378. Title = "Radio_Group",
  379. RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"]
  380. };
  381. group.SelectedItem = -1;
  382. superView.Add (group);
  383. Assert.False (group.HasFocus);
  384. Assert.Equal (-1, group.SelectedItem);
  385. group.NewKeyDownEvent (Key.G.WithAlt);
  386. Assert.Equal (0, group.SelectedItem);
  387. Assert.False (group.HasFocus);
  388. }
  389. [Fact]
  390. public void HotKeys_Does_Not_SetFocus ()
  391. {
  392. var superView = new View
  393. {
  394. CanFocus = true
  395. };
  396. superView.Add (new View { CanFocus = true });
  397. var group = new RadioGroup { RadioLabels = new [] { "_Left", "_Right", "Cen_tered", "_Justified" } };
  398. superView.Add (group);
  399. Assert.False (group.HasFocus);
  400. Assert.Equal (0, group.SelectedItem);
  401. group.NewKeyDownEvent (Key.R);
  402. Assert.Equal (1, group.SelectedItem);
  403. Assert.False (group.HasFocus);
  404. }
  405. [Fact]
  406. public void HotKey_Command_Does_Not_Accept ()
  407. {
  408. var group = new RadioGroup { RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"] };
  409. var accepted = false;
  410. group.Accepting += OnAccept;
  411. group.InvokeCommand (Command.HotKey);
  412. Assert.False (accepted);
  413. return;
  414. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  415. }
  416. [Fact]
  417. public void Accept_Command_Fires_Accept ()
  418. {
  419. var group = new RadioGroup { RadioLabels = ["_Left", "_Right", "Cen_tered", "_Justified"] };
  420. var accepted = false;
  421. group.Accepting += OnAccept;
  422. group.InvokeCommand (Command.Accept);
  423. Assert.True (accepted);
  424. return;
  425. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  426. }
  427. [Fact]
  428. [AutoInitShutdown]
  429. public void Orientation_Width_Height_Vertical_Horizontal_Space ()
  430. {
  431. var rg = new RadioGroup { RadioLabels = ["Test", "New Test 你"] };
  432. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  433. win.Add (rg);
  434. var top = new Toplevel ();
  435. top.Add (win);
  436. Application.Begin (top);
  437. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  438. Assert.Equal (Orientation.Vertical, rg.Orientation);
  439. Assert.Equal (2, rg.RadioLabels.Length);
  440. Assert.Equal (0, rg.X);
  441. Assert.Equal (0, rg.Y);
  442. Assert.Equal (13, rg.Frame.Width);
  443. Assert.Equal (2, rg.Frame.Height);
  444. var expected = @$"
  445. ┌────────────────────────────┐
  446. │{CM.Glyphs.Selected} Test │
  447. │{CM.Glyphs.UnSelected} New Test 你 │
  448. │ │
  449. └────────────────────────────┘
  450. ";
  451. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  452. Assert.Equal (new (0, 0, 30, 5), pos);
  453. rg.Orientation = Orientation.Horizontal;
  454. Application.LayoutAndDraw ();
  455. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  456. Assert.Equal (2, rg.HorizontalSpace);
  457. Assert.Equal (0, rg.X);
  458. Assert.Equal (0, rg.Y);
  459. Assert.Equal (21, rg.Frame.Width);
  460. Assert.Equal (1, rg.Frame.Height);
  461. expected = @$"
  462. ┌────────────────────────────┐
  463. │{CM.Glyphs.Selected} Test {CM.Glyphs.UnSelected} New Test 你 │
  464. │ │
  465. │ │
  466. └────────────────────────────┘
  467. ";
  468. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  469. Assert.Equal (new (0, 0, 30, 5), pos);
  470. rg.HorizontalSpace = 4;
  471. Application.LayoutAndDraw ();
  472. Assert.Equal (Orientation.Horizontal, rg.Orientation);
  473. Assert.Equal (4, rg.HorizontalSpace);
  474. Assert.Equal (0, rg.X);
  475. Assert.Equal (0, rg.Y);
  476. Assert.Equal (23, rg.Frame.Width);
  477. Assert.Equal (1, rg.Frame.Height);
  478. expected = @$"
  479. ┌────────────────────────────┐
  480. │{CM.Glyphs.Selected} Test {CM.Glyphs.UnSelected} New Test 你 │
  481. │ │
  482. │ │
  483. └────────────────────────────┘
  484. ";
  485. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  486. Assert.Equal (new (0, 0, 30, 5), pos);
  487. top.Dispose ();
  488. }
  489. [Fact]
  490. public void SelectedItemChanged_Event ()
  491. {
  492. int previousSelectedItem = -1;
  493. int selectedItem = -1;
  494. var rg = new RadioGroup { RadioLabels = ["Test", "New Test"] };
  495. rg.SelectedItemChanged += (s, e) =>
  496. {
  497. previousSelectedItem = e.PreviousSelectedItem;
  498. selectedItem = e.SelectedItem;
  499. };
  500. rg.SelectedItem = 1;
  501. Assert.Equal (0, previousSelectedItem);
  502. Assert.Equal (selectedItem, rg.SelectedItem);
  503. }
  504. #region Mouse Tests
  505. [Fact]
  506. [SetupFakeDriver]
  507. public void Mouse_Click ()
  508. {
  509. var radioGroup = new RadioGroup
  510. {
  511. RadioLabels = ["_1", "_2"]
  512. };
  513. Assert.True (radioGroup.CanFocus);
  514. var selectedItemChanged = 0;
  515. radioGroup.SelectedItemChanged += (s, e) => selectedItemChanged++;
  516. var selectingCount = 0;
  517. radioGroup.Selecting += (s, e) => selectingCount++;
  518. var acceptedCount = 0;
  519. radioGroup.Accepting += (s, e) => acceptedCount++;
  520. Assert.Equal (Orientation.Vertical, radioGroup.Orientation);
  521. radioGroup.HasFocus = true;
  522. Assert.True (radioGroup.HasFocus);
  523. Assert.Equal (0, radioGroup.SelectedItem);
  524. Assert.Equal (0, selectedItemChanged);
  525. Assert.Equal (0, selectingCount);
  526. Assert.Equal (0, acceptedCount);
  527. // Click on the first item, which is already selected
  528. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  529. Assert.Equal (0, radioGroup.SelectedItem);
  530. Assert.Equal (0, selectedItemChanged);
  531. Assert.Equal (0, selectingCount);
  532. Assert.Equal (0, acceptedCount);
  533. // Click on the second item
  534. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  535. Assert.Equal (1, radioGroup.SelectedItem);
  536. Assert.Equal (1, selectedItemChanged);
  537. Assert.Equal (1, selectingCount);
  538. Assert.Equal (0, acceptedCount);
  539. // Click on the first item
  540. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  541. Assert.Equal (0, radioGroup.SelectedItem);
  542. Assert.Equal (2, selectedItemChanged);
  543. Assert.Equal (2, selectingCount);
  544. Assert.Equal (0, acceptedCount);
  545. }
  546. [Fact]
  547. [SetupFakeDriver]
  548. public void Mouse_DoubleClick ()
  549. {
  550. var radioGroup = new RadioGroup
  551. {
  552. RadioLabels = ["_1", "__2"]
  553. };
  554. Assert.True (radioGroup.CanFocus);
  555. var selectedItemChanged = 0;
  556. radioGroup.SelectedItemChanged += (s, e) => selectedItemChanged++;
  557. var selectingCount = 0;
  558. radioGroup.Selecting += (s, e) => selectingCount++;
  559. var acceptedCount = 0;
  560. var handleAccepted = false;
  561. radioGroup.Accepting += (s, e) =>
  562. {
  563. acceptedCount++;
  564. e.Cancel = handleAccepted;
  565. };
  566. Assert.True (radioGroup.DoubleClickAccepts);
  567. Assert.Equal (Orientation.Vertical, radioGroup.Orientation);
  568. radioGroup.HasFocus = true;
  569. Assert.True (radioGroup.HasFocus);
  570. Assert.Equal (0, radioGroup.SelectedItem);
  571. Assert.Equal (0, selectedItemChanged);
  572. Assert.Equal (0, selectingCount);
  573. Assert.Equal (0, acceptedCount);
  574. // NOTE: Drivers ALWAYS generate a Button1Clicked event before Button1DoubleClicked
  575. // NOTE: We need to do the same
  576. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  577. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
  578. Assert.Equal (0, radioGroup.SelectedItem);
  579. Assert.Equal (0, selectedItemChanged);
  580. Assert.Equal (0, selectingCount);
  581. Assert.Equal (1, acceptedCount);
  582. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  583. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  584. Assert.Equal (1, radioGroup.SelectedItem);
  585. Assert.Equal (1, selectedItemChanged);
  586. Assert.Equal (1, selectingCount);
  587. Assert.Equal (1, acceptedCount);
  588. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  589. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1DoubleClicked }));
  590. Assert.Equal (1, radioGroup.SelectedItem);
  591. Assert.Equal (1, selectedItemChanged);
  592. Assert.Equal (1, selectingCount);
  593. Assert.Equal (2, acceptedCount);
  594. View superView = new () { Id = "superView", CanFocus = true };
  595. superView.Add (radioGroup);
  596. superView.SetFocus ();
  597. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  598. Assert.Equal (0, radioGroup.SelectedItem);
  599. Assert.Equal (2, selectedItemChanged);
  600. Assert.Equal (2, selectingCount);
  601. Assert.Equal (2, acceptedCount);
  602. var superViewAcceptCount = 0;
  603. superView.Accepting += (s, a) =>
  604. {
  605. superViewAcceptCount++;
  606. a.Cancel = true;
  607. };
  608. Assert.Equal (0, superViewAcceptCount);
  609. // By handling the event, we're cancelling it. So the radio group should not change.
  610. handleAccepted = true;
  611. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  612. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
  613. Assert.Equal (0, radioGroup.SelectedItem);
  614. Assert.Equal (2, selectedItemChanged);
  615. Assert.Equal (2, selectingCount);
  616. Assert.Equal (3, acceptedCount);
  617. Assert.Equal (0, superViewAcceptCount);
  618. handleAccepted = false;
  619. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked }));
  620. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
  621. Assert.Equal (0, radioGroup.SelectedItem);
  622. Assert.Equal (2, selectedItemChanged);
  623. Assert.Equal (2, selectingCount);
  624. Assert.Equal (4, acceptedCount);
  625. Assert.Equal (1, superViewAcceptCount); // Accept bubbles up to superview
  626. radioGroup.DoubleClickAccepts = false;
  627. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1Clicked }));
  628. Assert.True (radioGroup.NewMouseEvent (new () { Position = new (0, 1), Flags = MouseFlags.Button1DoubleClicked }));
  629. }
  630. #endregion Mouse Tests
  631. }