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