Chromium Code Reviews| 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 int id = menu->GetCommand(); | |
| 68 return bookmark_menu_delegate_->menu_id_to_node_map_.count(id) > 0 | |
| 69 ? bookmark_menu_delegate_->menu_id_to_node_map_[id] | |
| 70 : nullptr; | |
|
Peter Kasting
2015/03/30 21:35:24
Nit: Shorter and more efficient (not that it matte
sky
2015/03/31 15:17:39
Done.
| |
| 71 } | |
| 72 | |
| 73 int next_menu_id() { return bookmark_menu_delegate_->next_menu_id_; } | |
| 74 | |
| 75 // Forces all the menus to load by way of invoking WillShowMenu() on all menu | |
| 76 // items of tyep SUBMENU. | |
| 77 void LoadAllMenus() { LoadAllMenus(bookmark_menu_delegate_->menu()); } | |
| 78 | |
| 68 BookmarkModel* model_; | 79 BookmarkModel* model_; |
| 69 | 80 |
| 70 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; | 81 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; |
| 71 | 82 |
| 72 private: | 83 private: |
| 84 void LoadAllMenus(views::MenuItemView* menu) { | |
| 85 EXPECT_EQ(views::MenuItemView::SUBMENU, menu->GetType()); | |
| 86 | |
| 87 for (int i = 0; i < menu->GetSubmenu()->GetMenuItemCount(); ++i) { | |
| 88 views::MenuItemView* child = menu->GetSubmenu()->GetMenuItemAt(i); | |
| 89 if (child->GetType() == views::MenuItemView::SUBMENU) { | |
| 90 bookmark_menu_delegate_->WillShowMenu(child); | |
| 91 LoadAllMenus(child); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 73 std::string base_path() const { return "file:///c:/tmp/"; } | 96 std::string base_path() const { return "file:///c:/tmp/"; } |
| 74 | 97 |
| 75 // Creates the following structure: | 98 // Creates the following structure: |
| 76 // bookmark bar node | 99 // bookmark bar node |
| 77 // a | 100 // a |
| 78 // F1 | 101 // F1 |
| 79 // f1a | 102 // f1a |
| 80 // F11 | 103 // F11 |
| 81 // f11a | 104 // f11a |
| 82 // F2 | 105 // F2 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 100 const BookmarkNode* of1 = | 123 const BookmarkNode* of1 = |
| 101 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); | 124 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); |
| 102 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); | 125 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); |
| 103 } | 126 } |
| 104 | 127 |
| 105 views::MenuDelegate test_delegate_; | 128 views::MenuDelegate test_delegate_; |
| 106 | 129 |
| 107 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); | 130 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); |
| 108 }; | 131 }; |
| 109 | 132 |
| 133 TEST_F(BookmarkMenuDelegateTest, VerifyLazyLoad) { | |
| 134 NewAndInitDelegateForPermanent(); | |
| 135 views::MenuItemView* root_item = bookmark_menu_delegate_->menu(); | |
| 136 ASSERT_TRUE(root_item->HasSubmenu()); | |
| 137 EXPECT_EQ(4, root_item->GetSubmenu()->GetMenuItemCount()); | |
| 138 EXPECT_EQ(5, root_item->GetSubmenu()->child_count()); // Includes separator. | |
| 139 views::MenuItemView* f1_item = root_item->GetSubmenu()->GetMenuItemAt(1); | |
| 140 ASSERT_TRUE(f1_item->HasSubmenu()); | |
| 141 // f1 hasn't been loaded yet. | |
| 142 EXPECT_EQ(0, f1_item->GetSubmenu()->GetMenuItemCount()); | |
| 143 // Will show triggers a load. | |
| 144 int next_id_before_load = next_menu_id(); | |
| 145 bookmark_menu_delegate_->WillShowMenu(f1_item); | |
| 146 // f1 should have loaded it's children. | |
|
Peter Kasting
2015/03/30 21:35:24
Nit: its
sky
2015/03/31 15:17:39
Done.
| |
| 147 EXPECT_EQ(next_id_before_load + 2, next_menu_id()); | |
| 148 ASSERT_EQ(2, f1_item->GetSubmenu()->GetMenuItemCount()); | |
| 149 const BookmarkNode* f1_node = model_->bookmark_bar_node()->GetChild(1); | |
| 150 EXPECT_EQ(f1_node->GetChild(0), | |
| 151 GetNodeForMenuItem(f1_item->GetSubmenu()->GetMenuItemAt(0))); | |
| 152 EXPECT_EQ(f1_node->GetChild(1), | |
| 153 GetNodeForMenuItem(f1_item->GetSubmenu()->GetMenuItemAt(1))); | |
| 154 | |
| 155 // F11 shouldn't have loaded yet. | |
| 156 views::MenuItemView* f11_item = f1_item->GetSubmenu()->GetMenuItemAt(1); | |
| 157 ASSERT_TRUE(f11_item->HasSubmenu()); | |
| 158 EXPECT_EQ(0, f11_item->GetSubmenu()->GetMenuItemCount()); | |
| 159 | |
| 160 next_id_before_load = next_menu_id(); | |
| 161 bookmark_menu_delegate_->WillShowMenu(f11_item); | |
| 162 // Invoke WillShowMenu() twice to make sure the second call doesn't cause | |
| 163 // problems. | |
| 164 bookmark_menu_delegate_->WillShowMenu(f11_item); | |
| 165 // F11 should have loaded it's single child (f11a). | |
|
Peter Kasting
2015/03/30 21:35:25
Nit: its
sky
2015/03/31 15:17:39
Done.
| |
| 166 EXPECT_EQ(next_id_before_load + 1, next_menu_id()); | |
| 167 | |
| 168 ASSERT_EQ(1, f11_item->GetSubmenu()->GetMenuItemCount()); | |
| 169 const BookmarkNode* f11_node = f1_node->GetChild(1); | |
| 170 EXPECT_EQ(f11_node->GetChild(0), | |
| 171 GetNodeForMenuItem(f11_item->GetSubmenu()->GetMenuItemAt(0))); | |
| 172 } | |
| 173 | |
| 110 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that | 174 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that |
| 111 // have since been deleted. | 175 // have since been deleted. |
| 112 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { | 176 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { |
| 113 views::MenuDelegate test_delegate; | 177 views::MenuDelegate test_delegate; |
| 114 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); | 178 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); |
| 115 NewDelegate(0, kint32max); | 179 NewDelegate(); |
| 116 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, | 180 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, |
| 117 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, | 181 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 118 BOOKMARK_LAUNCH_LOCATION_NONE); | 182 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 183 LoadAllMenus(); | |
| 119 std::vector<const BookmarkNode*> nodes_to_remove; | 184 std::vector<const BookmarkNode*> nodes_to_remove; |
| 120 nodes_to_remove.push_back(node->GetChild(1)); | 185 nodes_to_remove.push_back(node->GetChild(1)); |
| 121 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); | 186 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); |
| 122 nodes_to_remove.clear(); | 187 nodes_to_remove.clear(); |
| 123 bookmark_menu_delegate_->DidRemoveBookmarks(); | 188 bookmark_menu_delegate_->DidRemoveBookmarks(); |
| 124 } | 189 } |
| 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 |