| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/views/mus/window_manager_connection.h" | 5 #include "ui/views/mus/window_manager_connection.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/events/event.h" | 11 #include "ui/events/event.h" |
| 12 #include "ui/views/pointer_watcher.h" | 12 #include "ui/views/pointer_watcher.h" |
| 13 #include "ui/views/test/scoped_views_test_helper.h" | 13 #include "ui/views/test/scoped_views_test_helper.h" |
| 14 | 14 |
| 15 namespace views { | 15 namespace views { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 class TestPointerWatcher : public PointerWatcher { | 18 class TestPointerWatcher : public PointerWatcher { |
| 19 public: | 19 public: |
| 20 TestPointerWatcher() {} | 20 TestPointerWatcher() {} |
| 21 ~TestPointerWatcher() override {} | 21 ~TestPointerWatcher() override {} |
| 22 | 22 |
| 23 ui::Event* last_event() { return last_event_.get(); } | 23 bool mouse_pressed() const { return mouse_pressed_; } |
| 24 bool touch_pressed() const { return touch_pressed_; } |
| 24 | 25 |
| 25 void Reset() { last_event_.reset(); } | 26 void Reset() { |
| 27 mouse_pressed_ = false; |
| 28 touch_pressed_ = false; |
| 29 } |
| 26 | 30 |
| 27 // PointerWatcher: | 31 // PointerWatcher: |
| 28 void OnMousePressed(const ui::MouseEvent& event, | 32 void OnMousePressed(const ui::MouseEvent& event, |
| 29 const gfx::Point& location_in_screen) override { | 33 const gfx::Point& location_in_screen, |
| 30 last_event_ = ui::Event::Clone(event); | 34 Widget* target) override { |
| 35 mouse_pressed_ = true; |
| 31 } | 36 } |
| 32 void OnTouchPressed(const ui::TouchEvent& event, | 37 void OnTouchPressed(const ui::TouchEvent& event, |
| 33 const gfx::Point& location_in_screen) override { | 38 const gfx::Point& location_in_screen, |
| 34 last_event_ = ui::Event::Clone(event); | 39 Widget* target) override { |
| 40 touch_pressed_ = true; |
| 35 } | 41 } |
| 36 | 42 |
| 37 private: | 43 private: |
| 38 std::unique_ptr<ui::Event> last_event_; | 44 bool mouse_pressed_ = false; |
| 45 bool touch_pressed_ = false; |
| 39 | 46 |
| 40 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 47 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
| 41 }; | 48 }; |
| 42 | 49 |
| 43 } // namespace | 50 } // namespace |
| 44 | 51 |
| 45 class WindowManagerConnectionTest : public testing::Test { | 52 class WindowManagerConnectionTest : public testing::Test { |
| 46 public: | 53 public: |
| 47 WindowManagerConnectionTest() {} | 54 WindowManagerConnectionTest() {} |
| 48 ~WindowManagerConnectionTest() override {} | 55 ~WindowManagerConnectionTest() override {} |
| 49 | 56 |
| 50 void OnEventObserved(const ui::Event& event) { | 57 void OnEventObserved(const ui::Event& event) { |
| 51 WindowManagerConnection::Get()->OnEventObserved(event); | 58 WindowManagerConnection::Get()->OnEventObserved(event, nullptr); |
| 52 } | 59 } |
| 53 | 60 |
| 54 private: | 61 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); | 62 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnectionTest); |
| 56 }; | 63 }; |
| 57 | 64 |
| 58 TEST_F(WindowManagerConnectionTest, PointerWatcher) { | 65 TEST_F(WindowManagerConnectionTest, PointerWatcher) { |
| 59 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | 66 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 60 ScopedViewsTestHelper helper; | 67 ScopedViewsTestHelper helper; |
| 61 WindowManagerConnection* connection = WindowManagerConnection::Get(); | 68 WindowManagerConnection* connection = WindowManagerConnection::Get(); |
| 62 ASSERT_TRUE(connection); | 69 ASSERT_TRUE(connection); |
| 63 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 70 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 64 base::TimeDelta(), ui::EF_NONE, 0); | 71 base::TimeDelta(), ui::EF_NONE, 0); |
| 65 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, | 72 ui::TouchEvent touch_pressed(ui::ET_TOUCH_PRESSED, gfx::Point(), 1, |
| 66 base::TimeDelta()); | 73 base::TimeDelta()); |
| 67 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | 74 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); |
| 68 | 75 |
| 69 // PointerWatchers receive mouse events. | 76 // PointerWatchers receive mouse events. |
| 70 TestPointerWatcher watcher1; | 77 TestPointerWatcher watcher1; |
| 71 connection->AddPointerWatcher(&watcher1); | 78 connection->AddPointerWatcher(&watcher1); |
| 72 OnEventObserved(mouse_pressed); | 79 OnEventObserved(mouse_pressed); |
| 73 EXPECT_EQ(ui::ET_MOUSE_PRESSED, watcher1.last_event()->type()); | 80 EXPECT_TRUE(watcher1.mouse_pressed()); |
| 74 watcher1.Reset(); | 81 watcher1.Reset(); |
| 75 | 82 |
| 76 // PointerWatchers receive touch events. | 83 // PointerWatchers receive touch events. |
| 77 OnEventObserved(touch_pressed); | 84 OnEventObserved(touch_pressed); |
| 78 EXPECT_EQ(ui::ET_TOUCH_PRESSED, watcher1.last_event()->type()); | 85 EXPECT_TRUE(watcher1.touch_pressed()); |
| 79 watcher1.Reset(); | 86 watcher1.Reset(); |
| 80 | 87 |
| 81 // PointerWatchers do not receive key events. | 88 // PointerWatchers do not trigger for key events. |
| 82 OnEventObserved(key_pressed); | 89 OnEventObserved(key_pressed); |
| 83 EXPECT_FALSE(watcher1.last_event()); | 90 EXPECT_FALSE(watcher1.mouse_pressed()); |
| 91 EXPECT_FALSE(watcher1.touch_pressed()); |
| 84 watcher1.Reset(); | 92 watcher1.Reset(); |
| 85 | 93 |
| 86 // Two PointerWatchers can both receive a single observed event. | 94 // Two PointerWatchers can both receive a single observed event. |
| 87 TestPointerWatcher watcher2; | 95 TestPointerWatcher watcher2; |
| 88 connection->AddPointerWatcher(&watcher2); | 96 connection->AddPointerWatcher(&watcher2); |
| 89 OnEventObserved(mouse_pressed); | 97 OnEventObserved(mouse_pressed); |
| 90 EXPECT_EQ(ui::ET_MOUSE_PRESSED, watcher1.last_event()->type()); | 98 EXPECT_TRUE(watcher1.mouse_pressed()); |
| 91 EXPECT_EQ(ui::ET_MOUSE_PRESSED, watcher2.last_event()->type()); | 99 EXPECT_TRUE(watcher2.mouse_pressed()); |
| 92 watcher1.Reset(); | 100 watcher1.Reset(); |
| 93 watcher2.Reset(); | 101 watcher2.Reset(); |
| 94 | 102 |
| 95 // Removing the first PointerWatcher stops sending events to it. | 103 // Removing the first PointerWatcher stops sending events to it. |
| 96 connection->RemovePointerWatcher(&watcher1); | 104 connection->RemovePointerWatcher(&watcher1); |
| 97 OnEventObserved(mouse_pressed); | 105 OnEventObserved(mouse_pressed); |
| 98 EXPECT_FALSE(watcher1.last_event()); | 106 EXPECT_FALSE(watcher1.mouse_pressed()); |
| 99 EXPECT_EQ(ui::ET_MOUSE_PRESSED, watcher2.last_event()->type()); | 107 EXPECT_TRUE(watcher2.mouse_pressed()); |
| 100 watcher1.Reset(); | 108 watcher1.Reset(); |
| 101 watcher2.Reset(); | 109 watcher2.Reset(); |
| 102 | 110 |
| 103 // Removing the last PointerWatcher stops sending events to it. | 111 // Removing the last PointerWatcher stops sending events to it. |
| 104 connection->RemovePointerWatcher(&watcher2); | 112 connection->RemovePointerWatcher(&watcher2); |
| 105 OnEventObserved(mouse_pressed); | 113 OnEventObserved(mouse_pressed); |
| 106 EXPECT_FALSE(watcher1.last_event()); | 114 EXPECT_FALSE(watcher1.mouse_pressed()); |
| 107 EXPECT_FALSE(watcher2.last_event()); | 115 EXPECT_FALSE(watcher1.touch_pressed()); |
| 108 } | 116 } |
| 109 | 117 |
| 110 } // namespace views | 118 } // namespace views |
| OLD | NEW |