ScenarioTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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.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 = new Toplevel ();
  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.Bounds);
  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 (Top);
  293. Assert.Equal (_viewClasses.Count, iterations);
  294. Top.Dispose ();
  295. Application.Shutdown ();
  296. void DimPosChanged (View view)
  297. {
  298. if (view == null)
  299. {
  300. return;
  301. }
  302. LayoutStyle layout = view.LayoutStyle;
  303. try
  304. {
  305. //view.LayoutStyle = LayoutStyle.Absolute;
  306. switch (_xRadioGroup.SelectedItem)
  307. {
  308. case 0:
  309. view.X = Pos.Percent (_xVal);
  310. break;
  311. case 1:
  312. view.X = Pos.AnchorEnd (_xVal);
  313. break;
  314. case 2:
  315. view.X = Pos.Center ();
  316. break;
  317. case 3:
  318. view.X = Pos.At (_xVal);
  319. break;
  320. }
  321. switch (_yRadioGroup.SelectedItem)
  322. {
  323. case 0:
  324. view.Y = Pos.Percent (_yVal);
  325. break;
  326. case 1:
  327. view.Y = Pos.AnchorEnd (_yVal);
  328. break;
  329. case 2:
  330. view.Y = Pos.Center ();
  331. break;
  332. case 3:
  333. view.Y = Pos.At (_yVal);
  334. break;
  335. }
  336. switch (_wRadioGroup.SelectedItem)
  337. {
  338. case 0:
  339. view.Width = Dim.Percent (_wVal);
  340. break;
  341. case 1:
  342. view.Width = Dim.Fill (_wVal);
  343. break;
  344. case 2:
  345. view.Width = Dim.Sized (_wVal);
  346. break;
  347. }
  348. switch (_hRadioGroup.SelectedItem)
  349. {
  350. case 0:
  351. view.Height = Dim.Percent (_hVal);
  352. break;
  353. case 1:
  354. view.Height = Dim.Fill (_hVal);
  355. break;
  356. case 2:
  357. view.Height = Dim.Sized (_hVal);
  358. break;
  359. }
  360. }
  361. catch (Exception e)
  362. {
  363. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  364. }
  365. UpdateTitle (view);
  366. }
  367. void UpdateSettings (View view)
  368. {
  369. var x = view.X.ToString ();
  370. var y = view.Y.ToString ();
  371. _xRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => x.Contains (s)).First ());
  372. _yRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => y.Contains (s)).First ());
  373. _xText.Text = $"{view.Frame.X}";
  374. _yText.Text = $"{view.Frame.Y}";
  375. var w = view.Width.ToString ();
  376. var h = view.Height.ToString ();
  377. _wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => w.Contains (s)).First ());
  378. _hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => h.Contains (s)).First ());
  379. _wText.Text = $"{view.Frame.Width}";
  380. _hText.Text = $"{view.Frame.Height}";
  381. }
  382. void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  383. List<Type> GetAllViewClassesCollection ()
  384. {
  385. List<Type> types = new ();
  386. foreach (Type type in typeof (View).Assembly.GetTypes ()
  387. .Where (
  388. myType =>
  389. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  390. ))
  391. {
  392. types.Add (type);
  393. }
  394. return types;
  395. }
  396. View CreateClass (Type type)
  397. {
  398. // If we are to create a generic Type
  399. if (type.IsGenericType)
  400. {
  401. // For each of the <T> arguments
  402. List<Type> typeArguments = new ();
  403. // use <object>
  404. foreach (Type arg in type.GetGenericArguments ())
  405. {
  406. typeArguments.Add (typeof (object));
  407. }
  408. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  409. type = type.MakeGenericType (typeArguments.ToArray ());
  410. }
  411. // Instantiate view
  412. var view = (View)Activator.CreateInstance (type);
  413. //_curView.X = Pos.Center ();
  414. //_curView.Y = Pos.Center ();
  415. if (!view.AutoSize)
  416. {
  417. view.Width = Dim.Percent (75);
  418. view.Height = Dim.Percent (75);
  419. }
  420. // Set the colorscheme to make it stand out if is null by default
  421. if (view.ColorScheme == null)
  422. {
  423. view.ColorScheme = Colors.ColorSchemes ["Base"];
  424. }
  425. // If the view supports a Text property, set it so we have something to look at
  426. if (view.GetType ().GetProperty ("Text") != null)
  427. {
  428. try
  429. {
  430. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { "Test Text" });
  431. }
  432. catch (TargetInvocationException e)
  433. {
  434. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  435. view = null;
  436. }
  437. }
  438. // If the view supports a Title property, set it so we have something to look at
  439. if (view != null && view.GetType ().GetProperty ("Title") != null)
  440. {
  441. if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
  442. {
  443. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  444. }
  445. else
  446. {
  447. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  448. }
  449. }
  450. // If the view supports a Source property, set it so we have something to look at
  451. if (view != null
  452. && view.GetType ().GetProperty ("Source") != null
  453. && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  454. {
  455. var source = new ListWrapper (new List<string> { "Test Text #1", "Test Text #2", "Test Text #3" });
  456. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  457. }
  458. // Set Settings
  459. _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;
  460. // Add
  461. _hostPane.Add (view);
  462. //DimPosChanged ();
  463. _hostPane.LayoutSubviews ();
  464. _hostPane.Clear ();
  465. _hostPane.SetNeedsDisplay ();
  466. UpdateSettings (view);
  467. UpdateTitle (view);
  468. view.LayoutComplete += LayoutCompleteHandler;
  469. return view;
  470. }
  471. void LayoutCompleteHandler (object sender, LayoutEventArgs args) { UpdateTitle (_curView); }
  472. }
  473. [Fact]
  474. public void Run_Generic ()
  475. {
  476. List<Scenario> scenarios = Scenario.GetScenarios ();
  477. Assert.NotEmpty (scenarios);
  478. int item = scenarios.FindIndex (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
  479. Scenario generic = scenarios [item];
  480. Application.Init (new FakeDriver ());
  481. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  482. // by adding this Space it seems to work.
  483. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  484. var ms = 100;
  485. var abortCount = 0;
  486. Func<bool> abortCallback = () =>
  487. {
  488. abortCount++;
  489. _output.WriteLine ($"'Generic' abortCount {abortCount}");
  490. Application.RequestStop ();
  491. return false;
  492. };
  493. var iterations = 0;
  494. object token = null;
  495. Application.Iteration += (s, a) =>
  496. {
  497. if (token == null)
  498. {
  499. // Timeout only must start at first iteration
  500. token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
  501. }
  502. iterations++;
  503. _output.WriteLine ($"'Generic' iteration {iterations}");
  504. // Stop if we run out of control...
  505. if (iterations == 10)
  506. {
  507. _output.WriteLine ("'Generic' had to be force quit!");
  508. Application.RequestStop ();
  509. }
  510. };
  511. Application.KeyDown += (sender, args) =>
  512. {
  513. // See #2474 for why this is commented out
  514. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, args.KeyCode);
  515. };
  516. generic.Init ();
  517. generic.Setup ();
  518. generic.Run ();
  519. Assert.Equal (0, abortCount);
  520. // # of key up events should match # of iterations
  521. Assert.Equal (1, iterations);
  522. generic.Dispose ();
  523. // Shutdown must be called to safely clean up Application if Init has been called
  524. Application.Shutdown ();
  525. #if DEBUG_IDISPOSABLE
  526. Assert.Empty (Responder.Instances);
  527. #endif
  528. }
  529. private int CreateInput (string input)
  530. {
  531. FakeConsole.MockKeyPresses.Clear ();
  532. // Put a QuitKey in at the end
  533. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  534. foreach (char c in input.Reverse ())
  535. {
  536. var key = KeyCode.Null;
  537. if (char.IsLetter (c))
  538. {
  539. key = (KeyCode)char.ToUpper (c) | (char.IsUpper (c) ? KeyCode.ShiftMask : 0);
  540. }
  541. else
  542. {
  543. key = (KeyCode)c;
  544. }
  545. FakeConsole.PushMockKeyPress (key);
  546. }
  547. return FakeConsole.MockKeyPresses.Count;
  548. }
  549. }