| 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 "ui/aura/desktop.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 #include "ui/aura/event.h" | |
| 9 #include "ui/aura/test/aura_test_base.h" | |
| 10 #include "ui/aura/test/test_window_delegate.h" | |
| 11 #include "ui/aura/test/test_windows.h" | |
| 12 #include "ui/base/hit_test.h" | |
| 13 #include "ui/gfx/point.h" | |
| 14 #include "ui/gfx/rect.h" | |
| 15 | |
| 16 namespace aura { | |
| 17 namespace test { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // A delegate that always returns a non-client component for hit tests. | |
| 22 class NonClientDelegate : public TestWindowDelegate { | |
| 23 public: | |
| 24 NonClientDelegate() | |
| 25 : non_client_count_(0), | |
| 26 mouse_event_count_(0), | |
| 27 mouse_event_flags_(0x0) { | |
| 28 } | |
| 29 virtual ~NonClientDelegate() {} | |
| 30 | |
| 31 int non_client_count() const { return non_client_count_; } | |
| 32 gfx::Point non_client_location() const { return non_client_location_; } | |
| 33 int mouse_event_count() const { return mouse_event_count_; } | |
| 34 gfx::Point mouse_event_location() const { return mouse_event_location_; } | |
| 35 int mouse_event_flags() const { return mouse_event_flags_; } | |
| 36 | |
| 37 virtual int GetNonClientComponent(const gfx::Point& location) const OVERRIDE { | |
| 38 NonClientDelegate* self = const_cast<NonClientDelegate*>(this); | |
| 39 self->non_client_count_++; | |
| 40 self->non_client_location_ = location; | |
| 41 return HTTOPLEFT; | |
| 42 } | |
| 43 virtual bool OnMouseEvent(MouseEvent* event) OVERRIDE { | |
| 44 mouse_event_count_++; | |
| 45 mouse_event_location_ = event->location(); | |
| 46 mouse_event_flags_ = event->flags(); | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 int non_client_count_; | |
| 52 gfx::Point non_client_location_; | |
| 53 int mouse_event_count_; | |
| 54 gfx::Point mouse_event_location_; | |
| 55 int mouse_event_flags_; | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 typedef AuraTestBase DesktopTest; | |
| 61 | |
| 62 TEST_F(DesktopTest, DispatchMouseEvent) { | |
| 63 // Create two non-overlapping windows so we don't have to worry about which | |
| 64 // is on top. | |
| 65 scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate()); | |
| 66 scoped_ptr<NonClientDelegate> delegate2(new NonClientDelegate()); | |
| 67 const int kWindowWidth = 123; | |
| 68 const int kWindowHeight = 45; | |
| 69 gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight); | |
| 70 gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight); | |
| 71 scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate( | |
| 72 delegate1.get(), -1234, bounds1, NULL)); | |
| 73 scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate( | |
| 74 delegate2.get(), -5678, bounds2, NULL)); | |
| 75 | |
| 76 // Send a mouse event to window1. | |
| 77 gfx::Point point(101, 201); | |
| 78 MouseEvent event1(ui::ET_MOUSE_PRESSED, point, ui::EF_LEFT_BUTTON_DOWN); | |
| 79 Desktop::GetInstance()->DispatchMouseEvent(&event1); | |
| 80 | |
| 81 // Event was tested for non-client area for the target window. | |
| 82 EXPECT_EQ(1, delegate1->non_client_count()); | |
| 83 EXPECT_EQ(0, delegate2->non_client_count()); | |
| 84 // The non-client component test was in local coordinates. | |
| 85 EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location()); | |
| 86 // Mouse event was received by target window. | |
| 87 EXPECT_EQ(1, delegate1->mouse_event_count()); | |
| 88 EXPECT_EQ(0, delegate2->mouse_event_count()); | |
| 89 // Event was in local coordinates. | |
| 90 EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location()); | |
| 91 // Non-client flag was set. | |
| 92 EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT); | |
| 93 } | |
| 94 | |
| 95 // Check that we correctly track the state of the mouse buttons in response to | |
| 96 // button press and release events. | |
| 97 TEST_F(DesktopTest, MouseButtonState) { | |
| 98 Desktop* desktop = Desktop::GetInstance(); | |
| 99 EXPECT_FALSE(desktop->IsMouseButtonDown()); | |
| 100 | |
| 101 gfx::Point location; | |
| 102 scoped_ptr<MouseEvent> event; | |
| 103 | |
| 104 // Press the left button. | |
| 105 event.reset(new MouseEvent( | |
| 106 ui::ET_MOUSE_PRESSED, | |
| 107 location, | |
| 108 ui::EF_LEFT_BUTTON_DOWN)); | |
| 109 desktop->DispatchMouseEvent(event.get()); | |
| 110 EXPECT_TRUE(desktop->IsMouseButtonDown()); | |
| 111 | |
| 112 // Additionally press the right. | |
| 113 event.reset(new MouseEvent( | |
| 114 ui::ET_MOUSE_PRESSED, | |
| 115 location, | |
| 116 ui::EF_LEFT_BUTTON_DOWN | ui::EF_RIGHT_BUTTON_DOWN)); | |
| 117 desktop->DispatchMouseEvent(event.get()); | |
| 118 EXPECT_TRUE(desktop->IsMouseButtonDown()); | |
| 119 | |
| 120 // Release the left button. | |
| 121 event.reset(new MouseEvent( | |
| 122 ui::ET_MOUSE_RELEASED, | |
| 123 location, | |
| 124 ui::EF_RIGHT_BUTTON_DOWN)); | |
| 125 desktop->DispatchMouseEvent(event.get()); | |
| 126 EXPECT_TRUE(desktop->IsMouseButtonDown()); | |
| 127 | |
| 128 // Release the right button. We should ignore the Shift-is-down flag. | |
| 129 event.reset(new MouseEvent( | |
| 130 ui::ET_MOUSE_RELEASED, | |
| 131 location, | |
| 132 ui::EF_SHIFT_DOWN)); | |
| 133 desktop->DispatchMouseEvent(event.get()); | |
| 134 EXPECT_FALSE(desktop->IsMouseButtonDown()); | |
| 135 | |
| 136 // Press the middle button. | |
| 137 event.reset(new MouseEvent( | |
| 138 ui::ET_MOUSE_PRESSED, | |
| 139 location, | |
| 140 ui::EF_MIDDLE_BUTTON_DOWN)); | |
| 141 desktop->DispatchMouseEvent(event.get()); | |
| 142 EXPECT_TRUE(desktop->IsMouseButtonDown()); | |
| 143 } | |
| 144 | |
| 145 } // namespace test | |
| 146 } // namespace aura | |
| OLD | NEW |