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

Side by Side Diff: chrome/browser/views/bookmark_bar_view_test.cc

Issue 1664001: Fixes possible crash if the window hosting a menu was closed while the (Closed)
Patch Set: Incorporated review feedback Created 10 years, 8 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
« no previous file with comments | « chrome/browser/views/bookmark_bar_view.cc ('k') | chrome/browser/views/chrome_views_delegate.h » ('j') | 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) 2010 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 "base/keyboard_codes.h" 5 #include "base/keyboard_codes.h"
6 #include "base/string_util.h" 6 #include "base/string_util.h"
7 #include "chrome/browser/automation/ui_controls.h" 7 #include "chrome/browser/automation/ui_controls.h"
8 #include "chrome/browser/bookmarks/bookmark_model.h" 8 #include "chrome/browser/bookmarks/bookmark_model.h"
9 #include "chrome/browser/bookmarks/bookmark_utils.h" 9 #include "chrome/browser/bookmarks/bookmark_utils.h"
10 #include "chrome/browser/pref_service.h" 10 #include "chrome/browser/pref_service.h"
11 #include "chrome/browser/profile.h" 11 #include "chrome/browser/profile.h"
12 #include "chrome/browser/tab_contents/page_navigator.h" 12 #include "chrome/browser/tab_contents/page_navigator.h"
13 #include "chrome/browser/views/bookmark_bar_view.h" 13 #include "chrome/browser/views/bookmark_bar_view.h"
14 #include "chrome/common/notification_service.h" 14 #include "chrome/common/notification_service.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/testing_profile.h" 16 #include "chrome/test/testing_profile.h"
17 #include "chrome/test/interactive_ui/view_event_test_base.h" 17 #include "chrome/test/interactive_ui/view_event_test_base.h"
18 #include "chrome/test/ui_test_utils.h" 18 #include "chrome/test/ui_test_utils.h"
19 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
20 #include "views/controls/button/menu_button.h" 20 #include "views/controls/button/menu_button.h"
21 #include "views/controls/button/text_button.h" 21 #include "views/controls/button/text_button.h"
22 #include "views/controls/menu/menu_controller.h" 22 #include "views/controls/menu/menu_controller.h"
23 #include "views/controls/menu/menu_item_view.h" 23 #include "views/controls/menu/menu_item_view.h"
24 #include "views/controls/menu/submenu_view.h" 24 #include "views/controls/menu/submenu_view.h"
25 #include "views/views_delegate.h"
25 #include "views/window/window.h" 26 #include "views/window/window.h"
26 27
27 #if defined(OS_LINUX) 28 #if defined(OS_LINUX)
28 // See http://crbug.com/40040 for details. 29 // See http://crbug.com/40040 for details.
29 #define MAYBE_DND DISABLED_DND 30 #define MAYBE_DND DISABLED_DND
30 #define MAYBE_DNDToDifferentMenu DISABLED_DNDToDifferentMenu 31 #define MAYBE_DNDToDifferentMenu DISABLED_DNDToDifferentMenu
31 #define MAYBE_DNDBackToOriginatingMenu DISABLED_DNDBackToOriginatingMenu 32 #define MAYBE_DNDBackToOriginatingMenu DISABLED_DNDBackToOriginatingMenu
32 33
33 // See http://crbug.com/40039 for details. 34 // See http://crbug.com/40039 for details.
34 #define MAYBE_KeyEvents DISABLED_KeyEvents 35 #define MAYBE_KeyEvents DISABLED_KeyEvents
35 36
36 #else 37 #else
37 38
38 #define MAYBE_DND DND 39 #define MAYBE_DND DND
39 #define MAYBE_DNDToDifferentMenu DNDToDifferentMenu 40 #define MAYBE_DNDToDifferentMenu DNDToDifferentMenu
40 #define MAYBE_DNDBackToOriginatingMenu DNDBackToOriginatingMenu 41 #define MAYBE_DNDBackToOriginatingMenu DNDBackToOriginatingMenu
41 #define MAYBE_DNDBackToOriginatingMenu DNDBackToOriginatingMenu 42 #define MAYBE_DNDBackToOriginatingMenu DNDBackToOriginatingMenu
42 #define MAYBE_KeyEvents KeyEvents 43 #define MAYBE_KeyEvents KeyEvents
43 44
44 #endif 45 #endif
45 46
46 namespace { 47 namespace {
47 48
49 class ViewsDelegateImpl : public views::ViewsDelegate {
50 public:
51 ViewsDelegateImpl() {}
52 virtual Clipboard* GetClipboard() const { return NULL; }
53 virtual void SaveWindowPlacement(const std::wstring& window_name,
54 const gfx::Rect& bounds,
55 bool maximized) {}
56 virtual bool GetSavedWindowBounds(const std::wstring& window_name,
57 gfx::Rect* bounds) const {
58 return false;
59 }
60 virtual bool GetSavedMaximizedState(const std::wstring& window_name,
61 bool* maximized) const {
62 return false;
63 }
64
65 #if defined(OS_WIN)
66 virtual HICON GetDefaultWindowIcon() const { return 0; }
67 #endif
68
69 virtual void AddRef() {
70 }
71
72 virtual void ReleaseRef() {
73 MessageLoopForUI::current()->Quit();
74 }
75
76 private:
77 DISALLOW_COPY_AND_ASSIGN(ViewsDelegateImpl);
78 };
79
48 // PageNavigator implementation that records the URL. 80 // PageNavigator implementation that records the URL.
49 class TestingPageNavigator : public PageNavigator { 81 class TestingPageNavigator : public PageNavigator {
50 public: 82 public:
51 virtual void OpenURL(const GURL& url, const GURL& referrer, 83 virtual void OpenURL(const GURL& url, const GURL& referrer,
52 WindowOpenDisposition disposition, 84 WindowOpenDisposition disposition,
53 PageTransition::Type transition) { 85 PageTransition::Type transition) {
54 url_ = url; 86 url_ = url;
55 } 87 }
56 88
57 GURL url_; 89 GURL url_;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 173
142 tmp_parent.RemoveChildView(bb_view_); 174 tmp_parent.RemoveChildView(bb_view_);
143 175
144 ViewEventTestBase::SetUp(); 176 ViewEventTestBase::SetUp();
145 } 177 }
146 178
147 virtual void TearDown() { 179 virtual void TearDown() {
148 views::MenuItemView::allow_task_nesting_during_run_ = false; 180 views::MenuItemView::allow_task_nesting_during_run_ = false;
149 ViewEventTestBase::TearDown(); 181 ViewEventTestBase::TearDown();
150 BookmarkBarView::testing_ = false; 182 BookmarkBarView::testing_ = false;
183 views::ViewsDelegate::views_delegate = NULL;
151 } 184 }
152 185
153 protected: 186 protected:
187 void InstallViewsDelegate() {
188 views::ViewsDelegate::views_delegate = &views_delegate_;
189 }
190
154 virtual views::View* CreateContentsView() { 191 virtual views::View* CreateContentsView() {
155 return bb_view_; 192 return bb_view_;
156 } 193 }
157 194
158 virtual gfx::Size GetPreferredSize() { return bb_view_pref_; } 195 virtual gfx::Size GetPreferredSize() { return bb_view_pref_; }
159 196
160 // See comment above class description for what this does. 197 // See comment above class description for what this does.
161 virtual bool CreateBigMenu() { return false; } 198 virtual bool CreateBigMenu() { return false; }
162 199
163 BookmarkModel* model_; 200 BookmarkModel* model_;
(...skipping 29 matching lines...) Expand all
193 model_->AddURL(of, 1, L"ofb", GURL(test_base + "ofb")); 230 model_->AddURL(of, 1, L"ofb", GURL(test_base + "ofb"));
194 const BookmarkNode* of2 = model_->AddGroup(model_->other_node(), 2, L"OF2"); 231 const BookmarkNode* of2 = model_->AddGroup(model_->other_node(), 2, L"OF2");
195 model_->AddURL(of2, 0, L"of2a", GURL(test_base + "of2a")); 232 model_->AddURL(of2, 0, L"of2a", GURL(test_base + "of2a"));
196 model_->AddURL(of2, 1, L"of2b", GURL(test_base + "of2b")); 233 model_->AddURL(of2, 1, L"of2b", GURL(test_base + "of2b"));
197 } 234 }
198 235
199 gfx::Size bb_view_pref_; 236 gfx::Size bb_view_pref_;
200 scoped_ptr<TestingProfile> profile_; 237 scoped_ptr<TestingProfile> profile_;
201 ChromeThread ui_thread_; 238 ChromeThread ui_thread_;
202 ChromeThread file_thread_; 239 ChromeThread file_thread_;
240 ViewsDelegateImpl views_delegate_;
203 }; 241 };
204 242
205 // Clicks on first menu, makes sure button is depressed. Moves mouse to first 243 // Clicks on first menu, makes sure button is depressed. Moves mouse to first
206 // child, clicks it and makes sure a navigation occurs. 244 // child, clicks it and makes sure a navigation occurs.
207 class BookmarkBarViewTest1 : public BookmarkBarViewEventTestBase { 245 class BookmarkBarViewTest1 : public BookmarkBarViewEventTestBase {
208 protected: 246 protected:
209 virtual void DoTestOnMessageLoop() { 247 virtual void DoTestOnMessageLoop() {
210 // Move the mouse to the first folder on the bookmark bar and press the 248 // Move the mouse to the first folder on the bookmark bar and press the
211 // mouse. 249 // mouse.
212 views::TextButton* button = bb_view_->GetBookmarkButton(0); 250 views::TextButton* button = bb_view_->GetBookmarkButton(0);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 400
363 // And submenu we last clicked is showing. 401 // And submenu we last clicked is showing.
364 child_menu = menu->GetSubmenu()->GetMenuItemAt(2); 402 child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
365 ASSERT_TRUE(child_menu != NULL); 403 ASSERT_TRUE(child_menu != NULL);
366 ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing()); 404 ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
367 405
368 // Nothing should have been selected. 406 // Nothing should have been selected.
369 EXPECT_EQ(GURL(), navigator_.url_); 407 EXPECT_EQ(GURL(), navigator_.url_);
370 408
371 // Hide menu. 409 // Hide menu.
372 menu->GetMenuController()->Cancel(true); 410 menu->GetMenuController()->CancelAll();
373 411
374 Done(); 412 Done();
375 } 413 }
376 }; 414 };
377 415
378 VIEW_TEST(BookmarkBarViewTest3, Submenus) 416 VIEW_TEST(BookmarkBarViewTest3, Submenus)
379 417
380 // Observer that posts task upon the context menu creation. 418 // Observer that posts task upon the context menu creation.
381 // This is necessary for Linux as the context menu has to check 419 // This is necessary for Linux as the context menu has to check
382 // the clipboard, which invokes the event loop. 420 // the clipboard, which invokes the event loop.
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 MessageLoop::current()->PostDelayedTask(FROM_HERE, 815 MessageLoop::current()->PostDelayedTask(FROM_HERE,
778 NewRunnableMethod(this, &BookmarkBarViewTest9::Step4), 200); 816 NewRunnableMethod(this, &BookmarkBarViewTest9::Step4), 200);
779 } 817 }
780 818
781 void Step4() { 819 void Step4() {
782 gfx::Point menu_loc; 820 gfx::Point menu_loc;
783 views::View::ConvertPointToScreen(first_menu_, &menu_loc); 821 views::View::ConvertPointToScreen(first_menu_, &menu_loc);
784 ASSERT_NE(start_y_, menu_loc.y()); 822 ASSERT_NE(start_y_, menu_loc.y());
785 823
786 // Hide menu. 824 // Hide menu.
787 bb_view_->GetMenu()->GetMenuController()->Cancel(true); 825 bb_view_->GetMenu()->GetMenuController()->CancelAll();
788 826
789 // On linux, Cancelling menu will call Quit on the message loop, 827 // On linux, Cancelling menu will call Quit on the message loop,
790 // which can interfere with Done. We need to run Done in the 828 // which can interfere with Done. We need to run Done in the
791 // next execution loop. 829 // next execution loop.
792 MessageLoop::current()->PostTask( 830 MessageLoop::current()->PostTask(
793 FROM_HERE, 831 FROM_HERE,
794 NewRunnableMethod(this, &ViewEventTestBase::Done)); 832 NewRunnableMethod(this, &ViewEventTestBase::Done));
795 } 833 }
796 834
797 int start_y_; 835 int start_y_;
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 ASSERT_TRUE(context_menu == NULL); 1286 ASSERT_TRUE(context_menu == NULL);
1249 1287
1250 // But the menu should be showing. 1288 // But the menu should be showing.
1251 views::MenuItemView* menu = bb_view_->GetMenu(); 1289 views::MenuItemView* menu = bb_view_->GetMenu();
1252 ASSERT_TRUE(menu != NULL); 1290 ASSERT_TRUE(menu != NULL);
1253 ASSERT_TRUE(menu->GetSubmenu()->IsShowing()); 1291 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1254 1292
1255 // And the deleted_menu_id_ should have been removed. 1293 // And the deleted_menu_id_ should have been removed.
1256 ASSERT_TRUE(menu->GetMenuItemByID(deleted_menu_id_) == NULL); 1294 ASSERT_TRUE(menu->GetMenuItemByID(deleted_menu_id_) == NULL);
1257 1295
1258 bb_view_->GetMenu()->GetMenuController()->Cancel(true); 1296 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1259 1297
1260 Done(); 1298 Done();
1261 } 1299 }
1262 1300
1263 int deleted_menu_id_; 1301 int deleted_menu_id_;
1264 ContextMenuNotificationObserver observer_; 1302 ContextMenuNotificationObserver observer_;
1265 }; 1303 };
1266 1304
1267 VIEW_TEST(BookmarkBarViewTest15, MenuStaysVisibleAfterDelete) 1305 VIEW_TEST(BookmarkBarViewTest15, MenuStaysVisibleAfterDelete)
1306
1307 // Tests that we don't crash or get stuck if the parent of a menu is closed.
1308 class BookmarkBarViewTest16 : public BookmarkBarViewEventTestBase {
1309 protected:
1310 virtual void DoTestOnMessageLoop() {
1311 InstallViewsDelegate();
1312
1313 // Move the mouse to the first folder on the bookmark bar and press the
1314 // mouse.
1315 views::TextButton* button = bb_view_->GetBookmarkButton(0);
1316 ui_controls::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1317 ui_controls::DOWN | ui_controls::UP,
1318 CreateEventTask(this, &BookmarkBarViewTest16::Step2));
1319 }
1320
1321 private:
1322 void Step2() {
1323 // Menu should be showing.
1324 views::MenuItemView* menu = bb_view_->GetMenu();
1325 ASSERT_TRUE(menu != NULL);
1326 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1327
1328 // Button should be depressed.
1329 views::TextButton* button = bb_view_->GetBookmarkButton(0);
1330 ASSERT_TRUE(button->state() == views::CustomButton::BS_PUSHED);
1331
1332 // Close the window.
1333 window_->Close();
1334 window_ = NULL;
1335 }
1336 };
1337
1338 VIEW_TEST(BookmarkBarViewTest16, DeleteMenu)
OLDNEW
« no previous file with comments | « chrome/browser/views/bookmark_bar_view.cc ('k') | chrome/browser/views/chrome_views_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698