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

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

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

Powered by Google App Engine
This is Rietveld 408576698