blob: 67a15faef8f34e0358e0f4c94cdbaf33353a95dc [file] [log] [blame]
Kaido Kert25902c62024-06-17 17:10:28 -07001// Copyright 2014 The Chromium Authors
Kaido Kert56d7c4e2024-04-13 12:59:27 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gfx/selection_bound.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8#include "ui/gfx/geometry/rect.h"
9#include "ui/gfx/geometry/rect_f.h"
10
11namespace gfx {
12
13TEST(SelectionBoundTest, RectBetweenSelectionBounds) {
14 SelectionBound b1, b2;
15 // Simple case of aligned vertical bounds of equal height
16 b1.SetEdge(gfx::PointF(0.f, 20.f), gfx::PointF(0.f, 25.f));
17 b2.SetEdge(gfx::PointF(110.f, 20.f), gfx::PointF(110.f, 25.f));
18 gfx::Rect expected_rect(
19 b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
20 b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
21 b2.edge_end_rounded().y() - b2.edge_start_rounded().y());
22 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
23 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
24
25 // Both bounds are invisible.
26 b1.SetVisibleEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 25.f));
27 b2.SetVisibleEdge(gfx::PointF(100.f, 20.f), gfx::PointF(100.f, 25.f));
28 gfx::RectF expected_visible_rect(
29 b1.visible_edge_start().x(), b1.visible_edge_start().y(),
30 b2.visible_edge_start().x() - b1.visible_edge_start().x(),
31 b2.visible_edge_end().y() - b2.visible_edge_start().y());
32 EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b1, b2));
33 EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b2, b1));
34
35 // One of the bounds is invisible.
36 b1.SetVisibleEdge(gfx::PointF(0.f, 20.f), gfx::PointF(0.f, 25.f));
37 b2.SetVisibleEdge(gfx::PointF(100.f, 20.f), gfx::PointF(100.f, 25.f));
38 expected_visible_rect =
39 gfx::RectF(b1.visible_edge_start().x(), b1.visible_edge_start().y(),
40 b2.visible_edge_start().x() - b1.visible_edge_start().x(),
41 b2.visible_edge_end().y() - b2.visible_edge_start().y());
42 EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b1, b2));
43 EXPECT_EQ(expected_visible_rect, RectFBetweenVisibleSelectionBounds(b2, b1));
44
45 // Parallel vertical bounds of different heights
46 b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 25.f));
47 b2.SetEdge(gfx::PointF(110.f, 0.f), gfx::PointF(110.f, 35.f));
48 expected_rect =
49 gfx::Rect(b1.edge_start_rounded().x(), b2.edge_start_rounded().y(),
50 b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
51 b2.edge_end_rounded().y() - b2.edge_start_rounded().y());
52 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
53 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
54
55 b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f));
56 b2.SetEdge(gfx::PointF(110.f, 25.f), gfx::PointF(110.f, 45.f));
57 expected_rect =
58 gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
59 b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
60 b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
61 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
62 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
63
64 b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f));
65 b2.SetEdge(gfx::PointF(110.f, 40.f), gfx::PointF(110.f, 60.f));
66 expected_rect =
67 gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
68 b2.edge_start_rounded().x() - b1.edge_start_rounded().x(),
69 b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
70 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
71 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
72
73 // Overlapping vertical bounds
74 b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(10.f, 30.f));
75 b2.SetEdge(gfx::PointF(10.f, 25.f), gfx::PointF(10.f, 40.f));
76 expected_rect =
77 gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(), 0,
78 b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
79 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
80 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
81
82 // Non-vertical bounds: "\ \"
83 b1.SetEdge(gfx::PointF(10.f, 20.f), gfx::PointF(20.f, 30.f));
84 b2.SetEdge(gfx::PointF(110.f, 40.f), gfx::PointF(120.f, 60.f));
85 expected_rect =
86 gfx::Rect(b1.edge_start_rounded().x(), b1.edge_start_rounded().y(),
87 b2.edge_end_rounded().x() - b1.edge_start_rounded().x(),
88 b2.edge_end_rounded().y() - b1.edge_start_rounded().y());
89 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
90 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
91
92 // Non-vertical bounds: "/ \"
93 b1.SetEdge(gfx::PointF(20.f, 30.f), gfx::PointF(0.f, 40.f));
94 b2.SetEdge(gfx::PointF(110.f, 30.f), gfx::PointF(120.f, 40.f));
95 expected_rect =
96 gfx::Rect(b1.edge_end_rounded().x(), b1.edge_start_rounded().y(),
97 b2.edge_end_rounded().x() - b1.edge_end_rounded().x(),
98 b2.edge_end_rounded().y() - b2.edge_start_rounded().y());
99 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b1, b2));
100 EXPECT_EQ(expected_rect, RectBetweenSelectionBounds(b2, b1));
101}
102
103} // namespace gfx