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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 transport_value.resize(bytes.size()); | 45 transport_value.resize(bytes.size()); |
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::PointerEvent* last_event_observed() { return last_event_observed_.get(); } |
56 | 56 |
57 void Reset() { last_event_observed_.reset(); } | 57 void Reset() { last_event_observed_.reset(); } |
58 | 58 |
59 // WindowTreeClientDelegate: | 59 // WindowTreeClientDelegate: |
60 void OnEmbed(Window* root) override {} | 60 void OnEmbed(Window* root) override {} |
61 void OnDidDestroyClient(WindowTreeClient* client) override {} | 61 void OnDidDestroyClient(WindowTreeClient* client) override {} |
62 void OnEventObserved(const ui::Event& event, Window* target) override { | 62 void OnPointerWatcherEvent(const ui::PointerEvent& event, |
63 last_event_observed_ = ui::Event::Clone(event); | 63 Window* target) override { |
| 64 last_event_observed_.reset(new ui::PointerEvent(event)); |
64 } | 65 } |
65 | 66 |
66 private: | 67 private: |
67 std::unique_ptr<ui::Event> last_event_observed_; | 68 std::unique_ptr<ui::PointerEvent> last_event_observed_; |
68 | 69 |
69 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeClientDelegate); | 70 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeClientDelegate); |
70 }; | 71 }; |
71 | 72 |
72 class WindowTreeSetup { | 73 class WindowTreeSetup { |
73 public: | 74 public: |
74 WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) { | 75 WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) { |
75 WindowTreeClientPrivate(&tree_client_).OnEmbed(&window_tree_); | 76 WindowTreeClientPrivate(&tree_client_).OnEmbed(&window_tree_); |
76 window_tree_.GetAndClearChangeId(nullptr); | 77 window_tree_.GetAndClearChangeId(nullptr); |
77 } | 78 } |
(...skipping 10 matching lines...) Expand all Loading... |
88 | 89 |
89 TestWindowTreeClientDelegate* window_tree_delegate() { | 90 TestWindowTreeClientDelegate* window_tree_delegate() { |
90 return &window_tree_delegate_; | 91 return &window_tree_delegate_; |
91 } | 92 } |
92 | 93 |
93 Window* GetFirstRoot() { | 94 Window* GetFirstRoot() { |
94 return client()->GetRoots().empty() ? nullptr | 95 return client()->GetRoots().empty() ? nullptr |
95 : *client()->GetRoots().begin(); | 96 : *client()->GetRoots().begin(); |
96 } | 97 } |
97 | 98 |
98 uint32_t GetEventObserverId() { | 99 uint32_t GetPointerWatcherId() { |
99 return WindowTreeClientPrivate(&tree_client_).event_observer_id(); | 100 return WindowTreeClientPrivate(&tree_client_).pointer_watcher_id(); |
100 } | 101 } |
101 | 102 |
102 private: | 103 private: |
103 TestWindowTree window_tree_; | 104 TestWindowTree window_tree_; |
104 TestWindowTreeClientDelegate window_tree_delegate_; | 105 TestWindowTreeClientDelegate window_tree_delegate_; |
105 WindowTreeClient tree_client_; | 106 WindowTreeClient tree_client_; |
106 | 107 |
107 DISALLOW_COPY_AND_ASSIGN(WindowTreeSetup); | 108 DISALLOW_COPY_AND_ASSIGN(WindowTreeSetup); |
108 }; | 109 }; |
109 | 110 |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 event_handler.set_should_manually_ack(); | 460 event_handler.set_should_manually_ack(); |
460 setup.window_tree_client()->OnWindowInputEvent( | 461 setup.window_tree_client()->OnWindowInputEvent( |
461 33, server_id(root), ui::Event::Clone(*ui_event.get()), 0); | 462 33, server_id(root), ui::Event::Clone(*ui_event.get()), 0); |
462 EXPECT_TRUE(event_handler.received_event()); | 463 EXPECT_TRUE(event_handler.received_event()); |
463 EXPECT_FALSE(setup.window_tree()->WasEventAcked(33)); | 464 EXPECT_FALSE(setup.window_tree()->WasEventAcked(33)); |
464 | 465 |
465 event_handler.AckEvent(); | 466 event_handler.AckEvent(); |
466 EXPECT_TRUE(setup.window_tree()->WasEventAcked(33)); | 467 EXPECT_TRUE(setup.window_tree()->WasEventAcked(33)); |
467 } | 468 } |
468 | 469 |
469 // Tests event observers triggered by events that did not hit a target in this | 470 // Tests pointer watchers triggered by events that did not hit a target in this |
470 // window tree. | 471 // window tree. |
471 TEST_F(WindowTreeClientTest, OnEventObserved) { | 472 TEST_F(WindowTreeClientTest, OnPointerWatcherEvent) { |
472 WindowTreeSetup setup; | 473 WindowTreeSetup setup; |
473 Window* root = setup.GetFirstRoot(); | 474 Window* root = setup.GetFirstRoot(); |
474 ASSERT_TRUE(root); | 475 ASSERT_TRUE(root); |
475 | 476 |
476 // Set up an event observer. | 477 // Start a pointer watcher for all events excluding move events. |
477 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); | 478 setup.client()->StartPointerWatcher(false /* want_moves */); |
478 matcher->type_matcher = mojom::EventTypeMatcher::New(); | |
479 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | |
480 setup.client()->SetEventObserver(std::move(matcher)); | |
481 | 479 |
482 // Simulate the server sending an observed event. | 480 // Simulate the server sending an observed event. |
483 uint32_t event_observer_id = setup.GetEventObserverId(); | 481 uint32_t pointer_watcher_id = setup.GetPointerWatcherId(); |
484 std::unique_ptr<ui::Event> ui_event( | 482 std::unique_ptr<ui::PointerEvent> pointer_event_down(new ui::PointerEvent( |
485 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 483 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 1, |
486 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); | 484 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
487 setup.window_tree_client()->OnEventObserved(ui::Event::Clone(*ui_event.get()), | 485 base::TimeTicks())); |
488 event_observer_id); | 486 setup.window_tree_client()->OnPointerWatcherEvent( |
| 487 std::move(pointer_event_down), pointer_watcher_id); |
489 | 488 |
490 // Delegate sensed the event. | 489 // Delegate sensed the event. |
491 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); | 490 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
492 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); | 491 EXPECT_EQ(ui::ET_POINTER_DOWN, last_event->type()); |
493 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); | 492 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
494 setup.window_tree_delegate()->Reset(); | 493 setup.window_tree_delegate()->Reset(); |
495 | 494 |
496 // Clear the event observer. | 495 // Stop the pointer watcher. |
497 setup.client()->SetEventObserver(nullptr); | 496 setup.client()->StopPointerWatcher(); |
498 | 497 |
499 // Simulate another event from the server. | 498 // Simulate another event from the server. |
500 setup.window_tree_client()->OnEventObserved(ui::Event::Clone(*ui_event.get()), | 499 setup.window_tree_client()->OnPointerWatcherEvent( |
501 event_observer_id); | 500 std::move(pointer_event_down), pointer_watcher_id); |
502 | 501 |
503 // No event was sensed. | 502 // No event was sensed. |
504 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); | 503 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); |
505 } | 504 } |
506 | 505 |
507 // Tests event observers triggered by events that hit this window tree. | 506 // Tests pointer watchers triggered by events that hit this window tree. |
508 TEST_F(WindowTreeClientTest, OnWindowInputEventWithEventObserver) { | 507 TEST_F(WindowTreeClientTest, OnWindowInputEventWithPointerWatcher) { |
509 WindowTreeSetup setup; | 508 WindowTreeSetup setup; |
510 Window* root = setup.GetFirstRoot(); | 509 Window* root = setup.GetFirstRoot(); |
511 ASSERT_TRUE(root); | 510 ASSERT_TRUE(root); |
512 | 511 |
513 // Set up an event observer. | 512 // Start a pointer watcher for all events excluding move events. |
514 mojom::EventMatcherPtr matcher = mojom::EventMatcher::New(); | 513 setup.client()->StartPointerWatcher(false /* want_moves */); |
515 matcher->type_matcher = mojom::EventTypeMatcher::New(); | |
516 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | |
517 setup.client()->SetEventObserver(std::move(matcher)); | |
518 | 514 |
519 // Simulate the server dispatching an event that also matched the observer. | 515 // Simulate the server dispatching an event that also matched the observer. |
520 uint32_t event_observer_id = setup.GetEventObserverId(); | 516 uint32_t pointer_watcher_id = setup.GetPointerWatcherId(); |
521 std::unique_ptr<ui::Event> ui_event( | 517 std::unique_ptr<ui::PointerEvent> pointer_event_down(new ui::PointerEvent( |
522 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 518 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 1, |
523 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); | 519 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
| 520 base::TimeTicks())); |
524 setup.window_tree_client()->OnWindowInputEvent( | 521 setup.window_tree_client()->OnWindowInputEvent( |
525 1, server_id(root), std::move(ui_event), event_observer_id); | 522 1, server_id(root), std::move(pointer_event_down), pointer_watcher_id); |
526 | 523 |
527 // Delegate sensed the event. | 524 // Delegate sensed the event. |
528 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); | 525 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
529 EXPECT_EQ(ui::ET_MOUSE_PRESSED, last_event->type()); | 526 EXPECT_EQ(ui::ET_POINTER_DOWN, last_event->type()); |
530 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); | 527 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
531 } | 528 } |
532 | 529 |
533 // Tests that replacing an event observer with a new one results in only new | 530 // Tests that replacing a pointer watcher with a new one that has different |
534 // events being observed. | 531 // |want_moves| values results in only new events being observed. |
535 TEST_F(WindowTreeClientTest, EventObserverReplaced) { | 532 TEST_F(WindowTreeClientTest, PointerWatcherReplaced) { |
536 WindowTreeSetup setup; | 533 WindowTreeSetup setup; |
537 Window* root = setup.GetFirstRoot(); | 534 Window* root = setup.GetFirstRoot(); |
538 ASSERT_TRUE(root); | 535 ASSERT_TRUE(root); |
539 | 536 |
540 // Set up an event observer. | 537 // Start a pointer watcher for all events excluding move events. |
541 mojom::EventMatcherPtr matcher1 = mojom::EventMatcher::New(); | 538 setup.client()->StartPointerWatcher(false /* want_moves */); |
542 matcher1->type_matcher = mojom::EventTypeMatcher::New(); | 539 uint32_t pointer_watcher_id1 = setup.GetPointerWatcherId(); |
543 matcher1->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | |
544 setup.client()->SetEventObserver(std::move(matcher1)); | |
545 uint32_t event_observer_id1 = setup.GetEventObserverId(); | |
546 | 540 |
547 // Replace it with a second observer. | 541 // Replace it with a second watcher that also watches for move events. |
548 mojom::EventMatcherPtr matcher2 = mojom::EventMatcher::New(); | 542 setup.client()->StartPointerWatcher(true /* want_moves */); |
549 matcher2->type_matcher = mojom::EventTypeMatcher::New(); | 543 uint32_t pointer_watcher_id2 = setup.GetPointerWatcherId(); |
550 matcher2->type_matcher->type = ui::mojom::EventType::POINTER_UP; | |
551 setup.client()->SetEventObserver(std::move(matcher2)); | |
552 uint32_t event_observer_id2 = setup.GetEventObserverId(); | |
553 | 544 |
554 // Simulate the server sending an observed event that matched the old observer | 545 // Simulate the server sending an observed event that matched the old watcher |
555 // (e.g. that was in-flight when the observer was replaced). | 546 // (e.g. that was in-flight when the observer was replaced). |
556 std::unique_ptr<ui::Event> pressed_event( | 547 std::unique_ptr<ui::PointerEvent> pointer_event_down(new ui::PointerEvent( |
557 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 548 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 1, |
558 ui::EventTimeForNow(), ui::EF_NONE, 0)); | 549 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
559 setup.window_tree_client()->OnEventObserved(std::move(pressed_event), | 550 base::TimeTicks())); |
560 event_observer_id1); | 551 setup.window_tree_client()->OnPointerWatcherEvent( |
| 552 std::move(pointer_event_down), pointer_watcher_id1); |
561 | 553 |
562 // The event was not sensed, because it does not match the current observer. | 554 // The event was not sensed, because it does not match the current watcher. |
563 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); | 555 EXPECT_FALSE(setup.window_tree_delegate()->last_event_observed()); |
564 | 556 |
565 // Simulate another event that matches the new observer. | 557 // Simulate another event that matches the new watcher. |
566 std::unique_ptr<ui::Event> released_event( | 558 std::unique_ptr<ui::PointerEvent> pointer_event_move(new ui::PointerEvent( |
567 new ui::MouseEvent(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), | 559 ui::ET_POINTER_MOVED, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 1, |
568 ui::EventTimeForNow(), ui::EF_CONTROL_DOWN, 0)); | 560 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH), |
569 setup.window_tree_client()->OnEventObserved(std::move(released_event), | 561 base::TimeTicks())); |
570 event_observer_id2); | 562 setup.window_tree_client()->OnPointerWatcherEvent( |
| 563 std::move(pointer_event_move), pointer_watcher_id2); |
571 | 564 |
572 // The delegate sensed the event. | 565 // The delegate sensed the event. |
573 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); | 566 ui::Event* last_event = setup.window_tree_delegate()->last_event_observed(); |
574 EXPECT_EQ(ui::ET_MOUSE_RELEASED, last_event->type()); | 567 EXPECT_EQ(ui::ET_POINTER_MOVED, last_event->type()); |
575 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); | 568 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); |
576 } | 569 } |
577 | 570 |
578 // Verifies focus is reverted if the server replied that the change failed. | 571 // Verifies focus is reverted if the server replied that the change failed. |
579 TEST_F(WindowTreeClientTest, SetFocusFailed) { | 572 TEST_F(WindowTreeClientTest, SetFocusFailed) { |
580 WindowTreeSetup setup; | 573 WindowTreeSetup setup; |
581 Window* root = setup.GetFirstRoot(); | 574 Window* root = setup.GetFirstRoot(); |
582 ASSERT_TRUE(root); | 575 ASSERT_TRUE(root); |
583 root->SetVisible(true); | 576 root->SetVisible(true); |
584 Window* child = setup.client()->NewWindow(); | 577 Window* child = setup.client()->NewWindow(); |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 | 1035 |
1043 // Create a new Window, and attempt to place capture on that. | 1036 // Create a new Window, and attempt to place capture on that. |
1044 Window* child = setup.client()->NewWindow(); | 1037 Window* child = setup.client()->NewWindow(); |
1045 child->SetVisible(true); | 1038 child->SetVisible(true); |
1046 root->AddChild(child); | 1039 root->AddChild(child); |
1047 child->SetCapture(); | 1040 child->SetCapture(); |
1048 EXPECT_TRUE(child->HasCapture()); | 1041 EXPECT_TRUE(child->HasCapture()); |
1049 } | 1042 } |
1050 | 1043 |
1051 } // namespace ui | 1044 } // namespace ui |
OLD | NEW |