ScenarioTests.cs 23 KB

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