| 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 "ui/aura/root_window.h" | 5 #include "ui/aura/root_window.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/aura/client/event_client.h" |
| 8 #include "ui/aura/env.h" | 9 #include "ui/aura/env.h" |
| 9 #include "ui/aura/event.h" | 10 #include "ui/aura/event.h" |
| 10 #include "ui/aura/event_filter.h" | 11 #include "ui/aura/event_filter.h" |
| 11 #include "ui/aura/test/aura_test_base.h" | 12 #include "ui/aura/test/aura_test_base.h" |
| 13 #include "ui/aura/test/event_generator.h" |
| 12 #include "ui/aura/test/test_window_delegate.h" | 14 #include "ui/aura/test/test_window_delegate.h" |
| 13 #include "ui/aura/test/test_windows.h" | 15 #include "ui/aura/test/test_windows.h" |
| 14 #include "ui/base/hit_test.h" | 16 #include "ui/base/hit_test.h" |
| 17 #include "ui/base/keycodes/keyboard_codes.h" |
| 15 #include "ui/gfx/point.h" | 18 #include "ui/gfx/point.h" |
| 16 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 17 | 20 |
| 18 namespace aura { | 21 namespace aura { |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 // A delegate that always returns a non-client component for hit tests. | 24 // A delegate that always returns a non-client component for hit tests. |
| 22 class NonClientDelegate : public test::TestWindowDelegate { | 25 class NonClientDelegate : public test::TestWindowDelegate { |
| 23 public: | 26 public: |
| 24 NonClientDelegate() | 27 NonClientDelegate() |
| (...skipping 27 matching lines...) Expand all Loading... |
| 52 gfx::Point non_client_location_; | 55 gfx::Point non_client_location_; |
| 53 int mouse_event_count_; | 56 int mouse_event_count_; |
| 54 gfx::Point mouse_event_location_; | 57 gfx::Point mouse_event_location_; |
| 55 int mouse_event_flags_; | 58 int mouse_event_flags_; |
| 56 | 59 |
| 57 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate); | 60 DISALLOW_COPY_AND_ASSIGN(NonClientDelegate); |
| 58 }; | 61 }; |
| 59 | 62 |
| 60 // A simple EventFilter that keeps track of the number of key events that it's | 63 // A simple EventFilter that keeps track of the number of key events that it's |
| 61 // seen. | 64 // seen. |
| 62 class TestEventFilter : public EventFilter { | 65 class EventCountFilter : public EventFilter { |
| 63 public: | 66 public: |
| 64 TestEventFilter() : num_key_events_(0) {} | 67 EventCountFilter() : num_key_events_(0), num_mouse_events_(0) {} |
| 65 virtual ~TestEventFilter() {} | 68 virtual ~EventCountFilter() {} |
| 66 | 69 |
| 67 int num_key_events() const { return num_key_events_; } | 70 int num_key_events() const { return num_key_events_; } |
| 71 int num_mouse_events() const { return num_mouse_events_; } |
| 72 |
| 73 void Reset() { |
| 74 num_key_events_ = 0; |
| 75 num_mouse_events_ = 0; |
| 76 } |
| 68 | 77 |
| 69 // EventFilter overrides: | 78 // EventFilter overrides: |
| 70 virtual bool PreHandleKeyEvent(Window* target, KeyEvent* event) OVERRIDE { | 79 virtual bool PreHandleKeyEvent(Window* target, KeyEvent* event) OVERRIDE { |
| 71 num_key_events_++; | 80 num_key_events_++; |
| 72 return true; | 81 return true; |
| 73 } | 82 } |
| 74 virtual bool PreHandleMouseEvent(Window* target, MouseEvent* event) OVERRIDE { | 83 virtual bool PreHandleMouseEvent(Window* target, MouseEvent* event) OVERRIDE { |
| 75 return false; | 84 num_mouse_events_++; |
| 85 return true; |
| 76 } | 86 } |
| 77 virtual ui::TouchStatus PreHandleTouchEvent( | 87 virtual ui::TouchStatus PreHandleTouchEvent( |
| 78 Window* target, TouchEvent* event) OVERRIDE { | 88 Window* target, TouchEvent* event) OVERRIDE { |
| 79 return ui::TOUCH_STATUS_UNKNOWN; | 89 return ui::TOUCH_STATUS_UNKNOWN; |
| 80 } | 90 } |
| 81 virtual ui::GestureStatus PreHandleGestureEvent( | 91 virtual ui::GestureStatus PreHandleGestureEvent( |
| 82 Window* target, GestureEvent* event) OVERRIDE { | 92 Window* target, GestureEvent* event) OVERRIDE { |
| 83 return ui::GESTURE_STATUS_UNKNOWN; | 93 return ui::GESTURE_STATUS_UNKNOWN; |
| 84 } | 94 } |
| 85 | 95 |
| 86 private: | 96 private: |
| 87 // How many key events have been received? | 97 // How many key events have been received? |
| 88 int num_key_events_; | 98 int num_key_events_; |
| 89 | 99 |
| 90 DISALLOW_COPY_AND_ASSIGN(TestEventFilter); | 100 // How many mouse events have been received? |
| 101 int num_mouse_events_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(EventCountFilter); |
| 91 }; | 104 }; |
| 92 | 105 |
| 93 } // namespace | 106 } // namespace |
| 94 | 107 |
| 95 typedef test::AuraTestBase RootWindowTest; | 108 typedef test::AuraTestBase RootWindowTest; |
| 96 | 109 |
| 97 TEST_F(RootWindowTest, DispatchMouseEvent) { | 110 TEST_F(RootWindowTest, DispatchMouseEvent) { |
| 98 // Create two non-overlapping windows so we don't have to worry about which | 111 // Create two non-overlapping windows so we don't have to worry about which |
| 99 // is on top. | 112 // is on top. |
| 100 scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate()); | 113 scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate()); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 EXPECT_EQ("100,100", root.location().ToString()); | 205 EXPECT_EQ("100,100", root.location().ToString()); |
| 193 EXPECT_EQ("100,100", root.root_location().ToString()); | 206 EXPECT_EQ("100,100", root.root_location().ToString()); |
| 194 | 207 |
| 195 MouseEvent translated_event( | 208 MouseEvent translated_event( |
| 196 root, root_window(), w1.get(), | 209 root, root_window(), w1.get(), |
| 197 ui::ET_MOUSE_ENTERED, root.flags()); | 210 ui::ET_MOUSE_ENTERED, root.flags()); |
| 198 EXPECT_EQ("50,50", translated_event.location().ToString()); | 211 EXPECT_EQ("50,50", translated_event.location().ToString()); |
| 199 EXPECT_EQ("100,100", translated_event.root_location().ToString()); | 212 EXPECT_EQ("100,100", translated_event.root_location().ToString()); |
| 200 } | 213 } |
| 201 | 214 |
| 215 namespace { |
| 216 |
| 217 class TestEventClient : public client::EventClient { |
| 218 public: |
| 219 static const int kNonLockWindowId = 100; |
| 220 static const int kLockWindowId = 200; |
| 221 |
| 222 explicit TestEventClient(RootWindow* root_window) |
| 223 : root_window_(root_window), |
| 224 lock_(false) { |
| 225 client::SetEventClient(root_window_, this); |
| 226 Window* lock_window = |
| 227 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_); |
| 228 lock_window->set_id(kLockWindowId); |
| 229 Window* non_lock_window = |
| 230 test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_); |
| 231 non_lock_window->set_id(kNonLockWindowId); |
| 232 } |
| 233 virtual ~TestEventClient() { |
| 234 client::SetEventClient(root_window_, NULL); |
| 235 } |
| 236 |
| 237 // Starts/stops locking. Locking prevents windows other than those inside |
| 238 // the lock container from receiving events, getting focus etc. |
| 239 void Lock() { |
| 240 lock_ = true; |
| 241 } |
| 242 void Unlock() { |
| 243 lock_ = false; |
| 244 } |
| 245 |
| 246 Window* GetLockWindow() { |
| 247 return const_cast<Window*>( |
| 248 static_cast<const TestEventClient*>(this)->GetLockWindow()); |
| 249 } |
| 250 const Window* GetLockWindow() const { |
| 251 return root_window_->GetChildById(kLockWindowId); |
| 252 } |
| 253 Window* GetNonLockWindow() { |
| 254 return root_window_->GetChildById(kNonLockWindowId); |
| 255 } |
| 256 |
| 257 private: |
| 258 // Overridden from client::EventClient: |
| 259 virtual bool CanProcessEventsWithinSubtree( |
| 260 const Window* window) const OVERRIDE { |
| 261 return lock_ ? GetLockWindow()->Contains(window) : true; |
| 262 } |
| 263 |
| 264 RootWindow* root_window_; |
| 265 bool lock_; |
| 266 |
| 267 DISALLOW_COPY_AND_ASSIGN(TestEventClient); |
| 268 }; |
| 269 |
| 270 } // namespace |
| 271 |
| 272 TEST_F(RootWindowTest, CanProcessEventsWithinSubtree) { |
| 273 TestEventClient client(root_window()); |
| 274 test::TestWindowDelegate d; |
| 275 |
| 276 EventCountFilter* nonlock_ef = new EventCountFilter; |
| 277 EventCountFilter* lock_ef = new EventCountFilter; |
| 278 client.GetNonLockWindow()->SetEventFilter(nonlock_ef); |
| 279 client.GetLockWindow()->SetEventFilter(lock_ef); |
| 280 |
| 281 Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20), |
| 282 client.GetNonLockWindow()); |
| 283 w1->set_id(1); |
| 284 Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20), |
| 285 client.GetNonLockWindow()); |
| 286 w2->set_id(2); |
| 287 scoped_ptr<Window> w3( |
| 288 test::CreateTestWindowWithDelegate(&d, 3, gfx::Rect(20, 20, 20, 20), |
| 289 client.GetLockWindow())); |
| 290 |
| 291 w1->Focus(); |
| 292 EXPECT_TRUE(w1->GetFocusManager()->IsFocusedWindow(w1)); |
| 293 |
| 294 client.Lock(); |
| 295 |
| 296 // Since we're locked, the attempt to focus w2 will be ignored. |
| 297 w2->Focus(); |
| 298 EXPECT_TRUE(w1->GetFocusManager()->IsFocusedWindow(w1)); |
| 299 EXPECT_FALSE(w1->GetFocusManager()->IsFocusedWindow(w2)); |
| 300 |
| 301 { |
| 302 // Attempting to send a key event to w1 (not in the lock container) should |
| 303 // cause focus to be reset. |
| 304 test::EventGenerator generator(root_window()); |
| 305 generator.PressKey(ui::VKEY_SPACE, 0); |
| 306 EXPECT_EQ(NULL, w1->GetFocusManager()->GetFocusedWindow()); |
| 307 } |
| 308 |
| 309 { |
| 310 // Events sent to a window not in the lock container will not be processed. |
| 311 // i.e. never sent to the non-lock container's event filter. |
| 312 test::EventGenerator generator(root_window(), w1); |
| 313 generator.PressLeftButton(); |
| 314 EXPECT_EQ(0, nonlock_ef->num_mouse_events()); |
| 315 |
| 316 // Events sent to a window in the lock container will be processed. |
| 317 test::EventGenerator generator3(root_window(), w3.get()); |
| 318 generator3.PressLeftButton(); |
| 319 EXPECT_EQ(1, lock_ef->num_mouse_events()); |
| 320 } |
| 321 |
| 322 // Prevent w3 from being deleted by the hierarchy since its delegate is owned |
| 323 // by this scope. |
| 324 w3->parent()->RemoveChild(w3.get()); |
| 325 } |
| 326 |
| 202 TEST_F(RootWindowTest, IgnoreUnknownKeys) { | 327 TEST_F(RootWindowTest, IgnoreUnknownKeys) { |
| 203 TestEventFilter* filter = new TestEventFilter; | 328 EventCountFilter* filter = new EventCountFilter; |
| 204 root_window()->SetEventFilter(filter); // passes ownership | 329 root_window()->SetEventFilter(filter); // passes ownership |
| 205 | 330 |
| 206 KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0); | 331 KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0); |
| 207 EXPECT_FALSE(root_window()->DispatchKeyEvent(&unknown_event)); | 332 EXPECT_FALSE(root_window()->DispatchKeyEvent(&unknown_event)); |
| 208 EXPECT_EQ(0, filter->num_key_events()); | 333 EXPECT_EQ(0, filter->num_key_events()); |
| 209 | 334 |
| 210 KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | 335 KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); |
| 211 EXPECT_TRUE(root_window()->DispatchKeyEvent(&known_event)); | 336 EXPECT_TRUE(root_window()->DispatchKeyEvent(&known_event)); |
| 212 EXPECT_EQ(1, filter->num_key_events()); | 337 EXPECT_EQ(1, filter->num_key_events()); |
| 213 } | 338 } |
| 214 | 339 |
| 215 } // namespace aura | 340 } // namespace aura |
| OLD | NEW |