TestRoot.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Layout;
  5. using Avalonia.LogicalTree;
  6. using Avalonia.Media;
  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. public TestRoot()
  18. {
  19. Renderer = Mock.Of<IRenderer>();
  20. LayoutManager = new LayoutManager(this);
  21. IsVisible = true;
  22. KeyboardNavigation.SetTabNavigation(this, KeyboardNavigationMode.Cycle);
  23. }
  24. public TestRoot(IControl child)
  25. : this(false, child)
  26. {
  27. Child = child;
  28. }
  29. public TestRoot(bool useGlobalStyles, IControl child)
  30. : this()
  31. {
  32. if (useGlobalStyles)
  33. {
  34. StylingParent = UnitTestApplication.Current;
  35. }
  36. Child = child;
  37. }
  38. public Size ClientSize { get; set; } = new Size(1000, 1000);
  39. public Size MaxClientSize { get; set; } = Size.Infinity;
  40. public double LayoutScaling { get; set; } = 1;
  41. public ILayoutManager LayoutManager { get; set; }
  42. public double RenderScaling => 1;
  43. public IRenderer Renderer { get; set; }
  44. public IAccessKeyHandler AccessKeyHandler => null;
  45. public IKeyboardNavigationHandler KeyboardNavigationHandler => null;
  46. public IInputElement PointerOverElement { get; set; }
  47. public IMouseDevice MouseDevice { get; set; }
  48. public bool ShowAccessKeys { get; set; }
  49. public IStyleHost StylingParent { get; set; }
  50. IStyleHost IStyleHost.StylingParent => StylingParent;
  51. public IRenderTarget CreateRenderTarget()
  52. {
  53. var dc = new Mock<IDrawingContextImpl>();
  54. dc.Setup(x => x.CreateLayer(It.IsAny<Size>())).Returns(() =>
  55. {
  56. var layerDc = new Mock<IDrawingContextImpl>();
  57. var layer = new Mock<IDrawingContextLayerImpl>();
  58. layer.Setup(x => x.CreateDrawingContext(It.IsAny<IVisualBrushRenderer>())).Returns(layerDc.Object);
  59. return layer.Object;
  60. });
  61. var result = new Mock<IRenderTarget>();
  62. result.Setup(x => x.CreateDrawingContext(It.IsAny<IVisualBrushRenderer>())).Returns(dc.Object);
  63. return result.Object;
  64. }
  65. public void Invalidate(Rect rect)
  66. {
  67. }
  68. public Point PointToClient(PixelPoint p) => p.ToPoint(1);
  69. public PixelPoint PointToScreen(Point p) => PixelPoint.FromPoint(p, 1);
  70. public void RegisterChildrenNames()
  71. {
  72. var scope = NameScope.GetNameScope(this) ?? new NameScope();
  73. NameScope.SetNameScope(this, scope);
  74. void Visit(StyledElement element, bool force = false)
  75. {
  76. if (element.Name != null)
  77. {
  78. if (scope.Find(element.Name) != element)
  79. scope.Register(element.Name, element);
  80. }
  81. if(element is IVisual visual && (force || NameScope.GetNameScope(element) == null))
  82. foreach(var child in visual.GetVisualChildren())
  83. if (child is StyledElement styledChild)
  84. Visit(styledChild);
  85. }
  86. Visit(this, true);
  87. }
  88. protected override Size MeasureOverride(Size availableSize)
  89. {
  90. return base.MeasureOverride(ClientSize);
  91. }
  92. }
  93. }