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.
5 #include "ash/wm/workspace/magnetism_matcher.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 // Trivial test case verifying assertions on left edge.
13 TEST(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());
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),
26 EXPECT_EQ(MAGNETISM_EDGE_LEFT
, edge
.primary_edge
);
27 EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_LEADING
, edge
.secondary_edge
);
29 EXPECT_TRUE(matcher
.ShouldAttach(
30 gfx::Rect(initial_bounds
.x() - 2,
31 initial_bounds
.y() + distance
+ 1 , 1, 1),
33 EXPECT_EQ(MAGNETISM_EDGE_LEFT
, edge
.primary_edge
);
34 EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE
, edge
.secondary_edge
);
37 // Trivial test case verifying assertions on bottom edge.
38 TEST(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());
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
);
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
);
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
);
67 // Verifies we don't match an obscured corner.
68 TEST(MagnetismMatcherTest
, ObscureLeading
) {
69 const int distance
= MagnetismMatcher::kMagneticDistance
;
70 const gfx::Rect
initial_bounds(20, 10, 150, 160);
71 MagnetismMatcher
matcher(initial_bounds
, kAllMagnetismEdges
);
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,
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,
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,
92 EXPECT_EQ(MAGNETISM_EDGE_RIGHT
, edge
.primary_edge
);
93 EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE
, edge
.secondary_edge
);
96 // Verifies obscuring one side doesn't obscure the other.
97 TEST(MagnetismMatcherTest
, DontObscureOtherSide
) {
98 const int distance
= MagnetismMatcher::kMagneticDistance
;
99 const gfx::Rect
initial_bounds(20, 10, 150, 160);
100 MagnetismMatcher
matcher(initial_bounds
, kAllMagnetismEdges
);
102 // Overlap with the left side.
103 EXPECT_FALSE(matcher
.ShouldAttach(
104 gfx::Rect(initial_bounds
.x() - distance
+ 1,
105 initial_bounds
.y() + 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,
115 EXPECT_EQ(MAGNETISM_EDGE_RIGHT
, edge
.primary_edge
);
116 EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE
, edge
.secondary_edge
);
119 // Verifies we don't match an obscured center.
120 TEST(MagnetismMatcherTest
, ObscureCenter
) {
121 const int distance
= MagnetismMatcher::kMagneticDistance
;
122 const gfx::Rect
initial_bounds(20, 10, 150, 160);
123 MagnetismMatcher
matcher(initial_bounds
, kAllMagnetismEdges
);
125 // Overlap with the center bottom edge.
126 EXPECT_FALSE(matcher
.ShouldAttach(
127 gfx::Rect(100, initial_bounds
.bottom() - distance
- 2,
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,
135 // Should match the following which extends into non-overlapping region.
136 EXPECT_TRUE(matcher
.ShouldAttach(
138 initial_bounds
.bottom() + 1,
140 EXPECT_EQ(MAGNETISM_EDGE_BOTTOM
, edge
.primary_edge
);
141 EXPECT_EQ(SECONDARY_MAGNETISM_EDGE_NONE
, edge
.secondary_edge
);
144 // Verifies we don't match an obscured trailing edge.
145 TEST(MagnetismMatcherTest
, ObscureTrailing
) {
146 const int distance
= MagnetismMatcher::kMagneticDistance
;
147 const gfx::Rect
initial_bounds(20, 10, 150, 160);
148 MagnetismMatcher
matcher(initial_bounds
, kAllMagnetismEdges
);
150 // Overlap with the trailing left edge.
151 EXPECT_FALSE(matcher
.ShouldAttach(
152 gfx::Rect(initial_bounds
.x() - distance
- 2,
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
);
169 } // namespace internal