ScenarioTests.cs 22 KB

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