| 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/views/test/views_test_base.h" | 9 #include "ui/views/test/views_test_base.h" | 
|  | 10 #include "ui/views/view_targeter.h" | 
| 10 #include "ui/views/widget/root_view.h" | 11 #include "ui/views/widget/root_view.h" | 
| 11 | 12 | 
| 12 namespace views { | 13 namespace views { | 
| 13 | 14 | 
| 14 // A derived class of View used for testing purposes. | 15 // A derived class of View used for testing purposes. | 
| 15 class TestingView : public View { | 16 class TestingView : public View { | 
| 16  public: | 17  public: | 
| 17   TestingView() : can_process_events_within_subtree_(true) {} | 18   TestingView() : can_process_events_within_subtree_(true) {} | 
| 18   virtual ~TestingView() {} | 19   virtual ~TestingView() {} | 
| 19 | 20 | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 54   View* child = new View; | 55   View* child = new View; | 
| 55   View* grandchild = new View; | 56   View* grandchild = new View; | 
| 56 | 57 | 
| 57   widget.SetContentsView(content); | 58   widget.SetContentsView(content); | 
| 58   content->AddChildView(child); | 59   content->AddChildView(child); | 
| 59   child->AddChildView(grandchild); | 60   child->AddChildView(grandchild); | 
| 60 | 61 | 
| 61   grandchild->SetFocusable(true); | 62   grandchild->SetFocusable(true); | 
| 62   grandchild->RequestFocus(); | 63   grandchild->RequestFocus(); | 
| 63 | 64 | 
| 64   ui::EventTargeter* targeter = new ViewTargeter(); | 65   ViewTargeter* view_targeter = new ViewTargeter(); | 
|  | 66   ui::EventTargeter* targeter = view_targeter; | 
| 65   internal::RootView* root_view = | 67   internal::RootView* root_view = | 
| 66       static_cast<internal::RootView*>(widget.GetRootView()); | 68       static_cast<internal::RootView*>(widget.GetRootView()); | 
| 67   root_view->SetEventTargeter(make_scoped_ptr(targeter)); | 69   root_view->SetEventTargeter(make_scoped_ptr(view_targeter)); | 
| 68 | 70 | 
| 69   ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, true); | 71   ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, true); | 
| 70 | 72 | 
| 71   // The focused view should be the initial target of the event. | 73   // The focused view should be the initial target of the event. | 
| 72   ui::EventTarget* current_target = targeter->FindTargetForEvent(root_view, | 74   ui::EventTarget* current_target = targeter->FindTargetForEvent(root_view, | 
| 73                                                                  &key_event); | 75                                                                  &key_event); | 
| 74   EXPECT_EQ(grandchild, static_cast<View*>(current_target)); | 76   EXPECT_EQ(grandchild, static_cast<View*>(current_target)); | 
| 75 | 77 | 
| 76   // Verify that FindNextBestTarget() will return the parent view of the | 78   // Verify that FindNextBestTarget() will return the parent view of the | 
| 77   // argument (and NULL if the argument has no parent view). | 79   // argument (and NULL if the argument has no parent view). | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
| 102   content->SetBounds(0, 0, 100, 100); | 104   content->SetBounds(0, 0, 100, 100); | 
| 103   View* child = new View; | 105   View* child = new View; | 
| 104   child->SetBounds(50, 50, 20, 20); | 106   child->SetBounds(50, 50, 20, 20); | 
| 105   View* grandchild = new View; | 107   View* grandchild = new View; | 
| 106   grandchild->SetBounds(0, 0, 5, 5); | 108   grandchild->SetBounds(0, 0, 5, 5); | 
| 107 | 109 | 
| 108   widget.SetContentsView(content); | 110   widget.SetContentsView(content); | 
| 109   content->AddChildView(child); | 111   content->AddChildView(child); | 
| 110   child->AddChildView(grandchild); | 112   child->AddChildView(grandchild); | 
| 111 | 113 | 
| 112   ui::EventTargeter* targeter = new ViewTargeter(); | 114   ViewTargeter* view_targeter = new ViewTargeter(); | 
|  | 115   ui::EventTargeter* targeter = view_targeter; | 
| 113   internal::RootView* root_view = | 116   internal::RootView* root_view = | 
| 114       static_cast<internal::RootView*>(widget.GetRootView()); | 117       static_cast<internal::RootView*>(widget.GetRootView()); | 
| 115   root_view->SetEventTargeter(make_scoped_ptr(targeter)); | 118   root_view->SetEventTargeter(make_scoped_ptr(view_targeter)); | 
| 116 | 119 | 
| 117   // The event falls within the bounds of |child| and |content| but not | 120   // The event falls within the bounds of |child| and |content| but not | 
| 118   // |grandchild|, so |child| should be the initial target for the event. | 121   // |grandchild|, so |child| should be the initial target for the event. | 
| 119   ui::ScrollEvent scroll(ui::ET_SCROLL, | 122   ui::ScrollEvent scroll(ui::ET_SCROLL, | 
| 120                          gfx::Point(60, 60), | 123                          gfx::Point(60, 60), | 
| 121                          ui::EventTimeForNow(), | 124                          ui::EventTimeForNow(), | 
| 122                          0, | 125                          0, | 
| 123                          0, 3, | 126                          0, 3, | 
| 124                          0, 3, | 127                          0, 3, | 
| 125                          2); | 128                          2); | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
| 154 | 157 | 
| 155 // Tests the basic functionality of the method | 158 // Tests the basic functionality of the method | 
| 156 // ViewTargeter::SubtreeShouldBeExploredForEvent(). | 159 // ViewTargeter::SubtreeShouldBeExploredForEvent(). | 
| 157 TEST_F(ViewTargeterTest, SubtreeShouldBeExploredForEvent) { | 160 TEST_F(ViewTargeterTest, SubtreeShouldBeExploredForEvent) { | 
| 158   Widget widget; | 161   Widget widget; | 
| 159   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 162   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 
| 160   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 163   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 
| 161   params.bounds = gfx::Rect(0, 0, 650, 650); | 164   params.bounds = gfx::Rect(0, 0, 650, 650); | 
| 162   widget.Init(params); | 165   widget.Init(params); | 
| 163 | 166 | 
| 164   ui::EventTargeter* targeter = new ViewTargeter(); | 167   ViewTargeter* targeter = new ViewTargeter(); | 
| 165   internal::RootView* root_view = | 168   internal::RootView* root_view = | 
| 166       static_cast<internal::RootView*>(widget.GetRootView()); | 169       static_cast<internal::RootView*>(widget.GetRootView()); | 
| 167   root_view->SetEventTargeter(make_scoped_ptr(targeter)); | 170   root_view->SetEventTargeter(make_scoped_ptr(targeter)); | 
| 168 | 171 | 
| 169   // The coordinates used for SetBounds() are in the parent coordinate space. | 172   // The coordinates used for SetBounds() are in the parent coordinate space. | 
| 170   View v1, v2, v3; | 173   View v1, v2, v3; | 
| 171   v1.SetBounds(0, 0, 300, 300); | 174   v1.SetBounds(0, 0, 300, 300); | 
| 172   v2.SetBounds(100, 100, 100, 100); | 175   v2.SetBounds(100, 100, 100, 100); | 
| 173   v3.SetBounds(0, 0, 10, 10); | 176   v3.SetBounds(0, 0, 10, 10); | 
| 174   v3.SetVisible(false); | 177   v3.SetVisible(false); | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 219 // Tests that FindTargetForEvent() returns the correct target when some | 222 // Tests that FindTargetForEvent() returns the correct target when some | 
| 220 // views in the view tree return false when CanProcessEventsWithinSubtree() | 223 // views in the view tree return false when CanProcessEventsWithinSubtree() | 
| 221 // is called on them. | 224 // is called on them. | 
| 222 TEST_F(ViewTargeterTest, CanProcessEventsWithinSubtree) { | 225 TEST_F(ViewTargeterTest, CanProcessEventsWithinSubtree) { | 
| 223   Widget widget; | 226   Widget widget; | 
| 224   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 227   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 
| 225   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 228   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 
| 226   params.bounds = gfx::Rect(0, 0, 650, 650); | 229   params.bounds = gfx::Rect(0, 0, 650, 650); | 
| 227   widget.Init(params); | 230   widget.Init(params); | 
| 228 | 231 | 
| 229   ui::EventTargeter* targeter = new ViewTargeter(); | 232   ViewTargeter* view_targeter = new ViewTargeter(); | 
|  | 233   ui::EventTargeter* targeter = view_targeter; | 
| 230   internal::RootView* root_view = | 234   internal::RootView* root_view = | 
| 231       static_cast<internal::RootView*>(widget.GetRootView()); | 235       static_cast<internal::RootView*>(widget.GetRootView()); | 
| 232   root_view->SetEventTargeter(make_scoped_ptr(targeter)); | 236   root_view->SetEventTargeter(make_scoped_ptr(view_targeter)); | 
| 233 | 237 | 
| 234   // The coordinates used for SetBounds() are in the parent coordinate space. | 238   // The coordinates used for SetBounds() are in the parent coordinate space. | 
| 235   TestingView v1, v2, v3; | 239   TestingView v1, v2, v3; | 
| 236   v1.SetBounds(0, 0, 300, 300); | 240   v1.SetBounds(0, 0, 300, 300); | 
| 237   v2.SetBounds(100, 100, 100, 100); | 241   v2.SetBounds(100, 100, 100, 100); | 
| 238   v3.SetBounds(0, 0, 10, 10); | 242   v3.SetBounds(0, 0, 10, 10); | 
| 239   root_view->AddChildView(&v1); | 243   root_view->AddChildView(&v1); | 
| 240   v1.AddChildView(&v2); | 244   v1.AddChildView(&v2); | 
| 241   v2.AddChildView(&v3); | 245   v2.AddChildView(&v3); | 
| 242 | 246 | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 283   v1.set_can_process_events_within_subtree(false); | 287   v1.set_can_process_events_within_subtree(false); | 
| 284   current_target = targeter->FindTargetForEvent(root_view, &scroll); | 288   current_target = targeter->FindTargetForEvent(root_view, &scroll); | 
| 285   EXPECT_EQ(root_view, current_target); | 289   EXPECT_EQ(root_view, current_target); | 
| 286 | 290 | 
| 287   // TODO(tdanderson): We should also test that targeting works correctly | 291   // TODO(tdanderson): We should also test that targeting works correctly | 
| 288   //                   with gestures. See crbug.com/375822. | 292   //                   with gestures. See crbug.com/375822. | 
| 289 } | 293 } | 
| 290 | 294 | 
| 291 }  // namespace test | 295 }  // namespace test | 
| 292 }  // namespace views | 296 }  // namespace views | 
| OLD | NEW | 
|---|