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