TestServices.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. keyboardDevice: () => new KeyboardDevice(),
  39. keyboardNavigation: () => new KeyboardNavigationHandler(),
  40. inputManager: new InputManager(),
  41. assetLoader: new StandardAssetLoader(),
  42. renderInterface: new HeadlessPlatformRenderInterface(),
  43. fontManagerImpl: new HeadlessFontManagerStub(),
  44. textShaperImpl: new HeadlessTextShaperStub());
  45. public static readonly TestServices FocusableWindow = new TestServices(
  46. keyboardDevice: () => new KeyboardDevice(),
  47. keyboardNavigation: () => new KeyboardNavigationHandler(),
  48. inputManager: new InputManager(),
  49. assetLoader: new StandardAssetLoader(),
  50. platform: new StandardRuntimePlatform(),
  51. renderInterface: new HeadlessPlatformRenderInterface(),
  52. standardCursorFactory: new HeadlessCursorFactoryStub(),
  53. theme: () => CreateSimpleTheme(),
  54. dispatcherImpl: new NullDispatcherImpl(),
  55. fontManagerImpl: new HeadlessFontManagerStub(),
  56. textShaperImpl: new HeadlessTextShaperStub(),
  57. windowingPlatform: new MockWindowingPlatform());
  58. public static readonly TestServices TextServices = new TestServices(
  59. assetLoader: new StandardAssetLoader(),
  60. renderInterface: new HeadlessPlatformRenderInterface(),
  61. fontManagerImpl: new HarfBuzzFontManagerImpl(),
  62. textShaperImpl: new HarfBuzzTextShaperImpl());
  63. public TestServices(
  64. IAssetLoader assetLoader = null,
  65. IInputManager inputManager = null,
  66. Func<IKeyboardDevice> keyboardDevice = null,
  67. Func<IKeyboardNavigationHandler> keyboardNavigation = null,
  68. Func<IMouseDevice> mouseDevice = null,
  69. IRuntimePlatform platform = null,
  70. IPlatformRenderInterface renderInterface = null,
  71. IRenderTimer renderLoop = null,
  72. ICursorFactory standardCursorFactory = null,
  73. Func<IStyle> theme = null,
  74. IDispatcherImpl dispatcherImpl = null,
  75. IFontManagerImpl fontManagerImpl = null,
  76. ITextShaperImpl textShaperImpl = null,
  77. IWindowImpl windowImpl = null,
  78. IWindowingPlatform windowingPlatform = null)
  79. {
  80. AssetLoader = assetLoader;
  81. InputManager = inputManager;
  82. KeyboardDevice = keyboardDevice;
  83. KeyboardNavigation = keyboardNavigation;
  84. MouseDevice = mouseDevice;
  85. Platform = platform;
  86. RenderInterface = renderInterface;
  87. FontManagerImpl = fontManagerImpl;
  88. TextShaperImpl = textShaperImpl;
  89. StandardCursorFactory = standardCursorFactory;
  90. Theme = theme;
  91. DispatcherImpl = dispatcherImpl;
  92. WindowImpl = windowImpl;
  93. WindowingPlatform = windowingPlatform;
  94. }
  95. internal TestServices(
  96. IGlobalClock globalClock,
  97. IAssetLoader assetLoader = null,
  98. IInputManager inputManager = null,
  99. Func<IKeyboardDevice> keyboardDevice = null,
  100. Func<IKeyboardNavigationHandler> keyboardNavigation = null,
  101. Func<IMouseDevice> mouseDevice = null,
  102. IRuntimePlatform platform = null,
  103. IPlatformRenderInterface renderInterface = null,
  104. IRenderTimer renderLoop = null,
  105. ICursorFactory standardCursorFactory = null,
  106. Func<IStyle> theme = null,
  107. IDispatcherImpl dispatcherImpl = null,
  108. IFontManagerImpl fontManagerImpl = null,
  109. ITextShaperImpl textShaperImpl = null,
  110. IWindowImpl windowImpl = null,
  111. IWindowingPlatform windowingPlatform = null,
  112. IAccessKeyHandler accessKeyHandler = null
  113. ) : this(assetLoader, inputManager, keyboardDevice,
  114. keyboardNavigation,
  115. mouseDevice, platform, renderInterface, renderLoop, standardCursorFactory, theme,
  116. dispatcherImpl, fontManagerImpl, textShaperImpl, windowImpl, windowingPlatform)
  117. {
  118. GlobalClock = globalClock;
  119. AccessKeyHandler = accessKeyHandler;
  120. }
  121. public IAssetLoader AssetLoader { get; }
  122. public IInputManager InputManager { get; }
  123. internal IGlobalClock GlobalClock { get; set; }
  124. internal IAccessKeyHandler AccessKeyHandler { get; }
  125. public Func<IKeyboardDevice> KeyboardDevice { get; }
  126. public Func<IKeyboardNavigationHandler> KeyboardNavigation { get; }
  127. public Func<IMouseDevice> MouseDevice { get; }
  128. public IRuntimePlatform Platform { get; }
  129. public IPlatformRenderInterface RenderInterface { get; }
  130. public IFontManagerImpl FontManagerImpl { get; }
  131. public ITextShaperImpl TextShaperImpl { get; }
  132. public ICursorFactory StandardCursorFactory { get; }
  133. public Func<IStyle> Theme { get; }
  134. public IDispatcherImpl DispatcherImpl { get; }
  135. public IWindowImpl WindowImpl { get; }
  136. public IWindowingPlatform WindowingPlatform { get; }
  137. internal TestServices With(
  138. IAssetLoader assetLoader = null,
  139. IInputManager inputManager = null,
  140. Func<IKeyboardDevice> keyboardDevice = null,
  141. Func<IKeyboardNavigationHandler> keyboardNavigation = null,
  142. Func<IMouseDevice> mouseDevice = null,
  143. IRuntimePlatform platform = null,
  144. IPlatformRenderInterface renderInterface = null,
  145. IRenderTimer renderLoop = null,
  146. IScheduler scheduler = null,
  147. ICursorFactory standardCursorFactory = null,
  148. Func<IStyle> theme = null,
  149. IDispatcherImpl dispatcherImpl = null,
  150. IFontManagerImpl fontManagerImpl = null,
  151. ITextShaperImpl textShaperImpl = null,
  152. IWindowImpl windowImpl = null,
  153. IWindowingPlatform windowingPlatform = null,
  154. IGlobalClock globalClock = null,
  155. IAccessKeyHandler accessKeyHandler = null)
  156. {
  157. return new TestServices(
  158. globalClock ?? GlobalClock,
  159. assetLoader: assetLoader ?? AssetLoader,
  160. inputManager: inputManager ?? InputManager,
  161. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  162. keyboardNavigation: keyboardNavigation ?? KeyboardNavigation,
  163. mouseDevice: mouseDevice ?? MouseDevice,
  164. platform: platform ?? Platform,
  165. renderInterface: renderInterface ?? RenderInterface,
  166. fontManagerImpl: fontManagerImpl ?? FontManagerImpl,
  167. textShaperImpl: textShaperImpl ?? TextShaperImpl,
  168. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  169. theme: theme ?? Theme,
  170. dispatcherImpl: dispatcherImpl ?? DispatcherImpl,
  171. windowingPlatform: windowingPlatform ?? WindowingPlatform,
  172. windowImpl: windowImpl ?? WindowImpl,
  173. accessKeyHandler: accessKeyHandler ?? AccessKeyHandler
  174. );
  175. }
  176. private static IStyle CreateSimpleTheme()
  177. {
  178. return new SimpleTheme();
  179. }
  180. }
  181. }