| 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/base/material_design/material_design_controller.h" |
| 11 #include "ui/events/event_utils.h" | 12 #include "ui/events/event_utils.h" |
| 12 #include "ui/events/test/event_generator.h" | 13 #include "ui/events/test/event_generator.h" |
| 13 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
| 14 #include "ui/views/animation/ink_drop_delegate.h" | 15 #include "ui/views/animation/ink_drop_delegate.h" |
| 15 #include "ui/views/animation/ink_drop_host.h" | 16 #include "ui/views/animation/ink_drop_host.h" |
| 16 #include "ui/views/animation/test/test_ink_drop_host.h" | 17 #include "ui/views/animation/test/test_ink_drop_host.h" |
| 17 #include "ui/views/controls/button/checkbox.h" | 18 #include "ui/views/controls/button/checkbox.h" |
| 18 #include "ui/views/controls/button/image_button.h" | 19 #include "ui/views/controls/button/image_button.h" |
| 19 #include "ui/views/controls/button/label_button.h" | 20 #include "ui/views/controls/button/label_button.h" |
| 20 #include "ui/views/controls/button/menu_button.h" | 21 #include "ui/views/controls/button/menu_button.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 set_ink_drop_delegate(ink_drop_delegate_.get()); | 114 set_ink_drop_delegate(ink_drop_delegate_.get()); |
| 114 } | 115 } |
| 115 ~TestButtonWithInkDrop() override {} | 116 ~TestButtonWithInkDrop() override {} |
| 116 | 117 |
| 117 private: | 118 private: |
| 118 scoped_ptr<views::InkDropDelegate> ink_drop_delegate_; | 119 scoped_ptr<views::InkDropDelegate> ink_drop_delegate_; |
| 119 | 120 |
| 120 DISALLOW_COPY_AND_ASSIGN(TestButtonWithInkDrop); | 121 DISALLOW_COPY_AND_ASSIGN(TestButtonWithInkDrop); |
| 121 }; | 122 }; |
| 122 | 123 |
| 124 } // namespace |
| 125 |
| 123 class CustomButtonTest : public ViewsTestBase { | 126 class CustomButtonTest : public ViewsTestBase { |
| 124 public: | 127 public: |
| 125 CustomButtonTest() {} | 128 CustomButtonTest() {} |
| 126 ~CustomButtonTest() override {} | 129 ~CustomButtonTest() override {} |
| 127 | 130 |
| 128 void SetUp() override { | 131 void SetUp() override { |
| 129 ViewsTestBase::SetUp(); | 132 ViewsTestBase::SetUp(); |
| 130 | 133 |
| 131 // Create a widget so that the CustomButton can query the hover state | 134 // Create a widget so that the CustomButton can query the hover state |
| 132 // correctly. | 135 // correctly. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 152 ink_hidden_ = false; | 155 ink_hidden_ = false; |
| 153 button_ = new TestButtonWithInkDrop(&ink_shown_, &ink_hidden_); | 156 button_ = new TestButtonWithInkDrop(&ink_shown_, &ink_hidden_); |
| 154 widget_->SetContentsView(button_); | 157 widget_->SetContentsView(button_); |
| 155 } | 158 } |
| 156 | 159 |
| 157 protected: | 160 protected: |
| 158 Widget* widget() { return widget_.get(); } | 161 Widget* widget() { return widget_.get(); } |
| 159 TestCustomButton* button() { return button_; } | 162 TestCustomButton* button() { return button_; } |
| 160 bool ink_shown() const { return ink_shown_; } | 163 bool ink_shown() const { return ink_shown_; } |
| 161 bool ink_hidden() const { return ink_hidden_; } | 164 bool ink_hidden() const { return ink_hidden_; } |
| 165 void SetDraggedView(View* dragged_view) { |
| 166 widget_->dragged_view_ = dragged_view; |
| 167 } |
| 162 | 168 |
| 163 private: | 169 private: |
| 164 scoped_ptr<Widget> widget_; | 170 scoped_ptr<Widget> widget_; |
| 165 TestCustomButton* button_; | 171 TestCustomButton* button_; |
| 166 bool ink_shown_ = false; | 172 bool ink_shown_ = false; |
| 167 bool ink_hidden_ = false; | 173 bool ink_hidden_ = false; |
| 168 | 174 |
| 169 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); | 175 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); |
| 170 }; | 176 }; |
| 171 | 177 |
| 172 } // namespace | |
| 173 | |
| 174 // Tests that hover state changes correctly when visiblity/enableness changes. | 178 // Tests that hover state changes correctly when visiblity/enableness changes. |
| 175 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { | 179 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { |
| 176 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 180 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| 177 | 181 |
| 178 generator.PressLeftButton(); | 182 generator.PressLeftButton(); |
| 179 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); | 183 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); |
| 180 | 184 |
| 181 generator.ReleaseLeftButton(); | 185 generator.ReleaseLeftButton(); |
| 182 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); | 186 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); |
| 183 | 187 |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 generator.set_current_location(gfx::Point(50, 50)); | 389 generator.set_current_location(gfx::Point(50, 50)); |
| 386 generator.PressLeftButton(); | 390 generator.PressLeftButton(); |
| 387 EXPECT_TRUE(ink_shown()); | 391 EXPECT_TRUE(ink_shown()); |
| 388 EXPECT_FALSE(ink_hidden()); | 392 EXPECT_FALSE(ink_hidden()); |
| 389 | 393 |
| 390 generator.ReleaseLeftButton(); | 394 generator.ReleaseLeftButton(); |
| 391 EXPECT_FALSE(ink_hidden()); | 395 EXPECT_FALSE(ink_hidden()); |
| 392 } | 396 } |
| 393 | 397 |
| 394 // Tests that pressing a button shows and releasing capture hides ink drop. | 398 // Tests that pressing a button shows and releasing capture hides ink drop. |
| 399 // Releasing capture should also reset PRESSED button state to NORMAL. |
| 395 TEST_F(CustomButtonTest, CaptureLossHidesInkDrop) { | 400 TEST_F(CustomButtonTest, CaptureLossHidesInkDrop) { |
| 396 gfx::Point old_cursor = gfx::Screen::GetScreen()->GetCursorScreenPoint(); | 401 gfx::Point old_cursor = gfx::Screen::GetScreen()->GetCursorScreenPoint(); |
| 397 CreateButtonWithInkDrop(); | 402 CreateButtonWithInkDrop(); |
| 398 | 403 |
| 399 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 404 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| 400 generator.set_current_location(gfx::Point(50, 50)); | 405 generator.set_current_location(gfx::Point(50, 50)); |
| 401 generator.PressLeftButton(); | 406 generator.PressLeftButton(); |
| 402 EXPECT_TRUE(ink_shown()); | 407 EXPECT_TRUE(ink_shown()); |
| 403 EXPECT_FALSE(ink_hidden()); | 408 EXPECT_FALSE(ink_hidden()); |
| 404 | 409 |
| 410 EXPECT_EQ(Button::ButtonState::STATE_PRESSED, button()->state()); |
| 411 SetDraggedView(button()); |
| 405 widget()->SetCapture(button()); | 412 widget()->SetCapture(button()); |
| 406 widget()->ReleaseCapture(); | 413 widget()->ReleaseCapture(); |
| 414 SetDraggedView(nullptr); |
| 407 EXPECT_TRUE(ink_hidden()); | 415 EXPECT_TRUE(ink_hidden()); |
| 416 EXPECT_EQ(ui::MaterialDesignController::IsModeMaterial() |
| 417 ? Button::ButtonState::STATE_NORMAL |
| 418 : Button::ButtonState::STATE_PRESSED, |
| 419 button()->state()); |
| 408 } | 420 } |
| 409 | 421 |
| 410 } // namespace views | 422 } // namespace views |
| OLD | NEW |