ScenarioTests.cs 22 KB

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