瀏覽代碼

Moved some mocks into Avalonia.UnitTests.

From Avalonia.Visuals.UnitTests as they're useful generally.
Steven Kirk 8 年之前
父節點
當前提交
abe69ac320

+ 2 - 0
tests/Avalonia.UnitTests/Avalonia.UnitTests.csproj

@@ -57,6 +57,8 @@
     <Compile Include="ImmediateDispatcher.cs" />
     <Compile Include="InvariantCultureFixture.cs" />
     <Compile Include="MockRendererFactory.cs" />
+    <Compile Include="MockPlatformRenderInterface.cs" />
+    <Compile Include="MockStreamGeometryImpl.cs" />
     <Compile Include="NotifyingBase.cs" />
     <Compile Include="TestLogSink.cs" />
     <Compile Include="TestTemplatedRoot.cs" />

+ 56 - 0
tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs

@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Avalonia.Media;
+using Avalonia.Platform;
+using Moq;
+
+namespace Avalonia.UnitTests
+{
+    public class MockPlatformRenderInterface : IPlatformRenderInterface
+    {
+        public IFormattedTextImpl CreateFormattedText(
+            string text,
+            string fontFamilyName,
+            double fontSize,
+            FontStyle fontStyle,
+            TextAlignment textAlignment,
+            FontWeight fontWeight,
+            TextWrapping wrapping,
+            Size constraint)
+        {
+            var result = new Mock<IFormattedTextImpl>();
+            result.Setup(x => x.WithConstraint(It.IsAny<Size>())).Returns(() => result.Object);
+            return result.Object;
+        }
+
+        public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
+        {
+            return Mock.Of<IRenderTarget>();
+        }
+
+        public IRenderTargetBitmapImpl CreateRenderTargetBitmap(
+            int width,
+            int height,
+            double dpiX,
+            double dpiY)
+        {
+            return Mock.Of<IRenderTargetBitmapImpl>();
+        }
+
+        public IStreamGeometryImpl CreateStreamGeometry()
+        {
+            return new MockStreamGeometryImpl();
+        }
+
+        public IBitmapImpl LoadBitmap(Stream stream)
+        {
+            return Mock.Of<IBitmapImpl>();
+        }
+
+        public IBitmapImpl LoadBitmap(string fileName)
+        {
+            return Mock.Of<IBitmapImpl>();
+        }
+    }
+}

+ 131 - 0
tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs

@@ -0,0 +1,131 @@
+using System;
+using System.Collections.Generic;
+using Avalonia.Media;
+using Avalonia.Platform;
+
+namespace Avalonia.UnitTests
+{
+    public class MockStreamGeometryImpl : IStreamGeometryImpl
+    {
+        private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
+
+        public Rect Bounds => _impl.CalculateBounds();
+
+        public Matrix Transform { get; set; }
+
+        public IStreamGeometryImpl Clone()
+        {
+            return this;
+        }
+
+        public bool FillContains(Point point)
+        {
+            return _impl.FillContains(point);
+        }
+
+        public bool StrokeContains(Pen pen, Point point)
+        {
+            return false;
+        }
+
+        public Rect GetRenderBounds(double strokeThickness) => Bounds;
+
+        public IStreamGeometryContextImpl Open()
+        {
+            return _impl;
+        }
+
+        public IGeometryImpl WithTransform(Matrix transform)
+        {
+            return this;
+        }
+
+        class MockStreamGeometryContext : IStreamGeometryContextImpl
+        {
+            private List<Point> points = new List<Point>();
+            public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
+            {
+                throw new NotImplementedException();
+            }
+
+            public void BeginFigure(Point startPoint, bool isFilled)
+            {
+                points.Add(startPoint);
+            }
+
+            public Rect CalculateBounds()
+            {
+                var left = double.MaxValue;
+                var right = double.MinValue;
+                var top = double.MaxValue;
+                var bottom = double.MinValue;
+
+                foreach (var p in points)
+                {
+                    left = Math.Min(p.X, left);
+                    right = Math.Max(p.X, right);
+                    top = Math.Min(p.Y, top);
+                    bottom = Math.Max(p.Y, bottom);
+                }
+
+                return new Rect(new Point(left, top), new Point(right, bottom));
+            }
+
+            public void CubicBezierTo(Point point1, Point point2, Point point3)
+            {
+                throw new NotImplementedException();
+            }
+
+            public void Dispose()
+            {
+            }
+
+            public void EndFigure(bool isClosed)
+            {
+            }
+
+            public void LineTo(Point point)
+            {
+                points.Add(point);
+            }
+
+            public void QuadraticBezierTo(Point control, Point endPoint)
+            {
+                throw new NotImplementedException();
+            }
+
+            public void SetFillRule(FillRule fillRule)
+            {
+            }
+
+            public bool FillContains(Point point)
+            {
+                // Use the algorithm from http://www.blackpawn.com/texts/pointinpoly/default.html
+                // to determine if the point is in the geometry (since it will always be convex in this situation)
+                for (int i = 0; i < points.Count; i++)
+                {
+                    var a = points[i];
+                    var b = points[(i + 1) % points.Count];
+                    var c = points[(i + 2) % points.Count];
+
+                    Vector v0 = c - a;
+                    Vector v1 = b - a;
+                    Vector v2 = point - a;
+
+                    var dot00 = v0 * v0;
+                    var dot01 = v0 * v1;
+                    var dot02 = v0 * v2;
+                    var dot11 = v1 * v1;
+                    var dot12 = v1 * v2;
+
+
+                    var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
+                    var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
+                    var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
+                    if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
+                }
+                return false;
+            }
+        }
+    }
+}

