ScenarioTests.cs 22 KB

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