OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/interpolated_transform.h" | 5 #include "ui/gfx/interpolated_transform.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/rect.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
12 void CheckApproximatelyEqual(const ui::Transform& lhs, | 13 void CheckApproximatelyEqual(const ui::Transform& lhs, |
13 const ui::Transform& rhs) { | 14 const ui::Transform& rhs) { |
14 for (int i = 0; i < 4; ++i) { | 15 for (int i = 0; i < 4; ++i) { |
15 for (int j = 0; j < 4; ++j) { | 16 for (int j = 0; j < 4; ++j) { |
16 EXPECT_FLOAT_EQ(lhs.matrix().get(i, j), rhs.matrix().get(i, j)); | 17 EXPECT_FLOAT_EQ(lhs.matrix().get(i, j), rhs.matrix().get(i, j)); |
17 } | 18 } |
18 } | 19 } |
19 } | 20 } |
20 | 21 |
21 float NormalizeAngle(float angle) { | 22 float NormalizeAngle(float angle) { |
22 while (angle < 0.0f) { | 23 while (angle < 0.0f) { |
23 angle += 360.0f; | 24 angle += 360.0f; |
24 } | 25 } |
25 while (angle > 360.0f) { | 26 while (angle > 360.0f) { |
26 angle -= 360.0f; | 27 angle -= 360.0f; |
27 } | 28 } |
28 return angle; | 29 return angle; |
29 } | 30 } |
30 | 31 |
| 32 const float EPSILON = 1e-6; |
| 33 |
| 34 // Ensures all entries are either 0, 1, or -1 |
| 35 void MassageRotationMatrix(ui::Transform* transform) { |
| 36 SkMatrix44& m = transform->matrix(); |
| 37 for (int i = 0; i < 3; ++i) { |
| 38 for (int j = 0; j < 3; ++j) { |
| 39 float entry = m.get(i, j); |
| 40 if (fabs(entry) < EPSILON) |
| 41 entry = 0; |
| 42 else if (fabs(entry + 1) < EPSILON) |
| 43 entry = -1; |
| 44 else if (fabs(entry - 1) < EPSILON) |
| 45 entry = 1; |
| 46 m.set(i, j, entry); |
| 47 } |
| 48 } |
| 49 } |
| 50 |
31 } // namespace | 51 } // namespace |
32 | 52 |
33 TEST(InterpolatedTransformTest, InterpolatedRotation) { | 53 TEST(InterpolatedTransformTest, InterpolatedRotation) { |
34 ui::InterpolatedRotation interpolated_rotation(0, 100); | 54 ui::InterpolatedRotation interpolated_rotation(0, 100); |
35 ui::InterpolatedRotation interpolated_rotation_diff_start_end( | 55 ui::InterpolatedRotation interpolated_rotation_diff_start_end( |
36 0, 100, 100, 200); | 56 0, 100, 100, 200); |
37 | 57 |
38 for (int i = 0; i <= 100; ++i) { | 58 for (int i = 0; i <= 100; ++i) { |
39 ui::Transform rotation; | 59 ui::Transform rotation; |
40 rotation.SetRotate(i); | 60 rotation.SetRotate(i); |
| 61 |
| 62 // If we're a multiple of 90 degrees, we must ensure that our matrix is |
| 63 // 'clean' -- that is, that its entries are all 0, 1 or -1. |
| 64 if (i % 90 == 0) |
| 65 MassageRotationMatrix(&rotation); |
| 66 |
41 ui::Transform interpolated = interpolated_rotation.Interpolate(i / 100.0f); | 67 ui::Transform interpolated = interpolated_rotation.Interpolate(i / 100.0f); |
42 CheckApproximatelyEqual(rotation, interpolated); | 68 CheckApproximatelyEqual(rotation, interpolated); |
43 interpolated = interpolated_rotation_diff_start_end.Interpolate(i + 100); | 69 interpolated = interpolated_rotation_diff_start_end.Interpolate(i + 100); |
44 CheckApproximatelyEqual(rotation, interpolated); | 70 CheckApproximatelyEqual(rotation, interpolated); |
45 } | 71 } |
46 } | 72 } |
47 | 73 |
48 TEST(InterpolatedTransformTest, InterpolatedScale) { | 74 TEST(InterpolatedTransformTest, InterpolatedScale) { |
49 ui::InterpolatedScale interpolated_scale(gfx::Point3f(0, 0, 0), | 75 ui::InterpolatedScale interpolated_scale(gfx::Point3f(0, 0, 0), |
50 gfx::Point3f(100, 100, 100)); | 76 gfx::Point3f(100, 100, 100)); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 &rotation, | 156 &rotation, |
131 &scale); | 157 &scale); |
132 EXPECT_TRUE(success); | 158 EXPECT_TRUE(success); |
133 EXPECT_FLOAT_EQ(translation.x(), degrees * 2); | 159 EXPECT_FLOAT_EQ(translation.x(), degrees * 2); |
134 EXPECT_FLOAT_EQ(translation.y(), -degrees * 3); | 160 EXPECT_FLOAT_EQ(translation.y(), -degrees * 3); |
135 EXPECT_FLOAT_EQ(NormalizeAngle(rotation), degrees); | 161 EXPECT_FLOAT_EQ(NormalizeAngle(rotation), degrees); |
136 EXPECT_FLOAT_EQ(scale.x(), degrees + 1); | 162 EXPECT_FLOAT_EQ(scale.x(), degrees + 1); |
137 EXPECT_FLOAT_EQ(scale.y(), 2 * degrees + 1); | 163 EXPECT_FLOAT_EQ(scale.y(), 2 * degrees + 1); |
138 } | 164 } |
139 } | 165 } |
| 166 |
| 167 ui::InterpolatedTransform* GetScreenRotation(int degrees, bool reversed) { |
| 168 gfx::Point old_pivot; |
| 169 gfx::Point new_pivot; |
| 170 |
| 171 int width = 1920; |
| 172 int height = 180; |
| 173 |
| 174 switch (degrees) { |
| 175 case 90: |
| 176 new_pivot = gfx::Point(width, 0); |
| 177 break; |
| 178 case -90: |
| 179 new_pivot = gfx::Point(0, height); |
| 180 break; |
| 181 case 180: |
| 182 case 360: |
| 183 new_pivot = old_pivot = gfx::Point(width / 2, height / 2); |
| 184 break; |
| 185 } |
| 186 |
| 187 scoped_ptr<ui::InterpolatedTransform> rotation( |
| 188 new ui::InterpolatedTransformAboutPivot( |
| 189 old_pivot, |
| 190 new ui::InterpolatedRotation(reversed ? degrees : 0, |
| 191 reversed ? 0 : degrees))); |
| 192 |
| 193 scoped_ptr<ui::InterpolatedTransform> translation( |
| 194 new ui::InterpolatedTranslation( |
| 195 gfx::Point(0, 0), |
| 196 gfx::Point(new_pivot.x() - old_pivot.x(), |
| 197 new_pivot.y() - old_pivot.y()))); |
| 198 |
| 199 float scale_factor = 0.9f; |
| 200 scoped_ptr<ui::InterpolatedTransform> scale_down( |
| 201 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f)); |
| 202 |
| 203 scoped_ptr<ui::InterpolatedTransform> scale_up( |
| 204 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f)); |
| 205 |
| 206 scoped_ptr<ui::InterpolatedTransform> to_return( |
| 207 new ui::InterpolatedConstantTransform(ui::Transform())); |
| 208 |
| 209 scale_up->SetChild(scale_down.release()); |
| 210 translation->SetChild(scale_up.release()); |
| 211 rotation->SetChild(translation.release()); |
| 212 to_return->SetChild(rotation.release()); |
| 213 to_return->SetReversed(reversed); |
| 214 |
| 215 return to_return.release(); |
| 216 } |
| 217 |
| 218 TEST(InterpolatedTransformTest, ScreenRotationEndsCleanly) { |
| 219 for (int i = 0; i < 2; ++i) { |
| 220 for (int degrees = -360; degrees <= 360; degrees += 90) { |
| 221 const bool reversed = i == 1; |
| 222 scoped_ptr<ui::InterpolatedTransform> screen_rotation( |
| 223 GetScreenRotation(degrees, reversed)); |
| 224 ui::Transform interpolated = screen_rotation->Interpolate(1.0f); |
| 225 SkMatrix44& m = interpolated.matrix(); |
| 226 // Upper-left 3x3 matrix should all be 0, 1 or -1. |
| 227 for (int row = 0; row < 3; ++row) { |
| 228 for (int col = 0; col < 3; ++col) { |
| 229 float entry = m.get(row, col); |
| 230 EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1); |
| 231 } |
| 232 } |
| 233 } |
| 234 } |
| 235 } |
| 236 |
| 237 ui::InterpolatedTransform* GetMaximize() { |
| 238 gfx::Rect target_bounds(0, 0, 1920, 1080); |
| 239 gfx::Rect initial_bounds(30, 1000, 192, 108); |
| 240 |
| 241 float scale_x = static_cast<float>( |
| 242 target_bounds.height()) / initial_bounds.width(); |
| 243 float scale_y = static_cast<float>( |
| 244 target_bounds.width()) / initial_bounds.height(); |
| 245 |
| 246 scoped_ptr<ui::InterpolatedTransform> scale( |
| 247 new ui::InterpolatedScale(gfx::Point3f(1, 1, 1), |
| 248 gfx::Point3f(scale_x, scale_y, 1))); |
| 249 |
| 250 scoped_ptr<ui::InterpolatedTransform> translation( |
| 251 new ui::InterpolatedTranslation( |
| 252 gfx::Point(), |
| 253 gfx::Point(target_bounds.x() - initial_bounds.x(), |
| 254 target_bounds.y() - initial_bounds.y()))); |
| 255 |
| 256 scoped_ptr<ui::InterpolatedTransform> rotation( |
| 257 new ui::InterpolatedRotation(0, 4.0f)); |
| 258 |
| 259 scoped_ptr<ui::InterpolatedTransform> rotation_about_pivot( |
| 260 new ui::InterpolatedTransformAboutPivot( |
| 261 gfx::Point(initial_bounds.width() * 0.5, |
| 262 initial_bounds.height() * 0.5), |
| 263 rotation.release())); |
| 264 |
| 265 scale->SetChild(translation.release()); |
| 266 rotation_about_pivot->SetChild(scale.release()); |
| 267 |
| 268 rotation_about_pivot->SetReversed(true); |
| 269 |
| 270 return rotation_about_pivot.release(); |
| 271 } |
| 272 |
| 273 TEST(InterpolatedTransformTest, MaximizeEndsCleanly) { |
| 274 scoped_ptr<ui::InterpolatedTransform> maximize(GetMaximize()); |
| 275 ui::Transform interpolated = maximize->Interpolate(1.0f); |
| 276 SkMatrix44& m = interpolated.matrix(); |
| 277 // Upper-left 3x3 matrix should all be 0, 1 or -1. |
| 278 for (int row = 0; row < 3; ++row) { |
| 279 for (int col = 0; col < 3; ++col) { |
| 280 float entry = m.get(row, col); |
| 281 EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1); |
| 282 } |
| 283 } |
| 284 } |
| 285 |
OLD | NEW |