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

Side by Side Diff: ui/views/controls/button/custom_button_unittest.cc

Issue 1437523005: Custom buttons should only handle accelerators when focused. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase & style Created 5 years, 1 month 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) 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 "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/base/layout.h" 8 #include "ui/base/layout.h"
9 #include "ui/events/event_utils.h" 9 #include "ui/events/event_utils.h"
10 #include "ui/gfx/screen.h" 10 #include "ui/gfx/screen.h"
(...skipping 25 matching lines...) Expand all
36 ~TestCustomButton() override {} 36 ~TestCustomButton() override {}
37 37
38 void ButtonPressed(Button* sender, const ui::Event& event) override { 38 void ButtonPressed(Button* sender, const ui::Event& event) override {
39 notified_ = true; 39 notified_ = true;
40 } 40 }
41 41
42 bool notified() { return notified_; } 42 bool notified() { return notified_; }
43 43
44 void Reset() { notified_ = false; } 44 void Reset() { notified_ = false; }
45 45
46 // CustomButton methods:
47 bool IsChildWidget() const override { return is_child_widget_; }
48 bool FocusInChildWidget() const override { return focus_in_child_widget_; }
49
50 void set_child_widget(bool b) { is_child_widget_ = b; }
51 void set_focus_in_child_widget(bool b) { focus_in_child_widget_ = b; }
52
46 private: 53 private:
47 bool notified_ = false; 54 bool notified_ = false;
55 bool is_child_widget_ = false;
56 bool focus_in_child_widget_ = false;
48 57
49 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); 58 DISALLOW_COPY_AND_ASSIGN(TestCustomButton);
50 }; 59 };
51 60
61 class TestWidget : public Widget {
62 public:
63 TestWidget() : Widget() {}
64
65 // Widget method:
66 bool IsActive() const override { return active_; }
67
68 void set_active(bool active) { active_ = active; }
69
70 private:
71 bool active_ = false;
72
73 DISALLOW_COPY_AND_ASSIGN(TestWidget);
74 };
75
52 class CustomButtonTest : public ViewsTestBase { 76 class CustomButtonTest : public ViewsTestBase {
53 public: 77 public:
54 CustomButtonTest() {} 78 CustomButtonTest() {}
55 ~CustomButtonTest() override {} 79 ~CustomButtonTest() override {}
56 80
57 void SetUp() override { 81 void SetUp() override {
58 ViewsTestBase::SetUp(); 82 ViewsTestBase::SetUp();
59 83
60 // Create a widget so that the CustomButton can query the hover state 84 // Create a widget so that the CustomButton can query the hover state
61 // correctly. 85 // correctly.
62 widget_.reset(new Widget); 86 widget_.reset(new TestWidget);
63 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 87 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
64 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 88 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
65 params.bounds = gfx::Rect(0, 0, 650, 650); 89 params.bounds = gfx::Rect(0, 0, 650, 650);
66 widget_->Init(params); 90 widget_->Init(params);
67 widget_->Show(); 91 widget_->Show();
68 92
69 // Position the widget in a way so that it is under the cursor. 93 // Position the widget in a way so that it is under the cursor.
70 gfx::Point cursor = gfx::Screen::GetScreenFor( 94 gfx::Point cursor = gfx::Screen::GetScreenFor(
71 widget_->GetNativeView())->GetCursorScreenPoint(); 95 widget_->GetNativeView())->GetCursorScreenPoint();
72 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); 96 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen();
73 widget_bounds.set_origin(cursor); 97 widget_bounds.set_origin(cursor);
74 widget_->SetBounds(widget_bounds); 98 widget_->SetBounds(widget_bounds);
75 99
76 button_ = new TestCustomButton(); 100 button_ = new TestCustomButton();
77 widget_->SetContentsView(button_); 101 widget_->SetContentsView(button_);
78 } 102 }
79 103
80 void TearDown() override { 104 void TearDown() override {
81 widget_.reset(); 105 widget_.reset();
82 ViewsTestBase::TearDown(); 106 ViewsTestBase::TearDown();
83 } 107 }
84 108
85 Widget* widget() { return widget_.get(); } 109 TestWidget* widget() { return widget_.get(); }
86 TestCustomButton* button() { return button_; } 110 TestCustomButton* button() { return button_; }
87 111
88 private: 112 private:
89 scoped_ptr<Widget> widget_; 113 scoped_ptr<TestWidget> widget_;
90 TestCustomButton* button_; 114 TestCustomButton* button_;
91 115
92 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); 116 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest);
93 }; 117 };
94 118
95 } // namespace 119 } // namespace
96 120
97 // Tests that hover state changes correctly when visiblity/enableness changes. 121 // Tests that hover state changes correctly when visiblity/enableness changes.
98 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { 122 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) {
99 gfx::Point center(10, 10); 123 gfx::Point center(10, 10);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 221
198 // The button should no longer notify on mouse release. 222 // The button should no longer notify on mouse release.
199 button()->Reset(); 223 button()->Reset();
200 button()->OnMouseReleased(ui::MouseEvent( 224 button()->OnMouseReleased(ui::MouseEvent(
201 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(), 225 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
202 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); 226 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
203 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); 227 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state());
204 EXPECT_FALSE(button()->notified()); 228 EXPECT_FALSE(button()->notified());
205 } 229 }
206 230
231 TEST_F(CustomButtonTest, HandleAccelerator) {
232 // Child widgets shouldn't handle accelerators when they are not focused.
233 EXPECT_FALSE(button()->IsChildWidget());
234 EXPECT_FALSE(button()->FocusInChildWidget());
235 EXPECT_FALSE(widget()->IsActive());
236 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
237 EXPECT_FALSE(button()->notified());
238 // Child without focus.
239 button()->set_child_widget(true);
240 button()->set_focus_in_child_widget(false);
241 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
242 EXPECT_FALSE(button()->notified());
243 button()->Reset();
244 // Child with focus.
245 button()->set_child_widget(true);
246 button()->set_focus_in_child_widget(true);
247 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
248 EXPECT_TRUE(button()->notified());
249 button()->Reset();
250 // Not a child, but active.
251 button()->set_child_widget(false);
252 button()->set_focus_in_child_widget(true);
253 widget()->set_active(true);
254 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
255 EXPECT_TRUE(button()->notified());
256 }
257
207 // No touch on desktop Mac. Tracked in http://crbug.com/445520. 258 // No touch on desktop Mac. Tracked in http://crbug.com/445520.
208 #if !defined(OS_MACOSX) || defined(USE_AURA) 259 #if !defined(OS_MACOSX) || defined(USE_AURA)
209 260
210 namespace { 261 namespace {
211 262
212 void PerformGesture(CustomButton* button, ui::EventType event_type) { 263 void PerformGesture(CustomButton* button, ui::EventType event_type) {
213 ui::GestureEventDetails gesture_details(event_type); 264 ui::GestureEventDetails gesture_details(event_type);
214 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0); 265 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0);
215 ui::GestureEvent gesture_event(0, 0, 0, time_stamp, gesture_details); 266 ui::GestureEvent gesture_event(0, 0, 0, time_stamp, gesture_details);
216 button->OnGestureEvent(&gesture_event); 267 button->OnGestureEvent(&gesture_event);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); 311 EXPECT_FALSE(CustomButton::AsCustomButton(&label));
261 312
262 Link link(text); 313 Link link(text);
263 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); 314 EXPECT_FALSE(CustomButton::AsCustomButton(&link));
264 315
265 Textfield textfield; 316 Textfield textfield;
266 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); 317 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield));
267 } 318 }
268 319
269 } // namespace views 320 } // namespace views
OLDNEW
« ui/views/controls/button/custom_button.cc ('K') | « ui/views/controls/button/custom_button.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698