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