OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "ui/aura/desktop.h" | 9 #include "ui/aura/desktop.h" |
10 #include "ui/aura/event.h" | 10 #include "ui/aura/event.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 const gfx::Rect& new_bounds) OVERRIDE {} | 35 const gfx::Rect& new_bounds) OVERRIDE {} |
36 virtual void OnFocus() OVERRIDE {} | 36 virtual void OnFocus() OVERRIDE {} |
37 virtual void OnBlur() OVERRIDE {} | 37 virtual void OnBlur() OVERRIDE {} |
38 virtual bool OnKeyEvent(KeyEvent* event) OVERRIDE { | 38 virtual bool OnKeyEvent(KeyEvent* event) OVERRIDE { |
39 return false; | 39 return false; |
40 } | 40 } |
41 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | 41 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { |
42 return HTCLIENT; | 42 return HTCLIENT; |
43 } | 43 } |
44 virtual bool OnMouseEvent(MouseEvent* event) OVERRIDE { return false; } | 44 virtual bool OnMouseEvent(MouseEvent* event) OVERRIDE { return false; } |
| 45 virtual void OnCaptureGained() OVERRIDE {} |
| 46 virtual void OnCaptureLost() OVERRIDE {} |
45 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} | 47 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} |
46 virtual void OnWindowDestroying() OVERRIDE {} | 48 virtual void OnWindowDestroying() OVERRIDE {} |
47 virtual void OnWindowDestroyed() OVERRIDE {} | 49 virtual void OnWindowDestroyed() OVERRIDE {} |
48 | 50 |
49 private: | 51 private: |
50 DISALLOW_COPY_AND_ASSIGN(WindowDelegateImpl); | 52 DISALLOW_COPY_AND_ASSIGN(WindowDelegateImpl); |
51 }; | 53 }; |
52 | 54 |
53 // Used for verifying destruction methods are invoked. | 55 // Used for verifying destruction methods are invoked. |
54 class DestroyTrackingDelegateImpl : public WindowDelegateImpl { | 56 class DestroyTrackingDelegateImpl : public WindowDelegateImpl { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 EXPECT_TRUE(parent_delegate_->in_destroying()); | 101 EXPECT_TRUE(parent_delegate_->in_destroying()); |
100 DestroyTrackingDelegateImpl::OnWindowDestroying(); | 102 DestroyTrackingDelegateImpl::OnWindowDestroying(); |
101 } | 103 } |
102 | 104 |
103 private: | 105 private: |
104 DestroyTrackingDelegateImpl* parent_delegate_; | 106 DestroyTrackingDelegateImpl* parent_delegate_; |
105 | 107 |
106 DISALLOW_COPY_AND_ASSIGN(ChildWindowDelegateImpl); | 108 DISALLOW_COPY_AND_ASSIGN(ChildWindowDelegateImpl); |
107 }; | 109 }; |
108 | 110 |
| 111 // Used in verifying mouse capture. |
| 112 class CaptureWindowDelegateImpl : public WindowDelegateImpl { |
| 113 public: |
| 114 explicit CaptureWindowDelegateImpl() |
| 115 : capture_gained_count_(0), |
| 116 capture_lost_count_(0), |
| 117 mouse_event_count_(0) { |
| 118 } |
| 119 |
| 120 int capture_gained_count() const { return capture_gained_count_; } |
| 121 void set_capture_gained_count(int value) { capture_gained_count_ = value; } |
| 122 int capture_lost_count() const { return capture_lost_count_; } |
| 123 void set_capture_lost_count(int value) { capture_lost_count_ = value; } |
| 124 int mouse_event_count() const { return mouse_event_count_; } |
| 125 void set_mouse_event_count(int value) { mouse_event_count_ = value; } |
| 126 |
| 127 virtual bool OnMouseEvent(MouseEvent* event) OVERRIDE { |
| 128 mouse_event_count_++; |
| 129 return false; |
| 130 } |
| 131 virtual void OnCaptureGained() OVERRIDE { |
| 132 capture_gained_count_++; |
| 133 } |
| 134 virtual void OnCaptureLost() OVERRIDE { |
| 135 capture_lost_count_++; |
| 136 } |
| 137 |
| 138 private: |
| 139 int capture_gained_count_; |
| 140 int capture_lost_count_; |
| 141 int mouse_event_count_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(CaptureWindowDelegateImpl); |
| 144 }; |
| 145 |
109 // A simple WindowDelegate implementation for these tests. It owns itself | 146 // A simple WindowDelegate implementation for these tests. It owns itself |
110 // (deletes itself when the Window it is attached to is destroyed). | 147 // (deletes itself when the Window it is attached to is destroyed). |
111 class TestWindowDelegate : public WindowDelegateImpl { | 148 class TestWindowDelegate : public WindowDelegateImpl { |
112 public: | 149 public: |
113 TestWindowDelegate(SkColor color) | 150 TestWindowDelegate(SkColor color) |
114 : color_(color), | 151 : color_(color), |
115 last_key_code_(ui::VKEY_UNKNOWN) { | 152 last_key_code_(ui::VKEY_UNKNOWN) { |
116 } | 153 } |
117 virtual ~TestWindowDelegate() {} | 154 virtual ~TestWindowDelegate() {} |
118 | 155 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 335 |
299 parent.MoveChildToFront(&child1); | 336 parent.MoveChildToFront(&child1); |
300 ASSERT_EQ(2u, parent.children().size()); | 337 ASSERT_EQ(2u, parent.children().size()); |
301 EXPECT_EQ(&child1, parent.children()[1]); | 338 EXPECT_EQ(&child1, parent.children()[1]); |
302 EXPECT_EQ(&child2, parent.children()[0]); | 339 EXPECT_EQ(&child2, parent.children()[0]); |
303 ASSERT_EQ(2u, parent.layer()->children().size()); | 340 ASSERT_EQ(2u, parent.layer()->children().size()); |
304 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]); | 341 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]); |
305 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]); | 342 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]); |
306 } | 343 } |
307 | 344 |
| 345 // Various destruction assertions. |
| 346 TEST_F(WindowTest, CaptureTests) { |
| 347 Desktop* desktop = Desktop::GetInstance(); |
| 348 CaptureWindowDelegateImpl delegate; |
| 349 scoped_ptr<Window> window(CreateTestWindowWithDelegate( |
| 350 &delegate, 0, gfx::Rect(0, 0, 20, 20), NULL)); |
| 351 EXPECT_FALSE(window->HasCapture()); |
| 352 |
| 353 // Do a capture. |
| 354 window->SetCapture(); |
| 355 EXPECT_TRUE(window->HasCapture()); |
| 356 EXPECT_EQ(1, delegate.capture_gained_count()); |
| 357 EXPECT_EQ(0, delegate.capture_lost_count()); |
| 358 |
| 359 desktop->OnMouseEvent(MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), |
| 360 ui::EF_LEFT_BUTTON_DOWN)); |
| 361 EXPECT_EQ(1, delegate.mouse_event_count()); |
| 362 desktop->OnMouseEvent(MouseEvent(ui::ET_MOUSE_RELEASED, gfx::Point(50, 50), |
| 363 ui::EF_LEFT_BUTTON_DOWN)); |
| 364 EXPECT_EQ(2, delegate.mouse_event_count()); |
| 365 delegate.set_mouse_event_count(0); |
| 366 |
| 367 window->ReleaseCapture(); |
| 368 EXPECT_FALSE(window->HasCapture()); |
| 369 EXPECT_EQ(1, delegate.capture_gained_count()); |
| 370 EXPECT_EQ(1, delegate.capture_lost_count()); |
| 371 |
| 372 desktop->OnMouseEvent(MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), |
| 373 ui::EF_LEFT_BUTTON_DOWN)); |
| 374 EXPECT_EQ(0, delegate.mouse_event_count()); |
| 375 } |
| 376 |
| 377 // Various destruction assertions. |
| 378 TEST_F(WindowTest, ReleaseCaptureOnDestroy) { |
| 379 Desktop* desktop = Desktop::GetInstance(); |
| 380 RootWindow* root = static_cast<RootWindow*>(desktop->window()); |
| 381 CaptureWindowDelegateImpl delegate; |
| 382 scoped_ptr<Window> window(CreateTestWindowWithDelegate( |
| 383 &delegate, 0, gfx::Rect(0, 0, 20, 20), NULL)); |
| 384 EXPECT_FALSE(window->HasCapture()); |
| 385 |
| 386 // Do a capture. |
| 387 window->SetCapture(); |
| 388 EXPECT_TRUE(window->HasCapture()); |
| 389 |
| 390 // Destroy the window. |
| 391 window.reset(); |
| 392 |
| 393 // Make sure the root doesn't reference the window anymore. |
| 394 EXPECT_EQ(NULL, root->mouse_pressed_handler()); |
| 395 EXPECT_EQ(NULL, root->capture_window()); |
| 396 } |
| 397 |
308 } // namespace internal | 398 } // namespace internal |
309 } // namespace aura | 399 } // namespace aura |
310 | |
OLD | NEW |