TestServices.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. namespace Avalonia.UnitTests
  17. {
  18. public class TestServices
  19. {
  20. public static readonly TestServices StyledWindow = new TestServices(
  21. assetLoader: new AssetLoader(),
  22. platform: new StandardRuntimePlatform(),
  23. renderInterface: new MockPlatformRenderInterface(),
  24. standardCursorFactory: Mock.Of<ICursorFactory>(),
  25. theme: () => CreateSimpleTheme(),
  26. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true),
  27. fontManagerImpl: new MockFontManagerImpl(),
  28. textShaperImpl: new MockTextShaperImpl(),
  29. windowingPlatform: new MockWindowingPlatform());
  30. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  31. assetLoader: new AssetLoader(),
  32. renderInterface: new MockPlatformRenderInterface(),
  33. fontManagerImpl: new MockFontManagerImpl(),
  34. textShaperImpl: new MockTextShaperImpl());
  35. public static readonly TestServices MockPlatformWrapper = new TestServices(
  36. platform: Mock.Of<IRuntimePlatform>());
  37. public static readonly TestServices MockThreadingInterface = new TestServices(
  38. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true));
  39. public static readonly TestServices MockWindowingPlatform = new TestServices(
  40. windowingPlatform: new MockWindowingPlatform());
  41. public static readonly TestServices RealFocus = new TestServices(
  42. focusManager: new FocusManager(),
  43. keyboardDevice: () => new KeyboardDevice(),
  44. keyboardNavigation: new KeyboardNavigationHandler(),
  45. inputManager: new InputManager(),
  46. assetLoader: new AssetLoader(),
  47. renderInterface: new MockPlatformRenderInterface(),
  48. fontManagerImpl: new MockFontManagerImpl(),
  49. textShaperImpl: new MockTextShaperImpl());
  50. public static readonly TestServices TextServices = new TestServices(
  51. assetLoader: new AssetLoader(),
  52. renderInterface: new MockPlatformRenderInterface(),
  53. fontManagerImpl: new HarfBuzzFontManagerImpl(),
  54. textShaperImpl: new HarfBuzzTextShaperImpl());
  55. public TestServices(
  56. IAssetLoader assetLoader = null,
  57. IFocusManager focusManager = null,
  58. IGlobalClock globalClock = null,
  59. IInputManager inputManager = null,
  60. Func<IKeyboardDevice> keyboardDevice = null,
  61. IKeyboardNavigationHandler keyboardNavigation = null,
  62. Func<IMouseDevice> mouseDevice = null,
  63. IRuntimePlatform platform = null,
  64. IPlatformRenderInterface renderInterface = null,
  65. IRenderTimer renderLoop = null,
  66. ICursorFactory standardCursorFactory = null,
  67. Func<IStyle> theme = null,
  68. IPlatformThreadingInterface threadingInterface = null,
  69. IFontManagerImpl fontManagerImpl = null,
  70. ITextShaperImpl textShaperImpl = null,
  71. IWindowImpl windowImpl = null,
  72. IWindowingPlatform windowingPlatform = null)
  73. {
  74. AssetLoader = assetLoader;
  75. FocusManager = focusManager;
  76. GlobalClock = globalClock;
  77. InputManager = inputManager;
  78. KeyboardDevice = keyboardDevice;
  79. KeyboardNavigation = keyboardNavigation;
  80. MouseDevice = mouseDevice;
  81. Platform = platform;
  82. RenderInterface = renderInterface;
  83. FontManagerImpl = fontManagerImpl;
  84. TextShaperImpl = textShaperImpl;
  85. StandardCursorFactory = standardCursorFactory;
  86. Theme = theme;
  87. ThreadingInterface = threadingInterface;
  88. WindowImpl = windowImpl;
  89. WindowingPlatform = windowingPlatform;
  90. }
  91. public IAssetLoader AssetLoader { get; }
  92. public IInputManager InputManager { get; }
  93. public IFocusManager FocusManager { get; }
  94. public IGlobalClock GlobalClock { get; }
  95. public Func<IKeyboardDevice> KeyboardDevice { get; }
  96. public IKeyboardNavigationHandler KeyboardNavigation { get; }
  97. public Func<IMouseDevice> MouseDevice { get; }
  98. public IRuntimePlatform Platform { get; }
  99. public IPlatformRenderInterface RenderInterface { get; }
  100. public IFontManagerImpl FontManagerImpl { get; }
  101. public ITextShaperImpl TextShaperImpl { get; }
  102. public ICursorFactory StandardCursorFactory { get; }
  103. public Func<IStyle> Theme { get; }
  104. public IPlatformThreadingInterface ThreadingInterface { get; }
  105. public IWindowImpl WindowImpl { get; }
  106. public IWindowingPlatform WindowingPlatform { get; }
  107. public TestServices With(
  108. IAssetLoader assetLoader = null,
  109. IFocusManager focusManager = null,
  110. IGlobalClock globalClock = null,
  111. IInputManager inputManager = null,
  112. Func<IKeyboardDevice> keyboardDevice = null,
  113. IKeyboardNavigationHandler keyboardNavigation = null,
  114. Func<IMouseDevice> mouseDevice = null,
  115. IRuntimePlatform platform = null,
  116. IPlatformRenderInterface renderInterface = null,
  117. IRenderTimer renderLoop = null,
  118. IScheduler scheduler = null,
  119. ICursorFactory standardCursorFactory = null,
  120. Func<IStyle> theme = null,
  121. IPlatformThreadingInterface threadingInterface = null,
  122. IFontManagerImpl fontManagerImpl = null,
  123. ITextShaperImpl textShaperImpl = null,
  124. IWindowImpl windowImpl = null,
  125. IWindowingPlatform windowingPlatform = null)
  126. {
  127. return new TestServices(
  128. assetLoader: assetLoader ?? AssetLoader,
  129. focusManager: focusManager ?? FocusManager,
  130. globalClock: globalClock ?? GlobalClock,
  131. inputManager: inputManager ?? InputManager,
  132. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  133. keyboardNavigation: keyboardNavigation ?? KeyboardNavigation,
  134. mouseDevice: mouseDevice ?? MouseDevice,
  135. platform: platform ?? Platform,
  136. renderInterface: renderInterface ?? RenderInterface,
  137. fontManagerImpl: fontManagerImpl ?? FontManagerImpl,
  138. textShaperImpl: textShaperImpl ?? TextShaperImpl,
  139. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  140. theme: theme ?? Theme,
  141. threadingInterface: threadingInterface ?? ThreadingInterface,
  142. windowingPlatform: windowingPlatform ?? WindowingPlatform,
  143. windowImpl: windowImpl ?? WindowImpl);
  144. }
  145. private static IStyle CreateSimpleTheme()
  146. {
  147. return new SimpleTheme { Mode = SimpleThemeMode.Light };
  148. }
  149. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  150. {
  151. return Mock.Of<IPlatformRenderInterface>(x =>
  152. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  153. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  154. }
  155. }
  156. }