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

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

Issue 2607923002: MacViews: Handle Space and Return keys correctly for Buttons. (Closed)
Patch Set: Address review. Created 3 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') | ui/views/style/platform_style.h » ('j') | 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 "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/base/layout.h" 12 #include "ui/base/layout.h"
13 #include "ui/display/screen.h" 13 #include "ui/display/screen.h"
14 #include "ui/events/event_utils.h" 14 #include "ui/events/event_utils.h"
15 #include "ui/events/test/event_generator.h" 15 #include "ui/events/test/event_generator.h"
16 #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" 17 #include "ui/views/animation/ink_drop_impl.h"
18 #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"
19 #include "ui/views/animation/test/test_ink_drop.h" 19 #include "ui/views/animation/test/test_ink_drop.h"
20 #include "ui/views/animation/test/test_ink_drop_host.h" 20 #include "ui/views/animation/test/test_ink_drop_host.h"
21 #include "ui/views/context_menu_controller.h" 21 #include "ui/views/context_menu_controller.h"
22 #include "ui/views/controls/button/checkbox.h" 22 #include "ui/views/controls/button/checkbox.h"
23 #include "ui/views/controls/button/image_button.h" 23 #include "ui/views/controls/button/image_button.h"
24 #include "ui/views/controls/button/label_button.h" 24 #include "ui/views/controls/button/label_button.h"
25 #include "ui/views/controls/button/menu_button.h" 25 #include "ui/views/controls/button/menu_button.h"
26 #include "ui/views/controls/button/radio_button.h" 26 #include "ui/views/controls/button/radio_button.h"
27 #include "ui/views/controls/button/toggle_button.h" 27 #include "ui/views/controls/button/toggle_button.h"
28 #include "ui/views/controls/link.h" 28 #include "ui/views/controls/link.h"
29 #include "ui/views/controls/textfield/textfield.h" 29 #include "ui/views/controls/textfield/textfield.h"
30 #include "ui/views/style/platform_style.h"
30 #include "ui/views/test/views_test_base.h" 31 #include "ui/views/test/views_test_base.h"
31 32
32 #if defined(USE_AURA) 33 #if defined(USE_AURA)
33 #include "ui/aura/test/test_cursor_client.h" 34 #include "ui/aura/test/test_cursor_client.h"
34 #include "ui/aura/window.h" 35 #include "ui/aura/window.h"
35 #include "ui/aura/window_event_dispatcher.h" 36 #include "ui/aura/window_event_dispatcher.h"
36 #endif 37 #endif
37 38
38 namespace views { 39 namespace views {
39 40
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 widget_->SetContentsView(button_); 152 widget_->SetContentsView(button_);
152 } 153 }
153 154
154 protected: 155 protected:
155 Widget* widget() { return widget_.get(); } 156 Widget* widget() { return widget_.get(); }
156 TestCustomButton* button() { return button_; } 157 TestCustomButton* button() { return button_; }
157 void SetDraggedView(View* dragged_view) { 158 void SetDraggedView(View* dragged_view) {
158 widget_->dragged_view_ = dragged_view; 159 widget_->dragged_view_ = dragged_view;
159 } 160 }
160 161
161 void SimulateKeyEvent(ui::KeyEvent* event) { widget()->OnKeyEvent(event); }
162
163 private: 162 private:
164 std::unique_ptr<Widget> widget_; 163 std::unique_ptr<Widget> widget_;
165 TestCustomButton* button_ = nullptr; 164 TestCustomButton* button_ = nullptr;
166 165
167 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest); 166 DISALLOW_COPY_AND_ASSIGN(CustomButtonTest);
168 }; 167 };
169 168
170 // Tests that hover state changes correctly when visiblity/enableness changes. 169 // Tests that hover state changes correctly when visiblity/enableness changes.
171 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) { 170 TEST_F(CustomButtonTest, HoverStateOnVisibilityChange) {
172 ui::test::EventGenerator generator(widget()->GetNativeWindow()); 171 ui::test::EventGenerator generator(widget()->GetNativeWindow());
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); 634 EXPECT_EQ(0, button()->ink_drop_layer_remove_count());
636 635
637 base::RunLoop().RunUntilIdle(); // Complete the Close(). 636 base::RunLoop().RunUntilIdle(); // Complete the Close().
638 EXPECT_FALSE(button()->layer()); 637 EXPECT_FALSE(button()->layer());
639 EXPECT_EQ(0, button()->ink_drop_layer_add_count()); 638 EXPECT_EQ(0, button()->ink_drop_layer_add_count());
640 EXPECT_EQ(0, button()->ink_drop_layer_remove_count()); 639 EXPECT_EQ(0, button()->ink_drop_layer_remove_count());
641 640
642 delete button(); 641 delete button();
643 } 642 }
644 643
645 // Todo(karandeepb): On Mac, a button should get clicked on a Space key press 644 // Verify that the Space key clicks the button on key-press on Mac, and
646 // (and not release). Modify this test after fixing crbug.com/607429. 645 // key-release on other platforms.
647 // Test that Space Key behaves correctly on a focused button. 646 TEST_F(CustomButtonTest, ActionOnSpace) {
648 TEST_F(CustomButtonTest, ClickOnSpace) {
649 // Give focus to the button. 647 // Give focus to the button.
650 button()->SetFocusForPlatform(); 648 button()->SetFocusForPlatform();
651 button()->RequestFocus(); 649 button()->RequestFocus();
652 EXPECT_EQ(button(), widget()->GetFocusManager()->GetFocusedView()); 650 EXPECT_TRUE(button()->HasFocus());
653 651
654 ui::KeyEvent space_press(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE); 652 ui::KeyEvent space_press(ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE);
655 SimulateKeyEvent(&space_press); 653 EXPECT_TRUE(button()->OnKeyPressed(space_press));
654
655 #if defined(OS_MACOSX)
656 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state());
657 EXPECT_TRUE(button()->pressed());
658 #else
656 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); 659 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state());
657 EXPECT_FALSE(button()->pressed()); 660 EXPECT_FALSE(button()->pressed());
661 #endif
658 662
659 ui::KeyEvent space_release(ui::ET_KEY_RELEASED, ui::VKEY_SPACE, ui::EF_NONE); 663 ui::KeyEvent space_release(ui::ET_KEY_RELEASED, ui::VKEY_SPACE, ui::EF_NONE);
660 SimulateKeyEvent(&space_release); 664
665 #if defined(OS_MACOSX)
666 EXPECT_FALSE(button()->OnKeyReleased(space_release));
667 #else
668 EXPECT_TRUE(button()->OnKeyReleased(space_release));
669 #endif
670
661 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state()); 671 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state());
662 EXPECT_TRUE(button()->pressed()); 672 EXPECT_TRUE(button()->pressed());
663 } 673 }
664 674
675 // Verify that the Return key clicks the button on key-press on all platforms
676 // except Mac. On Mac, the Return key performs the default action associated
677 // with a dialog, even if a button has focus.
678 TEST_F(CustomButtonTest, ActionOnReturn) {
679 // Give focus to the button.
680 button()->SetFocusForPlatform();
681 button()->RequestFocus();
682 EXPECT_TRUE(button()->HasFocus());
683
684 ui::KeyEvent return_press(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE);
685
686 #if defined(OS_MACOSX)
687 EXPECT_FALSE(button()->OnKeyPressed(return_press));
688 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state());
689 EXPECT_FALSE(button()->pressed());
690 #else
691 EXPECT_TRUE(button()->OnKeyPressed(return_press));
692 EXPECT_EQ(CustomButton::STATE_NORMAL, button()->state());
693 EXPECT_TRUE(button()->pressed());
694 #endif
695
696 ui::KeyEvent return_release(ui::ET_KEY_RELEASED, ui::VKEY_RETURN,
697 ui::EF_NONE);
698 EXPECT_FALSE(button()->OnKeyReleased(return_release));
699 }
700
665 } // namespace views 701 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/custom_button.cc ('k') | ui/views/style/platform_style.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698