summaryrefslogtreecommitdiff
path: root/libs/ui/tests/Rect_test.cpp
blob: 9cc36bb15b25636391202b444f1f690fb311b205 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <system/graphics.h>
#include <ui/FloatRect.h>
#include <ui/Point.h>
#include <ui/Rect.h>
#include <ui/Size.h>

#include <gtest/gtest.h>

namespace android::ui {

TEST(RectTest, constructDefault) {
    const Rect rect;
    EXPECT_FALSE(rect.isValid());
    EXPECT_TRUE(rect.isEmpty());
}

TEST(RectTest, constructFromWidthAndHeight) {
    const Rect rect(100, 200);
    EXPECT_TRUE(rect.isValid());
    EXPECT_FALSE(rect.isEmpty());
    EXPECT_EQ(0, rect.top);
    EXPECT_EQ(0, rect.left);
    EXPECT_EQ(100, rect.right);
    EXPECT_EQ(200, rect.bottom);
    EXPECT_EQ(100, rect.getWidth());
    EXPECT_EQ(200, rect.getHeight());
}

TEST(RectTest, constructFromSize) {
    const Rect rect(Size(100, 200));
    EXPECT_TRUE(rect.isValid());
    EXPECT_FALSE(rect.isEmpty());
    EXPECT_EQ(0, rect.top);
    EXPECT_EQ(0, rect.left);
    EXPECT_EQ(100, rect.right);
    EXPECT_EQ(200, rect.bottom);
    EXPECT_EQ(100, rect.getWidth());
    EXPECT_EQ(200, rect.getHeight());
}

TEST(RectTest, constructFromLTRB) {
    const Rect rect(11, 12, 14, 14);
    EXPECT_TRUE(rect.isValid());
    EXPECT_FALSE(rect.isEmpty());
    EXPECT_EQ(11, rect.left);
    EXPECT_EQ(12, rect.top);
    EXPECT_EQ(14, rect.right);
    EXPECT_EQ(14, rect.bottom);
    EXPECT_EQ(3, rect.getWidth());
    EXPECT_EQ(2, rect.getHeight());
}

TEST(RectTest, constructFromPoints) {
    const Rect rect(Point(11, 12), Point(14, 14));
    EXPECT_TRUE(rect.isValid());
    EXPECT_FALSE(rect.isEmpty());
    EXPECT_EQ(11, rect.left);
    EXPECT_EQ(12, rect.top);
    EXPECT_EQ(14, rect.right);
    EXPECT_EQ(14, rect.bottom);
    EXPECT_EQ(3, rect.getWidth());
    EXPECT_EQ(2, rect.getHeight());
}

TEST(RectTest, constructFromFloatRect) {
    {
        const Rect rect(FloatRect(10, 20, 30, 40));
        EXPECT_TRUE(rect.isValid());
        EXPECT_FALSE(rect.isEmpty());
        EXPECT_EQ(10, rect.left);
        EXPECT_EQ(20, rect.top);
        EXPECT_EQ(30, rect.right);
        EXPECT_EQ(40, rect.bottom);
    }
    // Construct with floating point error
    {
        constexpr float kError = 1e-3;
        const Rect rect(FloatRect(10 - kError, 20 - kError, 30 - kError, 40 - kError));
        EXPECT_TRUE(rect.isValid());
        EXPECT_FALSE(rect.isEmpty());
        EXPECT_EQ(10, rect.left);
        EXPECT_EQ(20, rect.top);
        EXPECT_EQ(30, rect.right);
        EXPECT_EQ(40, rect.bottom);
    }
}

TEST(RectTest, makeInvalid) {
    Rect rect(10, 20, 60, 60);
    EXPECT_TRUE(rect.isValid());
    rect.makeInvalid();
    EXPECT_FALSE(rect.isValid());
}

TEST(RectTest, clear) {
    Rect rect(10, 20, 60, 60);
    EXPECT_FALSE(rect.isEmpty());
    rect.clear();
    EXPECT_TRUE(rect.isEmpty());
}

TEST(RectTest, getSize) {
    const Rect rect(10, 20, 60, 60);
    EXPECT_EQ(Size(50, 40), rect.getSize());
}

TEST(RectTest, getBounds) {
    const Rect rect(10, 20, 60, 60);
    const Rect bounds = rect.getBounds();
    EXPECT_EQ(0, bounds.left);
    EXPECT_EQ(0, bounds.top);
    EXPECT_EQ(50, bounds.right);
    EXPECT_EQ(40, bounds.bottom);
    EXPECT_EQ(rect.getSize(), bounds.getSize());
}

TEST(RectTest, getCornerPoints) {
    const Rect rect(10, 20, 50, 60);
    EXPECT_EQ(Point(10, 20), rect.leftTop());
    EXPECT_EQ(Point(10, 60), rect.leftBottom());
    EXPECT_EQ(Point(50, 20), rect.rightTop());
    EXPECT_EQ(Point(50, 60), rect.rightBottom());
}

TEST(RectTest, operatorEquals) {
    const Rect rect(10, 20, 50, 60);
    EXPECT_EQ(rect, rect);
    EXPECT_NE(Rect(0, 20, 50, 60), rect);
    EXPECT_NE(Rect(10, 0, 50, 60), rect);
    EXPECT_NE(Rect(10, 20, 0, 60), rect);
    EXPECT_NE(Rect(10, 20, 50, 0), rect);
}

TEST(RectTest, operatorsPlusMinus) {
    Rect rect = Rect(10, 20, 50, 60) + Point(1, 2);
    EXPECT_EQ(Rect(11, 22, 51, 62), rect);
    rect -= Point(1, 2);
    EXPECT_EQ(Rect(10, 20, 50, 60), rect);

    rect = Rect(10, 20, 50, 60) - Point(1, 2);
    EXPECT_EQ(Rect(9, 18, 49, 58), rect);
    rect += Point(1, 2);
    EXPECT_EQ(Rect(10, 20, 50, 60), rect);
}

TEST(RectTest, scale) {
    Rect rect(10, 20, 50, 60);
    EXPECT_EQ(Rect(20, 60, 100, 180), rect.scale(2.f, 3.f));
    rect.scaleSelf(2.f, 3.f);
    EXPECT_EQ(Rect(20, 60, 100, 180), rect);

    rect = Rect(10, 20, 50, 60);
    constexpr float kError = 1e-3;
    EXPECT_EQ(Rect(20, 60, 100, 180), rect.scale(2.f - kError, 3.f - kError));
    rect.scaleSelf(2.f - kError, 3.f - kError);
    EXPECT_EQ(Rect(20, 60, 100, 180), rect);
}

TEST(RectTest, inset) {
    Rect rect(10, 20, 50, 60);
    rect.inset(0, 0, 0, 0);
    EXPECT_EQ(Rect(10, 20, 50, 60), rect);
    rect.inset(1, 2, 3, 4);
    EXPECT_EQ(Rect(11, 22, 47, 56), rect);
}

TEST(RectTest, intersect) {
    const Rect rect(10, 20, 50, 60);
    Rect intersection;

    // Intersect with self is self
    intersection.makeInvalid();
    EXPECT_TRUE(rect.intersect(rect, &intersection));
    EXPECT_EQ(Rect(10, 20, 50, 60), intersection);

    // Intersect with rect contained in us
    const Rect insideRect(11, 21, 45, 55);
    intersection.makeInvalid();
    EXPECT_TRUE(rect.intersect(insideRect, &intersection));
    EXPECT_EQ(insideRect, intersection);

    // Intersect with rect we are contained in
    intersection.makeInvalid();
    EXPECT_TRUE(insideRect.intersect(rect, &intersection));
    EXPECT_EQ(insideRect, intersection);

    // Empty intersection
    intersection.makeInvalid();
    EXPECT_FALSE(rect.intersect(Rect(100, 202, 150, 260), &intersection));
    EXPECT_TRUE(intersection.isEmpty());

    // Partial intersection
    const Rect other(30, 40, 70, 80);
    intersection.makeInvalid();
    EXPECT_TRUE(rect.intersect(other, &intersection));
    EXPECT_EQ(Rect(30, 40, 50, 60), intersection);

    // Intersetion is commutative
    intersection.makeInvalid();
    EXPECT_TRUE(other.intersect(rect, &intersection));
    EXPECT_EQ(Rect(30, 40, 50, 60), intersection);
}

TEST(RectTest, reduce) {
    const Rect rect(10, 20, 50, 60);

    // Reduce with self is empty
    EXPECT_TRUE(rect.reduce(rect).isEmpty());

    // Reduce with rect entirely inside is a noop
    const Rect insideRect(11, 21, 45, 55);
    EXPECT_EQ(rect, rect.reduce(insideRect));

    // Reduce with rect entirely outside is empty
    EXPECT_TRUE(insideRect.reduce(rect).isEmpty());

    // Reduce with rect on the right
    EXPECT_EQ(Rect(10, 20, 20, 60), rect.reduce(Rect(20, 0, 60, 70)));

    // Reduce with rect on the left
    EXPECT_EQ(Rect(40, 20, 50, 60), rect.reduce(Rect(0, 0, 40, 70)));

    // Reduce with rect at the top
    EXPECT_EQ(Rect(10, 40, 50, 60), rect.reduce(Rect(0, 0, 70, 40)));

    // Reduce with rect at the bottom
    EXPECT_EQ(Rect(10, 20, 50, 40), rect.reduce(Rect(0, 40, 70, 70)));
}

TEST(RectTest, transform) {
    const int32_t width = 100, height = 200;
    const Rect rect(1, 1, 2, 3);
    EXPECT_EQ(Rect(98, 1, 99, 3), rect.transform(HAL_TRANSFORM_FLIP_H, width, height));
    EXPECT_EQ(Rect(1, 197, 2, 199), rect.transform(HAL_TRANSFORM_FLIP_V, width, height));
    EXPECT_EQ(Rect(197, 1, 199, 2), rect.transform(HAL_TRANSFORM_ROT_90, width, height));
    EXPECT_EQ(Rect(98, 197, 99, 199), rect.transform(HAL_TRANSFORM_ROT_180, width, height));
    EXPECT_EQ(Rect(1, 98, 3, 99), rect.transform(HAL_TRANSFORM_ROT_270, width, height));
}

TEST(RectTest, toFloatRect) {
    const Rect rect(10, 20, 50, 60);
    const FloatRect floatRect = rect.toFloatRect();
    EXPECT_EQ(FloatRect(10.f, 20.f, 50.f, 60.f), floatRect);
}

TEST(RectTest, RectHash) {
    const std::vector<Rect> rects = {
            Rect(10, 20, 50, 60), Rect(11, 20, 50, 60), Rect(11, 21, 50, 60),
            Rect(11, 21, 51, 60), Rect(11, 21, 51, 61),
    };

    for (const auto& a : rects) {
        for (const auto& b : rects) {
            const bool hashEq = std::hash<Rect>{}(a) == std::hash<Rect>{}(b);
            EXPECT_EQ(a == b, hashEq);
        }
    }
}

TEST(RectTest, FloatRectHash) {
    const std::vector<FloatRect> floatRects = {
            Rect(10, 20, 50, 60).toFloatRect(), Rect(11, 20, 50, 60).toFloatRect(),
            Rect(11, 21, 50, 60).toFloatRect(), Rect(11, 21, 51, 60).toFloatRect(),
            Rect(11, 21, 51, 61).toFloatRect(),
    };

    for (const auto& a : floatRects) {
        for (const auto& b : floatRects) {
            const bool hashEq = std::hash<FloatRect>{}(a) == std::hash<FloatRect>{}(b);
            EXPECT_EQ(a == b, hashEq);
        }
    }
}

} // namespace android::ui