| 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 "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" |
| 9 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/base/layout.h" | 12 #include "ui/base/layout.h" |
| 12 #include "ui/display/screen.h" | 13 #include "ui/display/screen.h" |
| 13 #include "ui/events/event_utils.h" | 14 #include "ui/events/event_utils.h" |
| 14 #include "ui/events/test/event_generator.h" | 15 #include "ui/events/test/event_generator.h" |
| 15 #include "ui/views/animation/ink_drop_host.h" | 16 #include "ui/views/animation/ink_drop_host.h" |
| 17 #include "ui/views/animation/ink_drop_impl.h" |
| 16 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" | 18 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" |
| 17 #include "ui/views/animation/test/test_ink_drop.h" | 19 #include "ui/views/animation/test/test_ink_drop.h" |
| 18 #include "ui/views/animation/test/test_ink_drop_host.h" | 20 #include "ui/views/animation/test/test_ink_drop_host.h" |
| 19 #include "ui/views/context_menu_controller.h" | 21 #include "ui/views/context_menu_controller.h" |
| 20 #include "ui/views/controls/button/checkbox.h" | 22 #include "ui/views/controls/button/checkbox.h" |
| 21 #include "ui/views/controls/button/image_button.h" | 23 #include "ui/views/controls/button/image_button.h" |
| 22 #include "ui/views/controls/button/label_button.h" | 24 #include "ui/views/controls/button/label_button.h" |
| 23 #include "ui/views/controls/button/menu_button.h" | 25 #include "ui/views/controls/button/menu_button.h" |
| 24 #include "ui/views/controls/button/radio_button.h" | 26 #include "ui/views/controls/button/radio_button.h" |
| 25 #include "ui/views/controls/button/toggle_button.h" | 27 #include "ui/views/controls/button/toggle_button.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 ~TestCustomButton() override {} | 67 ~TestCustomButton() override {} |
| 66 | 68 |
| 67 void ButtonPressed(Button* sender, const ui::Event& event) override { | 69 void ButtonPressed(Button* sender, const ui::Event& event) override { |
| 68 pressed_ = true; | 70 pressed_ = true; |
| 69 } | 71 } |
| 70 | 72 |
| 71 void OnClickCanceled(const ui::Event& event) override { | 73 void OnClickCanceled(const ui::Event& event) override { |
| 72 canceled_ = true; | 74 canceled_ = true; |
| 73 } | 75 } |
| 74 | 76 |
| 77 // InkDropHostView: |
| 78 void AddInkDropLayer(ui::Layer* ink_drop_layer) override { |
| 79 ++ink_drop_layer_add_count_; |
| 80 CustomButton::AddInkDropLayer(ink_drop_layer); |
| 81 } |
| 82 void RemoveInkDropLayer(ui::Layer* ink_drop_layer) override { |
| 83 ++ink_drop_layer_remove_count_; |
| 84 CustomButton::RemoveInkDropLayer(ink_drop_layer); |
| 85 } |
| 86 |
| 75 bool pressed() { return pressed_; } | 87 bool pressed() { return pressed_; } |
| 76 bool canceled() { return canceled_; } | 88 bool canceled() { return canceled_; } |
| 89 int ink_drop_layer_add_count() { return ink_drop_layer_add_count_; } |
| 90 int ink_drop_layer_remove_count() { return ink_drop_layer_remove_count_; } |
| 77 | 91 |
| 78 void Reset() { | 92 void Reset() { |
| 79 pressed_ = false; | 93 pressed_ = false; |
| 80 canceled_ = false; | 94 canceled_ = false; |
| 81 } | 95 } |
| 82 | 96 |
| 83 // Raised visibility of OnFocus() to public | 97 // Raised visibility of OnFocus() to public |
| 84 void OnFocus() override { CustomButton::OnFocus(); } | 98 void OnFocus() override { CustomButton::OnFocus(); } |
| 85 | 99 |
| 86 private: | 100 private: |
| 87 bool pressed_ = false; | 101 bool pressed_ = false; |
| 88 bool canceled_ = false; | 102 bool canceled_ = false; |
| 89 | 103 |
| 104 int ink_drop_layer_add_count_ = 0; |
| 105 int ink_drop_layer_remove_count_ = 0; |
| 106 |
| 90 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); | 107 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); |
| 91 }; | 108 }; |
| 92 | 109 |
| 93 } // namespace | 110 } // namespace |
| 94 | 111 |
| 95 class CustomButtonTest : public ViewsTestBase { | 112 class CustomButtonTest : public ViewsTestBase { |
| 96 public: | 113 public: |
| 97 CustomButtonTest() {} | 114 CustomButtonTest() {} |
| 98 ~CustomButtonTest() override {} | 115 ~CustomButtonTest() override {} |
| 99 | 116 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 119 } | 136 } |
| 120 | 137 |
| 121 void CreateButtonWithInkDrop(std::unique_ptr<InkDrop> ink_drop, | 138 void CreateButtonWithInkDrop(std::unique_ptr<InkDrop> ink_drop, |
| 122 bool has_ink_drop_action_on_click) { | 139 bool has_ink_drop_action_on_click) { |
| 123 delete button_; | 140 delete button_; |
| 124 button_ = new TestCustomButton(has_ink_drop_action_on_click); | 141 button_ = new TestCustomButton(has_ink_drop_action_on_click); |
| 125 InkDropHostViewTestApi(button_).SetInkDrop(std::move(ink_drop)); | 142 InkDropHostViewTestApi(button_).SetInkDrop(std::move(ink_drop)); |
| 126 widget_->SetContentsView(button_); | 143 widget_->SetContentsView(button_); |
| 127 } | 144 } |
| 128 | 145 |
| 146 void CreateButtonWithRealInkDrop() { |
| 147 delete button_; |
| 148 button_ = new TestCustomButton(false); |
| 149 InkDropHostViewTestApi(button_).SetInkDrop( |
| 150 base::MakeUnique<InkDropImpl>(button_)); |
| 151 widget_->SetContentsView(button_); |
| 152 } |
| 153 |
| 129 protected: | 154 protected: |
| 130 Widget* widget() { return widget_.get(); } | 155 Widget* widget() { return widget_.get(); } |
| 131 TestCustomButton* button() { return button_; } | 156 TestCustomButton* button() { return button_; } |
| 132 void SetDraggedView(View* dragged_view) { | 157 void SetDraggedView(View* dragged_view) { |
| 133 widget_->dragged_view_ = dragged_view; | 158 widget_->dragged_view_ = dragged_view; |
| 134 } | 159 } |
| 135 | 160 |
| 136 void SimulateKeyEvent(ui::KeyEvent* event) { widget()->OnKeyEvent(event); } | 161 void SimulateKeyEvent(ui::KeyEvent* event) { widget()->OnKeyEvent(event); } |
| 137 | 162 |
| 138 private: | 163 private: |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 | 589 |
| 565 button()->OnMouseDragged(ui::MouseEvent( | 590 button()->OnMouseDragged(ui::MouseEvent( |
| 566 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), | 591 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), |
| 567 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | 592 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 568 | 593 |
| 569 EXPECT_EQ(InkDropState::HIDDEN, ink_drop->GetTargetInkDropState()); | 594 EXPECT_EQ(InkDropState::HIDDEN, ink_drop->GetTargetInkDropState()); |
| 570 | 595 |
| 571 SetDraggedView(nullptr); | 596 SetDraggedView(nullptr); |
| 572 } | 597 } |
| 573 | 598 |
| 599 // Test that hiding or closing a Widget doesn't attempt to add a layer due to |
| 600 // changed visibility states. |
| 601 TEST_F(CustomButtonTest, NoLayerAddedForWidgetVisibilityChanges) { |
| 602 CreateButtonWithRealInkDrop(); |
| 603 |
| 604 EXPECT_TRUE(button()->visible()); |
| 605 EXPECT_FALSE(button()->layer()); |
| 606 |
| 607 widget()->Hide(); |
| 608 EXPECT_FALSE(button()->layer()); |
| 609 EXPECT_EQ(0, button()->ink_drop_layer_add_count()); |
| 610 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); |
| 611 |
| 612 widget()->Show(); |
| 613 EXPECT_FALSE(button()->layer()); |
| 614 EXPECT_EQ(0, button()->ink_drop_layer_add_count()); |
| 615 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); |
| 616 |
| 617 // Allow the button to be interrogated after the view hierarchy is torn down. |
| 618 button()->set_owned_by_client(); |
| 619 widget()->Close(); // Start an asynchronous close. |
| 620 EXPECT_FALSE(button()->layer()); |
| 621 EXPECT_EQ(0, button()->ink_drop_layer_add_count()); |
| 622 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); |
| 623 |
| 624 base::RunLoop().RunUntilIdle(); // Complete the Close(). |
| 625 EXPECT_FALSE(button()->layer()); |
| 626 EXPECT_EQ(0, button()->ink_drop_layer_add_count()); |
| 627 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); |
| 628 |
| 629 delete button(); |
| 630 } |
| 631 |
| 574 // Todo(karandeepb): On Mac, a button should get clicked on a Space key press | 632 // Todo(karandeepb): On Mac, a button should get clicked on a Space key press |
| 575 // (and not release). Modify this test after fixing crbug.com/607429. | 633 // (and not release). Modify this test after fixing crbug.com/607429. |
| 576 // Test that Space Key behaves correctly on a focused button. | 634 // Test that Space Key behaves correctly on a focused button. |
| 577 TEST_F(CustomButtonTest, ClickOnSpace) { | 635 TEST_F(CustomButtonTest, ClickOnSpace) { |
| 578 // Give focus to the button. | 636 // Give focus to the button. |
| 579 button()->SetFocusForPlatform(); | 637 button()->SetFocusForPlatform(); |
| 580 button()->RequestFocus(); | 638 button()->RequestFocus(); |
| 581 EXPECT_EQ(button(), widget()->GetFocusManager()->GetFocusedView()); | 639 EXPECT_EQ(button(), widget()->GetFocusManager()->GetFocusedView()); |
| 582 | 640 |
| 583 ui::KeyEvent space_press(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE); | 641 ui::KeyEvent space_press(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE); |
| 584 SimulateKeyEvent(&space_press); | 642 SimulateKeyEvent(&space_press); |
| 585 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); | 643 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); |
| 586 EXPECT_FALSE(button()->pressed()); | 644 EXPECT_FALSE(button()->pressed()); |
| 587 | 645 |
| 588 ui::KeyEvent space_release(ui::ET_KEY_RELEASED, ui::VKEY_SPACE, ui::EF_NONE); | 646 ui::KeyEvent space_release(ui::ET_KEY_RELEASED, ui::VKEY_SPACE, ui::EF_NONE); |
| 589 SimulateKeyEvent(&space_release); | 647 SimulateKeyEvent(&space_release); |
| 590 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state()); | 648 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state()); |
| 591 EXPECT_TRUE(button()->pressed()); | 649 EXPECT_TRUE(button()->pressed()); |
| 592 } | 650 } |
| 593 | 651 |
| 594 } // namespace views | 652 } // namespace views |
| OLD | NEW |