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

Side by Side Diff: ui/aura/window_targeter_unittest.cc

Issue 2786693002: Add PointerDetails to ui::MouseEvent's constructors (Closed)
Patch Set: mouse event constructor 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 (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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 58
59 window->SetBounds(gfx::Rect(0, 0, 100, 100)); 59 window->SetBounds(gfx::Rect(0, 0, 100, 100));
60 one->SetBounds(gfx::Rect(0, 0, 500, 100)); 60 one->SetBounds(gfx::Rect(0, 0, 500, 100));
61 two->SetBounds(gfx::Rect(501, 0, 500, 1000)); 61 two->SetBounds(gfx::Rect(501, 0, 500, 1000));
62 62
63 root_window()->Show(); 63 root_window()->Show();
64 64
65 ui::test::TestEventHandler handler; 65 ui::test::TestEventHandler handler;
66 one->AddPreTargetHandler(&handler); 66 one->AddPreTargetHandler(&handler);
67 67
68 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(20, 20), 68 ui::MouseEvent press(
69 gfx::Point(20, 20), ui::EventTimeForNow(), ui::EF_NONE, 69 ui::ET_MOUSE_PRESSED, gfx::Point(20, 20), gfx::Point(20, 20),
70 ui::EF_NONE); 70 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
71 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
71 DispatchEventUsingWindowDispatcher(&press); 72 DispatchEventUsingWindowDispatcher(&press);
72 EXPECT_EQ(1, handler.num_mouse_events()); 73 EXPECT_EQ(1, handler.num_mouse_events());
73 74
74 handler.Reset(); 75 handler.Reset();
75 DispatchEventUsingWindowDispatcher(&press); 76 DispatchEventUsingWindowDispatcher(&press);
76 EXPECT_EQ(1, handler.num_mouse_events()); 77 EXPECT_EQ(1, handler.num_mouse_events());
77 78
78 one->RemovePreTargetHandler(&handler); 79 one->RemovePreTargetHandler(&handler);
79 } 80 }
80 81
81 TEST_P(WindowTargeterTest, ScopedWindowTargeter) { 82 TEST_P(WindowTargeterTest, ScopedWindowTargeter) {
82 test::TestWindowDelegate delegate; 83 test::TestWindowDelegate delegate;
83 std::unique_ptr<Window> window( 84 std::unique_ptr<Window> window(
84 CreateNormalWindow(1, root_window(), &delegate)); 85 CreateNormalWindow(1, root_window(), &delegate));
85 Window* child = CreateNormalWindow(2, window.get(), &delegate); 86 Window* child = CreateNormalWindow(2, window.get(), &delegate);
86 87
87 window->SetBounds(gfx::Rect(30, 30, 100, 100)); 88 window->SetBounds(gfx::Rect(30, 30, 100, 100));
88 child->SetBounds(gfx::Rect(20, 20, 50, 50)); 89 child->SetBounds(gfx::Rect(20, 20, 50, 50));
89 root_window()->Show(); 90 root_window()->Show();
90 91
91 ui::EventTarget* root = root_window(); 92 ui::EventTarget* root = root_window();
92 ui::EventTargeter* targeter = root->GetEventTargeter(); 93 ui::EventTargeter* targeter = root->GetEventTargeter();
93 94
94 gfx::Point event_location(60, 60); 95 gfx::Point event_location(60, 60);
95 { 96 {
96 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, 97 ui::MouseEvent mouse(
97 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 98 ui::ET_MOUSE_MOVED, event_location, event_location,
99 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
100 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
98 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); 101 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse));
99 } 102 }
100 103
101 // Install a targeter on |window| so that the events never reach the child. 104 // Install a targeter on |window| so that the events never reach the child.
102 std::unique_ptr<ScopedWindowTargeter> scoped_targeter( 105 std::unique_ptr<ScopedWindowTargeter> scoped_targeter(
103 new ScopedWindowTargeter(window.get(), 106 new ScopedWindowTargeter(window.get(),
104 std::unique_ptr<ui::EventTargeter>( 107 std::unique_ptr<ui::EventTargeter>(
105 new StaticWindowTargeter(window.get())))); 108 new StaticWindowTargeter(window.get()))));
106 { 109 {
107 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, 110 ui::MouseEvent mouse(
108 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 111 ui::ET_MOUSE_MOVED, event_location, event_location,
112 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
113 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
109 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse)); 114 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root, &mouse));
110 } 115 }
111 scoped_targeter.reset(); 116 scoped_targeter.reset();
112 { 117 {
113 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, 118 ui::MouseEvent mouse(
114 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 119 ui::ET_MOUSE_MOVED, event_location, event_location,
120 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
121 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
115 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse)); 122 EXPECT_EQ(child, targeter->FindTargetForEvent(root, &mouse));
116 } 123 }
117 } 124 }
118 125
119 // Test that ScopedWindowTargeter does not crash if the window for which it 126 // Test that ScopedWindowTargeter does not crash if the window for which it
120 // replaces the targeter gets destroyed before it does. 127 // replaces the targeter gets destroyed before it does.
121 TEST_P(WindowTargeterTest, ScopedWindowTargeterWindowDestroyed) { 128 TEST_P(WindowTargeterTest, ScopedWindowTargeterWindowDestroyed) {
122 test::TestWindowDelegate delegate; 129 test::TestWindowDelegate delegate;
123 std::unique_ptr<Window> window( 130 std::unique_ptr<Window> window(
124 CreateNormalWindow(1, root_window(), &delegate)); 131 CreateNormalWindow(1, root_window(), &delegate));
(...skipping 15 matching lines...) Expand all
140 std::unique_ptr<Window> window( 147 std::unique_ptr<Window> window(
141 CreateNormalWindow(2, root_window(), &delegate)); 148 CreateNormalWindow(2, root_window(), &delegate));
142 149
143 const gfx::Rect window_bounds(100, 20, 400, 80); 150 const gfx::Rect window_bounds(100, 20, 400, 80);
144 window->SetBounds(window_bounds); 151 window->SetBounds(window_bounds);
145 152
146 ui::EventTarget* root_target = root_window(); 153 ui::EventTarget* root_target = root_window();
147 ui::EventTargeter* targeter = root_target->GetEventTargeter(); 154 ui::EventTargeter* targeter = root_target->GetEventTargeter();
148 gfx::Point event_location(490, 50); 155 gfx::Point event_location(490, 50);
149 { 156 {
150 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, 157 ui::MouseEvent mouse(
151 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 158 ui::ET_MOUSE_MOVED, event_location, event_location,
159 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
160 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
152 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root_target, &mouse)); 161 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root_target, &mouse));
153 } 162 }
154 163
155 // Scale |window| by 50%. This should move it away from underneath 164 // Scale |window| by 50%. This should move it away from underneath
156 // |event_location|, so an event in that location will not be targeted to it. 165 // |event_location|, so an event in that location will not be targeted to it.
157 gfx::Transform transform; 166 gfx::Transform transform;
158 transform.Scale(0.5, 0.5); 167 transform.Scale(0.5, 0.5);
159 window->SetTransform(transform); 168 window->SetTransform(transform);
160 EXPECT_EQ(gfx::RectF(100, 20, 200, 40).ToString(), 169 EXPECT_EQ(gfx::RectF(100, 20, 200, 40).ToString(),
161 GetEffectiveVisibleBoundsInRootWindow(window.get()).ToString()); 170 GetEffectiveVisibleBoundsInRootWindow(window.get()).ToString());
162 { 171 {
163 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, 172 ui::MouseEvent mouse(
164 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 173 ui::ET_MOUSE_MOVED, event_location, event_location,
174 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
175 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
165 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse)); 176 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse));
166 } 177 }
167 178
168 transform = gfx::Transform(); 179 transform = gfx::Transform();
169 transform.Translate(200, 10); 180 transform.Translate(200, 10);
170 transform.Scale(0.5, 0.5); 181 transform.Scale(0.5, 0.5);
171 window->SetTransform(transform); 182 window->SetTransform(transform);
172 EXPECT_EQ(gfx::RectF(300, 30, 200, 40).ToString(), 183 EXPECT_EQ(gfx::RectF(300, 30, 200, 40).ToString(),
173 GetEffectiveVisibleBoundsInRootWindow(window.get()).ToString()); 184 GetEffectiveVisibleBoundsInRootWindow(window.get()).ToString());
174 { 185 {
175 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location, 186 ui::MouseEvent mouse(
176 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 187 ui::ET_MOUSE_MOVED, event_location, event_location,
188 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
189 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
177 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root_target, &mouse)); 190 EXPECT_EQ(window.get(), targeter->FindTargetForEvent(root_target, &mouse));
178 } 191 }
179 } 192 }
180 193
181 class IdCheckingEventTargeter : public WindowTargeter { 194 class IdCheckingEventTargeter : public WindowTargeter {
182 public: 195 public:
183 IdCheckingEventTargeter(int id) : id_(id) {} 196 IdCheckingEventTargeter(int id) : id_(id) {}
184 ~IdCheckingEventTargeter() override {} 197 ~IdCheckingEventTargeter() override {}
185 198
186 protected: 199 protected:
(...skipping 27 matching lines...) Expand all
214 Window* parent_r = root_window()->children()[0]; 227 Window* parent_r = root_window()->children()[0];
215 Window* child_r = parent_r->children()[0]; 228 Window* child_r = parent_r->children()[0];
216 Window* grandchild_r = child_r->children()[0]; 229 Window* grandchild_r = child_r->children()[0];
217 230
218 ui::EventTarget* root_target = root_window(); 231 ui::EventTarget* root_target = root_window();
219 ui::EventTargeter* targeter = root_target->GetEventTargeter(); 232 ui::EventTargeter* targeter = root_target->GetEventTargeter();
220 233
221 // Dispatch a mouse event that falls on the parent, but not on the child. When 234 // Dispatch a mouse event that falls on the parent, but not on the child. When
222 // the default event-targeter used, the event will still reach |grandchild|, 235 // the default event-targeter used, the event will still reach |grandchild|,
223 // because the default targeter does not look at the bounds. 236 // because the default targeter does not look at the bounds.
224 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1), 237 ui::MouseEvent mouse(
225 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 238 ui::ET_MOUSE_MOVED, gfx::Point(1, 1), gfx::Point(1, 1),
239 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
240 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
226 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse)); 241 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse));
227 242
228 // Install a targeter on the |child| that looks at the window id as well 243 // Install a targeter on the |child| that looks at the window id as well
229 // as the bounds and makes sure the event reaches the target only if the id of 244 // as the bounds and makes sure the event reaches the target only if the id of
230 // the window is equal to 2 (incorrect). This causes the event to get handled 245 // the window is equal to 2 (incorrect). This causes the event to get handled
231 // by |parent|. 246 // by |parent|.
232 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), 247 ui::MouseEvent mouse2(
233 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 248 ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8),
249 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
250 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
234 std::unique_ptr<ui::EventTargeter> original_targeter = 251 std::unique_ptr<ui::EventTargeter> original_targeter =
235 child_r->SetEventTargeter( 252 child_r->SetEventTargeter(
236 std::unique_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(2))); 253 std::unique_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(2)));
237 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse2)); 254 EXPECT_EQ(parent_r, targeter->FindTargetForEvent(root_target, &mouse2));
238 255
239 // Now install a targeter on the |child| that looks at the window id as well 256 // Now install a targeter on the |child| that looks at the window id as well
240 // as the bounds and makes sure the event reaches the target only if the id of 257 // as the bounds and makes sure the event reaches the target only if the id of
241 // the window is equal to 1 (correct). 258 // the window is equal to 1 (correct).
242 ui::MouseEvent mouse3(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), 259 ui::MouseEvent mouse3(
243 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 260 ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8),
261 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
262 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
244 child_r->SetEventTargeter( 263 child_r->SetEventTargeter(
245 std::unique_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(1))); 264 std::unique_ptr<ui::EventTargeter>(new IdCheckingEventTargeter(1)));
246 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &mouse3)); 265 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &mouse3));
247 266
248 // restore original WindowTargeter for |child|. 267 // restore original WindowTargeter for |child|.
249 child_r->SetEventTargeter(std::move(original_targeter)); 268 child_r->SetEventTargeter(std::move(original_targeter));
250 269
251 // Target |grandchild| location. 270 // Target |grandchild| location.
252 ui::MouseEvent second(ui::ET_MOUSE_MOVED, gfx::Point(12, 12), 271 ui::MouseEvent second(
253 gfx::Point(12, 12), ui::EventTimeForNow(), ui::EF_NONE, 272 ui::ET_MOUSE_MOVED, gfx::Point(12, 12), gfx::Point(12, 12),
254 ui::EF_NONE); 273 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
274 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
255 EXPECT_EQ(grandchild_r, targeter->FindTargetForEvent(root_target, &second)); 275 EXPECT_EQ(grandchild_r, targeter->FindTargetForEvent(root_target, &second));
256 276
257 // Target |child| location. 277 // Target |child| location.
258 ui::MouseEvent third(ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8), 278 ui::MouseEvent third(
259 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); 279 ui::ET_MOUSE_MOVED, gfx::Point(8, 8), gfx::Point(8, 8),
280 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
281 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
260 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &third)); 282 EXPECT_EQ(child_r, targeter->FindTargetForEvent(root_target, &third));
261 } 283 }
262 284
263 class IgnoreWindowTargeter : public WindowTargeter { 285 class IgnoreWindowTargeter : public WindowTargeter {
264 public: 286 public:
265 IgnoreWindowTargeter() {} 287 IgnoreWindowTargeter() {}
266 ~IgnoreWindowTargeter() override {} 288 ~IgnoreWindowTargeter() override {}
267 289
268 private: 290 private:
269 // WindowTargeter: 291 // WindowTargeter:
270 bool SubtreeShouldBeExploredForEvent(Window* window, 292 bool SubtreeShouldBeExploredForEvent(Window* window,
271 const ui::LocatedEvent& event) override { 293 const ui::LocatedEvent& event) override {
272 return false; 294 return false;
273 } 295 }
274 }; 296 };
275 297
276 // Verifies that an EventTargeter installed on an EventTarget can dictate 298 // Verifies that an EventTargeter installed on an EventTarget can dictate
277 // whether the target itself can process an event. 299 // whether the target itself can process an event.
278 TEST_P(WindowTargeterTest, TargeterChecksOwningEventTarget) { 300 TEST_P(WindowTargeterTest, TargeterChecksOwningEventTarget) {
279 test::TestWindowDelegate delegate; 301 test::TestWindowDelegate delegate;
280 std::unique_ptr<Window> child( 302 std::unique_ptr<Window> child(
281 CreateNormalWindow(1, root_window(), &delegate)); 303 CreateNormalWindow(1, root_window(), &delegate));
282 304
283 ui::EventTarget* root_target = root_window(); 305 ui::EventTarget* root_target = root_window();
284 ui::EventTargeter* targeter = root_target->GetEventTargeter(); 306 ui::EventTargeter* targeter = root_target->GetEventTargeter();
285 307
286 ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), 308 ui::MouseEvent mouse(
287 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, 309 ui::ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
288 ui::EF_NONE); 310 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
311 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
289 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse)); 312 EXPECT_EQ(child.get(), targeter->FindTargetForEvent(root_target, &mouse));
290 313
291 // Install an event targeter on |child| which always prevents the target from 314 // Install an event targeter on |child| which always prevents the target from
292 // receiving event. 315 // receiving event.
293 child->SetEventTargeter( 316 child->SetEventTargeter(
294 std::unique_ptr<ui::EventTargeter>(new IgnoreWindowTargeter())); 317 std::unique_ptr<ui::EventTargeter>(new IgnoreWindowTargeter()));
295 318
296 ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10), 319 ui::MouseEvent mouse2(
297 gfx::Point(10, 10), ui::EventTimeForNow(), ui::EF_NONE, 320 ui::ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10),
298 ui::EF_NONE); 321 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE,
322 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE));
299 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2)); 323 EXPECT_EQ(root_window(), targeter->FindTargetForEvent(root_target, &mouse2));
300 } 324 }
301 325
302 INSTANTIATE_TEST_CASE_P(/* no prefix */, 326 INSTANTIATE_TEST_CASE_P(/* no prefix */,
303 WindowTargeterTest, 327 WindowTargeterTest,
304 ::testing::Values(test::BackendType::CLASSIC, 328 ::testing::Values(test::BackendType::CLASSIC,
305 test::BackendType::MUS)); 329 test::BackendType::MUS));
306 330
307 } // namespace aura 331 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698