ScenarioTests.cs 16 KB

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