| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/views/animation/square_ink_drop_ripple.h" | 5 #include "ui/views/animation/square_ink_drop_ripple.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/gfx/geometry/point.h" | 11 #include "ui/gfx/geometry/point.h" |
| 12 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 #include "ui/gfx/geometry/size_f.h" | 13 #include "ui/gfx/geometry/size_f.h" |
| 14 #include "ui/views/animation/ink_drop_ripple_observer.h" | 14 #include "ui/views/animation/ink_drop_ripple_observer.h" |
| 15 #include "ui/views/animation/ink_drop_state.h" | 15 #include "ui/views/animation/ink_drop_state.h" |
| 16 #include "ui/views/animation/test/square_ink_drop_ripple_test_api.h" | 16 #include "ui/views/animation/test/square_ink_drop_ripple_test_api.h" |
| 17 #include "ui/views/animation/test/test_ink_drop_ripple_observer.h" | 17 #include "ui/views/animation/test/test_ink_drop_ripple_observer.h" |
| 18 #include "ui/views/test/widget_test.h" |
| 18 | 19 |
| 19 namespace views { | 20 namespace views { |
| 20 namespace test { | 21 namespace test { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 using PaintedShape = views::test::SquareInkDropRippleTestApi::PaintedShape; | 25 using PaintedShape = views::test::SquareInkDropRippleTestApi::PaintedShape; |
| 25 | 26 |
| 26 // Transforms a copy of |point| with |transform| and returns it. | 27 // Transforms a copy of |point| with |transform| and returns it. |
| 27 gfx::Point TransformPoint(const gfx::Transform& transform, | 28 gfx::Point TransformPoint(const gfx::Transform& transform, |
| 28 const gfx::Point& point) { | 29 const gfx::Point& point) { |
| 29 gfx::Point transformed_point = point; | 30 gfx::Point transformed_point = point; |
| 30 transform.TransformPoint(&transformed_point); | 31 transform.TransformPoint(&transformed_point); |
| 31 return transformed_point; | 32 return transformed_point; |
| 32 } | 33 } |
| 33 | 34 |
| 34 class SquareInkDropRippleCalculateTransformsTest : public testing::Test { | 35 class SquareInkDropRippleCalculateTransformsTest : public WidgetTest { |
| 35 public: | 36 public: |
| 36 SquareInkDropRippleCalculateTransformsTest(); | 37 SquareInkDropRippleCalculateTransformsTest(); |
| 37 ~SquareInkDropRippleCalculateTransformsTest() override; | 38 ~SquareInkDropRippleCalculateTransformsTest() override; |
| 38 | 39 |
| 39 protected: | 40 protected: |
| 40 // Half the width/height of the drawn ink drop. | 41 // Half the width/height of the drawn ink drop. |
| 41 static const int kHalfDrawnSize; | 42 static const int kHalfDrawnSize; |
| 42 | 43 |
| 43 // The full width/height of the drawn ink drop. | 44 // The full width/height of the drawn ink drop. |
| 44 static const int kDrawnSize; | 45 static const int kDrawnSize; |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 TransformPoint(transform, kDrawnMidLeftPoint)); | 232 TransformPoint(transform, kDrawnMidLeftPoint)); |
| 232 EXPECT_EQ(test_cases[i].mid_right_point, | 233 EXPECT_EQ(test_cases[i].mid_right_point, |
| 233 TransformPoint(transform, kDrawnMidRightPoint)); | 234 TransformPoint(transform, kDrawnMidRightPoint)); |
| 234 EXPECT_EQ(test_cases[i].top_mid_point, | 235 EXPECT_EQ(test_cases[i].top_mid_point, |
| 235 TransformPoint(transform, kDrawnTopMidPoint)); | 236 TransformPoint(transform, kDrawnTopMidPoint)); |
| 236 EXPECT_EQ(test_cases[i].bottom_mid_point, | 237 EXPECT_EQ(test_cases[i].bottom_mid_point, |
| 237 TransformPoint(transform, kDrawnBottomMidPoint)); | 238 TransformPoint(transform, kDrawnBottomMidPoint)); |
| 238 } | 239 } |
| 239 } | 240 } |
| 240 | 241 |
| 242 TEST_F(SquareInkDropRippleCalculateTransformsTest, RippleIsPixelAligned) { |
| 243 // Create a ripple that would not naturally be pixel aligned at a fractional |
| 244 // scale factor. |
| 245 const gfx::Point center(14, 14); |
| 246 const gfx::Rect drawn_rect_bounds(0, 0, 10, 10); |
| 247 SquareInkDropRipple ink_drop_ripple(drawn_rect_bounds.size(), 2, |
| 248 gfx::Size(1, 1), // unimportant |
| 249 1, center, SK_ColorBLACK, 0.175f); |
| 250 SquareInkDropRippleTestApi test_api(&ink_drop_ripple); |
| 251 |
| 252 // Add to a widget so we can control the DSF. |
| 253 auto* widget = CreateTopLevelPlatformWidget(); |
| 254 widget->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 255 auto* host_view = new View(); |
| 256 host_view->SetPaintToLayer(); |
| 257 widget->GetContentsView()->AddChildView(host_view); |
| 258 host_view->layer()->Add(ink_drop_ripple.GetRootLayer()); |
| 259 |
| 260 // Test a variety of scale factors and target transform sizes. |
| 261 std::vector<float> dsfs({1.0f, 1.25f, 1.5f, 2.0f, 3.0f}); |
| 262 std::vector<int> target_sizes({5, 7, 11, 13, 31}); |
| 263 |
| 264 for (float dsf : dsfs) { |
| 265 for (int target_size : target_sizes) { |
| 266 SCOPED_TRACE(testing::Message() |
| 267 << "target_size=" << target_size << " dsf=" << dsf); |
| 268 host_view->layer()->GetCompositor()->SetScaleAndSize(dsf, |
| 269 gfx::Size(100, 100)); |
| 270 |
| 271 SquareInkDropRippleTestApi::InkDropTransforms transforms; |
| 272 test_api.CalculateRectTransforms(gfx::Size(target_size, target_size), 0, |
| 273 &transforms); |
| 274 |
| 275 // Checks that a rectangle is integer-aligned modulo floating point error. |
| 276 auto verify_bounds = [](const gfx::RectF& rect) { |
| 277 float float_min_x = rect.x(); |
| 278 float float_min_y = rect.y(); |
| 279 float float_max_x = rect.right(); |
| 280 float float_max_y = rect.bottom(); |
| 281 |
| 282 int min_x = gfx::ToRoundedInt(float_min_x); |
| 283 int min_y = gfx::ToRoundedInt(float_min_y); |
| 284 int max_x = gfx::ToRoundedInt(float_max_x); |
| 285 int max_y = gfx::ToRoundedInt(float_max_y); |
| 286 |
| 287 EXPECT_LT(std::abs(min_x - float_min_x), 0.01f); |
| 288 EXPECT_LT(std::abs(min_y - float_min_y), 0.01f); |
| 289 EXPECT_LT(std::abs(max_x - float_max_x), 0.01f); |
| 290 EXPECT_LT(std::abs(max_y - float_max_y), 0.01f); |
| 291 }; |
| 292 |
| 293 // When you feed in the bounds of the rectangle layer delegate, no matter |
| 294 // what the target size was you should get an integer aligned bounding |
| 295 // box. |
| 296 gfx::Transform transform = transforms[PaintedShape::HORIZONTAL_RECT]; |
| 297 gfx::RectF horizontal_rect(drawn_rect_bounds); |
| 298 transform.TransformRect(&horizontal_rect); |
| 299 horizontal_rect.Scale(dsf); |
| 300 verify_bounds(horizontal_rect); |
| 301 |
| 302 transform = transforms[PaintedShape::VERTICAL_RECT]; |
| 303 gfx::RectF vertical_rect(drawn_rect_bounds); |
| 304 transform.TransformRect(&vertical_rect); |
| 305 vertical_rect.Scale(dsf); |
| 306 verify_bounds(vertical_rect); |
| 307 } |
| 308 } |
| 309 |
| 310 widget->CloseNow(); |
| 311 } |
| 312 |
| 241 } // namespace test | 313 } // namespace test |
| 242 } // namespace views | 314 } // namespace views |
| OLD | NEW |