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

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

Issue 7720020: Get views_unittests pass on touch build (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: enable TouchSelectionControllerImplTest as they're fixed Created 9 years, 3 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "testing/gtest/include/gtest/gtest.h"
6
7 #include "base/logging.h" 5 #include "base/logging.h"
8 #include "base/string16.h" 6 #include "base/string16.h"
9 #include "base/string_number_conversions.h" 7 #include "base/string_number_conversions.h"
10 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
11 #include "third_party/skia/include/core/SkColor.h" 9 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/base/keycodes/keyboard_codes.h" 10 #include "ui/base/keycodes/keyboard_codes.h"
13 #include "ui/base/models/combobox_model.h" 11 #include "ui/base/models/combobox_model.h"
14 #include "ui/gfx/rect.h" 12 #include "ui/gfx/rect.h"
15 #include "views/background.h" 13 #include "views/background.h"
16 #include "views/border.h" 14 #include "views/border.h"
17 #include "views/controls/button/checkbox.h" 15 #include "views/controls/button/checkbox.h"
18 #include "views/controls/button/radio_button.h" 16 #include "views/controls/button/radio_button.h"
19 #include "views/controls/combobox/combobox.h" 17 #include "views/controls/combobox/combobox.h"
20 #include "views/controls/combobox/native_combobox_wrapper.h" 18 #include "views/controls/combobox/native_combobox_wrapper.h"
21 #include "views/controls/label.h" 19 #include "views/controls/label.h"
22 #include "views/controls/link.h" 20 #include "views/controls/link.h"
23 #include "views/controls/native/native_view_host.h" 21 #include "views/controls/native/native_view_host.h"
24 #include "views/controls/scroll_view.h" 22 #include "views/controls/scroll_view.h"
25 #include "views/controls/tabbed_pane/native_tabbed_pane_wrapper.h" 23 #include "views/controls/tabbed_pane/native_tabbed_pane_wrapper.h"
26 #include "views/controls/tabbed_pane/tabbed_pane.h" 24 #include "views/controls/tabbed_pane/tabbed_pane.h"
27 #include "views/controls/textfield/textfield.h" 25 #include "views/controls/textfield/textfield.h"
28 #include "views/focus/accelerator_handler.h" 26 #include "views/focus/accelerator_handler.h"
29 #include "views/focus/focus_manager_factory.h" 27 #include "views/focus/focus_manager_factory.h"
28 #include "views/test/views_test_base.h"
30 #include "views/widget/root_view.h" 29 #include "views/widget/root_view.h"
31 #include "views/widget/widget.h" 30 #include "views/widget/widget.h"
32 #include "views/widget/widget_delegate.h" 31 #include "views/widget/widget_delegate.h"
33 #include "views/window/non_client_view.h" 32 #include "views/window/non_client_view.h"
34 33
35 #if defined(OS_LINUX) 34 #if defined(OS_LINUX)
36 #include "ui/base/keycodes/keyboard_code_conversion_gtk.h" 35 #include "ui/base/keycodes/keyboard_code_conversion_gtk.h"
37 #endif 36 #endif
38 37
39 namespace { 38 namespace {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 const int kHelpLinkID = count++; 92 const int kHelpLinkID = count++;
94 93
95 const int kThumbnailContainerID = count++; // 45 94 const int kThumbnailContainerID = count++; // 45
96 const int kThumbnailStarID = count++; 95 const int kThumbnailStarID = count++;
97 const int kThumbnailSuperStarID = count++; 96 const int kThumbnailSuperStarID = count++;
98 97
99 } // namespace 98 } // namespace
100 99
101 namespace views { 100 namespace views {
102 101
103 class FocusManagerTest : public testing::Test, public WidgetDelegate { 102 class FocusManagerTest : public ViewsTestBase, public WidgetDelegate {
104 public: 103 public:
105 FocusManagerTest() 104 FocusManagerTest()
106 : window_(NULL), 105 : window_(NULL),
107 content_view_(NULL), 106 content_view_(NULL),
108 focus_change_listener_(NULL) { 107 focus_change_listener_(NULL) {
109 #if defined(OS_WIN)
110 OleInitialize(NULL);
111 #endif
112 } 108 }
113 109
114 ~FocusManagerTest() { 110 ~FocusManagerTest() {
115 #if defined(OS_WIN)
116 OleUninitialize();
117 #endif
118 } 111 }
119 112
120 virtual void SetUp() OVERRIDE { 113 virtual void SetUp() OVERRIDE {
114 ViewsTestBase::SetUp();
121 window_ = Widget::CreateWindowWithBounds(this, bounds()); 115 window_ = Widget::CreateWindowWithBounds(this, bounds());
122 InitContentView(); 116 InitContentView();
123 window_->Show(); 117 window_->Show();
124 } 118 }
125 119
126 virtual void TearDown() OVERRIDE { 120 virtual void TearDown() OVERRIDE {
127 if (focus_change_listener_) 121 if (focus_change_listener_)
128 GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_); 122 GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_);
129 window_->Close(); 123 window_->Close();
130 124
131 // Flush the message loop to make application verifiers happy. 125 // Flush the message loop to make application verifiers happy.
132 message_loop()->RunAllPending(); 126 RunPendingMessages();
127 ViewsTestBase::TearDown();
133 } 128 }
134 129
135 FocusManager* GetFocusManager() { 130 FocusManager* GetFocusManager() {
136 return window_->GetFocusManager(); 131 return window_->GetFocusManager();
137 } 132 }
138 133
139 void FocusNativeView(gfx::NativeView native_view) { 134 void FocusNativeView(gfx::NativeView native_view) {
140 #if defined(OS_WIN) 135 #if defined(OS_WIN)
141 ::SendMessage(native_view, WM_SETFOCUS, NULL, NULL); 136 ::SendMessage(native_view, WM_SETFOCUS, NULL, NULL);
142 #else 137 #else
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 void SimulateDeactivateWindow() { 181 void SimulateDeactivateWindow() {
187 #if defined(OS_WIN) 182 #if defined(OS_WIN)
188 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_INACTIVE, NULL); 183 ::SendMessage(window_->GetNativeWindow(), WM_ACTIVATE, WA_INACTIVE, NULL);
189 #else 184 #else
190 gboolean result; 185 gboolean result;
191 g_signal_emit_by_name(G_OBJECT(window_->GetNativeWindow()), 186 g_signal_emit_by_name(G_OBJECT(window_->GetNativeWindow()),
192 "focus_out_event", 0, & result); 187 "focus_out_event", 0, & result);
193 #endif 188 #endif
194 } 189 }
195 190
196 MessageLoopForUI* message_loop() { return &message_loop_; }
197
198 Widget* window_; 191 Widget* window_;
199 View* content_view_; 192 View* content_view_;
200 193
201 void AddFocusChangeListener(FocusChangeListener* listener) { 194 void AddFocusChangeListener(FocusChangeListener* listener) {
202 ASSERT_FALSE(focus_change_listener_); 195 ASSERT_FALSE(focus_change_listener_);
203 focus_change_listener_ = listener; 196 focus_change_listener_ = listener;
204 GetFocusManager()->AddFocusChangeListener(listener); 197 GetFocusManager()->AddFocusChangeListener(listener);
205 } 198 }
206 199
207 #if defined(OS_WIN) 200 #if defined(OS_WIN)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 242
250 g_free(keys); 243 g_free(keys);
251 244
252 gdk_event_put(event); 245 gdk_event_put(event);
253 gdk_event_free(event); 246 gdk_event_free(event);
254 } 247 }
255 #endif 248 #endif
256 249
257 private: 250 private:
258 FocusChangeListener* focus_change_listener_; 251 FocusChangeListener* focus_change_listener_;
259 MessageLoopForUI message_loop_;
260 252
261 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); 253 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest);
262 }; 254 };
263 255
264 // BorderView is a view containing a native window with its own view hierarchy. 256 // BorderView is a view containing a native window with its own view hierarchy.
265 // It is interesting to test focus traversal from a view hierarchy to an inner 257 // It is interesting to test focus traversal from a view hierarchy to an inner
266 // view hierarchy. 258 // view hierarchy.
267 class BorderView : public NativeViewHost { 259 class BorderView : public NativeViewHost {
268 public: 260 public:
269 explicit BorderView(View* child) : child_(child), widget_(NULL) { 261 explicit BorderView(View* child) : child_(child), widget_(NULL) {
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 // Simulate an activate, otherwise the deactivate isn't going to do anything. 948 // Simulate an activate, otherwise the deactivate isn't going to do anything.
957 SimulateActivateWindow(); 949 SimulateActivateWindow();
958 950
959 NativeTextButton* button = new NativeTextButton(NULL, L"Press me"); 951 NativeTextButton* button = new NativeTextButton(NULL, L"Press me");
960 View* view = new View(); 952 View* view = new View();
961 view->set_focusable(true); 953 view->set_focusable(true);
962 954
963 content_view_->AddChildView(button); 955 content_view_->AddChildView(button);
964 button->SetBounds(10, 10, 200, 30); 956 button->SetBounds(10, 10, 200, 30);
965 content_view_->AddChildView(view); 957 content_view_->AddChildView(view);
966 message_loop()->RunAllPending(); 958 RunPendingMessages();
967 959
968 TestFocusChangeListener listener; 960 TestFocusChangeListener listener;
969 AddFocusChangeListener(&listener); 961 AddFocusChangeListener(&listener);
970 962
971 view->RequestFocus(); 963 view->RequestFocus();
972 message_loop()->RunAllPending(); 964 RunPendingMessages();
973 // MessageLoopForUI::current()->Run(new AcceleratorHandler()); 965 // MessageLoopForUI::current()->Run(new AcceleratorHandler());
974 966
975 // Visual Studio 2010 has problems converting NULL to the null pointer for 967 // Visual Studio 2010 has problems converting NULL to the null pointer for
976 // std::pair. See http://connect.microsoft.com/VisualStudio/feedback/details/ 520043/error-converting-from-null-to-a-pointer-type-in-std-pair 968 // std::pair. See http://connect.microsoft.com/VisualStudio/feedback/details/ 520043/error-converting-from-null-to-a-pointer-type-in-std-pair
977 // It will work if we pass nullptr. 969 // It will work if we pass nullptr.
978 #if defined(_MSC_VER) && _MSC_VER >= 1600 970 #if defined(_MSC_VER) && _MSC_VER >= 1600
979 views::View* null_view = nullptr; 971 views::View* null_view = nullptr;
980 #else 972 #else
981 views::View* null_view = NULL; 973 views::View* null_view = NULL;
982 #endif 974 #endif
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after
1692 } 1684 }
1693 1685
1694 virtual ~WindowDtorTracked() { 1686 virtual ~WindowDtorTracked() {
1695 dtor_tracker_->push_back("WindowDtorTracked"); 1687 dtor_tracker_->push_back("WindowDtorTracked");
1696 } 1688 }
1697 1689
1698 DtorTrackVector* dtor_tracker_; 1690 DtorTrackVector* dtor_tracker_;
1699 }; 1691 };
1700 1692
1701 virtual void SetUp() { 1693 virtual void SetUp() {
1694 ViewsTestBase::SetUp();
1702 FocusManagerFactory::Install(new TestFocusManagerFactory(&dtor_tracker_)); 1695 FocusManagerFactory::Install(new TestFocusManagerFactory(&dtor_tracker_));
1703 // Create WindowDtorTracked that uses FocusManagerDtorTracked. 1696 // Create WindowDtorTracked that uses FocusManagerDtorTracked.
1704 window_ = new WindowDtorTracked(&dtor_tracker_); 1697 window_ = new WindowDtorTracked(&dtor_tracker_);
1705 Widget::InitParams params; 1698 Widget::InitParams params;
1706 params.delegate = this; 1699 params.delegate = this;
1707 params.bounds = gfx::Rect(0, 0, 100, 100); 1700 params.bounds = gfx::Rect(0, 0, 100, 100);
1708 window_->Init(params); 1701 window_->Init(params);
1709 1702
1710 tracked_focus_manager_ = 1703 tracked_focus_manager_ =
1711 static_cast<FocusManagerDtorTracked*>(GetFocusManager()); 1704 static_cast<FocusManagerDtorTracked*>(GetFocusManager());
1712 window_->Show(); 1705 window_->Show();
1713 } 1706 }
1714 1707
1715 virtual void TearDown() { 1708 virtual void TearDown() {
1716 if (window_) { 1709 if (window_) {
1717 window_->Close(); 1710 window_->Close();
1718 message_loop()->RunAllPending(); 1711 RunPendingMessages();
1719 } 1712 }
1720 FocusManagerFactory::Install(NULL); 1713 FocusManagerFactory::Install(NULL);
1714 ViewsTestBase::TearDown();
1721 } 1715 }
1722 1716
1723 FocusManager* tracked_focus_manager_; 1717 FocusManager* tracked_focus_manager_;
1724 DtorTrackVector dtor_tracker_; 1718 DtorTrackVector dtor_tracker_;
1725 }; 1719 };
1726 1720
1727 TEST_F(FocusManagerDtorTest, FocusManagerDestructedLast) { 1721 TEST_F(FocusManagerDtorTest, FocusManagerDestructedLast) {
1728 // Setup views hierarchy. 1722 // Setup views hierarchy.
1729 TabbedPane* tabbed_pane = new TabbedPane(); 1723 TabbedPane* tabbed_pane = new TabbedPane();
1730 content_view_->AddChildView(tabbed_pane); 1724 content_view_->AddChildView(tabbed_pane);
1731 1725
1732 NativeButtonDtorTracked* button = new NativeButtonDtorTracked(L"button", 1726 NativeButtonDtorTracked* button = new NativeButtonDtorTracked(L"button",
1733 &dtor_tracker_); 1727 &dtor_tracker_);
1734 tabbed_pane->AddTab(L"Awesome tab", button); 1728 tabbed_pane->AddTab(L"Awesome tab", button);
1735 1729
1736 // Close the window. 1730 // Close the window.
1737 window_->Close(); 1731 window_->Close();
1738 message_loop()->RunAllPending(); 1732 RunPendingMessages();
1739 1733
1740 // Test window, button and focus manager should all be destructed. 1734 // Test window, button and focus manager should all be destructed.
1741 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size())); 1735 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size()));
1742 1736
1743 // Focus manager should be the last one to destruct. 1737 // Focus manager should be the last one to destruct.
1744 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str()); 1738 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str());
1745 1739
1746 // Clear window_ so that we don't try to close it again. 1740 // Clear window_ so that we don't try to close it again.
1747 window_ = NULL; 1741 window_ = NULL;
1748 } 1742 }
1749 1743
1750 } // namespace views 1744 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/textfield/native_textfield_views_unittest.cc ('k') | views/test/views_test_base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698