AllViewsTests.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Reflection;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests;
  5. public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
  6. {
  7. // TODO: Update all these tests to use AllViews like AllViews_Center_Properly does
  8. [Theory]
  9. [MemberData (nameof (AllViewTypes))]
  10. public void AllViews_Center_Properly (Type viewType)
  11. {
  12. var view = (View)CreateInstanceIfNotGeneric (viewType);
  13. // See https://github.com/gui-cs/Terminal.Gui/issues/3156
  14. if (view == null)
  15. {
  16. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  17. Application.Shutdown ();
  18. return;
  19. }
  20. view.X = Pos.Center ();
  21. view.Y = Pos.Center ();
  22. // Ensure the view has positive dimensions
  23. view.Width = 10;
  24. view.Height = 10;
  25. var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
  26. frame.Add (view);
  27. frame.BeginInit ();
  28. frame.EndInit ();
  29. frame.LayoutSubviews ();
  30. // What's the natural width/height?
  31. int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
  32. int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
  33. Assert.True (
  34. view.Frame.Left == expectedX,
  35. $"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
  36. );
  37. Assert.True (
  38. view.Frame.Top == expectedY,
  39. $"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
  40. );
  41. Application.Shutdown ();
  42. }
  43. [Theory]
  44. [MemberData (nameof (AllViewTypes))]
  45. public void AllViews_Tests_All_Constructors (Type viewType)
  46. {
  47. Assert.True (Test_All_Constructors_Of_Type (viewType));
  48. }
  49. //[Fact]
  50. //public void AllViews_HotKey_Works ()
  51. //{
  52. // foreach (var type in GetAllViewClasses ()) {
  53. // _output.WriteLine ($"Testing {type.Name}");
  54. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  55. // view.HotKeySpecifier = (Rune)'^';
  56. // view.Text = "^text";
  57. // Assert.Equal(Key.T, view.HotKey);
  58. // }
  59. //}
  60. public bool Test_All_Constructors_Of_Type (Type type)
  61. {
  62. foreach (ConstructorInfo ctor in type.GetConstructors ())
  63. {
  64. View view = TestHelpers.CreateViewFromType (type, ctor);
  65. if (view != null)
  66. {
  67. Assert.True (type.FullName == view.GetType ().FullName);
  68. }
  69. }
  70. return true;
  71. }
  72. }