TestRoot.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Layout;
  6. using Avalonia.LogicalTree;
  7. using Avalonia.Platform;
  8. using Avalonia.Rendering;
  9. using Avalonia.Styling;
  10. using Avalonia.VisualTree;
  11. using Moq;
  12. namespace Avalonia.UnitTests
  13. {
  14. public class TestRoot : Decorator, IFocusScope, ILayoutRoot, IInputRoot, IRenderRoot, IStyleHost, ILogicalRoot
  15. {
  16. private readonly NameScope _nameScope = new NameScope();
  17. private FocusManager? _focusManager;
  18. public TestRoot()
  19. {
  20. Renderer = RendererMocks.CreateRenderer().Object;
  21. HitTester = new NullHitTester();
  22. LayoutManager = new LayoutManager(this);
  23. IsVisible = true;
  24. KeyboardNavigation.SetTabNavigation(this, KeyboardNavigationMode.Cycle);
  25. }
  26. class NullHitTester : IHitTester
  27. {
  28. public IEnumerable<Visual> HitTest(Point p, Visual root, Func<Visual, bool> filter) => Array.Empty<Visual>();
  29. public Visual HitTestFirst(Point p, Visual root, Func<Visual, bool> filter) => null;
  30. }
  31. public TestRoot(Control child)
  32. : this(false, child)
  33. {
  34. }
  35. public TestRoot(bool useGlobalStyles, Control child)
  36. : this()
  37. {
  38. if (useGlobalStyles)
  39. {
  40. StylingParent = UnitTestApplication.Current;
  41. }
  42. Child = child;
  43. }
  44. public Size ClientSize { get; set; } = new Size(1000, 1000);
  45. public double LayoutScaling { get; set; } = 1;
  46. internal ILayoutManager LayoutManager { get; set; }
  47. ILayoutManager ILayoutRoot.LayoutManager => LayoutManager;
  48. public double RenderScaling => 1;
  49. internal IRenderer Renderer { get; set; }
  50. internal IHitTester HitTester { get; set; }
  51. IRenderer IRenderRoot.Renderer => Renderer;
  52. IHitTester IRenderRoot.HitTester => HitTester;
  53. public IKeyboardNavigationHandler KeyboardNavigationHandler => null;
  54. public IFocusManager FocusManager => _focusManager ??= new FocusManager(this);
  55. public IPlatformSettings PlatformSettings => AvaloniaLocator.Current.GetService<IPlatformSettings>();
  56. public IInputElement PointerOverElement { get; set; }
  57. public bool ShowAccessKeys { get; set; }
  58. public IStyleHost StylingParent { get; set; }
  59. IStyleHost IStyleHost.StylingParent => StylingParent;
  60. public IRenderTarget CreateRenderTarget()
  61. {
  62. var dc = new Mock<IDrawingContextImpl>();
  63. dc.Setup(x => x.CreateLayer(It.IsAny<PixelSize>())).Returns(() =>
  64. {
  65. var layerDc = new Mock<IDrawingContextImpl>();
  66. var layer = new Mock<IDrawingContextLayerImpl>();
  67. layer.Setup(x => x.CreateDrawingContext(It.IsAny<bool>())).Returns(layerDc.Object);
  68. return layer.Object;
  69. });
  70. var result = new Mock<IRenderTarget>();
  71. result.Setup(x => x.CreateDrawingContext(It.IsAny<bool>())).Returns(dc.Object);
  72. return result.Object;
  73. }
  74. public void ExecuteInitialLayoutPass() => LayoutManager.ExecuteInitialLayoutPass();
  75. public void Invalidate(Rect rect)
  76. {
  77. }
  78. public Point PointToClient(PixelPoint p) => p.ToPoint(1);
  79. public PixelPoint PointToScreen(Point p) => PixelPoint.FromPoint(p, 1);
  80. public void RegisterChildrenNames()
  81. {
  82. var scope = NameScope.GetNameScope(this) ?? new NameScope();
  83. NameScope.SetNameScope(this, scope);
  84. void Visit(StyledElement element, bool force = false)
  85. {
  86. if (element.Name != null)
  87. {
  88. if (scope.Find(element.Name) != element)
  89. scope.Register(element.Name, element);
  90. }
  91. if(element is Visual visual && (force || NameScope.GetNameScope(element) == null))
  92. foreach(var child in visual.GetVisualChildren())
  93. if (child is StyledElement styledChild)
  94. Visit(styledChild);
  95. }
  96. Visit(this, true);
  97. }
  98. protected override Size MeasureOverride(Size availableSize)
  99. {
  100. return base.MeasureOverride(ClientSize);
  101. }
  102. }
  103. }