BoundsTests.cs 3.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing;

namespace Svg.UnitTests
{
    /// <summary>
    /// Test class to test bounds for elements without vs. with transformations
    /// (see issue 281). Only some basic elements are tested - this should be sufficient
    /// to verify the functionality for all path-based elements.
    /// </summary>
    [TestClass]
    public class BoundsTests : SvgTestHelper
    {
        private const string BoundsTestSvg = "Issue281_Bounds.BoundsTest.svg";
        private static SvgDocument testDocument;

        [TestMethod]
        public void TestLineBounds()
        {
            // x1="10" x2="30" y1="20" y2="40", default line thickness is 1
            AssertEqualBounds("line", 9.5f, 19.5f, 21, 21);
            // additional translation(5, 5)
            AssertEqualBounds("line-xlate", 14.5f, 24.5f, 21, 21);
            // additional rotation(180) and translation(-50, 0)
            AssertEqualBounds("line-xform", 19.5f, -40.5f, 21, 21);
        }

        [TestMethod]
        public void TestRectangleBounds()
        {
            // x="10" y="30" width="10" height="20"
            AssertEqualBounds("rect", 9.5f, 29.5f, 10.5f, 20.5f);
            // additional translation(10, 10)
            AssertEqualBounds("rect-xlate", 19.5f, 39.5f, 10.5f, 20.5f);
            // additional rotation(90)
            AssertEqualBounds("rect-rot", -50, 9.5f, 20.5f, 10.5f);
        }

        [TestMethod]
        public void TestGroupBounds()
        {
            // all lines from TestLineBounds()
mrbean-bremen's avatar
mrbean-bremen committed
43
44
45
            AssertEqualBounds("lines", 9.5f, -40.5f, 31, 86);
            // all rectangles from TestRectangleBounds()
            AssertEqualBounds("rects", -50f, 9.5f, 80, 50.5f);
46
47
        }

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        [TestMethod]
        public void TestTranslatedGroupBounds()
        {
            AssertEqualBounds("lines-translated", -12.5f, -7, 31, 86);
        }

        [TestMethod]
        public void TestScaledGroupBounds()
        {
            AssertEqualBounds("lines-scaled", 19, -81, 62, 172);
        }

        [TestMethod]
        public void TestRotatedGroupBounds()
        {
            AssertEqualBounds("lines-rotated", -45.5f, 9.5f, 86, 31);
        }

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        private void AssertEqualBounds(string elementId, float x, float y, float width, float height)
        {
            const float Epsilon = 0.01f;
            var element = GetElement(elementId);
            var elementBounds = element.Bounds;
            Assert.AreEqual(x, elementBounds.X, Epsilon);
            Assert.AreEqual(y, elementBounds.Y, Epsilon);
            Assert.AreEqual(width, elementBounds.Width, Epsilon);
            Assert.AreEqual(height, elementBounds.Height, Epsilon);
        }

        private SvgVisualElement GetElement(string elementId)
        {
            if (testDocument == null)
            {
                testDocument = OpenSvg(GetXMLDocFromResource(GetFullResourceString(BoundsTestSvg)));
            }
            return testDocument.GetElementById<SvgVisualElement>(elementId);
        }
    }
}