Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / geometry / scroll_offset_unittest.cc
blob72073c14356d8c91559ed2fdefa5fada147c77e6
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.
5 #include <cmath>
6 #include <limits>
8 #include "base/basictypes.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/geometry/scroll_offset.h"
12 namespace gfx {
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);
26 const struct {
27 ScrollOffset expected;
28 ScrollOffset actual;
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) {
41 const struct {
42 ScrollOffset expected;
43 ScrollOffset actual;
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 },
65 { 4.5, 0, 3.3, 5.6 },
66 { 0, 1.2, 3.3, 5.6 }
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] = {
77 { 4.5, 1.2, 3.3 },
78 { 4.5, -1.2, 3.3 },
79 { 4.5, 1.2, 3.3 },
80 { 4.5, 1.2, -3.3 },
81 { -4.5, 1.2, 3.3 },
82 { -4.5, 1.2, 0 },
83 { -4.5, 1.2, 3.3 },
84 { 4.5, 0, 3.3 },
85 { 0, 1.2, 3.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) {
97 ScrollOffset a;
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());
122 } // namespace gfx