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