ScenarioTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. using System.Diagnostics;
  2. using System.Reflection;
  3. using System.Runtime.InteropServices;
  4. using UICatalog;
  5. using UnitTests;
  6. using Xunit.Abstractions;
  7. namespace IntegrationTests.UICatalog;
  8. public class ScenarioTests : TestsAllViews
  9. {
  10. public ScenarioTests (ITestOutputHelper output)
  11. {
  12. #if DEBUG_IDISPOSABLE
  13. View.EnableDebugIDisposableAsserts = true;
  14. View.Instances.Clear ();
  15. #endif
  16. _output = output;
  17. }
  18. private readonly ITestOutputHelper _output;
  19. private object? _timeoutLock;
  20. /// <summary>
  21. /// <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
  22. /// <para>Should find any Scenarios which crash on load or do not respond to <see cref="Application.RequestStop()"/>.</para>
  23. /// </summary>
  24. [Theory]
  25. [MemberData (nameof (AllScenarioTypes))]
  26. public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType)
  27. {
  28. // Disable on Mac due to random failures related to timing issues
  29. if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  30. {
  31. _output.WriteLine ($"Skipping Scenario '{scenarioType}' on macOS due to random timeout failures.");
  32. return;
  33. }
  34. Assert.Null (_timeoutLock);
  35. _timeoutLock = new ();
  36. ConfigurationManager.Disable (true);
  37. // If a previous test failed, this will ensure that the Application is in a clean state
  38. Application.ResetState (true);
  39. _output.WriteLine ($"Running Scenario '{scenarioType}'");
  40. Scenario? scenario = null;
  41. var scenarioName = string.Empty;
  42. object? timeout = null;
  43. var timeoutFired = false;
  44. // Increase timeout for macOS - it's consistently slower
  45. uint abortTime = 5000;
  46. if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  47. {
  48. abortTime = 10000;
  49. }
  50. var initialized = false;
  51. var shutdownGracefully = false;
  52. var iterationCount = 0;
  53. Key quitKey = Application.QuitKey;
  54. // Track if we've already unsubscribed to prevent double-removal
  55. var iterationHandlerRemoved = false;
  56. try
  57. {
  58. scenario = Activator.CreateInstance (scenarioType) as Scenario;
  59. scenarioName = scenario!.GetName ();
  60. Application.InitializedChanged += OnApplicationOnInitializedChanged;
  61. Application.ForceDriver = "FakeDriver";
  62. scenario!.Main ();
  63. Application.ForceDriver = string.Empty;
  64. }
  65. finally
  66. {
  67. // Ensure cleanup happens regardless of how we exit
  68. Application.InitializedChanged -= OnApplicationOnInitializedChanged;
  69. // Remove iteration handler if it wasn't removed
  70. if (!iterationHandlerRemoved)
  71. {
  72. Application.Iteration -= OnApplicationOnIteration;
  73. iterationHandlerRemoved = true;
  74. }
  75. lock (_timeoutLock)
  76. {
  77. if (timeout is { })
  78. {
  79. Application.RemoveTimeout (timeout);
  80. timeout = null;
  81. }
  82. }
  83. scenario?.Dispose ();
  84. scenario = null;
  85. ConfigurationManager.Disable (true);
  86. }
  87. Assert.True (initialized, $"Scenario '{scenarioName}' failed to initialize.");
  88. if (timeoutFired)
  89. {
  90. _output.WriteLine ($"WARNING: Scenario '{scenarioName}' timed out after {abortTime}ms. This may indicate a performance issue on this runner.");
  91. }
  92. Assert.True (
  93. shutdownGracefully,
  94. $"Scenario '{scenarioName}' failed to quit with {quitKey} after {abortTime}ms and {iterationCount} iterations. "
  95. + $"TimeoutFired={timeoutFired}");
  96. #if DEBUG_IDISPOSABLE
  97. Assert.Empty (View.Instances);
  98. #endif
  99. return;
  100. void OnApplicationOnInitializedChanged (object? s, EventArgs<bool> a)
  101. {
  102. if (a.Value)
  103. {
  104. Application.Iteration += OnApplicationOnIteration;
  105. initialized = true;
  106. lock (_timeoutLock)
  107. {
  108. timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  109. }
  110. }
  111. else
  112. {
  113. shutdownGracefully = true;
  114. }
  115. _output.WriteLine ($"Initialized == {a.Value}; shutdownGracefully == {shutdownGracefully}.");
  116. }
  117. // If the scenario doesn't close within abortTime ms, this will force it to quit
  118. bool ForceCloseCallback ()
  119. {
  120. lock (_timeoutLock)
  121. {
  122. timeoutFired = true;
  123. timeout = null;
  124. }
  125. _output.WriteLine ($"TIMEOUT FIRED for {scenarioName} after {abortTime}ms. Attempting graceful shutdown.");
  126. // Don't call ResetState here - let the finally block handle cleanup
  127. // Just try to stop the application gracefully
  128. try
  129. {
  130. if (Application.Initialized)
  131. {
  132. Application.RequestStop ();
  133. }
  134. }
  135. catch (Exception ex)
  136. {
  137. _output.WriteLine ($"Exception during timeout callback: {ex.Message}");
  138. }
  139. return false;
  140. }
  141. void OnApplicationOnIteration (object? s, IterationEventArgs a)
  142. {
  143. iterationCount++;
  144. if (Application.Initialized)
  145. {
  146. // Press QuitKey
  147. quitKey = Application.QuitKey;
  148. _output.WriteLine ($"Attempting to quit with {quitKey} after {iterationCount} iterations.");
  149. try
  150. {
  151. Application.RaiseKeyDownEvent (quitKey);
  152. }
  153. catch (Exception ex)
  154. {
  155. _output.WriteLine ($"Exception raising quit key: {ex.Message}");
  156. }
  157. Application.Iteration -= OnApplicationOnIteration;
  158. iterationHandlerRemoved = true;
  159. }
  160. }
  161. }
  162. public static IEnumerable<object []> AllScenarioTypes =>
  163. typeof (Scenario).Assembly
  164. .GetTypes ()
  165. .Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
  166. .Select (type => new object [] { type });
  167. [Fact]
  168. public void Run_All_Views_Tester_Scenario ()
  169. {
  170. // Disable any UIConfig settings
  171. ConfigurationManager.Disable (true);
  172. View? curView = null;
  173. // Settings
  174. var xVal = 0;
  175. var yVal = 0;
  176. var wVal = 0;
  177. var hVal = 0;
  178. List<string> posNames = ["Percent", "AnchorEnd", "Center", "Absolute"];
  179. List<string> dimNames = ["Auto", "Percent", "Fill", "Absolute"];
  180. Application.Init (null, "fake");
  181. var top = new Toplevel ();
  182. Dictionary<string, Type> viewClasses = GetAllViewClasses ().ToDictionary (t => t.Name);
  183. Window leftPane = new ()
  184. {
  185. Title = "Classes",
  186. X = 0,
  187. Y = 0,
  188. Width = 15,
  189. Height = Dim.Fill (1), // for status bar
  190. CanFocus = false,
  191. SchemeName = "TopLevel"
  192. };
  193. ListView classListView = new ()
  194. {
  195. X = 0,
  196. Y = 0,
  197. Width = Dim.Fill (),
  198. Height = Dim.Fill (),
  199. AllowsMarking = false,
  200. SchemeName = "TopLevel",
  201. Source = new ListWrapper<string> (new (viewClasses.Keys.ToList ()))
  202. };
  203. leftPane.Add (classListView);
  204. FrameView settingsPane = new ()
  205. {
  206. X = Pos.Right (leftPane),
  207. Y = 0, // for menu
  208. Width = Dim.Fill (),
  209. Height = 10,
  210. CanFocus = false,
  211. SchemeName = "TopLevel",
  212. Title = "Settings"
  213. };
  214. var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "Absolute(x)" };
  215. FrameView locationFrame = new ()
  216. {
  217. X = 0,
  218. Y = 0,
  219. Height = 3 + radioItems.Length,
  220. Width = 36,
  221. Title = "Location (Pos)"
  222. };
  223. settingsPane.Add (locationFrame);
  224. var label = new Label { X = 0, Y = 0, Text = "x:" };
  225. locationFrame.Add (label);
  226. OptionSelector xOptionSelector = new () { X = 0, Y = Pos.Bottom (label), Labels = radioItems };
  227. TextField xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{xVal}" };
  228. locationFrame.Add (xText);
  229. locationFrame.Add (xOptionSelector);
  230. radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "Absolute(y)" };
  231. label = new () { X = Pos.Right (xOptionSelector) + 1, Y = 0, Text = "y:" };
  232. locationFrame.Add (label);
  233. TextField yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{yVal}" };
  234. locationFrame.Add (yText);
  235. OptionSelector yOptionSelector = new () { X = Pos.X (label), Y = Pos.Bottom (label), Labels = radioItems };
  236. locationFrame.Add (yOptionSelector);
  237. FrameView sizeFrame = new ()
  238. {
  239. X = Pos.Right (locationFrame),
  240. Y = Pos.Y (locationFrame),
  241. Height = 3 + radioItems.Length,
  242. Width = 40,
  243. Title = "Size (Dim)"
  244. };
  245. radioItems = new [] { "Auto()", "Percent(width)", "Fill(width)", "Absolute(width)" };
  246. label = new () { X = 0, Y = 0, Text = "width:" };
  247. sizeFrame.Add (label);
  248. OptionSelector wOptionSelector = new () { X = 0, Y = Pos.Bottom (label), Labels = radioItems };
  249. TextField wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{wVal}" };
  250. sizeFrame.Add (wText);
  251. sizeFrame.Add (wOptionSelector);
  252. radioItems = new [] { "Auto()", "Percent(height)", "Fill(height)", "Absolute(height)" };
  253. label = new () { X = Pos.Right (wOptionSelector) + 1, Y = 0, Text = "height:" };
  254. sizeFrame.Add (label);
  255. TextField hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{hVal}" };
  256. sizeFrame.Add (hText);
  257. OptionSelector hOptionSelector = new () { X = Pos.X (label), Y = Pos.Bottom (label), Labels = radioItems };
  258. sizeFrame.Add (hOptionSelector);
  259. settingsPane.Add (sizeFrame);
  260. FrameView hostPane = new ()
  261. {
  262. X = Pos.Right (leftPane),
  263. Y = Pos.Bottom (settingsPane),
  264. Width = Dim.Fill (),
  265. Height = Dim.Fill (1), // + 1 for status bar
  266. SchemeName = "Dialog"
  267. };
  268. classListView.OpenSelectedItem += (s, a) => { settingsPane.SetFocus (); };
  269. classListView.SelectedItemChanged += (s, args) =>
  270. {
  271. // Remove existing class, if any
  272. if (curView is { })
  273. {
  274. curView.SubViewsLaidOut -= LayoutCompleteHandler;
  275. hostPane.Remove (curView);
  276. curView.Dispose ();
  277. curView = null;
  278. hostPane.FillRect (hostPane.Viewport);
  279. }
  280. curView = CreateClass (viewClasses.Values.ToArray () [classListView.SelectedItem]);
  281. };
  282. xOptionSelector.ValueChanged += (_, _) => DimPosChanged (curView);
  283. xText.TextChanged += (s, args) =>
  284. {
  285. try
  286. {
  287. xVal = int.Parse (xText.Text);
  288. DimPosChanged (curView);
  289. }
  290. catch
  291. { }
  292. };
  293. yText.TextChanged += (s, e) =>
  294. {
  295. try
  296. {
  297. yVal = int.Parse (yText.Text);
  298. DimPosChanged (curView);
  299. }
  300. catch
  301. { }
  302. };
  303. yOptionSelector.ValueChanged += (_, _) => DimPosChanged (curView);
  304. wOptionSelector.ValueChanged += (_, _) => DimPosChanged (curView);
  305. wText.TextChanged += (s, args) =>
  306. {
  307. try
  308. {
  309. wVal = int.Parse (wText.Text);
  310. DimPosChanged (curView);
  311. }
  312. catch
  313. { }
  314. };
  315. hText.TextChanged += (s, args) =>
  316. {
  317. try
  318. {
  319. hVal = int.Parse (hText.Text);
  320. DimPosChanged (curView);
  321. }
  322. catch
  323. { }
  324. };
  325. hOptionSelector.ValueChanged += (_, _) => DimPosChanged (curView);
  326. top.Add (leftPane, settingsPane, hostPane);
  327. top.LayoutSubViews ();
  328. curView = CreateClass (viewClasses.First ().Value);
  329. var iterations = 0;
  330. Application.Iteration += OnApplicationOnIteration;
  331. Application.Run (top);
  332. Application.Iteration -= OnApplicationOnIteration;
  333. Assert.Equal (viewClasses.Count, iterations);
  334. top.Dispose ();
  335. Application.Shutdown ();
  336. ConfigurationManager.Disable (true);
  337. return;
  338. void OnApplicationOnIteration (object? s, IterationEventArgs a)
  339. {
  340. iterations++;
  341. if (iterations < viewClasses.Count)
  342. {
  343. classListView.MoveDown ();
  344. if (curView is { })
  345. {
  346. Assert.Equal (
  347. curView.GetType ().Name,
  348. viewClasses.Values.ToArray () [classListView.SelectedItem].Name);
  349. }
  350. }
  351. else
  352. {
  353. Application.RequestStop ();
  354. }
  355. }
  356. void DimPosChanged (View? view)
  357. {
  358. if (view == null)
  359. {
  360. return;
  361. }
  362. try
  363. {
  364. switch (xOptionSelector.Value)
  365. {
  366. case 0:
  367. view.X = Pos.Percent (xVal);
  368. break;
  369. case 1:
  370. view.X = Pos.AnchorEnd (xVal);
  371. break;
  372. case 2:
  373. view.X = Pos.Center ();
  374. break;
  375. case 3:
  376. view.X = Pos.Absolute (xVal);
  377. break;
  378. }
  379. switch (yOptionSelector.Value)
  380. {
  381. case 0:
  382. view.Y = Pos.Percent (yVal);
  383. break;
  384. case 1:
  385. view.Y = Pos.AnchorEnd (yVal);
  386. break;
  387. case 2:
  388. view.Y = Pos.Center ();
  389. break;
  390. case 3:
  391. view.Y = Pos.Absolute (yVal);
  392. break;
  393. }
  394. switch (wOptionSelector.Value)
  395. {
  396. case 0:
  397. view.Width = Dim.Percent (wVal);
  398. break;
  399. case 1:
  400. view.Width = Dim.Fill (wVal);
  401. break;
  402. case 2:
  403. view.Width = Dim.Absolute (wVal);
  404. break;
  405. }
  406. switch (hOptionSelector.Value)
  407. {
  408. case 0:
  409. view.Height = Dim.Percent (hVal);
  410. break;
  411. case 1:
  412. view.Height = Dim.Fill (hVal);
  413. break;
  414. case 2:
  415. view.Height = Dim.Absolute (hVal);
  416. break;
  417. }
  418. }
  419. catch (Exception e)
  420. {
  421. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  422. }
  423. UpdateTitle (view);
  424. }
  425. void UpdateSettings (View view)
  426. {
  427. var x = view.X.ToString ();
  428. var y = view.Y.ToString ();
  429. try
  430. {
  431. xOptionSelector.Value = posNames.IndexOf (posNames.First (s => x.Contains (s)));
  432. yOptionSelector.Value = posNames.IndexOf (posNames.First (s => y.Contains (s)));
  433. }
  434. catch (InvalidOperationException e)
  435. {
  436. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  437. Debug.WriteLine ($"{e}");
  438. }
  439. xText.Text = $"{view.Frame.X}";
  440. yText.Text = $"{view.Frame.Y}";
  441. var w = view.Width!.ToString ();
  442. var h = view.Height!.ToString ();
  443. wOptionSelector.Value = dimNames.IndexOf (dimNames.First (s => w.Contains (s)));
  444. hOptionSelector.Value = dimNames.IndexOf (dimNames.First (s => h.Contains (s)));
  445. wText.Text = $"{view.Frame.Width}";
  446. hText.Text = $"{view.Frame.Height}";
  447. }
  448. void UpdateTitle (View? view) { hostPane.Title = $"{view!.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  449. View? CreateClass (Type type)
  450. {
  451. // If we are to create a generic Type
  452. if (type.IsGenericType)
  453. {
  454. // For each of the <T> arguments
  455. List<Type> typeArguments = new ();
  456. // use <object> or the original type if applicable
  457. foreach (Type arg in type.GetGenericArguments ())
  458. {
  459. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  460. {
  461. typeArguments.Add (arg);
  462. }
  463. else
  464. {
  465. typeArguments.Add (typeof (object));
  466. }
  467. }
  468. // Ensure the type does not contain any generic parameters
  469. if (type.ContainsGenericParameters)
  470. {
  471. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  472. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  473. return null;
  474. }
  475. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  476. type = type.MakeGenericType (typeArguments.ToArray ());
  477. }
  478. // Instantiate view
  479. var view = Activator.CreateInstance (type) as View;
  480. if (view is null)
  481. {
  482. return null;
  483. }
  484. if (view.Width is not DimAuto)
  485. {
  486. view.Width = Dim.Percent (75);
  487. }
  488. if (view.Height is not DimAuto)
  489. {
  490. view.Height = Dim.Percent (75);
  491. }
  492. // Set the colorscheme to make it stand out if is null by default
  493. if (!view.HasScheme)
  494. {
  495. view.SchemeName = "Base";
  496. }
  497. // If the view supports a Text property, set it so we have something to look at
  498. if (view.GetType ().GetProperty ("Text") != null)
  499. {
  500. try
  501. {
  502. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { "Test Text" });
  503. }
  504. catch (TargetInvocationException e)
  505. {
  506. MessageBox.ErrorQuery ("Exception", e.InnerException!.Message, "Ok");
  507. view = null;
  508. }
  509. }
  510. // If the view supports a Title property, set it so we have something to look at
  511. if (view != null && view.GetType ().GetProperty ("Title") != null)
  512. {
  513. if (view.GetType ().GetProperty ("Title")!.PropertyType == typeof (string))
  514. {
  515. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  516. }
  517. else
  518. {
  519. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  520. }
  521. }
  522. // If the view supports a Source property, set it so we have something to look at
  523. if (view != null
  524. && view.GetType ().GetProperty ("Source") != null
  525. && view.GetType ().GetProperty ("Source")!.PropertyType == typeof (IListDataSource))
  526. {
  527. ListWrapper<string> source = new (["Test Text #1", "Test Text #2", "Test Text #3"]);
  528. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  529. }
  530. // Add
  531. hostPane.Add (view);
  532. //DimPosChanged ();
  533. hostPane.LayoutSubViews ();
  534. hostPane.ClearViewport ();
  535. hostPane.SetNeedsDraw ();
  536. UpdateSettings (view!);
  537. UpdateTitle (view);
  538. view!.SubViewsLaidOut += LayoutCompleteHandler;
  539. return view;
  540. }
  541. void LayoutCompleteHandler (object? sender, LayoutEventArgs args) { UpdateTitle (curView); }
  542. }
  543. }