TestServices.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.Shared.PlatformSupport;
  9. using Avalonia.Styling;
  10. using Avalonia.Themes.Default;
  11. using Avalonia.Rendering;
  12. using System.Reactive.Concurrency;
  13. using System.Collections.Generic;
  14. using Avalonia.Controls;
  15. using System.Reflection;
  16. using Avalonia.Animation;
  17. namespace Avalonia.UnitTests
  18. {
  19. public class TestServices
  20. {
  21. public static readonly TestServices StyledWindow = new TestServices(
  22. assetLoader: new AssetLoader(),
  23. platform: new AppBuilder().RuntimePlatform,
  24. renderInterface: new MockPlatformRenderInterface(),
  25. standardCursorFactory: Mock.Of<ICursorFactory>(),
  26. styler: new Styler(),
  27. theme: () => CreateDefaultTheme(),
  28. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true),
  29. fontManagerImpl: new MockFontManagerImpl(),
  30. textShaperImpl: new MockTextShaperImpl(),
  31. windowingPlatform: new MockWindowingPlatform());
  32. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  33. assetLoader: new AssetLoader(),
  34. renderInterface: new MockPlatformRenderInterface(),
  35. fontManagerImpl: new MockFontManagerImpl(),
  36. textShaperImpl: new MockTextShaperImpl());
  37. public static readonly TestServices MockPlatformWrapper = new TestServices(
  38. platform: Mock.Of<IRuntimePlatform>());
  39. public static readonly TestServices MockStyler = new TestServices(
  40. styler: Mock.Of<IStyler>());
  41. public static readonly TestServices MockThreadingInterface = new TestServices(
  42. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true));
  43. public static readonly TestServices MockWindowingPlatform = new TestServices(
  44. windowingPlatform: new MockWindowingPlatform());
  45. public static readonly TestServices RealFocus = new TestServices(
  46. focusManager: new FocusManager(),
  47. keyboardDevice: () => new KeyboardDevice(),
  48. keyboardNavigation: new KeyboardNavigationHandler(),
  49. inputManager: new InputManager());
  50. public static readonly TestServices RealStyler = new TestServices(
  51. styler: new Styler());
  52. public static readonly TestServices TextServices = new TestServices(
  53. assetLoader: new AssetLoader(),
  54. renderInterface: new MockPlatformRenderInterface(),
  55. fontManagerImpl: new HarfBuzzFontManagerImpl(),
  56. textShaperImpl: new HarfBuzzTextShaperImpl());
  57. public TestServices(
  58. IAssetLoader assetLoader = null,
  59. IFocusManager focusManager = null,
  60. IGlobalClock globalClock = null,
  61. IInputManager inputManager = null,
  62. Func<IKeyboardDevice> keyboardDevice = null,
  63. IKeyboardNavigationHandler keyboardNavigation = null,
  64. Func<IMouseDevice> mouseDevice = null,
  65. IRuntimePlatform platform = null,
  66. IPlatformRenderInterface renderInterface = null,
  67. IRenderTimer renderLoop = null,
  68. IScheduler scheduler = null,
  69. ICursorFactory standardCursorFactory = null,
  70. IStyler styler = null,
  71. Func<Styles> theme = null,
  72. IPlatformThreadingInterface threadingInterface = null,
  73. IFontManagerImpl fontManagerImpl = null,
  74. ITextShaperImpl textShaperImpl = null,
  75. IWindowImpl windowImpl = null,
  76. IWindowingPlatform windowingPlatform = null)
  77. {
  78. AssetLoader = assetLoader;
  79. FocusManager = focusManager;
  80. GlobalClock = globalClock;
  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. Scheduler = scheduler;
  90. StandardCursorFactory = standardCursorFactory;
  91. Styler = styler;
  92. Theme = theme;
  93. ThreadingInterface = threadingInterface;
  94. WindowImpl = windowImpl;
  95. WindowingPlatform = windowingPlatform;
  96. }
  97. public IAssetLoader AssetLoader { get; }
  98. public IInputManager InputManager { get; }
  99. public IFocusManager FocusManager { get; }
  100. public IGlobalClock GlobalClock { get; }
  101. public Func<IKeyboardDevice> KeyboardDevice { get; }
  102. public IKeyboardNavigationHandler KeyboardNavigation { get; }
  103. public Func<IMouseDevice> MouseDevice { get; }
  104. public IRuntimePlatform Platform { get; }
  105. public IPlatformRenderInterface RenderInterface { get; }
  106. public IFontManagerImpl FontManagerImpl { get; }
  107. public ITextShaperImpl TextShaperImpl { get; }
  108. public IScheduler Scheduler { get; }
  109. public ICursorFactory StandardCursorFactory { get; }
  110. public IStyler Styler { get; }
  111. public Func<Styles> Theme { get; }
  112. public IPlatformThreadingInterface ThreadingInterface { get; }
  113. public IWindowImpl WindowImpl { get; }
  114. public IWindowingPlatform WindowingPlatform { get; }
  115. public TestServices With(
  116. IAssetLoader assetLoader = null,
  117. IFocusManager focusManager = null,
  118. IGlobalClock globalClock = null,
  119. IInputManager inputManager = null,
  120. Func<IKeyboardDevice> keyboardDevice = null,
  121. IKeyboardNavigationHandler keyboardNavigation = null,
  122. Func<IMouseDevice> mouseDevice = null,
  123. IRuntimePlatform platform = null,
  124. IPlatformRenderInterface renderInterface = null,
  125. IRenderTimer renderLoop = null,
  126. IScheduler scheduler = null,
  127. ICursorFactory standardCursorFactory = null,
  128. IStyler styler = null,
  129. Func<Styles> theme = null,
  130. IPlatformThreadingInterface threadingInterface = null,
  131. IFontManagerImpl fontManagerImpl = null,
  132. ITextShaperImpl textShaperImpl = null,
  133. IWindowImpl windowImpl = null,
  134. IWindowingPlatform windowingPlatform = null)
  135. {
  136. return new TestServices(
  137. assetLoader: assetLoader ?? AssetLoader,
  138. focusManager: focusManager ?? FocusManager,
  139. globalClock: globalClock ?? GlobalClock,
  140. inputManager: inputManager ?? InputManager,
  141. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  142. keyboardNavigation: keyboardNavigation ?? KeyboardNavigation,
  143. mouseDevice: mouseDevice ?? MouseDevice,
  144. platform: platform ?? Platform,
  145. renderInterface: renderInterface ?? RenderInterface,
  146. fontManagerImpl: fontManagerImpl ?? FontManagerImpl,
  147. textShaperImpl: textShaperImpl ?? TextShaperImpl,
  148. scheduler: scheduler ?? Scheduler,
  149. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  150. styler: styler ?? Styler,
  151. theme: theme ?? Theme,
  152. threadingInterface: threadingInterface ?? ThreadingInterface,
  153. windowingPlatform: windowingPlatform ?? WindowingPlatform,
  154. windowImpl: windowImpl ?? WindowImpl);
  155. }
  156. private static Styles CreateDefaultTheme()
  157. {
  158. var result = new Styles
  159. {
  160. new DefaultTheme(),
  161. };
  162. var baseLight = (IStyle)AvaloniaXamlLoader.Load(
  163. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"));
  164. result.Add(baseLight);
  165. return result;
  166. }
  167. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  168. {
  169. return Mock.Of<IPlatformRenderInterface>(x =>
  170. x.CreateFormattedText(
  171. It.IsAny<string>(),
  172. It.IsAny<Typeface>(),
  173. It.IsAny<double>(),
  174. It.IsAny<TextAlignment>(),
  175. It.IsAny<TextWrapping>(),
  176. It.IsAny<Size>(),
  177. It.IsAny<IReadOnlyList<FormattedTextStyleSpan>>()) == Mock.Of<IFormattedTextImpl>() &&
  178. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  179. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  180. }
  181. }
  182. public class AppBuilder : AppBuilderBase<AppBuilder>
  183. {
  184. public AppBuilder()
  185. : base(new StandardRuntimePlatform(),
  186. builder => StandardRuntimePlatformServices.Register(builder.Instance?.GetType()
  187. ?.GetTypeInfo().Assembly))
  188. {
  189. }
  190. protected override bool CheckSetup => false;
  191. }
  192. }