| 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_factory.h" | 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_stats.h" | 9 #include "chrome/browser/bookmarks/bookmark_stats.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 if (bookmark_menu_delegate_.get()) { | 39 if (bookmark_menu_delegate_.get()) { |
| 40 // Since we never show the menu we need to pass the MenuItemView to | 40 // Since we never show the menu we need to pass the MenuItemView to |
| 41 // MenuRunner so that the MenuItemView is destroyed. | 41 // MenuRunner so that the MenuItemView is destroyed. |
| 42 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); | 42 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); |
| 43 bookmark_menu_delegate_.reset(); | 43 bookmark_menu_delegate_.reset(); |
| 44 } | 44 } |
| 45 BrowserWithTestWindowTest::TearDown(); | 45 BrowserWithTestWindowTest::TearDown(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 protected: | 48 protected: |
| 49 void NewDelegate(int min_menu_id, int max_menu_id) { | 49 void NewDelegate() { |
| 50 // Destroy current menu if available, see comments in TearDown(). | 50 // Destroy current menu if available, see comments in TearDown(). |
| 51 if (bookmark_menu_delegate_.get()) | 51 if (bookmark_menu_delegate_.get()) |
| 52 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); | 52 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu(), 0); |
| 53 | 53 |
| 54 bookmark_menu_delegate_.reset( | 54 bookmark_menu_delegate_.reset( |
| 55 new BookmarkMenuDelegate(browser(), NULL, NULL, | 55 new BookmarkMenuDelegate(browser(), nullptr, nullptr)); |
| 56 min_menu_id, max_menu_id)); | |
| 57 } | 56 } |
| 58 | 57 |
| 59 void NewAndInitDelegateForPermanent(int min_menu_id, | 58 void NewAndInitDelegateForPermanent() { |
| 60 int max_menu_id) { | |
| 61 const BookmarkNode* node = model_->bookmark_bar_node(); | 59 const BookmarkNode* node = model_->bookmark_bar_node(); |
| 62 NewDelegate(min_menu_id, max_menu_id); | 60 NewDelegate(); |
| 63 bookmark_menu_delegate_->Init(&test_delegate_, NULL, node, 0, | 61 bookmark_menu_delegate_->Init(&test_delegate_, NULL, node, 0, |
| 64 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 62 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
| 65 BOOKMARK_LAUNCH_LOCATION_NONE); | 63 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 66 } | 64 } |
| 67 | 65 |
| 66 const BookmarkNode* GetNodeForMenuItem(views::MenuItemView* menu) { |
| 67 const auto& node_map = bookmark_menu_delegate_->menu_id_to_node_map_; |
| 68 auto iter = node_map.find(menu->GetCommand()); |
| 69 return (iter == node_map.end()) ? nullptr : iter->second; |
| 70 } |
| 71 |
| 72 int next_menu_id() { return bookmark_menu_delegate_->next_menu_id_; } |
| 73 |
| 74 // Forces all the menus to load by way of invoking WillShowMenu() on all menu |
| 75 // items of tyep SUBMENU. |
| 76 void LoadAllMenus() { LoadAllMenus(bookmark_menu_delegate_->menu()); } |
| 77 |
| 68 BookmarkModel* model_; | 78 BookmarkModel* model_; |
| 69 | 79 |
| 70 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; | 80 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; |
| 71 | 81 |
| 72 private: | 82 private: |
| 83 void LoadAllMenus(views::MenuItemView* menu) { |
| 84 EXPECT_EQ(views::MenuItemView::SUBMENU, menu->GetType()); |
| 85 |
| 86 for (int i = 0; i < menu->GetSubmenu()->GetMenuItemCount(); ++i) { |
| 87 views::MenuItemView* child = menu->GetSubmenu()->GetMenuItemAt(i); |
| 88 if (child->GetType() == views::MenuItemView::SUBMENU) { |
| 89 bookmark_menu_delegate_->WillShowMenu(child); |
| 90 LoadAllMenus(child); |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 73 std::string base_path() const { return "file:///c:/tmp/"; } | 95 std::string base_path() const { return "file:///c:/tmp/"; } |
| 74 | 96 |
| 75 // Creates the following structure: | 97 // Creates the following structure: |
| 76 // bookmark bar node | 98 // bookmark bar node |
| 77 // a | 99 // a |
| 78 // F1 | 100 // F1 |
| 79 // f1a | 101 // f1a |
| 80 // F11 | 102 // F11 |
| 81 // f11a | 103 // f11a |
| 82 // F2 | 104 // F2 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 100 const BookmarkNode* of1 = | 122 const BookmarkNode* of1 = |
| 101 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); | 123 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); |
| 102 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); | 124 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); |
| 103 } | 125 } |
| 104 | 126 |
| 105 views::MenuDelegate test_delegate_; | 127 views::MenuDelegate test_delegate_; |
| 106 | 128 |
| 107 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); | 129 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); |
| 108 }; | 130 }; |
| 109 | 131 |
| 132 TEST_F(BookmarkMenuDelegateTest, VerifyLazyLoad) { |
| 133 NewAndInitDelegateForPermanent(); |
| 134 views::MenuItemView* root_item = bookmark_menu_delegate_->menu(); |
| 135 ASSERT_TRUE(root_item->HasSubmenu()); |
| 136 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); |
| 137 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. |
| 138 views::MenuItemView* f1_item = root_item->GetSubmenu()->GetMenuItemAt(1); |
| 139 ASSERT_TRUE(f1_item->HasSubmenu()); |
| 140 // f1 hasn't been loaded yet. |
| 141 EXPECT_EQ(0, f1_item->GetSubmenu()->GetMenuItemCount()); |
| 142 // Will show triggers a load. |
| 143 int next_id_before_load = next_menu_id(); |
| 144 bookmark_menu_delegate_->WillShowMenu(f1_item); |
| 145 // f1 should have loaded its children. |
| 146 EXPECT_EQ(next_id_before_load + 2, next_menu_id()); |
| 147 ASSERT_EQ(2, f1_item->GetSubmenu()->GetMenuItemCount()); |
| 148 const BookmarkNode* f1_node = model_->bookmark_bar_node()->GetChild(1); |
| 149 EXPECT_EQ(f1_node->GetChild(0), |
| 150 GetNodeForMenuItem(f1_item->GetSubmenu()->GetMenuItemAt(0))); |
| 151 EXPECT_EQ(f1_node->GetChild(1), |
| 152 GetNodeForMenuItem(f1_item->GetSubmenu()->GetMenuItemAt(1))); |
| 153 |
| 154 // F11 shouldn't have loaded yet. |
| 155 views::MenuItemView* f11_item = f1_item->GetSubmenu()->GetMenuItemAt(1); |
| 156 ASSERT_TRUE(f11_item->HasSubmenu()); |
| 157 EXPECT_EQ(0, f11_item->GetSubmenu()->GetMenuItemCount()); |
| 158 |
| 159 next_id_before_load = next_menu_id(); |
| 160 bookmark_menu_delegate_->WillShowMenu(f11_item); |
| 161 // Invoke WillShowMenu() twice to make sure the second call doesn't cause |
| 162 // problems. |
| 163 bookmark_menu_delegate_->WillShowMenu(f11_item); |
| 164 // F11 should have loaded its single child (f11a). |
| 165 EXPECT_EQ(next_id_before_load + 1, next_menu_id()); |
| 166 |
| 167 ASSERT_EQ(1, f11_item->GetSubmenu()->GetMenuItemCount()); |
| 168 const BookmarkNode* f11_node = f1_node->GetChild(1); |
| 169 EXPECT_EQ(f11_node->GetChild(0), |
| 170 GetNodeForMenuItem(f11_item->GetSubmenu()->GetMenuItemAt(0))); |
| 171 } |
| 172 |
| 110 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that | 173 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that |
| 111 // have since been deleted. | 174 // have since been deleted. |
| 112 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { | 175 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { |
| 113 views::MenuDelegate test_delegate; | 176 views::MenuDelegate test_delegate; |
| 114 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); | 177 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); |
| 115 NewDelegate(0, kint32max); | 178 NewDelegate(); |
| 116 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, | 179 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, |
| 117 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, | 180 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 118 BOOKMARK_LAUNCH_LOCATION_NONE); | 181 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 182 LoadAllMenus(); |
| 119 std::vector<const BookmarkNode*> nodes_to_remove; | 183 std::vector<const BookmarkNode*> nodes_to_remove; |
| 120 nodes_to_remove.push_back(node->GetChild(1)); | 184 nodes_to_remove.push_back(node->GetChild(1)); |
| 121 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); | 185 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); |
| 122 nodes_to_remove.clear(); | 186 nodes_to_remove.clear(); |
| 123 bookmark_menu_delegate_->DidRemoveBookmarks(); | 187 bookmark_menu_delegate_->DidRemoveBookmarks(); |
| 124 } | 188 } |
| 125 | |
| 126 // Verifies menu ID's of items in menu fall within the specified range. | |
| 127 TEST_F(BookmarkMenuDelegateTest, MenuIdRange) { | |
| 128 // Start with maximum menu Id of 10 - the number of items that AddTestData() | |
| 129 // populated. Everything should be created. | |
| 130 NewAndInitDelegateForPermanent(0, 10); | |
| 131 views::MenuItemView* root_item = bookmark_menu_delegate_->menu(); | |
| 132 ASSERT_TRUE(root_item->HasSubmenu()); | |
| 133 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 134 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. | |
| 135 views::MenuItemView* F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); | |
| 136 ASSERT_TRUE(F1_item->HasSubmenu()); | |
| 137 EXPECT_EQ(2, F1_item->GetSubmenu()->GetMenuItemCount()); | |
| 138 views::MenuItemView* F11_item = F1_item->GetSubmenu()->GetMenuItemAt(1); | |
| 139 ASSERT_TRUE(F11_item->HasSubmenu()); | |
| 140 EXPECT_EQ(1, F11_item->GetSubmenu()->GetMenuItemCount()); | |
| 141 views::MenuItemView* other_item = root_item->GetSubmenu()->GetMenuItemAt(3); | |
| 142 ASSERT_TRUE(other_item->HasSubmenu()); | |
| 143 EXPECT_EQ(2, other_item->GetSubmenu()->GetMenuItemCount()); | |
| 144 views::MenuItemView* OF1_item = other_item->GetSubmenu()->GetMenuItemAt(1); | |
| 145 ASSERT_TRUE(OF1_item->HasSubmenu()); | |
| 146 EXPECT_EQ(1, OF1_item->GetSubmenu()->GetMenuItemCount()); | |
| 147 | |
| 148 // Reduce maximum 9. "of1a" item should not be created. | |
| 149 NewAndInitDelegateForPermanent(0, 9); | |
| 150 root_item = bookmark_menu_delegate_->menu(); | |
| 151 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 152 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. | |
| 153 other_item = root_item->GetSubmenu()->GetMenuItemAt(3); | |
| 154 OF1_item = other_item->GetSubmenu()->GetMenuItemAt(1); | |
| 155 EXPECT_EQ(0, OF1_item->GetSubmenu()->GetMenuItemCount()); | |
| 156 | |
| 157 // Reduce maximum 8. "OF1" submenu should not be created. | |
| 158 NewAndInitDelegateForPermanent(0, 8); | |
| 159 root_item = bookmark_menu_delegate_->menu(); | |
| 160 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 161 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. | |
| 162 other_item = root_item->GetSubmenu()->GetMenuItemAt(3); | |
| 163 EXPECT_EQ(1, other_item->GetSubmenu()->GetMenuItemCount()); | |
| 164 | |
| 165 // Reduce maximum 7. "Other" submenu should be empty. | |
| 166 NewAndInitDelegateForPermanent(0, 7); | |
| 167 root_item = bookmark_menu_delegate_->menu(); | |
| 168 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 169 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. | |
| 170 other_item = root_item->GetSubmenu()->GetMenuItemAt(3); | |
| 171 EXPECT_EQ(0, other_item->GetSubmenu()->GetMenuItemCount()); | |
| 172 | |
| 173 // Reduce maximum to 6. "Other" submenu should not be created, and no | |
| 174 // separator. | |
| 175 NewAndInitDelegateForPermanent(0, 6); | |
| 176 root_item = bookmark_menu_delegate_->menu(); | |
| 177 EXPECT_EQ(3, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 178 EXPECT_EQ(3, root_item->GetSubmenu()->child_count()); // No separator. | |
| 179 | |
| 180 // Reduce maximum 5. "F2" and "Other" submenus shouldn't be created. | |
| 181 NewAndInitDelegateForPermanent(0, 5); | |
| 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(1, F11_item->GetSubmenu()->GetMenuItemCount()); | |
| 188 | |
| 189 // Reduce maximum to 4. "f11a" item and "F2" and "Other" submenus should | |
| 190 // not be created. | |
| 191 NewAndInitDelegateForPermanent(0, 4); | |
| 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 F11_item = F1_item->GetSubmenu()->GetMenuItemAt(1); | |
| 197 EXPECT_EQ(0, F11_item->GetSubmenu()->GetMenuItemCount()); | |
| 198 | |
| 199 // Reduce maximum to 3. "F11", "F2" and "Other" submenus should not be | |
| 200 // created. | |
| 201 NewAndInitDelegateForPermanent(0, 3); | |
| 202 root_item = bookmark_menu_delegate_->menu(); | |
| 203 EXPECT_EQ(2, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 204 EXPECT_EQ(2, root_item->GetSubmenu()->child_count()); // No separator. | |
| 205 F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); | |
| 206 EXPECT_EQ(views::MenuItemView::SUBMENU, F1_item->GetType()); | |
| 207 EXPECT_EQ(1, F1_item->GetSubmenu()->GetMenuItemCount()); | |
| 208 | |
| 209 // Reduce maximum 2. Only "a" item and empty "F1" submenu should be created. | |
| 210 NewAndInitDelegateForPermanent(0, 2); | |
| 211 root_item = bookmark_menu_delegate_->menu(); | |
| 212 EXPECT_EQ(2, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 213 EXPECT_EQ(2, root_item->GetSubmenu()->child_count()); // No separator. | |
| 214 F1_item = root_item->GetSubmenu()->GetMenuItemAt(1); | |
| 215 EXPECT_EQ(views::MenuItemView::SUBMENU, F1_item->GetType()); | |
| 216 EXPECT_EQ(0, F1_item->GetSubmenu()->GetMenuItemCount()); | |
| 217 | |
| 218 // Reduce maximum to 1. Only "a" item should be created. | |
| 219 NewAndInitDelegateForPermanent(0, 1); | |
| 220 root_item = bookmark_menu_delegate_->menu(); | |
| 221 EXPECT_EQ(1, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 222 EXPECT_EQ(1, root_item->GetSubmenu()->child_count()); // No separator. | |
| 223 | |
| 224 // Verify correct handling of integer overflow with range, set kint32max as | |
| 225 // maximum and 1 less as minimum. Only "a" item should be created. | |
| 226 NewAndInitDelegateForPermanent(kint32max - 1, kint32max); | |
| 227 root_item = bookmark_menu_delegate_->menu(); | |
| 228 EXPECT_EQ(1, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 229 EXPECT_EQ(1, root_item->GetSubmenu()->child_count()); // No separator. | |
| 230 } | |
| OLD | NEW |