1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/bookmarks/bookmark_context_menu_controller.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/bookmarks/bookmark_model.h"
13 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/page_navigator.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "grit/generated_resources.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/clipboard/clipboard.h"
24 using content::BrowserThread
;
25 using content::OpenURLParams
;
26 using content::PageNavigator
;
27 using content::WebContents
;
29 // PageNavigator implementation that records the URL.
30 class TestingPageNavigator
: public PageNavigator
{
32 virtual WebContents
* OpenURL(const OpenURLParams
& params
) OVERRIDE
{
33 urls_
.push_back(params
.url
);
37 std::vector
<GURL
> urls_
;
40 class BookmarkContextMenuControllerTest
: public testing::Test
{
42 BookmarkContextMenuControllerTest()
43 : ui_thread_(BrowserThread::UI
, &message_loop_
),
44 file_thread_(BrowserThread::FILE, &message_loop_
),
48 virtual void SetUp() OVERRIDE
{
51 void Reset(bool incognito
) {
52 TestingProfile::Builder builder
;
54 builder
.SetIncognito();
55 profile_
= builder
.Build();
56 profile_
->CreateBookmarkModel(true);
58 model_
= BookmarkModelFactory::GetForProfile(profile_
.get());
59 ui_test_utils::WaitForBookmarkModelToLoad(model_
);
64 virtual void TearDown() OVERRIDE
{
65 ui::Clipboard::DestroyClipboardForCurrentThread();
67 // Flush the message loop to make application verifiers happy.
68 message_loop_
.RunUntilIdle();
72 base::MessageLoopForUI message_loop_
;
73 content::TestBrowserThread ui_thread_
;
74 content::TestBrowserThread file_thread_
;
75 scoped_ptr
<TestingProfile
> profile_
;
76 BookmarkModel
* model_
;
77 TestingPageNavigator navigator_
;
80 // Creates the following structure:
91 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
92 std::string test_base
= "file:///c:/tmp/";
93 model_
->AddURL(bb_node
, 0, ASCIIToUTF16("a"), GURL(test_base
+ "a"));
94 const BookmarkNode
* f1
= model_
->AddFolder(bb_node
, 1, ASCIIToUTF16("F1"));
95 model_
->AddURL(f1
, 0, ASCIIToUTF16("f1a"), GURL(test_base
+ "f1a"));
96 const BookmarkNode
* f11
= model_
->AddFolder(f1
, 1, ASCIIToUTF16("F11"));
97 model_
->AddURL(f11
, 0, ASCIIToUTF16("f11a"), GURL(test_base
+ "f11a"));
98 model_
->AddFolder(bb_node
, 2, ASCIIToUTF16("F2"));
99 model_
->AddFolder(bb_node
, 3, ASCIIToUTF16("F3"));
100 const BookmarkNode
* f4
= model_
->AddFolder(bb_node
, 4, ASCIIToUTF16("F4"));
101 model_
->AddURL(f4
, 0, ASCIIToUTF16("f4a"), GURL(test_base
+ "f4a"));
105 // Tests Deleting from the menu.
106 TEST_F(BookmarkContextMenuControllerTest
, DeleteURL
) {
107 std::vector
<const BookmarkNode
*> nodes
;
108 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
109 BookmarkContextMenuController
controller(
110 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
111 GURL url
= model_
->bookmark_bar_node()->GetChild(0)->url();
112 ASSERT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
114 controller
.ExecuteCommand(IDC_BOOKMARK_BAR_REMOVE
, 0);
115 // Model shouldn't have URL anymore.
116 ASSERT_FALSE(model_
->IsBookmarked(url
));
119 // Tests open all on a folder with a couple of bookmarks.
120 TEST_F(BookmarkContextMenuControllerTest
, OpenAll
) {
121 const BookmarkNode
* folder
= model_
->bookmark_bar_node()->GetChild(1);
122 chrome::OpenAll(NULL
, &navigator_
, folder
, NEW_FOREGROUND_TAB
, NULL
);
124 // Should have navigated to F1's child, but not F11's child.
125 ASSERT_EQ(static_cast<size_t>(1), navigator_
.urls_
.size());
126 ASSERT_TRUE(folder
->GetChild(0)->url() == navigator_
.urls_
[0]);
129 // Tests the enabled state of the menus when supplied an empty vector.
130 TEST_F(BookmarkContextMenuControllerTest
, EmptyNodes
) {
131 BookmarkContextMenuController
controller(
132 NULL
, NULL
, NULL
, profile_
.get(), NULL
, model_
->other_node(),
133 std::vector
<const BookmarkNode
*>());
134 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
136 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
138 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
139 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
141 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
143 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
146 // Tests the enabled state of the menus when supplied a vector with a single
148 TEST_F(BookmarkContextMenuControllerTest
, SingleURL
) {
149 std::vector
<const BookmarkNode
*> nodes
;
150 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
151 BookmarkContextMenuController
controller(
152 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
153 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
155 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
157 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
158 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
160 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
162 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
165 // Tests the enabled state of the menus when supplied a vector with multiple
167 TEST_F(BookmarkContextMenuControllerTest
, MultipleURLs
) {
168 std::vector
<const BookmarkNode
*> nodes
;
169 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
170 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(1)->GetChild(0));
171 BookmarkContextMenuController
controller(
172 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
173 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
175 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
177 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
178 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
180 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
182 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
185 // Tests the enabled state of the menus when supplied an vector with a single
187 TEST_F(BookmarkContextMenuControllerTest
, SingleFolder
) {
188 std::vector
<const BookmarkNode
*> nodes
;
189 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(2));
190 BookmarkContextMenuController
controller(
191 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
192 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
194 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
196 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
197 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
199 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
201 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
204 // Tests the enabled state of the menus when supplied a vector with multiple
205 // folders, all of which are empty.
206 TEST_F(BookmarkContextMenuControllerTest
, MultipleEmptyFolders
) {
207 std::vector
<const BookmarkNode
*> nodes
;
208 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(2));
209 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(3));
210 BookmarkContextMenuController
controller(
211 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
212 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
214 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
216 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
217 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
219 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
221 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
224 // Tests the enabled state of the menus when supplied a vector with multiple
225 // folders, some of which contain URLs.
226 TEST_F(BookmarkContextMenuControllerTest
, MultipleFoldersWithURLs
) {
227 std::vector
<const BookmarkNode
*> nodes
;
228 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(3));
229 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(4));
230 BookmarkContextMenuController
controller(
231 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
232 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
234 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
236 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
237 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
239 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
241 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
244 // Tests the enabled state of open incognito.
245 TEST_F(BookmarkContextMenuControllerTest
, DisableIncognito
) {
246 // Create a new incognito profile.
248 std::vector
<const BookmarkNode
*> nodes
;
249 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
250 BookmarkContextMenuController
controller(
251 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
252 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO
));
254 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
257 // Tests that you can't remove/edit when showing the other node.
258 TEST_F(BookmarkContextMenuControllerTest
, DisabledItemsWithOtherNode
) {
259 std::vector
<const BookmarkNode
*> nodes
;
260 nodes
.push_back(model_
->other_node());
261 BookmarkContextMenuController
controller(
262 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0], nodes
);
263 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_EDIT
));
264 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
267 // Tests the enabled state of the menus when supplied an empty vector and null
269 TEST_F(BookmarkContextMenuControllerTest
, EmptyNodesNullParent
) {
270 BookmarkContextMenuController
controller(
271 NULL
, NULL
, NULL
, profile_
.get(), NULL
, NULL
,
272 std::vector
<const BookmarkNode
*>());
273 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
275 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
277 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
278 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
280 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
282 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
285 // Tests the enabled state of the menus when supplied a vector containing just
286 // the top-level bookmark bar node.
287 TEST_F(BookmarkContextMenuControllerTest
, BookmarkBar
) {
288 std::vector
<const BookmarkNode
*> nodes
;
289 nodes
.push_back(model_
->bookmark_bar_node());
290 BookmarkContextMenuController
controller(
291 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
);
292 EXPECT_TRUE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
294 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
296 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
297 EXPECT_FALSE(controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_REMOVE
));
299 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
301 controller
.IsCommandIdEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
304 TEST_F(BookmarkContextMenuControllerTest
, CutCopyPasteNode
) {
305 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
306 std::vector
<const BookmarkNode
*> nodes
;
307 nodes
.push_back(bb_node
->GetChild(0));
308 scoped_ptr
<BookmarkContextMenuController
> controller(
309 new BookmarkContextMenuController(
310 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
));
311 EXPECT_TRUE(controller
->IsCommandIdEnabled(IDC_COPY
));
312 EXPECT_TRUE(controller
->IsCommandIdEnabled(IDC_CUT
));
315 controller
->ExecuteCommand(IDC_COPY
, 0);
317 controller
.reset(new BookmarkContextMenuController(
318 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
));
319 int old_count
= bb_node
->child_count();
320 controller
->ExecuteCommand(IDC_PASTE
, 0);
322 ASSERT_TRUE(bb_node
->GetChild(1)->is_url());
323 ASSERT_EQ(old_count
+ 1, bb_node
->child_count());
324 ASSERT_EQ(bb_node
->GetChild(0)->url(), bb_node
->GetChild(1)->url());
326 controller
.reset(new BookmarkContextMenuController(
327 NULL
, NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
));
329 controller
->ExecuteCommand(IDC_CUT
, 0);
330 ASSERT_TRUE(bb_node
->GetChild(0)->is_url());
331 ASSERT_TRUE(bb_node
->GetChild(1)->is_folder());
332 ASSERT_EQ(old_count
, bb_node
->child_count());