OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/basictypes.h" | |
6 #include "base/compiler_specific.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "ui/aura/event.h" | |
9 #include "ui/aura/root_window.h" | |
10 #include "ui/aura/test/aura_test_base.h" | |
11 #include "ui/aura/test/event_generator.h" | |
12 #include "ui/aura/test/test_window_delegate.h" | |
13 #include "ui/aura_shell/toplevel_window_event_filter.h" | |
14 #include "ui/aura_shell/window_util.h" | |
15 #include "ui/base/hit_test.h" | |
16 #include "ui/gfx/screen.h" | |
17 | |
18 #if defined(OS_WIN) | |
19 // Windows headers define macros for these function names which screw with us. | |
20 #if defined(CreateWindow) | |
21 #undef CreateWindow | |
22 #endif | |
23 #endif | |
24 | |
25 | |
26 namespace aura_shell { | |
27 namespace test { | |
28 | |
29 namespace { | |
30 | |
31 // A simple window delegate that returns the specified hit-test code when | |
32 // requested and applies a minimum size constraint if there is one. | |
33 class TestWindowDelegate : public aura::test::TestWindowDelegate { | |
34 public: | |
35 explicit TestWindowDelegate(int hittest_code) | |
36 : hittest_code_(hittest_code) { | |
37 } | |
38 virtual ~TestWindowDelegate() {} | |
39 | |
40 void set_min_size(const gfx::Size& size) { | |
41 min_size_ = size; | |
42 } | |
43 | |
44 private: | |
45 // Overridden from aura::Test::TestWindowDelegate: | |
46 virtual gfx::Size GetMinimumSize() const OVERRIDE { | |
47 return min_size_; | |
48 } | |
49 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
50 return hittest_code_; | |
51 } | |
52 virtual void OnWindowDestroyed() OVERRIDE { | |
53 delete this; | |
54 } | |
55 | |
56 int hittest_code_; | |
57 gfx::Size min_size_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(TestWindowDelegate); | |
60 }; | |
61 | |
62 class ToplevelWindowEventFilterTest : public aura::test::AuraTestBase { | |
63 public: | |
64 ToplevelWindowEventFilterTest() {} | |
65 virtual ~ToplevelWindowEventFilterTest() {} | |
66 | |
67 virtual void SetUp() OVERRIDE { | |
68 aura::test::AuraTestBase::SetUp(); | |
69 aura::RootWindow::GetInstance()->SetEventFilter( | |
70 new ToplevelWindowEventFilter(aura::RootWindow::GetInstance())); | |
71 } | |
72 | |
73 protected: | |
74 aura::Window* CreateWindow(int hittest_code) { | |
75 TestWindowDelegate* d1 = new TestWindowDelegate(hittest_code); | |
76 aura::Window* w1 = new aura::Window(d1); | |
77 w1->set_id(1); | |
78 w1->Init(ui::Layer::LAYER_HAS_TEXTURE); | |
79 w1->SetParent(NULL); | |
80 w1->SetBounds(gfx::Rect(0, 0, 100, 100)); | |
81 w1->Show(); | |
82 return w1; | |
83 } | |
84 | |
85 void DragFromCenterBy(aura::Window* window, int dx, int dy) { | |
86 aura::test::EventGenerator generator(window); | |
87 generator.DragMouseBy(dx, dy); | |
88 } | |
89 | |
90 void TouchDragFromCenterBy(aura::Window* window, int dx, int dy) { | |
91 aura::test::EventGenerator generator(window); | |
92 generator.PressMoveAndReleaseTouchBy(dx, dy); | |
93 } | |
94 | |
95 private: | |
96 DISALLOW_COPY_AND_ASSIGN(ToplevelWindowEventFilterTest); | |
97 }; | |
98 | |
99 } | |
100 | |
101 TEST_F(ToplevelWindowEventFilterTest, Caption) { | |
102 scoped_ptr<aura::Window> w1(CreateWindow(HTCAPTION)); | |
103 gfx::Size size = w1->bounds().size(); | |
104 DragFromCenterBy(w1.get(), 100, 100); | |
105 // Position should have been offset by 100,100. | |
106 EXPECT_EQ(gfx::Point(100, 100), w1->bounds().origin()); | |
107 // Size should not have. | |
108 EXPECT_EQ(size, w1->bounds().size()); | |
109 | |
110 TouchDragFromCenterBy(w1.get(), 100, 100); | |
111 // Position should have been offset by 100,100. | |
112 EXPECT_EQ(gfx::Point(200, 200), w1->bounds().origin()); | |
113 // Size should not have. | |
114 EXPECT_EQ(size, w1->bounds().size()); | |
115 } | |
116 | |
117 TEST_F(ToplevelWindowEventFilterTest, BottomRight) { | |
118 scoped_ptr<aura::Window> w1(CreateWindow(HTBOTTOMRIGHT)); | |
119 gfx::Point position = w1->bounds().origin(); | |
120 DragFromCenterBy(w1.get(), 100, 100); | |
121 // Position should not have changed. | |
122 EXPECT_EQ(position, w1->bounds().origin()); | |
123 // Size should have increased by 100,100. | |
124 EXPECT_EQ(gfx::Size(200, 200), w1->bounds().size()); | |
125 } | |
126 | |
127 TEST_F(ToplevelWindowEventFilterTest, GrowBox) { | |
128 scoped_ptr<aura::Window> w1(CreateWindow(HTGROWBOX)); | |
129 TestWindowDelegate* window_delegate = | |
130 static_cast<TestWindowDelegate*>(w1->delegate()); | |
131 window_delegate->set_min_size(gfx::Size(40, 40)); | |
132 | |
133 gfx::Point position = w1->bounds().origin(); | |
134 aura::test::EventGenerator generator; | |
135 generator.MoveMouseToCenterOf(w1.get()); | |
136 generator.DragMouseBy(100, 100); | |
137 // Position should not have changed. | |
138 EXPECT_EQ(position, w1->bounds().origin()); | |
139 // Size should have increased by 100,100. | |
140 EXPECT_EQ(gfx::Size(200, 200), w1->bounds().size()); | |
141 | |
142 // Shrink the wnidow by (-100, -100). | |
143 generator.DragMouseBy(-100, -100); | |
144 // Position should not have changed. | |
145 EXPECT_EQ(position, w1->bounds().origin()); | |
146 // Size should have decreased by 100,100. | |
147 EXPECT_EQ(gfx::Size(100, 100), w1->bounds().size()); | |
148 | |
149 // Enforce minimum size. | |
150 generator.DragMouseBy(-60, -60); | |
151 EXPECT_EQ(position, w1->bounds().origin()); | |
152 EXPECT_EQ(gfx::Size(40, 40), w1->bounds().size()); | |
153 } | |
154 | |
155 TEST_F(ToplevelWindowEventFilterTest, Right) { | |
156 scoped_ptr<aura::Window> w1(CreateWindow(HTRIGHT)); | |
157 gfx::Point position = w1->bounds().origin(); | |
158 DragFromCenterBy(w1.get(), 100, 100); | |
159 // Position should not have changed. | |
160 EXPECT_EQ(position, w1->bounds().origin()); | |
161 // Size should have increased by 100,0. | |
162 EXPECT_EQ(gfx::Size(200, 100), w1->bounds().size()); | |
163 } | |
164 | |
165 TEST_F(ToplevelWindowEventFilterTest, Bottom) { | |
166 scoped_ptr<aura::Window> w1(CreateWindow(HTBOTTOM)); | |
167 gfx::Point position = w1->bounds().origin(); | |
168 DragFromCenterBy(w1.get(), 100, 100); | |
169 // Position should not have changed. | |
170 EXPECT_EQ(position, w1->bounds().origin()); | |
171 // Size should have increased by 0,100. | |
172 EXPECT_EQ(gfx::Size(100, 200), w1->bounds().size()); | |
173 } | |
174 | |
175 TEST_F(ToplevelWindowEventFilterTest, TopRight) { | |
176 scoped_ptr<aura::Window> w1(CreateWindow(HTTOPRIGHT)); | |
177 DragFromCenterBy(w1.get(), -50, 50); | |
178 // Position should have been offset by 0,50. | |
179 EXPECT_EQ(gfx::Point(0, 50), w1->bounds().origin()); | |
180 // Size should have decreased by 50,50. | |
181 EXPECT_EQ(gfx::Size(50, 50), w1->bounds().size()); | |
182 } | |
183 | |
184 TEST_F(ToplevelWindowEventFilterTest, Top) { | |
185 scoped_ptr<aura::Window> w1(CreateWindow(HTTOP)); | |
186 DragFromCenterBy(w1.get(), 50, 50); | |
187 // Position should have been offset by 0,50. | |
188 EXPECT_EQ(gfx::Point(0, 50), w1->bounds().origin()); | |
189 // Size should have decreased by 0,50. | |
190 EXPECT_EQ(gfx::Size(100, 50), w1->bounds().size()); | |
191 } | |
192 | |
193 TEST_F(ToplevelWindowEventFilterTest, Left) { | |
194 scoped_ptr<aura::Window> w1(CreateWindow(HTLEFT)); | |
195 DragFromCenterBy(w1.get(), 50, 50); | |
196 // Position should have been offset by 50,0. | |
197 EXPECT_EQ(gfx::Point(50, 0), w1->bounds().origin()); | |
198 // Size should have decreased by 50,0. | |
199 EXPECT_EQ(gfx::Size(50, 100), w1->bounds().size()); | |
200 } | |
201 | |
202 TEST_F(ToplevelWindowEventFilterTest, BottomLeft) { | |
203 scoped_ptr<aura::Window> w1(CreateWindow(HTBOTTOMLEFT)); | |
204 DragFromCenterBy(w1.get(), 50, -50); | |
205 // Position should have been offset by 50,0. | |
206 EXPECT_EQ(gfx::Point(50, 0), w1->bounds().origin()); | |
207 // Size should have decreased by 50,50. | |
208 EXPECT_EQ(gfx::Size(50, 50), w1->bounds().size()); | |
209 } | |
210 | |
211 TEST_F(ToplevelWindowEventFilterTest, TopLeft) { | |
212 scoped_ptr<aura::Window> w1(CreateWindow(HTTOPLEFT)); | |
213 DragFromCenterBy(w1.get(), 50, 50); | |
214 // Position should have been offset by 50,50. | |
215 EXPECT_EQ(gfx::Point(50, 50), w1->bounds().origin()); | |
216 // Size should have decreased by 50,50. | |
217 EXPECT_EQ(gfx::Size(50, 50), w1->bounds().size()); | |
218 } | |
219 | |
220 TEST_F(ToplevelWindowEventFilterTest, Client) { | |
221 scoped_ptr<aura::Window> w1(CreateWindow(HTCLIENT)); | |
222 gfx::Rect bounds = w1->bounds(); | |
223 DragFromCenterBy(w1.get(), 100, 100); | |
224 // Neither position nor size should have changed. | |
225 EXPECT_EQ(bounds, w1->bounds()); | |
226 } | |
227 | |
228 TEST_F(ToplevelWindowEventFilterTest, LeftPastMinimum) { | |
229 scoped_ptr<aura::Window> w1(CreateWindow(HTLEFT)); | |
230 TestWindowDelegate* window_delegate = | |
231 static_cast<TestWindowDelegate*>(w1->delegate()); | |
232 window_delegate->set_min_size(gfx::Size(40, 40)); | |
233 | |
234 // Simulate a large left-to-right drag. Window width should be clamped to | |
235 // minimum and position change should be limited as well. | |
236 DragFromCenterBy(w1.get(), 333, 0); | |
237 EXPECT_EQ(gfx::Point(60, 0), w1->bounds().origin()); | |
238 EXPECT_EQ(gfx::Size(40, 100), w1->bounds().size()); | |
239 } | |
240 | |
241 TEST_F(ToplevelWindowEventFilterTest, RightPastMinimum) { | |
242 scoped_ptr<aura::Window> w1(CreateWindow(HTRIGHT)); | |
243 TestWindowDelegate* window_delegate = | |
244 static_cast<TestWindowDelegate*>(w1->delegate()); | |
245 window_delegate->set_min_size(gfx::Size(40, 40)); | |
246 gfx::Point position = w1->bounds().origin(); | |
247 | |
248 // Simulate a large right-to-left drag. Window width should be clamped to | |
249 // minimum and position should not change. | |
250 DragFromCenterBy(w1.get(), -333, 0); | |
251 EXPECT_EQ(position, w1->bounds().origin()); | |
252 EXPECT_EQ(gfx::Size(40, 100), w1->bounds().size()); | |
253 } | |
254 | |
255 TEST_F(ToplevelWindowEventFilterTest, TopLeftPastMinimum) { | |
256 scoped_ptr<aura::Window> w1(CreateWindow(HTTOPLEFT)); | |
257 TestWindowDelegate* window_delegate = | |
258 static_cast<TestWindowDelegate*>(w1->delegate()); | |
259 window_delegate->set_min_size(gfx::Size(40, 40)); | |
260 | |
261 // Simulate a large top-left to bottom-right drag. Window width should be | |
262 // clamped to minimum and position should be limited. | |
263 DragFromCenterBy(w1.get(), 333, 444); | |
264 EXPECT_EQ(gfx::Point(60, 60), w1->bounds().origin()); | |
265 EXPECT_EQ(gfx::Size(40, 40), w1->bounds().size()); | |
266 } | |
267 | |
268 TEST_F(ToplevelWindowEventFilterTest, TopRightPastMinimum) { | |
269 scoped_ptr<aura::Window> w1(CreateWindow(HTTOPRIGHT)); | |
270 TestWindowDelegate* window_delegate = | |
271 static_cast<TestWindowDelegate*>(w1->delegate()); | |
272 window_delegate->set_min_size(gfx::Size(40, 40)); | |
273 | |
274 // Simulate a large top-right to bottom-left drag. Window size should be | |
275 // clamped to minimum, x position should not change, and y position should | |
276 // be clamped. | |
277 DragFromCenterBy(w1.get(), -333, 444); | |
278 EXPECT_EQ(gfx::Point(0, 60), w1->bounds().origin()); | |
279 EXPECT_EQ(gfx::Size(40, 40), w1->bounds().size()); | |
280 } | |
281 | |
282 TEST_F(ToplevelWindowEventFilterTest, BottomLeftPastMinimum) { | |
283 scoped_ptr<aura::Window> w1(CreateWindow(HTBOTTOMLEFT)); | |
284 TestWindowDelegate* window_delegate = | |
285 static_cast<TestWindowDelegate*>(w1->delegate()); | |
286 window_delegate->set_min_size(gfx::Size(40, 40)); | |
287 | |
288 // Simulate a large bottom-left to top-right drag. Window size should be | |
289 // clamped to minimum, x position should be clamped, and y position should | |
290 // not change. | |
291 DragFromCenterBy(w1.get(), 333, -444); | |
292 EXPECT_EQ(gfx::Point(60, 0), w1->bounds().origin()); | |
293 EXPECT_EQ(gfx::Size(40, 40), w1->bounds().size()); | |
294 } | |
295 | |
296 TEST_F(ToplevelWindowEventFilterTest, BottomRightPastMinimum) { | |
297 scoped_ptr<aura::Window> w1(CreateWindow(HTBOTTOMRIGHT)); | |
298 TestWindowDelegate* window_delegate = | |
299 static_cast<TestWindowDelegate*>(w1->delegate()); | |
300 window_delegate->set_min_size(gfx::Size(40, 40)); | |
301 gfx::Point position = w1->bounds().origin(); | |
302 | |
303 // Simulate a large bottom-right to top-left drag. Window size should be | |
304 // clamped to minimum and position should not change. | |
305 DragFromCenterBy(w1.get(), -333, -444); | |
306 EXPECT_EQ(position, w1->bounds().origin()); | |
307 EXPECT_EQ(gfx::Size(40, 40), w1->bounds().size()); | |
308 } | |
309 | |
310 TEST_F(ToplevelWindowEventFilterTest, DoubleClickCaptionTogglesMaximize) { | |
311 scoped_ptr<aura::Window> w1(CreateWindow(HTCAPTION)); | |
312 EXPECT_FALSE(IsWindowMaximized(w1.get())); | |
313 | |
314 aura::test::EventGenerator generator(w1.get()); | |
315 generator.DoubleClickLeftButton(); | |
316 | |
317 EXPECT_TRUE(IsWindowMaximized(w1.get())); | |
318 generator.DoubleClickLeftButton(); | |
319 | |
320 EXPECT_FALSE(IsWindowMaximized(w1.get())); | |
321 } | |
322 | |
323 } // namespace test | |
324 } // namespace aura | |
OLD | NEW |