TestServices.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using Moq;
  3. using Avalonia.Input;
  4. using Avalonia.Layout;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Media;
  7. using Avalonia.Platform;
  8. using Avalonia.Styling;
  9. using Avalonia.Themes.Simple;
  10. using Avalonia.Rendering;
  11. using System.Reactive.Concurrency;
  12. using System.Collections.Generic;
  13. using Avalonia.Controls;
  14. using System.Reflection;
  15. using Avalonia.Animation;
  16. using Avalonia.Headless;
  17. using Avalonia.Threading;
  18. namespace Avalonia.UnitTests
  19. {
  20. public class TestServices
  21. {
  22. public static readonly TestServices StyledWindow = new TestServices(
  23. assetLoader: new StandardAssetLoader(),
  24. platform: new StandardRuntimePlatform(),
  25. renderInterface: new HeadlessPlatformRenderInterface(),
  26. standardCursorFactory: new HeadlessCursorFactoryStub(),
  27. theme: () => CreateSimpleTheme(),
  28. dispatcherImpl: new NullDispatcherImpl(),
  29. fontManagerImpl: new HeadlessFontManagerStub(),
  30. textShaperImpl: new HeadlessTextShaperStub(),
  31. windowingPlatform: new MockWindowingPlatform());
  32. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  33. assetLoader: new StandardAssetLoader(),
  34. renderInterface: new HeadlessPlatformRenderInterface(),
  35. fontManagerImpl: new HeadlessFontManagerStub(),
  36. textShaperImpl: new HeadlessTextShaperStub());
  37. public static readonly TestServices MockPlatformWrapper = new TestServices(
  38. platform: Mock.Of<IRuntimePlatform>());
  39. public static readonly TestServices MockThreadingInterface = new TestServices(
  40. dispatcherImpl: new NullDispatcherImpl());
  41. public static readonly TestServices MockWindowingPlatform = new TestServices(
  42. windowingPlatform: new MockWindowingPlatform());
  43. public static readonly TestServices RealFocus = new TestServices(
  44. focusManager: new FocusManager(),
  45. keyboardDevice: () => new KeyboardDevice(),
  46. keyboardNavigation: () => new KeyboardNavigationHandler(),
  47. inputManager: new InputManager(),
  48. assetLoader: new StandardAssetLoader(),
  49. renderInterface: new HeadlessPlatformRenderInterface(),
  50. fontManagerImpl: new HeadlessFontManagerStub(),
  51. textShaperImpl: new HeadlessTextShaperStub());
  52. public static readonly TestServices FocusableWindow = new TestServices(
  53. focusManager: new FocusManager(),
  54. keyboardDevice: () => new KeyboardDevice(),
  55. keyboardNavigation: () => new KeyboardNavigationHandler(),
  56. inputManager: new InputManager(),
  57. assetLoader: new StandardAssetLoader(),
  58. platform: new StandardRuntimePlatform(),
  59. renderInterface: new HeadlessPlatformRenderInterface(),
  60. standardCursorFactory: new HeadlessCursorFactoryStub(),
  61. theme: () => CreateSimpleTheme(),
  62. dispatcherImpl: new NullDispatcherImpl(),
  63. fontManagerImpl: new HeadlessFontManagerStub(),
  64. textShaperImpl: new HeadlessTextShaperStub(),
  65. windowingPlatform: new MockWindowingPlatform());
  66. public static readonly TestServices TextServices = new TestServices(
  67. assetLoader: new StandardAssetLoader(),
  68. renderInterface: new HeadlessPlatformRenderInterface(),
  69. fontManagerImpl: new HarfBuzzFontManagerImpl(),
  70. textShaperImpl: new HarfBuzzTextShaperImpl());
  71. public TestServices(
  72. IAssetLoader assetLoader = null,
  73. IFocusManager focusManager = null,
  74. IInputManager inputManager = null,
  75. Func<IKeyboardDevice> keyboardDevice = null,
  76. Func<IKeyboardNavigationHandler> keyboardNavigation = null,
  77. Func<IMouseDevice> mouseDevice = null,
  78. IRuntimePlatform platform = null,
  79. IPlatformRenderInterface renderInterface = null,
  80. IRenderTimer renderLoop = null,
  81. ICursorFactory standardCursorFactory = null,
  82. Func<IStyle> theme = null,
  83. IDispatcherImpl dispatcherImpl = null,
  84. IFontManagerImpl fontManagerImpl = null,
  85. ITextShaperImpl textShaperImpl = null,
  86. IWindowImpl windowImpl = null,
  87. IWindowingPlatform windowingPlatform = null)
  88. {
  89. AssetLoader = assetLoader;
  90. FocusManager = focusManager;
  91. InputManager = inputManager;
  92. KeyboardDevice = keyboardDevice;
  93. KeyboardNavigation = keyboardNavigation;
  94. MouseDevice = mouseDevice;
  95. Platform = platform;
  96. RenderInterface = renderInterface;
  97. FontManagerImpl = fontManagerImpl;
  98. TextShaperImpl = textShaperImpl;
  99. StandardCursorFactory = standardCursorFactory;
  100. Theme = theme;
  101. DispatcherImpl = dispatcherImpl;
  102. WindowImpl = windowImpl;
  103. WindowingPlatform = windowingPlatform;
  104. }
  105. internal TestServices(
  106. IGlobalClock globalClock,
  107. IAssetLoader assetLoader = null,
  108. IFocusManager focusManager = null,
  109. IInputManager inputManager = null,
  110. Func<IKeyboardDevice> keyboardDevice = null,
  111. Func<IKeyboardNavigationHandler> keyboardNavigation = null,
  112. Func<IMouseDevice> mouseDevice = null,
  113. IRuntimePlatform platform = null,
  114. IPlatformRenderInterface renderInterface = null,
  115. IRenderTimer renderLoop = null,
  116. ICursorFactory standardCursorFactory = null,
  117. Func<IStyle> theme = null,
  118. IDispatcherImpl dispatcherImpl = null,
  119. IFontManagerImpl fontManagerImpl = null,
  120. ITextShaperImpl textShaperImpl = null,
  121. IWindowImpl windowImpl = null,
  122. IWindowingPlatform windowingPlatform = null) : this(assetLoader, focusManager, inputManager, keyboardDevice,
  123. keyboardNavigation,
  124. mouseDevice, platform, renderInterface, renderLoop, standardCursorFactory, theme,
  125. dispatcherImpl, fontManagerImpl, textShaperImpl, windowImpl, windowingPlatform)
  126. {
  127. GlobalClock = globalClock;
  128. }
  129. public IAssetLoader AssetLoader { get; }
  130. public IInputManager InputManager { get; }
  131. public IFocusManager FocusManager { get; }
  132. internal IGlobalClock GlobalClock { get; set; }
  133. public Func<IKeyboardDevice> KeyboardDevice { get; }
  134. public Func<IKeyboardNavigationHandler> KeyboardNavigation { get; }
  135. public Func<IMouseDevice> MouseDevice { get; }
  136. public IRuntimePlatform Platform { get; }
  137. public IPlatformRenderInterface RenderInterface { get; }
  138. public IFontManagerImpl FontManagerImpl { get; }
  139. public ITextShaperImpl TextShaperImpl { get; }
  140. public ICursorFactory StandardCursorFactory { get; }
  141. public Func<IStyle> Theme { get; }
  142. public IDispatcherImpl DispatcherImpl { get; }
  143. public IWindowImpl WindowImpl { get; }
  144. public IWindowingPlatform WindowingPlatform { get; }
  145. internal TestServices With(
  146. IAssetLoader assetLoader = null,
  147. IFocusManager focusManager = null,
  148. IInputManager inputManager = null,
  149. Func<IKeyboardDevice> keyboardDevice = null,
  150. Func<IKeyboardNavigationHandler> keyboardNavigation = null,
  151. Func<IMouseDevice> mouseDevice = null,
  152. IRuntimePlatform platform = null,
  153. IPlatformRenderInterface renderInterface = null,
  154. IRenderTimer renderLoop = null,
  155. IScheduler scheduler = null,
  156. ICursorFactory standardCursorFactory = null,
  157. Func<IStyle> theme = null,
  158. IDispatcherImpl dispatcherImpl = null,
  159. IFontManagerImpl fontManagerImpl = null,
  160. ITextShaperImpl textShaperImpl = null,
  161. IWindowImpl windowImpl = null,
  162. IWindowingPlatform windowingPlatform = null,
  163. IGlobalClock globalClock = null)
  164. {
  165. return new TestServices(
  166. globalClock ?? GlobalClock,
  167. assetLoader: assetLoader ?? AssetLoader,
  168. focusManager: focusManager ?? FocusManager,
  169. inputManager: inputManager ?? InputManager,
  170. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  171. keyboardNavigation: keyboardNavigation ?? KeyboardNavigation,
  172. mouseDevice: mouseDevice ?? MouseDevice,
  173. platform: platform ?? Platform,
  174. renderInterface: renderInterface ?? RenderInterface,
  175. fontManagerImpl: fontManagerImpl ?? FontManagerImpl,
  176. textShaperImpl: textShaperImpl ?? TextShaperImpl,
  177. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  178. theme: theme ?? Theme,
  179. dispatcherImpl: dispatcherImpl ?? DispatcherImpl,
  180. windowingPlatform: windowingPlatform ?? WindowingPlatform,
  181. windowImpl: windowImpl ?? WindowImpl);
  182. }
  183. private static IStyle CreateSimpleTheme()
  184. {
  185. return new SimpleTheme();
  186. }
  187. }
  188. }