OLD | NEW |
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/public/cpp/lib/window_tree_client_impl.h" | 5 #include "components/mus/public/cpp/lib/window_tree_client_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 transport_value.resize(bytes.size()); | 46 transport_value.resize(bytes.size()); |
47 memcpy(&transport_value.front(), &(bytes.front()), bytes.size()); | 47 memcpy(&transport_value.front(), &(bytes.front()), bytes.size()); |
48 return transport_value; | 48 return transport_value; |
49 } | 49 } |
50 | 50 |
51 class TestWindowTreeDelegate : public WindowTreeDelegate { | 51 class TestWindowTreeDelegate : public WindowTreeDelegate { |
52 public: | 52 public: |
53 TestWindowTreeDelegate() {} | 53 TestWindowTreeDelegate() {} |
54 ~TestWindowTreeDelegate() override {} | 54 ~TestWindowTreeDelegate() override {} |
55 | 55 |
| 56 ui::Event* last_event_observed() { return last_event_observed_.get(); } |
| 57 |
| 58 void Reset() { last_event_observed_.reset(); } |
| 59 |
56 // WindowTreeDelegate: | 60 // WindowTreeDelegate: |
57 void OnEmbed(Window* root) override {} | 61 void OnEmbed(Window* root) override {} |
58 void OnConnectionLost(WindowTreeConnection* connection) override {} | 62 void OnConnectionLost(WindowTreeConnection* connection) override {} |
| 63 void OnEventObserved(const ui::Event& event) override { |
| 64 last_event_observed_ = ui::Event::Clone(event); |
| 65 } |
59 | 66 |
60 private: | 67 private: |
| 68 std::unique_ptr<ui::Event> last_event_observed_; |
| 69 |
61 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeDelegate); | 70 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeDelegate); |
62 }; | 71 }; |
63 | 72 |
64 class WindowTreeClientImplPrivate { | 73 class WindowTreeClientImplPrivate { |
65 public: | 74 public: |
66 WindowTreeClientImplPrivate(WindowTreeClientImpl* tree_client_impl) | 75 WindowTreeClientImplPrivate(WindowTreeClientImpl* tree_client_impl) |
67 : tree_client_impl_(tree_client_impl) {} | 76 : tree_client_impl_(tree_client_impl) {} |
68 ~WindowTreeClientImplPrivate() {} | 77 ~WindowTreeClientImplPrivate() {} |
69 | 78 |
70 void Init(mojom::WindowTree* window_tree) { | 79 void Init(mojom::WindowTree* window_tree) { |
71 mojom::WindowDataPtr root_data(mojom::WindowData::New()); | 80 mojom::WindowDataPtr root_data(mojom::WindowData::New()); |
72 root_data->parent_id = 0; | 81 root_data->parent_id = 0; |
73 root_data->window_id = 1; | 82 root_data->window_id = 1; |
74 root_data->bounds = mojo::Rect::From(gfx::Rect()); | 83 root_data->bounds = mojo::Rect::From(gfx::Rect()); |
75 root_data->properties.SetToEmpty(); | 84 root_data->properties.SetToEmpty(); |
76 root_data->visible = true; | 85 root_data->visible = true; |
77 root_data->viewport_metrics = mojom::ViewportMetrics::New(); | 86 root_data->viewport_metrics = mojom::ViewportMetrics::New(); |
78 root_data->viewport_metrics->size_in_pixels = | 87 root_data->viewport_metrics->size_in_pixels = |
79 mojo::Size::From(gfx::Size(1000, 1000)); | 88 mojo::Size::From(gfx::Size(1000, 1000)); |
80 root_data->viewport_metrics->device_pixel_ratio = 1; | 89 root_data->viewport_metrics->device_pixel_ratio = 1; |
81 tree_client_impl_->OnEmbedImpl(window_tree, 1, std::move(root_data), 0, | 90 tree_client_impl_->OnEmbedImpl(window_tree, 1, std::move(root_data), 0, |
82 true); | 91 true); |
83 } | 92 } |
84 | 93 |
| 94 uint32_t event_observer_id() { return tree_client_impl_->event_observer_id_; } |
| 95 |
85 private: | 96 private: |
86 WindowTreeClientImpl* tree_client_impl_; | 97 WindowTreeClientImpl* tree_client_impl_; |
87 | 98 |
88 DISALLOW_COPY_AND_ASSIGN(WindowTreeClientImplPrivate); | 99 DISALLOW_COPY_AND_ASSIGN(WindowTreeClientImplPrivate); |
89 }; | 100 }; |
90 | 101 |
91 class WindowTreeSetup { | 102 class WindowTreeSetup { |
92 public: | 103 public: |
93 WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) { | 104 WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) { |
94 WindowTreeClientImplPrivate(&tree_client_).Init(&window_tree_); | 105 WindowTreeClientImplPrivate(&tree_client_).Init(&window_tree_); |
95 window_tree_.GetAndClearChangeId(nullptr); | 106 window_tree_.GetAndClearChangeId(nullptr); |
96 } | 107 } |
97 | 108 |
98 WindowTreeConnection* window_tree_connection() { | 109 WindowTreeConnection* window_tree_connection() { |
99 return static_cast<WindowTreeConnection*>(&tree_client_); | 110 return static_cast<WindowTreeConnection*>(&tree_client_); |
100 } | 111 } |
101 | 112 |
102 mojom::WindowTreeClient* window_tree_client() { | 113 mojom::WindowTreeClient* window_tree_client() { |
103 return static_cast<mojom::WindowTreeClient*>(&tree_client_); | 114 return static_cast<mojom::WindowTreeClient*>(&tree_client_); |
104 } | 115 } |
105 | 116 |
106 TestWindowTree* window_tree() { return &window_tree_; } | 117 TestWindowTree* window_tree() { return &window_tree_; } |
107 | 118 |
| 119 TestWindowTreeDelegate* window_tree_delegate() { |
| 120 return &window_tree_delegate_; |
| 121 } |
| 122 |
108 Window* GetFirstRoot() { | 123 Window* GetFirstRoot() { |
109 return window_tree_connection()->GetRoots().empty() | 124 return window_tree_connection()->GetRoots().empty() |
110 ? nullptr | 125 ? nullptr |
111 : *window_tree_connection()->GetRoots().begin(); | 126 : *window_tree_connection()->GetRoots().begin(); |
112 } | 127 } |
113 | 128 |
| 129 uint32_t GetEventObserverId() { |
| 130 return WindowTreeClientImplPrivate(&tree_client_).event_observer_id(); |
| 131 } |
| 132 |
114 private: | 133 private: |
115 TestWindowTree window_tree_; | 134 TestWindowTree window_tree_; |
116 TestWindowTreeDelegate window_tree_delegate_; | 135 TestWindowTreeDelegate window_tree_delegate_; |
117 WindowTreeClientImpl tree_client_; | 136 WindowTreeClientImpl tree_client_; |
118 | 137 |
119 DISALLOW_COPY_AND_ASSIGN(WindowTreeSetup); | 138 DISALLOW_COPY_AND_ASSIGN(WindowTreeSetup); |
120 }; | 139 }; |
121 | 140 |
122 class TestInputEventHandler : public InputEventHandler { | 141 class TestInputEventHandler : public InputEventHandler { |
123 public: | 142 public: |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 Window* root = setup.GetFirstRoot(); | 477 Window* root = setup.GetFirstRoot(); |
459 ASSERT_TRUE(root); | 478 ASSERT_TRUE(root); |
460 | 479 |
461 TestInputEventHandler event_handler; | 480 TestInputEventHandler event_handler; |
462 root->set_input_event_handler(&event_handler); | 481 root->set_input_event_handler(&event_handler); |
463 | 482 |
464 std::unique_ptr<ui::Event> ui_event( | 483 std::unique_ptr<ui::Event> ui_event( |
465 new ui::MouseEvent(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), | 484 new ui::MouseEvent(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(), |
466 ui::EventTimeForNow(), ui::EF_NONE, 0)); | 485 ui::EventTimeForNow(), ui::EF_NONE, 0)); |
467 setup.window_tree_client()->OnWindowInputEvent( | 486 setup.window_tree_client()->OnWindowInputEvent( |
468 1, server_id(root), mojom::Event::From(*ui_event.get()), false); | 487 1, server_id(root), mojom::Event::From(*ui_event.get()), 0); |
469 EXPECT_TRUE(event_handler.received_event()); | 488 EXPECT_TRUE(event_handler.received_event()); |
470 EXPECT_TRUE(setup.window_tree()->WasEventAcked(1)); | 489 EXPECT_TRUE(setup.window_tree()->WasEventAcked(1)); |
471 event_handler.Reset(); | 490 event_handler.Reset(); |
472 | 491 |
473 event_handler.set_should_manually_ack(); | 492 event_handler.set_should_manually_ack(); |
474 setup.window_tree_client()->OnWindowInputEvent( | 493 setup.window_tree_client()->OnWindowInputEvent( |
475 33, server_id(root), mojom::Event::From(*ui_event.get()), false); | 494 33, server_id(root), mojom::Event::From(*ui_event.get()), 0); |
476 EXPECT_TRUE(event_handler.received_event()); | 495 EXPECT_TRUE(event_handler.received_event()); |
477 EXPECT_FALSE(setup.window_tree()->WasEventAcked(33)); | 496 EXPECT_FALSE(setup.window_tree()->WasEventAcked(33)); |
478 | 497 |
479 event_handler.AckEvent(); | 498 event_handler.AckEvent(); |
480 EXPECT_TRUE(setup.window_tree()->WasEventAcked(33)); | 499 EXPECT_TRUE(setup.window_tree()->WasEventAcked(33)); |
481 } | 500 } |
482 | 501 |
| 502 // Tests event observers triggered by events that did not hit a target in this |
| 503 // window tree. |
| 504 TEST_F(WindowTreeClientImplTest, OnEventObserved) { |
| 505 WindowTreeSetup setup; |
| 506 Window* root = setup.GetFirstRoot(); |
| 507 ASSERT_TRUE(root); |
| 508 |
| 509 // Set up an event observer. |
| 510 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); |
| 511 matcher->type_matcher = mojom::EventTypeMatcher::New(); |
| 512 matcher->type_matcher->type = mojom::EventType::POINTER_DOWN; |
| 513 setup.window_tree_connection()->SetEventObserver(std::move(matcher)); |
| 514 |
| 515 // Simulate the server sending an observed event. |
| 516 uint32_t event_observer_id = setup.GetEventObserverId(); |
| 517 std::unique_ptr<ui::Event> ui_event( |
| 518 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 519 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); |
| 520 setup.window_tree_client()->OnEventObserved( |
| 521 mojom::Event::From(*ui_event.get()), event_observer_id); |
| 522 |
| 523 // Delegate sensed the event. |
| 524 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
| 525 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); |
| 526 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
| 527 setup.window_tree_delegate()->Reset(); |
| 528 |
| 529 // Clear the event observer. |
| 530 setup.window_tree_connection()->SetEventObserver(nullptr); |
| 531 |
| 532 // Simulate another event from the server. |
| 533 setup.window_tree_client()->OnEventObserved( |
| 534 mojom::Event::From(*ui_event.get()), event_observer_id); |
| 535 |
| 536 // No event was sensed. |
| 537 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); |
| 538 } |
| 539 |
| 540 // Tests event observers triggered by events that hit this window tree. |
| 541 TEST_F(WindowTreeClientImplTest, OnWindowInputEventWithEventObserver) { |
| 542 WindowTreeSetup setup; |
| 543 Window* root = setup.GetFirstRoot(); |
| 544 ASSERT_TRUE(root); |
| 545 |
| 546 // Set up an event observer. |
| 547 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); |
| 548 matcher->type_matcher = mojom::EventTypeMatcher::New(); |
| 549 matcher->type_matcher->type = mojom::EventType::POINTER_DOWN; |
| 550 setup.window_tree_connection()->SetEventObserver(std::move(matcher)); |
| 551 |
| 552 // Simulate the server dispatching an event that also matched the observer. |
| 553 uint32_t event_observer_id = setup.GetEventObserverId(); |
| 554 std::unique_ptr<ui::Event> ui_event( |
| 555 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 556 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); |
| 557 setup.window_tree_client()->OnWindowInputEvent( |
| 558 1, server_id(root), mojom::Event::From(*ui_event.get()), |
| 559 event_observer_id); |
| 560 |
| 561 // Delegate sensed the event. |
| 562 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
| 563 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); |
| 564 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
| 565 } |
| 566 |
| 567 // Tests that replacing an event observer with a new one results in only new |
| 568 // events being observed. |
| 569 TEST_F(WindowTreeClientImplTest, EventObserverReplaced) { |
| 570 WindowTreeSetup setup; |
| 571 Window* root = setup.GetFirstRoot(); |
| 572 ASSERT_TRUE(root); |
| 573 |
| 574 // Set up an event observer. |
| 575 mojom::EventMatcherPtr matcher1 = mojom::EventMatcher::New(); |
| 576 matcher1->type_matcher = mojom::EventTypeMatcher::New(); |
| 577 matcher1->type_matcher->type = mojom::EventType::POINTER_DOWN; |
| 578 setup.window_tree_connection()->SetEventObserver(std::move(matcher1)); |
| 579 uint32_t event_observer_id1 = setup.GetEventObserverId(); |
| 580 |
| 581 // Replace it with a second observer. |
| 582 mojom::EventMatcherPtr matcher2 = mojom::EventMatcher::New(); |
| 583 matcher2->type_matcher = mojom::EventTypeMatcher::New(); |
| 584 matcher2->type_matcher->type = mojom::EventType::POINTER_UP; |
| 585 setup.window_tree_connection()->SetEventObserver(std::move(matcher2)); |
| 586 uint32_t event_observer_id2 = setup.GetEventObserverId(); |
| 587 |
| 588 // Simulate the server sending an observed event that matched the old observer |
| 589 // (e.g. that was in-flight when the observer was replaced). |
| 590 std::unique_ptr<ui::Event> pressed_event( |
| 591 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| 592 ui::EventTimeForNow(), ui::EF_NONE, 0)); |
| 593 setup.window_tree_client()->OnEventObserved( |
| 594 mojom::Event::From(*pressed_event.get()), event_observer_id1); |
| 595 |
| 596 // The event was not sensed, because it does not match the current observer. |
| 597 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); |
| 598 |
| 599 // Simulate another event that matches the new observer. |
| 600 std::unique_ptr<ui::Event> released_event( |
| 601 new ui::MouseEvent(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), |
| 602 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); |
| 603 setup.window_tree_client()->OnEventObserved( |
| 604 mojom::Event::From(*released_event.get()), event_observer_id2); |
| 605 |
| 606 // The delegate sensed the event. |
| 607 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
| 608 EXPECT_EQ(ui::ET_MOUSE_RELEASED, last_event->type()); |
| 609 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
| 610 } |
| 611 |
483 // Verifies focus is reverted if the server replied that the change failed. | 612 // Verifies focus is reverted if the server replied that the change failed. |
484 TEST_F(WindowTreeClientImplTest, SetFocusFailed) { | 613 TEST_F(WindowTreeClientImplTest, SetFocusFailed) { |
485 WindowTreeSetup setup; | 614 WindowTreeSetup setup; |
486 Window* root = setup.GetFirstRoot(); | 615 Window* root = setup.GetFirstRoot(); |
487 ASSERT_TRUE(root); | 616 ASSERT_TRUE(root); |
488 root->SetVisible(true); | 617 root->SetVisible(true); |
489 Window* child = setup.window_tree_connection()->NewWindow(); | 618 Window* child = setup.window_tree_connection()->NewWindow(); |
490 child->SetVisible(true); | 619 child->SetVisible(true); |
491 root->AddChild(child); | 620 root->AddChild(child); |
492 Window* original_focus = setup.window_tree_connection()->GetFocusedWindow(); | 621 Window* original_focus = setup.window_tree_connection()->GetFocusedWindow(); |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 | 1050 |
922 setup.window_tree_client()->OnChangeCompleted(change_id2, false); | 1051 setup.window_tree_client()->OnChangeCompleted(change_id2, false); |
923 EXPECT_FALSE(child->HasCapture()); | 1052 EXPECT_FALSE(child->HasCapture()); |
924 EXPECT_TRUE(root->HasCapture()); | 1053 EXPECT_TRUE(root->HasCapture()); |
925 | 1054 |
926 setup.window_tree_client()->OnLostCapture(server_id(root)); | 1055 setup.window_tree_client()->OnLostCapture(server_id(root)); |
927 EXPECT_FALSE(root->HasCapture()); | 1056 EXPECT_FALSE(root->HasCapture()); |
928 } | 1057 } |
929 | 1058 |
930 } // namespace mus | 1059 } // namespace mus |
OLD | NEW |