TestServices.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Moq;
  5. using Avalonia.Input;
  6. using Avalonia.Layout;
  7. using Avalonia.Markup.Xaml;
  8. using Avalonia.Media;
  9. using Avalonia.Platform;
  10. using Avalonia.Shared.PlatformSupport;
  11. using Avalonia.Styling;
  12. using Avalonia.Themes.Default;
  13. using Avalonia.Rendering;
  14. using System.Reactive.Concurrency;
  15. using System.Collections.Generic;
  16. using Avalonia.Controls;
  17. using System.Reflection;
  18. using Avalonia.Animation;
  19. namespace Avalonia.UnitTests
  20. {
  21. public class TestServices
  22. {
  23. public static readonly TestServices StyledWindow = new TestServices(
  24. assetLoader: new AssetLoader(),
  25. platform: new AppBuilder().RuntimePlatform,
  26. renderInterface: new MockPlatformRenderInterface(),
  27. standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
  28. styler: new Styler(),
  29. theme: () => CreateDefaultTheme(),
  30. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true),
  31. windowingPlatform: new MockWindowingPlatform());
  32. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  33. renderInterface: new MockPlatformRenderInterface());
  34. public static readonly TestServices MockPlatformWrapper = new TestServices(
  35. platform: Mock.Of<IRuntimePlatform>());
  36. public static readonly TestServices MockStyler = new TestServices(
  37. styler: Mock.Of<IStyler>());
  38. public static readonly TestServices MockThreadingInterface = new TestServices(
  39. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true));
  40. public static readonly TestServices MockWindowingPlatform = new TestServices(
  41. windowingPlatform: new MockWindowingPlatform());
  42. public static readonly TestServices RealFocus = new TestServices(
  43. focusManager: new FocusManager(),
  44. keyboardDevice: () => new KeyboardDevice(),
  45. keyboardNavigation: new KeyboardNavigationHandler(),
  46. inputManager: new InputManager());
  47. public static readonly TestServices RealStyler = new TestServices(
  48. styler: new Styler());
  49. public TestServices(
  50. IAssetLoader assetLoader = null,
  51. IFocusManager focusManager = null,
  52. IGlobalClock globalClock = null,
  53. IInputManager inputManager = null,
  54. Func<IKeyboardDevice> keyboardDevice = null,
  55. IKeyboardNavigationHandler keyboardNavigation = null,
  56. Func<IMouseDevice> mouseDevice = null,
  57. IRuntimePlatform platform = null,
  58. IPlatformRenderInterface renderInterface = null,
  59. IRenderTimer renderLoop = null,
  60. IScheduler scheduler = null,
  61. IStandardCursorFactory standardCursorFactory = null,
  62. IStyler styler = null,
  63. Func<Styles> theme = null,
  64. IPlatformThreadingInterface threadingInterface = null,
  65. IWindowImpl windowImpl = null,
  66. IWindowingPlatform windowingPlatform = null)
  67. {
  68. AssetLoader = assetLoader;
  69. FocusManager = focusManager;
  70. GlobalClock = globalClock;
  71. InputManager = inputManager;
  72. KeyboardDevice = keyboardDevice;
  73. KeyboardNavigation = keyboardNavigation;
  74. MouseDevice = mouseDevice;
  75. Platform = platform;
  76. RenderInterface = renderInterface;
  77. Scheduler = scheduler;
  78. StandardCursorFactory = standardCursorFactory;
  79. Styler = styler;
  80. Theme = theme;
  81. ThreadingInterface = threadingInterface;
  82. WindowImpl = windowImpl;
  83. WindowingPlatform = windowingPlatform;
  84. }
  85. public IAssetLoader AssetLoader { get; }
  86. public IInputManager InputManager { get; }
  87. public IFocusManager FocusManager { get; }
  88. public IGlobalClock GlobalClock { get; }
  89. public Func<IKeyboardDevice> KeyboardDevice { get; }
  90. public IKeyboardNavigationHandler KeyboardNavigation { get; }
  91. public Func<IMouseDevice> MouseDevice { get; }
  92. public IRuntimePlatform Platform { get; }
  93. public IPlatformRenderInterface RenderInterface { get; }
  94. public IScheduler Scheduler { get; }
  95. public IStandardCursorFactory StandardCursorFactory { get; }
  96. public IStyler Styler { get; }
  97. public Func<Styles> Theme { get; }
  98. public IPlatformThreadingInterface ThreadingInterface { get; }
  99. public IWindowImpl WindowImpl { get; }
  100. public IWindowingPlatform WindowingPlatform { get; }
  101. public TestServices With(
  102. IAssetLoader assetLoader = null,
  103. IFocusManager focusManager = null,
  104. IGlobalClock globalClock = null,
  105. IInputManager inputManager = null,
  106. Func<IKeyboardDevice> keyboardDevice = null,
  107. IKeyboardNavigationHandler keyboardNavigation = null,
  108. Func<IMouseDevice> mouseDevice = null,
  109. IRuntimePlatform platform = null,
  110. IPlatformRenderInterface renderInterface = null,
  111. IRenderTimer renderLoop = null,
  112. IScheduler scheduler = null,
  113. IStandardCursorFactory standardCursorFactory = null,
  114. IStyler styler = null,
  115. Func<Styles> theme = null,
  116. IPlatformThreadingInterface threadingInterface = null,
  117. IWindowImpl windowImpl = null,
  118. IWindowingPlatform windowingPlatform = null)
  119. {
  120. return new TestServices(
  121. assetLoader: assetLoader ?? AssetLoader,
  122. focusManager: focusManager ?? FocusManager,
  123. globalClock: globalClock ?? GlobalClock,
  124. inputManager: inputManager ?? InputManager,
  125. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  126. keyboardNavigation: keyboardNavigation ?? KeyboardNavigation,
  127. mouseDevice: mouseDevice ?? MouseDevice,
  128. platform: platform ?? Platform,
  129. renderInterface: renderInterface ?? RenderInterface,
  130. scheduler: scheduler ?? Scheduler,
  131. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  132. styler: styler ?? Styler,
  133. theme: theme ?? Theme,
  134. threadingInterface: threadingInterface ?? ThreadingInterface,
  135. windowingPlatform: windowingPlatform ?? WindowingPlatform,
  136. windowImpl: windowImpl ?? WindowImpl);
  137. }
  138. private static Styles CreateDefaultTheme()
  139. {
  140. var result = new Styles
  141. {
  142. new DefaultTheme(),
  143. };
  144. var loader = new AvaloniaXamlLoader();
  145. var baseLight = (IStyle)loader.Load(
  146. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"));
  147. result.Add(baseLight);
  148. return result;
  149. }
  150. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  151. {
  152. return Mock.Of<IPlatformRenderInterface>(x =>
  153. x.CreateFormattedText(
  154. It.IsAny<string>(),
  155. It.IsAny<Typeface>(),
  156. It.IsAny<TextAlignment>(),
  157. It.IsAny<TextWrapping>(),
  158. It.IsAny<Size>(),
  159. It.IsAny<IReadOnlyList<FormattedTextStyleSpan>>()) == Mock.Of<IFormattedTextImpl>() &&
  160. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  161. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  162. }
  163. }
  164. public class AppBuilder : AppBuilderBase<AppBuilder>
  165. {
  166. public AppBuilder()
  167. : base(new StandardRuntimePlatform(),
  168. builder => StandardRuntimePlatformServices.Register(builder.Instance?.GetType()
  169. ?.GetTypeInfo().Assembly))
  170. {
  171. }
  172. protected override bool CheckSetup => false;
  173. }
  174. }