ScenarioTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. using System.Collections.ObjectModel;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using Terminal.Gui;
  5. using UICatalog;
  6. using UnitTests;
  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 = Activator.CreateInstance (scenarioType) as Scenario;
  38. uint abortTime = 1500;
  39. object? timeout = null;
  40. var initialized = false;
  41. var shutdown = false;
  42. var 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 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. View? curView = null;
  129. // Settings
  130. var xVal = 0;
  131. var yVal = 0;
  132. var wVal = 0;
  133. var hVal = 0;
  134. List<string> posNames = ["Percent", "AnchorEnd", "Center", "Absolute"];
  135. List<string> dimNames = ["Auto", "Percent", "Fill", "Absolute"];
  136. Application.Init (new FakeDriver ());
  137. var top = new Toplevel ();
  138. Dictionary<string, Type> viewClasses = GetAllViewClasses().ToDictionary (t => t.Name);
  139. Window leftPane = new ()
  140. {
  141. Title = "Classes",
  142. X = 0,
  143. Y = 0,
  144. Width = 15,
  145. Height = Dim.Fill (1), // for status bar
  146. CanFocus = false,
  147. ColorScheme = Colors.ColorSchemes ["TopLevel"]
  148. };
  149. ListView classListView = new ()
  150. {
  151. X = 0,
  152. Y = 0,
  153. Width = Dim.Fill (),
  154. Height = Dim.Fill (),
  155. AllowsMarking = false,
  156. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  157. Source = new ListWrapper<string> (new (viewClasses.Keys.ToList ()))
  158. };
  159. leftPane.Add (classListView);
  160. FrameView settingsPane = new ()
  161. {
  162. X = Pos.Right (leftPane),
  163. Y = 0, // for menu
  164. Width = Dim.Fill (),
  165. Height = 10,
  166. CanFocus = false,
  167. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  168. Title = "Settings"
  169. };
  170. var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "Absolute(x)" };
  171. FrameView locationFrame = new ()
  172. {
  173. X = 0,
  174. Y = 0,
  175. Height = 3 + radioItems.Length,
  176. Width = 36,
  177. Title = "Location (Pos)"
  178. };
  179. settingsPane.Add (locationFrame);
  180. var label = new Label { X = 0, Y = 0, Text = "x:" };
  181. locationFrame.Add (label);
  182. RadioGroup xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  183. TextField xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{xVal}" };
  184. locationFrame.Add (xText);
  185. locationFrame.Add (xRadioGroup);
  186. radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "Absolute(y)" };
  187. label = new () { X = Pos.Right (xRadioGroup) + 1, Y = 0, Text = "y:" };
  188. locationFrame.Add (label);
  189. TextField yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{yVal}" };
  190. locationFrame.Add (yText);
  191. RadioGroup yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  192. locationFrame.Add (yRadioGroup);
  193. FrameView sizeFrame = new ()
  194. {
  195. X = Pos.Right (locationFrame),
  196. Y = Pos.Y (locationFrame),
  197. Height = 3 + radioItems.Length,
  198. Width = 40,
  199. Title = "Size (Dim)"
  200. };
  201. radioItems = new [] { "Auto()", "Percent(width)", "Fill(width)", "Absolute(width)" };
  202. label = new () { X = 0, Y = 0, Text = "width:" };
  203. sizeFrame.Add (label);
  204. RadioGroup wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  205. TextField wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{wVal}" };
  206. sizeFrame.Add (wText);
  207. sizeFrame.Add (wRadioGroup);
  208. radioItems = new [] { "Auto()", "Percent(height)", "Fill(height)", "Absolute(height)" };
  209. label = new () { X = Pos.Right (wRadioGroup) + 1, Y = 0, Text = "height:" };
  210. sizeFrame.Add (label);
  211. TextField hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{hVal}" };
  212. sizeFrame.Add (hText);
  213. RadioGroup hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  214. sizeFrame.Add (hRadioGroup);
  215. settingsPane.Add (sizeFrame);
  216. FrameView hostPane = new ()
  217. {
  218. X = Pos.Right (leftPane),
  219. Y = Pos.Bottom (settingsPane),
  220. Width = Dim.Fill (),
  221. Height = Dim.Fill (1), // + 1 for status bar
  222. ColorScheme = Colors.ColorSchemes ["Dialog"]
  223. };
  224. classListView.OpenSelectedItem += (s, a) => { settingsPane.SetFocus (); };
  225. classListView.SelectedItemChanged += (s, args) =>
  226. {
  227. // Remove existing class, if any
  228. if (curView is {})
  229. {
  230. curView.SubViewsLaidOut -= LayoutCompleteHandler;
  231. hostPane.Remove (curView);
  232. curView.Dispose ();
  233. curView = null;
  234. hostPane.FillRect (hostPane.Viewport);
  235. }
  236. curView = CreateClass (viewClasses.Values.ToArray () [classListView.SelectedItem]);
  237. };
  238. xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  239. xText.TextChanged += (s, args) =>
  240. {
  241. try
  242. {
  243. xVal = int.Parse (xText.Text);
  244. DimPosChanged (curView);
  245. }
  246. catch
  247. { }
  248. };
  249. yText.TextChanged += (s, e) =>
  250. {
  251. try
  252. {
  253. yVal = int.Parse (yText.Text);
  254. DimPosChanged (curView);
  255. }
  256. catch
  257. { }
  258. };
  259. yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  260. wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  261. wText.TextChanged += (s, args) =>
  262. {
  263. try
  264. {
  265. wVal = int.Parse (wText.Text);
  266. DimPosChanged (curView);
  267. }
  268. catch
  269. { }
  270. };
  271. hText.TextChanged += (s, args) =>
  272. {
  273. try
  274. {
  275. hVal = int.Parse (hText.Text);
  276. DimPosChanged (curView);
  277. }
  278. catch
  279. { }
  280. };
  281. hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  282. top.Add (leftPane, settingsPane, hostPane);
  283. top.LayoutSubViews ();
  284. curView = CreateClass (viewClasses.First ().Value);
  285. var iterations = 0;
  286. Application.Iteration += (s, a) =>
  287. {
  288. iterations++;
  289. if (iterations < viewClasses.Count)
  290. {
  291. classListView.MoveDown ();
  292. Assert.Equal (
  293. curView!.GetType ().Name,
  294. viewClasses.Values.ToArray () [classListView.SelectedItem].Name
  295. );
  296. }
  297. else
  298. {
  299. Application.RequestStop ();
  300. }
  301. };
  302. Application.Run (top);
  303. Assert.Equal (viewClasses.Count, iterations);
  304. top.Dispose ();
  305. Application.Shutdown ();
  306. // Restore the configuration locations
  307. ConfigurationManager.Locations = savedConfigLocations;
  308. ConfigurationManager.Reset ();
  309. void DimPosChanged (View? view)
  310. {
  311. if (view == null)
  312. {
  313. return;
  314. }
  315. try
  316. {
  317. switch (xRadioGroup.SelectedItem)
  318. {
  319. case 0:
  320. view.X = Pos.Percent (xVal);
  321. break;
  322. case 1:
  323. view.X = Pos.AnchorEnd (xVal);
  324. break;
  325. case 2:
  326. view.X = Pos.Center ();
  327. break;
  328. case 3:
  329. view.X = Pos.Absolute (xVal);
  330. break;
  331. }
  332. switch (yRadioGroup.SelectedItem)
  333. {
  334. case 0:
  335. view.Y = Pos.Percent (yVal);
  336. break;
  337. case 1:
  338. view.Y = Pos.AnchorEnd (yVal);
  339. break;
  340. case 2:
  341. view.Y = Pos.Center ();
  342. break;
  343. case 3:
  344. view.Y = Pos.Absolute (yVal);
  345. break;
  346. }
  347. switch (wRadioGroup.SelectedItem)
  348. {
  349. case 0:
  350. view.Width = Dim.Percent (wVal);
  351. break;
  352. case 1:
  353. view.Width = Dim.Fill (wVal);
  354. break;
  355. case 2:
  356. view.Width = Dim.Absolute (wVal);
  357. break;
  358. }
  359. switch (hRadioGroup.SelectedItem)
  360. {
  361. case 0:
  362. view.Height = Dim.Percent (hVal);
  363. break;
  364. case 1:
  365. view.Height = Dim.Fill (hVal);
  366. break;
  367. case 2:
  368. view.Height = Dim.Absolute (hVal);
  369. break;
  370. }
  371. }
  372. catch (Exception e)
  373. {
  374. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  375. }
  376. UpdateTitle (view);
  377. }
  378. void UpdateSettings (View view)
  379. {
  380. var x = view.X.ToString ();
  381. var y = view.Y.ToString ();
  382. try
  383. {
  384. xRadioGroup.SelectedItem = posNames.IndexOf (posNames.First (s => x.Contains (s)));
  385. yRadioGroup.SelectedItem = posNames.IndexOf (posNames.First (s => y.Contains (s)));
  386. }
  387. catch (InvalidOperationException e)
  388. {
  389. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  390. Debug.WriteLine ($"{e}");
  391. }
  392. xText.Text = $"{view.Frame.X}";
  393. yText.Text = $"{view.Frame.Y}";
  394. var w = view.Width!.ToString ();
  395. var h = view.Height!.ToString ();
  396. wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.First (s => w.Contains (s)));
  397. hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.First (s => h.Contains (s)));
  398. wText.Text = $"{view.Frame.Width}";
  399. hText.Text = $"{view.Frame.Height}";
  400. }
  401. void UpdateTitle (View? view) { hostPane.Title = $"{view!.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  402. View? CreateClass (Type type)
  403. {
  404. // If we are to create a generic Type
  405. if (type.IsGenericType)
  406. {
  407. // For each of the <T> arguments
  408. List<Type> typeArguments = new ();
  409. // use <object>
  410. foreach (Type arg in type.GetGenericArguments ())
  411. {
  412. typeArguments.Add (typeof (object));
  413. }
  414. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  415. type = type.MakeGenericType (typeArguments.ToArray ());
  416. }
  417. // Instantiate view
  418. var view = Activator.CreateInstance (type) as View;
  419. if (view is null)
  420. {
  421. return null;
  422. }
  423. if (view.Width is not DimAuto)
  424. {
  425. view.Width = Dim.Percent (75);
  426. }
  427. if (view.Height is not DimAuto)
  428. {
  429. view.Height = Dim.Percent (75);
  430. }
  431. // Set the colorscheme to make it stand out if is null by default
  432. view.ColorScheme ??= Colors.ColorSchemes ["Base"];
  433. // If the view supports a Text property, set it so we have something to look at
  434. if (view.GetType ().GetProperty ("Text") != null)
  435. {
  436. try
  437. {
  438. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { "Test Text" });
  439. }
  440. catch (TargetInvocationException e)
  441. {
  442. MessageBox.ErrorQuery ("Exception", e.InnerException!.Message, "Ok");
  443. view = null;
  444. }
  445. }
  446. // If the view supports a Title property, set it so we have something to look at
  447. if (view != null && view.GetType ().GetProperty ("Title") != null)
  448. {
  449. if (view.GetType ().GetProperty ("Title")!.PropertyType == typeof (string))
  450. {
  451. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  452. }
  453. else
  454. {
  455. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  456. }
  457. }
  458. // If the view supports a Source property, set it so we have something to look at
  459. if (view != null
  460. && view.GetType ().GetProperty ("Source") != null
  461. && view.GetType ().GetProperty ("Source")!.PropertyType == typeof (IListDataSource))
  462. {
  463. ListWrapper<string> source = new (["Test Text #1", "Test Text #2", "Test Text #3"]);
  464. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  465. }
  466. // Add
  467. hostPane.Add (view);
  468. //DimPosChanged ();
  469. hostPane.LayoutSubViews ();
  470. hostPane.ClearViewport ();
  471. hostPane.SetNeedsDraw ();
  472. UpdateSettings (view!);
  473. UpdateTitle (view);
  474. view!.SubViewsLaidOut += LayoutCompleteHandler;
  475. return view;
  476. }
  477. void LayoutCompleteHandler (object? sender, LayoutEventArgs args) { UpdateTitle (curView); }
  478. }
  479. [Fact]
  480. public void Run_Generic ()
  481. {
  482. // Disable any UIConfig settings
  483. ConfigLocations savedConfigLocations = ConfigurationManager.Locations;
  484. ConfigurationManager.Locations = ConfigLocations.Default;
  485. ObservableCollection<Scenario> scenarios = Scenario.GetScenarios ();
  486. Assert.NotEmpty (scenarios);
  487. int item = scenarios.IndexOf (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
  488. Scenario generic = scenarios [item];
  489. Application.Init (new FakeDriver ());
  490. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  491. // by adding this Space it seems to work.
  492. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  493. var ms = 100;
  494. var abortCount = 0;
  495. Func<bool> abortCallback = () =>
  496. {
  497. abortCount++;
  498. _output.WriteLine ($"'Generic' abortCount {abortCount}");
  499. Application.RequestStop ();
  500. return false;
  501. };
  502. var iterations = 0;
  503. object? token = null;
  504. Application.Iteration += (s, a) =>
  505. {
  506. if (token == null)
  507. {
  508. // Timeout only must start at first iteration
  509. token = Application.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
  510. }
  511. iterations++;
  512. _output.WriteLine ($"'Generic' iteration {iterations}");
  513. // Stop if we run out of control...
  514. if (iterations == 10)
  515. {
  516. _output.WriteLine ("'Generic' had to be force quit!");
  517. Application.RequestStop ();
  518. }
  519. };
  520. Application.KeyDown += (sender, args) => { Assert.Equal (Application.QuitKey, args.KeyCode); };
  521. generic.Main ();
  522. Assert.Equal (0, abortCount);
  523. // # of key up events should match # of iterations
  524. Assert.Equal (1, iterations);
  525. generic.Dispose ();
  526. // Shutdown must be called to safely clean up Application if Init has been called
  527. Application.Shutdown ();
  528. // Restore the configuration locations
  529. ConfigurationManager.Locations = savedConfigLocations;
  530. ConfigurationManager.Reset ();
  531. #if DEBUG_IDISPOSABLE
  532. Assert.Empty (View.Instances);
  533. #endif
  534. }
  535. }