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

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

Issue 231022: Reverting 27113. (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
208 protected: 168 protected:
209 virtual gfx::Rect bounds() { 169 virtual gfx::Rect bounds() {
210 return gfx::Rect(0, 0, 500, 500); 170 return gfx::Rect(0, 0, 500, 500);
211 } 171 }
212 172
213 // Mocks activating/deactivating the window. 173 // Mocks activating/deactivating the window.
214 void SimulateActivateWindow() { 174 void SimulateActivateWindow() {
215 #if defined(OS_WIN) 175 #if defined(OS_WIN)
216 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); 176 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL);
217 #else 177 #else
(...skipping 30 matching lines...) Expand all
248 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); 208 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest);
249 }; 209 };
250 210
251 // BorderView is a view containing a native window with its own view hierarchy. 211 // BorderView is a view containing a native window with its own view hierarchy.
252 // It is interesting to test focus traversal from a view hierarchy to an inner 212 // It is interesting to test focus traversal from a view hierarchy to an inner
253 // view hierarchy. 213 // view hierarchy.
254 class BorderView : public NativeViewHost { 214 class BorderView : public NativeViewHost {
255 public: 215 public:
256 explicit BorderView(View* child) : child_(child), widget_(NULL) { 216 explicit BorderView(View* child) : child_(child), widget_(NULL) {
257 DCHECK(child); 217 DCHECK(child);
258 // This is a container and no view should get focused when its associated 218 SetFocusable(false);
259 // native view gets the focus.
260 set_focus_view(NULL);
261 } 219 }
262 220
263 virtual ~BorderView() {} 221 virtual ~BorderView() {}
264 222
265 virtual RootView* GetContentsRootView() { 223 virtual RootView* GetContentsRootView() {
266 return widget_->GetRootView(); 224 return widget_->GetRootView();
267 } 225 }
268 226
269 virtual FocusTraversable* GetFocusTraversable() { 227 virtual FocusTraversable* GetFocusTraversable() {
270 return widget_->GetRootView(); 228 return widget_->GetRootView();
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 EXPECT_EQ(combobox, GetFocusManager()->GetFocusedView()); 809 EXPECT_EQ(combobox, GetFocusManager()->GetFocusedView());
852 810
853 FocusNativeView(tabbed_pane->TestGetNativeControlView()); 811 FocusNativeView(tabbed_pane->TestGetNativeControlView());
854 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView()); 812 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView());
855 813
856 FocusNativeView(tab_button->TestGetNativeControlView()); 814 FocusNativeView(tab_button->TestGetNativeControlView());
857 EXPECT_EQ(tab_button, GetFocusManager()->GetFocusedView()); 815 EXPECT_EQ(tab_button, GetFocusManager()->GetFocusedView());
858 } 816 }
859 #endif 817 #endif
860 818
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
921 // Test that when activating/deactivating the top window, the focus is stored/ 819 // Test that when activating/deactivating the top window, the focus is stored/
922 // restored properly. 820 // restored properly.
923 TEST_F(FocusManagerTest, FocusStoreRestore) { 821 TEST_F(FocusManagerTest, FocusStoreRestore) {
924 NativeButton* button = new NativeButton(NULL, L"Press me"); 822 NativeButton* button = new NativeButton(NULL, L"Press me");
925 View* view = new View(); 823 View* view = new View();
926 view->SetFocusable(true); 824 view->SetFocusable(true);
927 825
928 content_view_->AddChildView(button); 826 content_view_->AddChildView(button);
929 button->SetBounds(10, 10, 200, 30); 827 button->SetBounds(10, 10, 200, 30);
930 content_view_->AddChildView(view); 828 content_view_->AddChildView(view);
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1259 EXPECT_EQ(target.accelerator_count(), 1); 1157 EXPECT_EQ(target.accelerator_count(), 1);
1260 EXPECT_EQ(NULL, 1158 EXPECT_EQ(NULL,
1261 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); 1159 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
1262 1160
1263 // Hitting the return key again; nothing should happen. 1161 // Hitting the return key again; nothing should happen.
1264 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); 1162 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1265 EXPECT_EQ(target.accelerator_count(), 1); 1163 EXPECT_EQ(target.accelerator_count(), 1);
1266 } 1164 }
1267 1165
1268 } // namespace views 1166 } // 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