TestServices.cs 6.6 KB

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