ScenarioTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. // If a previous test failed, this will ensure that the Application is in a clean state
  28. Application.ResetState (true);
  29. _output.WriteLine ($"Running Scenario '{scenarioType}'");
  30. var scenario = (Scenario)Activator.CreateInstance (scenarioType);
  31. uint abortTime = 1500;
  32. var initialized = false;
  33. var shutdown = false;
  34. object timeout = null;
  35. Application.InitializedChanged += OnApplicationOnInitializedChanged;
  36. Application.ForceDriver = "FakeDriver";
  37. scenario.Main ();
  38. scenario.Dispose ();
  39. scenario = null;
  40. Application.ForceDriver = string.Empty;
  41. Application.InitializedChanged -= OnApplicationOnInitializedChanged;
  42. lock (_timeoutLock)
  43. {
  44. if (timeout is { })
  45. {
  46. timeout = null;
  47. }
  48. }
  49. Assert.True (initialized);
  50. Assert.True (shutdown);
  51. #if DEBUG_IDISPOSABLE
  52. Assert.Empty (Responder.Instances);
  53. #endif
  54. lock (_timeoutLock)
  55. {
  56. _timeoutLock = null;
  57. }
  58. return;
  59. void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
  60. {
  61. if (a.CurrentValue)
  62. {
  63. Application.Iteration += OnApplicationOnIteration;
  64. initialized = true;
  65. lock (_timeoutLock)
  66. {
  67. timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  68. }
  69. _output.WriteLine ($"Initialized '{Application.Driver}'");
  70. }
  71. else
  72. {
  73. Application.Iteration -= OnApplicationOnIteration;
  74. shutdown = true;
  75. }
  76. }
  77. // If the scenario doesn't close within 500ms, this will force it to quit
  78. bool ForceCloseCallback ()
  79. {
  80. lock (_timeoutLock)
  81. {
  82. if (timeout is { })
  83. {
  84. timeout = null;
  85. }
  86. }
  87. Assert.Fail (
  88. $"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey} after {abortTime}ms. Force quit.");
  89. Application.ResetState (true);
  90. return false;
  91. }
  92. void OnApplicationOnIteration (object s, IterationEventArgs a)
  93. {
  94. if (Application.IsInitialized)
  95. {
  96. // Press QuitKey
  97. //_output.WriteLine ($"Forcing Quit with {Application.QuitKey}");
  98. Application.OnKeyDown (Application.QuitKey);
  99. }
  100. }
  101. }
  102. public static IEnumerable<object []> AllScenarioTypes =>
  103. typeof (Scenario).Assembly
  104. .GetTypes ()
  105. .Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
  106. .Select (type => new object [] { type });
  107. [Fact]
  108. public void Run_All_Views_Tester_Scenario ()
  109. {
  110. Window _leftPane;
  111. ListView _classListView;
  112. FrameView _hostPane;
  113. Dictionary<string, Type> _viewClasses;
  114. View _curView = null;
  115. // Settings
  116. FrameView _settingsPane;
  117. FrameView _locationFrame;
  118. RadioGroup _xRadioGroup;
  119. TextField _xText;
  120. var _xVal = 0;
  121. RadioGroup _yRadioGroup;
  122. TextField _yText;
  123. var _yVal = 0;
  124. FrameView _sizeFrame;
  125. RadioGroup _wRadioGroup;
  126. TextField _wText;
  127. var _wVal = 0;
  128. RadioGroup _hRadioGroup;
  129. TextField _hText;
  130. var _hVal = 0;
  131. List<string> posNames = new () { "Percent", "AnchorEnd", "Center", "Absolute" };
  132. List<string> dimNames = new () { "Auto", "Percent", "Fill", "Absolute" };
  133. Application.Init (new FakeDriver ());
  134. var top = new Toplevel ();
  135. _viewClasses = TestHelpers.GetAllViewClasses ().ToDictionary (t => t.Name);
  136. _leftPane = new ()
  137. {
  138. Title = "Classes",
  139. X = 0,
  140. Y = 0,
  141. Width = 15,
  142. Height = Dim.Fill (1), // for status bar
  143. CanFocus = false,
  144. ColorScheme = Colors.ColorSchemes ["TopLevel"]
  145. };
  146. _classListView = new ()
  147. {
  148. X = 0,
  149. Y = 0,
  150. Width = Dim.Fill (),
  151. Height = Dim.Fill (),
  152. AllowsMarking = false,
  153. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  154. Source = new ListWrapper<string> (new (_viewClasses.Keys.ToList ()))
  155. };
  156. _leftPane.Add (_classListView);
  157. _settingsPane = new ()
  158. {
  159. X = Pos.Right (_leftPane),
  160. Y = 0, // for menu
  161. Width = Dim.Fill (),
  162. Height = 10,
  163. CanFocus = false,
  164. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  165. Title = "Settings"
  166. };
  167. var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "Absolute(x)" };
  168. _locationFrame = new ()
  169. {
  170. X = 0,
  171. Y = 0,
  172. Height = 3 + radioItems.Length,
  173. Width = 36,
  174. Title = "Location (Pos)"
  175. };
  176. _settingsPane.Add (_locationFrame);
  177. var label = new Label { X = 0, Y = 0, Text = "x:" };
  178. _locationFrame.Add (label);
  179. _xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  180. _xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  181. _locationFrame.Add (_xText);
  182. _locationFrame.Add (_xRadioGroup);
  183. radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "Absolute(y)" };
  184. label = new () { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "y:" };
  185. _locationFrame.Add (label);
  186. _yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  187. _locationFrame.Add (_yText);
  188. _yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  189. _locationFrame.Add (_yRadioGroup);
  190. _sizeFrame = new ()
  191. {
  192. X = Pos.Right (_locationFrame),
  193. Y = Pos.Y (_locationFrame),
  194. Height = 3 + radioItems.Length,
  195. Width = 40,
  196. Title = "Size (Dim)"
  197. };
  198. radioItems = new [] { "Auto()", "Percent(width)", "Fill(width)", "Absolute(width)" };
  199. label = new () { X = 0, Y = 0, Text = "width:" };
  200. _sizeFrame.Add (label);
  201. _wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  202. _wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  203. _sizeFrame.Add (_wText);
  204. _sizeFrame.Add (_wRadioGroup);
  205. radioItems = new [] { "Auto()", "Percent(height)", "Fill(height)", "Absolute(height)" };
  206. label = new () { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "height:" };
  207. _sizeFrame.Add (label);
  208. _hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  209. _sizeFrame.Add (_hText);
  210. _hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  211. _sizeFrame.Add (_hRadioGroup);
  212. _settingsPane.Add (_sizeFrame);
  213. _hostPane = new ()
  214. {
  215. X = Pos.Right (_leftPane),
  216. Y = Pos.Bottom (_settingsPane),
  217. Width = Dim.Fill (),
  218. Height = Dim.Fill (1), // + 1 for status bar
  219. ColorScheme = Colors.ColorSchemes ["Dialog"]
  220. };
  221. _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
  222. _classListView.SelectedItemChanged += (s, args) =>
  223. {
  224. // Remove existing class, if any
  225. if (_curView != null)
  226. {
  227. _curView.LayoutComplete -= LayoutCompleteHandler;
  228. _hostPane.Remove (_curView);
  229. _curView.Dispose ();
  230. _curView = null;
  231. _hostPane.FillRect (_hostPane.Viewport);
  232. }
  233. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  234. };
  235. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  236. _xText.TextChanged += (s, args) =>
  237. {
  238. try
  239. {
  240. _xVal = int.Parse (_xText.Text);
  241. DimPosChanged (_curView);
  242. }
  243. catch
  244. { }
  245. };
  246. _yText.TextChanged += (s, e) =>
  247. {
  248. try
  249. {
  250. _yVal = int.Parse (_yText.Text);
  251. DimPosChanged (_curView);
  252. }
  253. catch
  254. { }
  255. };
  256. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  257. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  258. _wText.TextChanged += (s, args) =>
  259. {
  260. try
  261. {
  262. _wVal = int.Parse (_wText.Text);
  263. DimPosChanged (_curView);
  264. }
  265. catch
  266. { }
  267. };
  268. _hText.TextChanged += (s, args) =>
  269. {
  270. try
  271. {
  272. _hVal = int.Parse (_hText.Text);
  273. DimPosChanged (_curView);
  274. }
  275. catch
  276. { }
  277. };
  278. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  279. top.Add (_leftPane, _settingsPane, _hostPane);
  280. top.LayoutSubviews ();
  281. _curView = CreateClass (_viewClasses.First ().Value);
  282. var iterations = 0;
  283. Application.Iteration += (s, a) =>
  284. {
  285. iterations++;
  286. if (iterations < _viewClasses.Count)
  287. {
  288. _classListView.MoveDown ();
  289. Assert.Equal (
  290. _curView.GetType ().Name,
  291. _viewClasses.Values.ToArray () [_classListView.SelectedItem].Name
  292. );
  293. }
  294. else
  295. {
  296. Application.RequestStop ();
  297. }
  298. };
  299. Application.Run (top);
  300. Assert.Equal (_viewClasses.Count, iterations);
  301. top.Dispose ();
  302. Application.Shutdown ();
  303. void DimPosChanged (View view)
  304. {
  305. if (view == null)
  306. {
  307. return;
  308. }
  309. try
  310. {
  311. switch (_xRadioGroup.SelectedItem)
  312. {
  313. case 0:
  314. view.X = Pos.Percent (_xVal);
  315. break;
  316. case 1:
  317. view.X = Pos.AnchorEnd (_xVal);
  318. break;
  319. case 2:
  320. view.X = Pos.Center ();
  321. break;
  322. case 3:
  323. view.X = Pos.Absolute (_xVal);
  324. break;
  325. }
  326. switch (_yRadioGroup.SelectedItem)
  327. {
  328. case 0:
  329. view.Y = Pos.Percent (_yVal);
  330. break;
  331. case 1:
  332. view.Y = Pos.AnchorEnd (_yVal);
  333. break;
  334. case 2:
  335. view.Y = Pos.Center ();
  336. break;
  337. case 3:
  338. view.Y = Pos.Absolute (_yVal);
  339. break;
  340. }
  341. switch (_wRadioGroup.SelectedItem)
  342. {
  343. case 0:
  344. view.Width = Dim.Percent (_wVal);
  345. break;
  346. case 1:
  347. view.Width = Dim.Fill (_wVal);
  348. break;
  349. case 2:
  350. view.Width = Dim.Absolute (_wVal);
  351. break;
  352. }
  353. switch (_hRadioGroup.SelectedItem)
  354. {
  355. case 0:
  356. view.Height = Dim.Percent (_hVal);
  357. break;
  358. case 1:
  359. view.Height = Dim.Fill (_hVal);
  360. break;
  361. case 2:
  362. view.Height = Dim.Absolute (_hVal);
  363. break;
  364. }
  365. }
  366. catch (Exception e)
  367. {
  368. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  369. }
  370. UpdateTitle (view);
  371. }
  372. void UpdateSettings (View view)
  373. {
  374. var x = view.X.ToString ();
  375. var y = view.Y.ToString ();
  376. try
  377. {
  378. _xRadioGroup.SelectedItem = posNames.IndexOf (posNames.First (s => x.Contains (s)));
  379. _yRadioGroup.SelectedItem = posNames.IndexOf (posNames.First (s => y.Contains (s)));
  380. }
  381. catch (InvalidOperationException e)
  382. {
  383. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  384. Debug.WriteLine ($"{e}");
  385. }
  386. _xText.Text = $"{view.Frame.X}";
  387. _yText.Text = $"{view.Frame.Y}";
  388. var w = view.Width.ToString ();
  389. var h = view.Height.ToString ();
  390. _wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.First (s => w.Contains (s)));
  391. _hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.First (s => h.Contains (s)));
  392. _wText.Text = $"{view.Frame.Width}";
  393. _hText.Text = $"{view.Frame.Height}";
  394. }
  395. void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  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. if (view is null)
  414. {
  415. return null;
  416. }
  417. if (view.Width is not DimAuto)
  418. {
  419. view.Width = Dim.Percent (75);
  420. }
  421. if (view.Height is not DimAuto)
  422. {
  423. view.Height = Dim.Percent (75);
  424. }
  425. // Set the colorscheme to make it stand out if is null by default
  426. view.ColorScheme ??= Colors.ColorSchemes ["Base"];
  427. // If the view supports a Text property, set it so we have something to look at
  428. if (view.GetType ().GetProperty ("Text") != null)
  429. {
  430. try
  431. {
  432. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { "Test Text" });
  433. }
  434. catch (TargetInvocationException e)
  435. {
  436. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  437. view = null;
  438. }
  439. }
  440. // If the view supports a Title property, set it so we have something to look at
  441. if (view != null && view.GetType ().GetProperty ("Title") != null)
  442. {
  443. if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
  444. {
  445. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  446. }
  447. else
  448. {
  449. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  450. }
  451. }
  452. // If the view supports a Source property, set it so we have something to look at
  453. if (view != null
  454. && view.GetType ().GetProperty ("Source") != null
  455. && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  456. {
  457. ListWrapper<string> source = new (["Test Text #1", "Test Text #2", "Test Text #3"]);
  458. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  459. }
  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. ObservableCollection<Scenario> scenarios = Scenario.GetScenarios ();
  477. Assert.NotEmpty (scenarios);
  478. int item = scenarios.IndexOf (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. Assert.Equal (Application.QuitKey, args.KeyCode);
  514. };
  515. generic.Main ();
  516. Assert.Equal (0, abortCount);
  517. // # of key up events should match # of iterations
  518. Assert.Equal (1, iterations);
  519. generic.Dispose ();
  520. // Shutdown must be called to safely clean up Application if Init has been called
  521. Application.Shutdown ();
  522. #if DEBUG_IDISPOSABLE
  523. Assert.Empty (Responder.Instances);
  524. #endif
  525. }
  526. private int CreateInput (string input)
  527. {
  528. FakeConsole.MockKeyPresses.Clear ();
  529. // Put a QuitKey in at the end
  530. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  531. foreach (char c in input.Reverse ())
  532. {
  533. var key = KeyCode.Null;
  534. if (char.IsLetter (c))
  535. {
  536. key = (KeyCode)char.ToUpper (c) | (char.IsUpper (c) ? KeyCode.ShiftMask : 0);
  537. }
  538. else
  539. {
  540. key = (KeyCode)c;
  541. }
  542. FakeConsole.PushMockKeyPress (key);
  543. }
  544. return FakeConsole.MockKeyPresses.Count;
  545. }
  546. }