MockWindowingPlatform.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using Avalonia.Controls.Primitives.PopupPositioning;
  3. using Avalonia.Input;
  4. using Moq;
  5. using Avalonia.Platform;
  6. using Avalonia.Rendering;
  7. namespace Avalonia.UnitTests
  8. {
  9. public class MockWindowingPlatform : IWindowingPlatform
  10. {
  11. private static readonly Size s_screenSize = new Size(1280, 1024);
  12. private readonly Func<IWindowImpl> _windowImpl;
  13. private readonly Func<IWindowBaseImpl, IPopupImpl> _popupImpl;
  14. public MockWindowingPlatform(
  15. Func<IWindowImpl> windowImpl = null,
  16. Func<IWindowBaseImpl, IPopupImpl> popupImpl = null )
  17. {
  18. _windowImpl = windowImpl;
  19. _popupImpl = popupImpl;
  20. }
  21. public static Mock<IWindowImpl> CreateWindowMock(double initialWidth = 800, double initialHeight = 600)
  22. {
  23. var windowImpl = new Mock<IWindowImpl>();
  24. var position = new PixelPoint();
  25. var clientSize = new Size(initialWidth, initialHeight);
  26. windowImpl.SetupAllProperties();
  27. windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
  28. windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(s_screenSize);
  29. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  30. windowImpl.Setup(x => x.RenderScaling).Returns(1);
  31. windowImpl.Setup(x => x.Screen).Returns(CreateScreenMock().Object);
  32. windowImpl.Setup(x => x.Position).Returns(() => position);
  33. windowImpl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null);
  34. windowImpl.Setup(x => x.CreatePopup()).Returns(() =>
  35. {
  36. return CreatePopupMock(windowImpl.Object).Object;
  37. });
  38. windowImpl.Setup(x => x.Dispose()).Callback(() =>
  39. {
  40. windowImpl.Object.Closed?.Invoke();
  41. });
  42. windowImpl.Setup(x => x.Move(It.IsAny<PixelPoint>())).Callback<PixelPoint>(x =>
  43. {
  44. position = x;
  45. windowImpl.Object.PositionChanged?.Invoke(x);
  46. });
  47. windowImpl.Setup(x => x.Resize(It.IsAny<Size>(), It.IsAny<PlatformResizeReason>()))
  48. .Callback<Size, PlatformResizeReason>((x, y) =>
  49. {
  50. var constrainedSize = x.Constrain(s_screenSize);
  51. if (constrainedSize != clientSize)
  52. {
  53. clientSize = constrainedSize;
  54. windowImpl.Object.Resized?.Invoke(clientSize, y);
  55. }
  56. });
  57. windowImpl.Setup(x => x.Show(true, It.IsAny<bool>())).Callback(() =>
  58. {
  59. windowImpl.Object.Resized?.Invoke(windowImpl.Object.ClientSize, PlatformResizeReason.Unspecified);
  60. windowImpl.Object.Activated?.Invoke();
  61. });
  62. windowImpl.Setup(x => x.PointToScreen(It.IsAny<Point>()))
  63. .Returns((Point p) => PixelPoint.FromPoint(p, 1D) + position);
  64. return windowImpl;
  65. }
  66. public static Mock<IPopupImpl> CreatePopupMock(IWindowBaseImpl parent)
  67. {
  68. var popupImpl = new Mock<IPopupImpl>();
  69. var clientSize = new Size();
  70. var positionerHelper = new ManagedPopupPositionerPopupImplHelper(parent, (pos, size, scale) =>
  71. {
  72. clientSize = size.Constrain(s_screenSize);
  73. popupImpl.Object.PositionChanged?.Invoke(pos);
  74. popupImpl.Object.Resized?.Invoke(clientSize, PlatformResizeReason.Unspecified);
  75. });
  76. var positioner = new ManagedPopupPositioner(positionerHelper);
  77. popupImpl.SetupAllProperties();
  78. popupImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
  79. popupImpl.Setup(x => x.MaxAutoSizeHint).Returns(s_screenSize);
  80. popupImpl.Setup(x => x.RenderScaling).Returns(1);
  81. popupImpl.Setup(x => x.PopupPositioner).Returns(positioner);
  82. popupImpl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null);
  83. popupImpl.Setup(x => x.Dispose()).Callback(() =>
  84. {
  85. popupImpl.Object.Closed?.Invoke();
  86. });
  87. return popupImpl;
  88. }
  89. public static Mock<IScreenImpl> CreateScreenMock()
  90. {
  91. var screenImpl = new Mock<IScreenImpl>();
  92. var bounds = new PixelRect(0, 0, (int)s_screenSize.Width, (int)s_screenSize.Height);
  93. var screen = new Screen(96, bounds, bounds, true);
  94. screenImpl.Setup(x => x.AllScreens).Returns(new[] { screen });
  95. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  96. return screenImpl;
  97. }
  98. public IWindowImpl CreateWindow()
  99. {
  100. if (_windowImpl is object)
  101. {
  102. return _windowImpl();
  103. }
  104. else
  105. {
  106. var mock = CreateWindowMock();
  107. if (_popupImpl is object)
  108. {
  109. mock.Setup(x => x.CreatePopup()).Returns(() => _popupImpl(mock.Object));
  110. }
  111. return mock.Object;
  112. }
  113. }
  114. public IWindowImpl CreateEmbeddableWindow()
  115. {
  116. throw new NotImplementedException();
  117. }
  118. public ITrayIconImpl CreateTrayIcon()
  119. {
  120. return null;
  121. }
  122. }
  123. }