| 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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.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" |
| 11 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 11 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/test/base/browser_with_test_window_test.h" | 13 #include "chrome/test/base/browser_with_test_window_test.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "ui/views/controls/menu/menu_runner.h" | 15 #include "ui/views/controls/menu/menu_runner.h" |
| 16 #include "ui/views/controls/menu/submenu_view.h" |
| 16 | 17 |
| 17 class BookmarkMenuDelegateTest : public BrowserWithTestWindowTest { | 18 class BookmarkMenuDelegateTest : public BrowserWithTestWindowTest { |
| 18 public: | 19 public: |
| 19 BookmarkMenuDelegateTest() : model_(NULL) { | 20 BookmarkMenuDelegateTest() : model_(NULL) {} |
| 20 } | |
| 21 | 21 |
| 22 virtual void SetUp() OVERRIDE { | 22 virtual void SetUp() OVERRIDE { |
| 23 BrowserWithTestWindowTest::SetUp(); | 23 BrowserWithTestWindowTest::SetUp(); |
| 24 | 24 |
| 25 profile()->CreateBookmarkModel(true); | 25 profile()->CreateBookmarkModel(true); |
| 26 | 26 |
| 27 model_ = BookmarkModelFactory::GetForProfile(profile()); | 27 model_ = BookmarkModelFactory::GetForProfile(profile()); |
| 28 test::WaitForBookmarkModelToLoad(model_); | 28 test::WaitForBookmarkModelToLoad(model_); |
| 29 | 29 |
| 30 AddTestData(); | 30 AddTestData(); |
| 31 | |
| 32 bookmark_menu_delegate_.reset( | |
| 33 new BookmarkMenuDelegate(browser(), NULL, NULL, 0)); | |
| 34 } | 31 } |
| 35 | 32 |
| 36 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
| 37 // Since we never show the menu we need to pass the MenuItemView to | 34 // Since we never show the menu we need to pass the MenuItemView to |
| 38 // MenuRunner so that the MenuItemView is destroyed. | 35 // MenuRunner so that the MenuItemView is destroyed. |
| 39 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu()); | 36 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu()); |
| 40 bookmark_menu_delegate_.reset(); | 37 bookmark_menu_delegate_.reset(); |
| 41 BrowserWithTestWindowTest::TearDown(); | 38 BrowserWithTestWindowTest::TearDown(); |
| 42 } | 39 } |
| 43 | 40 |
| 44 protected: | 41 protected: |
| 42 void NewDelegate(int min_menu_id, int max_menu_id) { |
| 43 bookmark_menu_delegate_.reset( |
| 44 new BookmarkMenuDelegate(browser(), NULL, NULL, |
| 45 min_menu_id, max_menu_id)); |
| 46 } |
| 47 |
| 48 void NewAndInitDelegateForPermanent(int min_menu_id, |
| 49 int max_menu_id) { |
| 50 const BookmarkNode* node = model_->bookmark_bar_node(); |
| 51 NewDelegate(min_menu_id, max_menu_id); |
| 52 bookmark_menu_delegate_->Init(&test_delegate_, NULL, node, 0, |
| 53 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
| 54 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 55 } |
| 56 |
| 45 BookmarkModel* model_; | 57 BookmarkModel* model_; |
| 46 | 58 |
| 47 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; | 59 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; |
| 48 | 60 |
| 49 private: | 61 private: |
| 50 std::string base_path() const { return "file:///c:/tmp/"; } | 62 std::string base_path() const { return "file:///c:/tmp/"; } |
| 51 | 63 |
| 52 // Creates the following structure: | 64 // Creates the following structure: |
| 53 // bookmark bar node | 65 // bookmark bar node |
| 54 // a | 66 // a |
| (...skipping 17 matching lines...) Expand all Loading... |
| 72 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2")); | 84 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2")); |
| 73 | 85 |
| 74 // Children of the other node. | 86 // Children of the other node. |
| 75 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"), | 87 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"), |
| 76 GURL(test_base + "oa")); | 88 GURL(test_base + "oa")); |
| 77 const BookmarkNode* of1 = | 89 const BookmarkNode* of1 = |
| 78 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); | 90 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); |
| 79 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); | 91 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); |
| 80 } | 92 } |
| 81 | 93 |
| 94 views::MenuDelegate test_delegate_; |
| 95 |
| 82 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); | 96 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); |
| 83 }; | 97 }; |
| 84 | 98 |
| 85 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that | 99 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that |
| 86 // have since been deleted. | 100 // have since been deleted. |
| 87 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { | 101 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { |
| 88 views::MenuDelegate test_delegate; | 102 views::MenuDelegate test_delegate; |
| 89 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); | 103 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); |
| 104 NewDelegate(0, kint32max); |
| 90 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, | 105 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, |
| 91 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, | 106 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 92 BOOKMARK_LAUNCH_LOCATION_NONE); | 107 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 93 std::vector<const BookmarkNode*> nodes_to_remove; | 108 std::vector<const BookmarkNode*> nodes_to_remove; |
| 94 nodes_to_remove.push_back(node->GetChild(1)); | 109 nodes_to_remove.push_back(node->GetChild(1)); |
| 95 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); | 110 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); |
| 96 nodes_to_remove.clear(); | 111 nodes_to_remove.clear(); |
| 97 bookmark_menu_delegate_->DidRemoveBookmarks(); | 112 bookmark_menu_delegate_->DidRemoveBookmarks(); |
| 113 |
| 98 } | 114 } |
| 115 |
| 116 // Verifies menu ID's of items in menu fall within the specified range. |
| 117 TEST_F(BookmarkMenuDelegateTest, MenuIdRange) { |
| 118 // Start with maximum menu Id of 10 - the number of items that AddTestData() |
| 119 // populated. Everything should be created. |
| 120 NewAndInitDelegateForPermanent(0, 10); |
| 121 views::MenuItemView* root_item = bookmark_menu_delegate_->menu_; |
| 122 ASSERT_TRUE(root_item->HasSubmenu()); |
| 123 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); |
| 124 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. |
| 125 views::MenuItemView* F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); |
| 126 ASSERT_TRUE(F1_item->HasSubmenu()); |
| 127 EXPECT_EQ(2, F1_item->GetSubmenu()->GetMenuItemCount()); |
| 128 views::MenuItemView* F11_item = F1_item->GetSubmenu()->GetMenuItemAt(1); |
| 129 ASSERT_TRUE(F11_item->HasSubmenu()); |
| 130 EXPECT_EQ(1, F11_item->GetSubmenu()->GetMenuItemCount()); |
| 131 views::MenuItemView* other_item = root_item->GetSubmenu()->GetMenuItemAt(3); |
| 132 ASSERT_TRUE(other_item->HasSubmenu()); |
| 133 EXPECT_EQ(2, other_item->GetSubmenu()->GetMenuItemCount()); |
| 134 views::MenuItemView* OF1_item = other_item->GetSubmenu()->GetMenuItemAt(1); |
| 135 ASSERT_TRUE(OF1_item->HasSubmenu()); |
| 136 EXPECT_EQ(1, OF1_item->GetSubmenu()->GetMenuItemCount()); |
| 137 |
| 138 // Reduce maximum 9. "of1a" item should not be created. |
| 139 NewAndInitDelegateForPermanent(0, 9); |
| 140 root_item = bookmark_menu_delegate_->menu_; |
| 141 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); |
| 142 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. |
| 143 other_item = root_item->GetSubmenu()->GetMenuItemAt(3); |
| 144 OF1_item = other_item->GetSubmenu()->GetMenuItemAt(1); |
| 145 EXPECT_EQ(0, OF1_item->GetSubmenu()->GetMenuItemCount()); |
| 146 |
| 147 // Reduce maximum 8. "OF1" submenu should not be created. |
| 148 NewAndInitDelegateForPermanent(0, 8); |
| 149 root_item = bookmark_menu_delegate_->menu_; |
| 150 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); |
| 151 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. |
| 152 other_item = root_item->GetSubmenu()->GetMenuItemAt(3); |
| 153 EXPECT_EQ(1, other_item->GetSubmenu()->GetMenuItemCount()); |
| 154 |
| 155 // Reduce maximum 7. "Other" submenu should be empty. |
| 156 NewAndInitDelegateForPermanent(0, 7); |
| 157 root_item = bookmark_menu_delegate_->menu_; |
| 158 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); |
| 159 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. |
| 160 other_item = root_item->GetSubmenu()->GetMenuItemAt(3); |
| 161 EXPECT_EQ(0, other_item->GetSubmenu()->GetMenuItemCount()); |
| 162 |
| 163 // Reduce maximum to 6. "Other" submenu should not be created, and no |
| 164 // separator. |
| 165 NewAndInitDelegateForPermanent(0, 6); |
| 166 root_item = bookmark_menu_delegate_->menu_; |
| 167 EXPECT_EQ(3, root_item->GetSubmenu()->GetMenuItemCount()); |
| 168 EXPECT_EQ(3, root_item->GetSubmenu()->child_count()); // No separator. |
| 169 |
| 170 // Reduce maximum 5. "F2" and "Other" submenus shouldn't be created. |
| 171 NewAndInitDelegateForPermanent(0, 5); |
| 172 root_item = bookmark_menu_delegate_->menu_; |
| 173 EXPECT_EQ(2, root_item->GetSubmenu()->GetMenuItemCount()); |
| 174 EXPECT_EQ(2, root_item->GetSubmenu()->child_count()); // No separator. |
| 175 F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); |
| 176 F11_item = F1_item->GetSubmenu()->GetMenuItemAt(1); |
| 177 EXPECT_EQ(1, F11_item->GetSubmenu()->GetMenuItemCount()); |
| 178 |
| 179 // Reduce maximum to 4. "f11a" item and "F2" and "Other" submenus should |
| 180 // not be created. |
| 181 NewAndInitDelegateForPermanent(0, 4); |
| 182 root_item = bookmark_menu_delegate_->menu_; |
| 183 EXPECT_EQ(2, root_item->GetSubmenu()->GetMenuItemCount()); |
| 184 EXPECT_EQ(2, root_item->GetSubmenu()->child_count()); // No separator. |
| 185 F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); |
| 186 F11_item = F1_item->GetSubmenu()->GetMenuItemAt(1); |
| 187 EXPECT_EQ(0, F11_item->GetSubmenu()->GetMenuItemCount()); |
| 188 |
| 189 // Reduce maximum to 3. "F11", "F2" and "Other" submenus should not be |
| 190 // created. |
| 191 NewAndInitDelegateForPermanent(0, 3); |
| 192 root_item = bookmark_menu_delegate_->menu_; |
| 193 EXPECT_EQ(2, root_item->GetSubmenu()->GetMenuItemCount()); |
| 194 EXPECT_EQ(2, root_item->GetSubmenu()->child_count()); // No separator. |
| 195 F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); |
| 196 EXPECT_EQ(views::MenuItemView::SUBMENU, F1_item->GetType()); |
| 197 EXPECT_EQ(1, F1_item->GetSubmenu()->GetMenuItemCount()); |
| 198 |
| 199 // Reduce maximum 2. Only "a" item and empty "F1" submenu should be created. |
| 200 NewAndInitDelegateForPermanent(0, 2); |
| 201 root_item = bookmark_menu_delegate_->menu_; |
| 202 EXPECT_EQ(2, root_item->GetSubmenu()->GetMenuItemCount()); |
| 203 EXPECT_EQ(2, root_item->GetSubmenu()->child_count()); // No separator. |
| 204 F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); |
| 205 EXPECT_EQ(views::MenuItemView::SUBMENU, F1_item->GetType()); |
| 206 EXPECT_EQ(0, F1_item->GetSubmenu()->GetMenuItemCount()); |
| 207 |
| 208 // Reduce maximum to 1. Only "a" item should be created. |
| 209 NewAndInitDelegateForPermanent(0, 1); |
| 210 root_item = bookmark_menu_delegate_->menu_; |
| 211 EXPECT_EQ(1, root_item->GetSubmenu()->GetMenuItemCount()); |
| 212 EXPECT_EQ(1, root_item->GetSubmenu()->child_count()); // No separator. |
| 213 |
| 214 // Verify correct handling of integer overflow with range, set kint32max as |
| 215 // maximum and 1 less as minimum. Only "a" item should be created. |
| 216 NewAndInitDelegateForPermanent(kint32max - 1, kint32max); |
| 217 root_item = bookmark_menu_delegate_->menu_; |
| 218 EXPECT_EQ(1, root_item->GetSubmenu()->GetMenuItemCount()); |
| 219 EXPECT_EQ(1, root_item->GetSubmenu()->child_count()); // No separator. |
| 220 } |
| OLD | NEW |