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

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: msw comments 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 private: 46 private:
47 bool notified_ = false; 47 bool notified_ = false;
48 48
49 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); 49 DISALLOW_COPY_AND_ASSIGN(TestCustomButton);
50 }; 50 };
51 51
52 class TestWidget : public Widget {
53 public:
54 TestWidget() : Widget() {}
55
56 bool IsActive() const override { return active_; }
sky 2015/11/12 23:42:57 nit: generally we prefix overrides with what class
57 void set_active(bool active) { active_ = active; }
58
59 private:
60 bool active_ = false;
61
62 DISALLOW_COPY_AND_ASSIGN(TestWidget);
63 };
64
52 class CustomButtonTest : public ViewsTestBase { 65 class CustomButtonTest : public ViewsTestBase {
53 public: 66 public:
54 CustomButtonTest() {} 67 CustomButtonTest() {}
55 ~CustomButtonTest() override {} 68 ~CustomButtonTest() override {}
56 69
57 void SetUp() override { 70 void SetUp() override {
58 ViewsTestBase::SetUp(); 71 ViewsTestBase::SetUp();
59 72
60 // Create a widget so that the CustomButton can query the hover state 73 // Create a widget so that the CustomButton can query the hover state
61 // correctly. 74 // correctly.
62 widget_.reset(new Widget); 75 widget_.reset(new TestWidget);
63 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 76 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
64 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 77 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
65 params.bounds = gfx::Rect(0, 0, 650, 650); 78 params.bounds = gfx::Rect(0, 0, 650, 650);
66 widget_->Init(params); 79 widget_->Init(params);
67 widget_->Show(); 80 widget_->Show();
68 81
69 // Position the widget in a way so that it is under the cursor. 82 // Position the widget in a way so that it is under the cursor.
70 gfx::Point cursor = gfx::Screen::GetScreenFor( 83 gfx::Point cursor = gfx::Screen::GetScreenFor(
71 widget_->GetNativeView())->GetCursorScreenPoint(); 84 widget_->GetNativeView())->GetCursorScreenPoint();
72 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); 85 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen();
73 widget_bounds.set_origin(cursor); 86 widget_bounds.set_origin(cursor);
74 widget_->SetBounds(widget_bounds); 87 widget_->SetBounds(widget_bounds);
75 88
76 button_ = new TestCustomButton(); 89 button_ = new TestCustomButton();
77 widget_->SetContentsView(button_); 90 widget_->SetContentsView(button_);
78 } 91 }
79 92
80 void TearDown() override { 93 void TearDown() override {
81 widget_.reset(); 94 widget_.reset();
82 ViewsTestBase::TearDown(); 95 ViewsTestBase::TearDown();
83 } 96 }
84 97
85 Widget* widget() { return widget_.get(); } 98 TestWidget* widget() { return widget_.get(); }
86 TestCustomButton* button() { return button_; } 99 TestCustomButton* button() { return button_; }
87 100
88 private: 101 private:
89 scoped_ptr<Widget> widget_; 102 scoped_ptr<TestWidget> widget_;
90 TestCustomButton* button_; 103 TestCustomButton* button_;
91 104
92 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); 105 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest);
93 }; 106 };
94 107
95 } // namespace 108 } // namespace
96 109
97 // Tests that hover state changes correctly when visiblity/enableness changes. 110 // Tests that hover state changes correctly when visiblity/enableness changes.
98 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { 111 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) {
99 gfx::Point center(10, 10); 112 gfx::Point center(10, 10);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 210
198 // The button should no longer notify on mouse release. 211 // The button should no longer notify on mouse release.
199 button()->Reset(); 212 button()->Reset();
200 button()->OnMouseReleased(ui::MouseEvent( 213 button()->OnMouseReleased(ui::MouseEvent(
201 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(), 214 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
202 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); 215 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
203 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); 216 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state());
204 EXPECT_FALSE(button()->notified()); 217 EXPECT_FALSE(button()->notified());
205 } 218 }
206 219
220 TEST_F(CustomButtonTest, HandleAcceleratorWhenFocused) {
221 // Shouldn't handle accelerators when not active.
222 EXPECT_FALSE(widget()->IsActive());
223 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
224 EXPECT_FALSE(button()->notified());
225 // Should handle accelerators when active.
226 widget()->set_active(true);
227 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
228 EXPECT_TRUE(button()->notified());
229 // Not active again.
230 button()->Reset();
231 widget()->set_active(false);
232 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
233 EXPECT_FALSE(button()->notified());
234 }
235
207 // No touch on desktop Mac. Tracked in http://crbug.com/445520. 236 // No touch on desktop Mac. Tracked in http://crbug.com/445520.
208 #if !defined(OS_MACOSX) || defined(USE_AURA) 237 #if !defined(OS_MACOSX) || defined(USE_AURA)
209 238
210 namespace { 239 namespace {
211 240
212 void PerformGesture(CustomButton* button, ui::EventType event_type) { 241 void PerformGesture(CustomButton* button, ui::EventType event_type) {
213 ui::GestureEventDetails gesture_details(event_type); 242 ui::GestureEventDetails gesture_details(event_type);
214 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0); 243 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0);
215 ui::GestureEvent gesture_event(0, 0, 0, time_stamp, gesture_details); 244 ui::GestureEvent gesture_event(0, 0, 0, time_stamp, gesture_details);
216 button->OnGestureEvent(&gesture_event); 245 button->OnGestureEvent(&gesture_event);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); 289 EXPECT_FALSE(CustomButton::AsCustomButton(&label));
261 290
262 Link link(text); 291 Link link(text);
263 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); 292 EXPECT_FALSE(CustomButton::AsCustomButton(&link));
264 293
265 Textfield textfield; 294 Textfield textfield;
266 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); 295 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield));
267 } 296 }
268 297
269 } // namespace views 298 } // 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