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 "ui/views/controls/button/custom_button.h" | 5 #include "ui/views/controls/button/custom_button.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "ui/base/layout.h" | 10 #include "ui/base/layout.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 } | 49 } |
50 | 50 |
51 bool pressed() { return pressed_; } | 51 bool pressed() { return pressed_; } |
52 bool canceled() { return canceled_; } | 52 bool canceled() { return canceled_; } |
53 | 53 |
54 void Reset() { | 54 void Reset() { |
55 pressed_ = false; | 55 pressed_ = false; |
56 canceled_ = false; | 56 canceled_ = false; |
57 } | 57 } |
58 | 58 |
59 // CustomButton methods: | |
60 bool IsChildWidget() const override { return is_child_widget_; } | |
61 bool FocusInChildWidget() const override { return focus_in_child_widget_; } | |
62 | |
63 void set_child_widget(bool b) { is_child_widget_ = b; } | |
64 void set_focus_in_child_widget(bool b) { focus_in_child_widget_ = b; } | |
65 | |
66 private: | 59 private: |
67 bool pressed_ = false; | 60 bool pressed_ = false; |
68 bool canceled_ = false; | 61 bool canceled_ = false; |
69 bool is_child_widget_ = false; | |
70 bool focus_in_child_widget_ = false; | |
71 | 62 |
72 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); | 63 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); |
73 }; | 64 }; |
74 | 65 |
75 class TestWidget : public Widget { | |
76 public: | |
77 TestWidget() : Widget() {} | |
78 | |
79 // Widget method: | |
80 bool IsActive() const override { return active_; } | |
81 | |
82 void set_active(bool active) { active_ = active; } | |
83 | |
84 private: | |
85 bool active_ = false; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(TestWidget); | |
88 }; | |
89 | |
90 // An InkDropDelegate that keeps track of ink drop visibility. | 66 // An InkDropDelegate that keeps track of ink drop visibility. |
91 class TestInkDropDelegate : public InkDropDelegate { | 67 class TestInkDropDelegate : public InkDropDelegate { |
92 public: | 68 public: |
93 TestInkDropDelegate(InkDropHost* ink_drop_host, | 69 TestInkDropDelegate(InkDropHost* ink_drop_host, |
94 bool* ink_shown, | 70 bool* ink_shown, |
95 bool* ink_hidden) | 71 bool* ink_hidden) |
96 : ink_drop_host_(ink_drop_host), | 72 : ink_drop_host_(ink_drop_host), |
97 ink_shown_(ink_shown), | 73 ink_shown_(ink_shown), |
98 ink_hidden_(ink_hidden) { | 74 ink_hidden_(ink_hidden) { |
99 ink_drop_host_->AddInkDropLayer(nullptr); | 75 ink_drop_host_->AddInkDropLayer(nullptr); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 class CustomButtonTest : public ViewsTestBase { | 132 class CustomButtonTest : public ViewsTestBase { |
157 public: | 133 public: |
158 CustomButtonTest() {} | 134 CustomButtonTest() {} |
159 ~CustomButtonTest() override {} | 135 ~CustomButtonTest() override {} |
160 | 136 |
161 void SetUp() override { | 137 void SetUp() override { |
162 ViewsTestBase::SetUp(); | 138 ViewsTestBase::SetUp(); |
163 | 139 |
164 // Create a widget so that the CustomButton can query the hover state | 140 // Create a widget so that the CustomButton can query the hover state |
165 // correctly. | 141 // correctly. |
166 widget_.reset(new TestWidget); | 142 widget_.reset(new Widget); |
167 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 143 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
168 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 144 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
169 params.bounds = gfx::Rect(0, 0, 650, 650); | 145 params.bounds = gfx::Rect(0, 0, 650, 650); |
170 widget_->Init(params); | 146 widget_->Init(params); |
171 widget_->Show(); | 147 widget_->Show(); |
172 | 148 |
173 button_ = new TestCustomButton(); | 149 button_ = new TestCustomButton(); |
174 widget_->SetContentsView(button_); | 150 widget_->SetContentsView(button_); |
175 } | 151 } |
176 | 152 |
177 void TearDown() override { | 153 void TearDown() override { |
178 widget_.reset(); | 154 widget_.reset(); |
179 ViewsTestBase::TearDown(); | 155 ViewsTestBase::TearDown(); |
180 } | 156 } |
181 | 157 |
182 void CreateButtonWithInkDrop() { | 158 void CreateButtonWithInkDrop() { |
183 delete button_; | 159 delete button_; |
184 ink_shown_ = false; | 160 ink_shown_ = false; |
185 ink_hidden_ = false; | 161 ink_hidden_ = false; |
186 button_ = new TestButtonWithInkDrop(&ink_shown_, &ink_hidden_); | 162 button_ = new TestButtonWithInkDrop(&ink_shown_, &ink_hidden_); |
187 widget_->SetContentsView(button_); | 163 widget_->SetContentsView(button_); |
188 } | 164 } |
189 | 165 |
190 protected: | 166 protected: |
191 TestWidget* widget() { return widget_.get(); } | 167 Widget* widget() { return widget_.get(); } |
192 TestCustomButton* button() { return button_; } | 168 TestCustomButton* button() { return button_; } |
193 bool ink_shown() const { return ink_shown_; } | 169 bool ink_shown() const { return ink_shown_; } |
194 bool ink_hidden() const { return ink_hidden_; } | 170 bool ink_hidden() const { return ink_hidden_; } |
195 | 171 |
196 private: | 172 private: |
197 scoped_ptr<TestWidget> widget_; | 173 scoped_ptr<Widget> widget_; |
198 TestCustomButton* button_; | 174 TestCustomButton* button_; |
199 bool ink_shown_ = false; | 175 bool ink_shown_ = false; |
200 bool ink_hidden_ = false; | 176 bool ink_hidden_ = false; |
201 | 177 |
202 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); | 178 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); |
203 }; | 179 }; |
204 | 180 |
205 } // namespace | 181 } // namespace |
206 | 182 |
207 // Tests that hover state changes correctly when visiblity/enableness changes. | 183 // Tests that hover state changes correctly when visiblity/enableness changes. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 283 |
308 // The button should no longer notify on mouse release. | 284 // The button should no longer notify on mouse release. |
309 button()->Reset(); | 285 button()->Reset(); |
310 button()->OnMouseReleased(ui::MouseEvent( | 286 button()->OnMouseReleased(ui::MouseEvent( |
311 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(), | 287 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(), |
312 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | 288 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
313 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); | 289 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); |
314 EXPECT_FALSE(button()->pressed()); | 290 EXPECT_FALSE(button()->pressed()); |
315 } | 291 } |
316 | 292 |
317 TEST_F(CustomButtonTest, HandleAccelerator) { | |
318 // Child widgets shouldn't handle accelerators when they are not focused. | |
319 EXPECT_FALSE(button()->IsChildWidget()); | |
320 EXPECT_FALSE(button()->FocusInChildWidget()); | |
321 EXPECT_FALSE(widget()->IsActive()); | |
322 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); | |
323 EXPECT_FALSE(button()->pressed()); | |
324 // Child without focus. | |
325 button()->set_child_widget(true); | |
326 button()->set_focus_in_child_widget(false); | |
327 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); | |
328 EXPECT_FALSE(button()->pressed()); | |
329 button()->Reset(); | |
330 // Child with focus. | |
331 button()->set_child_widget(true); | |
332 button()->set_focus_in_child_widget(true); | |
333 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); | |
334 EXPECT_TRUE(button()->pressed()); | |
335 button()->Reset(); | |
336 // Not a child, but active. | |
337 button()->set_child_widget(false); | |
338 button()->set_focus_in_child_widget(true); | |
339 widget()->set_active(true); | |
340 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); | |
341 EXPECT_TRUE(button()->pressed()); | |
342 } | |
343 | |
344 // Tests that OnClickCanceled gets called when NotifyClick is not expected | 293 // Tests that OnClickCanceled gets called when NotifyClick is not expected |
345 // anymore. | 294 // anymore. |
346 TEST_F(CustomButtonTest, NotifyActionNoClick) { | 295 TEST_F(CustomButtonTest, NotifyActionNoClick) { |
347 gfx::Point center(10, 10); | 296 gfx::Point center(10, 10); |
348 | 297 |
349 // By default the button should notify its listener on mouse release. | 298 // By default the button should notify its listener on mouse release. |
350 button()->OnMousePressed(ui::MouseEvent( | 299 button()->OnMousePressed(ui::MouseEvent( |
351 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), | 300 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), |
352 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON)); | 301 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON)); |
353 EXPECT_FALSE(button()->canceled()); | 302 EXPECT_FALSE(button()->canceled()); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 generator.PressLeftButton(); | 415 generator.PressLeftButton(); |
467 EXPECT_TRUE(ink_shown()); | 416 EXPECT_TRUE(ink_shown()); |
468 EXPECT_FALSE(ink_hidden()); | 417 EXPECT_FALSE(ink_hidden()); |
469 | 418 |
470 widget()->SetCapture(button()); | 419 widget()->SetCapture(button()); |
471 widget()->ReleaseCapture(); | 420 widget()->ReleaseCapture(); |
472 EXPECT_TRUE(ink_hidden()); | 421 EXPECT_TRUE(ink_hidden()); |
473 } | 422 } |
474 | 423 |
475 } // namespace views | 424 } // namespace views |
OLD | NEW |