diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-03 15:31:59 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-03 15:31:59 +0000 |
commit | f5965773a0783a57aa1ca3f0d649d69794b5b18c (patch) | |
tree | 496ffe1792dd8d6a57f9c2e69fee6d512602b5db /ui | |
parent | 2e00101307973c6b48e63c09b7b47cd0fd4a47b2 (diff) | |
download | chromium_src-f5965773a0783a57aa1ca3f0d649d69794b5b18c.zip chromium_src-f5965773a0783a57aa1ca3f0d649d69794b5b18c.tar.gz chromium_src-f5965773a0783a57aa1ca3f0d649d69794b5b18c.tar.bz2 |
Verify that:
- RequestFocus() works.
- Removing the focused view from the view hierarchy resets the focused view in the FocusManager.
BUG=none
TEST=unittest
Review URL: http://codereview.chromium.org/6286073
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@73621 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/views/focus/focus_manager.cc | 5 | ||||
-rw-r--r-- | ui/views/focus/focus_manager.h | 5 | ||||
-rw-r--r-- | ui/views/views.gyp | 1 | ||||
-rw-r--r-- | ui/views/widget/root_view.cc | 6 | ||||
-rw-r--r-- | ui/views/widget/root_view_unittest.cc | 42 | ||||
-rw-r--r-- | ui/views/widget/widget_test_util.cc | 6 | ||||
-rw-r--r-- | ui/views/widget/widget_test_util.h | 2 |
7 files changed, 56 insertions, 11 deletions
diff --git a/ui/views/focus/focus_manager.cc b/ui/views/focus/focus_manager.cc index 5039918..79c1206 100644 --- a/ui/views/focus/focus_manager.cc +++ b/ui/views/focus/focus_manager.cc @@ -520,11 +520,6 @@ FocusManager* FocusManager::GetFocusManagerForNativeWindow( return native_widget ? native_widget->GetWidget()->GetFocusManager() : NULL; } -void FocusManager::ViewRemoved(View* parent, View* removed) { - if (focused_view_ && focused_view_ == removed) - ClearFocus(); -} - void FocusManager::AddFocusChangeListener(FocusChangeListener* listener) { DCHECK(std::find(focus_change_listeners_.begin(), focus_change_listeners_.end(), listener) == diff --git a/ui/views/focus/focus_manager.h b/ui/views/focus/focus_manager.h index 8169d63..f1bb19e 100644 --- a/ui/views/focus/focus_manager.h +++ b/ui/views/focus/focus_manager.h @@ -252,11 +252,6 @@ class FocusManager { // Returns true if an accelerator was activated. bool ProcessAccelerator(const Accelerator& accelerator); - // Called by a RootView when a view within its hierarchy is removed from its - // parent. This will only be called by a RootView in a hierarchy of Widgets - // that this FocusManager is attached to the parent Widget of. - void ViewRemoved(View* parent, View* removed); - // Adds/removes a listener. The FocusChangeListener is notified every time // the focused view is about to change. void AddFocusChangeListener(FocusChangeListener* listener); diff --git a/ui/views/views.gyp b/ui/views/views.gyp index 82be13a..3fb91fc 100644 --- a/ui/views/views.gyp +++ b/ui/views/views.gyp @@ -132,6 +132,7 @@ 'run_all_unittests.cc', 'view_unittest.cc', 'widget/native_widget_win_unittest.cc', + 'widget/root_view_unittest.cc', 'widget/widget_test_util.cc', 'widget/widget_test_util.h', 'widget/widget_unittest.cc', diff --git a/ui/views/widget/root_view.cc b/ui/views/widget/root_view.cc index 6ca3cb3..1e11ee0 100644 --- a/ui/views/widget/root_view.cc +++ b/ui/views/widget/root_view.cc @@ -4,6 +4,7 @@ #include "ui/views/widget/root_view.h" +#include "ui/views/focus/focus_manager.h" #include "ui/views/layout/fill_layout.h" #include "ui/views/widget/widget.h" @@ -30,6 +31,11 @@ RootView::~RootView() { void RootView::OnViewRemoved(View* parent, View* child) { if (child == mouse_pressed_handler_) mouse_pressed_handler_ = NULL; + + // Clear focus if the removed child was focused. + FocusManager* focus_manager = GetFocusManager(); + if (focus_manager && focus_manager->focused_view() == child) + focus_manager->ClearFocus(); } bool RootView::OnKeyPressed(const KeyEvent& event) { diff --git a/ui/views/widget/root_view_unittest.cc b/ui/views/widget/root_view_unittest.cc new file mode 100644 index 0000000..71b2161 --- /dev/null +++ b/ui/views/widget/root_view_unittest.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/logging.h" +#include "base/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/views/view.h" +#include "ui/views/widget/widget.h" +#include "ui/views/widget/widget_test_util.h" + +namespace ui { +namespace internal { + +class RootViewTest : public testing::Test { + public: + RootViewTest() {} + virtual ~RootViewTest() {} + + private: + DISALLOW_COPY_AND_ASSIGN(RootViewTest); +}; + +TEST_F(RootViewTest, FocusedViewResetOnViewRemoval) { + View v; + v.set_parent_owned(false); + scoped_ptr<Widget> widget(CreateWidgetWithContents(&v)); + + v.RequestFocus(); + + FocusManager* focus_manager = widget->GetFocusManager(); + EXPECT_TRUE(focus_manager != NULL); + EXPECT_EQ(&v, focus_manager->focused_view()); + + v.parent()->RemoveChildView(&v); + + EXPECT_NE(&v, focus_manager->focused_view()); + EXPECT_EQ(NULL, focus_manager->focused_view()); +} + +} // namespace internal +} // namespace ui diff --git a/ui/views/widget/widget_test_util.cc b/ui/views/widget/widget_test_util.cc index 8382ea8..19b49df 100644 --- a/ui/views/widget/widget_test_util.cc +++ b/ui/views/widget/widget_test_util.cc @@ -11,7 +11,11 @@ namespace ui { namespace internal { Widget* CreateWidget() { - Widget* widget = new Widget(new View); + return CreateWidgetWithContents(new View); +} + +Widget* CreateWidgetWithContents(View* contents_view) { + Widget* widget = new Widget(contents_view); widget->set_delete_on_destroy(false); widget->InitWithNativeViewParent(NULL, gfx::Rect(10, 10, 200, 200)); return widget; diff --git a/ui/views/widget/widget_test_util.h b/ui/views/widget/widget_test_util.h index 35df923..ca6a0d0 100644 --- a/ui/views/widget/widget_test_util.h +++ b/ui/views/widget/widget_test_util.h @@ -7,11 +7,13 @@ #pragma once namespace ui { +class View; class Widget; namespace internal { // Create dummy Widgets for use in testing. Widget* CreateWidget(); +Widget* CreateWidgetWithContents(View* contents_view); Widget* CreateWidgetWithParent(Widget* parent); } // namespace internal |