Index: ui/gfx/win/rect_util_unittest.cc |
diff --git a/ui/gfx/win/rect_util_unittest.cc b/ui/gfx/win/rect_util_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7fd245dd78321631597ee021543d231be1071115 |
--- /dev/null |
+++ b/ui/gfx/win/rect_util_unittest.cc |
@@ -0,0 +1,322 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/geometry/rect.h" |
+#include "ui/gfx/win/rect_util.h" |
+ |
+namespace gfx { |
+namespace win { |
+ |
+TEST(RectUtilTest, RectTouchBottom) { |
+ EXPECT_EQ(RectEdge::BOTTOM, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 2, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchLeft) { |
+ EXPECT_EQ(RectEdge::LEFT, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(0, 1, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchTop) { |
+ EXPECT_EQ(RectEdge::TOP, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(1, 0, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchRight) { |
+ EXPECT_EQ(RectEdge::RIGHT, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(2, 1, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchBottomRightCorner) { |
+ EXPECT_EQ(RectEdge::BOTTOM, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(2, 2, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchBottomLeftCorner) { |
+ EXPECT_EQ(RectEdge::BOTTOM, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(0, 2, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchUpperLeftCorner) { |
+ EXPECT_EQ(RectEdge::LEFT, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(0, 0, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchUpperRightCorner) { |
+ EXPECT_EQ(RectEdge::TOP, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(2, 0, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, RectTouchNone) { |
+ EXPECT_EQ(RectEdge::NONE, |
+ RectTouch(gfx::Rect(1, 1, 1, 1), gfx::Rect(3, 1, 1, 1))); |
+} |
+ |
+TEST(RectUtilTest, ScaleAndPositionRectNoScaleRight) { |
+ // Top edge aligned. |
+ EXPECT_EQ(gfx::Rect(800, 0, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(800, 0, 1024, 768), |
+ 1.0f)); |
+ // Bottom edge aligned. |
+ EXPECT_EQ(gfx::Rect(800, -168, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(800, -168, 1024, 768), |
+ 1.0f)); |
+ // Offset to the top. |
+ EXPECT_EQ(gfx::Rect(800, -10, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(800, -10, 1024, 768), |
+ 1.0f)); |
+ // Offset to the bottom. |
+ EXPECT_EQ(gfx::Rect(800, 10, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(800, 10, 1024, 768), |
+ 1.0f)); |
+ |
+} |
+ |
+TEST(RectUtilTest, ScaleAndPositionRectNoScaleLeft) { |
+ // Top edge aligned. |
+ EXPECT_EQ(gfx::Rect(-1024, 0, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-1024, 0, 1024, 768), |
+ 1.0f)); |
+ // Bottom edge aligned. |
+ EXPECT_EQ(gfx::Rect(-1024, -168, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-1024, -168, 1024, 768), |
+ 1.0f)); |
+ // Offset to the top. |
+ EXPECT_EQ(gfx::Rect(-1024, -10, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-1024, -10, 1024, 768), |
+ 1.0f)); |
+ // Offset to the bottom. |
+ EXPECT_EQ(gfx::Rect(-1024, 10, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-1024, 10, 1024, 768), |
+ 1.0f)); |
+} |
+ |
+TEST(RectUtilTest, ScaleAndPositionRectNoScaleTop) { |
+ // Left edge aligned. |
+ EXPECT_EQ(gfx::Rect(0, -768, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, -768, 1024, 768), |
+ 1.0f)); |
+ // Right edge aligned. |
+ EXPECT_EQ(gfx::Rect(-224, -768, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-224, -768, 1024, 768), |
+ 1.0f)); |
+ // Offset to the right. |
+ EXPECT_EQ(gfx::Rect(10, -768, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(10, -768, 1024, 768), |
+ 1.0f)); |
+ // Offset to the left. |
+ EXPECT_EQ(gfx::Rect(-10, -768, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-10, -768, 1024, 768), |
+ 1.0f)); |
+} |
+ |
+TEST(RectUtilTest, ScaleAndPositionRectNoScaleBottom) { |
+ // Left edge aligned. |
+ EXPECT_EQ(gfx::Rect(0, 600, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 600, 1024, 768), |
+ 1.0f)); |
+ // Right edge aligned. |
+ EXPECT_EQ(gfx::Rect(-224, 600, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-224, 600, 1024, 768), |
+ 1.0f)); |
+ // Offset to the right |
+ EXPECT_EQ(gfx::Rect(10, 600, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(10, 600, 1024, 768), |
+ 1.0f)); |
+ // Offset to the left |
+ EXPECT_EQ(gfx::Rect(-10, 600, 1024, 768), |
+ ScaleAndPositionRect(gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(0, 0, 800, 600), |
+ gfx::Rect(-10, 600, 1024, 768), |
+ 1.0f)); |
+} |
+ |
+TEST(RectUtilTest, ScaleAndPositionRectNoScaleOddDimensions) { |
+ EXPECT_EQ(gfx::Rect(59, 77, 7, 9), |
+ ScaleAndPositionRect(gfx::Rect(35, 72, 24, 24), |
+ gfx::Rect(35, 72, 24, 24), |
+ gfx::Rect(59, 77, 7, 9), |
+ 1.0f)); |
+ EXPECT_EQ(gfx::Rect(-701, 9, 702, 2), |
+ ScaleAndPositionRect(gfx::Rect(1, 7, 30, 40), |
+ gfx::Rect(1, 7, 30, 40), |
+ gfx::Rect(-701, 9, 702, 2), |
+ 1.0f)); |
+} |
+ |
+TEST(RectUtilTest, ScaleAndPositionRect2xScale) { |
+ // Side by side to the right. |
+ EXPECT_EQ(gfx::Rect(900, 50, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(900, 50, 1000, 700), |
+ 2.0f)); |
+ // Side-by-side to the left. |
+ EXPECT_EQ(gfx::Rect(-400, 50, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(-900, 50, 1000, 700), |
+ 2.0f)); |
+ |
+ // Side-by-side to the top. |
+ EXPECT_EQ(gfx::Rect(75, -300, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(50, -650, 1000, 700), |
+ 2.0f)); |
+ |
+ // Side-by-side on the bottom. |
+ EXPECT_EQ(gfx::Rect(75, 650, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(50, 650, 1000, 700), |
+ 2.0f)); |
+ |
+ // Side by side to the right. |
+ EXPECT_EQ(gfx::Rect(500, 50, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 400, 300), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(900, 50, 1000, 700), |
+ 2.0f)); |
+ |
+ // Side-by-side to the left. |
+ EXPECT_EQ(gfx::Rect(-400, 50, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 400, 300), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(-900, 50, 1000, 700), |
+ 2.0f)); |
+ |
+ // Side-by-side to the top. |
+ EXPECT_EQ(gfx::Rect(75, -300, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 400, 300), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(50, -650, 1000, 700), |
+ 2.0f)); |
+ |
+ |
+ // Side-by-side to the bottom. |
+ EXPECT_EQ(gfx::Rect(75, 350, 500, 350), |
+ ScaleAndPositionRect(gfx::Rect(100, 50, 400, 300), |
+ gfx::Rect(100, 50, 800, 600), |
+ gfx::Rect(50, 650, 1000, 700), |
+ 2.0f)); |
+} |
+ |
+TEST(RectUtilTest, SquaredDistanceBetweenRectsFullyIntersecting) { |
+ gfx::Rect rect1(0, 0, 100, 100); |
+ gfx::Rect rect2(5, 5, 10, 10); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(rect1, rect2)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(rect2, rect1)); |
+} |
+ |
+TEST(RectUtilTest, SquaredDistanceBetweenRectsPartiallyIntersecting) { |
+ gfx::Rect rect1(0, 0, 10, 10); |
+ gfx::Rect rect2(5, 5, 10, 10); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(rect1, rect2)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(rect2, rect1)); |
+} |
+ |
+TEST(RectUtilTest, SquaredDistanceBetweenRectsTouching) { |
+ gfx::Rect ref(2, 2, 2, 2); |
+ |
+ gfx::Rect top_left(0, 0, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, top_left)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(top_left, ref)); |
+ gfx::Rect top_left_partial_top(1, 0, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, top_left_partial_top)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(top_left_partial_top, ref)); |
+ gfx::Rect top(2, 0, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, top)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(top, ref)); |
+ gfx::Rect top_right_partial_top(3, 0, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, top_right_partial_top)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(top_right_partial_top, ref)); |
+ gfx::Rect top_right(4, 0, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, top_right)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(top_right, ref)); |
+ |
+ gfx::Rect top_left_partial_left(0, 1, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, top_left_partial_left)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(top_left_partial_left, ref)); |
+ gfx::Rect left(0, 2, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, left)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(left, ref)); |
+ gfx::Rect bottom_left_partial(0, 3, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, bottom_left_partial)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(bottom_left_partial, ref)); |
+ gfx::Rect bottom_left(0, 4, 2, 2); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(ref, bottom_left)); |
+ EXPECT_EQ(0, SquaredDistanceBetweenRects(bottom_left, ref)); |
+} |
+ |
+TEST(RectUtilTest, SquaredDistanceBetweenRectsOverlapping) { |
+ gfx::Rect ref(5, 5, 2, 2); |
+ |
+ gfx::Rect top_left_partial_top(4, 0, 2, 2); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(ref, top_left_partial_top)); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(top_left_partial_top, ref)); |
+ gfx::Rect top(5, 0, 2, 2); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(ref, top)); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(top, ref)); |
+ gfx::Rect top_right_partial(6, 0, 2, 2); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(ref, top_right_partial)); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(top_right_partial, ref)); |
+ |
+ gfx::Rect top_left_partial_left(0, 4, 2, 2); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(ref, top_left_partial_left)); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(top_left_partial_left, ref)); |
+ gfx::Rect left(0, 5, 2, 2); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(ref, left)); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(left, ref)); |
+ gfx::Rect bottom_left_partial(0, 6, 2, 2); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(ref, bottom_left_partial)); |
+ EXPECT_EQ(9, SquaredDistanceBetweenRects(bottom_left_partial, ref)); |
+} |
+ |
+TEST(RectUtilTest, SquaredDistanceBetweenRectsDiagonals) { |
+ gfx::Rect ref(5, 5, 2, 2); |
+ |
+ gfx::Rect top_left(0, 0, 2, 2); |
+ EXPECT_EQ(18, SquaredDistanceBetweenRects(ref, top_left)); |
+ EXPECT_EQ(18, SquaredDistanceBetweenRects(top_left, ref)); |
+ |
+ gfx::Rect top_right(10, 0, 2, 2); |
+ EXPECT_EQ(18, SquaredDistanceBetweenRects(ref, top_right)); |
+ EXPECT_EQ(18, SquaredDistanceBetweenRects(top_right, ref)); |
+} |
+ |
+ |
+} // namespace win |
+} // namespace gfx |