| 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" | |
| 17 | 16 |
| 18 class BookmarkMenuDelegateTest : public BrowserWithTestWindowTest { | 17 class BookmarkMenuDelegateTest : public BrowserWithTestWindowTest { |
| 19 public: | 18 public: |
| 20 BookmarkMenuDelegateTest() : model_(NULL) {} | 19 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)); |
| 31 } | 34 } |
| 32 | 35 |
| 33 virtual void TearDown() OVERRIDE { | 36 virtual void TearDown() OVERRIDE { |
| 34 // Since we never show the menu we need to pass the MenuItemView to | 37 // Since we never show the menu we need to pass the MenuItemView to |
| 35 // MenuRunner so that the MenuItemView is destroyed. | 38 // MenuRunner so that the MenuItemView is destroyed. |
| 36 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu()); | 39 views::MenuRunner menu_runner(bookmark_menu_delegate_->menu()); |
| 37 bookmark_menu_delegate_.reset(); | 40 bookmark_menu_delegate_.reset(); |
| 38 BrowserWithTestWindowTest::TearDown(); | 41 BrowserWithTestWindowTest::TearDown(); |
| 39 } | 42 } |
| 40 | 43 |
| 41 protected: | 44 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 | |
| 57 BookmarkModel* model_; | 45 BookmarkModel* model_; |
| 58 | 46 |
| 59 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; | 47 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; |
| 60 | 48 |
| 61 private: | 49 private: |
| 62 std::string base_path() const { return "file:///c:/tmp/"; } | 50 std::string base_path() const { return "file:///c:/tmp/"; } |
| 63 | 51 |
| 64 // Creates the following structure: | 52 // Creates the following structure: |
| 65 // bookmark bar node | 53 // bookmark bar node |
| 66 // a | 54 // a |
| (...skipping 17 matching lines...) Expand all Loading... |
| 84 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2")); | 72 model_->AddFolder(bb_node, 2, ASCIIToUTF16("F2")); |
| 85 | 73 |
| 86 // Children of the other node. | 74 // Children of the other node. |
| 87 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"), | 75 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"), |
| 88 GURL(test_base + "oa")); | 76 GURL(test_base + "oa")); |
| 89 const BookmarkNode* of1 = | 77 const BookmarkNode* of1 = |
| 90 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); | 78 model_->AddFolder(model_->other_node(), 1, ASCIIToUTF16("OF1")); |
| 91 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); | 79 model_->AddURL(of1, 0, ASCIIToUTF16("of1a"), GURL(test_base + "of1a")); |
| 92 } | 80 } |
| 93 | 81 |
| 94 views::MenuDelegate test_delegate_; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); | 82 DISALLOW_COPY_AND_ASSIGN(BookmarkMenuDelegateTest); |
| 97 }; | 83 }; |
| 98 | 84 |
| 99 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that | 85 // Verifies WillRemoveBookmarks() doesn't attempt to access MenuItemViews that |
| 100 // have since been deleted. | 86 // have since been deleted. |
| 101 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { | 87 TEST_F(BookmarkMenuDelegateTest, RemoveBookmarks) { |
| 102 views::MenuDelegate test_delegate; | 88 views::MenuDelegate test_delegate; |
| 103 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); | 89 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(1); |
| 104 NewDelegate(0, kint32max); | |
| 105 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, | 90 bookmark_menu_delegate_->Init(&test_delegate, NULL, node, 0, |
| 106 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, | 91 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, |
| 107 BOOKMARK_LAUNCH_LOCATION_NONE); | 92 BOOKMARK_LAUNCH_LOCATION_NONE); |
| 108 std::vector<const BookmarkNode*> nodes_to_remove; | 93 std::vector<const BookmarkNode*> nodes_to_remove; |
| 109 nodes_to_remove.push_back(node->GetChild(1)); | 94 nodes_to_remove.push_back(node->GetChild(1)); |
| 110 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); | 95 bookmark_menu_delegate_->WillRemoveBookmarks(nodes_to_remove); |
| 111 nodes_to_remove.clear(); | 96 nodes_to_remove.clear(); |
| 112 bookmark_menu_delegate_->DidRemoveBookmarks(); | 97 bookmark_menu_delegate_->DidRemoveBookmarks(); |
| 113 | |
| 114 } | 98 } |
| 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 |