• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 <sstream>
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/range/range.h"
9 #include "ui/gfx/range/range_f.h"
10 
11 namespace {
12 
13 template <typename T>
14 class RangeTest : public testing::Test {
15 };
16 
17 typedef testing::Types<gfx::Range, gfx::RangeF> RangeTypes;
18 TYPED_TEST_CASE(RangeTest, RangeTypes);
19 
20 template <typename T>
TestContainsAndIntersects(const T & r1,const T & r2,const T & r3)21 void TestContainsAndIntersects(const T& r1,
22                                const T& r2,
23                                const T& r3) {
24   EXPECT_TRUE(r1.Intersects(r1));
25   EXPECT_TRUE(r1.Contains(r1));
26   EXPECT_EQ(T(10, 12), r1.Intersect(r1));
27 
28   EXPECT_FALSE(r1.Intersects(r2));
29   EXPECT_FALSE(r1.Contains(r2));
30   EXPECT_TRUE(r1.Intersect(r2).is_empty());
31   EXPECT_FALSE(r2.Intersects(r1));
32   EXPECT_FALSE(r2.Contains(r1));
33   EXPECT_TRUE(r2.Intersect(r1).is_empty());
34 
35   EXPECT_TRUE(r1.Intersects(r3));
36   EXPECT_TRUE(r3.Intersects(r1));
37   EXPECT_TRUE(r3.Contains(r1));
38   EXPECT_FALSE(r1.Contains(r3));
39   EXPECT_EQ(T(10, 12), r1.Intersect(r3));
40   EXPECT_EQ(T(10, 12), r3.Intersect(r1));
41 
42   EXPECT_TRUE(r2.Intersects(r3));
43   EXPECT_TRUE(r3.Intersects(r2));
44   EXPECT_FALSE(r3.Contains(r2));
45   EXPECT_FALSE(r2.Contains(r3));
46   EXPECT_EQ(T(5, 8), r2.Intersect(r3));
47   EXPECT_EQ(T(5, 8), r3.Intersect(r2));
48 }
49 
50 }  // namespace
51 
TYPED_TEST(RangeTest,EmptyInit)52 TYPED_TEST(RangeTest, EmptyInit) {
53   TypeParam r;
54   EXPECT_EQ(0U, r.start());
55   EXPECT_EQ(0U, r.end());
56   EXPECT_EQ(0U, r.length());
57   EXPECT_FALSE(r.is_reversed());
58   EXPECT_TRUE(r.is_empty());
59   EXPECT_TRUE(r.IsValid());
60   EXPECT_EQ(0U, r.GetMin());
61   EXPECT_EQ(0U, r.GetMax());
62 }
63 
TYPED_TEST(RangeTest,StartEndInit)64 TYPED_TEST(RangeTest, StartEndInit) {
65   TypeParam r(10, 15);
66   EXPECT_EQ(10U, r.start());
67   EXPECT_EQ(15U, r.end());
68   EXPECT_EQ(5U, r.length());
69   EXPECT_FALSE(r.is_reversed());
70   EXPECT_FALSE(r.is_empty());
71   EXPECT_TRUE(r.IsValid());
72   EXPECT_EQ(10U, r.GetMin());
73   EXPECT_EQ(15U, r.GetMax());
74 }
75 
TYPED_TEST(RangeTest,StartEndReversedInit)76 TYPED_TEST(RangeTest, StartEndReversedInit) {
77   TypeParam r(10, 5);
78   EXPECT_EQ(10U, r.start());
79   EXPECT_EQ(5U, r.end());
80   EXPECT_EQ(5U, r.length());
81   EXPECT_TRUE(r.is_reversed());
82   EXPECT_FALSE(r.is_empty());
83   EXPECT_TRUE(r.IsValid());
84   EXPECT_EQ(5U, r.GetMin());
85   EXPECT_EQ(10U, r.GetMax());
86 }
87 
TYPED_TEST(RangeTest,PositionInit)88 TYPED_TEST(RangeTest, PositionInit) {
89   TypeParam r(12);
90   EXPECT_EQ(12U, r.start());
91   EXPECT_EQ(12U, r.end());
92   EXPECT_EQ(0U, r.length());
93   EXPECT_FALSE(r.is_reversed());
94   EXPECT_TRUE(r.is_empty());
95   EXPECT_TRUE(r.IsValid());
96   EXPECT_EQ(12U, r.GetMin());
97   EXPECT_EQ(12U, r.GetMax());
98 }
99 
TYPED_TEST(RangeTest,InvalidRange)100 TYPED_TEST(RangeTest, InvalidRange) {
101   TypeParam r(TypeParam::InvalidRange());
102   EXPECT_EQ(0U, r.length());
103   EXPECT_EQ(r.start(), r.end());
104   EXPECT_EQ(r.GetMax(), r.GetMin());
105   EXPECT_FALSE(r.is_reversed());
106   EXPECT_TRUE(r.is_empty());
107   EXPECT_FALSE(r.IsValid());
108   EXPECT_EQ(r, TypeParam::InvalidRange());
109   EXPECT_TRUE(r.EqualsIgnoringDirection(TypeParam::InvalidRange()));
110 }
111 
TYPED_TEST(RangeTest,Equality)112 TYPED_TEST(RangeTest, Equality) {
113   TypeParam r1(10, 4);
114   TypeParam r2(10, 4);
115   TypeParam r3(10, 2);
116   EXPECT_EQ(r1, r2);
117   EXPECT_NE(r1, r3);
118   EXPECT_NE(r2, r3);
119 
120   TypeParam r4(11, 4);
121   EXPECT_NE(r1, r4);
122   EXPECT_NE(r2, r4);
123   EXPECT_NE(r3, r4);
124 
125   TypeParam r5(12, 5);
126   EXPECT_NE(r1, r5);
127   EXPECT_NE(r2, r5);
128   EXPECT_NE(r3, r5);
129 }
130 
TYPED_TEST(RangeTest,EqualsIgnoringDirection)131 TYPED_TEST(RangeTest, EqualsIgnoringDirection) {
132   TypeParam r1(10, 5);
133   TypeParam r2(5, 10);
134   EXPECT_TRUE(r1.EqualsIgnoringDirection(r2));
135 }
136 
TYPED_TEST(RangeTest,SetStart)137 TYPED_TEST(RangeTest, SetStart) {
138   TypeParam r(10, 20);
139   EXPECT_EQ(10U, r.start());
140   EXPECT_EQ(10U, r.length());
141 
142   r.set_start(42);
143   EXPECT_EQ(42U, r.start());
144   EXPECT_EQ(20U, r.end());
145   EXPECT_EQ(22U, r.length());
146   EXPECT_TRUE(r.is_reversed());
147 }
148 
TYPED_TEST(RangeTest,SetEnd)149 TYPED_TEST(RangeTest, SetEnd) {
150   TypeParam r(10, 13);
151   EXPECT_EQ(10U, r.start());
152   EXPECT_EQ(3U, r.length());
153 
154   r.set_end(20);
155   EXPECT_EQ(10U, r.start());
156   EXPECT_EQ(20U, r.end());
157   EXPECT_EQ(10U, r.length());
158 }
159 
TYPED_TEST(RangeTest,SetStartAndEnd)160 TYPED_TEST(RangeTest, SetStartAndEnd) {
161   TypeParam r;
162   r.set_end(5);
163   r.set_start(1);
164   EXPECT_EQ(1U, r.start());
165   EXPECT_EQ(5U, r.end());
166   EXPECT_EQ(4U, r.length());
167   EXPECT_EQ(1U, r.GetMin());
168   EXPECT_EQ(5U, r.GetMax());
169 }
170 
TYPED_TEST(RangeTest,ReversedRange)171 TYPED_TEST(RangeTest, ReversedRange) {
172   TypeParam r(10, 5);
173   EXPECT_EQ(10U, r.start());
174   EXPECT_EQ(5U, r.end());
175   EXPECT_EQ(5U, r.length());
176   EXPECT_TRUE(r.is_reversed());
177   EXPECT_TRUE(r.IsValid());
178   EXPECT_EQ(5U, r.GetMin());
179   EXPECT_EQ(10U, r.GetMax());
180 }
181 
TYPED_TEST(RangeTest,SetReversedRange)182 TYPED_TEST(RangeTest, SetReversedRange) {
183   TypeParam r(10, 20);
184   r.set_start(25);
185   EXPECT_EQ(25U, r.start());
186   EXPECT_EQ(20U, r.end());
187   EXPECT_EQ(5U, r.length());
188   EXPECT_TRUE(r.is_reversed());
189   EXPECT_TRUE(r.IsValid());
190 
191   r.set_end(21);
192   EXPECT_EQ(25U, r.start());
193   EXPECT_EQ(21U, r.end());
194   EXPECT_EQ(4U, r.length());
195   EXPECT_TRUE(r.IsValid());
196   EXPECT_EQ(21U, r.GetMin());
197   EXPECT_EQ(25U, r.GetMax());
198 }
199 
TYPED_TEST(RangeTest,ContainAndIntersect)200 TYPED_TEST(RangeTest, ContainAndIntersect) {
201   {
202     SCOPED_TRACE("contain and intersect");
203     TypeParam r1(10, 12);
204     TypeParam r2(1, 8);
205     TypeParam r3(5, 12);
206     TestContainsAndIntersects(r1, r2, r3);
207   }
208   {
209     SCOPED_TRACE("contain and intersect: reversed");
210     TypeParam r1(12, 10);
211     TypeParam r2(8, 1);
212     TypeParam r3(12, 5);
213     TestContainsAndIntersects(r1, r2, r3);
214   }
215   // Invalid rect tests
216   TypeParam r1(10, 12);
217   TypeParam r2(8, 1);
218   TypeParam invalid = r1.Intersect(r2);
219   EXPECT_FALSE(invalid.IsValid());
220   EXPECT_FALSE(invalid.Contains(invalid));
221   EXPECT_FALSE(invalid.Contains(r1));
222   EXPECT_FALSE(invalid.Intersects(invalid));
223   EXPECT_FALSE(invalid.Intersects(r1));
224   EXPECT_FALSE(r1.Contains(invalid));
225   EXPECT_FALSE(r1.Intersects(invalid));
226 }
227 
TEST(RangeTest,RangeFConverterTest)228 TEST(RangeTest, RangeFConverterTest) {
229   gfx::RangeF range_f(1.2f, 3.9f);
230   gfx::Range range = range_f.Floor();
231   EXPECT_EQ(1U, range.start());
232   EXPECT_EQ(3U, range.end());
233 
234   range = range_f.Ceil();
235   EXPECT_EQ(2U, range.start());
236   EXPECT_EQ(4U, range.end());
237 
238   range = range_f.Round();
239   EXPECT_EQ(1U, range.start());
240   EXPECT_EQ(4U, range.end());
241 
242   // Test for negative values.
243   range_f.set_start(-1.2f);
244   range_f.set_end(-3.8f);
245   range = range_f.Floor();
246   EXPECT_EQ(0U, range.start());
247   EXPECT_EQ(0U, range.end());
248 
249   range = range_f.Ceil();
250   EXPECT_EQ(0U, range.start());
251   EXPECT_EQ(0U, range.end());
252 
253   range = range_f.Round();
254   EXPECT_EQ(0U, range.start());
255   EXPECT_EQ(0U, range.end());
256 }
257 
TEST(RangeTest,ToString)258 TEST(RangeTest, ToString) {
259   gfx::Range range(4, 7);
260   EXPECT_EQ("{4,7}", range.ToString());
261 
262   range = gfx::Range::InvalidRange();
263   std::ostringstream expected;
264   expected << "{" << range.start() << "," << range.end() << "}";
265   EXPECT_EQ(expected.str(), range.ToString());
266 }
267