ScenarioTests.cs 17 KB

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