TestServices.cs 9.2 KB

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