TestRoot.cs 3.5 KB

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