ScenarioTests.cs 23 KB

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