+ 2 - 21
tests/Avalonia.UnitTests/TestServices.cs

@@ -22,7 +22,7 @@ namespace Avalonia.UnitTests
             layoutManager: new LayoutManager(),
             platform: new AppBuilder().RuntimePlatform,
             renderer: (_, __) => Mock.Of<IRenderer>(),
-            renderInterface: CreateRenderInterfaceMock(),
+            renderInterface: new MockPlatformRenderInterface(),
             renderLoop: Mock.Of<IRenderLoop>(),
             standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
             styler: new Styler(),
@@ -31,7 +31,7 @@ namespace Avalonia.UnitTests
             windowingPlatform: new MockWindowingPlatform());
 
         public static readonly TestServices MockPlatformRenderInterface = new TestServices(
-            renderInterface: CreateRenderInterfaceMock());
+            renderInterface: new MockPlatformRenderInterface());
 
         public static readonly TestServices MockPlatformWrapper = new TestServices(
             platform: Mock.Of<IRuntimePlatform>());
@@ -163,24 +163,5 @@ namespace Avalonia.UnitTests
 
             return result;
         }
-
-        private static IPlatformRenderInterface CreateRenderInterfaceMock()
-        {
-            var formattedTextImpl = new Mock<IFormattedTextImpl>();
-            formattedTextImpl.Setup(x => x.WithConstraint(It.IsAny<Size>())).Returns(() => formattedTextImpl.Object);
-
-            return Mock.Of<IPlatformRenderInterface>(x => 
-                x.CreateFormattedText(
-                    It.IsAny<string>(),
-                    It.IsAny<string>(),
-                    It.IsAny<double>(),
-                    It.IsAny<FontStyle>(),
-                    It.IsAny<TextAlignment>(),
-                    It.IsAny<FontWeight>(),
-                    It.IsAny<TextWrapping>(),
-                    It.IsAny<Size>()) == formattedTextImpl.Object &&
-                x.CreateStreamGeometry() == Mock.Of<IStreamGeometryImpl>(
-                    y => y.Open() == Mock.Of<IStreamGeometryContextImpl>()));
-        }
     }
 }

+ 0 - 1
tests/Avalonia.Visuals.UnitTests/Avalonia.Visuals.UnitTests.csproj

@@ -98,7 +98,6 @@
     <Compile Include="VisualTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="VisualTree\BoundsTrackerTests.cs" />
-    <Compile Include="VisualTree\MockRenderInterface.cs" />
     <Compile Include="VisualTree\VisualExtensionsTests_GetVisualsAt.cs" />
   </ItemGroup>
   <ItemGroup>

+ 0 - 178
tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs

