ScenarioTests.cs 17 KB

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