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

Side by Side Diff: ui/aura/mus/window_tree_client_unittest.cc

Issue 2779093004: Update event states in Env when target window has been destroyed. (Closed)
Patch Set: test Created 3 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 "ui/aura/mus/window_tree_client.h" 5 #include "ui/aura/mus/window_tree_client.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 581
582 namespace { 582 namespace {
583 583
584 class InputEventBasicTestWindowDelegate : public test::TestWindowDelegate { 584 class InputEventBasicTestWindowDelegate : public test::TestWindowDelegate {
585 public: 585 public:
586 explicit InputEventBasicTestWindowDelegate(TestWindowTree* test_window_tree) 586 explicit InputEventBasicTestWindowDelegate(TestWindowTree* test_window_tree)
587 : test_window_tree_(test_window_tree) {} 587 : test_window_tree_(test_window_tree) {}
588 ~InputEventBasicTestWindowDelegate() override {} 588 ~InputEventBasicTestWindowDelegate() override {}
589 589
590 bool got_move() const { return got_move_; } 590 bool got_move() const { return got_move_; }
591 bool got_press() const { return got_press_; }
592 bool got_release() const { return got_release_; }
591 bool was_acked() const { return was_acked_; } 593 bool was_acked() const { return was_acked_; }
592 const gfx::Point& last_event_location() const { return last_event_location_; } 594 const gfx::Point& last_event_location() const { return last_event_location_; }
593 void set_event_id(uint32_t event_id) { event_id_ = event_id; } 595 void set_event_id(uint32_t event_id) { event_id_ = event_id; }
594 596
595 // TestWindowDelegate:: 597 // TestWindowDelegate::
596 void OnMouseEvent(ui::MouseEvent* event) override { 598 void OnMouseEvent(ui::MouseEvent* event) override {
597 was_acked_ = test_window_tree_->WasEventAcked(event_id_); 599 was_acked_ = test_window_tree_->WasEventAcked(event_id_);
598 if (event->type() == ui::ET_MOUSE_MOVED) 600 if (event->type() == ui::ET_MOUSE_MOVED)
599 got_move_ = true; 601 got_move_ = true;
602 else if (event->type() == ui::ET_MOUSE_PRESSED)
603 got_press_ = true;
604 else if (event->type() == ui::ET_MOUSE_RELEASED)
605 got_release_ = true;
600 last_event_location_ = event->location(); 606 last_event_location_ = event->location();
601 event->SetHandled(); 607 event->SetHandled();
602 } 608 }
609
610 void OnTouchEvent(ui::TouchEvent* event) override {
611 was_acked_ = test_window_tree_->WasEventAcked(event_id_);
612 if (event->type() == ui::ET_TOUCH_PRESSED)
613 got_press_ = true;
614 else if (event->type() == ui::ET_TOUCH_RELEASED)
615 got_release_ = true;
616 last_event_location_ = event->location();
617 event->SetHandled();
618 }
603 619
604 void reset() { 620 void reset() {
605 was_acked_ = false; 621 was_acked_ = false;
606 got_move_ = false; 622 got_move_ = false;
623 got_press_ = false;
624 got_release_ = false;
607 last_event_location_ = gfx::Point(); 625 last_event_location_ = gfx::Point();
608 event_id_ = 0; 626 event_id_ = 0;
609 } 627 }
610 628
611 private: 629 private:
612 TestWindowTree* test_window_tree_; 630 TestWindowTree* test_window_tree_;
613 bool was_acked_ = false; 631 bool was_acked_ = false;
614 bool got_move_ = false; 632 bool got_move_ = false;
633 bool got_press_ = false;
634 bool got_release_ = false;
615 gfx::Point last_event_location_; 635 gfx::Point last_event_location_;
616 uint32_t event_id_ = 0; 636 uint32_t event_id_ = 0;
617 637
618 DISALLOW_COPY_AND_ASSIGN(InputEventBasicTestWindowDelegate); 638 DISALLOW_COPY_AND_ASSIGN(InputEventBasicTestWindowDelegate);
619 }; 639 };
620 640
621 class InputEventBasicTestEventHandler : public ui::test::TestEventHandler { 641 class InputEventBasicTestEventHandler : public ui::test::TestEventHandler {
622 public: 642 public:
623 InputEventBasicTestEventHandler() {} 643 explicit InputEventBasicTestEventHandler(Window* target_window)
644 : target_window_(target_window) {}
624 ~InputEventBasicTestEventHandler() override {} 645 ~InputEventBasicTestEventHandler() override {}
625 646
626 bool got_move() const { return got_move_; } 647 bool got_move() const { return got_move_; }
627 const gfx::Point& last_event_location() const { return last_event_location_; } 648 const gfx::Point& last_event_location() const { return last_event_location_; }
628 void set_event_id(uint32_t event_id) { event_id_ = event_id; } 649 void set_event_id(uint32_t event_id) { event_id_ = event_id; }
629 650
630 // ui::test::TestEventHandler overrides. 651 // ui::test::TestEventHandler overrides.
631 void OnMouseEvent(ui::MouseEvent* event) override { 652 void OnMouseEvent(ui::MouseEvent* event) override {
632 if (event->type() == ui::ET_MOUSE_MOVED) 653 if (event->target() == target_window_) {
633 got_move_ = true; 654 if (event->type() == ui::ET_MOUSE_MOVED)
634 last_event_location_ = event->location(); 655 got_move_ = true;
635 event->SetHandled(); 656 last_event_location_ = event->location();
657 event->SetHandled();
658 }
636 } 659 }
637 660
638 void reset() { 661 void reset() {
639 got_move_ = false; 662 got_move_ = false;
640 last_event_location_ = gfx::Point(); 663 last_event_location_ = gfx::Point();
641 event_id_ = 0; 664 event_id_ = 0;
642 } 665 }
643 666
644 private: 667 private:
668 Window* target_window_ = nullptr;
645 bool got_move_ = false; 669 bool got_move_ = false;
646 gfx::Point last_event_location_; 670 gfx::Point last_event_location_;
647 uint32_t event_id_ = 0; 671 uint32_t event_id_ = 0;
648 672
649 DISALLOW_COPY_AND_ASSIGN(InputEventBasicTestEventHandler); 673 DISALLOW_COPY_AND_ASSIGN(InputEventBasicTestEventHandler);
650 }; 674 };
651 675
652 } // namespace 676 } // namespace
653 677
654 TEST_F(WindowTreeClientClientTest, InputEventBasic) { 678 TEST_F(WindowTreeClientClientTest, InputEventBasic) {
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 EXPECT_EQ(gfx::Point(30, 30), window_delegate2->last_event_location()); 914 EXPECT_EQ(gfx::Point(30, 30), window_delegate2->last_event_location());
891 child2.reset(); 915 child2.reset();
892 child1.reset(); 916 child1.reset();
893 window_tree_host.reset(); 917 window_tree_host.reset();
894 capture_client.reset(); 918 capture_client.reset();
895 } 919 }
896 920
897 TEST_F(WindowTreeClientClientTest, InputEventRootWindow) { 921 TEST_F(WindowTreeClientClientTest, InputEventRootWindow) {
898 WindowTreeHostMus window_tree_host(window_tree_client_impl()); 922 WindowTreeHostMus window_tree_host(window_tree_client_impl());
899 Window* top_level = window_tree_host.window(); 923 Window* top_level = window_tree_host.window();
900 InputEventBasicTestEventHandler root_handler; 924 InputEventBasicTestEventHandler root_handler(top_level);
901 top_level->AddPreTargetHandler(&root_handler); 925 top_level->AddPreTargetHandler(&root_handler);
902 const gfx::Rect bounds(0, 0, 100, 100); 926 const gfx::Rect bounds(0, 0, 100, 100);
903 window_tree_host.SetBoundsInPixels(bounds); 927 window_tree_host.SetBoundsInPixels(bounds);
904 window_tree_host.InitHost(); 928 window_tree_host.InitHost();
905 window_tree_host.Show(); 929 window_tree_host.Show();
906 EXPECT_EQ(bounds, top_level->bounds()); 930 EXPECT_EQ(bounds, top_level->bounds());
907 EXPECT_EQ(bounds, window_tree_host.GetBoundsInPixels()); 931 EXPECT_EQ(bounds, window_tree_host.GetBoundsInPixels());
908 InputEventBasicTestWindowDelegate child_delegate(window_tree()); 932 InputEventBasicTestWindowDelegate child_delegate(window_tree());
909 Window child(&child_delegate); 933 Window child(&child_delegate);
910 child.Init(ui::LAYER_NOT_DRAWN); 934 child.Init(ui::LAYER_NOT_DRAWN);
(...skipping 17 matching lines...) Expand all
928 952
929 EXPECT_TRUE(window_tree()->WasEventAcked(event_id)); 953 EXPECT_TRUE(window_tree()->WasEventAcked(event_id));
930 EXPECT_EQ(ui::mojom::EventResult::HANDLED, 954 EXPECT_EQ(ui::mojom::EventResult::HANDLED,
931 window_tree()->GetEventResult(event_id)); 955 window_tree()->GetEventResult(event_id));
932 EXPECT_TRUE(root_handler.got_move()); 956 EXPECT_TRUE(root_handler.got_move());
933 EXPECT_EQ(gfx::Point(20, 30), root_handler.last_event_location()); 957 EXPECT_EQ(gfx::Point(20, 30), root_handler.last_event_location());
934 EXPECT_FALSE(child_delegate.got_move()); 958 EXPECT_FALSE(child_delegate.got_move());
935 EXPECT_EQ(gfx::Point(), child_delegate.last_event_location()); 959 EXPECT_EQ(gfx::Point(), child_delegate.last_event_location());
936 } 960 }
937 961
962 TEST_F(WindowTreeClientClientTest, InputMouseEventNoWindow) {
963 Env* env = Env::GetInstance();
964 InputEventBasicTestWindowDelegate window_delegate(window_tree());
965 WindowTreeHostMus window_tree_host(window_tree_client_impl(),
966 cc::FrameSinkId(1, 1));
967 Window* top_level = window_tree_host.window();
968 const gfx::Rect bounds(0, 0, 100, 100);
969 window_tree_host.SetBoundsInPixels(bounds);
970 window_tree_host.InitHost();
971 window_tree_host.Show();
972 EXPECT_EQ(bounds, top_level->bounds());
973 EXPECT_EQ(bounds, window_tree_host.GetBoundsInPixels());
974 Window child(&window_delegate);
975 child.Init(ui::LAYER_NOT_DRAWN);
976 top_level->AddChild(&child);
977 child.SetBounds(gfx::Rect(10, 10, 100, 100));
978 child.Show();
979
980 EXPECT_FALSE(window_delegate.got_press());
981 EXPECT_FALSE(env->IsMouseButtonDown());
982 EXPECT_FALSE(env->mouse_button_flags());
983 EXPECT_EQ(gfx::Point(), env->last_mouse_location());
984
985 const gfx::Point event_location(2, 3);
986 uint32_t event_id = 1;
987 window_delegate.set_event_id(event_id);
988 std::unique_ptr<ui::Event> ui_event(
989 new ui::MouseEvent(ui::ET_MOUSE_PRESSED, event_location, gfx::Point(),
990 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0));
991 window_tree_client()->OnWindowInputEvent(
992 event_id, server_id(&child), window_tree_host.display_id(),
993 ui::Event::Clone(*ui_event.get()), 0);
994 EXPECT_TRUE(window_tree()->WasEventAcked(event_id));
995 EXPECT_EQ(ui::mojom::EventResult::HANDLED,
996 window_tree()->GetEventResult(event_id));
997 EXPECT_TRUE(window_delegate.got_press());
998 EXPECT_TRUE(env->IsMouseButtonDown());
999 EXPECT_EQ(1024, env->mouse_button_flags()); // ui::EF_LEFT_MOUSE_BUTTON
1000 EXPECT_EQ(event_location, env->last_mouse_location());
1001 window_delegate.reset();
1002
1003 const gfx::Point event_location1(4, 5);
1004 event_id = 2;
1005 window_delegate.set_event_id(event_id);
1006 std::unique_ptr<ui::Event> ui_event1(
1007 new ui::MouseEvent(ui::ET_MOUSE_RELEASED, event_location1, gfx::Point(),
1008 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1009 ui::EF_LEFT_MOUSE_BUTTON));
1010 window_tree_client()->OnWindowInputEvent(
1011 event_id, kInvalidServerId, window_tree_host.display_id(),
1012 ui::Event::Clone(*ui_event1.get()), 0);
1013 EXPECT_TRUE(window_tree()->WasEventAcked(event_id));
1014 EXPECT_EQ(ui::mojom::EventResult::HANDLED,
1015 window_tree()->GetEventResult(event_id));
1016 // WindowTreeClient::OnWindowInputEvent cannot find a target window with
1017 // kInvalidServerId but should use the event to update event states kept in
1018 // aura::Env, location shouldn't be updated.
1019 EXPECT_FALSE(window_delegate.got_release());
1020 EXPECT_FALSE(env->IsMouseButtonDown());
1021 EXPECT_FALSE(env->mouse_button_flags());
1022 EXPECT_EQ(event_location, env->last_mouse_location());
1023 }
1024
1025 TEST_F(WindowTreeClientClientTest, InputTouchEventNoWindow) {
1026 Env* env = Env::GetInstance();
1027 InputEventBasicTestWindowDelegate window_delegate(window_tree());
1028 WindowTreeHostMus window_tree_host(window_tree_client_impl(),
1029 cc::FrameSinkId(1, 1));
1030 Window* top_level = window_tree_host.window();
1031 const gfx::Rect bounds(0, 0, 100, 100);
1032 window_tree_host.SetBoundsInPixels(bounds);
1033 window_tree_host.InitHost();
1034 window_tree_host.Show();
1035 EXPECT_EQ(bounds, top_level->bounds());
1036 EXPECT_EQ(bounds, window_tree_host.GetBoundsInPixels());
1037 Window child(&window_delegate);
1038 child.Init(ui::LAYER_NOT_DRAWN);
1039 top_level->AddChild(&child);
1040 child.SetBounds(gfx::Rect(10, 10, 100, 100));
1041 child.Show();
1042
1043 EXPECT_FALSE(window_delegate.got_press());
1044 EXPECT_FALSE(env->is_touch_down());
1045
1046 const gfx::Point event_location(2, 3);
1047 uint32_t event_id = 1;
1048 window_delegate.set_event_id(event_id);
1049 std::unique_ptr<ui::Event> ui_event(new ui::TouchEvent(
1050 ui::ET_TOUCH_PRESSED, event_location, ui::EventTimeForNow(),
1051 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0)));
1052 window_tree_client()->OnWindowInputEvent(
1053 event_id, server_id(&child), window_tree_host.display_id(),
1054 ui::Event::Clone(*ui_event.get()), 0);
1055 EXPECT_TRUE(window_tree()->WasEventAcked(event_id));
1056 EXPECT_EQ(ui::mojom::EventResult::HANDLED,
1057 window_tree()->GetEventResult(event_id));
1058 EXPECT_TRUE(window_delegate.got_press());
1059 EXPECT_TRUE(env->is_touch_down());
1060 window_delegate.reset();
1061
1062 event_id = 2;
1063 window_delegate.set_event_id(event_id);
1064 std::unique_ptr<ui::Event> ui_event1(new ui::TouchEvent(
1065 ui::ET_TOUCH_RELEASED, event_location, ui::EventTimeForNow(),
1066 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0)));
1067 window_tree_client()->OnWindowInputEvent(
1068 event_id, kInvalidServerId, window_tree_host.display_id(),
1069 ui::Event::Clone(*ui_event1.get()), 0);
1070 EXPECT_TRUE(window_tree()->WasEventAcked(event_id));
1071 EXPECT_EQ(ui::mojom::EventResult::HANDLED,
1072 window_tree()->GetEventResult(event_id));
1073 // WindowTreeClient::OnWindowInputEvent cannot find a target window with
1074 // kInvalidServerId but should use the event to update event states kept in
1075 // aura::Env.
1076 EXPECT_FALSE(window_delegate.got_release());
1077 EXPECT_FALSE(env->is_touch_down());
1078 }
1079
938 class WindowTreeClientPointerObserverTest : public WindowTreeClientClientTest { 1080 class WindowTreeClientPointerObserverTest : public WindowTreeClientClientTest {
939 public: 1081 public:
940 WindowTreeClientPointerObserverTest() {} 1082 WindowTreeClientPointerObserverTest() {}
941 ~WindowTreeClientPointerObserverTest() override {} 1083 ~WindowTreeClientPointerObserverTest() override {}
942 1084
943 void DeleteLastEventObserved() { last_event_observed_.reset(); } 1085 void DeleteLastEventObserved() { last_event_observed_.reset(); }
944 const ui::PointerEvent* last_event_observed() const { 1086 const ui::PointerEvent* last_event_observed() const {
945 return last_event_observed_.get(); 1087 return last_event_observed_.get();
946 } 1088 }
947 1089
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
995 window_tree_client()->OnPointerEventObserved(std::move(pointer_event_up), 0u, 1137 window_tree_client()->OnPointerEventObserved(std::move(pointer_event_up), 0u,
996 0); 1138 0);
997 1139
998 // No event was sensed. 1140 // No event was sensed.
999 EXPECT_FALSE(last_event_observed()); 1141 EXPECT_FALSE(last_event_observed());
1000 } 1142 }
1001 1143
1002 // Tests pointer watchers triggered by events that hit this window tree. 1144 // Tests pointer watchers triggered by events that hit this window tree.
1003 TEST_F(WindowTreeClientPointerObserverTest, 1145 TEST_F(WindowTreeClientPointerObserverTest,
1004 OnWindowInputEventWithPointerWatcher) { 1146 OnWindowInputEventWithPointerWatcher) {
1005 std::unique_ptr<Window> top_level(base::MakeUnique<Window>(nullptr)); 1147 WindowTreeHostMus window_tree_host(window_tree_client_impl(),
1006 top_level->SetType(ui::wm::WINDOW_TYPE_NORMAL); 1148 cc::FrameSinkId(1, 1));
1007 top_level->Init(ui::LAYER_NOT_DRAWN); 1149 Window* top_level = window_tree_host.window();
1008 top_level->SetBounds(gfx::Rect(0, 0, 100, 100)); 1150 const gfx::Rect bounds(0, 0, 100, 100);
1009 top_level->Show(); 1151 window_tree_host.SetBoundsInPixels(bounds);
1152 window_tree_host.InitHost();
1153 window_tree_host.Show();
1154 EXPECT_EQ(bounds, top_level->bounds());
1010 1155
1011 // Start a pointer watcher for all events excluding move events. 1156 // Start a pointer watcher for all events excluding move events.
1012 window_tree_client_impl()->StartPointerWatcher(false /* want_moves */); 1157 window_tree_client_impl()->StartPointerWatcher(false /* want_moves */);
1013 1158
1014 // Simulate the server dispatching an event that also matched the observer. 1159 // Simulate the server dispatching an event that also matched the observer.
1015 std::unique_ptr<ui::PointerEvent> pointer_event_down(new ui::PointerEvent( 1160 std::unique_ptr<ui::PointerEvent> pointer_event_down(new ui::PointerEvent(
1016 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 0, 1161 ui::ET_POINTER_DOWN, gfx::Point(), gfx::Point(), ui::EF_CONTROL_DOWN, 0,
1017 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1), 1162 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1),
1018 base::TimeTicks::Now())); 1163 base::TimeTicks::Now()));
1019 window_tree_client()->OnWindowInputEvent(1, server_id(top_level.get()), 0, 1164 window_tree_client()->OnWindowInputEvent(1, server_id(top_level), 0,
1020 std::move(pointer_event_down), true); 1165 std::move(pointer_event_down), true);
1021 1166
1022 // Delegate sensed the event. 1167 // Delegate sensed the event.
1023 const ui::Event* last_event = last_event_observed(); 1168 const ui::Event* last_event = last_event_observed();
1024 ASSERT_TRUE(last_event); 1169 ASSERT_TRUE(last_event);
1025 EXPECT_EQ(ui::ET_POINTER_DOWN, last_event->type()); 1170 EXPECT_EQ(ui::ET_POINTER_DOWN, last_event->type());
1026 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags()); 1171 EXPECT_EQ(ui::EF_CONTROL_DOWN, last_event->flags());
1027 } 1172 }
1028 1173
1029 // Verifies focus is reverted if the server replied that the change failed. 1174 // Verifies focus is reverted if the server replied that the change failed.
(...skipping 1076 matching lines...) Expand 10 before | Expand all | Expand 10 after
2106 window_tree()->GetEventResult(event_id)); 2251 window_tree()->GetEventResult(event_id));
2107 EXPECT_TRUE(window_delegate1.got_move()); 2252 EXPECT_TRUE(window_delegate1.got_move());
2108 EXPECT_FALSE(window_delegate2.got_move()); 2253 EXPECT_FALSE(window_delegate2.got_move());
2109 gfx::Point transformed_event_location_in_dip(event_location_in_dip.x() + 20, 2254 gfx::Point transformed_event_location_in_dip(event_location_in_dip.x() + 20,
2110 event_location_in_dip.y() + 30); 2255 event_location_in_dip.y() + 30);
2111 EXPECT_EQ(transformed_event_location_in_dip, 2256 EXPECT_EQ(transformed_event_location_in_dip,
2112 window_delegate1.last_event_location()); 2257 window_delegate1.last_event_location());
2113 } 2258 }
2114 2259
2115 } // namespace aura 2260 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698