Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: ui/gfx/interpolated_transform_unittest.cc

Issue 10210002: Interpolated rotations should end cleanly where possible. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 &rotation, 131 &rotation,
131 &scale); 132 &scale);
132 EXPECT_TRUE(success); 133 EXPECT_TRUE(success);
133 EXPECT_FLOAT_EQ(translation.x(), degrees * 2); 134 EXPECT_FLOAT_EQ(translation.x(), degrees * 2);
134 EXPECT_FLOAT_EQ(translation.y(), -degrees * 3); 135 EXPECT_FLOAT_EQ(translation.y(), -degrees * 3);
135 EXPECT_FLOAT_EQ(NormalizeAngle(rotation), degrees); 136 EXPECT_FLOAT_EQ(NormalizeAngle(rotation), degrees);
136 EXPECT_FLOAT_EQ(scale.x(), degrees + 1); 137 EXPECT_FLOAT_EQ(scale.x(), degrees + 1);
137 EXPECT_FLOAT_EQ(scale.y(), 2 * degrees + 1); 138 EXPECT_FLOAT_EQ(scale.y(), 2 * degrees + 1);
138 } 139 }
139 } 140 }
141
142 ui::InterpolatedTransform* GetScreenRotation(int degrees, bool reversed) {
143 gfx::Point old_pivot;
144 gfx::Point new_pivot;
145
146 int width = 1920;
147 int height = 180;
148
149 switch (degrees) {
150 case 90:
151 new_pivot = gfx::Point(width, 0);
152 break;
153 case -90:
154 new_pivot = gfx::Point(0, height);
155 break;
156 case 180:
157 case 360:
158 new_pivot = old_pivot = gfx::Point(width / 2, height / 2);
159 break;
160 }
161
162 scoped_ptr<ui::InterpolatedTransform> rotation(
163 new ui::InterpolatedTransformAboutPivot(
164 old_pivot,
165 new ui::InterpolatedRotation(reversed ? degrees : 0,
166 reversed ? 0 : degrees)));
167
168 scoped_ptr<ui::InterpolatedTransform> translation(
169 new ui::InterpolatedTranslation(
170 gfx::Point(0, 0),
171 gfx::Point(new_pivot.x() - old_pivot.x(),
172 new_pivot.y() - old_pivot.y())));
173
174 float scale_factor = 0.9f;
175 scoped_ptr<ui::InterpolatedTransform> scale_down(
176 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f));
177
178 scoped_ptr<ui::InterpolatedTransform> scale_up(
179 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f));
180
181 scoped_ptr<ui::InterpolatedTransform> to_return(
182 new ui::InterpolatedConstantTransform(ui::Transform()));
183
184 scale_up->SetChild(scale_down.release());
185 translation->SetChild(scale_up.release());
186 rotation->SetChild(translation.release());
187 to_return->SetChild(rotation.release());
188 to_return->SetReversed(reversed);
189
190 return to_return.release();
191 }
192
193 TEST(InterpolatedTransformTest, ScreenRotationEndsCleanly) {
James Cook 2012/04/24 17:18:18 Awesome that you added a test for this!
194 for (int i = 0; i < 2; ++i) {
195 for (int degrees = -360; degrees <= 360; degrees += 90) {
196 const bool reversed = i == 1;
197 scoped_ptr<ui::InterpolatedTransform> screen_rotation(
198 GetScreenRotation(degrees, reversed));
199 ui::Transform interpolated = screen_rotation->Interpolate(1.0f);
200 SkMatrix44& m = interpolated.matrix();
201 // Upper-left 3x3 matrix should all be 0, 1 or -1.
202
203 for (int row = 0; row < 3; ++row) {
204 for (int col = 0; col < 3; ++col) {
205 float entry = m.get(row, col);
206 EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1);
207 }
208 }
209 }
210 }
211 }
212
213 ui::InterpolatedTransform* GetMaximize() {
214 gfx::Rect target_bounds(0, 0, 1920, 1080);
215 gfx::Rect initial_bounds(30, 1000, 192, 108);
216
217 float scale_x = static_cast<float>(
218 target_bounds.height()) / initial_bounds.width();
219 float scale_y = static_cast<float>(
220 target_bounds.width()) / initial_bounds.height();
221
222 scoped_ptr<ui::InterpolatedTransform> scale(
223 new ui::InterpolatedScale(gfx::Point3f(1, 1, 1),
224 gfx::Point3f(scale_x, scale_y, 1)));
225
226 scoped_ptr<ui::InterpolatedTransform> translation(
227 new ui::InterpolatedTranslation(
228 gfx::Point(),
229 gfx::Point(target_bounds.x() - initial_bounds.x(),
230 target_bounds.y() - initial_bounds.y())));
231
232 scoped_ptr<ui::InterpolatedTransform> rotation(
233 new ui::InterpolatedRotation(0, 4.0f));
234
235 scoped_ptr<ui::InterpolatedTransform> rotation_about_pivot(
236 new ui::InterpolatedTransformAboutPivot(
237 gfx::Point(initial_bounds.width() * 0.5,
238 initial_bounds.height() * 0.5),
239 rotation.release()));
240
241 scale->SetChild(translation.release());
242 rotation_about_pivot->SetChild(scale.release());
243
244 rotation_about_pivot->SetReversed(true);
245
246 return rotation_about_pivot.release();
247 }
248
249 TEST(InterpolatedTransformTest, MaximizeEndsCleanly) {
250 scoped_ptr<ui::InterpolatedTransform> maximize(GetMaximize());
251 ui::Transform interpolated = maximize->Interpolate(1.0f);
252 SkMatrix44& m = interpolated.matrix();
253 // Upper-left 3x3 matrix should all be 0, 1 or -1.
254 for (int row = 0; row < 3; ++row) {
255 for (int col = 0; col < 3; ++col) {
256 float entry = m.get(row, col);
257 EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1);
258 }
259 }
260 }
261
OLDNEW
« ui/gfx/interpolated_transform.cc ('K') | « ui/gfx/interpolated_transform.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698