ScenarioTests.cs 17 KB

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