1 // Copyright 2014 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.
8 #include "base/basictypes.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/geometry/scroll_offset.h"
14 TEST(ScrollOffsetTest
, IsZero
) {
15 ScrollOffset
zero(0, 0);
16 ScrollOffset
nonzero(0.1, -0.1);
18 EXPECT_TRUE(zero
.IsZero());
19 EXPECT_FALSE(nonzero
.IsZero());
22 TEST(ScrollOffsetTest
, Add
) {
23 ScrollOffset
f1(3.1, 5.1);
24 ScrollOffset
f2(4.3, -1.3);
27 ScrollOffset expected
;
29 } scroll_offset_tests
[] = {
30 { ScrollOffset(3.1, 5.1), f1
+ ScrollOffset() },
31 { ScrollOffset(3.1 + 4.3, 5.1f
- 1.3), f1
+ f2
},
32 { ScrollOffset(3.1 - 4.3, 5.1f
+ 1.3), f1
- f2
}
35 for (size_t i
= 0; i
< arraysize(scroll_offset_tests
); ++i
)
36 EXPECT_EQ(scroll_offset_tests
[i
].expected
.ToString(),
37 scroll_offset_tests
[i
].actual
.ToString());
40 TEST(ScrollOffsetTest
, Negative
) {
42 ScrollOffset expected
;
44 } scroll_offset_tests
[] = {
45 { ScrollOffset(-0.3, -0.3), -ScrollOffset(0.3, 0.3) },
46 { ScrollOffset(0.3, 0.3), -ScrollOffset(-0.3, -0.3) },
47 { ScrollOffset(-0.3, 0.3), -ScrollOffset(0.3, -0.3) },
48 { ScrollOffset(0.3, -0.3), -ScrollOffset(-0.3, 0.3) }
51 for (size_t i
= 0; i
< arraysize(scroll_offset_tests
); ++i
)
52 EXPECT_EQ(scroll_offset_tests
[i
].expected
.ToString(),
53 scroll_offset_tests
[i
].actual
.ToString());
56 TEST(ScrollOffsetTest
, Scale
) {
57 double double_values
[][4] = {
58 { 4.5, 1.2, 3.3, 5.6 },
59 { 4.5, -1.2, 3.3, 5.6 },
60 { 4.5, 1.2, 3.3, -5.6 },
61 { 4.5, 1.2, -3.3, -5.6 },
62 { -4.5, 1.2, 3.3, 5.6 },
63 { -4.5, 1.2, 0, 5.6 },
64 { -4.5, 1.2, 3.3, 0 },
69 for (size_t i
= 0; i
< arraysize(double_values
); ++i
) {
70 ScrollOffset
v(double_values
[i
][0], double_values
[i
][1]);
71 v
.Scale(double_values
[i
][2], double_values
[i
][3]);
72 EXPECT_EQ(v
.x(), double_values
[i
][0] * double_values
[i
][2]);
73 EXPECT_EQ(v
.y(), double_values
[i
][1] * double_values
[i
][3]);
76 double single_values
[][3] = {
88 for (size_t i
= 0; i
< arraysize(single_values
); ++i
) {
89 ScrollOffset
v(single_values
[i
][0], single_values
[i
][1]);
90 v
.Scale(single_values
[i
][2]);
91 EXPECT_EQ(v
.x(), single_values
[i
][0] * single_values
[i
][2]);
92 EXPECT_EQ(v
.y(), single_values
[i
][1] * single_values
[i
][2]);
96 TEST(ScrollOffsetTest
, ClampScrollOffset
) {
99 a
= ScrollOffset(3.5, 5.5);
100 EXPECT_EQ(ScrollOffset(3.5, 5.5).ToString(), a
.ToString());
101 a
.SetToMax(ScrollOffset(2.5, 4.5));
102 EXPECT_EQ(ScrollOffset(3.5, 5.5).ToString(), a
.ToString());
103 a
.SetToMax(ScrollOffset(3.5, 5.5));
104 EXPECT_EQ(ScrollOffset(3.5, 5.5).ToString(), a
.ToString());
105 a
.SetToMax(ScrollOffset(4.5, 2.5));
106 EXPECT_EQ(ScrollOffset(4.5, 5.5).ToString(), a
.ToString());
107 a
.SetToMax(ScrollOffset(8.5, 10.5));
108 EXPECT_EQ(ScrollOffset(8.5, 10.5).ToString(), a
.ToString());
110 a
.SetToMin(ScrollOffset(9.5, 11.5));
111 EXPECT_EQ(ScrollOffset(8.5, 10.5).ToString(), a
.ToString());
112 a
.SetToMin(ScrollOffset(8.5, 10.5));
113 EXPECT_EQ(ScrollOffset(8.5, 10.5).ToString(), a
.ToString());
114 a
.SetToMin(ScrollOffset(11.5, 9.5));
115 EXPECT_EQ(ScrollOffset(8.5, 9.5).ToString(), a
.ToString());
116 a
.SetToMin(ScrollOffset(7.5, 11.5));
117 EXPECT_EQ(ScrollOffset(7.5, 9.5).ToString(), a
.ToString());
118 a
.SetToMin(ScrollOffset(3.5, 5.5));
119 EXPECT_EQ(ScrollOffset(3.5, 5.5).ToString(), a
.ToString());