ScenarioTests.cs 22 KB

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