Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / interpolated_transform_unittest.cc
blobbe95eaceca328194bd7f3092d0380eb8c48bb527
1 // Copyright (c) 2012 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 "ui/gfx/interpolated_transform.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/geometry/rect.h"
11 namespace {
13 void CheckApproximatelyEqual(const gfx::Transform& lhs,
14 const gfx::Transform& rhs) {
15 for (int i = 0; i < 4; ++i) {
16 for (int j = 0; j < 4; ++j) {
17 EXPECT_FLOAT_EQ(lhs.matrix().get(i, j), rhs.matrix().get(i, j));
22 } // namespace
24 TEST(InterpolatedTransformTest, InterpolatedRotation) {
25 ui::InterpolatedRotation interpolated_rotation(0, 100);
26 ui::InterpolatedRotation interpolated_rotation_diff_start_end(
27 0, 100, 100, 200);
29 for (int i = 0; i <= 100; ++i) {
30 gfx::Transform rotation;
31 rotation.Rotate(i);
32 gfx::Transform interpolated = interpolated_rotation.Interpolate(i / 100.0f);
33 CheckApproximatelyEqual(rotation, interpolated);
34 interpolated = interpolated_rotation_diff_start_end.Interpolate(i + 100);
35 CheckApproximatelyEqual(rotation, interpolated);
39 TEST(InterpolatedTransformTest, InterpolatedScale) {
40 ui::InterpolatedScale interpolated_scale(gfx::Point3F(0, 0, 0),
41 gfx::Point3F(100, 100, 100));
42 ui::InterpolatedScale interpolated_scale_diff_start_end(
43 gfx::Point3F(0, 0, 0), gfx::Point3F(100, 100, 100), 100, 200);
45 for (int i = 0; i <= 100; ++i) {
46 gfx::Transform scale;
47 scale.Scale3d(i, i, i);
48 gfx::Transform interpolated = interpolated_scale.Interpolate(i / 100.0f);
49 CheckApproximatelyEqual(scale, interpolated);
50 interpolated = interpolated_scale_diff_start_end.Interpolate(i + 100);
51 CheckApproximatelyEqual(scale, interpolated);
55 TEST(InterpolatedTransformTest, InterpolatedTranslate) {
56 ui::InterpolatedTranslation interpolated_xform(gfx::Point(0, 0),
57 gfx::Point(100, 100));
59 ui::InterpolatedTranslation interpolated_xform_diff_start_end(
60 gfx::Point(0, 0), gfx::Point(100, 100), 100, 200);
62 for (int i = 0; i <= 100; ++i) {
63 gfx::Transform xform;
64 xform.Translate(i, i);
65 gfx::Transform interpolated = interpolated_xform.Interpolate(i / 100.0f);
66 CheckApproximatelyEqual(xform, interpolated);
67 interpolated = interpolated_xform_diff_start_end.Interpolate(i + 100);
68 CheckApproximatelyEqual(xform, interpolated);
72 TEST(InterpolatedTransformTest, InterpolatedTranslate3d) {
73 ui::InterpolatedTranslation interpolated_xform(gfx::Point3F(0, 0, 0),
74 gfx::Point3F(100, 100, 100));
76 ui::InterpolatedTranslation interpolated_xform_diff_start_end(
77 gfx::Point3F(0, 0, 0), gfx::Point3F(100, 100, 100), 100, 200);
79 for (int i = 0; i <= 100; ++i) {
80 gfx::Transform xform;
81 xform.Translate3d(i, i, i);
82 gfx::Transform interpolated = interpolated_xform.Interpolate(i / 100.0f);
83 CheckApproximatelyEqual(xform, interpolated);
84 interpolated = interpolated_xform_diff_start_end.Interpolate(i + 100);
85 CheckApproximatelyEqual(xform, interpolated);
89 TEST(InterpolatedTransformTest, InterpolatedRotationAboutPivot) {
90 gfx::Point pivot(100, 100);
91 gfx::Point above_pivot(100, 200);
92 ui::InterpolatedRotation rot(0, 90);
93 ui::InterpolatedTransformAboutPivot interpolated_xform(
94 pivot,
95 new ui::InterpolatedRotation(0, 90));
96 gfx::Transform result = interpolated_xform.Interpolate(0.0f);
97 CheckApproximatelyEqual(gfx::Transform(), result);
98 result = interpolated_xform.Interpolate(1.0f);
99 gfx::Point expected_result = pivot;
100 result.TransformPoint(&pivot);
101 EXPECT_EQ(expected_result, pivot);
102 expected_result = gfx::Point(0, 100);
103 result.TransformPoint(&above_pivot);
104 EXPECT_EQ(expected_result, above_pivot);
107 TEST(InterpolatedTransformTest, InterpolatedScaleAboutPivot) {
108 gfx::Point pivot(100, 100);
109 gfx::Point above_pivot(100, 200);
110 ui::InterpolatedTransformAboutPivot interpolated_xform(
111 pivot,
112 new ui::InterpolatedScale(gfx::Point3F(1, 1, 1), gfx::Point3F(2, 2, 2)));
113 gfx::Transform result = interpolated_xform.Interpolate(0.0f);
114 CheckApproximatelyEqual(gfx::Transform(), result);
115 result = interpolated_xform.Interpolate(1.0f);
116 gfx::Point expected_result = pivot;
117 result.TransformPoint(&pivot);
118 EXPECT_EQ(expected_result, pivot);
119 expected_result = gfx::Point(100, 300);
120 result.TransformPoint(&above_pivot);
121 EXPECT_EQ(expected_result, above_pivot);
124 ui::InterpolatedTransform* GetScreenRotation(int degrees, bool reversed) {
125 gfx::Point old_pivot;
126 gfx::Point new_pivot;
128 int width = 1920;
129 int height = 180;
131 switch (degrees) {
132 case 90:
133 new_pivot = gfx::Point(width, 0);
134 break;
135 case -90:
136 new_pivot = gfx::Point(0, height);
137 break;
138 case 180:
139 case 360:
140 new_pivot = old_pivot = gfx::Point(width / 2, height / 2);
141 break;
144 scoped_ptr<ui::InterpolatedTransform> rotation(
145 new ui::InterpolatedTransformAboutPivot(
146 old_pivot,
147 new ui::InterpolatedRotation(reversed ? degrees : 0,
148 reversed ? 0 : degrees)));
150 scoped_ptr<ui::InterpolatedTransform> translation(
151 new ui::InterpolatedTranslation(
152 gfx::Point(0, 0),
153 gfx::Point(new_pivot.x() - old_pivot.x(),
154 new_pivot.y() - old_pivot.y())));
156 float scale_factor = 0.9f;
157 scoped_ptr<ui::InterpolatedTransform> scale_down(
158 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f));
160 scoped_ptr<ui::InterpolatedTransform> scale_up(
161 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f));
163 scoped_ptr<ui::InterpolatedTransform> to_return(
164 new ui::InterpolatedConstantTransform(gfx::Transform()));
166 scale_up->SetChild(scale_down.release());
167 translation->SetChild(scale_up.release());
168 rotation->SetChild(translation.release());
169 to_return->SetChild(rotation.release());
170 to_return->SetReversed(reversed);
172 return to_return.release();
175 TEST(InterpolatedTransformTest, ScreenRotationEndsCleanly) {
176 for (int i = 0; i < 2; ++i) {
177 for (int degrees = -360; degrees <= 360; degrees += 90) {
178 const bool reversed = i == 1;
179 scoped_ptr<ui::InterpolatedTransform> screen_rotation(
180 GetScreenRotation(degrees, reversed));
181 gfx::Transform interpolated = screen_rotation->Interpolate(1.0f);
182 SkMatrix44& m = interpolated.matrix();
183 // Upper-left 3x3 matrix should all be 0, 1 or -1.
184 for (int row = 0; row < 3; ++row) {
185 for (int col = 0; col < 3; ++col) {
186 float entry = m.get(row, col);
187 EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1);
194 ui::InterpolatedTransform* GetMaximize() {
195 gfx::Rect target_bounds(0, 0, 1920, 1080);
196 gfx::Rect initial_bounds(30, 1000, 192, 108);
198 float scale_x = static_cast<float>(
199 target_bounds.height()) / initial_bounds.width();
200 float scale_y = static_cast<float>(
201 target_bounds.width()) / initial_bounds.height();
203 scoped_ptr<ui::InterpolatedTransform> scale(
204 new ui::InterpolatedScale(gfx::Point3F(1, 1, 1),
205 gfx::Point3F(scale_x, scale_y, 1)));
207 scoped_ptr<ui::InterpolatedTransform> translation(
208 new ui::InterpolatedTranslation(
209 gfx::Point(),
210 gfx::Point(target_bounds.x() - initial_bounds.x(),
211 target_bounds.y() - initial_bounds.y())));
213 scoped_ptr<ui::InterpolatedTransform> rotation(
214 new ui::InterpolatedRotation(0, 4.0f));
216 scoped_ptr<ui::InterpolatedTransform> rotation_about_pivot(
217 new ui::InterpolatedTransformAboutPivot(
218 gfx::Point(initial_bounds.width() * 0.5,
219 initial_bounds.height() * 0.5),
220 rotation.release()));
222 scale->SetChild(translation.release());
223 rotation_about_pivot->SetChild(scale.release());
225 rotation_about_pivot->SetReversed(true);
227 return rotation_about_pivot.release();
230 TEST(InterpolatedTransformTest, MaximizeEndsCleanly) {
231 scoped_ptr<ui::InterpolatedTransform> maximize(GetMaximize());
232 gfx::Transform interpolated = maximize->Interpolate(1.0f);
233 SkMatrix44& m = interpolated.matrix();
234 // Upper-left 3x3 matrix should all be 0, 1 or -1.
235 for (int row = 0; row < 3; ++row) {
236 for (int col = 0; col < 3; ++col) {
237 float entry = m.get(row, col);
238 EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1);