TestServices.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. using System.Reactive.Concurrency;
  15. using System.Collections.Generic;
  16. namespace Avalonia.UnitTests
  17. {
  18. public class TestServices
  19. {
  20. public static readonly TestServices StyledWindow = new TestServices(
  21. assetLoader: new AssetLoader(),
  22. layoutManager: new LayoutManager(),
  23. platform: new AppBuilder().RuntimePlatform,
  24. renderer: (_, __) => Mock.Of<IRenderer>(),
  25. renderInterface: new MockPlatformRenderInterface(),
  26. renderLoop: Mock.Of<IRenderLoop>(),
  27. standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
  28. styler: new Styler(),
  29. theme: () => CreateDefaultTheme(),
  30. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true),
  31. windowingPlatform: new MockWindowingPlatform());
  32. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  33. renderInterface: new MockPlatformRenderInterface());
  34. public static readonly TestServices MockPlatformWrapper = new TestServices(
  35. platform: Mock.Of<IRuntimePlatform>());
  36. public static readonly TestServices MockStyler = new TestServices(
  37. styler: Mock.Of<IStyler>());
  38. public static readonly TestServices MockThreadingInterface = new TestServices(
  39. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true));
  40. public static readonly TestServices MockWindowingPlatform = new TestServices(
  41. windowingPlatform: new MockWindowingPlatform());
  42. public static readonly TestServices RealDeferredRenderer = new TestServices(
  43. renderer: (root, loop) => new DeferredRenderer(root, loop));
  44. public static readonly TestServices RealFocus = new TestServices(
  45. focusManager: new FocusManager(),
  46. keyboardDevice: () => new KeyboardDevice(),
  47. inputManager: new InputManager());
  48. public static readonly TestServices RealLayoutManager = new TestServices(
  49. layoutManager: new LayoutManager());
  50. public static readonly TestServices RealStyler = new TestServices(
  51. styler: new Styler());
  52. public TestServices(
  53. IAssetLoader assetLoader = null,
  54. IFocusManager focusManager = null,
  55. IInputManager inputManager = null,
  56. Func<IKeyboardDevice> keyboardDevice = null,
  57. ILayoutManager layoutManager = null,
  58. Func<IMouseDevice> mouseDevice = null,
  59. IRuntimePlatform platform = null,
  60. Func<IRenderRoot, IRenderLoop, IRenderer> renderer = null,
  61. IPlatformRenderInterface renderInterface = null,
  62. IRenderLoop renderLoop = null,
  63. IScheduler scheduler = null,
  64. IStandardCursorFactory standardCursorFactory = null,
  65. IStyler styler = null,
  66. Func<Styles> theme = null,
  67. IPlatformThreadingInterface threadingInterface = null,
  68. IWindowingPlatform windowingPlatform = null)
  69. {
  70. AssetLoader = assetLoader;
  71. FocusManager = focusManager;
  72. InputManager = inputManager;
  73. KeyboardDevice = keyboardDevice;
  74. LayoutManager = layoutManager;
  75. MouseDevice = mouseDevice;
  76. Platform = platform;
  77. Renderer = renderer;
  78. RenderInterface = renderInterface;
  79. RenderLoop = renderLoop;
  80. Scheduler = scheduler;
  81. StandardCursorFactory = standardCursorFactory;
  82. Styler = styler;
  83. Theme = theme;
  84. ThreadingInterface = threadingInterface;
  85. WindowingPlatform = windowingPlatform;
  86. }
  87. public IAssetLoader AssetLoader { get; }
  88. public IInputManager InputManager { get; }
  89. public IFocusManager FocusManager { get; }
  90. public Func<IKeyboardDevice> KeyboardDevice { get; }
  91. public ILayoutManager LayoutManager { get; }
  92. public Func<IMouseDevice> MouseDevice { get; }
  93. public IRuntimePlatform Platform { get; }
  94. public Func<IRenderRoot, IRenderLoop, IRenderer> Renderer { get; }
  95. public IPlatformRenderInterface RenderInterface { get; }
  96. public IRenderLoop RenderLoop { get; }
  97. public IScheduler Scheduler { get; }
  98. public IStandardCursorFactory StandardCursorFactory { get; }
  99. public IStyler Styler { get; }
  100. public Func<Styles> Theme { get; }
  101. public IPlatformThreadingInterface ThreadingInterface { get; }
  102. public IWindowingPlatform WindowingPlatform { get; }
  103. public TestServices With(
  104. IAssetLoader assetLoader = null,
  105. IFocusManager focusManager = null,
  106. IInputManager inputManager = null,
  107. Func<IKeyboardDevice> keyboardDevice = null,
  108. ILayoutManager layoutManager = null,
  109. Func<IMouseDevice> mouseDevice = null,
  110. IRuntimePlatform platform = null,
  111. Func<IRenderRoot, IRenderLoop, IRenderer> renderer = null,
  112. IPlatformRenderInterface renderInterface = null,
  113. IRenderLoop renderLoop = null,
  114. IScheduler scheduler = null,
  115. IStandardCursorFactory standardCursorFactory = null,
  116. IStyler styler = null,
  117. Func<Styles> theme = null,
  118. IPlatformThreadingInterface threadingInterface = null,
  119. IWindowImpl windowImpl = null,
  120. IWindowingPlatform windowingPlatform = null)
  121. {
  122. return new TestServices(
  123. assetLoader: assetLoader ?? AssetLoader,
  124. focusManager: focusManager ?? FocusManager,
  125. inputManager: inputManager ?? InputManager,
  126. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  127. layoutManager: layoutManager ?? LayoutManager,
  128. mouseDevice: mouseDevice ?? MouseDevice,
  129. platform: platform ?? Platform,
  130. renderer: renderer ?? Renderer,
  131. renderInterface: renderInterface ?? RenderInterface,
  132. renderLoop: renderLoop ?? RenderLoop,
  133. scheduler: scheduler ?? Scheduler,
  134. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  135. styler: styler ?? Styler,
  136. theme: theme ?? Theme,
  137. threadingInterface: threadingInterface ?? ThreadingInterface,
  138. windowingPlatform: windowingPlatform ?? WindowingPlatform);
  139. }
  140. private static Styles CreateDefaultTheme()
  141. {
  142. var result = new Styles
  143. {
  144. new DefaultTheme(),
  145. };
  146. var loader = new AvaloniaXamlLoader();
  147. var baseLight = (IStyle)loader.Load(
  148. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"));
  149. result.Add(baseLight);
  150. return result;
  151. }
  152. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  153. {
  154. return Mock.Of<IPlatformRenderInterface>(x =>
  155. x.CreateFormattedText(
  156. It.IsAny<string>(),
  157. It.IsAny<Typeface>(),
  158. It.IsAny<TextAlignment>(),
  159. It.IsAny<TextWrapping>(),
  160. It.IsAny<Size>(),
  161. It.IsAny<IReadOnlyList<FormattedTextStyleSpan>>()) == Mock.Of<IFormattedTextImpl>() &&
  162. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  163. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  164. }
  165. }
  166. }