Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
insets_unittest.cc
Go to the documentation of this file.
1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "insets.h"
6
7#include "gtest/gtest.h"
8#include "insets_f.h"
9#include "rect.h"
10#include "size.h"
11#include "vector2d.h"
12
13TEST(InsetsTest, InsetsDefault) {
14 gfx::Insets insets;
15 EXPECT_EQ(0, insets.top());
16 EXPECT_EQ(0, insets.left());
17 EXPECT_EQ(0, insets.bottom());
18 EXPECT_EQ(0, insets.right());
19 EXPECT_EQ(0, insets.width());
20 EXPECT_EQ(0, insets.height());
21 EXPECT_TRUE(insets.IsEmpty());
22}
23
24TEST(InsetsTest, Insets) {
25 gfx::Insets insets(1, 2, 3, 4);
26 EXPECT_EQ(1, insets.top());
27 EXPECT_EQ(2, insets.left());
28 EXPECT_EQ(3, insets.bottom());
29 EXPECT_EQ(4, insets.right());
30 EXPECT_EQ(6, insets.width()); // Left + right.
31 EXPECT_EQ(4, insets.height()); // Top + bottom.
32 EXPECT_FALSE(insets.IsEmpty());
33}
34
35TEST(InsetsTest, SetTop) {
36 gfx::Insets insets(1);
37 insets.set_top(2);
38 EXPECT_EQ(gfx::Insets(2, 1, 1, 1), insets);
39}
40
41TEST(InsetsTest, SetBottom) {
42 gfx::Insets insets(1);
43 insets.set_bottom(2);
44 EXPECT_EQ(gfx::Insets(1, 1, 2, 1), insets);
45}
46
47TEST(InsetsTest, SetLeft) {
48 gfx::Insets insets(1);
49 insets.set_left(2);
50 EXPECT_EQ(gfx::Insets(1, 2, 1, 1), insets);
51}
52
53TEST(InsetsTest, SetRight) {
54 gfx::Insets insets(1);
55 insets.set_right(2);
56 EXPECT_EQ(gfx::Insets(1, 1, 1, 2), insets);
57}
58
59TEST(InsetsTest, Set) {
60 gfx::Insets insets;
61 insets.Set(1, 2, 3, 4);
62 EXPECT_EQ(1, insets.top());
63 EXPECT_EQ(2, insets.left());
64 EXPECT_EQ(3, insets.bottom());
65 EXPECT_EQ(4, insets.right());
66}
67
68TEST(InsetsTest, Operators) {
69 gfx::Insets insets;
70 insets.Set(1, 2, 3, 4);
71 insets += gfx::Insets(5, 6, 7, 8);
72 EXPECT_EQ(6, insets.top());
73 EXPECT_EQ(8, insets.left());
74 EXPECT_EQ(10, insets.bottom());
75 EXPECT_EQ(12, insets.right());
76
77 insets -= gfx::Insets(-1, 0, 1, 2);
78 EXPECT_EQ(7, insets.top());
79 EXPECT_EQ(8, insets.left());
80 EXPECT_EQ(9, insets.bottom());
81 EXPECT_EQ(10, insets.right());
82
83 insets = gfx::Insets(10, 10, 10, 10) + gfx::Insets(5, 5, 0, -20);
84 EXPECT_EQ(15, insets.top());
85 EXPECT_EQ(15, insets.left());
86 EXPECT_EQ(10, insets.bottom());
87 EXPECT_EQ(-10, insets.right());
88
89 insets = gfx::Insets(10, 10, 10, 10) - gfx::Insets(5, 5, 0, -20);
90 EXPECT_EQ(5, insets.top());
91 EXPECT_EQ(5, insets.left());
92 EXPECT_EQ(10, insets.bottom());
93 EXPECT_EQ(30, insets.right());
94}
95
96TEST(InsetsFTest, Operators) {
97 gfx::InsetsF insets;
98 insets.Set(1.f, 2.5f, 3.3f, 4.1f);
99 insets += gfx::InsetsF(5.8f, 6.7f, 7.6f, 8.5f);
100 EXPECT_FLOAT_EQ(6.8f, insets.top());
101 EXPECT_FLOAT_EQ(9.2f, insets.left());
102 EXPECT_FLOAT_EQ(10.9f, insets.bottom());
103 EXPECT_FLOAT_EQ(12.6f, insets.right());
104
105 insets -= gfx::InsetsF(-1.f, 0, 1.1f, 2.2f);
106 EXPECT_FLOAT_EQ(7.8f, insets.top());
107 EXPECT_FLOAT_EQ(9.2f, insets.left());
108 EXPECT_FLOAT_EQ(9.8f, insets.bottom());
109 EXPECT_FLOAT_EQ(10.4f, insets.right());
110
111 insets = gfx::InsetsF(10, 10.1f, 10.01f, 10.001f) +
112 gfx::InsetsF(5.5f, 5.f, 0, -20.2f);
113 EXPECT_FLOAT_EQ(15.5f, insets.top());
114 EXPECT_FLOAT_EQ(15.1f, insets.left());
115 EXPECT_FLOAT_EQ(10.01f, insets.bottom());
116 EXPECT_FLOAT_EQ(-10.199f, insets.right());
117
118 insets = gfx::InsetsF(10, 10.1f, 10.01f, 10.001f) -
119 gfx::InsetsF(5.5f, 5.f, 0, -20.2f);
120 EXPECT_FLOAT_EQ(4.5f, insets.top());
121 EXPECT_FLOAT_EQ(5.1f, insets.left());
122 EXPECT_FLOAT_EQ(10.01f, insets.bottom());
123 EXPECT_FLOAT_EQ(30.201f, insets.right());
124}
125
126TEST(InsetsTest, Equality) {
127 gfx::Insets insets1;
128 insets1.Set(1, 2, 3, 4);
129 gfx::Insets insets2;
130 // Test operator== and operator!=.
131 EXPECT_FALSE(insets1 == insets2);
132 EXPECT_TRUE(insets1 != insets2);
133
134 insets2.Set(1, 2, 3, 4);
135 EXPECT_TRUE(insets1 == insets2);
136 EXPECT_FALSE(insets1 != insets2);
137}
138
139TEST(InsetsTest, ToString) {
140 gfx::Insets insets(1, 2, 3, 4);
141 EXPECT_EQ("1,2,3,4", insets.ToString());
142}
143
144TEST(InsetsTest, Offset) {
145 const gfx::Insets insets(1, 2, 3, 4);
146 const gfx::Rect rect(5, 6, 7, 8);
147 const gfx::Vector2d vector(9, 10);
148
149 // Whether you inset then offset the rect, offset then inset the rect, or
150 // offset the insets then apply to the rect, the outcome should be the same.
151 gfx::Rect inset_first = rect;
152 inset_first.Inset(insets);
153 inset_first.Offset(vector);
154
155 gfx::Rect offset_first = rect;
156 offset_first.Offset(vector);
157 offset_first.Inset(insets);
158
159 gfx::Rect inset_by_offset = rect;
160 inset_by_offset.Inset(insets.Offset(vector));
161
162 EXPECT_EQ(inset_first, offset_first);
163 EXPECT_EQ(inset_by_offset, inset_first);
164}
165
166TEST(InsetsTest, Scale) {
167 gfx::Insets test(10, 5);
168 test = test.Scale(2.f, 3.f);
169 EXPECT_EQ(gfx::Insets(30, 10), test);
170
171 test = gfx::Insets(7, 3);
172 test = test.Scale(-2.f, -3.f);
173 EXPECT_EQ(gfx::Insets(-21, -6), test);
174}
175
176TEST(InsetsTest, IntegerOverflow) {
177 constexpr int int_min = std::numeric_limits<int>::min();
178 constexpr int int_max = std::numeric_limits<int>::max();
179
180 gfx::Insets width_height_test(int_max);
181 EXPECT_EQ(int_max, width_height_test.width());
182 EXPECT_EQ(int_max, width_height_test.height());
183
184 gfx::Insets plus_test(int_max);
185 plus_test += gfx::Insets(int_max);
186 EXPECT_EQ(gfx::Insets(int_max), plus_test);
187
188 gfx::Insets negation_test = -gfx::Insets(int_min);
189 EXPECT_EQ(gfx::Insets(int_max), negation_test);
190
191 gfx::Insets scale_test(int_max);
192 scale_test = scale_test.Scale(2.f, 2.f);
193 EXPECT_EQ(gfx::Insets(int_max), scale_test);
194}
195
196TEST(InsetsTest, IntegerUnderflow) {
197 constexpr int int_min = std::numeric_limits<int>::min();
198 constexpr int int_max = std::numeric_limits<int>::max();
199
200 gfx::Insets width_height_test = gfx::Insets(int_min);
201 EXPECT_EQ(int_min, width_height_test.width());
202 EXPECT_EQ(int_min, width_height_test.height());
203
204 gfx::Insets minus_test(int_min);
205 minus_test -= gfx::Insets(int_max);
206 EXPECT_EQ(gfx::Insets(int_min), minus_test);
207
208 gfx::Insets scale_test = gfx::Insets(int_min);
209 scale_test = scale_test.Scale(2.f, 2.f);
210 EXPECT_EQ(gfx::Insets(int_min), scale_test);
211}
212
213TEST(InsetsTest, IntegerOverflowSetVariants) {
214 constexpr int int_max = std::numeric_limits<int>::max();
215
216 gfx::Insets set_test(20);
217 set_test.set_top(int_max);
218 EXPECT_EQ(int_max, set_test.top());
219 EXPECT_EQ(0, set_test.bottom());
220
221 set_test.set_left(int_max);
222 EXPECT_EQ(int_max, set_test.left());
223 EXPECT_EQ(0, set_test.right());
224
225 set_test = gfx::Insets(30);
226 set_test.set_bottom(int_max);
227 EXPECT_EQ(int_max - 30, set_test.bottom());
228 EXPECT_EQ(30, set_test.top());
229
230 set_test.set_right(int_max);
231 EXPECT_EQ(int_max - 30, set_test.right());
232 EXPECT_EQ(30, set_test.left());
233}
234
235TEST(InsetsTest, IntegerUnderflowSetVariants) {
236 constexpr int int_min = std::numeric_limits<int>::min();
237
238 gfx::Insets set_test(-20);
239 set_test.set_top(int_min);
240 EXPECT_EQ(int_min, set_test.top());
241 EXPECT_EQ(0, set_test.bottom());
242
243 set_test.set_left(int_min);
244 EXPECT_EQ(int_min, set_test.left());
245 EXPECT_EQ(0, set_test.right());
246
247 set_test = gfx::Insets(-30);
248 set_test.set_bottom(int_min);
249 EXPECT_EQ(int_min + 30, set_test.bottom());
250 EXPECT_EQ(-30, set_test.top());
251
252 set_test.set_right(int_min);
253 EXPECT_EQ(int_min + 30, set_test.right());
254 EXPECT_EQ(-30, set_test.left());
255}
256
257TEST(InsetsTest, IntegerOverflowSet) {
258 constexpr int int_max = std::numeric_limits<int>::max();
259
260 gfx::Insets set_all_test;
261 set_all_test.Set(10, 20, int_max, int_max);
262 EXPECT_EQ(gfx::Insets(10, 20, int_max - 10, int_max - 20), set_all_test);
263}
264
265TEST(InsetsTest, IntegerOverflowOffset) {
266 constexpr int int_max = std::numeric_limits<int>::max();
267
268 const gfx::Vector2d max_vector(int_max, int_max);
269 gfx::Insets insets(1, 2, 3, 4);
270 gfx::Insets offset_test = insets.Offset(max_vector);
271 EXPECT_EQ(gfx::Insets(int_max, int_max, 3 - int_max, 4 - int_max),
272 offset_test);
273}
274
275TEST(InsetsTest, IntegerUnderflowOffset) {
276 constexpr int int_min = std::numeric_limits<int>::min();
277
278 const gfx::Vector2d min_vector(int_min, int_min);
279 gfx::Insets insets(-10);
280 gfx::Insets offset_test = insets.Offset(min_vector);
281 EXPECT_EQ(gfx::Insets(int_min, int_min, -10 - int_min, -10 - int_min),
282 offset_test);
283}
284
285TEST(InsetsTest, Size) {
286 gfx::Insets insets(1, 2, 3, 4);
287 EXPECT_EQ(gfx::Size(6, 4), insets.size());
288}
#define TEST(S, s, D, expected)
#define test(name)
constexpr float left() const
Definition insets_f.h:29
constexpr float bottom() const
Definition insets_f.h:30
constexpr float top() const
Definition insets_f.h:28
void Set(float top, float left, float bottom, float right)
Definition insets_f.h:44
constexpr float right() const
Definition insets_f.h:31
void Set(int top, int left, int bottom, int right)
Definition insets.h:76
constexpr int width() const
Definition insets.h:52
constexpr int right() const
Definition insets.h:48
void set_right(int right)
Definition insets.h:74
constexpr int bottom() const
Definition insets.h:47
Insets Scale(float scale) const
Definition insets.h:110
void set_left(int left)
Definition insets.h:69
std::string ToString() const
Definition insets.cc:12
void set_top(int top)
Definition insets.h:65
constexpr Size size() const
Definition insets.h:60
void set_bottom(int bottom)
Definition insets.h:73
constexpr int top() const
Definition insets.h:45
constexpr int height() const
Definition insets.h:56
constexpr int left() const
Definition insets.h:46
bool IsEmpty() const
Definition insets.h:63
Insets Offset(const gfx::Vector2d &vector) const
Definition insets.cc:17
void Offset(int horizontal, int vertical)
Definition rect.cc:121
void Inset(int horizontal, int vertical)
Definition rect.h:123
#define EXPECT_TRUE(handle)
Definition unit_test.h:685