ScenarioTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using System.Collections.ObjectModel;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using System.Runtime.CompilerServices;
  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.EnableDebugIDisposableAsserts = 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. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  32. // If a previous test failed, this will ensure that the Application is in a clean state
  33. Application.ResetState (true);
  34. _output.WriteLine ($"Running Scenario '{scenarioType}'");
  35. var scenario = Activator.CreateInstance (scenarioType) as Scenario;
  36. uint abortTime = 2000;
  37. object? timeout = null;
  38. var initialized = false;
  39. var shutdownGracefully = false;
  40. var iterationCount = 0;
  41. Key quitKey = Application.QuitKey;
  42. Application.InitializedChanged += OnApplicationOnInitializedChanged;
  43. Application.ForceDriver = "FakeDriver";
  44. scenario!.Main ();
  45. scenario.Dispose ();
  46. scenario = null;
  47. Application.ForceDriver = string.Empty;
  48. Application.InitializedChanged -= OnApplicationOnInitializedChanged;
  49. lock (_timeoutLock)
  50. {
  51. if (timeout is { })
  52. {
  53. timeout = null;
  54. }
  55. }
  56. Assert.True (initialized);
  57. Assert.True (shutdownGracefully, $"Scenario Failed to Quit with {quitKey} after {abortTime}ms and {iterationCount} iterations. Force quit.");
  58. #if DEBUG_IDISPOSABLE
  59. Assert.Empty (View.Instances);
  60. #endif
  61. lock (_timeoutLock)
  62. {
  63. _timeoutLock = null;
  64. }
  65. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  66. return;
  67. void OnApplicationOnInitializedChanged (object? s, EventArgs<bool> a)
  68. {
  69. if (a.Value)
  70. {
  71. Application.Iteration += OnApplicationOnIteration;
  72. initialized = true;
  73. lock (_timeoutLock)
  74. {
  75. timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  76. }
  77. }
  78. else
  79. {
  80. Application.Iteration -= OnApplicationOnIteration;
  81. shutdownGracefully = true;
  82. }
  83. _output.WriteLine ($"Initialized == {a.Value}; shutdownGracefully == {shutdownGracefully}.");
  84. }
  85. // If the scenario doesn't close within abortTime ms, this will force it to quit
  86. bool ForceCloseCallback ()
  87. {
  88. lock (_timeoutLock)
  89. {
  90. if (timeout is { })
  91. {
  92. timeout = null;
  93. }
  94. }
  95. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  96. Application.ResetState (true);
  97. return false;
  98. }
  99. void OnApplicationOnIteration (object? s, IterationEventArgs a)
  100. {
  101. iterationCount++;
  102. if (Application.Initialized)
  103. {
  104. // Press QuitKey
  105. quitKey = Application.QuitKey;
  106. _output.WriteLine ($"Attempting to quit with {quitKey} after {iterationCount} iterations.");
  107. Application.RaiseKeyDownEvent (quitKey);
  108. }
  109. }
  110. }
  111. public static IEnumerable<object []> AllScenarioTypes =>
  112. typeof (Scenario).Assembly
  113. .GetTypes ()
  114. .Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
  115. .Select (type => new object [] { type });
  116. [Fact]
  117. public void Run_All_Views_Tester_Scenario ()
  118. {
  119. // Disable any UIConfig settings
  120. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  121. View? curView = null;
  122. // Settings
  123. var xVal = 0;
  124. var yVal = 0;
  125. var wVal = 0;
  126. var hVal = 0;
  127. List<string> posNames = ["Percent", "AnchorEnd", "Center", "Absolute"];
  128. List<string> dimNames = ["Auto", "Percent", "Fill", "Absolute"];
  129. Application.Init (new FakeDriver ());
  130. var top = new Toplevel ();
  131. Dictionary<string, Type> viewClasses = GetAllViewClasses ().ToDictionary (t => t.Name);
  132. Window leftPane = new ()
  133. {
  134. Title = "Classes",
  135. X = 0,
  136. Y = 0,
  137. Width = 15,
  138. Height = Dim.Fill (1), // for status bar
  139. CanFocus = false,
  140. SchemeName = "TopLevel"
  141. };
  142. ListView classListView = new ()
  143. {
  144. X = 0,
  145. Y = 0,
  146. Width = Dim.Fill (),
  147. Height = Dim.Fill (),
  148. AllowsMarking = false,
  149. SchemeName = "TopLevel",
  150. Source = new ListWrapper<string> (new (viewClasses.Keys.ToList ()))
  151. };
  152. leftPane.Add (classListView);
  153. FrameView settingsPane = new ()
  154. {
  155. X = Pos.Right (leftPane),
  156. Y = 0, // for menu
  157. Width = Dim.Fill (),
  158. Height = 10,
  159. CanFocus = false,
  160. SchemeName = "TopLevel",
  161. Title = "Settings"
  162. };
  163. var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "Absolute(x)" };
  164. FrameView locationFrame = new ()
  165. {
  166. X = 0,
  167. Y = 0,
  168. Height = 3 + radioItems.Length,
  169. Width = 36,
  170. Title = "Location (Pos)"
  171. };
  172. settingsPane.Add (locationFrame);
  173. var label = new Label { X = 0, Y = 0, Text = "x:" };
  174. locationFrame.Add (label);
  175. RadioGroup xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  176. TextField xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{xVal}" };
  177. locationFrame.Add (xText);
  178. locationFrame.Add (xRadioGroup);
  179. radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "Absolute(y)" };
  180. label = new () { X = Pos.Right (xRadioGroup) + 1, Y = 0, Text = "y:" };
  181. locationFrame.Add (label);
  182. TextField yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{yVal}" };
  183. locationFrame.Add (yText);
  184. RadioGroup yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  185. locationFrame.Add (yRadioGroup);
  186. FrameView sizeFrame = new ()
  187. {
  188. X = Pos.Right (locationFrame),
  189. Y = Pos.Y (locationFrame),
  190. Height = 3 + radioItems.Length,
  191. Width = 40,
  192. Title = "Size (Dim)"
  193. };
  194. radioItems = new [] { "Auto()", "Percent(width)", "Fill(width)", "Absolute(width)" };
  195. label = new () { X = 0, Y = 0, Text = "width:" };
  196. sizeFrame.Add (label);
  197. RadioGroup wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  198. TextField wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{wVal}" };
  199. sizeFrame.Add (wText);
  200. sizeFrame.Add (wRadioGroup);
  201. radioItems = new [] { "Auto()", "Percent(height)", "Fill(height)", "Absolute(height)" };
  202. label = new () { X = Pos.Right (wRadioGroup) + 1, Y = 0, Text = "height:" };
  203. sizeFrame.Add (label);
  204. TextField hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{hVal}" };
  205. sizeFrame.Add (hText);
  206. RadioGroup hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  207. sizeFrame.Add (hRadioGroup);
  208. settingsPane.Add (sizeFrame);
  209. FrameView hostPane = new ()
  210. {
  211. X = Pos.Right (leftPane),
  212. Y = Pos.Bottom (settingsPane),
  213. Width = Dim.Fill (),
  214. Height = Dim.Fill (1), // + 1 for status bar
  215. SchemeName = "Dialog"
  216. };
  217. classListView.OpenSelectedItem += (s, a) => { settingsPane.SetFocus (); };
  218. classListView.SelectedItemChanged += (s, args) =>
  219. {
  220. // Remove existing class, if any
  221. if (curView is { })
  222. {
  223. curView.SubViewsLaidOut -= LayoutCompleteHandler;
  224. hostPane.Remove (curView);
  225. curView.Dispose ();
  226. curView = null;
  227. hostPane.FillRect (hostPane.Viewport);
  228. }
  229. curView = CreateClass (viewClasses.Values.ToArray () [classListView.SelectedItem]);
  230. };
  231. xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  232. xText.TextChanged += (s, args) =>
  233. {
  234. try
  235. {
  236. xVal = int.Parse (xText.Text);
  237. DimPosChanged (curView);
  238. }
  239. catch
  240. { }
  241. };
  242. yText.TextChanged += (s, e) =>
  243. {
  244. try
  245. {
  246. yVal = int.Parse (yText.Text);
  247. DimPosChanged (curView);
  248. }
  249. catch
  250. { }
  251. };
  252. yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  253. wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  254. wText.TextChanged += (s, args) =>
  255. {
  256. try
  257. {
  258. wVal = int.Parse (wText.Text);
  259. DimPosChanged (curView);
  260. }
  261. catch
  262. { }
  263. };
  264. hText.TextChanged += (s, args) =>
  265. {
  266. try
  267. {
  268. hVal = int.Parse (hText.Text);
  269. DimPosChanged (curView);
  270. }
  271. catch
  272. { }
  273. };
  274. hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (curView);
  275. top.Add (leftPane, settingsPane, hostPane);
  276. top.LayoutSubViews ();
  277. curView = CreateClass (viewClasses.First ().Value);
  278. var iterations = 0;
  279. Application.Iteration += (s, a) =>
  280. {
  281. iterations++;
  282. if (iterations < viewClasses.Count)
  283. {
  284. classListView.MoveDown ();
  285. if (curView is { })
  286. {
  287. Assert.Equal (
  288. curView.GetType ().Name,
  289. viewClasses.Values.ToArray () [classListView.SelectedItem].Name
  290. );
  291. }
  292. }
  293. else
  294. {
  295. Application.RequestStop ();
  296. }
  297. };
  298. Application.Run (top);
  299. Assert.Equal (viewClasses.Count, iterations);
  300. top.Dispose ();
  301. Application.Shutdown ();
  302. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  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> or the original type if applicable
  404. foreach (Type arg in type.GetGenericArguments ())
  405. {
  406. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  407. {
  408. typeArguments.Add (arg);
  409. }
  410. else
  411. {
  412. typeArguments.Add (typeof (object));
  413. }
  414. }
  415. // Ensure the type does not contain any generic parameters
  416. if (type.ContainsGenericParameters)
  417. {
  418. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  419. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  420. return null;
  421. }
  422. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  423. type = type.MakeGenericType (typeArguments.ToArray ());
  424. }
  425. // Instantiate view
  426. var view = Activator.CreateInstance (type) as View;
  427. if (view is null)
  428. {
  429. return null;
  430. }
  431. if (view.Width is not DimAuto)
  432. {
  433. view.Width = Dim.Percent (75);
  434. }
  435. if (view.Height is not DimAuto)
  436. {
  437. view.Height = Dim.Percent (75);
  438. }
  439. // Set the colorscheme to make it stand out if is null by default
  440. if (!view.HasScheme)
  441. {
  442. view.SchemeName = "Base";
  443. }
  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.ClearViewport ();
  482. hostPane.SetNeedsDraw ();
  483. UpdateSettings (view!);
  484. UpdateTitle (view);
  485. view!.SubViewsLaidOut += LayoutCompleteHandler;
  486. return view;
  487. }
  488. void LayoutCompleteHandler (object? sender, LayoutEventArgs args) { UpdateTitle (curView); }
  489. }
  490. [Fact]
  491. public void Run_Generic ()
  492. {
  493. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  494. Assert.Equal (Key.Esc, Application.QuitKey);
  495. ObservableCollection<Scenario> scenarios = Scenario.GetScenarios ();
  496. Assert.NotEmpty (scenarios);
  497. int item = scenarios.IndexOf (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
  498. Scenario generic = scenarios [item];
  499. Application.Init (new FakeDriver ());
  500. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  501. // by adding this Space it seems to work.
  502. Assert.Equal (Key.Esc, Application.QuitKey);
  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.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) => { Assert.Equal (Application.QuitKey, args); };
  532. generic.Main ();
  533. Assert.Equal (0, abortCount);
  534. // # of key up events should match # of iterations
  535. Assert.Equal (1, iterations);
  536. generic.Dispose ();
  537. // Shutdown must be called to safely clean up Application if Init has been called
  538. Application.Shutdown ();
  539. ConfigurationManager.Disable (resetToHardCodedDefaults: true);
  540. #if DEBUG_IDISPOSABLE
  541. Assert.Empty (View.Instances);
  542. #endif
  543. }
  544. }