1// Copyright (c) 2012 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
5#include "ash/wm/workspace/magnetism_matcher.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace ash {
10namespace internal {
11
12// Trivial test case verifying assertions on left edge.
13TEST(MagnetismMatcherTest, TrivialLeft) {
14  const int distance = MagnetismMatcher::kMagneticDistance;
15  const gfx::Rect initial_bounds(20, 10, 50, 60);
16  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
17  EXPECT_FALSE(matcher.AreEdgesObscured());
18  MatchedEdge edge;
19  EXPECT_FALSE(matcher.ShouldAttach(
20                   gfx::Rect(initial_bounds.x() - distance - 10,
21                             initial_bounds.y() - distance - 10, 2, 3), &edge));
22  EXPECT_FALSE(matcher.AreEdgesObscured());
23  EXPECT_TRUE(matcher.ShouldAttach(
24                  gfx::Rect(initial_bounds.x() - 2, initial_bounds.y(), 1, 1),
25                  &edge));
26  EXPECT_EQ(MAGNETISM_EDGE_LEFT, edge.primary_edge);
27  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_LEADING, edge.secondary_edge);
28
29  EXPECT_TRUE(matcher.ShouldAttach(
30                  gfx::Rect(initial_bounds.x() - 2,
31                            initial_bounds.y() + distance + 1 , 1, 1),
32                  &edge));
33  EXPECT_EQ(MAGNETISM_EDGE_LEFT, edge.primary_edge);
34  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
35}
36
37// Trivial test case verifying assertions on bottom edge.
38TEST(MagnetismMatcherTest, TrivialBottom) {
39  const int distance = MagnetismMatcher::kMagneticDistance;
40  const gfx::Rect initial_bounds(20, 10, 50, 60);
41  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
42  EXPECT_FALSE(matcher.AreEdgesObscured());
43  MatchedEdge edge;
44  EXPECT_FALSE(matcher.ShouldAttach(
45                   gfx::Rect(initial_bounds.x() - distance - 10,
46                             initial_bounds.y() - distance - 10, 2, 3), &edge));
47  EXPECT_FALSE(matcher.AreEdgesObscured());
48  EXPECT_TRUE(matcher.ShouldAttach(
49                  gfx::Rect(initial_bounds.x() - 2,
50                            initial_bounds.bottom() + 4, 10, 1), &edge));
51  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
52  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_LEADING, edge.secondary_edge);
53
54  EXPECT_TRUE(matcher.ShouldAttach(
55                  gfx::Rect(initial_bounds.x() + distance + 1,
56                            initial_bounds.bottom() + 4, 10, 1), &edge));
57  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
58  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
59
60  EXPECT_TRUE(matcher.ShouldAttach(
61                  gfx::Rect(initial_bounds.right() - 10 - 1,
62                            initial_bounds.bottom() + 4, 10, 1), &edge));
63  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
64  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_TRAILING, edge.secondary_edge);
65}
66
67// Verifies we don't match an obscured corner.
68TEST(MagnetismMatcherTest, ObscureLeading) {
69  const int distance = MagnetismMatcher::kMagneticDistance;
70  const gfx::Rect initial_bounds(20, 10, 150, 160);
71  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
72  MatchedEdge edge;
73  // Overlap with the upper right corner.
74  EXPECT_FALSE(matcher.ShouldAttach(
75                   gfx::Rect(initial_bounds.right() - distance * 2,
76                             initial_bounds.y() - distance - 2,
77                             distance * 3,
78                             (distance + 2) * 2), &edge));
79  EXPECT_FALSE(matcher.AreEdgesObscured());
80  // Verify doesn't match the following which is obscured by first.
81  EXPECT_FALSE(matcher.ShouldAttach(
82                   gfx::Rect(initial_bounds.right() + 1,
83                             initial_bounds.y(),
84                             distance,
85                             5), &edge));
86  // Should match the following which extends into non-overlapping region.
87  EXPECT_TRUE(matcher.ShouldAttach(
88                   gfx::Rect(initial_bounds.right() + 1,
89                             initial_bounds.y() + distance + 1,
90                             distance,
91                             15), &edge));
92  EXPECT_EQ(MAGNETISM_EDGE_RIGHT, edge.primary_edge);
93  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
94}
95
96// Verifies obscuring one side doesn't obscure the other.
97TEST(MagnetismMatcherTest, DontObscureOtherSide) {
98  const int distance = MagnetismMatcher::kMagneticDistance;
99  const gfx::Rect initial_bounds(20, 10, 150, 160);
100  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
101  MatchedEdge edge;
102  // Overlap with the left side.
103  EXPECT_FALSE(matcher.ShouldAttach(
104                   gfx::Rect(initial_bounds.x() - distance + 1,
105                             initial_bounds.y() + 2,
106                             distance * 2 + 2,
107                             initial_bounds.height() + distance * 4), &edge));
108  EXPECT_FALSE(matcher.AreEdgesObscured());
109  // Should match the right side since it isn't obscured.
110  EXPECT_TRUE(matcher.ShouldAttach(
111                   gfx::Rect(initial_bounds.right() - 1,
112                             initial_bounds.y() + distance + 1,
113                             distance,
114                             5), &edge));
115  EXPECT_EQ(MAGNETISM_EDGE_RIGHT, edge.primary_edge);
116  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
117}
118
119// Verifies we don't match an obscured center.
120TEST(MagnetismMatcherTest, ObscureCenter) {
121  const int distance = MagnetismMatcher::kMagneticDistance;
122  const gfx::Rect initial_bounds(20, 10, 150, 160);
123  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
124  MatchedEdge edge;
125  // Overlap with the center bottom edge.
126  EXPECT_FALSE(matcher.ShouldAttach(
127                   gfx::Rect(100, initial_bounds.bottom() - distance - 2,
128                             20,
129                             (distance + 2) * 2), &edge));
130  EXPECT_FALSE(matcher.AreEdgesObscured());
131  // Verify doesn't match the following which is obscured by first.
132  EXPECT_FALSE(matcher.ShouldAttach(
133                   gfx::Rect(110, initial_bounds.bottom() + 1,
134                             10, 5), &edge));
135  // Should match the following which extends into non-overlapping region.
136  EXPECT_TRUE(matcher.ShouldAttach(
137                  gfx::Rect(90,
138                            initial_bounds.bottom() + 1,
139                            10, 5), &edge));
140  EXPECT_EQ(MAGNETISM_EDGE_BOTTOM, edge.primary_edge);
141  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
142}
143
144// Verifies we don't match an obscured trailing edge.
145TEST(MagnetismMatcherTest, ObscureTrailing) {
146  const int distance = MagnetismMatcher::kMagneticDistance;
147  const gfx::Rect initial_bounds(20, 10, 150, 160);
148  MagnetismMatcher matcher(initial_bounds, kAllMagnetismEdges);
149  MatchedEdge edge;
150  // Overlap with the trailing left edge.
151  EXPECT_FALSE(matcher.ShouldAttach(
152                   gfx::Rect(initial_bounds.x() - distance - 2,
153                             150,
154                             (distance + 2) * 2,
155                             50), &edge));
156  EXPECT_FALSE(matcher.AreEdgesObscured());
157  // Verify doesn't match the following which is obscured by first.
158  EXPECT_FALSE(matcher.ShouldAttach(
159                   gfx::Rect(initial_bounds.x() - 4,
160                             160, 3, 20), &edge));
161  // Should match the following which extends into non-overlapping region.
162  EXPECT_TRUE(matcher.ShouldAttach(
163                   gfx::Rect(initial_bounds.x() - 4,
164                             140, 3, 20), &edge));
165  EXPECT_EQ(MAGNETISM_EDGE_LEFT, edge.primary_edge);
166  EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE, edge.secondary_edge);
167}
168
169}  // namespace internal
170}  // namespace ash
171
172