diff options
Diffstat (limited to 'ui/views/focus')
-rw-r--r-- | ui/views/focus/focus_manager_test.cc | 35 | ||||
-rw-r--r-- | ui/views/focus/focus_manager_test.h | 30 | ||||
-rw-r--r-- | ui/views/focus/focus_manager_unittest.cc | 39 |
3 files changed, 101 insertions, 3 deletions
diff --git a/ui/views/focus/focus_manager_test.cc b/ui/views/focus/focus_manager_test.cc index da15b27..4344f12 100644 --- a/ui/views/focus/focus_manager_test.cc +++ b/ui/views/focus/focus_manager_test.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -14,7 +14,8 @@ namespace views { FocusManagerTest::FocusManagerTest() : contents_view_(new View), - focus_change_listener_(NULL) { + focus_change_listener_(NULL), + widget_focus_change_listener_(NULL) { } FocusManagerTest::~FocusManagerTest() { @@ -38,6 +39,10 @@ void FocusManagerTest::SetUp() { void FocusManagerTest::TearDown() { if (focus_change_listener_) GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_); + if (widget_focus_change_listener_) { + WidgetFocusManager::GetInstance()->RemoveFocusChangeListener( + widget_focus_change_listener_); + } GetWidget()->Close(); // Flush the message loop to make application verifiers happy. @@ -72,6 +77,13 @@ void FocusManagerTest::AddFocusChangeListener(FocusChangeListener* listener) { GetFocusManager()->AddFocusChangeListener(listener); } +void FocusManagerTest::AddWidgetFocusChangeListener( + WidgetFocusChangeListener* listener) { + ASSERT_FALSE(widget_focus_change_listener_); + widget_focus_change_listener_ = listener; + WidgetFocusManager::GetInstance()->AddFocusChangeListener(listener); +} + #if defined(OS_WIN) && !defined(USE_AURA) void FocusManagerTest::SimulateActivateWindow() { SendMessage(GetWidget()->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); @@ -111,4 +123,23 @@ void TestFocusChangeListener::ClearFocusChanges() { focus_changes_.clear(); } +//////////////////////////////////////////////////////////////////////////////// +// TestWidgetFocusChangeListener + +TestWidgetFocusChangeListener::TestWidgetFocusChangeListener() { +} + +TestWidgetFocusChangeListener::~TestWidgetFocusChangeListener() { +} + +void TestWidgetFocusChangeListener::ClearFocusChanges() { + focus_changes_.clear(); +} + +void TestWidgetFocusChangeListener::OnNativeFocusChange( + gfx::NativeView focused_before, + gfx::NativeView focused_now) { + focus_changes_.push_back(NativeViewPair(focused_before, focused_now)); +} + } // namespace views diff --git a/ui/views/focus/focus_manager_test.h b/ui/views/focus/focus_manager_test.h index 188ad23..f2c0102 100644 --- a/ui/views/focus/focus_manager_test.h +++ b/ui/views/focus/focus_manager_test.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -6,6 +6,7 @@ #define UI_VIEWS_FOCUS_FOCUS_MANAGER_TEST_H_ #include "ui/views/focus/focus_manager.h" +#include "ui/views/focus/widget_focus_manager.h" #include "ui/views/test/views_test_base.h" #include "ui/views/widget/widget_delegate.h" @@ -37,6 +38,7 @@ class FocusManagerTest : public ViewsTestBase, virtual void InitContentView(); void AddFocusChangeListener(FocusChangeListener* listener); + void AddWidgetFocusChangeListener(WidgetFocusChangeListener* listener); #if defined(OS_WIN) && !defined(USE_AURA) // Mocks activating/deactivating the window. @@ -50,6 +52,7 @@ class FocusManagerTest : public ViewsTestBase, private: View* contents_view_; FocusChangeListener* focus_change_listener_; + WidgetFocusChangeListener* widget_focus_change_listener_; DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); }; @@ -78,6 +81,31 @@ class TestFocusChangeListener : public FocusChangeListener { DISALLOW_COPY_AND_ASSIGN(TestFocusChangeListener); }; +typedef std::pair<gfx::NativeView, gfx::NativeView> NativeViewPair; + +// Use to record widget focus change notifications. +class TestWidgetFocusChangeListener : public WidgetFocusChangeListener { + public: + TestWidgetFocusChangeListener(); + virtual ~TestWidgetFocusChangeListener(); + + const std::vector<NativeViewPair>& focus_changes() const { + return focus_changes_; + } + void ClearFocusChanges(); + + // Overridden from WidgetFocusChangeListener: + virtual void OnNativeFocusChange(gfx::NativeView focused_before, + gfx::NativeView focused_now) OVERRIDE; + + private: + // Pairs of (focused_before, focused_now) parameters we've received via calls + // to OnNativeFocusChange(), in oldest-to-newest-received order. + std::vector<NativeViewPair> focus_changes_; + + DISALLOW_COPY_AND_ASSIGN(TestWidgetFocusChangeListener); +}; + } // namespace views #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_TEST_H_ diff --git a/ui/views/focus/focus_manager_unittest.cc b/ui/views/focus/focus_manager_unittest.cc index b121e44..c545bb8 100644 --- a/ui/views/focus/focus_manager_unittest.cc +++ b/ui/views/focus/focus_manager_unittest.cc @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <utility> +#include <vector> + #include "base/utf_string_conversions.h" #include "ui/base/accelerators/accelerator.h" #include "ui/base/keycodes/keyboard_codes.h" @@ -10,6 +13,7 @@ #include "ui/views/focus/accelerator_handler.h" #include "ui/views/focus/focus_manager_factory.h" #include "ui/views/focus/focus_manager_test.h" +#include "ui/views/focus/widget_focus_manager.h" #include "ui/views/widget/widget.h" #if !defined(USE_AURA) @@ -115,6 +119,41 @@ TEST_F(FocusManagerTest, FocusChangeListener) { EXPECT_TRUE(listener.focus_changes()[0] == ViewPair(view2, null_view)); } +TEST_F(FocusManagerTest, WidgetFocusChangeListener) { + TestWidgetFocusChangeListener widget_listener; + AddWidgetFocusChangeListener(&widget_listener); + + Widget::InitParams params; + params.type = views::Widget::InitParams::TYPE_WINDOW; + params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; + params.bounds = gfx::Rect(10, 10, 100, 100); + params.parent_widget = GetWidget(); + + scoped_ptr<Widget> widget1(new Widget); + widget1->Init(params); + widget1->Show(); + + scoped_ptr<Widget> widget2(new Widget); + widget2->Init(params); + widget2->Show(); + + widget_listener.ClearFocusChanges(); + gfx::NativeView native_view1 = widget1->GetNativeView(); + GetWidget()->FocusNativeView(native_view1); + ASSERT_EQ(2, static_cast<int>(widget_listener.focus_changes().size())); + EXPECT_EQ(native_view1, widget_listener.focus_changes()[0].second); + EXPECT_EQ(native_view1, widget_listener.focus_changes()[1].second); + + widget_listener.ClearFocusChanges(); + gfx::NativeView native_view2 = widget2->GetNativeView(); + GetWidget()->FocusNativeView(native_view2); + ASSERT_EQ(2, static_cast<int>(widget_listener.focus_changes().size())); + EXPECT_EQ(NativeViewPair(native_view1, native_view2), + widget_listener.focus_changes()[0]); + EXPECT_EQ(NativeViewPair(native_view1, native_view2), + widget_listener.focus_changes()[1]); +} + #if !defined(USE_AURA) class TestTextfield : public Textfield { public: |