| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" | 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_stats.h" | 10 #include "chrome/browser/bookmarks/bookmark_stats.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 profile()->CreateBookmarkModel(true); | 31 profile()->CreateBookmarkModel(true); |
| 32 | 32 |
| 33 model_ = BookmarkModelFactory::GetForProfile(profile()); | 33 model_ = BookmarkModelFactory::GetForProfile(profile()); |
| 34 bookmarks::test::WaitForBookmarkModelToLoad(model_); | 34 bookmarks::test::WaitForBookmarkModelToLoad(model_); |
| 35 | 35 |
| 36 AddTestData(); | 36 AddTestData(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void TearDown() override { | 39 void TearDown() override { |
| 40 if (bookmark_menu_delegate_.get()) { | 40 DestroyDelegate(); |
| 41 // Since we never show the menu we need to pass the MenuItemView to | 41 |
| 42 // MenuRunner so that the MenuItemView is destroyed. | |
| 43 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); | |
| 44 bookmark_menu_delegate_.reset(); | |
| 45 } | |
| 46 BrowserWithTestWindowTest::TearDown(); | 42 BrowserWithTestWindowTest::TearDown(); |
| 47 } | 43 } |
| 48 | 44 |
| 49 protected: | 45 protected: |
| 46 bool ShouldCloseOnRemove(const bookmarks::BookmarkNode* node) const { |
| 47 return bookmark_menu_delegate_->ShouldCloseOnRemove(node); |
| 48 } |
| 49 |
| 50 // Destroys the delegate. Do this rather than directly deleting |
| 51 // |bookmark_menu_delegate_| as otherwise the menu is leaked. |
| 52 void DestroyDelegate() { |
| 53 if (!bookmark_menu_delegate_.get()) |
| 54 return; |
| 55 |
| 56 // Since we never show the menu we need to pass the MenuItemView to |
| 57 // MenuRunner so that the MenuItemView is destroyed. |
| 58 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); |
| 59 bookmark_menu_delegate_.reset(); |
| 60 } |
| 61 |
| 50 void NewDelegate() { | 62 void NewDelegate() { |
| 51 // Destroy current menu if available, see comments in TearDown(). | 63 DestroyDelegate(); |
| 52 if (bookmark_menu_delegate_.get()) | |
| 53 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); | |
| 54 | 64 |
| 55 bookmark_menu_delegate_.reset( | 65 bookmark_menu_delegate_.reset( |
| 56 new BookmarkMenuDelegate(browser(), nullptr, nullptr)); | 66 new BookmarkMenuDelegate(browser(), nullptr, nullptr)); |
| 57 } | 67 } |
| 58 | 68 |
| 59 void NewAndInitDelegateForPermanent() { | 69 void NewAndInitDelegateForPermanent() { |
| 60 const BookmarkNode* node = model_->bookmark_bar_node(); | 70 const BookmarkNode* node = model_->bookmark_bar_node(); |
| 61 NewDelegate(); | 71 NewDelegate(); |
| 62 bookmark_menu_delegate_->Init(&test_delegate_, NULL, node, 0, | 72 bookmark_menu_delegate_->Init(&test_delegate_, NULL, node, 0, |
| 63 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 73 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, | 190 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, |
| 181 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, | 191 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 182 BOOKMARK_LAUNCH_LOCATION_NONE); | 192 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 183 LoadAllMenus(); | 193 LoadAllMenus(); |
| 184 std::vector<const BookmarkNode*> nodes_to_remove; | 194 std::vector<const BookmarkNode*> nodes_to_remove; |
| 185 nodes_to_remove.push_back(node->GetChild(1)); | 195 nodes_to_remove.push_back(node->GetChild(1)); |
| 186 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); | 196 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); |
| 187 nodes_to_remove.clear(); | 197 nodes_to_remove.clear(); |
| 188 bookmark_menu_delegate_->DidRemoveBookmarks(); | 198 bookmark_menu_delegate_->DidRemoveBookmarks(); |
| 189 } | 199 } |
| 200 |
| 201 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that |
| 202 // have since been deleted. |
| 203 TEST_F(BookmarkMenuDelegateTest, CloseOnRemove) { |
| 204 views::MenuDelegate test_delegate; |
| 205 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); |
| 206 NewDelegate(); |
| 207 bookmark_menu_delegate_->Init(&test_delegate, nullptr, node, 0, |
| 208 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 209 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 210 // Any nodes on the bookmark bar should close on remove. |
| 211 EXPECT_TRUE(ShouldCloseOnRemove(model_->bookmark_bar_node()->GetChild(2))); |
| 212 |
| 213 // Descendants of the bookmark should not close on remove. |
| 214 EXPECT_FALSE(ShouldCloseOnRemove( |
| 215 model_->bookmark_bar_node()->GetChild(1)->GetChild(0))); |
| 216 |
| 217 EXPECT_FALSE(ShouldCloseOnRemove(model_->other_node()->GetChild(0))); |
| 218 |
| 219 // Make it so the other node only has one child. |
| 220 // Destroy the current delegate so that it doesn't have any references to |
| 221 // deleted nodes. |
| 222 DestroyDelegate(); |
| 223 while (model_->other_node()->child_count() > 1) |
| 224 model_->Remove(model_->other_node()->GetChild(1)); |
| 225 |
| 226 NewDelegate(); |
| 227 bookmark_menu_delegate_->Init(&test_delegate, nullptr, node, 0, |
| 228 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 229 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 230 // Any nodes on the bookmark bar should close on remove. |
| 231 EXPECT_TRUE(ShouldCloseOnRemove(model_->other_node()->GetChild(0))); |
| 232 } |
| OLD | NEW |