Chromium Code Reviews| 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 "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/base/layout.h" | 11 #include "ui/base/layout.h" |
| 12 #include "ui/display/screen.h" | 12 #include "ui/display/screen.h" |
| 13 #include "ui/events/event_utils.h" | 13 #include "ui/events/event_utils.h" |
| 14 #include "ui/events/test/event_generator.h" | 14 #include "ui/events/test/event_generator.h" |
| 15 #include "ui/views/animation/ink_drop_host.h" | 15 #include "ui/views/animation/ink_drop_host.h" |
| 16 #include "ui/views/animation/ink_drop_impl.h" | |
| 16 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" | 17 #include "ui/views/animation/test/ink_drop_host_view_test_api.h" |
| 17 #include "ui/views/animation/test/test_ink_drop.h" | 18 #include "ui/views/animation/test/test_ink_drop.h" |
| 18 #include "ui/views/animation/test/test_ink_drop_host.h" | 19 #include "ui/views/animation/test/test_ink_drop_host.h" |
| 19 #include "ui/views/context_menu_controller.h" | 20 #include "ui/views/context_menu_controller.h" |
| 20 #include "ui/views/controls/button/checkbox.h" | 21 #include "ui/views/controls/button/checkbox.h" |
| 21 #include "ui/views/controls/button/image_button.h" | 22 #include "ui/views/controls/button/image_button.h" |
| 22 #include "ui/views/controls/button/label_button.h" | 23 #include "ui/views/controls/button/label_button.h" |
| 23 #include "ui/views/controls/button/menu_button.h" | 24 #include "ui/views/controls/button/menu_button.h" |
| 24 #include "ui/views/controls/button/radio_button.h" | 25 #include "ui/views/controls/button/radio_button.h" |
| 25 #include "ui/views/controls/button/toggle_button.h" | 26 #include "ui/views/controls/button/toggle_button.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 ~TestCustomButton() override {} | 66 ~TestCustomButton() override {} |
| 66 | 67 |
| 67 void ButtonPressed(Button* sender, const ui::Event& event) override { | 68 void ButtonPressed(Button* sender, const ui::Event& event) override { |
| 68 pressed_ = true; | 69 pressed_ = true; |
| 69 } | 70 } |
| 70 | 71 |
| 71 void OnClickCanceled(const ui::Event& event) override { | 72 void OnClickCanceled(const ui::Event& event) override { |
| 72 canceled_ = true; | 73 canceled_ = true; |
| 73 } | 74 } |
| 74 | 75 |
| 76 // InkDropHostView: | |
| 77 void AddInkDropLayer(ui::Layer* ink_drop_layer) override { | |
|
bruthig
2016/10/18 14:31:26
Feel free to add this behavior to the TestInkDropH
tapted
2016/10/19 02:59:31
Done.
| |
| 78 ++ink_drop_layer_add_count_; | |
| 79 CustomButton::AddInkDropLayer(ink_drop_layer); | |
| 80 } | |
| 81 void RemoveInkDropLayer(ui::Layer* ink_drop_layer) override { | |
| 82 ++ink_drop_layer_remove_count_; | |
| 83 CustomButton::RemoveInkDropLayer(ink_drop_layer); | |
| 84 } | |
| 85 | |
| 75 bool pressed() { return pressed_; } | 86 bool pressed() { return pressed_; } |
| 76 bool canceled() { return canceled_; } | 87 bool canceled() { return canceled_; } |
| 88 int ink_drop_layer_add_count() { return ink_drop_layer_add_count_; } | |
| 89 int ink_drop_layer_remove_count() { return ink_drop_layer_remove_count_; } | |
| 77 | 90 |
| 78 void Reset() { | 91 void Reset() { |
| 79 pressed_ = false; | 92 pressed_ = false; |
| 80 canceled_ = false; | 93 canceled_ = false; |
| 81 } | 94 } |
| 82 | 95 |
| 83 // Raised visibility of OnFocus() to public | 96 // Raised visibility of OnFocus() to public |
| 84 void OnFocus() override { CustomButton::OnFocus(); } | 97 void OnFocus() override { CustomButton::OnFocus(); } |
| 85 | 98 |
| 86 private: | 99 private: |
| 87 bool pressed_ = false; | 100 bool pressed_ = false; |
| 88 bool canceled_ = false; | 101 bool canceled_ = false; |
| 89 | 102 |
| 103 int ink_drop_layer_add_count_ = 0; | |
| 104 int ink_drop_layer_remove_count_ = 0; | |
| 105 | |
| 90 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); | 106 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); |
| 91 }; | 107 }; |
| 92 | 108 |
| 93 } // namespace | 109 } // namespace |
| 94 | 110 |
| 95 class CustomButtonTest : public ViewsTestBase { | 111 class CustomButtonTest : public ViewsTestBase { |
| 96 public: | 112 public: |
| 97 CustomButtonTest() {} | 113 CustomButtonTest() {} |
| 98 ~CustomButtonTest() override {} | 114 ~CustomButtonTest() override {} |
| 99 | 115 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 119 } | 135 } |
| 120 | 136 |
| 121 void CreateButtonWithInkDrop(std::unique_ptr<InkDrop> ink_drop, | 137 void CreateButtonWithInkDrop(std::unique_ptr<InkDrop> ink_drop, |
| 122 bool has_ink_drop_action_on_click) { | 138 bool has_ink_drop_action_on_click) { |
| 123 delete button_; | 139 delete button_; |
| 124 button_ = new TestCustomButton(has_ink_drop_action_on_click); | 140 button_ = new TestCustomButton(has_ink_drop_action_on_click); |
| 125 InkDropHostViewTestApi(button_).SetInkDrop(std::move(ink_drop)); | 141 InkDropHostViewTestApi(button_).SetInkDrop(std::move(ink_drop)); |
| 126 widget_->SetContentsView(button_); | 142 widget_->SetContentsView(button_); |
| 127 } | 143 } |
| 128 | 144 |
| 145 void CreateButtonWithRealInkDrop() { | |
| 146 delete button_; | |
| 147 button_ = new TestCustomButton(false); | |
| 148 InkDropHostViewTestApi(button_).SetInkDrop( | |
| 149 base::MakeUnique<InkDropImpl>(button_)); | |
| 150 widget_->SetContentsView(button_); | |
| 151 } | |
| 152 | |
| 129 protected: | 153 protected: |
| 130 Widget* widget() { return widget_.get(); } | 154 Widget* widget() { return widget_.get(); } |
| 131 TestCustomButton* button() { return button_; } | 155 TestCustomButton* button() { return button_; } |
| 132 void SetDraggedView(View* dragged_view) { | 156 void SetDraggedView(View* dragged_view) { |
| 133 widget_->dragged_view_ = dragged_view; | 157 widget_->dragged_view_ = dragged_view; |
| 134 } | 158 } |
| 135 | 159 |
| 136 void SimulateKeyEvent(ui::KeyEvent* event) { widget()->OnKeyEvent(event); } | 160 void SimulateKeyEvent(ui::KeyEvent* event) { widget()->OnKeyEvent(event); } |
| 137 | 161 |
| 138 private: | 162 private: |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 | 588 |
| 565 button()->OnMouseDragged(ui::MouseEvent( | 589 button()->OnMouseDragged(ui::MouseEvent( |
| 566 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), | 590 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), |
| 567 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | 591 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 568 | 592 |
| 569 EXPECT_EQ(InkDropState::HIDDEN, ink_drop->GetTargetInkDropState()); | 593 EXPECT_EQ(InkDropState::HIDDEN, ink_drop->GetTargetInkDropState()); |
| 570 | 594 |
| 571 SetDraggedView(nullptr); | 595 SetDraggedView(nullptr); |
| 572 } | 596 } |
| 573 | 597 |
| 598 // Test that hiding an already-hidden ink drop doesn't add layers. | |
| 599 TEST_F(CustomButtonTest, AlwaysHiddenInkDropHasNoLayers) { | |
|
bruthig
2016/10/18 14:31:26
I think this test would make more sense in InkDrop
tapted
2016/10/19 02:59:31
Hm - I wanted to include coverage of the NativeWid
| |
| 600 CreateButtonWithRealInkDrop(); | |
| 601 | |
| 602 EXPECT_TRUE(button()->visible()); | |
| 603 EXPECT_FALSE(button()->layer()); | |
| 604 | |
| 605 button()->SetVisible(false); | |
| 606 EXPECT_FALSE(button()->visible()); | |
| 607 | |
| 608 // The animation may immediately end and remove the layer. So count calls as | |
| 609 // well as checking for the presence of a layer. | |
| 610 EXPECT_FALSE(button()->layer()); | |
| 611 EXPECT_EQ(0, button()->ink_drop_layer_add_count()); | |
| 612 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); | |
| 613 | |
| 614 // Sanity check that making the button visible and clicking it should add a | |
| 615 // layer for the ripple. | |
| 616 button()->SetVisible(true); | |
| 617 EXPECT_TRUE(button()->visible()); | |
| 618 | |
| 619 // No expected change when just making the button visible. | |
| 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 ui::test::EventGenerator generator(widget()->GetNativeWindow()); | |
| 625 generator.PressLeftButton(); | |
| 626 EXPECT_TRUE(button()->layer()); | |
| 627 EXPECT_EQ(1, button()->ink_drop_layer_add_count()); | |
| 628 | |
| 629 // The animation is running here, so the layer won't be removed. | |
| 630 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); | |
| 631 | |
| 632 // This test doesn't wait for the animation to finish. | |
| 633 } | |
| 634 | |
| 574 // Todo(karandeepb): On Mac, a button should get clicked on a Space key press | 635 // 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. | 636 // (and not release). Modify this test after fixing crbug.com/607429. |
| 576 // Test that Space Key behaves correctly on a focused button. | 637 // Test that Space Key behaves correctly on a focused button. |
| 577 TEST_F(CustomButtonTest, ClickOnSpace) { | 638 TEST_F(CustomButtonTest, ClickOnSpace) { |
| 578 // Give focus to the button. | 639 // Give focus to the button. |
| 579 button()->SetFocusForPlatform(); | 640 button()->SetFocusForPlatform(); |
| 580 button()->RequestFocus(); | 641 button()->RequestFocus(); |
| 581 EXPECT_EQ(button(), widget()->GetFocusManager()->GetFocusedView()); | 642 EXPECT_EQ(button(), widget()->GetFocusManager()->GetFocusedView()); |
| 582 | 643 |
| 583 ui::KeyEvent space_press(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE); | 644 ui::KeyEvent space_press(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE); |
| 584 SimulateKeyEvent(&space_press); | 645 SimulateKeyEvent(&space_press); |
| 585 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); | 646 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); |
| 586 EXPECT_FALSE(button()->pressed()); | 647 EXPECT_FALSE(button()->pressed()); |
| 587 | 648 |
| 588 ui::KeyEvent space_release(ui::ET_KEY_RELEASED, ui::VKEY_SPACE, ui::EF_NONE); | 649 ui::KeyEvent space_release(ui::ET_KEY_RELEASED, ui::VKEY_SPACE, ui::EF_NONE); |
| 589 SimulateKeyEvent(&space_release); | 650 SimulateKeyEvent(&space_release); |
| 590 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state()); | 651 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state()); |
| 591 EXPECT_TRUE(button()->pressed()); | 652 EXPECT_TRUE(button()->pressed()); |
| 592 } | 653 } |
| 593 | 654 |
| 594 } // namespace views | 655 } // namespace views |
| OLD | NEW |