AllViewsTests.cs 1.9 KB

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