diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-29 23:15:10 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-29 23:15:10 +0000 |
commit | 0aaec000440c2453f20265a7b580efc4ad10b282 (patch) | |
tree | c1739e498cf814750a0c63495a3ebca358117261 /views/focus | |
parent | f5c8a1596de89f2f3010186b03cc2378d23254ae (diff) | |
download | chromium_src-0aaec000440c2453f20265a7b580efc4ad10b282.zip chromium_src-0aaec000440c2453f20265a7b580efc4ad10b282.tar.gz chromium_src-0aaec000440c2453f20265a7b580efc4ad10b282.tar.bz2 |
Relanding the NativeViewHost refactoring (it was breaking the ChromeOS build).
Refactoring some of the NativeViewHost and NativeControl focus management so their consumers don't have to explicitly set the focused view.
See original review:
http://codereview.chromium.org/235011/show
BUG=None
TEST=Run all tests. Make sure focus is stored/restored properly in Chrome.
TBR=ben
Review URL: http://codereview.chromium.org/246032
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27563 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/focus')
-rw-r--r-- | views/focus/focus_manager_unittest.cc | 104 |
1 files changed, 103 insertions, 1 deletions
diff --git a/views/focus/focus_manager_unittest.cc b/views/focus/focus_manager_unittest.cc index 29175ec..14a6578 100644 --- a/views/focus/focus_manager_unittest.cc +++ b/views/focus/focus_manager_unittest.cc @@ -165,6 +165,46 @@ class FocusManagerTest : public testing::Test, public WindowDelegate { virtual void InitContentView() { } + gfx::NativeView CreateChildNativeView(gfx::NativeView parent) { +#if defined(OS_WIN) + const wchar_t* kChildClassName = L"FocusTestChildClass"; + WNDCLASS wnd_class = { 0 }; + if (!::GetClassInfo(::GetModuleHandle(NULL), kChildClassName, &wnd_class)) { + // Let's register our dummy class. + wnd_class.lpfnWndProc = ::DefWindowProc; + wnd_class.hInstance = ::GetModuleHandle(NULL); + wnd_class.lpszClassName = kChildClassName; + ATOM atom = RegisterClass(&wnd_class); + } + return ::CreateWindow(kChildClassName, NULL, WS_CHILD, 0, 0, 0, 0, parent, + NULL, NULL, NULL); +#else + GtkWidget* widget = gtk_link_button_new("stupid button"); + if (parent) + gtk_container_add(GTK_CONTAINER(parent), widget); + return widget; +#endif + } + + gfx::NativeView CreateContainerNativeView() { +#if defined(OS_WIN) + const wchar_t* kTopClassName = L"FocusTestTopClass"; + WNDCLASS wnd_class = { 0 }; + if (!::GetClassInfo(::GetModuleHandle(NULL), kTopClassName, &wnd_class)) { + // Let's register our dummy class. + wnd_class.lpfnWndProc = ::DefWindowProc; + wnd_class.hInstance = ::GetModuleHandle(NULL); + wnd_class.lpszClassName = kTopClassName; + ATOM atom = RegisterClass(&wnd_class); + } + // Create a top window HWND + return ::CreateWindow(kTopClassName, NULL, 0, 0, 0, 0, 0, 0, + NULL, NULL, NULL); +#else + return gtk_fixed_new(); +#endif + } + protected: virtual gfx::Rect bounds() { return gfx::Rect(0, 0, 500, 500); @@ -215,7 +255,9 @@ class BorderView : public NativeViewHost { public: explicit BorderView(View* child) : child_(child), widget_(NULL) { DCHECK(child); - SetFocusable(false); + // This is a container and no view should get focused when its associated + // native view gets the focus. + set_focus_view(NULL); } virtual ~BorderView() {} @@ -816,6 +858,66 @@ TEST_F(FocusManagerTest, FocusNativeControls) { } #endif +// A simple view we use to contain a NativeViewHost. +// The only thing it does is not mess with the native focus when focused. +class NoNativeFocusView : public View { + public: + NoNativeFocusView() { + SetFocusable(true); + } + virtual void Focus() { + } +}; + +// Tests that the NativeViewHost class sets the focus View appropriately on the +// FocusManager. +TEST_F(FocusManagerTest, FocusNativeViewHost) { + { + // Test wrapping a simple native view. + gfx::NativeView native_view = + CreateChildNativeView(content_view_->GetWidget()->GetNativeView()); + NativeViewHost* native_view_host = new NativeViewHost(); + content_view_->AddChildView(native_view_host); + native_view_host->Attach(native_view); + FocusNativeView(native_view); + EXPECT_EQ(native_view_host, GetFocusManager()->GetFocusedView()); + GetFocusManager()->ClearFocus(); + } + + { + // Test with nested native views, making sure set_focus_native_view() works. + gfx::NativeView top_native_view = CreateContainerNativeView(); + gfx::NativeView child_native_view = CreateChildNativeView(top_native_view); + NativeViewHost* native_view_host = new NativeViewHost(); + native_view_host->set_focus_native_view(child_native_view); + content_view_->AddChildView(native_view_host); + native_view_host->Attach(top_native_view); + + // Focus the top native view, that shouldn't change the focus. + // (Note this isn't a case that we expect to happen.) + FocusNativeView(top_native_view); + EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); + // Focus the inner HWND, the focused view should change. + FocusNativeView(child_native_view); + EXPECT_EQ(native_view_host, GetFocusManager()->GetFocusedView()); + GetFocusManager()->ClearFocus(); + } + + { + // Now also make sure set_focused_view() works. + gfx::NativeView native_view = + CreateChildNativeView(content_view_->GetWidget()->GetNativeView()); + NativeViewHost* native_view_host = new NativeViewHost(); + NoNativeFocusView* container_view = new NoNativeFocusView(); + container_view->AddChildView(native_view_host); + content_view_->AddChildView(container_view); + native_view_host->set_focus_view(container_view); + native_view_host->Attach(native_view); + FocusNativeView(native_view); + EXPECT_EQ(container_view, GetFocusManager()->GetFocusedView()); + } +} + // Test that when activating/deactivating the top window, the focus is stored/ // restored properly. TEST_F(FocusManagerTest, FocusStoreRestore) { |