ScenarioTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. using System.Collections.ObjectModel;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using Xunit.Abstractions;
  5. namespace UICatalog.Tests;
  6. public class ScenarioTests : TestsAllViews
  7. {
  8. private readonly ITestOutputHelper _output;
  9. public ScenarioTests (ITestOutputHelper output)
  10. {
  11. #if DEBUG_IDISPOSABLE
  12. Responder.Instances.Clear ();
  13. #endif
  14. _output = output;
  15. }
  16. public static IEnumerable<object []> AllScenarioTypes =>
  17. typeof (Scenario).Assembly
  18. .GetTypes ()
  19. .Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
  20. .Select (type => new object [] { type });
  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 Run_All_Scenarios (Type scenarioType)
  28. {
  29. _output.WriteLine ($"Running Scenario '{scenarioType}'");
  30. Scenario scenario = (Scenario)Activator.CreateInstance (scenarioType);
  31. // BUGBUG: Scenario.Main is supposed to call Init. This is a workaround for now.
  32. // BUGBUG: To be able to test Scenarios we need Application to have an event like "Application.Started"
  33. // BUGBUG: That tests like this could subscribe to.
  34. Application.Init (new FakeDriver ());
  35. // Press QuitKey
  36. Assert.Empty (FakeConsole.MockKeyPresses);
  37. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  38. uint abortTime = 500;
  39. // If the scenario doesn't close within 500ms, this will force it to quit
  40. bool ForceCloseCallback ()
  41. {
  42. if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
  43. {
  44. Application.RequestStop ();
  45. // See #2474 for why this is commented out
  46. Assert.Fail (
  47. $"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey} after {abortTime}ms. Force quit.");
  48. }
  49. return false;
  50. }
  51. //output.WriteLine ($" Add timeout to force quit after {abortTime}ms");
  52. _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  53. Application.Iteration += (s, a) =>
  54. {
  55. // Press QuitKey
  56. Assert.Empty (FakeConsole.MockKeyPresses);
  57. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  58. //output.WriteLine ($" iteration {++iterations}");
  59. if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
  60. {
  61. Application.RequestStop ();
  62. Assert.Fail ($"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey}. Force quit.");
  63. }
  64. };
  65. scenario.Main ();
  66. scenario.Dispose ();
  67. Application.Shutdown ();
  68. #if DEBUG_IDISPOSABLE
  69. Assert.Empty (Responder.Instances);
  70. #endif
  71. }
  72. [Fact]
  73. public void Run_All_Views_Tester_Scenario ()
  74. {
  75. Window _leftPane;
  76. ListView _classListView;
  77. FrameView _hostPane;
  78. Dictionary<string, Type> _viewClasses;
  79. View _curView = null;
  80. // Settings
  81. FrameView _settingsPane;
  82. FrameView _locationFrame;
  83. RadioGroup _xRadioGroup;
  84. TextField _xText;
  85. var _xVal = 0;
  86. RadioGroup _yRadioGroup;
  87. TextField _yText;
  88. var _yVal = 0;
  89. FrameView _sizeFrame;
  90. RadioGroup _wRadioGroup;
  91. TextField _wText;
  92. var _wVal = 0;
  93. RadioGroup _hRadioGroup;
  94. TextField _hText;
  95. var _hVal = 0;
  96. List<string> posNames = new () { "Percent", "AnchorEnd", "Center", "Absolute" };
  97. List<string> dimNames = new () { "Auto", "Percent", "Fill", "Absolute" };
  98. Application.Init (new FakeDriver ());
  99. var top = new Toplevel ();
  100. _viewClasses = TestHelpers.GetAllViewClasses ().ToDictionary (t => t.Name);
  101. _leftPane = new ()
  102. {
  103. Title = "Classes",
  104. X = 0,
  105. Y = 0,
  106. Width = 15,
  107. Height = Dim.Fill (1), // for status bar
  108. CanFocus = false,
  109. ColorScheme = Colors.ColorSchemes ["TopLevel"]
  110. };
  111. _classListView = new ()
  112. {
  113. X = 0,
  114. Y = 0,
  115. Width = Dim.Fill (),
  116. Height = Dim.Fill (),
  117. AllowsMarking = false,
  118. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  119. Source = new ListWrapper<string> (new (_viewClasses.Keys.ToList ()))
  120. };
  121. _leftPane.Add (_classListView);
  122. _settingsPane = new ()
  123. {
  124. X = Pos.Right (_leftPane),
  125. Y = 0, // for menu
  126. Width = Dim.Fill (),
  127. Height = 10,
  128. CanFocus = false,
  129. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  130. Title = "Settings"
  131. };
  132. var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "Absolute(x)" };
  133. _locationFrame = new ()
  134. {
  135. X = 0,
  136. Y = 0,
  137. Height = 3 + radioItems.Length,
  138. Width = 36,
  139. Title = "Location (Pos)"
  140. };
  141. _settingsPane.Add (_locationFrame);
  142. var label = new Label { X = 0, Y = 0, Text = "x:" };
  143. _locationFrame.Add (label);
  144. _xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  145. _xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  146. _locationFrame.Add (_xText);
  147. _locationFrame.Add (_xRadioGroup);
  148. radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "Absolute(y)" };
  149. label = new () { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "y:" };
  150. _locationFrame.Add (label);
  151. _yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  152. _locationFrame.Add (_yText);
  153. _yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  154. _locationFrame.Add (_yRadioGroup);
  155. _sizeFrame = new ()
  156. {
  157. X = Pos.Right (_locationFrame),
  158. Y = Pos.Y (_locationFrame),
  159. Height = 3 + radioItems.Length,
  160. Width = 40,
  161. Title = "Size (Dim)"
  162. };
  163. radioItems = new [] { "Auto()", "Percent(width)", "Fill(width)", "Absolute(width)" };
  164. label = new () { X = 0, Y = 0, Text = "width:" };
  165. _sizeFrame.Add (label);
  166. _wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  167. _wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  168. _sizeFrame.Add (_wText);
  169. _sizeFrame.Add (_wRadioGroup);
  170. radioItems = new [] { "Auto()", "Percent(height)", "Fill(height)", "Absolute(height)" };
  171. label = new () { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "height:" };
  172. _sizeFrame.Add (label);
  173. _hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  174. _sizeFrame.Add (_hText);
  175. _hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  176. _sizeFrame.Add (_hRadioGroup);
  177. _settingsPane.Add (_sizeFrame);
  178. _hostPane = new ()
  179. {
  180. X = Pos.Right (_leftPane),
  181. Y = Pos.Bottom (_settingsPane),
  182. Width = Dim.Fill (),
  183. Height = Dim.Fill (1), // + 1 for status bar
  184. ColorScheme = Colors.ColorSchemes ["Dialog"]
  185. };
  186. _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
  187. _classListView.SelectedItemChanged += (s, args) =>
  188. {
  189. // Remove existing class, if any
  190. if (_curView != null)
  191. {
  192. _curView.LayoutComplete -= LayoutCompleteHandler;
  193. _hostPane.Remove (_curView);
  194. _curView.Dispose ();
  195. _curView = null;
  196. _hostPane.FillRect (_hostPane.Viewport);
  197. }
  198. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  199. };
  200. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  201. _xText.TextChanged += (s, args) =>
  202. {
  203. try
  204. {
  205. _xVal = int.Parse (_xText.Text);
  206. DimPosChanged (_curView);
  207. }
  208. catch
  209. { }
  210. };
  211. _yText.TextChanged += (s, e) =>
  212. {
  213. try
  214. {
  215. _yVal = int.Parse (_yText.Text);
  216. DimPosChanged (_curView);
  217. }
  218. catch
  219. { }
  220. };
  221. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  222. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  223. _wText.TextChanged += (s, args) =>
  224. {
  225. try
  226. {
  227. _wVal = int.Parse (_wText.Text);
  228. DimPosChanged (_curView);
  229. }
  230. catch
  231. { }
  232. };
  233. _hText.TextChanged += (s, args) =>
  234. {
  235. try
  236. {
  237. _hVal = int.Parse (_hText.Text);
  238. DimPosChanged (_curView);
  239. }
  240. catch
  241. { }
  242. };
  243. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  244. top.Add (_leftPane, _settingsPane, _hostPane);
  245. top.LayoutSubviews ();
  246. _curView = CreateClass (_viewClasses.First ().Value);
  247. var iterations = 0;
  248. Application.Iteration += (s, a) =>
  249. {
  250. iterations++;
  251. if (iterations < _viewClasses.Count)
  252. {
  253. _classListView.MoveDown ();
  254. Assert.Equal (
  255. _curView.GetType ().Name,
  256. _viewClasses.Values.ToArray () [_classListView.SelectedItem].Name
  257. );
  258. }
  259. else
  260. {
  261. Application.RequestStop ();
  262. }
  263. };
  264. Application.Run (top);
  265. Assert.Equal (_viewClasses.Count, iterations);
  266. top.Dispose ();
  267. Application.Shutdown ();
  268. void DimPosChanged (View view)
  269. {
  270. if (view == null)
  271. {
  272. return;
  273. }
  274. try
  275. {
  276. switch (_xRadioGroup.SelectedItem)
  277. {
  278. case 0:
  279. view.X = Pos.Percent (_xVal);
  280. break;
  281. case 1:
  282. view.X = Pos.AnchorEnd (_xVal);
  283. break;
  284. case 2:
  285. view.X = Pos.Center ();
  286. break;
  287. case 3:
  288. view.X = Pos.Absolute (_xVal);
  289. break;
  290. }
  291. switch (_yRadioGroup.SelectedItem)
  292. {
  293. case 0:
  294. view.Y = Pos.Percent (_yVal);
  295. break;
  296. case 1:
  297. view.Y = Pos.AnchorEnd (_yVal);
  298. break;
  299. case 2:
  300. view.Y = Pos.Center ();
  301. break;
  302. case 3:
  303. view.Y = Pos.Absolute (_yVal);
  304. break;
  305. }
  306. switch (_wRadioGroup.SelectedItem)
  307. {
  308. case 0:
  309. view.Width = Dim.Percent (_wVal);
  310. break;
  311. case 1:
  312. view.Width = Dim.Fill (_wVal);
  313. break;
  314. case 2:
  315. view.Width = Dim.Absolute (_wVal);
  316. break;
  317. }
  318. switch (_hRadioGroup.SelectedItem)
  319. {
  320. case 0:
  321. view.Height = Dim.Percent (_hVal);
  322. break;
  323. case 1:
  324. view.Height = Dim.Fill (_hVal);
  325. break;
  326. case 2:
  327. view.Height = Dim.Absolute (_hVal);
  328. break;
  329. }
  330. }
  331. catch (Exception e)
  332. {
  333. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  334. }
  335. UpdateTitle (view);
  336. }
  337. void UpdateSettings (View view)
  338. {
  339. var x = view.X.ToString ();
  340. var y = view.Y.ToString ();
  341. try
  342. {
  343. _xRadioGroup.SelectedItem = posNames.IndexOf (posNames.First (s => x.Contains (s)));
  344. _yRadioGroup.SelectedItem = posNames.IndexOf (posNames.First (s => y.Contains (s)));
  345. }
  346. catch (InvalidOperationException e)
  347. {
  348. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  349. Debug.WriteLine ($"{e}");
  350. }
  351. _xText.Text = $"{view.Frame.X}";
  352. _yText.Text = $"{view.Frame.Y}";
  353. var w = view.Width.ToString ();
  354. var h = view.Height.ToString ();
  355. _wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.First (s => w.Contains (s)));
  356. _hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.First (s => h.Contains (s)));
  357. _wText.Text = $"{view.Frame.Width}";
  358. _hText.Text = $"{view.Frame.Height}";
  359. }
  360. void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  361. View CreateClass (Type type)
  362. {
  363. // If we are to create a generic Type
  364. if (type.IsGenericType)
  365. {
  366. // For each of the <T> arguments
  367. List<Type> typeArguments = new ();
  368. // use <object>
  369. foreach (Type arg in type.GetGenericArguments ())
  370. {
  371. typeArguments.Add (typeof (object));
  372. }
  373. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  374. type = type.MakeGenericType (typeArguments.ToArray ());
  375. }
  376. // Instantiate view
  377. var view = (View)Activator.CreateInstance (type);
  378. if (view is null)
  379. {
  380. return null;
  381. }
  382. if (view.Width is not DimAuto)
  383. {
  384. view.Width = Dim.Percent (75);
  385. }
  386. if (view.Height is not DimAuto)
  387. {
  388. view.Height = Dim.Percent (75);
  389. }
  390. // Set the colorscheme to make it stand out if is null by default
  391. view.ColorScheme ??= Colors.ColorSchemes ["Base"];
  392. // If the view supports a Text property, set it so we have something to look at
  393. if (view.GetType ().GetProperty ("Text") != null)
  394. {
  395. try
  396. {
  397. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { "Test Text" });
  398. }
  399. catch (TargetInvocationException e)
  400. {
  401. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  402. view = null;
  403. }
  404. }
  405. // If the view supports a Title property, set it so we have something to look at
  406. if (view != null && view.GetType ().GetProperty ("Title") != null)
  407. {
  408. if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
  409. {
  410. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  411. }
  412. else
  413. {
  414. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { "Test Title" });
  415. }
  416. }
  417. // If the view supports a Source property, set it so we have something to look at
  418. if (view != null
  419. && view.GetType ().GetProperty ("Source") != null
  420. && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  421. {
  422. var source = new ListWrapper<string> (["Test Text #1", "Test Text #2", "Test Text #3"]);
  423. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  424. }
  425. // Add
  426. _hostPane.Add (view);
  427. //DimPosChanged ();
  428. _hostPane.LayoutSubviews ();
  429. _hostPane.Clear ();
  430. _hostPane.SetNeedsDisplay ();
  431. UpdateSettings (view);
  432. UpdateTitle (view);
  433. view.LayoutComplete += LayoutCompleteHandler;
  434. return view;
  435. }
  436. void LayoutCompleteHandler (object sender, LayoutEventArgs args) { UpdateTitle (_curView); }
  437. }
  438. [Fact]
  439. public void Run_Generic ()
  440. {
  441. ObservableCollection<Scenario> scenarios = Scenario.GetScenarios ();
  442. Assert.NotEmpty (scenarios);
  443. int item = scenarios.IndexOf (s => s.GetName ().Equals ("Generic", StringComparison.OrdinalIgnoreCase));
  444. Scenario generic = scenarios [item];
  445. Application.Init (new FakeDriver ());
  446. // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
  447. // by adding this Space it seems to work.
  448. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  449. var ms = 100;
  450. var abortCount = 0;
  451. Func<bool> abortCallback = () =>
  452. {
  453. abortCount++;
  454. _output.WriteLine ($"'Generic' abortCount {abortCount}");
  455. Application.RequestStop ();
  456. return false;
  457. };
  458. var iterations = 0;
  459. object token = null;
  460. Application.Iteration += (s, a) =>
  461. {
  462. if (token == null)
  463. {
  464. // Timeout only must start at first iteration
  465. token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
  466. }
  467. iterations++;
  468. _output.WriteLine ($"'Generic' iteration {iterations}");
  469. // Stop if we run out of control...
  470. if (iterations == 10)
  471. {
  472. _output.WriteLine ("'Generic' had to be force quit!");
  473. Application.RequestStop ();
  474. }
  475. };
  476. Application.KeyDown += (sender, args) =>
  477. {
  478. // See #2474 for why this is commented out
  479. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, args.KeyCode);
  480. };
  481. generic.Main ();
  482. Assert.Equal (0, abortCount);
  483. // # of key up events should match # of iterations
  484. Assert.Equal (1, iterations);
  485. generic.Dispose ();
  486. // Shutdown must be called to safely clean up Application if Init has been called
  487. Application.Shutdown ();
  488. #if DEBUG_IDISPOSABLE
  489. Assert.Empty (Responder.Instances);
  490. #endif
  491. }
  492. private int CreateInput (string input)
  493. {
  494. FakeConsole.MockKeyPresses.Clear ();
  495. // Put a QuitKey in at the end
  496. FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
  497. foreach (char c in input.Reverse ())
  498. {
  499. var key = KeyCode.Null;
  500. if (char.IsLetter (c))
  501. {
  502. key = (KeyCode)char.ToUpper (c) | (char.IsUpper (c) ? KeyCode.ShiftMask : 0);
  503. }
  504. else
  505. {
  506. key = (KeyCode)c;
  507. }
  508. FakeConsole.PushMockKeyPress (key);
  509. }
  510. return FakeConsole.MockKeyPresses.Count;
  511. }
  512. }