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/views/bookmarks/bookmark_context_menu.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/bookmarks/bookmark_model.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/page_navigator.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "grit/generated_resources.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/base/clipboard/clipboard.h"
28 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
31 using content::BrowserThread
;
32 using content::OpenURLParams
;
33 using content::PageNavigator
;
34 using content::WebContents
;
38 // PageNavigator implementation that records the URL.
39 class TestingPageNavigator
: public PageNavigator
{
41 virtual WebContents
* OpenURL(const OpenURLParams
& params
) OVERRIDE
{
42 urls_
.push_back(params
.url
);
46 std::vector
<GURL
> urls_
;
51 class BookmarkContextMenuTest
: public testing::Test
{
53 BookmarkContextMenuTest()
54 : ui_thread_(BrowserThread::UI
, &message_loop_
),
55 file_thread_(BrowserThread::FILE, &message_loop_
),
59 virtual void SetUp() OVERRIDE
{
60 profile_
.reset(new TestingProfile());
61 profile_
->CreateBookmarkModel(true);
63 model_
= BookmarkModelFactory::GetForProfile(profile_
.get());
64 ui_test_utils::WaitForBookmarkModelToLoad(model_
);
69 virtual void TearDown() OVERRIDE
{
70 ui::Clipboard::DestroyClipboardForCurrentThread();
72 BrowserThread::GetBlockingPool()->FlushForTesting();
73 // Flush the message loop to make application verifiers happy.
74 message_loop_
.RunUntilIdle();
78 base::MessageLoopForUI message_loop_
;
79 content::TestBrowserThread ui_thread_
;
80 content::TestBrowserThread file_thread_
;
81 scoped_ptr
<TestingProfile
> profile_
;
82 BookmarkModel
* model_
;
83 TestingPageNavigator navigator_
;
86 // Creates the following structure:
90 // -f1b as "chrome://settings"
98 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
99 std::string test_base
= "file:///c:/tmp/";
100 model_
->AddURL(bb_node
, 0, ASCIIToUTF16("a"), GURL(test_base
+ "a"));
101 const BookmarkNode
* f1
= model_
->AddFolder(bb_node
, 1, ASCIIToUTF16("F1"));
102 model_
->AddURL(f1
, 0, ASCIIToUTF16("f1a"), GURL(test_base
+ "f1a"));
103 model_
->AddURL(f1
, 1, ASCIIToUTF16("f1b"), GURL("chrome://settings"));
104 const BookmarkNode
* f11
= model_
->AddFolder(f1
, 2, ASCIIToUTF16("F11"));
105 model_
->AddURL(f11
, 0, ASCIIToUTF16("f11a"), GURL(test_base
+ "f11a"));
106 model_
->AddFolder(bb_node
, 2, ASCIIToUTF16("F2"));
107 model_
->AddFolder(bb_node
, 3, ASCIIToUTF16("F3"));
108 const BookmarkNode
* f4
= model_
->AddFolder(bb_node
, 4, ASCIIToUTF16("F4"));
109 model_
->AddURL(f4
, 0, ASCIIToUTF16("f4a"), GURL(test_base
+ "f4a"));
113 // Tests Deleting from the menu.
114 TEST_F(BookmarkContextMenuTest
, DeleteURL
) {
115 std::vector
<const BookmarkNode
*> nodes
;
116 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
117 BookmarkContextMenu
controller(
118 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
119 GURL url
= model_
->bookmark_bar_node()->GetChild(0)->url();
120 ASSERT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
122 controller
.ExecuteCommand(IDC_BOOKMARK_BAR_REMOVE
, 0);
123 // Model shouldn't have URL anymore.
124 ASSERT_FALSE(model_
->IsBookmarked(url
));
127 // Tests open all on a folder with a couple of bookmarks.
128 TEST_F(BookmarkContextMenuTest
, OpenAll
) {
129 const BookmarkNode
* folder
= model_
->bookmark_bar_node()->GetChild(1);
130 chrome::OpenAll(NULL
, &navigator_
, folder
, NEW_FOREGROUND_TAB
, NULL
);
132 // Should have navigated to F1's child but not F11's child.
133 ASSERT_EQ(static_cast<size_t>(2), navigator_
.urls_
.size());
134 ASSERT_TRUE(folder
->GetChild(0)->url() == navigator_
.urls_
[0]);
137 // Tests open all on a folder with a couple of bookmarks in incognito window.
138 TEST_F(BookmarkContextMenuTest
, OpenAllIngonito
) {
139 const BookmarkNode
* folder
= model_
->bookmark_bar_node()->GetChild(1);
140 chrome::OpenAll(NULL
, &navigator_
, folder
, OFF_THE_RECORD
, NULL
);
142 // Should have navigated to only f1a but not f2a.
143 ASSERT_EQ(static_cast<size_t>(1), navigator_
.urls_
.size());
144 ASSERT_TRUE(folder
->GetChild(0)->url() == navigator_
.urls_
[0]);
147 // Tests the enabled state of the menus when supplied an empty vector.
148 TEST_F(BookmarkContextMenuTest
, EmptyNodes
) {
149 BookmarkContextMenu
controller(
150 NULL
, NULL
, profile_
.get(), NULL
, model_
->other_node(),
151 std::vector
<const BookmarkNode
*>(), false);
152 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
154 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
156 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
157 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
159 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
161 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
164 // Tests the enabled state of the menus when supplied a vector with a single
166 TEST_F(BookmarkContextMenuTest
, SingleURL
) {
167 std::vector
<const BookmarkNode
*> nodes
;
168 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
169 BookmarkContextMenu
controller(
170 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
171 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
173 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
174 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
175 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
177 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
179 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
182 // Tests the enabled state of the menus when supplied a vector with multiple
184 TEST_F(BookmarkContextMenuTest
, MultipleURLs
) {
185 std::vector
<const BookmarkNode
*> nodes
;
186 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
187 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(1)->GetChild(0));
188 BookmarkContextMenu
controller(
189 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
190 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
192 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
193 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
194 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
196 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
198 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
201 // Tests the enabled state of the menus when supplied an vector with a single
203 TEST_F(BookmarkContextMenuTest
, SingleFolder
) {
204 std::vector
<const BookmarkNode
*> nodes
;
205 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(2));
206 BookmarkContextMenu
controller(
207 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
208 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
210 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
212 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
213 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
215 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
217 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
220 // Tests the enabled state of the menus when supplied a vector with multiple
221 // folders, all of which are empty.
222 TEST_F(BookmarkContextMenuTest
, MultipleEmptyFolders
) {
223 std::vector
<const BookmarkNode
*> nodes
;
224 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(2));
225 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(3));
226 BookmarkContextMenu
controller(
227 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
228 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
230 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
232 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
233 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
235 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
237 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
240 // Tests the enabled state of the menus when supplied a vector with multiple
241 // folders, some of which contain URLs.
242 TEST_F(BookmarkContextMenuTest
, MultipleFoldersWithURLs
) {
243 std::vector
<const BookmarkNode
*> nodes
;
244 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(3));
245 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(4));
246 BookmarkContextMenu
controller(
247 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
248 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
250 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
251 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
252 EXPECT_TRUE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
254 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
256 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
259 // Tests the enabled state of open incognito.
260 TEST_F(BookmarkContextMenuTest
, DisableIncognito
) {
261 std::vector
<const BookmarkNode
*> nodes
;
262 nodes
.push_back(model_
->bookmark_bar_node()->GetChild(0));
263 TestingProfile::Builder builder
;
264 builder
.SetIncognito();
265 scoped_ptr
<TestingProfile
> incognito_
= builder
.Build().Pass();
266 incognito_
->SetOriginalProfile(profile_
.get());
267 BookmarkContextMenu
controller(
268 NULL
, NULL
, incognito_
.get(), NULL
, nodes
[0]->parent(), nodes
, false);
269 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_INCOGNITO
));
271 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
274 // Tests that you can't remove/edit when showing the other node.
275 TEST_F(BookmarkContextMenuTest
, DisabledItemsWithOtherNode
) {
276 std::vector
<const BookmarkNode
*> nodes
;
277 nodes
.push_back(model_
->other_node());
278 BookmarkContextMenu
controller(
279 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0], nodes
, false);
280 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_EDIT
));
281 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
284 // Tests the enabled state of the menus when supplied an empty vector and null
286 TEST_F(BookmarkContextMenuTest
, EmptyNodesNullParent
) {
287 BookmarkContextMenu
controller(
288 NULL
, NULL
, profile_
.get(), NULL
, NULL
,
289 std::vector
<const BookmarkNode
*>(), false);
290 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL
));
292 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_NEW_WINDOW
));
294 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_OPEN_ALL_INCOGNITO
));
295 EXPECT_FALSE(controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_REMOVE
));
297 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_ADD_NEW_BOOKMARK
));
299 controller
.IsCommandEnabled(IDC_BOOKMARK_BAR_NEW_FOLDER
));
302 TEST_F(BookmarkContextMenuTest
, CutCopyPasteNode
) {
303 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
304 std::vector
<const BookmarkNode
*> nodes
;
305 nodes
.push_back(bb_node
->GetChild(0));
306 scoped_ptr
<BookmarkContextMenu
> controller(new BookmarkContextMenu(
307 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false));
308 EXPECT_TRUE(controller
->IsCommandEnabled(IDC_COPY
));
309 EXPECT_TRUE(controller
->IsCommandEnabled(IDC_CUT
));
312 controller
->ExecuteCommand(IDC_COPY
, 0);
314 controller
.reset(new BookmarkContextMenu(
315 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false));
316 int old_count
= bb_node
->child_count();
317 controller
->ExecuteCommand(IDC_PASTE
, 0);
319 ASSERT_TRUE(bb_node
->GetChild(1)->is_url());
320 ASSERT_EQ(old_count
+ 1, bb_node
->child_count());
321 ASSERT_EQ(bb_node
->GetChild(0)->url(), bb_node
->GetChild(1)->url());
323 controller
.reset(new BookmarkContextMenu(
324 NULL
, NULL
, profile_
.get(), NULL
, nodes
[0]->parent(), nodes
, false));
326 controller
->ExecuteCommand(IDC_CUT
, 0);
327 ASSERT_TRUE(bb_node
->GetChild(0)->is_url());
328 ASSERT_TRUE(bb_node
->GetChild(1)->is_folder());
329 ASSERT_EQ(old_count
, bb_node
->child_count());