TestServices.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. IRuntimePlatform platform = null,
  55. Func<IRenderRoot, IRenderLoop, IRenderer> renderer = null,
  56. IPlatformRenderInterface renderInterface = null,
  57. IRenderLoop renderLoop = null,
  58. IStandardCursorFactory standardCursorFactory = null,
  59. IStyler styler = null,
  60. Func<Styles> theme = null,
  61. IPlatformThreadingInterface threadingInterface = null,
  62. IWindowImpl windowImpl = null,
  63. IWindowingPlatform windowingPlatform = null)
  64. {
  65. AssetLoader = assetLoader;
  66. FocusManager = focusManager;
  67. InputManager = inputManager;
  68. KeyboardDevice = keyboardDevice;
  69. LayoutManager = layoutManager;
  70. Platform = platform;
  71. Renderer = renderer;
  72. RenderInterface = renderInterface;
  73. RenderLoop = renderLoop;
  74. StandardCursorFactory = standardCursorFactory;
  75. Styler = styler;
  76. Theme = theme;
  77. ThreadingInterface = threadingInterface;
  78. WindowImpl = windowImpl;
  79. WindowingPlatform = windowingPlatform;
  80. }
  81. public IAssetLoader AssetLoader { get; }
  82. public IInputManager InputManager { get; }
  83. public IFocusManager FocusManager { get; }
  84. public Func<IKeyboardDevice> KeyboardDevice { get; }
  85. public ILayoutManager LayoutManager { get; }
  86. public IRuntimePlatform Platform { get; }
  87. public Func<IRenderRoot, IRenderLoop, IRenderer> Renderer { get; }
  88. public IPlatformRenderInterface RenderInterface { get; }
  89. public IRenderLoop RenderLoop { get; }
  90. public IStandardCursorFactory StandardCursorFactory { get; }
  91. public IStyler Styler { get; }
  92. public Func<Styles> Theme { get; }
  93. public IPlatformThreadingInterface ThreadingInterface { get; }
  94. public IWindowImpl WindowImpl { get; }
  95. public IWindowingPlatform WindowingPlatform { get; }
  96. public TestServices With(
  97. IAssetLoader assetLoader = null,
  98. IFocusManager focusManager = null,
  99. IInputManager inputManager = null,
  100. Func<IKeyboardDevice> keyboardDevice = null,
  101. ILayoutManager layoutManager = null,
  102. IRuntimePlatform platform = null,
  103. Func<IRenderRoot, IRenderLoop, IRenderer> renderer = null,
  104. IPlatformRenderInterface renderInterface = null,
  105. IRenderLoop renderLoop = null,
  106. IStandardCursorFactory standardCursorFactory = null,
  107. IStyler styler = null,
  108. Func<Styles> theme = null,
  109. IPlatformThreadingInterface threadingInterface = null,
  110. IWindowImpl windowImpl = null,
  111. IWindowingPlatform windowingPlatform = null)
  112. {
  113. return new TestServices(
  114. assetLoader: assetLoader ?? AssetLoader,
  115. focusManager: focusManager ?? FocusManager,
  116. inputManager: inputManager ?? InputManager,
  117. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  118. layoutManager: layoutManager ?? LayoutManager,
  119. platform: platform ?? Platform,
  120. renderer: renderer ?? Renderer,
  121. renderInterface: renderInterface ?? RenderInterface,
  122. renderLoop: renderLoop ?? RenderLoop,
  123. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  124. styler: styler ?? Styler,
  125. theme: theme ?? Theme,
  126. threadingInterface: threadingInterface ?? ThreadingInterface,
  127. windowImpl: windowImpl ?? WindowImpl,
  128. windowingPlatform: windowingPlatform ?? WindowingPlatform);
  129. }
  130. private static Styles CreateDefaultTheme()
  131. {
  132. var result = new Styles
  133. {
  134. new DefaultTheme(),
  135. };
  136. var loader = new AvaloniaXamlLoader();
  137. var baseLight = (IStyle)loader.Load(
  138. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"));
  139. result.Add(baseLight);
  140. return result;
  141. }
  142. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  143. {
  144. return Mock.Of<IPlatformRenderInterface>(x =>
  145. x.CreateFormattedText(
  146. It.IsAny<string>(),
  147. It.IsAny<string>(),
  148. It.IsAny<double>(),
  149. It.IsAny<FontStyle>(),
  150. It.IsAny<TextAlignment>(),
  151. It.IsAny<FontWeight>(),
  152. It.IsAny<TextWrapping>()) == Mock.Of<IFormattedTextImpl>() &&
  153. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  154. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  155. }
  156. }
  157. }