TestRoot.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Avalonia.Controls;
  5. using Avalonia.Input;
  6. using Avalonia.Layout;
  7. using Avalonia.LogicalTree;
  8. using Avalonia.Media;
  9. using Avalonia.Platform;
  10. using Avalonia.Rendering;
  11. using Avalonia.Styling;
  12. using Avalonia.VisualTree;
  13. using Moq;
  14. namespace Avalonia.UnitTests
  15. {
  16. public class TestRoot : Decorator, IFocusScope, ILayoutRoot, IInputRoot, IRenderRoot, IStyleHost, ILogicalRoot
  17. {
  18. private readonly NameScope _nameScope = new NameScope();
  19. public TestRoot()
  20. {
  21. Renderer = Mock.Of<IRenderer>();
  22. }
  23. public TestRoot(IControl child)
  24. : this(false, child)
  25. {
  26. Child = child;
  27. }
  28. public TestRoot(bool useGlobalStyles, IControl child)
  29. : this()
  30. {
  31. if (useGlobalStyles)
  32. {
  33. StylingParent = UnitTestApplication.Current;
  34. }
  35. Child = child;
  36. }
  37. public Size ClientSize { get; set; } = new Size(100, 100);
  38. public Size MaxClientSize { get; set; } = Size.Infinity;
  39. public double LayoutScaling => 1;
  40. public ILayoutManager LayoutManager { get; set; } = new LayoutManager();
  41. public double RenderScaling => 1;
  42. public IRenderer Renderer { get; set; }
  43. public IAccessKeyHandler AccessKeyHandler => null;
  44. public IKeyboardNavigationHandler KeyboardNavigationHandler => null;
  45. public IInputElement PointerOverElement { get; set; }
  46. public IMouseDevice MouseDevice { get; set; }
  47. public bool ShowAccessKeys { get; set; }
  48. public IStyleHost StylingParent { get; set; }
  49. IStyleHost IStyleHost.StylingParent => StylingParent;
  50. public IRenderTarget CreateRenderTarget()
  51. {
  52. var dc = new Mock<IDrawingContextImpl>();
  53. dc.Setup(x => x.CreateLayer(It.IsAny<Size>())).Returns(() =>
  54. {
  55. var layerDc = new Mock<IDrawingContextImpl>();
  56. var layer = new Mock<IRenderTargetBitmapImpl>();
  57. layer.Setup(x => x.CreateDrawingContext(It.IsAny<IVisualBrushRenderer>())).Returns(layerDc.Object);
  58. return layer.Object;
  59. });
  60. var result = new Mock<IRenderTarget>();
  61. result.Setup(x => x.CreateDrawingContext(It.IsAny<IVisualBrushRenderer>())).Returns(dc.Object);
  62. return result.Object;
  63. }
  64. public void Invalidate(Rect rect)
  65. {
  66. }
  67. public Point PointToClient(PixelPoint p) => p.ToPoint(1);
  68. public PixelPoint PointToScreen(Point p) => PixelPoint.FromPoint(p, 1);
  69. public void RegisterChildrenNames()
  70. {
  71. var scope = NameScope.GetNameScope(this) ?? new NameScope();
  72. NameScope.SetNameScope(this, scope);
  73. void Visit(StyledElement element, bool force = false)
  74. {
  75. if (element.Name != null)
  76. {
  77. if (scope.Find(element.Name) != element)
  78. scope.Register(element.Name, element);
  79. }
  80. if(element is IVisual visual && (force || NameScope.GetNameScope(element) == null))
  81. foreach(var child in visual.GetVisualChildren())
  82. if (child is StyledElement styledChild)
  83. Visit(styledChild);
  84. }
  85. Visit(this, true);
  86. }
  87. }
  88. }