1 // Copyright 2014 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 "components/bookmarks/browser/bookmark_utils.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/bookmarks/browser/bookmark_node_data.h"
14 #include "components/bookmarks/test/test_bookmark_client.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/clipboard/clipboard.h"
17 #include "ui/base/clipboard/scoped_clipboard_writer.h"
19 using base::ASCIIToUTF16
;
25 class BookmarkUtilsTest
: public testing::Test
,
26 public BaseBookmarkModelObserver
{
29 : grouped_changes_beginning_count_(0),
30 grouped_changes_ended_count_(0) {}
31 virtual ~BookmarkUtilsTest() {}
33 // Copy and paste is not yet supported on iOS. http://crbug.com/228147
35 virtual void TearDown() OVERRIDE
{
36 ui::Clipboard::DestroyClipboardForCurrentThread();
38 #endif // !defined(OS_IOS)
40 // Certain user actions require multiple changes to the bookmark model,
41 // however these modifications need to be atomic for the undo framework. The
42 // BaseBookmarkModelObserver is used to inform the boundaries of the user
43 // action. For example, when multiple bookmarks are cut to the clipboard we
44 // expect one call each to GroupedBookmarkChangesBeginning/Ended.
45 void ExpectGroupedChangeCount(int expected_beginning_count
,
46 int expected_ended_count
) {
47 // The undo framework is not used under Android. Thus the group change
48 // events will not be fired and so should not be tested for Android.
49 #if !defined(OS_ANDROID)
50 EXPECT_EQ(grouped_changes_beginning_count_
, expected_beginning_count
);
51 EXPECT_EQ(grouped_changes_ended_count_
, expected_ended_count
);
56 // BaseBookmarkModelObserver:
57 virtual void BookmarkModelChanged() OVERRIDE
{}
59 virtual void GroupedBookmarkChangesBeginning(BookmarkModel
* model
) OVERRIDE
{
60 ++grouped_changes_beginning_count_
;
63 virtual void GroupedBookmarkChangesEnded(BookmarkModel
* model
) OVERRIDE
{
64 ++grouped_changes_ended_count_
;
67 int grouped_changes_beginning_count_
;
68 int grouped_changes_ended_count_
;
70 // Clipboard requires a message loop.
71 base::MessageLoopForUI loop_
;
73 DISALLOW_COPY_AND_ASSIGN(BookmarkUtilsTest
);
76 TEST_F(BookmarkUtilsTest
, GetBookmarksMatchingPropertiesWordPhraseQuery
) {
77 test::TestBookmarkClient client
;
78 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
79 const BookmarkNode
* node1
= model
->AddURL(model
->other_node(),
81 ASCIIToUTF16("foo bar"),
82 GURL("http://www.google.com"));
83 const BookmarkNode
* node2
= model
->AddURL(model
->other_node(),
85 ASCIIToUTF16("baz buz"),
86 GURL("http://www.cnn.com"));
87 const BookmarkNode
* folder1
=
88 model
->AddFolder(model
->other_node(), 0, ASCIIToUTF16("foo"));
89 std::vector
<const BookmarkNode
*> nodes
;
91 query
.word_phrase_query
.reset(new base::string16
);
92 // No nodes are returned for empty string.
93 *query
.word_phrase_query
= ASCIIToUTF16("");
94 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
95 EXPECT_TRUE(nodes
.empty());
98 // No nodes are returned for space-only string.
99 *query
.word_phrase_query
= ASCIIToUTF16(" ");
100 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
101 EXPECT_TRUE(nodes
.empty());
104 // Node "foo bar" and folder "foo" are returned in search results.
105 *query
.word_phrase_query
= ASCIIToUTF16("foo");
106 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
107 ASSERT_EQ(2U, nodes
.size());
108 EXPECT_TRUE(nodes
[0] == folder1
);
109 EXPECT_TRUE(nodes
[1] == node1
);
112 // Ensure url matches return in search results.
113 *query
.word_phrase_query
= ASCIIToUTF16("cnn");
114 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
115 ASSERT_EQ(1U, nodes
.size());
116 EXPECT_TRUE(nodes
[0] == node2
);
119 // Ensure folder "foo" is not returned in more specific search.
120 *query
.word_phrase_query
= ASCIIToUTF16("foo bar");
121 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
122 ASSERT_EQ(1U, nodes
.size());
123 EXPECT_TRUE(nodes
[0] == node1
);
126 // Bookmark Bar and Other Bookmarks are not returned in search results.
127 *query
.word_phrase_query
= ASCIIToUTF16("Bookmark");
128 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
129 ASSERT_EQ(0U, nodes
.size());
133 // Check exact matching against a URL query.
134 TEST_F(BookmarkUtilsTest
, GetBookmarksMatchingPropertiesUrl
) {
135 test::TestBookmarkClient client
;
136 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
137 const BookmarkNode
* node1
= model
->AddURL(model
->other_node(),
139 ASCIIToUTF16("Google"),
140 GURL("https://www.google.com/"));
141 model
->AddURL(model
->other_node(),
143 ASCIIToUTF16("Google Calendar"),
144 GURL("https://www.google.com/calendar"));
146 model
->AddFolder(model
->other_node(), 0, ASCIIToUTF16("Folder"));
148 std::vector
<const BookmarkNode
*> nodes
;
150 query
.url
.reset(new base::string16
);
151 *query
.url
= ASCIIToUTF16("https://www.google.com/");
152 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
153 ASSERT_EQ(1U, nodes
.size());
154 EXPECT_TRUE(nodes
[0] == node1
);
157 *query
.url
= ASCIIToUTF16("calendar");
158 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
159 ASSERT_EQ(0U, nodes
.size());
162 // Empty URL should not match folders.
163 *query
.url
= ASCIIToUTF16("");
164 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
165 ASSERT_EQ(0U, nodes
.size());
169 // Check exact matching against a title query.
170 TEST_F(BookmarkUtilsTest
, GetBookmarksMatchingPropertiesTitle
) {
171 test::TestBookmarkClient client
;
172 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
173 const BookmarkNode
* node1
= model
->AddURL(model
->other_node(),
175 ASCIIToUTF16("Google"),
176 GURL("https://www.google.com/"));
177 model
->AddURL(model
->other_node(),
179 ASCIIToUTF16("Google Calendar"),
180 GURL("https://www.google.com/calendar"));
182 const BookmarkNode
* folder1
=
183 model
->AddFolder(model
->other_node(), 0, ASCIIToUTF16("Folder"));
185 std::vector
<const BookmarkNode
*> nodes
;
187 query
.title
.reset(new base::string16
);
188 *query
.title
= ASCIIToUTF16("Google");
189 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
190 ASSERT_EQ(1U, nodes
.size());
191 EXPECT_TRUE(nodes
[0] == node1
);
194 *query
.title
= ASCIIToUTF16("Calendar");
195 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
196 ASSERT_EQ(0U, nodes
.size());
199 // Title should match folders.
200 *query
.title
= ASCIIToUTF16("Folder");
201 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
202 ASSERT_EQ(1U, nodes
.size());
203 EXPECT_TRUE(nodes
[0] == folder1
);
207 // Check matching against a query with multiple predicates.
208 TEST_F(BookmarkUtilsTest
, GetBookmarksMatchingPropertiesConjunction
) {
209 test::TestBookmarkClient client
;
210 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
211 const BookmarkNode
* node1
= model
->AddURL(model
->other_node(),
213 ASCIIToUTF16("Google"),
214 GURL("https://www.google.com/"));
215 model
->AddURL(model
->other_node(),
217 ASCIIToUTF16("Google Calendar"),
218 GURL("https://www.google.com/calendar"));
220 model
->AddFolder(model
->other_node(), 0, ASCIIToUTF16("Folder"));
222 std::vector
<const BookmarkNode
*> nodes
;
225 // Test all fields matching.
226 query
.word_phrase_query
.reset(new base::string16(ASCIIToUTF16("www")));
227 query
.url
.reset(new base::string16(ASCIIToUTF16("https://www.google.com/")));
228 query
.title
.reset(new base::string16(ASCIIToUTF16("Google")));
229 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
230 ASSERT_EQ(1U, nodes
.size());
231 EXPECT_TRUE(nodes
[0] == node1
);
234 scoped_ptr
<base::string16
>* fields
[] = {
235 &query
.word_phrase_query
, &query
.url
, &query
.title
};
237 // Test two fields matching.
238 for (size_t i
= 0; i
< arraysize(fields
); i
++) {
239 scoped_ptr
<base::string16
> original_value(fields
[i
]->release());
240 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
241 ASSERT_EQ(1U, nodes
.size());
242 EXPECT_TRUE(nodes
[0] == node1
);
244 fields
[i
]->reset(original_value
.release());
247 // Test two fields matching with one non-matching field.
248 for (size_t i
= 0; i
< arraysize(fields
); i
++) {
249 scoped_ptr
<base::string16
> original_value(fields
[i
]->release());
250 fields
[i
]->reset(new base::string16(ASCIIToUTF16("fjdkslafjkldsa")));
251 GetBookmarksMatchingProperties(model
.get(), query
, 100, string(), &nodes
);
252 ASSERT_EQ(0U, nodes
.size());
254 fields
[i
]->reset(original_value
.release());
258 // Copy and paste is not yet supported on iOS. http://crbug.com/228147
260 TEST_F(BookmarkUtilsTest
, CopyPaste
) {
261 test::TestBookmarkClient client
;
262 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
263 const BookmarkNode
* node
= model
->AddURL(model
->other_node(),
265 ASCIIToUTF16("foo bar"),
266 GURL("http://www.google.com"));
268 // Copy a node to the clipboard.
269 std::vector
<const BookmarkNode
*> nodes
;
270 nodes
.push_back(node
);
271 CopyToClipboard(model
.get(), nodes
, false);
273 // And make sure we can paste a bookmark from the clipboard.
274 EXPECT_TRUE(CanPasteFromClipboard(model
.get(), model
->bookmark_bar_node()));
276 // Write some text to the clipboard.
278 ui::ScopedClipboardWriter
clipboard_writer(
279 ui::Clipboard::GetForCurrentThread(),
280 ui::CLIPBOARD_TYPE_COPY_PASTE
);
281 clipboard_writer
.WriteText(ASCIIToUTF16("foo"));
284 // Now we shouldn't be able to paste from the clipboard.
285 EXPECT_FALSE(CanPasteFromClipboard(model
.get(), model
->bookmark_bar_node()));
288 TEST_F(BookmarkUtilsTest
, CopyPasteMetaInfo
) {
289 test::TestBookmarkClient client
;
290 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
291 const BookmarkNode
* node
= model
->AddURL(model
->other_node(),
293 ASCIIToUTF16("foo bar"),
294 GURL("http://www.google.com"));
295 model
->SetNodeMetaInfo(node
, "somekey", "somevalue");
296 model
->SetNodeMetaInfo(node
, "someotherkey", "someothervalue");
298 // Copy a node to the clipboard.
299 std::vector
<const BookmarkNode
*> nodes
;
300 nodes
.push_back(node
);
301 CopyToClipboard(model
.get(), nodes
, false);
303 // Paste node to a different folder.
304 const BookmarkNode
* folder
=
305 model
->AddFolder(model
->bookmark_bar_node(), 0, ASCIIToUTF16("Folder"));
306 EXPECT_EQ(0, folder
->child_count());
308 // And make sure we can paste a bookmark from the clipboard.
309 EXPECT_TRUE(CanPasteFromClipboard(model
.get(), folder
));
311 PasteFromClipboard(model
.get(), folder
, 0);
312 ASSERT_EQ(1, folder
->child_count());
314 // Verify that the pasted node contains the same meta info.
315 const BookmarkNode
* pasted
= folder
->GetChild(0);
316 ASSERT_TRUE(pasted
->GetMetaInfoMap());
317 EXPECT_EQ(2u, pasted
->GetMetaInfoMap()->size());
319 EXPECT_TRUE(pasted
->GetMetaInfo("somekey", &value
));
320 EXPECT_EQ("somevalue", value
);
321 EXPECT_TRUE(pasted
->GetMetaInfo("someotherkey", &value
));
322 EXPECT_EQ("someothervalue", value
);
325 #if defined(OS_LINUX) || defined(OS_MACOSX)
326 // http://crbug.com/396472
327 #define MAYBE_CutToClipboard DISABLED_CutToClipboard
329 #define MAYBE_CutToClipboard CutToClipboard
331 TEST_F(BookmarkUtilsTest
, MAYBE_CutToClipboard
) {
332 test::TestBookmarkClient client
;
333 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
334 model
->AddObserver(this);
336 base::string16
title(ASCIIToUTF16("foo"));
337 GURL
url("http://foo.com");
338 const BookmarkNode
* n1
= model
->AddURL(model
->other_node(), 0, title
, url
);
339 const BookmarkNode
* n2
= model
->AddURL(model
->other_node(), 1, title
, url
);
341 // Cut the nodes to the clipboard.
342 std::vector
<const BookmarkNode
*> nodes
;
345 CopyToClipboard(model
.get(), nodes
, true);
347 // Make sure the nodes were removed.
348 EXPECT_EQ(0, model
->other_node()->child_count());
350 // Make sure observers were notified the set of changes should be grouped.
351 ExpectGroupedChangeCount(1, 1);
353 // And make sure we can paste from the clipboard.
354 EXPECT_TRUE(CanPasteFromClipboard(model
.get(), model
->other_node()));
357 TEST_F(BookmarkUtilsTest
, PasteNonEditableNodes
) {
358 test::TestBookmarkClient client
;
359 // Load a model with an extra node that is not editable.
360 BookmarkPermanentNode
* extra_node
= new BookmarkPermanentNode(100);
361 BookmarkPermanentNodeList extra_nodes
;
362 extra_nodes
.push_back(extra_node
);
363 client
.SetExtraNodesToLoad(extra_nodes
.Pass());
365 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
366 const BookmarkNode
* node
= model
->AddURL(model
->other_node(),
368 ASCIIToUTF16("foo bar"),
369 GURL("http://www.google.com"));
371 // Copy a node to the clipboard.
372 std::vector
<const BookmarkNode
*> nodes
;
373 nodes
.push_back(node
);
374 CopyToClipboard(model
.get(), nodes
, false);
376 // And make sure we can paste a bookmark from the clipboard.
377 EXPECT_TRUE(CanPasteFromClipboard(model
.get(), model
->bookmark_bar_node()));
379 // But it can't be pasted into a non-editable folder.
380 BookmarkClient
* upcast
= &client
;
381 EXPECT_FALSE(upcast
->CanBeEditedByUser(extra_node
));
382 EXPECT_FALSE(CanPasteFromClipboard(model
.get(), extra_node
));
384 #endif // !defined(OS_IOS)
386 TEST_F(BookmarkUtilsTest
, GetParentForNewNodes
) {
387 test::TestBookmarkClient client
;
388 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
389 // This tests the case where selection contains one item and that item is a
391 std::vector
<const BookmarkNode
*> nodes
;
392 nodes
.push_back(model
->bookmark_bar_node());
394 const BookmarkNode
* real_parent
=
395 GetParentForNewNodes(model
->bookmark_bar_node(), nodes
, &index
);
396 EXPECT_EQ(real_parent
, model
->bookmark_bar_node());
401 // This tests the case where selection contains one item and that item is an
403 const BookmarkNode
* page1
= model
->AddURL(model
->bookmark_bar_node(),
405 ASCIIToUTF16("Google"),
406 GURL("http://google.com"));
407 nodes
.push_back(page1
);
408 real_parent
= GetParentForNewNodes(model
->bookmark_bar_node(), nodes
, &index
);
409 EXPECT_EQ(real_parent
, model
->bookmark_bar_node());
412 // This tests the case where selection has more than one item.
413 const BookmarkNode
* folder1
=
414 model
->AddFolder(model
->bookmark_bar_node(), 1, ASCIIToUTF16("Folder 1"));
415 nodes
.push_back(folder1
);
416 real_parent
= GetParentForNewNodes(model
->bookmark_bar_node(), nodes
, &index
);
417 EXPECT_EQ(real_parent
, model
->bookmark_bar_node());
420 // This tests the case where selection doesn't contain any items.
422 real_parent
= GetParentForNewNodes(model
->bookmark_bar_node(), nodes
, &index
);
423 EXPECT_EQ(real_parent
, model
->bookmark_bar_node());
427 // Verifies that meta info is copied when nodes are cloned.
428 TEST_F(BookmarkUtilsTest
, CloneMetaInfo
) {
429 test::TestBookmarkClient client
;
430 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
431 // Add a node containing meta info.
432 const BookmarkNode
* node
= model
->AddURL(model
->other_node(),
434 ASCIIToUTF16("foo bar"),
435 GURL("http://www.google.com"));
436 model
->SetNodeMetaInfo(node
, "somekey", "somevalue");
437 model
->SetNodeMetaInfo(node
, "someotherkey", "someothervalue");
439 // Clone node to a different folder.
440 const BookmarkNode
* folder
=
441 model
->AddFolder(model
->bookmark_bar_node(), 0, ASCIIToUTF16("Folder"));
442 std::vector
<BookmarkNodeData::Element
> elements
;
443 BookmarkNodeData::Element
node_data(node
);
444 elements
.push_back(node_data
);
445 EXPECT_EQ(0, folder
->child_count());
446 CloneBookmarkNode(model
.get(), elements
, folder
, 0, false);
447 ASSERT_EQ(1, folder
->child_count());
449 // Verify that the cloned node contains the same meta info.
450 const BookmarkNode
* clone
= folder
->GetChild(0);
451 ASSERT_TRUE(clone
->GetMetaInfoMap());
452 EXPECT_EQ(2u, clone
->GetMetaInfoMap()->size());
454 EXPECT_TRUE(clone
->GetMetaInfo("somekey", &value
));
455 EXPECT_EQ("somevalue", value
);
456 EXPECT_TRUE(clone
->GetMetaInfo("someotherkey", &value
));
457 EXPECT_EQ("someothervalue", value
);
460 TEST_F(BookmarkUtilsTest
, RemoveAllBookmarks
) {
461 test::TestBookmarkClient client
;
462 // Load a model with an extra node that is not editable.
463 BookmarkPermanentNode
* extra_node
= new BookmarkPermanentNode(100);
464 BookmarkPermanentNodeList extra_nodes
;
465 extra_nodes
.push_back(extra_node
);
466 client
.SetExtraNodesToLoad(extra_nodes
.Pass());
468 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
469 EXPECT_TRUE(model
->bookmark_bar_node()->empty());
470 EXPECT_TRUE(model
->other_node()->empty());
471 EXPECT_TRUE(model
->mobile_node()->empty());
472 EXPECT_TRUE(extra_node
->empty());
474 const base::string16 title
= base::ASCIIToUTF16("Title");
475 const GURL
url("http://google.com");
476 model
->AddURL(model
->bookmark_bar_node(), 0, title
, url
);
477 model
->AddURL(model
->other_node(), 0, title
, url
);
478 model
->AddURL(model
->mobile_node(), 0, title
, url
);
479 model
->AddURL(extra_node
, 0, title
, url
);
481 std::vector
<const BookmarkNode
*> nodes
;
482 model
->GetNodesByURL(url
, &nodes
);
483 ASSERT_EQ(4u, nodes
.size());
485 RemoveAllBookmarks(model
.get(), url
);
488 model
->GetNodesByURL(url
, &nodes
);
489 ASSERT_EQ(1u, nodes
.size());
490 EXPECT_TRUE(model
->bookmark_bar_node()->empty());
491 EXPECT_TRUE(model
->other_node()->empty());
492 EXPECT_TRUE(model
->mobile_node()->empty());
493 EXPECT_EQ(1, extra_node
->child_count());
497 } // namespace bookmarks