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

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

Issue 1517003002: Fixes stale hover or ink ripple visuals when dragging over extension buttons (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes stale hover or ink ripple visuals when dragging over extension buttons (moved the test to cus… Created 4 years, 11 months 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
« no previous file with comments | « ui/views/controls/button/custom_button.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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"
11 #include "ui/events/event_utils.h" 11 #include "ui/events/event_utils.h"
12 #include "ui/events/test/event_generator.h"
12 #include "ui/gfx/screen.h" 13 #include "ui/gfx/screen.h"
14 #include "ui/views/animation/ink_drop_delegate.h"
15 #include "ui/views/animation/ink_drop_host.h"
13 #include "ui/views/controls/button/checkbox.h" 16 #include "ui/views/controls/button/checkbox.h"
14 #include "ui/views/controls/button/image_button.h" 17 #include "ui/views/controls/button/image_button.h"
15 #include "ui/views/controls/button/label_button.h" 18 #include "ui/views/controls/button/label_button.h"
16 #include "ui/views/controls/button/menu_button.h" 19 #include "ui/views/controls/button/menu_button.h"
17 #include "ui/views/controls/button/radio_button.h" 20 #include "ui/views/controls/button/radio_button.h"
18 #include "ui/views/controls/link.h" 21 #include "ui/views/controls/link.h"
19 #include "ui/views/controls/textfield/textfield.h" 22 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/test/views_test_base.h" 23 #include "ui/views/test/views_test_base.h"
21 24
22 #if defined(USE_AURA) 25 #if defined(USE_AURA)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 bool IsActive() const override { return active_; } 80 bool IsActive() const override { return active_; }
78 81
79 void set_active(bool active) { active_ = active; } 82 void set_active(bool active) { active_ = active; }
80 83
81 private: 84 private:
82 bool active_ = false; 85 bool active_ = false;
83 86
84 DISALLOW_COPY_AND_ASSIGN(TestWidget); 87 DISALLOW_COPY_AND_ASSIGN(TestWidget);
85 }; 88 };
86 89
90 // An InkDropDelegate that keeps track of order of deletions.
bruthig 2016/01/07 16:55:09 Is this comment still accurate?
varkha 2016/01/07 17:06:39 Done.
91 class TestInkDropDelegate : public InkDropDelegate {
92 public:
93 TestInkDropDelegate(InkDropHost* ink_drop_host,
94 bool* ink_shown,
95 bool* ink_hidden)
96 : ink_drop_host_(ink_drop_host),
97 ink_shown_(ink_shown),
98 ink_hidden_(ink_hidden) {
99 ink_drop_host_->AddInkDropLayer(nullptr);
100 }
101 ~TestInkDropDelegate() override {}
102
103 // InkDropDelegate:
104 void SetInkDropSize(int large_size,
105 int large_corner_radius,
106 int small_size,
107 int small_corner_radius) override {}
108 void OnLayout() override {}
109 void OnAction(InkDropState state) override {
110 switch (state) {
111 case InkDropState::ACTION_PENDING:
112 case InkDropState::QUICK_ACTION:
113 case InkDropState::SLOW_ACTION_PENDING:
114 case InkDropState::SLOW_ACTION:
115 case InkDropState::ACTIVATED:
116 *ink_shown_ = true;
117 break;
118 case InkDropState::HIDDEN:
119 case InkDropState::DEACTIVATED:
bruthig 2016/01/07 16:55:09 The DEACTIVATED, SLOW_ACTION and QUICK_ACTION stat
varkha 2016/01/07 17:06:39 Done.
120 *ink_hidden_ = true;
121 break;
122 }
123 }
124
125 private:
126 InkDropHost* ink_drop_host_;
127 bool* ink_shown_;
128 bool* ink_hidden_;
129
130 DISALLOW_COPY_AND_ASSIGN(TestInkDropDelegate);
131 };
132
133 // A test Button class that owns a TestInkDropDelegate.
134 class TestButtonWithInkDrop : public TestCustomButton,
135 public views::InkDropHost {
136 public:
137 TestButtonWithInkDrop(bool* ink_shown, bool* ink_hidden)
138 : TestCustomButton(),
139 ink_drop_delegate_(
140 new TestInkDropDelegate(this, ink_shown, ink_hidden)) {
141 set_ink_drop_delegate(ink_drop_delegate_.get());
142 }
143 ~TestButtonWithInkDrop() override {}
144
145 // views::InkDropHost:
146 void AddInkDropLayer(ui::Layer* ink_drop_layer) override {}
147 void RemoveInkDropLayer(ui::Layer* ink_drop_layer) override {}
148 gfx::Point CalculateInkDropCenter() const override { return gfx::Point(); }
149
150 private:
151 scoped_ptr<views::InkDropDelegate> ink_drop_delegate_;
152
153 DISALLOW_COPY_AND_ASSIGN(TestButtonWithInkDrop);
154 };
155
87 class CustomButtonTest : public ViewsTestBase { 156 class CustomButtonTest : public ViewsTestBase {
88 public: 157 public:
89 CustomButtonTest() {} 158 CustomButtonTest() {}
90 ~CustomButtonTest() override {} 159 ~CustomButtonTest() override {}
91 160
92 void SetUp() override { 161 void SetUp() override {
93 ViewsTestBase::SetUp(); 162 ViewsTestBase::SetUp();
94 163
95 // Create a widget so that the CustomButton can query the hover state 164 // Create a widget so that the CustomButton can query the hover state
96 // correctly. 165 // correctly.
97 widget_.reset(new TestWidget); 166 widget_.reset(new TestWidget);
98 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); 167 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
99 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 168 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
100 params.bounds = gfx::Rect(0, 0, 650, 650); 169 params.bounds = gfx::Rect(0, 0, 650, 650);
101 widget_->Init(params); 170 widget_->Init(params);
102 widget_->Show(); 171 widget_->Show();
103 172
104 // Position the widget in a way so that it is under the cursor.
105 gfx::Point cursor = gfx::Screen::GetScreenFor(
106 widget_->GetNativeView())->GetCursorScreenPoint();
107 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen();
108 widget_bounds.set_origin(cursor);
109 widget_->SetBounds(widget_bounds);
varkha 2016/01/07 01:40:35 This code made sense in the original first submiss
110
111 button_ = new TestCustomButton(); 173 button_ = new TestCustomButton();
112 widget_->SetContentsView(button_); 174 widget_->SetContentsView(button_);
113 } 175 }
114 176
115 void TearDown() override { 177 void TearDown() override {
116 widget_.reset(); 178 widget_.reset();
117 ViewsTestBase::TearDown(); 179 ViewsTestBase::TearDown();
118 } 180 }
119 181
182 void CreateButtonWithInkDrop() {
183 delete button_;
bruthig 2016/01/07 16:55:09 Consider resetting |ink_shown_| and |ink_hidden_|
varkha 2016/01/07 17:06:39 Done.
184 button_ = new TestButtonWithInkDrop(&ink_shown_, &ink_hidden_);
185 widget_->SetContentsView(button_);
186 }
187
188 protected:
120 TestWidget* widget() { return widget_.get(); } 189 TestWidget* widget() { return widget_.get(); }
121 TestCustomButton* button() { return button_; } 190 TestCustomButton* button() { return button_; }
191 bool ink_shown() const { return ink_shown_; }
192 bool ink_hidden() const { return ink_hidden_; }
122 193
123 private: 194 private:
124 scoped_ptr<TestWidget> widget_; 195 scoped_ptr<TestWidget> widget_;
125 TestCustomButton* button_; 196 TestCustomButton* button_;
197 bool ink_shown_ = false;
198 bool ink_hidden_ = false;
126 199
127 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); 200 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest);
128 }; 201 };
129 202
130 } // namespace 203 } // namespace
131 204
132 // Tests that hover state changes correctly when visiblity/enableness changes. 205 // Tests that hover state changes correctly when visiblity/enableness changes.
133 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { 206 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) {
134 gfx::Point center(10, 10); 207 gfx::Point center(10, 10);
135 button()->OnMousePressed(ui::MouseEvent( 208 button()->OnMousePressed(ui::MouseEvent(
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 Label label; 427 Label label;
355 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); 428 EXPECT_FALSE(CustomButton::AsCustomButton(&label));
356 429
357 Link link(text); 430 Link link(text);
358 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); 431 EXPECT_FALSE(CustomButton::AsCustomButton(&link));
359 432
360 Textfield textfield; 433 Textfield textfield;
361 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); 434 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield));
362 } 435 }
363 436
437 // Tests that pressing a button shows the ink drop and releasing the button
438 // does not hide the ink drop.
439 // Note: Ink drop is not hidden upon release because CustomButton descendants
440 // may enter a different ink drop state.
441 TEST_F(CustomButtonTest, ButtonClickTogglesInkDrop) {
442 gfx::Point old_cursor = gfx::Screen::GetScreenFor(
443 widget()->GetNativeView())->GetCursorScreenPoint();
444 CreateButtonWithInkDrop();
445
446 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());
447 generator.set_current_location(gfx::Point(50, 50));
448 generator.PressLeftButton();
449 EXPECT_TRUE(ink_shown());
450 EXPECT_FALSE(ink_hidden());
451
452 generator.ReleaseLeftButton();
453 EXPECT_FALSE(ink_hidden());
454 }
455
456 // Tests that pressing a button shows and releasing capture hides ink drop.
457 TEST_F(CustomButtonTest, CaptureLossHidesInkDrop) {
458 gfx::Point old_cursor = gfx::Screen::GetScreenFor(
459 widget()->GetNativeView())->GetCursorScreenPoint();
460 CreateButtonWithInkDrop();
461
462 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());
463 generator.set_current_location(gfx::Point(50, 50));
464 generator.PressLeftButton();
465 EXPECT_TRUE(ink_shown());
466 EXPECT_FALSE(ink_hidden());
467
468 widget()->SetCapture(button());
469 widget()->ReleaseCapture();
470 EXPECT_TRUE(ink_hidden());
471 }
472
364 } // namespace views 473 } // namespace views
OLDNEW
« no previous file with comments | « 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