ScenarioTests.cs 22 KB

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