OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "ui/events/keycodes/keyboard_codes.h" | 24 #include "ui/events/keycodes/keyboard_codes.h" |
25 #include "ui/gfx/canvas.h" | 25 #include "ui/gfx/canvas.h" |
26 #include "ui/gfx/path.h" | 26 #include "ui/gfx/path.h" |
27 #include "ui/gfx/transform.h" | 27 #include "ui/gfx/transform.h" |
28 #include "ui/strings/grit/ui_strings.h" | 28 #include "ui/strings/grit/ui_strings.h" |
29 #include "ui/views/background.h" | 29 #include "ui/views/background.h" |
30 #include "ui/views/controls/native/native_view_host.h" | 30 #include "ui/views/controls/native/native_view_host.h" |
31 #include "ui/views/controls/scroll_view.h" | 31 #include "ui/views/controls/scroll_view.h" |
32 #include "ui/views/controls/textfield/textfield.h" | 32 #include "ui/views/controls/textfield/textfield.h" |
33 #include "ui/views/focus/view_storage.h" | 33 #include "ui/views/focus/view_storage.h" |
| 34 #include "ui/views/scoped_target_handler.h" |
34 #include "ui/views/test/views_test_base.h" | 35 #include "ui/views/test/views_test_base.h" |
35 #include "ui/views/view.h" | 36 #include "ui/views/view.h" |
36 #include "ui/views/widget/native_widget.h" | 37 #include "ui/views/widget/native_widget.h" |
37 #include "ui/views/widget/root_view.h" | 38 #include "ui/views/widget/root_view.h" |
38 #include "ui/views/window/dialog_client_view.h" | 39 #include "ui/views/window/dialog_client_view.h" |
39 #include "ui/views/window/dialog_delegate.h" | 40 #include "ui/views/window/dialog_delegate.h" |
40 | 41 |
41 using base::ASCIIToUTF16; | 42 using base::ASCIIToUTF16; |
42 | 43 |
43 namespace { | 44 namespace { |
(...skipping 4087 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4131 // Child view added after the widget hierarchy exists should also get the | 4132 // Child view added after the widget hierarchy exists should also get the |
4132 // notification. | 4133 // notification. |
4133 TestView* test_view_child_2 = new TestView(); | 4134 TestView* test_view_child_2 = new TestView(); |
4134 test_view->AddChildView(test_view_child_2); | 4135 test_view->AddChildView(test_view_child_2); |
4135 EXPECT_TRUE(test_view_child_2->native_theme_); | 4136 EXPECT_TRUE(test_view_child_2->native_theme_); |
4136 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_); | 4137 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_); |
4137 | 4138 |
4138 widget->CloseNow(); | 4139 widget->CloseNow(); |
4139 } | 4140 } |
4140 | 4141 |
| 4142 class TestEventHandler : public ui::EventHandler { |
| 4143 public: |
| 4144 TestEventHandler(TestView* view) : view_(view), had_mouse_event_(false) {} |
| 4145 ~TestEventHandler() override {} |
| 4146 |
| 4147 void OnMouseEvent(ui::MouseEvent* event) override { |
| 4148 // The |view_| should have received the event first. |
| 4149 EXPECT_EQ(ui::ET_MOUSE_PRESSED, view_->last_mouse_event_type_); |
| 4150 had_mouse_event_ = true; |
| 4151 } |
| 4152 |
| 4153 TestView* view_; |
| 4154 bool had_mouse_event_; |
| 4155 }; |
| 4156 |
| 4157 TEST_F(ViewTest, ScopedTargetHandlerReceivesEvents) { |
| 4158 TestView* v = new TestView(); |
| 4159 v->SetBoundsRect(gfx::Rect(0, 0, 300, 300)); |
| 4160 |
| 4161 scoped_ptr<Widget> widget(new Widget); |
| 4162 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 4163 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 4164 params.bounds = gfx::Rect(50, 50, 350, 350); |
| 4165 widget->Init(params); |
| 4166 internal::RootView* root = |
| 4167 static_cast<internal::RootView*>(widget->GetRootView()); |
| 4168 root->AddChildView(v); |
| 4169 v->Reset(); |
| 4170 TestEventHandler handler(v); |
| 4171 { |
| 4172 ScopedTargetHandler scoped_target_handler(v, &handler); |
| 4173 // View's target EventHandler should be set to the |scoped_target_handler|. |
| 4174 EXPECT_EQ(&scoped_target_handler, |
| 4175 v->SetTargetHandler(&scoped_target_handler)); |
| 4176 |
| 4177 EXPECT_EQ(ui::ET_UNKNOWN, v->last_mouse_event_type_); |
| 4178 gfx::Point p(10, 120); |
| 4179 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p, p, ui::EventTimeForNow(), |
| 4180 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 4181 root->OnMousePressed(pressed); |
| 4182 |
| 4183 // Both the View |v| and the |handler| should have received the event. |
| 4184 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v->last_mouse_event_type_); |
| 4185 EXPECT_TRUE(handler.had_mouse_event_); |
| 4186 } |
| 4187 |
| 4188 // The View should no longer have a target EventHandler. |
| 4189 EXPECT_EQ(nullptr, v->SetTargetHandler(nullptr)); |
| 4190 |
| 4191 // The View should continue receiving events after the |handler| is deleted. |
| 4192 v->Reset(); |
| 4193 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), |
| 4194 ui::EventTimeForNow(), 0, 0); |
| 4195 root->OnMouseReleased(released); |
| 4196 EXPECT_EQ(ui::ET_MOUSE_RELEASED, v->last_mouse_event_type_); |
| 4197 } |
| 4198 |
4141 } // namespace views | 4199 } // namespace views |
OLD | NEW |