MockWindowingPlatform.cs 6.3 KB

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