Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: views/focus/focus_manager_unittest.cc

Issue 214029: Adding focus to NaviteViewHost. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « views/controls/native_control_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 // WindowDelegate Implementation. 158 // WindowDelegate Implementation.
159 virtual View* GetContentsView() { 159 virtual View* GetContentsView() {
160 if (!content_view_) 160 if (!content_view_)
161 content_view_ = new View(); 161 content_view_ = new View();
162 return content_view_; 162 return content_view_;
163 } 163 }
164 164
165 virtual void InitContentView() { 165 virtual void InitContentView() {
166 } 166 }
167 167
168 gfx::NativeView CreateChildNativeView(gfx::NativeView parent) {
169 #if defined(OS_WIN)
170 const wchar_t* kChildClassName = L"FocusTestChildClass";
171 WNDCLASS wnd_class = { 0 };
172 if (!::GetClassInfo(::GetModuleHandle(NULL), kChildClassName, &wnd_class)) {
173 // Let's register our dummy class.
174 wnd_class.lpfnWndProc = ::DefWindowProc;
175 wnd_class.hInstance = ::GetModuleHandle(NULL);
176 wnd_class.lpszClassName = kChildClassName;
177 ATOM atom = RegisterClass(&wnd_class);
178 }
179 return ::CreateWindow(kChildClassName, NULL, WS_CHILD, 0, 0, 0, 0, parent,
180 NULL, NULL, NULL);
181 #else
182 GtkWidget* widget = gtk_link_button_new("stupid button");
183 if (parent)
184 gtk_container_add(GTK_CONTAINER(parent), widget);
185 return widget;
186 #endif
187 }
188
189 gfx::NativeView CreateContainerNativeView() {
190 #if defined(OS_WIN)
191 const wchar_t* kTopClassName = L"FocusTestTopClass";
192 WNDCLASS wnd_class = { 0 };
193 if (!::GetClassInfo(::GetModuleHandle(NULL), kTopClassName, &wnd_class)) {
194 // Let's register our dummy class.
195 wnd_class.lpfnWndProc = ::DefWindowProc;
196 wnd_class.hInstance = ::GetModuleHandle(NULL);
197 wnd_class.lpszClassName = kTopClassName;
198 ATOM atom = RegisterClass(&wnd_class);
199 }
200 // Create a top window HWND
201 return ::CreateWindow(kTopClassName, NULL, 0, 0, 0, 0, 0, 0,
202 NULL, NULL, NULL);
203 #else
204 return gtk_fixed_new();
205 #endif
206 }
207
168 protected: 208 protected:
169 virtual gfx::Rect bounds() { 209 virtual gfx::Rect bounds() {
170 return gfx::Rect(0, 0, 500, 500); 210 return gfx::Rect(0, 0, 500, 500);
171 } 211 }
172 212
173 // Mocks activating/deactivating the window. 213 // Mocks activating/deactivating the window.
174 void SimulateActivateWindow() { 214 void SimulateActivateWindow() {
175 #if defined(OS_WIN) 215 #if defined(OS_WIN)
176 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); 216 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL);
177 #else 217 #else
(...skipping 30 matching lines...) Expand all
208 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); 248 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest);
209 }; 249 };
210 250
211 // BorderView is a view containing a native window with its own view hierarchy. 251 // BorderView is a view containing a native window with its own view hierarchy.
212 // It is interesting to test focus traversal from a view hierarchy to an inner 252 // It is interesting to test focus traversal from a view hierarchy to an inner
213 // view hierarchy. 253 // view hierarchy.
214 class BorderView : public NativeViewHost { 254 class BorderView : public NativeViewHost {
215 public: 255 public:
216 explicit BorderView(View* child) : child_(child), widget_(NULL) { 256 explicit BorderView(View* child) : child_(child), widget_(NULL) {
217 DCHECK(child); 257 DCHECK(child);
218 SetFocusable(false); 258 // This is a container and no view should get focused when its associated
259 // native view gets the focus.
260 set_focus_view(NULL);
219 } 261 }
220 262
221 virtual ~BorderView() {} 263 virtual ~BorderView() {}
222 264
223 virtual RootView* GetContentsRootView() { 265 virtual RootView* GetContentsRootView() {
224 return widget_->GetRootView(); 266 return widget_->GetRootView();
225 } 267 }
226 268
227 virtual FocusTraversable* GetFocusTraversable() { 269 virtual FocusTraversable* GetFocusTraversable() {
228 return widget_->GetRootView(); 270 return widget_->GetRootView();
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 EXPECT_EQ(combobox, GetFocusManager()->GetFocusedView()); 851 EXPECT_EQ(combobox, GetFocusManager()->GetFocusedView());
810 852
811 FocusNativeView(tabbed_pane->TestGetNativeControlView()); 853 FocusNativeView(tabbed_pane->TestGetNativeControlView());
812 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView()); 854 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView());
813 855
814 FocusNativeView(tab_button->TestGetNativeControlView()); 856 FocusNativeView(tab_button->TestGetNativeControlView());
815 EXPECT_EQ(tab_button, GetFocusManager()->GetFocusedView()); 857 EXPECT_EQ(tab_button, GetFocusManager()->GetFocusedView());
816 } 858 }
817 #endif 859 #endif
818 860
861 // A simple view we use to contain a NativeViewHost.
862 // The only thing it does is not mess with the native focus when focused.
863 class NoNativeFocusView : public View {
864 public:
865 NoNativeFocusView() {
866 SetFocusable(true);
867 }
868 virtual void Focus() {
869 }
870 };
871
872 // Tests that the NativeViewHost class sets the focus View appropriately on the
873 // FocusManager.
874 TEST_F(FocusManagerTest, FocusNativeViewHost) {
875 {
876 // Test wrapping a simple native view.
877 gfx::NativeView native_view =
878 CreateChildNativeView(window_->GetNativeWindow());
879 NativeViewHost* native_view_host = new NativeViewHost();
880 content_view_->AddChildView(native_view_host);
881 native_view_host->Attach(native_view);
882 FocusNativeView(native_view);
883 EXPECT_EQ(native_view_host, GetFocusManager()->GetFocusedView());
884 GetFocusManager()->ClearFocus();
885 }
886
887 {
888 // Test with nested native views, making sure set_focus_native_view() works.
889 gfx::NativeView top_native_view = CreateContainerNativeView();
890 gfx::NativeView child_native_view = CreateChildNativeView(top_native_view);
891 NativeViewHost* native_view_host = new NativeViewHost();
892 native_view_host->set_focus_native_view(child_native_view);
893 content_view_->AddChildView(native_view_host);
894 native_view_host->Attach(top_native_view);
895
896 // Focus the top native view, that shouldn't change the focus.
897 // (Note this isn't a case that we expect to happen.)
898 FocusNativeView(top_native_view);
899 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView());
900 // Focus the inner HWND, the focused view should change.
901 FocusNativeView(child_native_view);
902 EXPECT_EQ(native_view_host, GetFocusManager()->GetFocusedView());
903 GetFocusManager()->ClearFocus();
904 }
905
906 {
907 // Now also make sure set_focused_view() works.
908 gfx::NativeView native_view = CreateChildNativeView(
909 window_->GetNativeWindow());
910 NativeViewHost* native_view_host = new NativeViewHost();
911 NoNativeFocusView* container_view = new NoNativeFocusView();
912 container_view->AddChildView(native_view_host);
913 content_view_->AddChildView(container_view);
914 native_view_host->set_focus_view(container_view);
915 native_view_host->Attach(native_view);
916 FocusNativeView(native_view);
917 EXPECT_EQ(container_view, GetFocusManager()->GetFocusedView());
918 }
919 }
920
819 // Test that when activating/deactivating the top window, the focus is stored/ 921 // Test that when activating/deactivating the top window, the focus is stored/
820 // restored properly. 922 // restored properly.
821 TEST_F(FocusManagerTest, FocusStoreRestore) { 923 TEST_F(FocusManagerTest, FocusStoreRestore) {
822 NativeButton* button = new NativeButton(NULL, L"Press me"); 924 NativeButton* button = new NativeButton(NULL, L"Press me");
823 View* view = new View(); 925 View* view = new View();
824 view->SetFocusable(true); 926 view->SetFocusable(true);
825 927
826 content_view_->AddChildView(button); 928 content_view_->AddChildView(button);
827 button->SetBounds(10, 10, 200, 30); 929 button->SetBounds(10, 10, 200, 30);
828 content_view_->AddChildView(view); 930 content_view_->AddChildView(view);
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1157 EXPECT_EQ(target.accelerator_count(), 1); 1259 EXPECT_EQ(target.accelerator_count(), 1);
1158 EXPECT_EQ(NULL, 1260 EXPECT_EQ(NULL,
1159 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); 1261 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
1160 1262
1161 // Hitting the return key again; nothing should happen. 1263 // Hitting the return key again; nothing should happen.
1162 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); 1264 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1163 EXPECT_EQ(target.accelerator_count(), 1); 1265 EXPECT_EQ(target.accelerator_count(), 1);
1164 } 1266 }
1165 1267
1166 } // namespace views 1268 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/native_control_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698