ScenarioTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. using System.Reflection;
  2. using Xunit.Abstractions;
  3. namespace UICatalog.Tests;
  4. public class ScenarioTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public ScenarioTests (ITestOutputHelper output)
  8. {
  9. #if DEBUG_IDISPOSABLE
  10. Responder.Instances.Clear ();
  11. #endif
  12. _output = output;
  13. }
  14. /// <summary>
  15. /// <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
  16. /// <para>Should find any Scenarios which crash on load or do not respond to <see cref="Application.RequestStop()"/>.</para>
  17. /// </summary>
  18. [Fact]
  19. public void Run_All_Scenarios ()
  20. {
  21. List<Scenario> scenarios = Scenario.GetScenarios ();
  22. Assert.NotEmpty (scenarios);
  23. foreach (Scenario scenario in scenarios)
  24. {
  25. _output.WriteLine ($"Running Scenario '{scenario.GetName ()}'");
  26. Application.Init (new FakeDriver ());
  27. // Press QuitKey
  28. Assert.Empty (FakeConsole.MockKeyPresses);
  29. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  30. // by adding this Space it seems to work.
  31. //FakeConsole.PushMockKeyPress (Key.Space);
  32. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  33. // The only key we care about is the QuitKey
  34. Application.Top.KeyDown += (sender, args) =>
  35. {
  36. _output.WriteLine ($" Keypress: {args.KeyCode}");
  37. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  38. // by adding this Space it seems to work.
  39. // See #2474 for why this is commented out
  40. Assert.Equal (Application.QuitKey.KeyCode, args.KeyCode);
  41. };
  42. uint abortTime = 500;
  43. // If the scenario doesn't close within 500ms, this will force it to quit
  44. Func<bool> forceCloseCallback = () =>
  45. {
  46. if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
  47. {
  48. Application.RequestStop ();
  49. // See #2474 for why this is commented out
  50. Assert.Fail (
  51. $"'{
  52. scenario.GetName ()
  53. }' failed to Quit with {
  54. Application.QuitKey
  55. } after {
  56. abortTime
  57. }ms. Force quit."
  58. );
  59. }
  60. return false;
  61. };
  62. //output.WriteLine ($" Add timeout to force quit after {abortTime}ms");
  63. _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), forceCloseCallback);
  64. Application.Iteration += (s, a) =>
  65. {
  66. //output.WriteLine ($" iteration {++iterations}");
  67. if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
  68. {
  69. Application.RequestStop ();
  70. Assert.Fail ($"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey}. Force quit.");
  71. }
  72. };
  73. scenario.Init ();
  74. scenario.Setup ();
  75. scenario.Run ();
  76. scenario.Dispose ();
  77. Application.Shutdown ();
  78. #if DEBUG_IDISPOSABLE
  79. Assert.Empty (Responder.Instances);
  80. #endif
  81. }
  82. #if DEBUG_IDISPOSABLE
  83. Assert.Empty (Responder.Instances);
  84. #endif
  85. }
  86. [Fact]
  87. public void Run_All_Views_Tester_Scenario ()
  88. {
  89. Window _leftPane;
  90. ListView _classListView;
  91. FrameView _hostPane;
  92. Dictionary<string, Type> _viewClasses;
  93. View _curView = null;
  94. // Settings
  95. FrameView _settingsPane;
  96. CheckBox _computedCheckBox;
  97. FrameView _locationFrame;
  98. RadioGroup _xRadioGroup;
  99. TextField _xText;
  100. var _xVal = 0;
  101. RadioGroup _yRadioGroup;
  102. TextField _yText;
  103. var _yVal = 0;
  104. FrameView _sizeFrame;
  105. RadioGroup _wRadioGroup;
  106. TextField _wText;
  107. var _wVal = 0;
  108. RadioGroup _hRadioGroup;
  109. TextField _hText;
  110. var _hVal = 0;
  111. List<string> posNames = new () { "Factor", "AnchorEnd", "Center", "Absolute" };
  112. List<string> dimNames = new () { "Factor", "Fill", "Absolute" };
  113. Application.Init (new FakeDriver ());
  114. Toplevel Top = Application.Top;
  115. _viewClasses = GetAllViewClassesCollection ()
  116. .OrderBy (t => t.Name)
  117. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  118. .ToDictionary (t => t.Key, t => t.Value);
  119. _leftPane = new Window
  120. {
  121. Title = "Classes",
  122. X = 0,
  123. Y = 0,
  124. Width = 15,
  125. Height = Dim.Fill (1), // for status bar
  126. CanFocus = false,
  127. ColorScheme = Colors.ColorSchemes ["TopLevel"]
  128. };
  129. _classListView = new ListView
  130. {
  131. X = 0,
  132. Y = 0,
  133. Width = Dim.Fill (),
  134. Height = Dim.Fill (),
  135. AllowsMarking = false,
  136. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  137. Source = new ListWrapper (_viewClasses.Keys.ToList ())
  138. };
  139. _leftPane.Add (_classListView);
  140. _settingsPane = new FrameView
  141. {
  142. X = Pos.Right (_leftPane),
  143. Y = 0, // for menu
  144. Width = Dim.Fill (),
  145. Height = 10,
  146. CanFocus = false,
  147. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  148. Title = "Settings"
  149. };
  150. _computedCheckBox = new CheckBox { X = 0, Y = 0, Text = "Computed Layout", Checked = true };
  151. _settingsPane.Add (_computedCheckBox);
  152. var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "At(x)" };
  153. _locationFrame = new FrameView
  154. {
  155. X = Pos.Left (_computedCheckBox),
  156. Y = Pos.Bottom (_computedCheckBox),
  157. Height = 3 + radioItems.Length,
  158. Width = 36,
  159. Title = "Location (Pos)"
  160. };
  161. _settingsPane.Add (_locationFrame);
  162. var label = new Label { X = 0, Y = 0, Text = "x:" };
  163. _locationFrame.Add (label);
  164. _xRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  165. _xText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  166. _locationFrame.Add (_xText);
  167. _locationFrame.Add (_xRadioGroup);
  168. radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "At(y)" };
  169. label = new Label { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "y:" };
  170. _locationFrame.Add (label);
  171. _yText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  172. _locationFrame.Add (_yText);
  173. _yRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  174. _locationFrame.Add (_yRadioGroup);
  175. _sizeFrame = new FrameView
  176. {
  177. X = Pos.Right (_locationFrame),
  178. Y = Pos.Y (_locationFrame),
  179. Height = 3 + radioItems.Length,
  180. Width = 40,
  181. Title = "Size (Dim)"
  182. };
  183. radioItems = new [] { "Percent(width)", "Fill(width)", "Sized(width)" };
  184. label = new Label { X = 0, Y = 0, Text = "width:" };
  185. _sizeFrame.Add (label);
  186. _wRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  187. _wText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  188. _sizeFrame.Add (_wText);
  189. _sizeFrame.Add (_wRadioGroup);
  190. radioItems = new [] { "Percent(height)", "Fill(height)", "Sized(height)" };
  191. label = new Label { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "height:" };
  192. _sizeFrame.Add (label);
  193. _hText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  194. _sizeFrame.Add (_hText);
  195. _hRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  196. _sizeFrame.Add (_hRadioGroup);
  197. _settingsPane.Add (_sizeFrame);
  198. _hostPane = new FrameView
  199. {
  200. X = Pos.Right (_leftPane),
  201. Y = Pos.Bottom (_settingsPane),
  202. Width = Dim.Fill (),
  203. Height = Dim.Fill (1), // + 1 for status bar
  204. ColorScheme = Colors.ColorSchemes ["Dialog"]
  205. };
  206. _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
  207. _classListView.SelectedItemChanged += (s, args) =>
  208. {
  209. // Remove existing class, if any
  210. if (_curView != null)
  211. {
  212. _curView.LayoutComplete -= LayoutCompleteHandler;
  213. _hostPane.Remove (_curView);
  214. _curView.Dispose ();
  215. _curView = null;
  216. _hostPane.Clear (_hostPane.Viewport);
  217. }
  218. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  219. };
  220. _computedCheckBox.Toggled += (s, e) =>
  221. {
  222. if (_curView != null)
  223. {
  224. //_curView.LayoutStyle = e.OldValue == true ? LayoutStyle.Absolute : LayoutStyle.Computed;
  225. _hostPane.LayoutSubviews ();
  226. }
  227. };
  228. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  229. _xText.TextChanged += (s, args) =>
  230. {
  231. try
  232. {
  233. _xVal = int.Parse (_xText.Text);
  234. DimPosChanged (_curView);
  235. }
  236. catch
  237. { }
  238. };
  239. _yText.TextChanged += (s, e) =>
  240. {
  241. try
  242. {
  243. _yVal = int.Parse (_yText.Text);
  244. DimPosChanged (_curView);
  245. }
  246. catch
  247. { }
  248. };
  249. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  250. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  251. _wText.TextChanged += (s, args) =>
  252. {
  253. try
  254. {
  255. _wVal = int.Parse (_wText.Text);
  256. DimPosChanged (_curView);
  257. }
  258. catch
  259. { }
  260. };
  261. _hText.TextChanged += (s, args) =>
  262. {
  263. try
  264. {
  265. _hVal = int.Parse (_hText.Text);
  266. DimPosChanged (_curView);
  267. }
  268. catch
  269. { }
  270. };
  271. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  272. Top.Add (_leftPane, _settingsPane, _hostPane);
  273. Top.LayoutSubviews ();
  274. _curView = CreateClass (_viewClasses.First ().Value);
  275. var iterations = 0;
  276. Application.Iteration += (s, a) =>
  277. {
  278. iterations++;
  279. if (iterations < _viewClasses.Count)
  280. {
  281. _classListView.MoveDown ();
  282. Assert.Equal (
  283. _curView.GetType ().Name,
  284. _viewClasses.Values.ToArray () [_classListView.SelectedItem].Name
  285. );
  286. }
  287. else
  288. {
  289. Application.RequestStop ();
  290. }
  291. };
  292. Application.Run ();
  293. Assert.Equal (_viewClasses.Count, iterations);
  294. Application.Shutdown ();
  295. void DimPosChanged (View view)
  296. {
  297. if (view == null)
  298. {
  299. return;
  300. }
  301. LayoutStyle layout = view.LayoutStyle;
  302. try
  303. {
  304. //view.LayoutStyle = LayoutStyle.Absolute;
  305. switch (_xRadioGroup.SelectedItem)
  306. {
  307. case 0:
  308. view.X = Pos.Percent (_xVal);
  309. break;
  310. case 1:
  311. view.X = Pos.AnchorEnd (_xVal);
  312. break;
  313. case 2:
  314. view.X = Pos.Center ();
  315. break;
  316. case 3:
  317. view.X = Pos.At (_xVal);
  318. break;
  319. }
  320. switch (_yRadioGroup.SelectedItem)
  321. {
  322. case 0:
  323. view.Y = Pos.Percent (_yVal);
  324. break;
  325. case 1:
  326. view.Y = Pos.AnchorEnd (_yVal);
  327. break;
  328. case 2:
  329. view.Y = Pos.Center ();
  330. break;
  331. case 3:
  332. view.Y = Pos.At (_yVal);
  333. break;
  334. }
  335. switch (_wRadioGroup.SelectedItem)
  336. {
  337. case 0:
  338. view.Width = Dim.Percent (_wVal);
  339. break;
  340. case 1:
  341. view.Width = Dim.Fill (_wVal);
  342. break;
  343. case 2:
  344. view.Width = Dim.Sized (_wVal);
  345. break;
  346. }
  347. switch (_hRadioGroup.SelectedItem)
  348. {
  349. case 0:
  350. view.Height = Dim.Percent (_hVal);
  351. break;
  352. case 1:
  353. view.Height = Dim.Fill (_hVal);
  354. break;
  355. case 2:
  356. view.Height = Dim.Sized (_hVal);
  357. break;
  358. }
  359. }
  360. catch (Exception e)
  361. {
  362. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  363. }
  364. UpdateTitle (view);
  365. }
  366. void UpdateSettings (View view)
  367. {
  368. var x = view.X.ToString ();
  369. var y = view.Y.ToString ();
  370. _xRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => x.Contains (s)).First ());
  371. _yRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => y.Contains (s)).First ());
  372. _xText.Text = $"{view.Frame.X}";
  373. _yText.Text = $"{view.Frame.Y}";
  374. var w = view.Width.ToString ();
  375. var h = view.Height.ToString ();
  376. _wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => w.Contains (s)).First ());
  377. _hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => h.Contains (s)).First ());
  378. _wText.Text = $"{view.Frame.Width}";
  379. _hText.Text = $"{view.Frame.Height}";
  380. }
  381. void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  382. List<Type> GetAllViewClassesCollection ()
  383. {
  384. List<Type> types = new ();
  385. foreach (Type type in typeof (View).Assembly.GetTypes ()
  386. .Where (
  387. myType =>
  388. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  389. ))
  390. {
  391. types.Add (type);
  392. }
  393. return types;
  394. }
  395. View CreateClass (Type type)
  396. {
  397. // If we are to create a generic Type
  398. if (type.IsGenericType)
  399. {
  400. // For each of the <T> arguments
  401. List<Type> typeArguments = new ();
  402. // use <object>
  403. foreach (Type arg in type.GetGenericArguments ())
  404. {
  405. typeArguments.Add (typeof (object));
  406. }
  407. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  408. type = type.MakeGenericType (typeArguments.ToArray ());
  409. }
  410. // Instantiate view
  411. var view = (View)Activator.CreateInstance (type);
  412. //_curView.X = Pos.Center ();
  413. //_curView.Y = Pos.Center ();
  414. if (!view.AutoSize)
  415. {
  416. view.Width = Dim.Percent (75);
  417. view.Height = Dim.Percent (75);
  418. }
  419. // Set the colorscheme to make it stand out if is null by default
  420. if (view.ColorScheme == null)
  421. {
  422. view.ColorScheme = Colors.ColorSchemes ["Base"];
  423. }
  424. // If the view supports a Text property, set it so we have something to look at
  425. if (view.GetType ().GetProperty ("Text") != null)
  426. {
  427. try
  428. {
  429. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { "Test Text" });
  430. }
  431. catch (TargetInvocationException e)
  432. {
  433. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  434. view = null;
  435. }
  436. }
  437. // If the view supports a Title property, set it so we have something to look at
  438. if (view != null && view.GetType ().GetProperty ("Title") != null)
  439. {
  440. if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
  441. {
  442. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  443. }
  444. else
  445. {
  446. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  447. }
  448. }
  449. // If the view supports a Source property, set it so we have something to look at
  450. if (view != null
  451. && view.GetType ().GetProperty ("Source") != null
  452. && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  453. {
  454. var source = new ListWrapper (new List<string> { "Test Text #1", "Test Text #2", "Test Text #3" });
  455. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  456. }
  457. // Set Settings
  458. _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;
  459. // Add
  460. _hostPane.Add (view);
  461. //DimPosChanged ();
  462. _hostPane.LayoutSubviews ();
  463. _hostPane.Clear ();
  464. _hostPane.SetNeedsDisplay ();
  465. UpdateSettings (view);
  466. UpdateTitle (view);
  467. view.LayoutComplete += LayoutCompleteHandler;
  468. return view;
  469. }
  470. void LayoutCompleteHandler (object sender, LayoutEventArgs args) { UpdateTitle (_curView); }
  471. }
  472. [Fact]
  473. public void Run_Generic ()
  474. {
  475. List<Scenario> scenarios = Scenario.GetScenarios ();
  476. Assert.NotEmpty (scenarios);
  477. int item = scenarios.FindIndex (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
  478. Scenario generic = scenarios [item];
  479. Application.Init (new FakeDriver ());
  480. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  481. // by adding this Space it seems to work.
  482. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  483. var ms = 100;
  484. var abortCount = 0;
  485. Func<bool> abortCallback = () =>
  486. {
  487. abortCount++;
  488. _output.WriteLine ($"'Generic' abortCount {abortCount}");
  489. Application.RequestStop ();
  490. return false;
  491. };
  492. var iterations = 0;
  493. object token = null;
  494. Application.Iteration += (s, a) =>
  495. {
  496. if (token == null)
  497. {
  498. // Timeout only must start at first iteration
  499. token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
  500. }
  501. iterations++;
  502. _output.WriteLine ($"'Generic' iteration {iterations}");
  503. // Stop if we run out of control...
  504. if (iterations == 10)
  505. {
  506. _output.WriteLine ("'Generic' had to be force quit!");
  507. Application.RequestStop ();
  508. }
  509. };
  510. Application.Top.KeyDown += (sender, args) =>
  511. {
  512. // See #2474 for why this is commented out
  513. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, args.KeyCode);
  514. };
  515. generic.Init ();
  516. generic.Setup ();
  517. generic.Run ();
  518. Assert.Equal (0, abortCount);
  519. // # of key up events should match # of iterations
  520. Assert.Equal (1, iterations);
  521. generic.Dispose ();
  522. // Shutdown must be called to safely clean up Application if Init has been called
  523. Application.Shutdown ();
  524. #if DEBUG_IDISPOSABLE
  525. Assert.Empty (Responder.Instances);
  526. #endif
  527. }
  528. private int CreateInput (string input)
  529. {
  530. FakeConsole.MockKeyPresses.Clear ();
  531. // Put a QuitKey in at the end
  532. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  533. foreach (char c in input.Reverse ())
  534. {
  535. var key = KeyCode.Null;
  536. if (char.IsLetter (c))
  537. {
  538. key = (KeyCode)char.ToUpper (c) | (char.IsUpper (c) ? KeyCode.ShiftMask : 0);
  539. }
  540. else
  541. {
  542. key = (KeyCode)c;
  543. }
  544. FakeConsole.PushMockKeyPress (key);
  545. }
  546. return FakeConsole.MockKeyPresses.Count;
  547. }
  548. }