@@ -1,178 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using Avalonia.Media;
-using Avalonia.Platform;
-
-namespace Avalonia.Visuals.UnitTests.VisualTree
-{
-    class MockRenderInterface : IPlatformRenderInterface
-    {
-        public IFormattedTextImpl CreateFormattedText(
-            string text,
-            string fontFamilyName,
-            double fontSize,
-            FontStyle fontStyle,
-            TextAlignment textAlignment,
-            FontWeight fontWeight,
-            TextWrapping wrapping,
-            Size constraint)
-        {
-            throw new NotImplementedException();
-        }
-
-        public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
-        {
-            throw new NotImplementedException();
-        }
-
-        public IRenderTargetBitmapImpl CreateRenderTargetBitmap(
-            int width,
-            int height,
-            double dpiX,
-            double dpiY)
-        {
-            throw new NotImplementedException();
-        }
-
-        public IStreamGeometryImpl CreateStreamGeometry()
-        {
-            return new MockStreamGeometry();
-        }
-
-        public IBitmapImpl LoadBitmap(Stream stream)
-        {
-            throw new NotImplementedException();
-        }
-
-        public IBitmapImpl LoadBitmap(string fileName)
-        {
-            throw new NotImplementedException();
-        }
-
-        class MockStreamGeometry : IStreamGeometryImpl
-        {
-            private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
-
-            public Rect Bounds => _impl.CalculateBounds();
-
-            public Matrix Transform { get; set; }
-
-            public IStreamGeometryImpl Clone()
-            {
-                return this;
-            }
-
-            public bool FillContains(Point point)
-            {
-                return _impl.FillContains(point);
-            }
-
-            public bool StrokeContains(Pen pen, Point point)
-            {
-                return false;
-            }
-
-            public Rect GetRenderBounds(double strokeThickness) => Bounds;
-
-            public IStreamGeometryContextImpl Open()
-            {
-                return _impl;
-            }
-
-            public IGeometryImpl WithTransform(Matrix transform)
-            {
-                return this;
-            }
-
-            class MockStreamGeometryContext : IStreamGeometryContextImpl
-            {
-                private List<Point> points = new List<Point>();
-                public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
-                {
-                    throw new NotImplementedException();
-                }
-
-                public void BeginFigure(Point startPoint, bool isFilled)
-                {
-                    points.Add(startPoint);
-                }
-
-                public Rect CalculateBounds()
-                {
-                    var left = double.MaxValue;
-                    var right = double.MinValue;
-                    var top = double.MaxValue;
-                    var bottom = double.MinValue;
-                    
-                    foreach (var p in points)
-                    {
-                        left = Math.Min(p.X, left);
-                        right = Math.Max(p.X, right);
-                        top = Math.Min(p.Y, top);
-                        bottom = Math.Max(p.Y, bottom);
-                    }
-
-                    return new Rect(new Point(left, top), new Point(right, bottom));
-                }
-
-                public void CubicBezierTo(Point point1, Point point2, Point point3)
-                {
-                    throw new NotImplementedException();
-                }
-
-                public void Dispose()
-                {
-                }
-
-                public void EndFigure(bool isClosed)
-                {
-                }
-
-                public void LineTo(Point point)
-                {
-                    points.Add(point);
-                }
-
-                public void QuadraticBezierTo(Point control, Point endPoint)
-                {
-                    throw new NotImplementedException();
-                }
-
-                public void SetFillRule(FillRule fillRule)
-                {
-                }
-
-                public bool FillContains(Point point)
-                {
-                    // Use the algorithm from http://www.blackpawn.com/texts/pointinpoly/default.html
-                    // to determine if the point is in the geometry (since it will always be convex in this situation)
-                    for (int i = 0; i < points.Count; i++)
-                    {
-                        var a = points[i];
-                        var b = points[(i + 1) % points.Count];
-                        var c = points[(i + 2) % points.Count];
-
-                        Vector v0 = c - a;
-                        Vector v1 = b - a;
-                        Vector v2 = point - a;
-
-                        var dot00 = v0 * v0;
-                        var dot01 = v0 * v1;
-                        var dot02 = v0 * v2;
-                        var dot11 = v1 * v1;
-                        var dot12 = v1 * v2;
-
-
-                        var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
-                        var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
-                        var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
-                        if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
-                    }
-                    return false;
-                }
-            }
-        }
-    }
-
-}

+ 1 - 1
tests/Avalonia.Visuals.UnitTests/VisualTree/VisualExtensionsTests_GetVisualsAt.cs

@@ -492,7 +492,7 @@ namespace Avalonia.Visuals.UnitTests.VisualTree
         {
             return UnitTestApplication.Start(
                 new TestServices(
-                    renderInterface: new MockRenderInterface(),
+                    renderInterface: new MockPlatformRenderInterface(),
                     renderer: (root, loop) => new DeferredRenderer(root, loop)));
         }
     }