mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 5 | #include "ui/gfx/selection_bound.h" |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 6 | |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 8 | #include "ui/gfx/geometry/rect.h" |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 9 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 10 | namespace gfx { |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 11 | |
| 12 | TEST(SelectionBoundTest, RectBetweenSelectionBounds) { |
| 13 | SelectionBound b1, b2; |
| 14 | // Simple case of aligned vertical bounds of equal height |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 15 | b1.SetEdge(gfx::PointF(0.f, 20.f), gfx::PointF(0.f, 25.f)); |
| 16 | b2.SetEdge(gfx::PointF(110.f, 20.f), gfx::PointF(110.f, 25.f)); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 17 | gfx::Rect expected_rect( |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 18 | b1.edge_top_rounded().x(), b1.edge_top_rounded().y(), |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 19 | b2.edge_top_rounded().x() - b1.edge_top_rounded().x(), |
| 20 | b2.edge_bottom_rounded().y() - b2.edge_top_rounded().y()); |
| 21 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 22 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 23 | |
| 24 | // Parallel vertical bounds of different heights |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 25 | b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 25.f)); |
| 26 | b2.SetEdge(gfx::PointF(110.f, 0.f), gfx::PointF(110.f, 35.f)); |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 27 | expected_rect = |
| 28 | gfx::Rect(b1.edge_top_rounded().x(), b2.edge_top_rounded().y(), |
| 29 | b2.edge_top_rounded().x() - b1.edge_top_rounded().x(), |
| 30 | b2.edge_bottom_rounded().y() - b2.edge_top_rounded().y()); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 31 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 32 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 33 | |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 34 | b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f)); |
| 35 | b2.SetEdge(gfx::PointF(110.f, 25.f), gfx::PointF(110.f, 45.f)); |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 36 | expected_rect = |
| 37 | gfx::Rect(b1.edge_top_rounded().x(), b1.edge_top_rounded().y(), |
| 38 | b2.edge_top_rounded().x() - b1.edge_top_rounded().x(), |
| 39 | b2.edge_bottom_rounded().y() - b1.edge_top_rounded().y()); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 40 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 41 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 42 | |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 43 | b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f)); |
| 44 | b2.SetEdge(gfx::PointF(110.f, 40.f), gfx::PointF(110.f, 60.f)); |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 45 | expected_rect = |
| 46 | gfx::Rect(b1.edge_top_rounded().x(), b1.edge_top_rounded().y(), |
| 47 | b2.edge_top_rounded().x() - b1.edge_top_rounded().x(), |
| 48 | b2.edge_bottom_rounded().y() - b1.edge_top_rounded().y()); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 49 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 50 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 51 | |
| 52 | // Overlapping vertical bounds |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 53 | b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f)); |
| 54 | b2.SetEdge(gfx::PointF(10.f, 25.f), gfx::PointF(10.f, 40.f)); |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 55 | expected_rect = |
| 56 | gfx::Rect(b1.edge_top_rounded().x(), b1.edge_top_rounded().y(), 0, |
| 57 | b2.edge_bottom_rounded().y() - b1.edge_top_rounded().y()); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 58 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 59 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 60 | |
| 61 | // Non-vertical bounds: "\ \" |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 62 | b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(20.f, 30.f)); |
| 63 | b2.SetEdge(gfx::PointF(110.f, 40.f), gfx::PointF(120.f, 60.f)); |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 64 | expected_rect = |
| 65 | gfx::Rect(b1.edge_top_rounded().x(), b1.edge_top_rounded().y(), |
| 66 | b2.edge_bottom_rounded().x() - b1.edge_top_rounded().x(), |
| 67 | b2.edge_bottom_rounded().y() - b1.edge_top_rounded().y()); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 68 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 69 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 70 | |
| 71 | // Non-vertical bounds: "/ \" |
danakj | 089746f | 2015-11-02 22:52:33 | [diff] [blame] | 72 | b1.SetEdge(gfx::PointF(20.f, 30.f), gfx::PointF(0.f, 40.f)); |
| 73 | b2.SetEdge(gfx::PointF(110.f, 30.f), gfx::PointF(120.f, 40.f)); |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 74 | expected_rect = |
| 75 | gfx::Rect(b1.edge_bottom_rounded().x(), b1.edge_top_rounded().y(), |
| 76 | b2.edge_bottom_rounded().x() - b1.edge_bottom_rounded().x(), |
| 77 | b2.edge_bottom_rounded().y() - b2.edge_top_rounded().y()); |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 78 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2)); |
| 79 | EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1)); |
| 80 | } |
| 81 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 82 | } // namespace gfx |