TestServices.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Moq;
  5. using Avalonia.Input;
  6. using Avalonia.Layout;
  7. using Avalonia.Markup.Xaml;
  8. using Avalonia.Media;
  9. using Avalonia.Platform;
  10. using Avalonia.Shared.PlatformSupport;
  11. using Avalonia.Styling;
  12. using Avalonia.Themes.Default;
  13. using Avalonia.Rendering;
  14. namespace Avalonia.UnitTests
  15. {
  16. public class TestServices
  17. {
  18. public static readonly TestServices StyledWindow = new TestServices(
  19. assetLoader: new AssetLoader(),
  20. layoutManager: new LayoutManager(),
  21. platform: new AppBuilder().RuntimePlatform,
  22. renderer: (_, __) => Mock.Of<IRenderer>(),
  23. renderInterface: CreateRenderInterfaceMock(),
  24. renderLoop: Mock.Of<IRenderLoop>(),
  25. standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
  26. styler: new Styler(),
  27. theme: () => CreateDefaultTheme(),
  28. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true),
  29. windowingPlatform: new MockWindowingPlatform());
  30. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  31. renderInterface: CreateRenderInterfaceMock());
  32. public static readonly TestServices MockPlatformWrapper = new TestServices(
  33. platform: Mock.Of<IRuntimePlatform>());
  34. public static readonly TestServices MockStyler = new TestServices(
  35. styler: Mock.Of<IStyler>());
  36. public static readonly TestServices MockThreadingInterface = new TestServices(
  37. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true));
  38. public static readonly TestServices RealDeferredRenderer = new TestServices(
  39. renderer: (root, loop) => new DeferredRenderer(root, loop));
  40. public static readonly TestServices RealFocus = new TestServices(
  41. focusManager: new FocusManager(),
  42. keyboardDevice: () => new KeyboardDevice(),
  43. inputManager: new InputManager());
  44. public static readonly TestServices RealLayoutManager = new TestServices(
  45. layoutManager: new LayoutManager());
  46. public static readonly TestServices RealStyler = new TestServices(
  47. styler: new Styler());
  48. public TestServices(
  49. IAssetLoader assetLoader = null,
  50. IFocusManager focusManager = null,
  51. IInputManager inputManager = null,
  52. Func<IKeyboardDevice> keyboardDevice = null,
  53. ILayoutManager layoutManager = null,
  54. Func<IMouseDevice> mouseDevice = null,
  55. IRuntimePlatform platform = null,
  56. Func<IRenderRoot, IRenderLoop, IRenderer> renderer = null,
  57. IPlatformRenderInterface renderInterface = null,
  58. IRenderLoop renderLoop = null,
  59. IStandardCursorFactory standardCursorFactory = null,
  60. IStyler styler = null,
  61. Func<Styles> theme = null,
  62. IPlatformThreadingInterface threadingInterface = null,
  63. IWindowImpl windowImpl = null,
  64. IWindowingPlatform windowingPlatform = null)
  65. {
  66. AssetLoader = assetLoader;
  67. FocusManager = focusManager;
  68. InputManager = inputManager;
  69. KeyboardDevice = keyboardDevice;
  70. LayoutManager = layoutManager;
  71. MouseDevice = mouseDevice;
  72. Platform = platform;
  73. Renderer = renderer;
  74. RenderInterface = renderInterface;
  75. RenderLoop = renderLoop;
  76. StandardCursorFactory = standardCursorFactory;
  77. Styler = styler;
  78. Theme = theme;
  79. ThreadingInterface = threadingInterface;
  80. WindowImpl = windowImpl;
  81. WindowingPlatform = windowingPlatform;
  82. }
  83. public IAssetLoader AssetLoader { get; }
  84. public IInputManager InputManager { get; }
  85. public IFocusManager FocusManager { get; }
  86. public Func<IKeyboardDevice> KeyboardDevice { get; }
  87. public ILayoutManager LayoutManager { get; }
  88. public Func<IMouseDevice> MouseDevice { get; }
  89. public IRuntimePlatform Platform { get; }
  90. public Func<IRenderRoot, IRenderLoop, IRenderer> Renderer { get; }
  91. public IPlatformRenderInterface RenderInterface { get; }
  92. public IRenderLoop RenderLoop { get; }
  93. public IStandardCursorFactory StandardCursorFactory { get; }
  94. public IStyler Styler { get; }
  95. public Func<Styles> Theme { get; }
  96. public IPlatformThreadingInterface ThreadingInterface { get; }
  97. public IWindowImpl WindowImpl { get; }
  98. public IWindowingPlatform WindowingPlatform { get; }
  99. public TestServices With(
  100. IAssetLoader assetLoader = null,
  101. IFocusManager focusManager = null,
  102. IInputManager inputManager = null,
  103. Func<IKeyboardDevice> keyboardDevice = null,
  104. ILayoutManager layoutManager = null,
  105. Func<IMouseDevice> mouseDevice = null,
  106. IRuntimePlatform platform = null,
  107. Func<IRenderRoot, IRenderLoop, IRenderer> renderer = null,
  108. IPlatformRenderInterface renderInterface = null,
  109. IRenderLoop renderLoop = null,
  110. IStandardCursorFactory standardCursorFactory = null,
  111. IStyler styler = null,
  112. Func<Styles> theme = null,
  113. IPlatformThreadingInterface threadingInterface = null,
  114. IWindowImpl windowImpl = null,
  115. IWindowingPlatform windowingPlatform = null)
  116. {
  117. return new TestServices(
  118. assetLoader: assetLoader ?? AssetLoader,
  119. focusManager: focusManager ?? FocusManager,
  120. inputManager: inputManager ?? InputManager,
  121. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  122. layoutManager: layoutManager ?? LayoutManager,
  123. mouseDevice: mouseDevice ?? MouseDevice,
  124. platform: platform ?? Platform,
  125. renderer: renderer ?? Renderer,
  126. renderInterface: renderInterface ?? RenderInterface,
  127. renderLoop: renderLoop ?? RenderLoop,
  128. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  129. styler: styler ?? Styler,
  130. theme: theme ?? Theme,
  131. threadingInterface: threadingInterface ?? ThreadingInterface,
  132. windowImpl: windowImpl ?? WindowImpl,
  133. windowingPlatform: windowingPlatform ?? WindowingPlatform);
  134. }
  135. private static Styles CreateDefaultTheme()
  136. {
  137. var result = new Styles
  138. {
  139. new DefaultTheme(),
  140. };
  141. var loader = new AvaloniaXamlLoader();
  142. var baseLight = (IStyle)loader.Load(
  143. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"));
  144. result.Add(baseLight);
  145. return result;
  146. }
  147. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  148. {
  149. var formattedTextImpl = new Mock<IFormattedTextImpl>();
  150. formattedTextImpl.Setup(x => x.WithConstraint(It.IsAny<Size>())).Returns(() => formattedTextImpl.Object);
  151. return Mock.Of<IPlatformRenderInterface>(x =>
  152. x.CreateFormattedText(
  153. It.IsAny<string>(),
  154. It.IsAny<string>(),
  155. It.IsAny<double>(),
  156. It.IsAny<FontStyle>(),
  157. It.IsAny<TextAlignment>(),
  158. It.IsAny<FontWeight>(),
  159. It.IsAny<TextWrapping>(),
  160. It.IsAny<Size>()) == formattedTextImpl.Object &&
  161. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  162. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  163. }
  164. }
  165. }