From 71421c3fc725d9d55acba7a4233525f8b6e07350 Mon Sep 17 00:00:00 2001 From: "yutak@chromium.org" Date: Sat, 6 Jun 2009 00:41:44 +0000 Subject: Fix keyboard accelerator registration issue in views::View. View should not register the same accelerator target multiple times. BUG=13275 TEST=None Review URL: http://codereview.chromium.org/118242 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17810 0039d316-1c4b-4281-b951-d872f2087c98 --- views/view_unittest.cc | 83 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) (limited to 'views/view_unittest.cc') diff --git a/views/view_unittest.cc b/views/view_unittest.cc index cd0184e..91f3149 100644 --- a/views/view_unittest.cc +++ b/views/view_unittest.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include + #include "app/gfx/canvas.h" #include "app/gfx/path.h" #include "base/clipboard.h" @@ -109,7 +111,7 @@ class EmptyWindow : public CWindowImpl accelerator_count_map_; }; //////////////////////////////////////////////////////////////////////////////// @@ -702,6 +709,80 @@ TEST_F(ViewTest, TextfieldCutCopyPaste) { } #endif +//////////////////////////////////////////////////////////////////////////////// +// Accelerators +//////////////////////////////////////////////////////////////////////////////// +bool TestView::AcceleratorPressed(const Accelerator& accelerator) { + accelerator_count_map_[accelerator]++; + return true; +} + +#if defined(OS_WIN) +TEST_F(ViewTest, ActivateAccelerator) { + // Register a keyboard accelerator before the view is added to a window. + views::Accelerator return_accelerator(VK_RETURN, false, false, false); + TestView* view = new TestView(); + view->Reset(); + view->AddAccelerator(return_accelerator); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); + + // Create a window and add the view as its child. + WidgetWin window; + window.Init(NULL, gfx::Rect(0, 0, 100, 100), true); + window.set_delete_on_destroy(false); + window.set_window_style(WS_OVERLAPPEDWINDOW); + RootView* root = window.GetRootView(); + root->AddChildView(view); + + // Get the focus manager. + views::FocusManager* focus_manager = + views::FocusManager::GetFocusManager(window.GetNativeView()); + ASSERT_TRUE(focus_manager); + + // Hit the return key and see if it takes effect. + EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); + + // Hit the escape key. Nothing should happen. + views::Accelerator escape_accelerator(VK_ESCAPE, false, false, false); + EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0); + + // Now register the escape key and hit it again. + view->AddAccelerator(escape_accelerator); + EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1); + + // Remove the return key accelerator. + view->RemoveAccelerator(return_accelerator); + EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1); + + // Add it again. Hit the return key and the escape key. + view->AddAccelerator(return_accelerator); + EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1); + EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); + + // Remove all the accelerators. + view->ResetAccelerators(); + EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); + EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); + EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); + EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); + + window.CloseNow(); +} +#endif + #if defined(OS_WIN) //////////////////////////////////////////////////////////////////////////////// // Mouse-wheel message rerouting -- cgit v1.1