| 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 "services/ui/public/cpp/window_tree_client.h" | 5 #include "services/ui/public/cpp/window_tree_client.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 memcpy(&transport_value.front(), &(bytes.front()), bytes.size()); | 46 memcpy(&transport_value.front(), &(bytes.front()), bytes.size()); |
| 47 return transport_value; | 47 return transport_value; |
| 48 } | 48 } |
| 49 | 49 |
| 50 class TestWindowTreeClientDelegate : public WindowTreeClientDelegate { | 50 class TestWindowTreeClientDelegate : public WindowTreeClientDelegate { |
| 51 public: | 51 public: |
| 52 TestWindowTreeClientDelegate() {} | 52 TestWindowTreeClientDelegate() {} |
| 53 ~TestWindowTreeClientDelegate() override {} | 53 ~TestWindowTreeClientDelegate() override {} |
| 54 | 54 |
| 55 ui::Event* last_event_observed() { return last_event_observed_.get(); } | 55 ui::Event* last_event_observed() { return last_event_observed_.get(); } |
| 56 Window* last_target() { return last_target_; } |
| 56 | 57 |
| 57 void Reset() { last_event_observed_.reset(); } | 58 void Reset() { |
| 59 last_event_observed_.reset(); |
| 60 last_target_ = nullptr; |
| 61 } |
| 58 | 62 |
| 59 // WindowTreeClientDelegate: | 63 // WindowTreeClientDelegate: |
| 60 void OnEmbed(Window* root) override {} | 64 void OnEmbed(Window* root) override {} |
| 61 void OnDidDestroyClient(WindowTreeClient* client) override {} | 65 void OnDidDestroyClient(WindowTreeClient* client) override {} |
| 62 void OnEventObserved(const ui::Event& event, Window* target) override { | 66 void OnEventObserved(const ui::Event& event, Window* target) override { |
| 63 last_event_observed_ = ui::Event::Clone(event); | 67 last_event_observed_ = ui::Event::Clone(event); |
| 68 last_target_ = target; |
| 64 } | 69 } |
| 65 | 70 |
| 66 private: | 71 private: |
| 67 std::unique_ptr<ui::Event> last_event_observed_; | 72 std::unique_ptr<ui::Event> last_event_observed_; |
| 73 Window* last_target_ = nullptr; |
| 68 | 74 |
| 69 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeClientDelegate); | 75 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeClientDelegate); |
| 70 }; | 76 }; |
| 71 | 77 |
| 72 class WindowTreeSetup { | 78 class WindowTreeSetup { |
| 73 public: | 79 public: |
| 74 WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) { | 80 WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) { |
| 75 WindowTreeClientPrivate(&tree_client_).OnEmbed(&window_tree_); | 81 WindowTreeClientPrivate(&tree_client_).OnEmbed(&window_tree_); |
| 76 window_tree_.GetAndClearChangeId(nullptr); | 82 window_tree_.GetAndClearChangeId(nullptr); |
| 77 } | 83 } |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 ASSERT_TRUE(root); | 480 ASSERT_TRUE(root); |
| 475 | 481 |
| 476 // Set up an event observer. | 482 // Set up an event observer. |
| 477 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); | 483 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); |
| 478 matcher->type_matcher = mojom::EventTypeMatcher::New(); | 484 matcher->type_matcher = mojom::EventTypeMatcher::New(); |
| 479 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | 485 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; |
| 480 setup.client()->SetEventObserver(std::move(matcher)); | 486 setup.client()->SetEventObserver(std::move(matcher)); |
| 481 | 487 |
| 482 // Simulate the server sending an observed event. | 488 // Simulate the server sending an observed event. |
| 483 uint32_t event_observer_id = setup.GetEventObserverId(); | 489 uint32_t event_observer_id = setup.GetEventObserverId(); |
| 484 std::unique_ptr<ui::Event> ui_event( | 490 ui::PointerEvent ui_event( |
| 485 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 491 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 0, |
| 486 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); | 492 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 487 setup.window_tree_client()->OnEventObserved(ui::Event::Clone(*ui_event.get()), | 493 ui::EventTimeForNow()); |
| 494 setup.window_tree_client()->OnEventObserved(ui::Event::Clone(ui_event), |
| 488 event_observer_id); | 495 event_observer_id); |
| 489 | 496 |
| 490 // Delegate sensed the event. | 497 // Delegate sensed the event and it has been converted to a mouse down. |
| 491 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); | 498 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
| 492 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); | 499 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); |
| 493 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); | 500 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
| 501 // There was no target window. |
| 502 EXPECT_FALSE(setup.window_tree_delegate()->last_target()); |
| 494 setup.window_tree_delegate()->Reset(); | 503 setup.window_tree_delegate()->Reset(); |
| 495 | 504 |
| 496 // Clear the event observer. | 505 // Clear the event observer. |
| 497 setup.client()->SetEventObserver(nullptr); | 506 setup.client()->SetEventObserver(nullptr); |
| 498 | 507 |
| 499 // Simulate another event from the server. | 508 // Simulate another event from the server. |
| 500 setup.window_tree_client()->OnEventObserved(ui::Event::Clone(*ui_event.get()), | 509 setup.window_tree_client()->OnEventObserved(ui::Event::Clone(ui_event), |
| 501 event_observer_id); | 510 event_observer_id); |
| 502 | 511 |
| 503 // No event was sensed. | 512 // No event was sensed. |
| 504 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); | 513 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); |
| 505 } | 514 } |
| 506 | 515 |
| 507 // Tests event observers triggered by events that hit this window tree. | 516 // Tests event observers triggered by events that hit this window tree. |
| 508 TEST_F(WindowTreeClientTest, OnWindowInputEventWithEventObserver) { | 517 TEST_F(WindowTreeClientTest, OnWindowInputEventWithEventObserver) { |
| 509 WindowTreeSetup setup; | 518 WindowTreeSetup setup; |
| 510 Window* root = setup.GetFirstRoot(); | 519 Window* root = setup.GetFirstRoot(); |
| 511 ASSERT_TRUE(root); | 520 ASSERT_TRUE(root); |
| 512 | 521 |
| 513 // Set up an event observer. | 522 // Set up an event observer. |
| 514 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); | 523 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); |
| 515 matcher->type_matcher = mojom::EventTypeMatcher::New(); | 524 matcher->type_matcher = mojom::EventTypeMatcher::New(); |
| 516 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | 525 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; |
| 517 setup.client()->SetEventObserver(std::move(matcher)); | 526 setup.client()->SetEventObserver(std::move(matcher)); |
| 518 | 527 |
| 519 // Simulate the server dispatching an event that also matched the observer. | 528 // Simulate the server dispatching an event that also matched the observer. |
| 520 uint32_t event_observer_id = setup.GetEventObserverId(); | 529 uint32_t event_observer_id = setup.GetEventObserverId(); |
| 521 std::unique_ptr<ui::Event> ui_event( | 530 std::unique_ptr<ui::Event> ui_event(new ui::PointerEvent( |
| 522 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 531 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 0, |
| 523 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); | 532 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 533 ui::EventTimeForNow())); |
| 524 setup.window_tree_client()->OnWindowInputEvent( | 534 setup.window_tree_client()->OnWindowInputEvent( |
| 525 1, server_id(root), std::move(ui_event), event_observer_id); | 535 1, server_id(root), std::move(ui_event), event_observer_id); |
| 526 | 536 |
| 527 // Delegate sensed the event. | 537 // Delegate sensed the event. |
| 528 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); | 538 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
| 529 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); | 539 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); |
| 530 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); | 540 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
| 541 // Delegate sensed the target window. |
| 542 EXPECT_EQ(root, setup.window_tree_delegate()->last_target()); |
| 531 } | 543 } |
| 532 | 544 |
| 533 // Tests that replacing an event observer with a new one results in only new | 545 // Tests that replacing an event observer with a new one results in only new |
| 534 // events being observed. | 546 // events being observed. |
| 535 TEST_F(WindowTreeClientTest, EventObserverReplaced) { | 547 TEST_F(WindowTreeClientTest, EventObserverReplaced) { |
| 536 WindowTreeSetup setup; | 548 WindowTreeSetup setup; |
| 537 Window* root = setup.GetFirstRoot(); | 549 Window* root = setup.GetFirstRoot(); |
| 538 ASSERT_TRUE(root); | 550 ASSERT_TRUE(root); |
| 539 | 551 |
| 540 // Set up an event observer. | 552 // Set up an event observer. |
| 541 mojom::EventMatcherPtr matcher1 = mojom::EventMatcher::New(); | 553 mojom::EventMatcherPtr matcher1 = mojom::EventMatcher::New(); |
| 542 matcher1->type_matcher = mojom::EventTypeMatcher::New(); | 554 matcher1->type_matcher = mojom::EventTypeMatcher::New(); |
| 543 matcher1->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | 555 matcher1->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; |
| 544 setup.client()->SetEventObserver(std::move(matcher1)); | 556 setup.client()->SetEventObserver(std::move(matcher1)); |
| 545 uint32_t event_observer_id1 = setup.GetEventObserverId(); | 557 uint32_t event_observer_id1 = setup.GetEventObserverId(); |
| 546 | 558 |
| 547 // Replace it with a second observer. | 559 // Replace it with a second observer. |
| 548 mojom::EventMatcherPtr matcher2 = mojom::EventMatcher::New(); | 560 mojom::EventMatcherPtr matcher2 = mojom::EventMatcher::New(); |
| 549 matcher2->type_matcher = mojom::EventTypeMatcher::New(); | 561 matcher2->type_matcher = mojom::EventTypeMatcher::New(); |
| 550 matcher2->type_matcher->type = ui::mojom::EventType::POINTER_UP; | 562 matcher2->type_matcher->type = ui::mojom::EventType::POINTER_UP; |
| 551 setup.client()->SetEventObserver(std::move(matcher2)); | 563 setup.client()->SetEventObserver(std::move(matcher2)); |
| 552 uint32_t event_observer_id2 = setup.GetEventObserverId(); | 564 uint32_t event_observer_id2 = setup.GetEventObserverId(); |
| 553 | 565 |
| 554 // Simulate the server sending an observed event that matched the old observer | 566 // Simulate the server sending an observed event that matched the old observer |
| 555 // (e.g. that was in-flight when the observer was replaced). | 567 // (e.g. that was in-flight when the observer was replaced). |
| 556 std::unique_ptr<ui::Event> pressed_event( | 568 std::unique_ptr<ui::Event> pressed_event(new ui::PointerEvent( |
| 557 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 569 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_NONE, 0, |
| 558 ui::EventTimeForNow(), ui::EF_NONE, 0)); | 570 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 571 ui::EventTimeForNow())); |
| 559 setup.window_tree_client()->OnEventObserved(std::move(pressed_event), | 572 setup.window_tree_client()->OnEventObserved(std::move(pressed_event), |
| 560 event_observer_id1); | 573 event_observer_id1); |
| 561 | 574 |
| 562 // The event was not sensed, because it does not match the current observer. | 575 // The event was not sensed, because it does not match the current observer. |
| 563 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); | 576 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); |
| 564 | 577 |
| 565 // Simulate another event that matches the new observer. | 578 // Simulate another event that matches the new observer. |
| 566 std::unique_ptr<ui::Event> released_event( | 579 std::unique_ptr<ui::Event> released_event(new ui::PointerEvent( |
| 567 new ui::MouseEvent(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), | 580 ui::ET_POINTER_UP, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 0, |
| 568 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); | 581 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE), |
| 582 ui::EventTimeForNow())); |
| 569 setup.window_tree_client()->OnEventObserved(std::move(released_event), | 583 setup.window_tree_client()->OnEventObserved(std::move(released_event), |
| 570 event_observer_id2); | 584 event_observer_id2); |
| 571 | 585 |
| 572 // The delegate sensed the event. | 586 // The delegate sensed the event. |
| 573 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); | 587 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
| 574 EXPECT_EQ(ui::ET_MOUSE_RELEASED, last_event->type()); | 588 EXPECT_EQ(ui::ET_MOUSE_RELEASED, last_event->type()); |
| 575 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); | 589 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
| 576 } | 590 } |
| 577 | 591 |
| 578 // Verifies focus is reverted if the server replied that the change failed. | 592 // Verifies focus is reverted if the server replied that the change failed. |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 | 1056 |
| 1043 // Create a new Window, and attempt to place capture on that. | 1057 // Create a new Window, and attempt to place capture on that. |
| 1044 Window* child = setup.client()->NewWindow(); | 1058 Window* child = setup.client()->NewWindow(); |
| 1045 child->SetVisible(true); | 1059 child->SetVisible(true); |
| 1046 root->AddChild(child); | 1060 root->AddChild(child); |
| 1047 child->SetCapture(); | 1061 child->SetCapture(); |
| 1048 EXPECT_TRUE(child->HasCapture()); | 1062 EXPECT_TRUE(child->HasCapture()); |
| 1049 } | 1063 } |
| 1050 | 1064 |
| 1051 } // namespace ui | 1065 } // namespace ui |
| OLD | NEW |