| 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 |
| 11 #include "app/resource_bundle.h" | 11 #include "app/resource_bundle.h" |
| 12 #include "base/gfx/rect.h" | 12 #include "base/gfx/rect.h" |
| 13 #include "base/keyboard_codes.h" |
| 13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 14 #include "third_party/skia/include/core/SkColor.h" | 15 #include "third_party/skia/include/core/SkColor.h" |
| 15 #include "views/background.h" | 16 #include "views/background.h" |
| 16 #include "views/border.h" | 17 #include "views/border.h" |
| 17 #include "views/controls/button/checkbox.h" | 18 #include "views/controls/button/checkbox.h" |
| 18 #include "views/controls/button/native_button.h" | 19 #include "views/controls/button/native_button.h" |
| 19 #include "views/controls/button/radio_button.h" | 20 #include "views/controls/button/radio_button.h" |
| 20 #include "views/controls/combobox/combobox.h" | 21 #include "views/controls/combobox/combobox.h" |
| 21 #include "views/controls/combobox/native_combobox_wrapper.h" | 22 #include "views/controls/combobox/native_combobox_wrapper.h" |
| 22 #include "views/controls/label.h" | 23 #include "views/controls/label.h" |
| 23 #include "views/controls/link.h" | 24 #include "views/controls/link.h" |
| 25 #if defined(OS_WIN) |
| 24 #include "views/controls/native_control.h" | 26 #include "views/controls/native_control.h" |
| 25 #include "views/controls/scroll_view.h" | 27 #include "views/controls/scroll_view.h" |
| 26 #include "views/controls/tabbed_pane/native_tabbed_pane_wrapper.h" | 28 #include "views/controls/tabbed_pane/native_tabbed_pane_wrapper.h" |
| 27 #include "views/controls/tabbed_pane/tabbed_pane.h" | 29 #include "views/controls/tabbed_pane/tabbed_pane.h" |
| 28 #include "views/controls/textfield/textfield.h" | 30 #include "views/controls/textfield/textfield.h" |
| 29 #include "views/widget/accelerator_handler.h" | 31 #endif |
| 32 #include "views/focus/accelerator_handler.h" |
| 30 #include "views/widget/root_view.h" | 33 #include "views/widget/root_view.h" |
| 34 #include "views/window/window.h" |
| 35 #include "views/window/window_delegate.h" |
| 36 |
| 37 #if defined(OS_WIN) |
| 31 #include "views/widget/widget_win.h" | 38 #include "views/widget/widget_win.h" |
| 32 #include "views/window/window_delegate.h" | |
| 33 #include "views/window/window_win.h" | 39 #include "views/window/window_win.h" |
| 40 #else |
| 41 #include "views/window/window_gtk.h" |
| 42 #endif |
| 34 | 43 |
| 35 namespace views { | 44 namespace views { |
| 36 | 45 |
| 46 #if defined(OS_WIN) |
| 37 static const int kWindowWidth = 600; | 47 static const int kWindowWidth = 600; |
| 38 static const int kWindowHeight = 500; | 48 static const int kWindowHeight = 500; |
| 39 | 49 |
| 40 static int count = 1; | 50 static int count = 1; |
| 41 | 51 |
| 42 static const int kTopCheckBoxID = count++; // 1 | 52 static const int kTopCheckBoxID = count++; // 1 |
| 43 static const int kLeftContainerID = count++; | 53 static const int kLeftContainerID = count++; |
| 44 static const int kAppleLabelID = count++; | 54 static const int kAppleLabelID = count++; |
| 45 static const int kAppleTextfieldID = count++; | 55 static const int kAppleTextfieldID = count++; |
| 46 static const int kOrangeLabelID = count++; // 5 | 56 static const int kOrangeLabelID = count++; // 5 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 static const int kStyleTextEditID = count++; | 96 static const int kStyleTextEditID = count++; |
| 87 | 97 |
| 88 static const int kSearchContainerID = count++; | 98 static const int kSearchContainerID = count++; |
| 89 static const int kSearchTextfieldID = count++; | 99 static const int kSearchTextfieldID = count++; |
| 90 static const int kSearchButtonID = count++; | 100 static const int kSearchButtonID = count++; |
| 91 static const int kHelpLinkID = count++; // 45 | 101 static const int kHelpLinkID = count++; // 45 |
| 92 | 102 |
| 93 static const int kThumbnailContainerID = count++; | 103 static const int kThumbnailContainerID = count++; |
| 94 static const int kThumbnailStarID = count++; | 104 static const int kThumbnailStarID = count++; |
| 95 static const int kThumbnailSuperStarID = count++; | 105 static const int kThumbnailSuperStarID = count++; |
| 106 #endif |
| 96 | 107 |
| 97 class FocusManagerTest; | 108 class FocusManagerTest : public testing::Test, public WindowDelegate { |
| 109 public: |
| 110 FocusManagerTest() |
| 111 : window_(NULL), |
| 112 content_view_(NULL), |
| 113 focus_change_listener_(NULL) { |
| 114 #if defined(OS_WIN) |
| 115 OleInitialize(NULL); |
| 116 #endif |
| 117 } |
| 98 | 118 |
| 119 ~FocusManagerTest() { |
| 120 #if defined(OS_WIN) |
| 121 OleUninitialize(); |
| 122 #endif |
| 123 } |
| 124 |
| 125 virtual void SetUp() { |
| 126 window_ = Window::CreateChromeWindow(NULL, bounds(), this); |
| 127 InitContentView(); |
| 128 window_->Show(); |
| 129 } |
| 130 |
| 131 virtual void TearDown() { |
| 132 if (focus_change_listener_) |
| 133 GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_); |
| 134 // window_->CloseNow(); |
| 135 window_->Close(); |
| 136 |
| 137 // Flush the message loop to make Purify happy. |
| 138 message_loop()->RunAllPending(); |
| 139 } |
| 140 |
| 141 FocusManager* GetFocusManager() { |
| 142 #if defined(OS_WIN) |
| 143 return static_cast<WindowWin*>(window_)->GetFocusManager(); |
| 144 #elif defined(OS_LINUX) |
| 145 return static_cast<WindowGtk*>(window_)->GetFocusManager(); |
| 146 #elif |
| 147 NOTIMPLEMENTED(); |
| 148 #endif |
| 149 } |
| 150 |
| 151 // WindowDelegate Implementation. |
| 152 virtual View* GetContentsView() { |
| 153 if (!content_view_) |
| 154 content_view_ = new View(); |
| 155 return content_view_; |
| 156 } |
| 157 |
| 158 virtual void InitContentView() { |
| 159 } |
| 160 |
| 161 protected: |
| 162 virtual gfx::Rect bounds() { |
| 163 return gfx::Rect(0, 0, 500, 500); |
| 164 } |
| 165 |
| 166 #if defined(OS_WIN) |
| 167 // Mocks activating/deactivating the window. |
| 168 void SimulateActivateWindow() { |
| 169 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); |
| 170 } |
| 171 void SimulateDeactivateWindow() { |
| 172 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_INACTIVE, NULL); |
| 173 } |
| 174 #endif |
| 175 |
| 176 MessageLoopForUI* message_loop() { return &message_loop_; } |
| 177 |
| 178 Window* window_; |
| 179 View* content_view_; |
| 180 |
| 181 void AddFocusChangeListener(FocusChangeListener* listener) { |
| 182 ASSERT_FALSE(focus_change_listener_); |
| 183 focus_change_listener_ = listener; |
| 184 GetFocusManager()->AddFocusChangeListener(listener); |
| 185 } |
| 186 |
| 187 private: |
| 188 FocusChangeListener* focus_change_listener_; |
| 189 MessageLoopForUI message_loop_; |
| 190 |
| 191 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); |
| 192 }; |
| 193 |
| 194 #if defined(OS_WIN) |
| 99 // BorderView is a NativeControl that creates a tab control as its child and | 195 // BorderView is a NativeControl that creates a tab control as its child and |
| 100 // takes a View to add as the child of the tab control. The tab control is used | 196 // takes a View to add as the child of the tab control. The tab control is used |
| 101 // to give a nice background for the view. At some point we'll have a real | 197 // to give a nice background for the view. At some point we'll have a real |
| 102 // wrapper for TabControl, and this can be nuked in favor of it. | 198 // wrapper for TabControl, and this can be nuked in favor of it. |
| 103 // Taken from keyword_editor_view.cc. | 199 // Taken from keyword_editor_view.cc. |
| 104 // It is interesting in our test as it is a native control containing another | 200 // It is interesting in our test as it is a native control containing another |
| 105 // RootView. | 201 // RootView. |
| 106 class BorderView : public NativeControl { | 202 class BorderView : public NativeControl { |
| 107 public: | 203 public: |
| 108 explicit BorderView(View* child) : child_(child) { | 204 explicit BorderView(View* child) : child_(child) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 272 |
| 177 class DummyComboboxModel : public Combobox::Model { | 273 class DummyComboboxModel : public Combobox::Model { |
| 178 public: | 274 public: |
| 179 virtual int GetItemCount(Combobox* source) { return 10; } | 275 virtual int GetItemCount(Combobox* source) { return 10; } |
| 180 | 276 |
| 181 virtual std::wstring GetItemAt(Combobox* source, int index) { | 277 virtual std::wstring GetItemAt(Combobox* source, int index) { |
| 182 return L"Item " + IntToWString(index); | 278 return L"Item " + IntToWString(index); |
| 183 } | 279 } |
| 184 }; | 280 }; |
| 185 | 281 |
| 186 class FocusManagerTest : public testing::Test, public WindowDelegate { | |
| 187 public: | |
| 188 FocusManagerTest() | |
| 189 : window_(NULL), | |
| 190 focus_change_listener_(NULL), | |
| 191 content_view_(NULL) { | |
| 192 OleInitialize(NULL); | |
| 193 } | |
| 194 | |
| 195 ~FocusManagerTest() { | |
| 196 OleUninitialize(); | |
| 197 } | |
| 198 | |
| 199 virtual void SetUp() { | |
| 200 window_ = static_cast<WindowWin*>( | |
| 201 Window::CreateChromeWindow(NULL, bounds(), this)); | |
| 202 InitContentView(); | |
| 203 window_->Show(); | |
| 204 } | |
| 205 | |
| 206 virtual void TearDown() { | |
| 207 if (focus_change_listener_) | |
| 208 GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_); | |
| 209 window_->CloseNow(); | |
| 210 | |
| 211 // Flush the message loop to make Purify happy. | |
| 212 message_loop()->RunAllPending(); | |
| 213 } | |
| 214 | |
| 215 FocusManager* GetFocusManager() { | |
| 216 return FocusManager::GetFocusManagerForNativeView( | |
| 217 window_->GetNativeWindow()); | |
| 218 } | |
| 219 | |
| 220 // WindowDelegate Implementation. | |
| 221 virtual View* GetContentsView() { | |
| 222 if (!content_view_) | |
| 223 content_view_ = new View(); | |
| 224 return content_view_; | |
| 225 } | |
| 226 | |
| 227 virtual void InitContentView() { | |
| 228 } | |
| 229 | |
| 230 protected: | |
| 231 virtual gfx::Rect bounds() { | |
| 232 return gfx::Rect(0, 0, 500, 500); | |
| 233 } | |
| 234 | |
| 235 // Mocks activating/deactivating the window. | |
| 236 void SimulateActivateWindow() { | |
| 237 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); | |
| 238 } | |
| 239 void SimulateDeactivateWindow() { | |
| 240 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_INACTIVE, NULL); | |
| 241 } | |
| 242 | |
| 243 MessageLoopForUI* message_loop() { return &message_loop_; } | |
| 244 | |
| 245 WindowWin* window_; | |
| 246 View* content_view_; | |
| 247 | |
| 248 void AddFocusChangeListener(FocusChangeListener* listener) { | |
| 249 ASSERT_FALSE(focus_change_listener_); | |
| 250 focus_change_listener_ = listener; | |
| 251 GetFocusManager()->AddFocusChangeListener(listener); | |
| 252 } | |
| 253 | |
| 254 private: | |
| 255 FocusChangeListener* focus_change_listener_; | |
| 256 MessageLoopForUI message_loop_; | |
| 257 | |
| 258 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); | |
| 259 }; | |
| 260 | |
| 261 class FocusTraversalTest : public FocusManagerTest { | 282 class FocusTraversalTest : public FocusManagerTest { |
| 262 public: | 283 public: |
| 263 ~FocusTraversalTest(); | 284 ~FocusTraversalTest(); |
| 264 | 285 |
| 265 virtual void InitContentView(); | 286 virtual void InitContentView(); |
| 266 | 287 |
| 267 protected: | 288 protected: |
| 268 FocusTraversalTest(); | 289 FocusTraversalTest(); |
| 269 | 290 |
| 270 virtual gfx::Rect bounds() { | 291 virtual gfx::Rect bounds() { |
| (...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) { | 989 for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) { |
| 969 GetFocusManager()->AdvanceFocus(true); | 990 GetFocusManager()->AdvanceFocus(true); |
| 970 focused_view = GetFocusManager()->GetFocusedView(); | 991 focused_view = GetFocusManager()->GetFocusedView(); |
| 971 EXPECT_TRUE(focused_view != NULL); | 992 EXPECT_TRUE(focused_view != NULL); |
| 972 if (focused_view) | 993 if (focused_view) |
| 973 EXPECT_EQ(kTraversalIDs[j], focused_view->GetID()); | 994 EXPECT_EQ(kTraversalIDs[j], focused_view->GetID()); |
| 974 } | 995 } |
| 975 } | 996 } |
| 976 } | 997 } |
| 977 | 998 |
| 999 #endif // WIN_OS |
| 1000 |
| 978 // Counts accelerator calls. | 1001 // Counts accelerator calls. |
| 979 class TestAcceleratorTarget : public AcceleratorTarget { | 1002 class TestAcceleratorTarget : public AcceleratorTarget { |
| 980 public: | 1003 public: |
| 981 explicit TestAcceleratorTarget(bool process_accelerator) | 1004 explicit TestAcceleratorTarget(bool process_accelerator) |
| 982 : accelerator_count_(0), process_accelerator_(process_accelerator) {} | 1005 : accelerator_count_(0), process_accelerator_(process_accelerator) {} |
| 983 | 1006 |
| 984 virtual bool AcceleratorPressed(const Accelerator& accelerator) { | 1007 virtual bool AcceleratorPressed(const Accelerator& accelerator) { |
| 985 ++accelerator_count_; | 1008 ++accelerator_count_; |
| 986 return process_accelerator_; | 1009 return process_accelerator_; |
| 987 } | 1010 } |
| 988 | 1011 |
| 989 int accelerator_count() const { return accelerator_count_; } | 1012 int accelerator_count() const { return accelerator_count_; } |
| 990 | 1013 |
| 991 private: | 1014 private: |
| 992 int accelerator_count_; // number of times that the accelerator is activated | 1015 int accelerator_count_; // number of times that the accelerator is activated |
| 993 bool process_accelerator_; // return value of AcceleratorPressed | 1016 bool process_accelerator_; // return value of AcceleratorPressed |
| 994 | 1017 |
| 995 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorTarget); | 1018 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorTarget); |
| 996 }; | 1019 }; |
| 997 | 1020 |
| 998 TEST_F(FocusManagerTest, CallsNormalAcceleratorTarget) { | 1021 TEST_F(FocusManagerTest, CallsNormalAcceleratorTarget) { |
| 999 FocusManager* focus_manager = GetFocusManager(); | 1022 FocusManager* focus_manager = GetFocusManager(); |
| 1000 Accelerator return_accelerator(VK_RETURN, false, false, false); | 1023 Accelerator return_accelerator(base::VKEY_RETURN, false, false, false); |
| 1001 Accelerator escape_accelerator(VK_ESCAPE, false, false, false); | 1024 Accelerator escape_accelerator(base::VKEY_ESCAPE, false, false, false); |
| 1002 | 1025 |
| 1003 TestAcceleratorTarget return_target(true); | 1026 TestAcceleratorTarget return_target(true); |
| 1004 TestAcceleratorTarget escape_target(true); | 1027 TestAcceleratorTarget escape_target(true); |
| 1005 EXPECT_EQ(return_target.accelerator_count(), 0); | 1028 EXPECT_EQ(return_target.accelerator_count(), 0); |
| 1006 EXPECT_EQ(escape_target.accelerator_count(), 0); | 1029 EXPECT_EQ(escape_target.accelerator_count(), 0); |
| 1007 EXPECT_EQ(NULL, | 1030 EXPECT_EQ(NULL, |
| 1008 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | 1031 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); |
| 1009 EXPECT_EQ(NULL, | 1032 EXPECT_EQ(NULL, |
| 1010 focus_manager->GetCurrentTargetForAccelerator(escape_accelerator)); | 1033 focus_manager->GetCurrentTargetForAccelerator(escape_accelerator)); |
| 1011 | 1034 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 private: | 1131 private: |
| 1109 Accelerator accelerator_; | 1132 Accelerator accelerator_; |
| 1110 FocusManager* focus_manager_; | 1133 FocusManager* focus_manager_; |
| 1111 int accelerator_count_; | 1134 int accelerator_count_; |
| 1112 | 1135 |
| 1113 DISALLOW_COPY_AND_ASSIGN(SelfUnregisteringAcceleratorTarget); | 1136 DISALLOW_COPY_AND_ASSIGN(SelfUnregisteringAcceleratorTarget); |
| 1114 }; | 1137 }; |
| 1115 | 1138 |
| 1116 TEST_F(FocusManagerTest, CallsSelfDeletingAcceleratorTarget) { | 1139 TEST_F(FocusManagerTest, CallsSelfDeletingAcceleratorTarget) { |
| 1117 FocusManager* focus_manager = GetFocusManager(); | 1140 FocusManager* focus_manager = GetFocusManager(); |
| 1118 Accelerator return_accelerator(VK_RETURN, false, false, false); | 1141 Accelerator return_accelerator(base::VKEY_RETURN, false, false, false); |
| 1119 SelfUnregisteringAcceleratorTarget target(return_accelerator, focus_manager); | 1142 SelfUnregisteringAcceleratorTarget target(return_accelerator, focus_manager); |
| 1120 EXPECT_EQ(target.accelerator_count(), 0); | 1143 EXPECT_EQ(target.accelerator_count(), 0); |
| 1121 EXPECT_EQ(NULL, | 1144 EXPECT_EQ(NULL, |
| 1122 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | 1145 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); |
| 1123 | 1146 |
| 1124 // Register the target. | 1147 // Register the target. |
| 1125 focus_manager->RegisterAccelerator(return_accelerator, &target); | 1148 focus_manager->RegisterAccelerator(return_accelerator, &target); |
| 1126 EXPECT_EQ(&target, | 1149 EXPECT_EQ(&target, |
| 1127 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | 1150 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); |
| 1128 | 1151 |
| 1129 // Hitting the return key. The target will be unregistered. | 1152 // Hitting the return key. The target will be unregistered. |
| 1130 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); | 1153 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 1131 EXPECT_EQ(target.accelerator_count(), 1); | 1154 EXPECT_EQ(target.accelerator_count(), 1); |
| 1132 EXPECT_EQ(NULL, | 1155 EXPECT_EQ(NULL, |
| 1133 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | 1156 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); |
| 1134 | 1157 |
| 1135 // Hitting the return key again; nothing should happen. | 1158 // Hitting the return key again; nothing should happen. |
| 1136 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | 1159 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); |
| 1137 EXPECT_EQ(target.accelerator_count(), 1); | 1160 EXPECT_EQ(target.accelerator_count(), 1); |
| 1138 } | 1161 } |
| 1139 | 1162 |
| 1140 } // namespace views | 1163 } // namespace views |
| OLD | NEW |