| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 CHECK(root->layer()); | 50 CHECK(root->layer()); |
| 51 gfx::Transform transform; | 51 gfx::Transform transform; |
| 52 if (!window->layer()->GetTargetTransformRelativeTo(root->layer(), &transform)) | 52 if (!window->layer()->GetTargetTransformRelativeTo(root->layer(), &transform)) |
| 53 return gfx::RectF(); | 53 return gfx::RectF(); |
| 54 transform.TransformRect(&bounds); | 54 transform.TransformRect(&bounds); |
| 55 return bounds; | 55 return bounds; |
| 56 } | 56 } |
| 57 | 57 |
| 58 TEST_F(WindowTargeterTest, Basic) { | 58 TEST_F(WindowTargeterTest, Basic) { |
| 59 test::TestWindowDelegate delegate; | 59 test::TestWindowDelegate delegate; |
| 60 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), &delegate)); | 60 std::unique_ptr<Window> window( |
| 61 CreateNormalWindow(1, root_window(), &delegate)); |
| 61 Window* one = CreateNormalWindow(2, window.get(), &delegate); | 62 Window* one = CreateNormalWindow(2, window.get(), &delegate); |
| 62 Window* two = CreateNormalWindow(3, window.get(), &delegate); | 63 Window* two = CreateNormalWindow(3, window.get(), &delegate); |
| 63 | 64 |
| 64 window->SetBounds(gfx::Rect(0, 0, 100, 100)); | 65 window->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 65 one->SetBounds(gfx::Rect(0, 0, 500, 100)); | 66 one->SetBounds(gfx::Rect(0, 0, 500, 100)); |
| 66 two->SetBounds(gfx::Rect(501, 0, 500, 1000)); | 67 two->SetBounds(gfx::Rect(501, 0, 500, 1000)); |
| 67 | 68 |
| 68 root_window()->Show(); | 69 root_window()->Show(); |
| 69 | 70 |
| 70 ui::test::TestEventHandler handler; | 71 ui::test::TestEventHandler handler; |
| 71 one->AddPreTargetHandler(&handler); | 72 one->AddPreTargetHandler(&handler); |
| 72 | 73 |
| 73 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(20, 20), | 74 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(20, 20), |
| 74 gfx::Point(20, 20), ui::EventTimeForNow(), ui::EF_NONE, | 75 gfx::Point(20, 20), ui::EventTimeForNow(), ui::EF_NONE, |
| 75 ui::EF_NONE); | 76 ui::EF_NONE); |
| 76 DispatchEventUsingWindowDispatcher(&press); | 77 DispatchEventUsingWindowDispatcher(&press); |
| 77 EXPECT_EQ(1, handler.num_mouse_events()); | 78 EXPECT_EQ(1, handler.num_mouse_events()); |
| 78 | 79 |
| 79 handler.Reset(); | 80 handler.Reset(); |
| 80 DispatchEventUsingWindowDispatcher(&press); | 81 DispatchEventUsingWindowDispatcher(&press); |
| 81 EXPECT_EQ(1, handler.num_mouse_events()); | 82 EXPECT_EQ(1, handler.num_mouse_events()); |
| 82 | 83 |
| 83 one->RemovePreTargetHandler(&handler); | 84 one->RemovePreTargetHandler(&handler); |
| 84 } | 85 } |
| 85 | 86 |
| 86 TEST_F(WindowTargeterTest, ScopedWindowTargeter) { | 87 TEST_F(WindowTargeterTest, ScopedWindowTargeter) { |
| 87 test::TestWindowDelegate delegate; | 88 test::TestWindowDelegate delegate; |
| 88 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), &delegate)); | 89 std::unique_ptr<Window> window( |
| 90 CreateNormalWindow(1, root_window(), &delegate)); |
| 89 Window* child = CreateNormalWindow(2, window.get(), &delegate); | 91 Window* child = CreateNormalWindow(2, window.get(), &delegate); |
| 90 | 92 |
| 91 window->SetBounds(gfx::Rect(30, 30, 100, 100)); | 93 window->SetBounds(gfx::Rect(30, 30, 100, 100)); |
| 92 child->SetBounds(gfx::Rect(20, 20, 50, 50)); | 94 child->SetBounds(gfx::Rect(20, 20, 50, 50)); |
| 93 root_window()->Show(); | 95 root_window()->Show(); |
| 94 | 96 |
| 95 ui::EventTarget* root = root_window(); | 97 ui::EventTarget* root = root_window(); |
| 96 ui::EventTargeter* targeter = root->GetEventTargeter(); | 98 ui::EventTargeter* targeter = root->GetEventTargeter(); |
| 97 | 99 |
| 98 gfx::Point event_location(60, 60); | 100 gfx::Point event_location(60, 60); |
| 99 { | 101 { |
| 100 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, | 102 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, |
| 101 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 103 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 102 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); | 104 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); |
| 103 } | 105 } |
| 104 | 106 |
| 105 // Install a targeter on |window| so that the events never reach the child. | 107 // Install a targeter on |window| so that the events never reach the child. |
| 106 scoped_ptr<ScopedWindowTargeter> scoped_targeter( | 108 std::unique_ptr<ScopedWindowTargeter> scoped_targeter( |
| 107 new ScopedWindowTargeter(window.get(), scoped_ptr<ui::EventTargeter>( | 109 new ScopedWindowTargeter(window.get(), |
| 108 new StaticWindowTargeter(window.get())))); | 110 std::unique_ptr<ui::EventTargeter>( |
| 111 new StaticWindowTargeter(window.get())))); |
| 109 { | 112 { |
| 110 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, | 113 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, |
| 111 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 114 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 112 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse)); | 115 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse)); |
| 113 } | 116 } |
| 114 scoped_targeter.reset(); | 117 scoped_targeter.reset(); |
| 115 { | 118 { |
| 116 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, | 119 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, |
| 117 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 120 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 118 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); | 121 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); |
| 119 } | 122 } |
| 120 } | 123 } |
| 121 | 124 |
| 122 // Test that ScopedWindowTargeter does not crash if the window for which it | 125 // Test that ScopedWindowTargeter does not crash if the window for which it |
| 123 // replaces the targeter gets destroyed before it does. | 126 // replaces the targeter gets destroyed before it does. |
| 124 TEST_F(WindowTargeterTest, ScopedWindowTargeterWindowDestroyed) { | 127 TEST_F(WindowTargeterTest, ScopedWindowTargeterWindowDestroyed) { |
| 125 test::TestWindowDelegate delegate; | 128 test::TestWindowDelegate delegate; |
| 126 scoped_ptr<Window> window(CreateNormalWindow(1, root_window(), &delegate)); | 129 std::unique_ptr<Window> window( |
| 127 scoped_ptr<ScopedWindowTargeter> scoped_targeter( | 130 CreateNormalWindow(1, root_window(), &delegate)); |
| 128 new ScopedWindowTargeter(window.get(), scoped_ptr<ui::EventTargeter>( | 131 std::unique_ptr<ScopedWindowTargeter> scoped_targeter( |
| 129 new StaticWindowTargeter(window.get())))); | 132 new ScopedWindowTargeter(window.get(), |
| 133 std::unique_ptr<ui::EventTargeter>( |
| 134 new StaticWindowTargeter(window.get())))); |
| 130 | 135 |
| 131 window.reset(); | 136 window.reset(); |
| 132 scoped_targeter.reset(); | 137 scoped_targeter.reset(); |
| 133 | 138 |
| 134 // We did not crash! | 139 // We did not crash! |
| 135 } | 140 } |
| 136 | 141 |
| 137 TEST_F(WindowTargeterTest, TargetTransformedWindow) { | 142 TEST_F(WindowTargeterTest, TargetTransformedWindow) { |
| 138 root_window()->Show(); | 143 root_window()->Show(); |
| 139 | 144 |
| 140 test::TestWindowDelegate delegate; | 145 test::TestWindowDelegate delegate; |
| 141 scoped_ptr<Window> window(CreateNormalWindow(2, root_window(), &delegate)); | 146 std::unique_ptr<Window> window( |
| 147 CreateNormalWindow(2, root_window(), &delegate)); |
| 142 | 148 |
| 143 const gfx::Rect window_bounds(100, 20, 400, 80); | 149 const gfx::Rect window_bounds(100, 20, 400, 80); |
| 144 window->SetBounds(window_bounds); | 150 window->SetBounds(window_bounds); |
| 145 | 151 |
| 146 ui::EventTarget* root_target = root_window(); | 152 ui::EventTarget* root_target = root_window(); |
| 147 ui::EventTargeter* targeter = root_target->GetEventTargeter(); | 153 ui::EventTargeter* targeter = root_target->GetEventTargeter(); |
| 148 gfx::Point event_location(490, 50); | 154 gfx::Point event_location(490, 50); |
| 149 { | 155 { |
| 150 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, | 156 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, |
| 151 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 157 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 return (window->id() == id_ && | 196 return (window->id() == id_ && |
| 191 WindowTargeter::SubtreeShouldBeExploredForEvent(window, event)); | 197 WindowTargeter::SubtreeShouldBeExploredForEvent(window, event)); |
| 192 } | 198 } |
| 193 | 199 |
| 194 private: | 200 private: |
| 195 int id_; | 201 int id_; |
| 196 }; | 202 }; |
| 197 | 203 |
| 198 TEST_F(WindowTargeterTest, Bounds) { | 204 TEST_F(WindowTargeterTest, Bounds) { |
| 199 test::TestWindowDelegate delegate; | 205 test::TestWindowDelegate delegate; |
| 200 scoped_ptr<Window> parent(CreateNormalWindow(1, root_window(), &delegate)); | 206 std::unique_ptr<Window> parent( |
| 201 scoped_ptr<Window> child(CreateNormalWindow(1, parent.get(), &delegate)); | 207 CreateNormalWindow(1, root_window(), &delegate)); |
| 202 scoped_ptr<Window> grandchild(CreateNormalWindow(1, child.get(), &delegate)); | 208 std::unique_ptr<Window> child(CreateNormalWindow(1, parent.get(), &delegate)); |
| 209 std::unique_ptr<Window> grandchild( |
| 210 CreateNormalWindow(1, child.get(), &delegate)); |
| 203 | 211 |
| 204 parent->SetBounds(gfx::Rect(0, 0, 30, 30)); | 212 parent->SetBounds(gfx::Rect(0, 0, 30, 30)); |
| 205 child->SetBounds(gfx::Rect(5, 5, 20, 20)); | 213 child->SetBounds(gfx::Rect(5, 5, 20, 20)); |
| 206 grandchild->SetBounds(gfx::Rect(5, 5, 5, 5)); | 214 grandchild->SetBounds(gfx::Rect(5, 5, 5, 5)); |
| 207 | 215 |
| 208 ASSERT_EQ(1u, root_window()->children().size()); | 216 ASSERT_EQ(1u, root_window()->children().size()); |
| 209 ASSERT_EQ(1u, root_window()->children()[0]->children().size()); | 217 ASSERT_EQ(1u, root_window()->children()[0]->children().size()); |
| 210 ASSERT_EQ(1u, root_window()->children()[0]->children()[0]->children().size()); | 218 ASSERT_EQ(1u, root_window()->children()[0]->children()[0]->children().size()); |
| 211 | 219 |
| 212 Window* parent_r = root_window()->children()[0]; | 220 Window* parent_r = root_window()->children()[0]; |
| 213 Window* child_r = parent_r->children()[0]; | 221 Window* child_r = parent_r->children()[0]; |
| 214 Window* grandchild_r = child_r->children()[0]; | 222 Window* grandchild_r = child_r->children()[0]; |
| 215 | 223 |
| 216 ui::EventTarget* root_target = root_window(); | 224 ui::EventTarget* root_target = root_window(); |
| 217 ui::EventTargeter* targeter = root_target->GetEventTargeter(); | 225 ui::EventTargeter* targeter = root_target->GetEventTargeter(); |
| 218 | 226 |
| 219 // Dispatch a mouse event that falls on the parent, but not on the child. When | 227 // Dispatch a mouse event that falls on the parent, but not on the child. When |
| 220 // the default event-targeter used, the event will still reach |grandchild|, | 228 // the default event-targeter used, the event will still reach |grandchild|, |
| 221 // because the default targeter does not look at the bounds. | 229 // because the default targeter does not look at the bounds. |
| 222 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1), | 230 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1), |
| 223 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 231 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 224 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse)); | 232 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse)); |
| 225 | 233 |
| 226 // Install a targeter on the |child| that looks at the window id as well | 234 // Install a targeter on the |child| that looks at the window id as well |
| 227 // as the bounds and makes sure the event reaches the target only if the id of | 235 // as the bounds and makes sure the event reaches the target only if the id of |
| 228 // the window is equal to 2 (incorrect). This causes the event to get handled | 236 // the window is equal to 2 (incorrect). This causes the event to get handled |
| 229 // by |parent|. | 237 // by |parent|. |
| 230 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), | 238 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), |
| 231 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 239 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 232 scoped_ptr<ui::EventTargeter> original_targeter = child_r->SetEventTargeter( | 240 std::unique_ptr<ui::EventTargeter> original_targeter = |
| 233 scoped_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(2))); | 241 child_r->SetEventTargeter( |
| 242 std::unique_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(2))); |
| 234 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse2)); | 243 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse2)); |
| 235 | 244 |
| 236 // Now install a targeter on the |child| that looks at the window id as well | 245 // Now install a targeter on the |child| that looks at the window id as well |
| 237 // as the bounds and makes sure the event reaches the target only if the id of | 246 // as the bounds and makes sure the event reaches the target only if the id of |
| 238 // the window is equal to 1 (correct). | 247 // the window is equal to 1 (correct). |
| 239 ui::MouseEvent mouse3(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), | 248 ui::MouseEvent mouse3(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), |
| 240 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 249 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 241 child_r->SetEventTargeter( | 250 child_r->SetEventTargeter( |
| 242 scoped_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(1))); | 251 std::unique_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(1))); |
| 243 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &mouse3)); | 252 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &mouse3)); |
| 244 | 253 |
| 245 // restore original WindowTargeter for |child|. | 254 // restore original WindowTargeter for |child|. |
| 246 child_r->SetEventTargeter(std::move(original_targeter)); | 255 child_r->SetEventTargeter(std::move(original_targeter)); |
| 247 | 256 |
| 248 // Target |grandchild| location. | 257 // Target |grandchild| location. |
| 249 ui::MouseEvent second(ui::ET_MOUSE_MOVED, gfx::Point(12, 12), | 258 ui::MouseEvent second(ui::ET_MOUSE_MOVED, gfx::Point(12, 12), |
| 250 gfx::Point(12, 12), ui::EventTimeForNow(), ui::EF_NONE, | 259 gfx::Point(12, 12), ui::EventTimeForNow(), ui::EF_NONE, |
| 251 ui::EF_NONE); | 260 ui::EF_NONE); |
| 252 EXPECT_EQ(grandchild_r, targeter->FindTargetForEvent(root_target, &second)); | 261 EXPECT_EQ(grandchild_r, targeter->FindTargetForEvent(root_target, &second)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 267 bool SubtreeShouldBeExploredForEvent(Window* window, | 276 bool SubtreeShouldBeExploredForEvent(Window* window, |
| 268 const ui::LocatedEvent& event) override { | 277 const ui::LocatedEvent& event) override { |
| 269 return false; | 278 return false; |
| 270 } | 279 } |
| 271 }; | 280 }; |
| 272 | 281 |
| 273 // Verifies that an EventTargeter installed on an EventTarget can dictate | 282 // Verifies that an EventTargeter installed on an EventTarget can dictate |
| 274 // whether the target itself can process an event. | 283 // whether the target itself can process an event. |
| 275 TEST_F(WindowTargeterTest, TargeterChecksOwningEventTarget) { | 284 TEST_F(WindowTargeterTest, TargeterChecksOwningEventTarget) { |
| 276 test::TestWindowDelegate delegate; | 285 test::TestWindowDelegate delegate; |
| 277 scoped_ptr<Window> child(CreateNormalWindow(1, root_window(), &delegate)); | 286 std::unique_ptr<Window> child( |
| 287 CreateNormalWindow(1, root_window(), &delegate)); |
| 278 | 288 |
| 279 ui::EventTarget* root_target = root_window(); | 289 ui::EventTarget* root_target = root_window(); |
| 280 ui::EventTargeter* targeter = root_target->GetEventTargeter(); | 290 ui::EventTargeter* targeter = root_target->GetEventTargeter(); |
| 281 | 291 |
| 282 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), | 292 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), |
| 283 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, | 293 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, |
| 284 ui::EF_NONE); | 294 ui::EF_NONE); |
| 285 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse)); | 295 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse)); |
| 286 | 296 |
| 287 // Install an event targeter on |child| which always prevents the target from | 297 // Install an event targeter on |child| which always prevents the target from |
| 288 // receiving event. | 298 // receiving event. |
| 289 child->SetEventTargeter( | 299 child->SetEventTargeter( |
| 290 scoped_ptr<ui::EventTargeter>(new IgnoreWindowTargeter())); | 300 std::unique_ptr<ui::EventTargeter>(new IgnoreWindowTargeter())); |
| 291 | 301 |
| 292 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), | 302 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), |
| 293 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, | 303 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, |
| 294 ui::EF_NONE); | 304 ui::EF_NONE); |
| 295 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2)); | 305 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2)); |
| 296 } | 306 } |
| 297 | 307 |
| 298 } // namespace aura | 308 } // namespace aura |
| OLD | NEW |