Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/tests/gtest/TestPolygon.cpp
Line
Count
Source (jump to first uncovered line)
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
0
{
21
0
  const MozPolygon p {
22
0
    Point4D(0.0f, 0.0f, 1.0f, 1.0f),
23
0
    Point4D(0.0f, 1.0f, 1.0f, 1.0f),
24
0
    Point4D(1.0f, 1.0f, 1.0f, 1.0f),
25
0
    Point4D(1.0f, 0.0f, 1.0f, 1.0f)
26
0
  };
27
0
28
0
  const nsTArray<Triangle> triangles = p.ToTriangles();
29
0
  const nsTArray<Triangle> expected = {
30
0
    Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(1.0f, 1.0f)),
31
0
    Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))
32
0
  };
33
0
34
0
  AssertArrayEQ(triangles, expected);
35
0
}
36
37
TEST(MozPolygon, TriangulatePentagon)
38
0
{
39
0
  const MozPolygon p {
40
0
    Point4D(0.0f, 0.0f, 1.0f, 1.0f),
41
0
    Point4D(0.0f, 1.0f, 1.0f, 1.0f),
42
0
    Point4D(0.5f, 1.5f, 1.0f, 1.0f),
43
0
    Point4D(1.0f, 1.0f, 1.0f, 1.0f),
44
0
    Point4D(1.0f, 0.0f, 1.0f, 1.0f)
45
0
  };
46
0
47
0
  const nsTArray<Triangle> triangles = p.ToTriangles();
48
0
  const nsTArray<Triangle> expected = {
49
0
    Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(0.5f, 1.5f)),
50
0
    Triangle(Point(0.0f, 0.0f), Point(0.5f, 1.5f), Point(1.0f, 1.0f)),
51
0
    Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))
52
0
  };
53
0
54
0
  AssertArrayEQ(triangles, expected);
55
0
}
56
57
void
58
TestClipRect(const MozPolygon& aPolygon,
59
             const MozPolygon& aExpected,
60
             const Rect& aRect)
61
0
{
62
0
  const MozPolygon res = aPolygon.ClipPolygon(MozPolygon::FromRect(aRect));
63
0
  EXPECT_TRUE(res == aExpected);
64
0
}
65
66
TEST(MozPolygon, ClipRectangle)
67
0
{
68
0
  MozPolygon polygon {
69
0
    Point4D(0.0f, 0.0f, 0.0f, 1.0f),
70
0
    Point4D(0.0f, 1.0f, 0.0f, 1.0f),
71
0
    Point4D(1.0f, 1.0f, 0.0f, 1.0f),
72
0
    Point4D(1.0f, 0.0f, 0.0f, 1.0f)
73
0
  };
74
0
  TestClipRect(polygon, polygon, Rect(0.0f, 0.0f, 1.0f, 1.0f));
75
0
76
0
  MozPolygon expected = MozPolygon {
77
0
    Point4D(0.0f, 0.0f, 0.0f, 1.0f),
78
0
    Point4D(0.0f, 0.8f, 0.0f, 1.0f),
79
0
    Point4D(0.8f, 0.8f, 0.0f, 1.0f),
80
0
    Point4D(0.8f, 0.0f, 0.0f, 1.0f)
81
0
  };
82
0
  TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 0.8f, 0.8f));
83
0
84
0
  expected = MozPolygon {
85
0
    Point4D(0.2f, 0.2f, 0.0f, 1.0f),
86
0
    Point4D(0.2f, 1.0f, 0.0f, 1.0f),
87
0
    Point4D(1.0f, 1.0f, 0.0f, 1.0f),
88
0
    Point4D(1.0f, 0.2f, 0.0f, 1.0f)
89
0
  };
90
0
  TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.8f, 0.8f));
91
0
92
0
  expected = MozPolygon {
93
0
    Point4D(0.2f, 0.2f, 0.0f, 1.0f),
94
0
    Point4D(0.2f, 0.8f, 0.0f, 1.0f),
95
0
    Point4D(0.8f, 0.8f, 0.0f, 1.0f),
96
0
    Point4D(0.8f, 0.2f, 0.0f, 1.0f)
97
0
  };
98
0
  TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.6f, 0.6f));
99
0
}
100
101
TEST(MozPolygon, ClipTriangle)
102
0
{
103
0
  MozPolygon clipped, expected;
104
0
  const MozPolygon polygon {
105
0
    Point4D(0.0f, 0.0f, 0.0f, 1.0f),
106
0
    Point4D(0.0f, 1.0f, 0.0f, 1.0f),
107
0
    Point4D(1.0f, 1.0f, 0.0f, 1.0f)
108
0
  };
109
0
110
0
  expected = MozPolygon {
111
0
    Point4D(0.0f, 0.0f, 0.0f, 1.0f),
112
0
    Point4D(0.0f, 1.0f, 0.0f, 1.0f),
113
0
    Point4D(1.0f, 1.0f, 0.0f, 1.0f)
114
0
  };
115
0
  TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 1.0f, 1.0f));
116
0
117
0
  expected = MozPolygon {
118
0
    Point4D(0.0f, 0.0f, 0.0f, 1.0f),
119
0
    Point4D(0.0f, 0.8f, 0.0f, 1.0f),
120
0
    Point4D(0.8f, 0.8f, 0.0f, 1.0f)
121
0
  };
122
0
  TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 0.8f, 0.8f));
123
0
124
0
  expected = MozPolygon {
125
0
    Point4D(0.2f, 0.2f, 0.0f, 1.0f),
126
0
    Point4D(0.2f, 1.0f, 0.0f, 1.0f),
127
0
    Point4D(1.0f, 1.0f, 0.0f, 1.0f)
128
0
  };
129
0
  TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.8f, 0.8f));
130
0
131
0
  expected = MozPolygon {
132
0
    Point4D(0.2f, 0.2f, 0.0f, 1.0f),
133
0
    Point4D(0.2f, 0.8f, 0.0f, 1.0f),
134
0
    Point4D(0.8f, 0.8f, 0.0f, 1.0f)
135
0
  };
136
0
  TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.6f, 0.6f));
137
0
}