TestPolygon.cpp (4068B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 9 #include "PolygonTestUtils.h" 10 11 #include "nsTArray.h" 12 #include "Point.h" 13 #include "Polygon.h" 14 #include "Triangle.h" 15 16 using namespace mozilla::gfx; 17 typedef mozilla::gfx::Polygon MozPolygon; 18 19 TEST(MozPolygon, TriangulateRectangle) 20 { 21 const MozPolygon p{ 22 Point4D(0.0f, 0.0f, 1.0f, 1.0f), Point4D(0.0f, 1.0f, 1.0f, 1.0f), 23 Point4D(1.0f, 1.0f, 1.0f, 1.0f), Point4D(1.0f, 0.0f, 1.0f, 1.0f)}; 24 25 const nsTArray<Triangle> triangles = p.ToTriangles(); 26 const nsTArray<Triangle> expected = { 27 Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(1.0f, 1.0f)), 28 Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))}; 29 30 AssertArrayEQ(triangles, expected); 31 } 32 33 TEST(MozPolygon, TriangulatePentagon) 34 { 35 const MozPolygon p{ 36 Point4D(0.0f, 0.0f, 1.0f, 1.0f), Point4D(0.0f, 1.0f, 1.0f, 1.0f), 37 Point4D(0.5f, 1.5f, 1.0f, 1.0f), Point4D(1.0f, 1.0f, 1.0f, 1.0f), 38 Point4D(1.0f, 0.0f, 1.0f, 1.0f)}; 39 40 const nsTArray<Triangle> triangles = p.ToTriangles(); 41 const nsTArray<Triangle> expected = { 42 Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(0.5f, 1.5f)), 43 Triangle(Point(0.0f, 0.0f), Point(0.5f, 1.5f), Point(1.0f, 1.0f)), 44 Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))}; 45 46 AssertArrayEQ(triangles, expected); 47 } 48 49 static void TestClipRect(const MozPolygon& aPolygon, 50 const MozPolygon& aExpected, const Rect& aRect) { 51 const MozPolygon res = aPolygon.ClipPolygon(MozPolygon::FromRect(aRect)); 52 EXPECT_TRUE(res == aExpected); 53 } 54 55 TEST(MozPolygon, ClipRectangle) 56 { 57 MozPolygon polygon{ 58 Point4D(0.0f, 0.0f, 0.0f, 1.0f), Point4D(0.0f, 1.0f, 0.0f, 1.0f), 59 Point4D(1.0f, 1.0f, 0.0f, 1.0f), Point4D(1.0f, 0.0f, 0.0f, 1.0f)}; 60 TestClipRect(polygon, polygon, Rect(0.0f, 0.0f, 1.0f, 1.0f)); 61 62 MozPolygon expected = MozPolygon{ 63 Point4D(0.0f, 0.0f, 0.0f, 1.0f), Point4D(0.0f, 0.8f, 0.0f, 1.0f), 64 Point4D(0.8f, 0.8f, 0.0f, 1.0f), Point4D(0.8f, 0.0f, 0.0f, 1.0f)}; 65 TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 0.8f, 0.8f)); 66 67 expected = MozPolygon{ 68 Point4D(0.2f, 0.2f, 0.0f, 1.0f), Point4D(0.2f, 1.0f, 0.0f, 1.0f), 69 Point4D(1.0f, 1.0f, 0.0f, 1.0f), Point4D(1.0f, 0.2f, 0.0f, 1.0f)}; 70 TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.8f, 0.8f)); 71 72 expected = MozPolygon{ 73 Point4D(0.2f, 0.2f, 0.0f, 1.0f), Point4D(0.2f, 0.8f, 0.0f, 1.0f), 74 Point4D(0.8f, 0.8f, 0.0f, 1.0f), Point4D(0.8f, 0.2f, 0.0f, 1.0f)}; 75 TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.6f, 0.6f)); 76 } 77 78 TEST(MozPolygon, ClipTriangle) 79 { 80 MozPolygon clipped, expected; 81 const MozPolygon polygon{Point4D(0.0f, 0.0f, 0.0f, 1.0f), 82 Point4D(0.0f, 1.0f, 0.0f, 1.0f), 83 Point4D(1.0f, 1.0f, 0.0f, 1.0f)}; 84 85 expected = MozPolygon{Point4D(0.0f, 0.0f, 0.0f, 1.0f), 86 Point4D(0.0f, 1.0f, 0.0f, 1.0f), 87 Point4D(1.0f, 1.0f, 0.0f, 1.0f)}; 88 TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 1.0f, 1.0f)); 89 90 expected = MozPolygon{Point4D(0.0f, 0.0f, 0.0f, 1.0f), 91 Point4D(0.0f, 0.8f, 0.0f, 1.0f), 92 Point4D(0.8f, 0.8f, 0.0f, 1.0f)}; 93 TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 0.8f, 0.8f)); 94 95 expected = MozPolygon{Point4D(0.2f, 0.2f, 0.0f, 1.0f), 96 Point4D(0.2f, 1.0f, 0.0f, 1.0f), 97 Point4D(1.0f, 1.0f, 0.0f, 1.0f)}; 98 TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.8f, 0.8f)); 99 100 expected = MozPolygon{Point4D(0.2f, 0.2f, 0.0f, 1.0f), 101 Point4D(0.2f, 0.8f, 0.0f, 1.0f), 102 Point4D(0.8f, 0.8f, 0.0f, 1.0f)}; 103 TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.6f, 0.6f)); 104 }