ScenarioTests.cs 16 KB

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