|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CommonLibraryGuiTests |
| 4 | + * FILE: WpfVectorRendererTests.cs |
| 5 | + * PURPOSE: Basic tests for the WpfVectorRenderer, especially to catch the "Double Offset" bug where the line's internal coordinates are incorrectly offset by the StartCoordinates. |
| 6 | + * PROGRAMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Windows; |
| 12 | +using System.Windows.Controls; |
| 13 | +using System.Windows.Shapes; |
| 14 | +using NUnit.Framework; |
| 15 | +using System.Threading; |
| 16 | +using System.Linq; |
| 17 | +using LightVector; |
| 18 | + |
| 19 | +namespace CommonLibraryGuiTests |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Simple tests for the WpfVectorRenderer, especially to catch the "Double Offset" bug where the line's internal coordinates are incorrectly offset by the StartCoordinates. |
| 23 | + /// </summary> |
| 24 | + [TestFixture] |
| 25 | + [Apartment(ApartmentState.STA)] |
| 26 | + public class WpfVectorRendererTests |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Renders to container verify normalization. |
| 30 | + /// </summary> |
| 31 | + [Test] |
| 32 | + public void RenderToContainer_VerifyNormalization() |
| 33 | + { |
| 34 | + // Arrange |
| 35 | + var startPoint = new Point(50, 50); |
| 36 | + var line = new LineObject { Direction = new System.Numerics.Vector2(10, 10), Thickness = 2 }; |
| 37 | + var saveObj = new SaveObject { Graphic = line, Type = GraphicTypes.Line, StartCoordinates = startPoint }; |
| 38 | + |
| 39 | + var renderer = new WpfVectorRenderer(new List<SaveObject> { saveObj }); |
| 40 | + |
| 41 | + // Act |
| 42 | + var canvas = (Canvas)renderer.RenderToContainer(); |
| 43 | + var renderedLine = canvas.Children.OfType<Line>().First(); |
| 44 | + |
| 45 | + // Assert |
| 46 | + // Because of normalization, the line is shifted. |
| 47 | + // It should now be at 'padding' (Thickness / 2) |
| 48 | + Assert.That(Canvas.GetLeft(renderedLine), Is.EqualTo(1.0d), "Line should be shifted to the edge of the canvas."); |
| 49 | + Assert.That(renderedLine.X1, Is.EqualTo(0), "Internal offset must remain 0."); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Renders to image verify dimensions match bounds. |
| 54 | + /// </summary> |
| 55 | + [Test] |
| 56 | + public void RenderToImage_VerifyDimensionsMatchBounds() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var circle = new CircleObject { Radius = 100, Thickness = 10 }; |
| 60 | + var saveObj = new SaveObject |
| 61 | + { |
| 62 | + Graphic = circle, |
| 63 | + Type = GraphicTypes.Circle, |
| 64 | + StartCoordinates = new Point(0, 0), |
| 65 | + Layer = 0 |
| 66 | + }; |
| 67 | + |
| 68 | + var renderer = new WpfVectorRenderer(new List<SaveObject> { saveObj }); |
| 69 | + |
| 70 | + // Act |
| 71 | + var image = renderer.RenderToImage(); |
| 72 | + |
| 73 | + // Assert |
| 74 | + // Circle radius 100 + thickness 10 = total diameter 210. |
| 75 | + // Our GetGraphicBounds should account for this. |
| 76 | + Assert.That(image.Width, Is.GreaterThanOrEqualTo(200), "Image must be wide enough for the diameter."); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments