ScenarioTests.cs 23 KB

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