summaryrefslogtreecommitdiffstats
path: root/views/focus
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-24 19:54:15 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-24 19:54:15 +0000
commite8a9a7472f544a839cbc47b8279d3ce99b0cf0a0 (patch)
tree8d784135c8791292a9413904f6b4dedbee89ab3a /views/focus
parent1e4a731e18cc375d537776576d379d4349c0b86c (diff)
downloadchromium_src-e8a9a7472f544a839cbc47b8279d3ce99b0cf0a0.zip
chromium_src-e8a9a7472f544a839cbc47b8279d3ce99b0cf0a0.tar.gz
chromium_src-e8a9a7472f544a839cbc47b8279d3ce99b0cf0a0.tar.bz2
Refactoring some of the NativeViewHost and NativeControl focus management so their consumers don't have to explicitly set the focused view.
BUG=None TEST=Run all tests. Make sure focus is stored/restored properly in Chrome. Review URL: http://codereview.chromium.org/214029 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27113 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/focus')
-rw-r--r--views/focus/focus_manager_unittest.cc104
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..75cbd2c 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(window_->GetNativeWindow());
+ 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(
+ window_->GetNativeWindow());
+ 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) {