AllViewsTests.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Reflection;
  2. using UnitTests;
  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. [SetupFakeDriver] // Required for spinner view that wants to register timeouts
  11. public void AllViews_Center_Properly (Type viewType)
  12. {
  13. // Required for spinner view that wants to register timeouts
  14. Application.MainLoop = new (new FakeMainLoop (Application.Driver));
  15. var view = CreateInstanceIfNotGeneric (viewType);
  16. // See https://github.com/gui-cs/Terminal.Gui/issues/3156
  17. if (view == null)
  18. {
  19. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  20. Application.Shutdown ();
  21. return;
  22. }
  23. if (view is IDesignable designable)
  24. {
  25. designable.EnableForDesign ();
  26. }
  27. view.X = Pos.Center ();
  28. view.Y = Pos.Center ();
  29. // Ensure the view has positive dimensions
  30. view.Width = 10;
  31. view.Height = 10;
  32. var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
  33. frame.Add (view);
  34. frame.BeginInit ();
  35. frame.EndInit ();
  36. frame.LayoutSubViews ();
  37. // What's the natural width/height?
  38. int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
  39. int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
  40. Assert.True (
  41. view.Frame.Left == expectedX,
  42. $"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
  43. );
  44. Assert.True (
  45. view.Frame.Top == expectedY,
  46. $"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
  47. );
  48. Application.Shutdown ();
  49. }
  50. }