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 "ui/aura/scoped_window_targeter.h" | 7 #include "ui/aura/scoped_window_targeter.h" |
8 #include "ui/aura/test/aura_test_base.h" | 8 #include "ui/aura/test/aura_test_base.h" |
9 #include "ui/aura/test/test_window_delegate.h" | 9 #include "ui/aura/test/test_window_delegate.h" |
10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
11 #include "ui/events/event_utils.h" | 11 #include "ui/events/event_utils.h" |
12 #include "ui/events/test/test_event_handler.h" | 12 #include "ui/events/test/test_event_handler.h" |
13 | 13 |
14 namespace aura { | 14 namespace aura { |
15 | 15 |
16 // Always returns the same window. | 16 // Always returns the same window. |
17 class StaticWindowTargeter : public ui::EventTargeter { | 17 class StaticWindowTargeter : public WindowTargeter { |
18 public: | 18 public: |
19 explicit StaticWindowTargeter(aura::Window* window) | 19 explicit StaticWindowTargeter(aura::Window* window) |
20 : window_(window) {} | 20 : window_(window) {} |
21 ~StaticWindowTargeter() override {} | 21 ~StaticWindowTargeter() override {} |
22 | 22 |
23 private: | 23 private: |
24 // ui::EventTargeter: | 24 // aura::WindowTargeter: |
25 ui::EventTarget* FindTargetForLocatedEvent(ui::EventTarget* root, | 25 Window* FindTargetForLocatedEvent(Window* window, |
26 ui::LocatedEvent* event) override { | 26 ui::LocatedEvent* event) override { |
27 return window_; | 27 return window_; |
28 } | 28 } |
29 | 29 |
30 Window* window_; | 30 Window* window_; |
31 | 31 |
32 DISALLOW_COPY_AND_ASSIGN(StaticWindowTargeter); | 32 DISALLOW_COPY_AND_ASSIGN(StaticWindowTargeter); |
33 }; | 33 }; |
34 | 34 |
35 class WindowTargeterTest : public test::AuraTestBase { | 35 class WindowTargeterTest : public test::AuraTestBase { |
36 public: | 36 public: |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 window->SetTransform(transform); | 168 window->SetTransform(transform); |
169 EXPECT_EQ(gfx::RectF(300, 30, 200, 40).ToString(), | 169 EXPECT_EQ(gfx::RectF(300, 30, 200, 40).ToString(), |
170 GetEffectiveVisibleBoundsInRootWindow(window.get()).ToString()); | 170 GetEffectiveVisibleBoundsInRootWindow(window.get()).ToString()); |
171 { | 171 { |
172 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, | 172 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, |
173 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); | 173 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
174 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root_target, &mouse)); | 174 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root_target, &mouse)); |
175 } | 175 } |
176 } | 176 } |
177 | 177 |
| 178 class IdCheckingEventTargeter : public WindowTargeter { |
| 179 public: |
| 180 IdCheckingEventTargeter(int id) : id_(id) {} |
| 181 ~IdCheckingEventTargeter() override {} |
| 182 |
| 183 protected: |
| 184 // WindowTargeter: |
| 185 bool SubtreeShouldBeExploredForEvent(Window* window, |
| 186 const ui::LocatedEvent& event) override { |
| 187 return (window->id() == id_ && |
| 188 WindowTargeter::SubtreeShouldBeExploredForEvent(window, event)); |
| 189 } |
| 190 |
| 191 private: |
| 192 int id_; |
| 193 }; |
| 194 |
| 195 TEST_F(WindowTargeterTest, Bounds) { |
| 196 test::TestWindowDelegate delegate; |
| 197 scoped_ptr<Window> parent(CreateNormalWindow(1, root_window(), &delegate)); |
| 198 scoped_ptr<Window> child(CreateNormalWindow(1, parent.get(), &delegate)); |
| 199 scoped_ptr<Window> grandchild(CreateNormalWindow(1, child.get(), &delegate)); |
| 200 |
| 201 parent->SetBounds(gfx::Rect(0, 0, 30, 30)); |
| 202 child->SetBounds(gfx::Rect(5, 5, 20, 20)); |
| 203 grandchild->SetBounds(gfx::Rect(5, 5, 5, 5)); |
| 204 |
| 205 ASSERT_EQ(1u, root_window()->children().size()); |
| 206 ASSERT_EQ(1u, root_window()->children()[0]->children().size()); |
| 207 ASSERT_EQ(1u, root_window()->children()[0]->children()[0]->children().size()); |
| 208 |
| 209 Window* parent_r = root_window()->children()[0]; |
| 210 Window* child_r = parent_r->children()[0]; |
| 211 Window* grandchild_r = child_r->children()[0]; |
| 212 |
| 213 ui::EventTarget* root_target = root_window(); |
| 214 ui::EventTargeter* targeter = root_target->GetEventTargeter(); |
| 215 |
| 216 // Dispatch a mouse event that falls on the parent, but not on the child. When |
| 217 // the default event-targeter used, the event will still reach |grandchild|, |
| 218 // because the default targeter does not look at the bounds. |
| 219 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1), |
| 220 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 221 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse)); |
| 222 |
| 223 // Install a targeter on the |child| that looks at the window id as well |
| 224 // as the bounds and makes sure the event reaches the target only if the id of |
| 225 // the window is equal to 2 (incorrect). This causes the event to get handled |
| 226 // by |parent|. |
| 227 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), |
| 228 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 229 scoped_ptr<ui::EventTargeter> original_targeter = child_r->SetEventTargeter( |
| 230 scoped_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(2))); |
| 231 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse2)); |
| 232 |
| 233 // Now install a targeter on the |child| that looks at the window id as well |
| 234 // as the bounds and makes sure the event reaches the target only if the id of |
| 235 // the window is equal to 1 (correct). |
| 236 ui::MouseEvent mouse3(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), |
| 237 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 238 child_r->SetEventTargeter( |
| 239 scoped_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(1))); |
| 240 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &mouse3)); |
| 241 |
| 242 // restore original WindowTargeter for |child|. |
| 243 child_r->SetEventTargeter(original_targeter.Pass()); |
| 244 |
| 245 // Target |grandchild| location. |
| 246 ui::MouseEvent second(ui::ET_MOUSE_MOVED, gfx::Point(12, 12), |
| 247 gfx::Point(12, 12), ui::EventTimeForNow(), ui::EF_NONE, |
| 248 ui::EF_NONE); |
| 249 EXPECT_EQ(grandchild_r, targeter->FindTargetForEvent(root_target, &second)); |
| 250 |
| 251 // Target |child| location. |
| 252 ui::MouseEvent third(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), |
| 253 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 254 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &third)); |
| 255 } |
| 256 |
| 257 class IgnoreWindowTargeter : public WindowTargeter { |
| 258 public: |
| 259 IgnoreWindowTargeter() {} |
| 260 ~IgnoreWindowTargeter() override {} |
| 261 |
| 262 private: |
| 263 // WindowTargeter: |
| 264 bool SubtreeShouldBeExploredForEvent(Window* window, |
| 265 const ui::LocatedEvent& event) override { |
| 266 return false; |
| 267 } |
| 268 }; |
| 269 |
| 270 // Verifies that an EventTargeter installed on an EventTarget can dictate |
| 271 // whether the target itself can process an event. |
| 272 TEST_F(WindowTargeterTest, TargeterChecksOwningEventTarget) { |
| 273 test::TestWindowDelegate delegate; |
| 274 scoped_ptr<Window> child(CreateNormalWindow(1, root_window(), &delegate)); |
| 275 |
| 276 ui::EventTarget* root_target = root_window(); |
| 277 ui::EventTargeter* targeter = root_target->GetEventTargeter(); |
| 278 |
| 279 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), |
| 280 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, |
| 281 ui::EF_NONE); |
| 282 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse)); |
| 283 |
| 284 // Install an event targeter on |child| which always prevents the target from |
| 285 // receiving event. |
| 286 child->SetEventTargeter( |
| 287 scoped_ptr<ui::EventTargeter>(new IgnoreWindowTargeter())); |
| 288 |
| 289 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), |
| 290 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, |
| 291 ui::EF_NONE); |
| 292 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2)); |
| 293 } |
| 294 |
178 } // namespace aura | 295 } // namespace aura |
OLD | NEW |