1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "SizeTest"
18 
19 #include <cmath>
20 #include <cstdlib>
21 
22 #include <ui/Size.h>
23 
24 #include <gtest/gtest.h>
25 
26 namespace android {
27 namespace ui {
28 
TEST(SizeTest,BasicConstructionAndEqualityComparison)29 TEST(SizeTest, BasicConstructionAndEqualityComparison) {
30     Size s(123, 456);
31 
32     EXPECT_EQ(123, s.width);
33     EXPECT_EQ(123, s.getWidth());
34 
35     EXPECT_EQ(456, s.height);
36     EXPECT_EQ(456, s.getHeight());
37 
38     EXPECT_EQ(Size(123, 456), s);
39     EXPECT_NE(Size(456, 123), s);
40 }
41 
TEST(SizeTest,BasicLessThanComparison)42 TEST(SizeTest, BasicLessThanComparison) {
43     EXPECT_TRUE(Size(0, 1) < Size(2, 3));
44     EXPECT_FALSE(Size(2, 3) < Size(0, 1));
45 
46     EXPECT_TRUE(Size(0, 3) < Size(2, 1));
47     EXPECT_FALSE(Size(2, 1) < Size(0, 3));
48 
49     EXPECT_TRUE(Size(0, 1) < Size(0, 3));
50     EXPECT_FALSE(Size(0, 3) < Size(0, 1));
51 
52     EXPECT_FALSE(Size(1, 1) < Size(1, 1));
53 }
54 
TEST(SizeTest,ValidAndEmpty)55 TEST(SizeTest, ValidAndEmpty) {
56     {
57         Size s;
58         EXPECT_FALSE(s.isValid());
59         EXPECT_FALSE(s.isEmpty());
60     }
61 
62     {
63         Size s(-1, -1);
64         EXPECT_FALSE(s.isValid());
65         EXPECT_FALSE(s.isEmpty());
66     }
67 
68     {
69         Size s(1, -1000);
70         EXPECT_FALSE(s.isValid());
71         EXPECT_FALSE(s.isEmpty());
72     }
73 
74     {
75         Size s(-1000, 1);
76         EXPECT_FALSE(s.isValid());
77         EXPECT_FALSE(s.isEmpty());
78     }
79 
80     {
81         Size s(-1000, -1000);
82         EXPECT_FALSE(s.isValid());
83         EXPECT_FALSE(s.isEmpty());
84     }
85 
86     {
87         const auto& s = Size::INVALID;
88         EXPECT_FALSE(s.isValid());
89         EXPECT_FALSE(s.isEmpty());
90     }
91 
92     {
93         Size s(123, 456);
94         s.makeInvalid();
95         EXPECT_FALSE(s.isValid());
96         EXPECT_FALSE(s.isEmpty());
97     }
98 
99     {
100         Size s(0, 0);
101         EXPECT_TRUE(s.isValid());
102         EXPECT_TRUE(s.isEmpty());
103     }
104 
105     {
106         const auto& s = Size::EMPTY;
107         EXPECT_TRUE(s.isValid());
108         EXPECT_TRUE(s.isEmpty());
109     }
110 
111     {
112         Size s(123, 456);
113         s.clear();
114         EXPECT_TRUE(s.isValid());
115         EXPECT_TRUE(s.isEmpty());
116     }
117 
118     {
119         Size s(123, 456);
120         EXPECT_TRUE(s.isValid());
121         EXPECT_FALSE(s.isEmpty());
122     }
123 }
124 
TEST(SizeTest,Set)125 TEST(SizeTest, Set) {
126     {
127         Size s;
128         s.setWidth(0);
129         EXPECT_EQ(Size(0, -1), s);
130     }
131 
132     {
133         Size s;
134         s.setHeight(0);
135         EXPECT_EQ(Size(-1, 0), s);
136     }
137 
138     {
139         Size s;
140         s.set(123, 456);
141         EXPECT_EQ(Size(123, 456), s);
142     }
143 }
144 
145 template <typename T, typename U>
ClampTest(T input,U expected)146 void ClampTest(T input, U expected) {
147     // The constructor, set(), setWidth() and setHeight() all allow arbitrary
148     // conversions from other numeric types, and implement clamping if necessary.
149 
150     EXPECT_EQ(Size(expected, expected), Size(input, input));
151 
152     {
153         Size s;
154         s.set(input, input);
155         EXPECT_EQ(Size(expected, expected), s);
156     }
157 
158     {
159         Size s;
160         s.setWidth(input);
161         EXPECT_EQ(expected, s.width);
162     }
163 
164     {
165         Size s;
166         s.setHeight(input);
167         EXPECT_EQ(expected, s.height);
168     }
169 }
170 
TEST(SizeTest,Int8RangeIsNotClamped)171 TEST(SizeTest, Int8RangeIsNotClamped) {
172     ClampTest(std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max());
173     ClampTest(int8_t(0), int8_t(0));
174     ClampTest(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::lowest());
175 }
176 
TEST(SizeTest,FloatRangeIsClamped)177 TEST(SizeTest, FloatRangeIsClamped) {
178     ClampTest(std::numeric_limits<float>::max(), std::numeric_limits<int32_t>::max());
179     ClampTest(float(0), int32_t(0));
180     ClampTest(std::numeric_limits<float>::lowest(), std::numeric_limits<int32_t>::lowest());
181 }
182 
183 } // namespace ui
184 } // namespace android
185