OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/views/view_targeter.h" | 5 #include "ui/views/view_targeter.h" |
6 | 6 |
7 #include "ui/events/event_targeter.h" | 7 #include "ui/events/event_targeter.h" |
8 #include "ui/events/event_utils.h" | 8 #include "ui/events/event_utils.h" |
9 #include "ui/gfx/path.h" | 9 #include "ui/gfx/path.h" |
10 #include "ui/views/masked_targeter_delegate.h" | 10 #include "ui/views/masked_targeter_delegate.h" |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 gfx::Point(150, 150), | 201 gfx::Point(150, 150), |
202 ui::EventTimeForNow(), | 202 ui::EventTimeForNow(), |
203 0, | 203 0, |
204 0, 3, | 204 0, 3, |
205 0, 3, | 205 0, 3, |
206 2); | 206 2); |
207 current_target = targeter->FindTargetForEvent(root_view, &scroll); | 207 current_target = targeter->FindTargetForEvent(root_view, &scroll); |
208 EXPECT_EQ(content, static_cast<View*>(current_target)); | 208 EXPECT_EQ(content, static_cast<View*>(current_target)); |
209 } | 209 } |
210 | 210 |
211 // Tests the basic functionality of the method | |
212 // ViewTargeter::SubtreeShouldBeExploredForEvent(). | |
213 TEST_F(ViewTargeterTest, SubtreeShouldBeExploredForEvent) { | |
214 Widget widget; | |
215 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
216 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
217 params.bounds = gfx::Rect(0, 0, 650, 650); | |
218 widget.Init(params); | |
219 | |
220 internal::RootView* root_view = | |
221 static_cast<internal::RootView*>(widget.GetRootView()); | |
222 ViewTargeter* targeter = new ViewTargeter(root_view); | |
223 root_view->SetEventTargeter(make_scoped_ptr(targeter)); | |
224 | |
225 // The coordinates used for SetBounds() are in the parent coordinate space. | |
226 View v1, v2, v3; | |
227 v1.SetBounds(0, 0, 300, 300); | |
228 v2.SetBounds(100, 100, 100, 100); | |
229 v3.SetBounds(0, 0, 10, 10); | |
230 v3.SetVisible(false); | |
231 root_view->AddChildView(&v1); | |
232 v1.AddChildView(&v2); | |
233 v2.AddChildView(&v3); | |
234 | |
235 // Note that the coordinates used below are in |v1|'s coordinate space, | |
236 // and that SubtreeShouldBeExploredForEvent() expects the event location | |
237 // to be in the coordinate space of the target's parent. |v1| and | |
238 // its parent share a common coordinate space. | |
239 | |
240 // Event located within |v1| only. | |
241 gfx::Point point(10, 10); | |
242 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, point, point, | |
243 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | |
244 EXPECT_TRUE(targeter->SubtreeShouldBeExploredForEvent(&v1, event)); | |
245 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v2, event)); | |
246 v1.ConvertEventToTarget(&v2, &event); | |
247 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v3, event)); | |
248 | |
249 // Event located within |v1| and |v2| only. | |
250 event.set_location(gfx::Point(150, 150)); | |
251 EXPECT_TRUE(targeter->SubtreeShouldBeExploredForEvent(&v1, event)); | |
252 EXPECT_TRUE(targeter->SubtreeShouldBeExploredForEvent(&v2, event)); | |
253 v1.ConvertEventToTarget(&v2, &event); | |
254 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v3, event)); | |
255 | |
256 // Event located within |v1|, |v2|, and |v3|. Note that |v3| is not | |
257 // visible, so it cannot handle the event. | |
258 event.set_location(gfx::Point(105, 105)); | |
259 EXPECT_TRUE(targeter->SubtreeShouldBeExploredForEvent(&v1, event)); | |
260 EXPECT_TRUE(targeter->SubtreeShouldBeExploredForEvent(&v2, event)); | |
261 v1.ConvertEventToTarget(&v2, &event); | |
262 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v3, event)); | |
263 | |
264 // Event located outside the bounds of all views. | |
265 event.set_location(gfx::Point(400, 400)); | |
266 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v1, event)); | |
267 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v2, event)); | |
268 v1.ConvertEventToTarget(&v2, &event); | |
269 EXPECT_FALSE(targeter->SubtreeShouldBeExploredForEvent(&v3, event)); | |
270 } | |
271 | |
272 // Tests that FindTargetForEvent() returns the correct target when some | |
273 // views in the view tree return false when CanProcessEventsWithinSubtree() | |
274 // is called on them. | |
275 TEST_F(ViewTargeterTest, CanProcessEventsWithinSubtree) { | |
276 Widget widget; | |
277 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
278 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
279 params.bounds = gfx::Rect(0, 0, 650, 650); | |
280 widget.Init(params); | |
281 | |
282 internal::RootView* root_view = | |
283 static_cast<internal::RootView*>(widget.GetRootView()); | |
284 ViewTargeter* view_targeter = new ViewTargeter(root_view); | |
285 ui::EventTargeter* targeter = view_targeter; | |
286 root_view->SetEventTargeter(make_scoped_ptr(view_targeter)); | |
287 | |
288 // The coordinates used for SetBounds() are in the parent coordinate space. | |
289 TestingView v1, v2, v3; | |
290 v1.SetBounds(0, 0, 300, 300); | |
291 v2.SetBounds(100, 100, 100, 100); | |
292 v3.SetBounds(0, 0, 10, 10); | |
293 root_view->AddChildView(&v1); | |
294 v1.AddChildView(&v2); | |
295 v2.AddChildView(&v3); | |
296 | |
297 // Note that the coordinates used below are in the coordinate space of | |
298 // the root view. | |
299 | |
300 // Define |scroll| to be (105, 105) (in the coordinate space of the root | |
301 // view). This is located within all of |v1|, |v2|, and |v3|. | |
302 gfx::Point scroll_point(105, 105); | |
303 ui::ScrollEvent scroll( | |
304 ui::ET_SCROLL, scroll_point, ui::EventTimeForNow(), 0, 0, 3, 0, 3, 2); | |
305 | |
306 // If CanProcessEventsWithinSubtree() returns true for each view, | |
307 // |scroll| should be targeted at the deepest view in the hierarchy, | |
308 // which is |v3|. | |
309 ui::EventTarget* current_target = | |
310 targeter->FindTargetForEvent(root_view, &scroll); | |
311 EXPECT_EQ(&v3, current_target); | |
312 | |
313 // If CanProcessEventsWithinSubtree() returns |false| when called | |
314 // on |v3|, then |v3| cannot be the target of |scroll| (this should | |
315 // instead be |v2|). Note we need to reset the location of |scroll| | |
316 // because it may have been mutated by the previous call to | |
317 // FindTargetForEvent(). | |
318 scroll.set_location(scroll_point); | |
319 v3.set_can_process_events_within_subtree(false); | |
320 current_target = targeter->FindTargetForEvent(root_view, &scroll); | |
321 EXPECT_EQ(&v2, current_target); | |
322 | |
323 // If CanProcessEventsWithinSubtree() returns |false| when called | |
324 // on |v2|, then neither |v2| nor |v3| can be the target of |scroll| | |
325 // (this should instead be |v1|). | |
326 scroll.set_location(scroll_point); | |
327 v3.Reset(); | |
328 v2.set_can_process_events_within_subtree(false); | |
329 current_target = targeter->FindTargetForEvent(root_view, &scroll); | |
330 EXPECT_EQ(&v1, current_target); | |
331 | |
332 // If CanProcessEventsWithinSubtree() returns |false| when called | |
333 // on |v1|, then none of |v1|, |v2| or |v3| can be the target of |scroll| | |
334 // (this should instead be the root view itself). | |
335 scroll.set_location(scroll_point); | |
336 v2.Reset(); | |
337 v1.set_can_process_events_within_subtree(false); | |
338 current_target = targeter->FindTargetForEvent(root_view, &scroll); | |
339 EXPECT_EQ(root_view, current_target); | |
340 | |
341 // TODO(tdanderson): We should also test that targeting works correctly | |
342 // with gestures. See crbug.com/375822. | |
343 } | |
344 | |
345 // Tests that the functions ViewTargeterDelegate::DoesIntersectRect() | 211 // Tests that the functions ViewTargeterDelegate::DoesIntersectRect() |
346 // and MaskedTargeterDelegate::DoesIntersectRect() work as intended when | 212 // and MaskedTargeterDelegate::DoesIntersectRect() work as intended when |
347 // called on views which are derived from ViewTargeterDelegate. | 213 // called on views which are derived from ViewTargeterDelegate. |
348 // Also verifies that ViewTargeterDelegate::DoesIntersectRect() can | 214 // Also verifies that ViewTargeterDelegate::DoesIntersectRect() can |
349 // be called from the ViewTargeter installed on RootView. | 215 // be called from the ViewTargeter installed on RootView. |
350 TEST_F(ViewTargeterTest, DoesIntersectRect) { | 216 TEST_F(ViewTargeterTest, DoesIntersectRect) { |
351 Widget widget; | 217 Widget widget; |
352 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 218 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
353 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 219 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
354 params.bounds = gfx::Rect(0, 0, 650, 650); | 220 params.bounds = gfx::Rect(0, 0, 650, 650); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin)); | 337 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin)); |
472 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin)); | 338 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin)); |
473 | 339 |
474 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin)); | 340 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin)); |
475 | 341 |
476 widget->CloseNow(); | 342 widget->CloseNow(); |
477 } | 343 } |
478 | 344 |
479 } // namespace test | 345 } // namespace test |
480 } // namespace views | 346 } // namespace views |
OLD | NEW |