MockWindowingPlatform.cs 5.6 KB

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