TestServices.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. namespace Avalonia.UnitTests
  16. {
  17. public class TestServices
  18. {
  19. public static readonly TestServices StyledWindow = new TestServices(
  20. assetLoader: new AssetLoader(),
  21. layoutManager: new LayoutManager(),
  22. platform: new AppBuilder().RuntimePlatform,
  23. renderer: Mock.Of<IRenderer>(),
  24. renderInterface: CreateRenderInterfaceMock(),
  25. renderLoop: Mock.Of<IRenderLoop>(),
  26. standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
  27. styler: new Styler(),
  28. theme: () => CreateDefaultTheme(),
  29. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true),
  30. windowingPlatform: new MockWindowingPlatform());
  31. public static readonly TestServices MockPlatformRenderInterface = new TestServices(
  32. renderInterface: CreateRenderInterfaceMock());
  33. public static readonly TestServices MockPlatformWrapper = new TestServices(
  34. platform: Mock.Of<IRuntimePlatform>());
  35. public static readonly TestServices MockStyler = new TestServices(
  36. styler: Mock.Of<IStyler>());
  37. public static readonly TestServices MockThreadingInterface = new TestServices(
  38. threadingInterface: Mock.Of<IPlatformThreadingInterface>(x => x.CurrentThreadIsLoopThread == true));
  39. public static readonly TestServices RealFocus = new TestServices(
  40. focusManager: new FocusManager(),
  41. keyboardDevice: () => new KeyboardDevice(),
  42. inputManager: new InputManager());
  43. public static readonly TestServices RealLayoutManager = new TestServices(
  44. layoutManager: new LayoutManager());
  45. public static readonly TestServices RealStyler = new TestServices(
  46. styler: new Styler());
  47. public TestServices(
  48. IAssetLoader assetLoader = null,
  49. IFocusManager focusManager = null,
  50. IInputManager inputManager = null,
  51. Func<IKeyboardDevice> keyboardDevice = null,
  52. ILayoutManager layoutManager = null,
  53. IRuntimePlatform platform = null,
  54. IRenderer renderer = null,
  55. IPlatformRenderInterface renderInterface = null,
  56. IRenderLoop renderLoop = null,
  57. IScheduler scheduler = null,
  58. IStandardCursorFactory standardCursorFactory = null,
  59. IStyler styler = null,
  60. Func<Styles> theme = null,
  61. IPlatformThreadingInterface threadingInterface = null,
  62. IWindowImpl windowImpl = null,
  63. IWindowingPlatform windowingPlatform = null)
  64. {
  65. AssetLoader = assetLoader;
  66. FocusManager = focusManager;
  67. InputManager = inputManager;
  68. KeyboardDevice = keyboardDevice;
  69. LayoutManager = layoutManager;
  70. Platform = platform;
  71. Renderer = renderer;
  72. RenderInterface = renderInterface;
  73. RenderLoop = renderLoop;
  74. Scheduler = scheduler;
  75. StandardCursorFactory = standardCursorFactory;
  76. Styler = styler;
  77. Theme = theme;
  78. ThreadingInterface = threadingInterface;
  79. WindowImpl = windowImpl;
  80. WindowingPlatform = windowingPlatform;
  81. }
  82. public IAssetLoader AssetLoader { get; }
  83. public IInputManager InputManager { get; }
  84. public IFocusManager FocusManager { get; }
  85. public Func<IKeyboardDevice> KeyboardDevice { get; }
  86. public ILayoutManager LayoutManager { get; }
  87. public IRuntimePlatform Platform { get; }
  88. public IRenderer Renderer { get; }
  89. public IPlatformRenderInterface RenderInterface { get; }
  90. public IRenderLoop RenderLoop { get; }
  91. public IScheduler Scheduler { get; }
  92. public IStandardCursorFactory StandardCursorFactory { get; }
  93. public IStyler Styler { get; }
  94. public Func<Styles> Theme { get; }
  95. public IPlatformThreadingInterface ThreadingInterface { get; }
  96. public IWindowImpl WindowImpl { get; }
  97. public IWindowingPlatform WindowingPlatform { get; }
  98. public TestServices With(
  99. IAssetLoader assetLoader = null,
  100. IFocusManager focusManager = null,
  101. IInputManager inputManager = null,
  102. Func<IKeyboardDevice> keyboardDevice = null,
  103. ILayoutManager layoutManager = null,
  104. IRuntimePlatform platform = null,
  105. IRenderer renderer = null,
  106. IPlatformRenderInterface renderInterface = null,
  107. IRenderLoop renderLoop = null,
  108. IScheduler scheduler = null,
  109. IStandardCursorFactory standardCursorFactory = null,
  110. IStyler styler = null,
  111. Func<Styles> theme = null,
  112. IPlatformThreadingInterface threadingInterface = null,
  113. IWindowImpl windowImpl = null,
  114. IWindowingPlatform windowingPlatform = null)
  115. {
  116. return new TestServices(
  117. assetLoader: assetLoader ?? AssetLoader,
  118. focusManager: focusManager ?? FocusManager,
  119. inputManager: inputManager ?? InputManager,
  120. keyboardDevice: keyboardDevice ?? KeyboardDevice,
  121. layoutManager: layoutManager ?? LayoutManager,
  122. platform: platform ?? Platform,
  123. renderer: renderer ?? Renderer,
  124. renderInterface: renderInterface ?? RenderInterface,
  125. renderLoop: renderLoop ?? RenderLoop,
  126. scheduler: scheduler ?? Scheduler,
  127. standardCursorFactory: standardCursorFactory ?? StandardCursorFactory,
  128. styler: styler ?? Styler,
  129. theme: theme ?? Theme,
  130. threadingInterface: threadingInterface ?? ThreadingInterface,
  131. windowImpl: windowImpl ?? WindowImpl,
  132. windowingPlatform: windowingPlatform ?? WindowingPlatform);
  133. }
  134. private static Styles CreateDefaultTheme()
  135. {
  136. var result = new Styles
  137. {
  138. new DefaultTheme(),
  139. };
  140. var loader = new AvaloniaXamlLoader();
  141. var baseLight = (IStyle)loader.Load(
  142. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"));
  143. result.Add(baseLight);
  144. return result;
  145. }
  146. private static IPlatformRenderInterface CreateRenderInterfaceMock()
  147. {
  148. return Mock.Of<IPlatformRenderInterface>(x =>
  149. x.CreateFormattedText(
  150. It.IsAny<string>(),
  151. It.IsAny<string>(),
  152. It.IsAny<double>(),
  153. It.IsAny<FontStyle>(),
  154. It.IsAny<TextAlignment>(),
  155. It.IsAny<FontWeight>(),
  156. It.IsAny<TextWrapping>()) == Mock.Of<IFormattedTextImpl>() &&
  157. x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
  158. y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
  159. }
  160. }
  161. }