Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: components/mus/ws/event_dispatcher_unittest.cc

Issue 1909733002: mus: Add EventObserver to allow passively listening to UI events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/mus/ws/event_dispatcher.h" 5 #include "components/mus/ws/event_dispatcher.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <queue> 10 #include <queue>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "components/mus/public/cpp/event_matcher.h" 14 #include "components/mus/public/cpp/event_matcher_util.h"
15 #include "components/mus/ws/accelerator.h" 15 #include "components/mus/ws/accelerator.h"
16 #include "components/mus/ws/event_dispatcher_delegate.h" 16 #include "components/mus/ws/event_dispatcher_delegate.h"
17 #include "components/mus/ws/server_window.h" 17 #include "components/mus/ws/server_window.h"
18 #include "components/mus/ws/server_window_surface_manager_test_api.h" 18 #include "components/mus/ws/server_window_surface_manager_test_api.h"
19 #include "components/mus/ws/test_server_window_delegate.h" 19 #include "components/mus/ws/test_server_window_delegate.h"
20 #include "components/mus/ws/test_utils.h" 20 #include "components/mus/ws/test_utils.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/events/event.h" 22 #include "ui/events/event.h"
23 23
24 namespace mus { 24 namespace mus {
(...skipping 14 matching lines...) Expand all
39 39
40 class TestEventDispatcherDelegate : public EventDispatcherDelegate { 40 class TestEventDispatcherDelegate : public EventDispatcherDelegate {
41 public: 41 public:
42 explicit TestEventDispatcherDelegate(ServerWindow* root) 42 explicit TestEventDispatcherDelegate(ServerWindow* root)
43 : root_(root), 43 : root_(root),
44 focused_window_(nullptr), 44 focused_window_(nullptr),
45 lost_capture_window_(nullptr), 45 lost_capture_window_(nullptr),
46 last_accelerator_(0) {} 46 last_accelerator_(0) {}
47 ~TestEventDispatcherDelegate() override {} 47 ~TestEventDispatcherDelegate() override {}
48 48
49 ui::Event* last_event_target_not_found() {
50 return last_event_target_not_found_.get();
51 }
52
49 uint32_t GetAndClearLastAccelerator() { 53 uint32_t GetAndClearLastAccelerator() {
50 uint32_t return_value = last_accelerator_; 54 uint32_t return_value = last_accelerator_;
51 last_accelerator_ = 0; 55 last_accelerator_ = 0;
52 return return_value; 56 return return_value;
53 } 57 }
54 58
55 // Returns the last dispatched event, or null if there are no more. 59 // Returns the last dispatched event, or null if there are no more.
56 scoped_ptr<DispatchedEventDetails> GetAndAdvanceDispatchedEventDetails() { 60 scoped_ptr<DispatchedEventDetails> GetAndAdvanceDispatchedEventDetails() {
57 if (dispatched_event_queue_.empty()) 61 if (dispatched_event_queue_.empty())
58 return nullptr; 62 return nullptr;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 bool in_nonclient_area, 99 bool in_nonclient_area,
96 const ui::Event& event, 100 const ui::Event& event,
97 Accelerator* accelerator) override { 101 Accelerator* accelerator) override {
98 scoped_ptr<DispatchedEventDetails> details(new DispatchedEventDetails); 102 scoped_ptr<DispatchedEventDetails> details(new DispatchedEventDetails);
99 details->window = target; 103 details->window = target;
100 details->in_nonclient_area = in_nonclient_area; 104 details->in_nonclient_area = in_nonclient_area;
101 details->event = ui::Event::Clone(event); 105 details->event = ui::Event::Clone(event);
102 details->accelerator = accelerator; 106 details->accelerator = accelerator;
103 dispatched_event_queue_.push(std::move(details)); 107 dispatched_event_queue_.push(std::move(details));
104 } 108 }
109 void OnEventTargetNotFound(const ui::Event& event) override {
110 last_event_target_not_found_ = ui::Event::Clone(event);
111 }
105 112
106 ServerWindow* root_; 113 ServerWindow* root_;
107 ServerWindow* focused_window_; 114 ServerWindow* focused_window_;
108 ServerWindow* lost_capture_window_; 115 ServerWindow* lost_capture_window_;
109 uint32_t last_accelerator_; 116 uint32_t last_accelerator_;
110 std::queue<scoped_ptr<DispatchedEventDetails>> dispatched_event_queue_; 117 std::queue<scoped_ptr<DispatchedEventDetails>> dispatched_event_queue_;
118 std::unique_ptr<ui::Event> last_event_target_not_found_;
111 119
112 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcherDelegate); 120 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcherDelegate);
113 }; 121 };
114 122
115 // Used by RunMouseEventTests(). Can identify up to two generated events. The 123 // Used by RunMouseEventTests(). Can identify up to two generated events. The
116 // first ServerWindow and two points identify the first event, the second 124 // first ServerWindow and two points identify the first event, the second
117 // ServerWindow and points identify the second event. If only one event is 125 // ServerWindow and points identify the second event. If only one event is
118 // generated set the second window to null. 126 // generated set the second window to null.
119 struct MouseEventTest { 127 struct MouseEventTest {
120 ui::MouseEvent input_event; 128 ui::MouseEvent input_event;
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 ASSERT_EQ(child.get(), details->window); 295 ASSERT_EQ(child.get(), details->window);
288 296
289 ASSERT_TRUE(details->event); 297 ASSERT_TRUE(details->event);
290 ASSERT_TRUE(details->event->IsPointerEvent()); 298 ASSERT_TRUE(details->event->IsPointerEvent());
291 299
292 ui::PointerEvent* dispatched_event = details->event->AsPointerEvent(); 300 ui::PointerEvent* dispatched_event = details->event->AsPointerEvent();
293 EXPECT_EQ(gfx::Point(20, 25), dispatched_event->root_location()); 301 EXPECT_EQ(gfx::Point(20, 25), dispatched_event->root_location());
294 EXPECT_EQ(gfx::Point(10, 15), dispatched_event->location()); 302 EXPECT_EQ(gfx::Point(10, 15), dispatched_event->location());
295 } 303 }
296 304
305 TEST_F(EventDispatcherTest, ProcessEventNoTarget) {
306 // Send event without a target.
307 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
308 event_dispatcher()->ProcessEvent(key);
309
310 // Event wasn't dispatched to a target.
311 scoped_ptr<DispatchedEventDetails> details =
312 test_event_dispatcher_delegate()->GetAndAdvanceDispatchedEventDetails();
313 EXPECT_FALSE(details);
314
315 // Delegate was informed that there wasn't a target.
316 ui::Event* event_out =
317 test_event_dispatcher_delegate()->last_event_target_not_found();
318 ASSERT_TRUE(event_out);
319 EXPECT_TRUE(event_out->IsKeyEvent());
320 EXPECT_EQ(ui::VKEY_A, event_out->AsKeyEvent()->key_code());
321 }
322
297 TEST_F(EventDispatcherTest, AcceleratorBasic) { 323 TEST_F(EventDispatcherTest, AcceleratorBasic) {
298 ClearSetup(); 324 ClearSetup();
299 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr); 325 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr);
300 EventDispatcher dispatcher(&event_dispatcher_delegate); 326 EventDispatcher dispatcher(&event_dispatcher_delegate);
301 327
302 uint32_t accelerator_1 = 1; 328 uint32_t accelerator_1 = 1;
303 mojom::EventMatcherPtr matcher = mus::CreateKeyMatcher( 329 mojom::EventMatcherPtr matcher = mus::CreateKeyMatcher(
304 mus::mojom::KeyboardCode::W, mus::mojom::kEventFlagControlDown); 330 mus::mojom::KeyboardCode::W, mus::mojom::kEventFlagControlDown);
305 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_1, std::move(matcher))); 331 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_1, std::move(matcher)));
306 332
(...skipping 1027 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 w1->AddTransientWindow(w2.get()); 1360 w1->AddTransientWindow(w2.get());
1335 w2->SetModal(); 1361 w2->SetModal();
1336 1362
1337 EXPECT_TRUE(event_dispatcher()->SetCaptureWindow(w3.get(), false)); 1363 EXPECT_TRUE(event_dispatcher()->SetCaptureWindow(w3.get(), false));
1338 EXPECT_EQ(w3.get(), event_dispatcher()->capture_window()); 1364 EXPECT_EQ(w3.get(), event_dispatcher()->capture_window());
1339 } 1365 }
1340 1366
1341 } // namespace test 1367 } // namespace test
1342 } // namespace ws 1368 } // namespace ws
1343 } // namespace mus 1369 } // namespace mus
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698