StandardWindowTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Chrome;
  6. using Avalonia.Interactivity;
  7. using Avalonia.Media;
  8. using Avalonia.Platform;
  9. using Avalonia.VisualTree;
  10. using Xunit;
  11. namespace Avalonia.IntegrationTests.Win32;
  12. public abstract class StandardWindowTests : IDisposable
  13. {
  14. private const double ClientWidth = 200;
  15. private const double ClientHeight = 200;
  16. private Window? _window;
  17. private Window Window
  18. {
  19. get
  20. {
  21. Assert.NotNull(_window);
  22. return _window;
  23. }
  24. }
  25. protected abstract SystemDecorations Decorations { get; }
  26. protected abstract bool HasCaption { get; }
  27. public static MatrixTheoryData<bool, WindowState> States
  28. => new([true, false], Enum.GetValues<WindowState>());
  29. private async Task InitWindowAsync(WindowState state, bool canResize)
  30. {
  31. Assert.Null(_window);
  32. _window = new Window
  33. {
  34. CanResize = canResize,
  35. WindowState = state,
  36. SystemDecorations = Decorations,
  37. ExtendClientAreaToDecorationsHint = false,
  38. Width = ClientWidth,
  39. Height = ClientHeight,
  40. WindowStartupLocation = WindowStartupLocation.Manual,
  41. Position = new PixelPoint(50, 50),
  42. Content = new Border
  43. {
  44. Background = Brushes.DodgerBlue,
  45. BorderBrush = Brushes.Yellow,
  46. BorderThickness = new Thickness(1)
  47. }
  48. };
  49. _window.Show();
  50. await Window.WhenLoadedAsync();
  51. }
  52. [Theory]
  53. [MemberData(nameof(States))]
  54. public async Task Maximized_State_Fills_Screen_Working_Area(bool canResize, WindowState initialState)
  55. {
  56. await InitWindowAsync(initialState, canResize);
  57. if (initialState != WindowState.Maximized)
  58. Window.WindowState = WindowState.Maximized;
  59. // The client size should match the screen working area
  60. var clientSize = Window.GetWin32ClientSize();
  61. var screenWorkingArea = Window.GetScreen().WorkingArea;
  62. if (HasCaption)
  63. {
  64. Assert.Equal(screenWorkingArea.Size.Width, clientSize.Width);
  65. Assert.True(clientSize.Height < screenWorkingArea.Size.Height);
  66. }
  67. else
  68. Assert.Equal(screenWorkingArea.Size, clientSize);
  69. }
  70. [Theory]
  71. [MemberData(nameof(States))]
  72. public async Task FullScreen_State_Fills_Screen(bool canResize, WindowState initialState)
  73. {
  74. await InitWindowAsync(initialState, canResize);
  75. if (initialState != WindowState.FullScreen)
  76. Window.WindowState = WindowState.FullScreen;
  77. // The client size should match the screen bounds
  78. var clientSize = Window.GetWin32ClientSize();
  79. var screenBounds = Window.GetScreen().Bounds;
  80. Assert.Equal(screenBounds.Width, clientSize.Width);
  81. Assert.Equal(screenBounds.Height, clientSize.Height);
  82. // The window size should also match the screen bounds
  83. var windowBounds = Window.GetWin32WindowBounds();
  84. Assert.Equal(screenBounds, windowBounds);
  85. }
  86. public void Dispose()
  87. => _window?.Close();
  88. public sealed class DecorationsFull : StandardWindowTests
  89. {
  90. protected override SystemDecorations Decorations
  91. => SystemDecorations.Full;
  92. protected override bool HasCaption
  93. => true;
  94. }
  95. public sealed class DecorationsBorderOnly : StandardWindowTests
  96. {
  97. protected override SystemDecorations Decorations
  98. => SystemDecorations.BorderOnly;
  99. protected override bool HasCaption
  100. => false;
  101. }
  102. public sealed class DecorationsNone : StandardWindowTests
  103. {
  104. protected override SystemDecorations Decorations
  105. => SystemDecorations.None;
  106. protected override bool HasCaption
  107. => false;
  108. }
  109. }