| 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" |
| 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 Loading... |
| 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 ink drop visibility. |
| 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::SLOW_ACTION_PENDING: |
| 113 case InkDropState::ACTIVATED: |
| 114 *ink_shown_ = true; |
| 115 break; |
| 116 case InkDropState::HIDDEN: |
| 117 *ink_hidden_ = true; |
| 118 break; |
| 119 case InkDropState::QUICK_ACTION: |
| 120 case InkDropState::SLOW_ACTION: |
| 121 case InkDropState::DEACTIVATED: |
| 122 break; |
| 123 } |
| 124 } |
| 125 |
| 126 private: |
| 127 InkDropHost* ink_drop_host_; |
| 128 bool* ink_shown_; |
| 129 bool* ink_hidden_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(TestInkDropDelegate); |
| 132 }; |
| 133 |
| 134 // A test Button class that owns a TestInkDropDelegate. |
| 135 class TestButtonWithInkDrop : public TestCustomButton, |
| 136 public views::InkDropHost { |
| 137 public: |
| 138 TestButtonWithInkDrop(bool* ink_shown, bool* ink_hidden) |
| 139 : TestCustomButton(), |
| 140 ink_drop_delegate_( |
| 141 new TestInkDropDelegate(this, ink_shown, ink_hidden)) { |
| 142 set_ink_drop_delegate(ink_drop_delegate_.get()); |
| 143 } |
| 144 ~TestButtonWithInkDrop() override {} |
| 145 |
| 146 // views::InkDropHost: |
| 147 void AddInkDropLayer(ui::Layer* ink_drop_layer) override {} |
| 148 void RemoveInkDropLayer(ui::Layer* ink_drop_layer) override {} |
| 149 gfx::Point CalculateInkDropCenter() const override { return gfx::Point(); } |
| 150 |
| 151 private: |
| 152 scoped_ptr<views::InkDropDelegate> ink_drop_delegate_; |
| 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(TestButtonWithInkDrop); |
| 155 }; |
| 156 |
| 87 class CustomButtonTest : public ViewsTestBase { | 157 class CustomButtonTest : public ViewsTestBase { |
| 88 public: | 158 public: |
| 89 CustomButtonTest() {} | 159 CustomButtonTest() {} |
| 90 ~CustomButtonTest() override {} | 160 ~CustomButtonTest() override {} |
| 91 | 161 |
| 92 void SetUp() override { | 162 void SetUp() override { |
| 93 ViewsTestBase::SetUp(); | 163 ViewsTestBase::SetUp(); |
| 94 | 164 |
| 95 // Create a widget so that the CustomButton can query the hover state | 165 // Create a widget so that the CustomButton can query the hover state |
| 96 // correctly. | 166 // correctly. |
| 97 widget_.reset(new TestWidget); | 167 widget_.reset(new TestWidget); |
| 98 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 168 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 99 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 169 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 100 params.bounds = gfx::Rect(0, 0, 650, 650); | 170 params.bounds = gfx::Rect(0, 0, 650, 650); |
| 101 widget_->Init(params); | 171 widget_->Init(params); |
| 102 widget_->Show(); | 172 widget_->Show(); |
| 103 | 173 |
| 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); | |
| 110 | |
| 111 button_ = new TestCustomButton(); | 174 button_ = new TestCustomButton(); |
| 112 widget_->SetContentsView(button_); | 175 widget_->SetContentsView(button_); |
| 113 } | 176 } |
| 114 | 177 |
| 115 void TearDown() override { | 178 void TearDown() override { |
| 116 widget_.reset(); | 179 widget_.reset(); |
| 117 ViewsTestBase::TearDown(); | 180 ViewsTestBase::TearDown(); |
| 118 } | 181 } |
| 119 | 182 |
| 183 void CreateButtonWithInkDrop() { |
| 184 delete button_; |
| 185 ink_shown_ = false; |
| 186 ink_hidden_ = false; |
| 187 button_ = new TestButtonWithInkDrop(&ink_shown_, &ink_hidden_); |
| 188 widget_->SetContentsView(button_); |
| 189 } |
| 190 |
| 191 protected: |
| 120 TestWidget* widget() { return widget_.get(); } | 192 TestWidget* widget() { return widget_.get(); } |
| 121 TestCustomButton* button() { return button_; } | 193 TestCustomButton* button() { return button_; } |
| 194 bool ink_shown() const { return ink_shown_; } |
| 195 bool ink_hidden() const { return ink_hidden_; } |
| 122 | 196 |
| 123 private: | 197 private: |
| 124 scoped_ptr<TestWidget> widget_; | 198 scoped_ptr<TestWidget> widget_; |
| 125 TestCustomButton* button_; | 199 TestCustomButton* button_; |
| 200 bool ink_shown_ = false; |
| 201 bool ink_hidden_ = false; |
| 126 | 202 |
| 127 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); | 203 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); |
| 128 }; | 204 }; |
| 129 | 205 |
| 130 } // namespace | 206 } // namespace |
| 131 | 207 |
| 132 // Tests that hover state changes correctly when visiblity/enableness changes. | 208 // Tests that hover state changes correctly when visiblity/enableness changes. |
| 133 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { | 209 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { |
| 134 gfx::Point center(10, 10); | 210 gfx::Point center(10, 10); |
| 135 button()->OnMousePressed(ui::MouseEvent( | 211 button()->OnMousePressed(ui::MouseEvent( |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 Label label; | 430 Label label; |
| 355 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); | 431 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); |
| 356 | 432 |
| 357 Link link(text); | 433 Link link(text); |
| 358 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); | 434 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); |
| 359 | 435 |
| 360 Textfield textfield; | 436 Textfield textfield; |
| 361 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); | 437 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); |
| 362 } | 438 } |
| 363 | 439 |
| 440 // Tests that pressing a button shows the ink drop and releasing the button |
| 441 // does not hide the ink drop. |
| 442 // Note: Ink drop is not hidden upon release because CustomButton descendants |
| 443 // may enter a different ink drop state. |
| 444 TEST_F(CustomButtonTest, ButtonClickTogglesInkDrop) { |
| 445 gfx::Point old_cursor = gfx::Screen::GetScreenFor( |
| 446 widget()->GetNativeView())->GetCursorScreenPoint(); |
| 447 CreateButtonWithInkDrop(); |
| 448 |
| 449 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| 450 generator.set_current_location(gfx::Point(50, 50)); |
| 451 generator.PressLeftButton(); |
| 452 EXPECT_TRUE(ink_shown()); |
| 453 EXPECT_FALSE(ink_hidden()); |
| 454 |
| 455 generator.ReleaseLeftButton(); |
| 456 EXPECT_FALSE(ink_hidden()); |
| 457 } |
| 458 |
| 459 // Tests that pressing a button shows and releasing capture hides ink drop. |
| 460 TEST_F(CustomButtonTest, CaptureLossHidesInkDrop) { |
| 461 gfx::Point old_cursor = gfx::Screen::GetScreenFor( |
| 462 widget()->GetNativeView())->GetCursorScreenPoint(); |
| 463 CreateButtonWithInkDrop(); |
| 464 |
| 465 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| 466 generator.set_current_location(gfx::Point(50, 50)); |
| 467 generator.PressLeftButton(); |
| 468 EXPECT_TRUE(ink_shown()); |
| 469 EXPECT_FALSE(ink_hidden()); |
| 470 |
| 471 widget()->SetCapture(button()); |
| 472 widget()->ReleaseCapture(); |
| 473 EXPECT_TRUE(ink_hidden()); |
| 474 } |
| 475 |
| 364 } // namespace views | 476 } // namespace views |
| OLD | NEW |