| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/window_targeter.h" | 5 #include "ui/aura/window_targeter.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "ui/aura/scoped_window_targeter.h" | 10 #include "ui/aura/scoped_window_targeter.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 Window* FindTargetForLocatedEvent(Window* window, | 28 Window* FindTargetForLocatedEvent(Window* window, |
| 29 ui::LocatedEvent* event) override { | 29 ui::LocatedEvent* event) override { |
| 30 return window_; | 30 return window_; |
| 31 } | 31 } |
| 32 | 32 |
| 33 Window* window_; | 33 Window* window_; |
| 34 | 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(StaticWindowTargeter); | 35 DISALLOW_COPY_AND_ASSIGN(StaticWindowTargeter); |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 class WindowTargeterTest : public test::AuraTestBase { | |
| 39 public: | |
| 40 WindowTargeterTest() {} | |
| 41 ~WindowTargeterTest() override {} | |
| 42 | |
| 43 Window* root_window() { return AuraTestBase::root_window(); } | |
| 44 }; | |
| 45 | |
| 46 gfx::RectF GetEffectiveVisibleBoundsInRootWindow(Window* window) { | 38 gfx::RectF GetEffectiveVisibleBoundsInRootWindow(Window* window) { |
| 47 gfx::RectF bounds = gfx::RectF(gfx::SizeF(window->bounds().size())); | 39 gfx::RectF bounds = gfx::RectF(gfx::SizeF(window->bounds().size())); |
| 48 Window* root = window->GetRootWindow(); | 40 Window* root = window->GetRootWindow(); |
| 49 CHECK(window->layer()); | 41 CHECK(window->layer()); |
| 50 CHECK(root->layer()); | 42 CHECK(root->layer()); |
| 51 gfx::Transform transform; | 43 gfx::Transform transform; |
| 52 if (!window->layer()->GetTargetTransformRelativeTo(root->layer(), &transform)) | 44 if (!window->layer()->GetTargetTransformRelativeTo(root->layer(), &transform)) |
| 53 return gfx::RectF(); | 45 return gfx::RectF(); |
| 54 transform.TransformRect(&bounds); | 46 transform.TransformRect(&bounds); |
| 55 return bounds; | 47 return bounds; |
| 56 } | 48 } |
| 57 | 49 |
| 58 TEST_F(WindowTargeterTest, Basic) { | 50 using WindowTargeterTest = test::AuraTestBaseWithType; |
| 51 |
| 52 TEST_P(WindowTargeterTest, Basic) { |
| 59 test::TestWindowDelegate delegate; | 53 test::TestWindowDelegate delegate; |
| 60 std::unique_ptr<Window> window( | 54 std::unique_ptr<Window> window( |
| 61 CreateNormalWindow(1, root_window(), &delegate)); | 55 CreateNormalWindow(1, root_window(), &delegate)); |
| 62 Window* one = CreateNormalWindow(2, window.get(), &delegate); | 56 Window* one = CreateNormalWindow(2, window.get(), &delegate); |
| 63 Window* two = CreateNormalWindow(3, window.get(), &delegate); | 57 Window* two = CreateNormalWindow(3, window.get(), &delegate); |
| 64 | 58 |
| 65 window->SetBounds(gfx::Rect(0, 0, 100, 100)); | 59 window->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 66 one->SetBounds(gfx::Rect(0, 0, 500, 100)); | 60 one->SetBounds(gfx::Rect(0, 0, 500, 100)); |
| 67 two->SetBounds(gfx::Rect(501, 0, 500, 1000)); | 61 two->SetBounds(gfx::Rect(501, 0, 500, 1000)); |
| 68 | 62 |
| 69 root_window()->Show(); | 63 root_window()->Show(); |
| 70 | 64 |
| 71 ui::test::TestEventHandler handler; | 65 ui::test::TestEventHandler handler; |
| 72 one->AddPreTargetHandler(&handler); | 66 one->AddPreTargetHandler(&handler); |
| 73 | 67 |
| 74 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(20, 20), | 68 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(20, 20), |
| 75 gfx::Point(20, 20), ui::EventTimeForNow(), ui::EF_NONE, | 69 gfx::Point(20, 20), ui::EventTimeForNow(), ui::EF_NONE, |
| 76 ui::EF_NONE); | 70 ui::EF_NONE); |
| 77 DispatchEventUsingWindowDispatcher(&press); | 71 DispatchEventUsingWindowDispatcher(&press); |
| 78 EXPECT_EQ(1, handler.num_mouse_events()); | 72 EXPECT_EQ(1, handler.num_mouse_events()); |
| 79 | 73 |
| 80 handler.Reset(); | 74 handler.Reset(); |
| 81 DispatchEventUsingWindowDispatcher(&press); | 75 DispatchEventUsingWindowDispatcher(&press); |
| 82 EXPECT_EQ(1, handler.num_mouse_events()); | 76 EXPECT_EQ(1, handler.num_mouse_events()); |
| 83 | 77 |
| 84 one->RemovePreTargetHandler(&handler); | 78 one->RemovePreTargetHandler(&handler); |
| 85 } | 79 } |
| 86 | 80 |
| 87 TEST_F(WindowTargeterTest, ScopedWindowTargeter) { | 81 TEST_P(WindowTargeterTest, ScopedWindowTargeter) { |
| 88 test::TestWindowDelegate delegate; | 82 test::TestWindowDelegate delegate; |
| 89 std::unique_ptr<Window> window( | 83 std::unique_ptr<Window> window( |
| 90 CreateNormalWindow(1, root_window(), &delegate)); | 84 CreateNormalWindow(1, root_window(), &delegate)); |
| 91 Window* child = CreateNormalWindow(2, window.get(), &delegate); | 85 Window* child = CreateNormalWindow(2, window.get(), &delegate); |
| 92 | 86 |
| 93 window->SetBounds(gfx::Rect(30, 30, 100, 100)); | 87 window->SetBounds(gfx::Rect(30, 30, 100, 100)); |
| 94 child->SetBounds(gfx::Rect(20, 20, 50, 50)); | 88 child->SetBounds(gfx::Rect(20, 20, 50, 50)); |
| 95 root_window()->Show(); | 89 root_window()->Show(); |
| 96 | 90 |
| 97 ui::EventTarget* root = root_window(); | 91 ui::EventTarget* root = root_window(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 117 scoped_targeter.reset(); | 111 scoped_targeter.reset(); |
| 118 { | 112 { |
| 119 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, | 113 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, |
| 120 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 114 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 121 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); | 115 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); |
| 122 } | 116 } |
| 123 } | 117 } |
| 124 | 118 |
| 125 // Test that ScopedWindowTargeter does not crash if the window for which it | 119 // Test that ScopedWindowTargeter does not crash if the window for which it |
| 126 // replaces the targeter gets destroyed before it does. | 120 // replaces the targeter gets destroyed before it does. |
| 127 TEST_F(WindowTargeterTest, ScopedWindowTargeterWindowDestroyed) { | 121 TEST_P(WindowTargeterTest, ScopedWindowTargeterWindowDestroyed) { |
| 128 test::TestWindowDelegate delegate; | 122 test::TestWindowDelegate delegate; |
| 129 std::unique_ptr<Window> window( | 123 std::unique_ptr<Window> window( |
| 130 CreateNormalWindow(1, root_window(), &delegate)); | 124 CreateNormalWindow(1, root_window(), &delegate)); |
| 131 std::unique_ptr<ScopedWindowTargeter> scoped_targeter( | 125 std::unique_ptr<ScopedWindowTargeter> scoped_targeter( |
| 132 new ScopedWindowTargeter(window.get(), | 126 new ScopedWindowTargeter(window.get(), |
| 133 std::unique_ptr<ui::EventTargeter>( | 127 std::unique_ptr<ui::EventTargeter>( |
| 134 new StaticWindowTargeter(window.get())))); | 128 new StaticWindowTargeter(window.get())))); |
| 135 | 129 |
| 136 window.reset(); | 130 window.reset(); |
| 137 scoped_targeter.reset(); | 131 scoped_targeter.reset(); |
| 138 | 132 |
| 139 // We did not crash! | 133 // We did not crash! |
| 140 } | 134 } |
| 141 | 135 |
| 142 TEST_F(WindowTargeterTest, TargetTransformedWindow) { | 136 TEST_P(WindowTargeterTest, TargetTransformedWindow) { |
| 143 root_window()->Show(); | 137 root_window()->Show(); |
| 144 | 138 |
| 145 test::TestWindowDelegate delegate; | 139 test::TestWindowDelegate delegate; |
| 146 std::unique_ptr<Window> window( | 140 std::unique_ptr<Window> window( |
| 147 CreateNormalWindow(2, root_window(), &delegate)); | 141 CreateNormalWindow(2, root_window(), &delegate)); |
| 148 | 142 |
| 149 const gfx::Rect window_bounds(100, 20, 400, 80); | 143 const gfx::Rect window_bounds(100, 20, 400, 80); |
| 150 window->SetBounds(window_bounds); | 144 window->SetBounds(window_bounds); |
| 151 | 145 |
| 152 ui::EventTarget* root_target = root_window(); | 146 ui::EventTarget* root_target = root_window(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 bool SubtreeShouldBeExploredForEvent(Window* window, | 188 bool SubtreeShouldBeExploredForEvent(Window* window, |
| 195 const ui::LocatedEvent& event) override { | 189 const ui::LocatedEvent& event) override { |
| 196 return (window->id() == id_ && | 190 return (window->id() == id_ && |
| 197 WindowTargeter::SubtreeShouldBeExploredForEvent(window, event)); | 191 WindowTargeter::SubtreeShouldBeExploredForEvent(window, event)); |
| 198 } | 192 } |
| 199 | 193 |
| 200 private: | 194 private: |
| 201 int id_; | 195 int id_; |
| 202 }; | 196 }; |
| 203 | 197 |
| 204 TEST_F(WindowTargeterTest, Bounds) { | 198 TEST_P(WindowTargeterTest, Bounds) { |
| 205 test::TestWindowDelegate delegate; | 199 test::TestWindowDelegate delegate; |
| 206 std::unique_ptr<Window> parent( | 200 std::unique_ptr<Window> parent( |
| 207 CreateNormalWindow(1, root_window(), &delegate)); | 201 CreateNormalWindow(1, root_window(), &delegate)); |
| 208 std::unique_ptr<Window> child(CreateNormalWindow(1, parent.get(), &delegate)); | 202 std::unique_ptr<Window> child(CreateNormalWindow(1, parent.get(), &delegate)); |
| 209 std::unique_ptr<Window> grandchild( | 203 std::unique_ptr<Window> grandchild( |
| 210 CreateNormalWindow(1, child.get(), &delegate)); | 204 CreateNormalWindow(1, child.get(), &delegate)); |
| 211 | 205 |
| 212 parent->SetBounds(gfx::Rect(0, 0, 30, 30)); | 206 parent->SetBounds(gfx::Rect(0, 0, 30, 30)); |
| 213 child->SetBounds(gfx::Rect(5, 5, 20, 20)); | 207 child->SetBounds(gfx::Rect(5, 5, 20, 20)); |
| 214 grandchild->SetBounds(gfx::Rect(5, 5, 5, 5)); | 208 grandchild->SetBounds(gfx::Rect(5, 5, 5, 5)); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 private: | 268 private: |
| 275 // WindowTargeter: | 269 // WindowTargeter: |
| 276 bool SubtreeShouldBeExploredForEvent(Window* window, | 270 bool SubtreeShouldBeExploredForEvent(Window* window, |
| 277 const ui::LocatedEvent& event) override { | 271 const ui::LocatedEvent& event) override { |
| 278 return false; | 272 return false; |
| 279 } | 273 } |
| 280 }; | 274 }; |
| 281 | 275 |
| 282 // Verifies that an EventTargeter installed on an EventTarget can dictate | 276 // Verifies that an EventTargeter installed on an EventTarget can dictate |
| 283 // whether the target itself can process an event. | 277 // whether the target itself can process an event. |
| 284 TEST_F(WindowTargeterTest, TargeterChecksOwningEventTarget) { | 278 TEST_P(WindowTargeterTest, TargeterChecksOwningEventTarget) { |
| 285 test::TestWindowDelegate delegate; | 279 test::TestWindowDelegate delegate; |
| 286 std::unique_ptr<Window> child( | 280 std::unique_ptr<Window> child( |
| 287 CreateNormalWindow(1, root_window(), &delegate)); | 281 CreateNormalWindow(1, root_window(), &delegate)); |
| 288 | 282 |
| 289 ui::EventTarget* root_target = root_window(); | 283 ui::EventTarget* root_target = root_window(); |
| 290 ui::EventTargeter* targeter = root_target->GetEventTargeter(); | 284 ui::EventTargeter* targeter = root_target->GetEventTargeter(); |
| 291 | 285 |
| 292 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), | 286 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), |
| 293 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, | 287 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, |
| 294 ui::EF_NONE); | 288 ui::EF_NONE); |
| 295 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse)); | 289 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse)); |
| 296 | 290 |
| 297 // Install an event targeter on |child| which always prevents the target from | 291 // Install an event targeter on |child| which always prevents the target from |
| 298 // receiving event. | 292 // receiving event. |
| 299 child->SetEventTargeter( | 293 child->SetEventTargeter( |
| 300 std::unique_ptr<ui::EventTargeter>(new IgnoreWindowTargeter())); | 294 std::unique_ptr<ui::EventTargeter>(new IgnoreWindowTargeter())); |
| 301 | 295 |
| 302 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), | 296 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), |
| 303 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, | 297 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, |
| 304 ui::EF_NONE); | 298 ui::EF_NONE); |
| 305 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2)); | 299 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2)); |
| 306 } | 300 } |
| 307 | 301 |
| 302 INSTANTIATE_TEST_CASE_P(/* no prefix */, |
| 303 WindowTargeterTest, |
| 304 ::testing::Values(test::BackendType::CLASSIC, |
| 305 test::BackendType::MUS)); |
| 306 |
| 308 } // namespace aura | 307 } // namespace aura |
| OLD | NEW |