| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Disabled right now as this won't work on BuildBots right now as this test | 5 // Disabled right now as this won't work on BuildBots right now as this test |
| 6 // require the box it runs on to be unlocked (and no screen-savers). | 6 // require the box it runs on to be unlocked (and no screen-savers). |
| 7 // The test actually simulates mouse and key events, so if the screen is locked, | 7 // The test actually simulates mouse and key events, so if the screen is locked, |
| 8 // the events don't go to the Chrome window. | 8 // the events don't go to the Chrome window. |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 // WindowDelegate Implementation. | 165 // WindowDelegate Implementation. |
| 166 virtual View* GetContentsView() { | 166 virtual View* GetContentsView() { |
| 167 if (!content_view_) | 167 if (!content_view_) |
| 168 content_view_ = new View(); | 168 content_view_ = new View(); |
| 169 return content_view_; | 169 return content_view_; |
| 170 } | 170 } |
| 171 | 171 |
| 172 virtual void InitContentView() { | 172 virtual void InitContentView() { |
| 173 } | 173 } |
| 174 | 174 |
| 175 gfx::NativeView CreateChildNativeView(gfx::NativeView parent) { | |
| 176 #if defined(OS_WIN) | |
| 177 const wchar_t* kChildClassName = L"FocusTestChildClass"; | |
| 178 WNDCLASS wnd_class = { 0 }; | |
| 179 if (!::GetClassInfo(::GetModuleHandle(NULL), kChildClassName, &wnd_class)) { | |
| 180 // Let's register our dummy class. | |
| 181 wnd_class.lpfnWndProc = ::DefWindowProc; | |
| 182 wnd_class.hInstance = ::GetModuleHandle(NULL); | |
| 183 wnd_class.lpszClassName = kChildClassName; | |
| 184 ATOM atom = RegisterClass(&wnd_class); | |
| 185 } | |
| 186 return ::CreateWindow(kChildClassName, NULL, WS_CHILD, 0, 0, 0, 0, parent, | |
| 187 NULL, NULL, NULL); | |
| 188 #else | |
| 189 GtkWidget* widget = gtk_link_button_new("stupid button"); | |
| 190 if (parent) | |
| 191 gtk_container_add(GTK_CONTAINER(parent), widget); | |
| 192 return widget; | |
| 193 #endif | |
| 194 } | |
| 195 | |
| 196 gfx::NativeView CreateContainerNativeView() { | |
| 197 #if defined(OS_WIN) | |
| 198 const wchar_t* kTopClassName = L"FocusTestTopClass"; | |
| 199 WNDCLASS wnd_class = { 0 }; | |
| 200 if (!::GetClassInfo(::GetModuleHandle(NULL), kTopClassName, &wnd_class)) { | |
| 201 // Let's register our dummy class. | |
| 202 wnd_class.lpfnWndProc = ::DefWindowProc; | |
| 203 wnd_class.hInstance = ::GetModuleHandle(NULL); | |
| 204 wnd_class.lpszClassName = kTopClassName; | |
| 205 ATOM atom = RegisterClass(&wnd_class); | |
| 206 } | |
| 207 // Create a top window HWND | |
| 208 return ::CreateWindow(kTopClassName, NULL, 0, 0, 0, 0, 0, 0, | |
| 209 NULL, NULL, NULL); | |
| 210 #else | |
| 211 return gtk_fixed_new(); | |
| 212 #endif | |
| 213 } | |
| 214 | |
| 215 protected: | 175 protected: |
| 216 virtual gfx::Rect bounds() { | 176 virtual gfx::Rect bounds() { |
| 217 return gfx::Rect(0, 0, 500, 500); | 177 return gfx::Rect(0, 0, 500, 500); |
| 218 } | 178 } |
| 219 | 179 |
| 220 // Mocks activating/deactivating the window. | 180 // Mocks activating/deactivating the window. |
| 221 void SimulateActivateWindow() { | 181 void SimulateActivateWindow() { |
| 222 #if defined(OS_WIN) | 182 #if defined(OS_WIN) |
| 223 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); | 183 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); |
| 224 #else | 184 #else |
| (...skipping 30 matching lines...) Expand all Loading... |
| 255 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); | 215 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); |
| 256 }; | 216 }; |
| 257 | 217 |
| 258 // BorderView is a view containing a native window with its own view hierarchy. | 218 // BorderView is a view containing a native window with its own view hierarchy. |
| 259 // It is interesting to test focus traversal from a view hierarchy to an inner | 219 // It is interesting to test focus traversal from a view hierarchy to an inner |
| 260 // view hierarchy. | 220 // view hierarchy. |
| 261 class BorderView : public NativeViewHost { | 221 class BorderView : public NativeViewHost { |
| 262 public: | 222 public: |
| 263 explicit BorderView(View* child) : child_(child), widget_(NULL) { | 223 explicit BorderView(View* child) : child_(child), widget_(NULL) { |
| 264 DCHECK(child); | 224 DCHECK(child); |
| 265 // This is a container and no view should get focused when its associated | 225 SetFocusable(false); |
| 266 // native view gets the focus. | |
| 267 set_focus_view(NULL); | |
| 268 } | 226 } |
| 269 | 227 |
| 270 virtual ~BorderView() {} | 228 virtual ~BorderView() {} |
| 271 | 229 |
| 272 virtual RootView* GetContentsRootView() { | 230 virtual RootView* GetContentsRootView() { |
| 273 return widget_->GetRootView(); | 231 return widget_->GetRootView(); |
| 274 } | 232 } |
| 275 | 233 |
| 276 virtual FocusTraversable* GetFocusTraversable() { | 234 virtual FocusTraversable* GetFocusTraversable() { |
| 277 return widget_->GetRootView(); | 235 return widget_->GetRootView(); |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 EXPECT_EQ(combobox, GetFocusManager()->GetFocusedView()); | 816 EXPECT_EQ(combobox, GetFocusManager()->GetFocusedView()); |
| 859 | 817 |
| 860 FocusNativeView(tabbed_pane->TestGetNativeControlView()); | 818 FocusNativeView(tabbed_pane->TestGetNativeControlView()); |
| 861 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView()); | 819 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView()); |
| 862 | 820 |
| 863 FocusNativeView(tab_button->TestGetNativeControlView()); | 821 FocusNativeView(tab_button->TestGetNativeControlView()); |
| 864 EXPECT_EQ(tab_button, GetFocusManager()->GetFocusedView()); | 822 EXPECT_EQ(tab_button, GetFocusManager()->GetFocusedView()); |
| 865 } | 823 } |
| 866 #endif | 824 #endif |
| 867 | 825 |
| 868 // A simple view we use to contain a NativeViewHost. | |
| 869 // The only thing it does is not mess with the native focus when focused. | |
| 870 class NoNativeFocusView : public View { | |
| 871 public: | |
| 872 NoNativeFocusView() { | |
| 873 SetFocusable(true); | |
| 874 } | |
| 875 virtual void Focus() { | |
| 876 } | |
| 877 }; | |
| 878 | |
| 879 // Tests that the NativeViewHost class sets the focus View appropriately on the | |
| 880 // FocusManager. | |
| 881 TEST_F(FocusManagerTest, FocusNativeViewHost) { | |
| 882 { | |
| 883 // Test wrapping a simple native view. | |
| 884 gfx::NativeView top_native_view = CreateContainerNativeView(); | |
| 885 gfx::NativeView native_view = CreateChildNativeView(top_native_view); | |
| 886 NativeViewHost* native_view_host = new NativeViewHost(); | |
| 887 content_view_->AddChildView(native_view_host); | |
| 888 native_view_host->Attach(native_view); | |
| 889 FocusNativeView(native_view); | |
| 890 EXPECT_EQ(native_view_host, GetFocusManager()->GetFocusedView()); | |
| 891 GetFocusManager()->ClearFocus(); | |
| 892 } | |
| 893 | |
| 894 { | |
| 895 // Test with nested native views, making sure set_focus_native_view() works. | |
| 896 gfx::NativeView top_native_view = CreateContainerNativeView(); | |
| 897 gfx::NativeView child_native_view = CreateChildNativeView(top_native_view); | |
| 898 NativeViewHost* native_view_host = new NativeViewHost(); | |
| 899 native_view_host->set_focus_native_view(child_native_view); | |
| 900 content_view_->AddChildView(native_view_host); | |
| 901 native_view_host->Attach(top_native_view); | |
| 902 | |
| 903 // Focus the top native view, that shouldn't change the focus. | |
| 904 // (Note this isn't a case that we expect to happen.) | |
| 905 FocusNativeView(top_native_view); | |
| 906 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); | |
| 907 // Focus the inner HWND, the focused view should change. | |
| 908 FocusNativeView(child_native_view); | |
| 909 EXPECT_EQ(native_view_host, GetFocusManager()->GetFocusedView()); | |
| 910 GetFocusManager()->ClearFocus(); | |
| 911 } | |
| 912 | |
| 913 { | |
| 914 // Now also make sure set_focused_view() works. | |
| 915 gfx::NativeView top_native_view = CreateContainerNativeView(); | |
| 916 gfx::NativeView native_view = CreateChildNativeView(top_native_view); | |
| 917 NativeViewHost* native_view_host = new NativeViewHost(); | |
| 918 NoNativeFocusView* container_view = new NoNativeFocusView(); | |
| 919 container_view->AddChildView(native_view_host); | |
| 920 content_view_->AddChildView(container_view); | |
| 921 native_view_host->set_focus_view(container_view); | |
| 922 native_view_host->Attach(native_view); | |
| 923 FocusNativeView(native_view); | |
| 924 EXPECT_EQ(container_view, GetFocusManager()->GetFocusedView()); | |
| 925 } | |
| 926 } | |
| 927 | |
| 928 // Test that when activating/deactivating the top window, the focus is stored/ | 826 // Test that when activating/deactivating the top window, the focus is stored/ |
| 929 // restored properly. | 827 // restored properly. |
| 930 TEST_F(FocusManagerTest, FocusStoreRestore) { | 828 TEST_F(FocusManagerTest, FocusStoreRestore) { |
| 931 NativeButton* button = new NativeButton(NULL, L"Press me"); | 829 NativeButton* button = new NativeButton(NULL, L"Press me"); |
| 932 View* view = new View(); | 830 View* view = new View(); |
| 933 view->SetFocusable(true); | 831 view->SetFocusable(true); |
| 934 | 832 |
| 935 content_view_->AddChildView(button); | 833 content_view_->AddChildView(button); |
| 936 button->SetBounds(10, 10, 200, 30); | 834 button->SetBounds(10, 10, 200, 30); |
| 937 content_view_->AddChildView(view); | 835 content_view_->AddChildView(view); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1266 EXPECT_EQ(target.accelerator_count(), 1); | 1164 EXPECT_EQ(target.accelerator_count(), 1); |
| 1267 EXPECT_EQ(NULL, | 1165 EXPECT_EQ(NULL, |
| 1268 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | 1166 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); |
| 1269 | 1167 |
| 1270 // Hitting the return key again; nothing should happen. | 1168 // Hitting the return key again; nothing should happen. |
| 1271 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 1169 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 1272 EXPECT_EQ(target.accelerator_count(), 1); | 1170 EXPECT_EQ(target.accelerator_count(), 1); |
| 1273 } | 1171 } |
| 1274 | 1172 |
| 1275 } // namespace views | 1173 } // namespace views |
| OLD | NEW |