summaryrefslogtreecommitdiffstats
path: root/ui/views/widget
diff options
context:
space:
mode:
Diffstat (limited to 'ui/views/widget')
-rw-r--r--ui/views/widget/root_view.cc6
-rw-r--r--ui/views/widget/root_view_unittest.cc42
-rw-r--r--ui/views/widget/widget_test_util.cc6
-rw-r--r--ui/views/widget/widget_test_util.h2
4 files changed, 55 insertions, 1 deletions
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