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_model.h"
10 #include "base/base_paths.h"
11 #include "base/basictypes.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h"
21 #include "components/bookmarks/browser/bookmark_model_observer.h"
22 #include "components/bookmarks/browser/bookmark_utils.h"
23 #include "components/bookmarks/test/bookmark_test_helpers.h"
24 #include "components/bookmarks/test/test_bookmark_client.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/base/models/tree_node_iterator.h"
27 #include "ui/base/models/tree_node_model.h"
30 using base::ASCIIToUTF16
;
32 using base::TimeDelta
;
37 // Test cases used to test the removal of extra whitespace when adding
38 // a new folder/bookmark or updating a title of a folder/bookmark.
40 const std::string input_title
;
41 const std::string expected_title
;
42 } url_whitespace_test_cases
[] = {
45 {"foo\nbar", "foo bar"},
46 {"foo\n\nbar", "foo bar"},
47 {"foo\n\n\nbar", "foo bar"},
48 {"foo\r\nbar", "foo bar"},
49 {"foo\r\n\r\nbar", "foo bar"},
50 {"\nfoo\nbar\n", "foo bar"},
52 {"foo bar", "foo bar"},
53 {" foo bar ", "foo bar"},
54 {" foo bar ", "foo bar"},
56 {"\tfoo\tbar\t", "foo bar"},
57 {"\tfoo bar\t", "foo bar"},
59 {"\tfoo\nbar\t", "foo bar"},
60 {"\tfoo\r\nbar\t", "foo bar"},
61 {" foo\tbar\n", "foo bar"},
62 {"\t foo \t bar \t", "foo bar"},
63 {"\n foo\r\n\tbar\n \t", "foo bar"},
66 // Test cases used to test the removal of extra whitespace when adding
67 // a new folder/bookmark or updating a title of a folder/bookmark.
69 const std::string input_title
;
70 const std::string expected_title
;
71 } title_whitespace_test_cases
[] = {
74 {"foo\nbar", "foo bar"},
75 {"foo\n\nbar", "foo bar"},
76 {"foo\n\n\nbar", "foo bar"},
77 {"foo\r\nbar", "foo bar"},
78 {"foo\r\n\r\nbar", "foo bar"},
79 {"\nfoo\nbar\n", " foo bar "},
81 {"foo bar", "foo bar"},
82 {" foo bar ", " foo bar "},
83 {" foo bar ", " foo bar "},
85 {"\tfoo\tbar\t", " foo bar "},
86 {"\tfoo bar\t", " foo bar "},
88 {"\tfoo\nbar\t", " foo bar "},
89 {"\tfoo\r\nbar\t", " foo bar "},
90 {" foo\tbar\n", " foo bar "},
91 {"\t foo \t bar \t", " foo bar "},
92 {"\n foo\r\n\tbar\n \t", " foo bar "},
95 // Helper to get a mutable bookmark node.
96 BookmarkNode
* AsMutable(const BookmarkNode
* node
) {
97 return const_cast<BookmarkNode
*>(node
);
100 void SwapDateAdded(BookmarkNode
* n1
, BookmarkNode
* n2
) {
101 Time tmp
= n1
->date_added();
102 n1
->set_date_added(n2
->date_added());
103 n2
->set_date_added(tmp
);
106 // See comment in PopulateNodeFromString.
107 using TestNode
= ui::TreeNodeWithValue
<BookmarkNode::Type
>;
109 // Does the work of PopulateNodeFromString. index gives the index of the current
110 // element in description to process.
111 void PopulateNodeImpl(const std::vector
<std::string
>& description
,
114 while (*index
< description
.size()) {
115 const std::string
& element
= description
[*index
];
117 if (element
== "[") {
118 // Create a new folder and recurse to add all the children.
119 // Folders are given a unique named by way of an ever increasing integer
120 // value. The folders need not have a name, but one is assigned to help
122 static int next_folder_id
= 1;
123 TestNode
* new_node
= new TestNode(base::IntToString16(next_folder_id
++),
124 BookmarkNode::FOLDER
);
125 parent
->Add(new_node
, parent
->child_count());
126 PopulateNodeImpl(description
, index
, new_node
);
127 } else if (element
== "]") {
128 // End the current folder.
133 // All tokens must be space separated. If there is a [ or ] in the name it
134 // likely means a space was forgotten.
135 DCHECK(element
.find('[') == std::string::npos
);
136 DCHECK(element
.find(']') == std::string::npos
);
137 parent
->Add(new TestNode(base::UTF8ToUTF16(element
), BookmarkNode::URL
),
138 parent
->child_count());
143 // Creates and adds nodes to parent based on description. description consists
144 // of the following tokens (all space separated):
145 // [ : creates a new USER_FOLDER node. All elements following the [ until the
146 // next balanced ] is encountered are added as children to the node.
147 // ] : closes the last folder created by [ so that any further nodes are added
148 // to the current folders parent.
149 // text: creates a new URL node.
150 // For example, "a [b] c" creates the following nodes:
154 // In words: a node of type URL with the title a, followed by a folder node with
155 // the title 1 having the single child of type url with name b, followed by
156 // the url node with the title c.
158 // NOTE: each name must be unique, and folders are assigned a unique title by
159 // way of an increasing integer.
160 void PopulateNodeFromString(const std::string
& description
, TestNode
* parent
) {
161 std::vector
<std::string
> elements
;
162 base::SplitStringAlongWhitespace(description
, &elements
);
164 PopulateNodeImpl(elements
, &index
, parent
);
167 // Populates the BookmarkNode with the children of parent.
168 void PopulateBookmarkNode(TestNode
* parent
,
169 BookmarkModel
* model
,
170 const BookmarkNode
* bb_node
) {
171 for (int i
= 0; i
< parent
->child_count(); ++i
) {
172 TestNode
* child
= parent
->GetChild(i
);
173 if (child
->value
== BookmarkNode::FOLDER
) {
174 const BookmarkNode
* new_bb_node
=
175 model
->AddFolder(bb_node
, i
, child
->GetTitle());
176 PopulateBookmarkNode(child
, model
, new_bb_node
);
178 model
->AddURL(bb_node
, i
, child
->GetTitle(),
179 GURL("http://" + base::UTF16ToASCII(child
->GetTitle())));
184 // Verifies the contents of the bookmark bar node match the contents of the
186 void VerifyModelMatchesNode(TestNode
* expected
, const BookmarkNode
* actual
) {
187 ASSERT_EQ(expected
->child_count(), actual
->child_count());
188 for (int i
= 0; i
< expected
->child_count(); ++i
) {
189 TestNode
* expected_child
= expected
->GetChild(i
);
190 const BookmarkNode
* actual_child
= actual
->GetChild(i
);
191 ASSERT_EQ(expected_child
->GetTitle(), actual_child
->GetTitle());
192 if (expected_child
->value
== BookmarkNode::FOLDER
) {
193 ASSERT_TRUE(actual_child
->type() == BookmarkNode::FOLDER
);
194 // Recurse throught children.
195 VerifyModelMatchesNode(expected_child
, actual_child
);
197 // No need to check the URL, just the title is enough.
198 ASSERT_TRUE(actual_child
->is_url());
203 void VerifyNoDuplicateIDs(BookmarkModel
* model
) {
204 ui::TreeNodeIterator
<const BookmarkNode
> it(model
->root_node());
205 base::hash_set
<int64
> ids
;
206 while (it
.has_next())
207 ASSERT_TRUE(ids
.insert(it
.Next()->id()).second
);
210 class BookmarkModelTest
: public testing::Test
,
211 public BookmarkModelObserver
{
213 struct ObserverDetails
{
215 Set(NULL
, NULL
, -1, -1);
218 void Set(const BookmarkNode
* node1
,
219 const BookmarkNode
* node2
,
228 void ExpectEquals(const BookmarkNode
* node1
,
229 const BookmarkNode
* node2
,
232 EXPECT_EQ(node1_
, node1
);
233 EXPECT_EQ(node2_
, node2
);
234 EXPECT_EQ(index1_
, index1
);
235 EXPECT_EQ(index2_
, index2
);
239 const BookmarkNode
* node1_
;
240 const BookmarkNode
* node2_
;
245 BookmarkModelTest() : model_(client_
.CreateModel()) {
246 model_
->AddObserver(this);
250 void BookmarkModelLoaded(BookmarkModel
* model
, bool ids_reassigned
) override
{
251 // We never load from the db, so that this should never get invoked.
255 void BookmarkNodeMoved(BookmarkModel
* model
,
256 const BookmarkNode
* old_parent
,
258 const BookmarkNode
* new_parent
,
259 int new_index
) override
{
261 observer_details_
.Set(old_parent
, new_parent
, old_index
, new_index
);
264 void BookmarkNodeAdded(BookmarkModel
* model
,
265 const BookmarkNode
* parent
,
266 int index
) override
{
268 observer_details_
.Set(parent
, NULL
, index
, -1);
271 void OnWillRemoveBookmarks(BookmarkModel
* model
,
272 const BookmarkNode
* parent
,
274 const BookmarkNode
* node
) override
{
275 ++before_remove_count_
;
278 void BookmarkNodeRemoved(BookmarkModel
* model
,
279 const BookmarkNode
* parent
,
281 const BookmarkNode
* node
,
282 const std::set
<GURL
>& removed_urls
) override
{
284 observer_details_
.Set(parent
, NULL
, old_index
, -1);
287 void BookmarkNodeChanged(BookmarkModel
* model
,
288 const BookmarkNode
* node
) override
{
290 observer_details_
.Set(node
, NULL
, -1, -1);
293 void OnWillChangeBookmarkNode(BookmarkModel
* model
,
294 const BookmarkNode
* node
) override
{
295 ++before_change_count_
;
298 void BookmarkNodeChildrenReordered(BookmarkModel
* model
,
299 const BookmarkNode
* node
) override
{
303 void OnWillReorderBookmarkNode(BookmarkModel
* model
,
304 const BookmarkNode
* node
) override
{
305 ++before_reorder_count_
;
308 void BookmarkNodeFaviconChanged(BookmarkModel
* model
,
309 const BookmarkNode
* node
) override
{
310 // We never attempt to load favicons, so that this method never
314 void ExtensiveBookmarkChangesBeginning(BookmarkModel
* model
) override
{
315 ++extensive_changes_beginning_count_
;
318 void ExtensiveBookmarkChangesEnded(BookmarkModel
* model
) override
{
319 ++extensive_changes_ended_count_
;
322 void BookmarkAllUserNodesRemoved(
323 BookmarkModel
* model
,
324 const std::set
<GURL
>& removed_urls
) override
{
325 ++all_bookmarks_removed_
;
328 void OnWillRemoveAllUserBookmarks(BookmarkModel
* model
) override
{
329 ++before_remove_all_count_
;
333 added_count_
= moved_count_
= removed_count_
= changed_count_
=
334 reordered_count_
= extensive_changes_beginning_count_
=
335 extensive_changes_ended_count_
= all_bookmarks_removed_
=
336 before_remove_count_
= before_change_count_
= before_reorder_count_
=
337 before_remove_all_count_
= 0;
340 void AssertObserverCount(int added_count
,
345 int before_remove_count
,
346 int before_change_count
,
347 int before_reorder_count
,
348 int before_remove_all_count
) {
349 EXPECT_EQ(added_count
, added_count_
);
350 EXPECT_EQ(moved_count
, moved_count_
);
351 EXPECT_EQ(removed_count
, removed_count_
);
352 EXPECT_EQ(changed_count
, changed_count_
);
353 EXPECT_EQ(reordered_count
, reordered_count_
);
354 EXPECT_EQ(before_remove_count
, before_remove_count_
);
355 EXPECT_EQ(before_change_count
, before_change_count_
);
356 EXPECT_EQ(before_reorder_count
, before_reorder_count_
);
357 EXPECT_EQ(before_remove_all_count
, before_remove_all_count_
);
360 void AssertExtensiveChangesObserverCount(
361 int extensive_changes_beginning_count
,
362 int extensive_changes_ended_count
) {
363 EXPECT_EQ(extensive_changes_beginning_count
,
364 extensive_changes_beginning_count_
);
365 EXPECT_EQ(extensive_changes_ended_count
, extensive_changes_ended_count_
);
368 int AllNodesRemovedObserverCount() const { return all_bookmarks_removed_
; }
370 BookmarkPermanentNode
* ReloadModelWithExtraNode() {
371 BookmarkPermanentNode
* extra_node
= new BookmarkPermanentNode(100);
372 BookmarkPermanentNodeList extra_nodes
;
373 extra_nodes
.push_back(extra_node
);
374 client_
.SetExtraNodesToLoad(extra_nodes
.Pass());
376 model_
->RemoveObserver(this);
377 model_
= client_
.CreateModel();
378 model_
->AddObserver(this);
381 if (model_
->root_node()->GetIndexOf(extra_node
) == -1)
388 TestBookmarkClient client_
;
389 scoped_ptr
<BookmarkModel
> model_
;
390 ObserverDetails observer_details_
;
397 int reordered_count_
;
398 int extensive_changes_beginning_count_
;
399 int extensive_changes_ended_count_
;
400 int all_bookmarks_removed_
;
401 int before_remove_count_
;
402 int before_change_count_
;
403 int before_reorder_count_
;
404 int before_remove_all_count_
;
406 DISALLOW_COPY_AND_ASSIGN(BookmarkModelTest
);
409 TEST_F(BookmarkModelTest
, InitialState
) {
410 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
411 ASSERT_TRUE(bb_node
!= NULL
);
412 EXPECT_EQ(0, bb_node
->child_count());
413 EXPECT_EQ(BookmarkNode::BOOKMARK_BAR
, bb_node
->type());
415 const BookmarkNode
* other_node
= model_
->other_node();
416 ASSERT_TRUE(other_node
!= NULL
);
417 EXPECT_EQ(0, other_node
->child_count());
418 EXPECT_EQ(BookmarkNode::OTHER_NODE
, other_node
->type());
420 const BookmarkNode
* mobile_node
= model_
->mobile_node();
421 ASSERT_TRUE(mobile_node
!= NULL
);
422 EXPECT_EQ(0, mobile_node
->child_count());
423 EXPECT_EQ(BookmarkNode::MOBILE
, mobile_node
->type());
425 EXPECT_TRUE(bb_node
->id() != other_node
->id());
426 EXPECT_TRUE(bb_node
->id() != mobile_node
->id());
427 EXPECT_TRUE(other_node
->id() != mobile_node
->id());
430 TEST_F(BookmarkModelTest
, AddURL
) {
431 const BookmarkNode
* root
= model_
->bookmark_bar_node();
432 const base::string16
title(ASCIIToUTF16("foo"));
433 const GURL
url("http://foo.com");
435 const BookmarkNode
* new_node
= model_
->AddURL(root
, 0, title
, url
);
436 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
437 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
439 ASSERT_EQ(1, root
->child_count());
440 ASSERT_EQ(title
, new_node
->GetTitle());
441 ASSERT_TRUE(url
== new_node
->url());
442 ASSERT_EQ(BookmarkNode::URL
, new_node
->type());
443 ASSERT_TRUE(new_node
== model_
->GetMostRecentlyAddedUserNodeForURL(url
));
445 EXPECT_TRUE(new_node
->id() != root
->id() &&
446 new_node
->id() != model_
->other_node()->id() &&
447 new_node
->id() != model_
->mobile_node()->id());
450 TEST_F(BookmarkModelTest
, AddURLWithUnicodeTitle
) {
451 const BookmarkNode
* root
= model_
->bookmark_bar_node();
452 const base::string16
title(base::WideToUTF16(
453 L
"\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053"));
454 const GURL
url("https://www.baidu.com/");
456 const BookmarkNode
* new_node
= model_
->AddURL(root
, 0, title
, url
);
457 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
458 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
460 ASSERT_EQ(1, root
->child_count());
461 ASSERT_EQ(title
, new_node
->GetTitle());
462 ASSERT_TRUE(url
== new_node
->url());
463 ASSERT_EQ(BookmarkNode::URL
, new_node
->type());
464 ASSERT_TRUE(new_node
== model_
->GetMostRecentlyAddedUserNodeForURL(url
));
466 EXPECT_TRUE(new_node
->id() != root
->id() &&
467 new_node
->id() != model_
->other_node()->id() &&
468 new_node
->id() != model_
->mobile_node()->id());
471 TEST_F(BookmarkModelTest
, AddURLWithWhitespaceTitle
) {
472 for (size_t i
= 0; i
< arraysize(url_whitespace_test_cases
); ++i
) {
473 const BookmarkNode
* root
= model_
->bookmark_bar_node();
474 const base::string16
title(
475 ASCIIToUTF16(url_whitespace_test_cases
[i
].input_title
));
476 const GURL
url("http://foo.com");
478 const BookmarkNode
* new_node
= model_
->AddURL(root
, i
, title
, url
);
481 EXPECT_EQ(size
, root
->child_count());
482 EXPECT_EQ(ASCIIToUTF16(url_whitespace_test_cases
[i
].expected_title
),
483 new_node
->GetTitle());
484 EXPECT_EQ(BookmarkNode::URL
, new_node
->type());
488 TEST_F(BookmarkModelTest
, AddURLWithCreationTimeAndMetaInfo
) {
489 const BookmarkNode
* root
= model_
->bookmark_bar_node();
490 const base::string16
title(ASCIIToUTF16("foo"));
491 const GURL
url("http://foo.com");
492 const Time time
= Time::Now() - TimeDelta::FromDays(1);
493 BookmarkNode::MetaInfoMap meta_info
;
494 meta_info
["foo"] = "bar";
496 const BookmarkNode
* new_node
= model_
->AddURLWithCreationTimeAndMetaInfo(
497 root
, 0, title
, url
, time
, &meta_info
);
498 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
499 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
501 ASSERT_EQ(1, root
->child_count());
502 ASSERT_EQ(title
, new_node
->GetTitle());
503 ASSERT_TRUE(url
== new_node
->url());
504 ASSERT_EQ(BookmarkNode::URL
, new_node
->type());
505 ASSERT_EQ(time
, new_node
->date_added());
506 ASSERT_TRUE(new_node
->GetMetaInfoMap());
507 ASSERT_EQ(meta_info
, *new_node
->GetMetaInfoMap());
508 ASSERT_TRUE(new_node
== model_
->GetMostRecentlyAddedUserNodeForURL(url
));
510 EXPECT_TRUE(new_node
->id() != root
->id() &&
511 new_node
->id() != model_
->other_node()->id() &&
512 new_node
->id() != model_
->mobile_node()->id());
515 TEST_F(BookmarkModelTest
, AddURLToMobileBookmarks
) {
516 const BookmarkNode
* root
= model_
->mobile_node();
517 const base::string16
title(ASCIIToUTF16("foo"));
518 const GURL
url("http://foo.com");
520 const BookmarkNode
* new_node
= model_
->AddURL(root
, 0, title
, url
);
521 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
522 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
524 ASSERT_EQ(1, root
->child_count());
525 ASSERT_EQ(title
, new_node
->GetTitle());
526 ASSERT_TRUE(url
== new_node
->url());
527 ASSERT_EQ(BookmarkNode::URL
, new_node
->type());
528 ASSERT_TRUE(new_node
== model_
->GetMostRecentlyAddedUserNodeForURL(url
));
530 EXPECT_TRUE(new_node
->id() != root
->id() &&
531 new_node
->id() != model_
->other_node()->id() &&
532 new_node
->id() != model_
->mobile_node()->id());
535 TEST_F(BookmarkModelTest
, AddFolder
) {
536 const BookmarkNode
* root
= model_
->bookmark_bar_node();
537 const base::string16
title(ASCIIToUTF16("foo"));
539 const BookmarkNode
* new_node
= model_
->AddFolder(root
, 0, title
);
540 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
541 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
543 ASSERT_EQ(1, root
->child_count());
544 ASSERT_EQ(title
, new_node
->GetTitle());
545 ASSERT_EQ(BookmarkNode::FOLDER
, new_node
->type());
547 EXPECT_TRUE(new_node
->id() != root
->id() &&
548 new_node
->id() != model_
->other_node()->id() &&
549 new_node
->id() != model_
->mobile_node()->id());
551 // Add another folder, just to make sure folder_ids are incremented correctly.
553 model_
->AddFolder(root
, 0, title
);
554 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
555 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
558 TEST_F(BookmarkModelTest
, AddFolderWithWhitespaceTitle
) {
559 for (size_t i
= 0; i
< arraysize(title_whitespace_test_cases
); ++i
) {
560 const BookmarkNode
* root
= model_
->bookmark_bar_node();
561 const base::string16
title(
562 ASCIIToUTF16(title_whitespace_test_cases
[i
].input_title
));
564 const BookmarkNode
* new_node
= model_
->AddFolder(root
, i
, title
);
567 EXPECT_EQ(size
, root
->child_count());
568 EXPECT_EQ(ASCIIToUTF16(title_whitespace_test_cases
[i
].expected_title
),
569 new_node
->GetTitle());
570 EXPECT_EQ(BookmarkNode::FOLDER
, new_node
->type());
574 TEST_F(BookmarkModelTest
, RemoveURL
) {
575 const BookmarkNode
* root
= model_
->bookmark_bar_node();
576 const base::string16
title(ASCIIToUTF16("foo"));
577 const GURL
url("http://foo.com");
578 model_
->AddURL(root
, 0, title
, url
);
581 model_
->Remove(root
->GetChild(0));
582 ASSERT_EQ(0, root
->child_count());
583 AssertObserverCount(0, 0, 1, 0, 0, 1, 0, 0, 0);
584 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
586 // Make sure there is no mapping for the URL.
587 ASSERT_TRUE(model_
->GetMostRecentlyAddedUserNodeForURL(url
) == NULL
);
590 TEST_F(BookmarkModelTest
, RemoveFolder
) {
591 const BookmarkNode
* root
= model_
->bookmark_bar_node();
592 const BookmarkNode
* folder
= model_
->AddFolder(root
, 0, ASCIIToUTF16("foo"));
596 // Add a URL as a child.
597 const base::string16
title(ASCIIToUTF16("foo"));
598 const GURL
url("http://foo.com");
599 model_
->AddURL(folder
, 0, title
, url
);
603 // Now remove the folder.
604 model_
->Remove(root
->GetChild(0));
605 ASSERT_EQ(0, root
->child_count());
606 AssertObserverCount(0, 0, 1, 0, 0, 1, 0, 0, 0);
607 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
609 // Make sure there is no mapping for the URL.
610 ASSERT_TRUE(model_
->GetMostRecentlyAddedUserNodeForURL(url
) == NULL
);
613 TEST_F(BookmarkModelTest
, RemoveAllUserBookmarks
) {
614 const BookmarkNode
* bookmark_bar_node
= model_
->bookmark_bar_node();
618 // Add a url to bookmark bar.
619 base::string16
title(ASCIIToUTF16("foo"));
620 GURL
url("http://foo.com");
621 model_
->AddURL(bookmark_bar_node
, 0, title
, url
);
623 // Add a folder with child URL.
624 const BookmarkNode
* folder
= model_
->AddFolder(bookmark_bar_node
, 0, title
);
625 model_
->AddURL(folder
, 0, title
, url
);
627 AssertObserverCount(3, 0, 0, 0, 0, 0, 0, 0, 0);
630 model_
->RemoveAllUserBookmarks();
632 EXPECT_EQ(0, bookmark_bar_node
->child_count());
633 // No individual BookmarkNodeRemoved events are fired, so removed count
635 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 1);
636 AssertExtensiveChangesObserverCount(1, 1);
637 EXPECT_EQ(1, AllNodesRemovedObserverCount());
640 TEST_F(BookmarkModelTest
, SetTitle
) {
641 const BookmarkNode
* root
= model_
->bookmark_bar_node();
642 base::string16
title(ASCIIToUTF16("foo"));
643 const GURL
url("http://foo.com");
644 const BookmarkNode
* node
= model_
->AddURL(root
, 0, title
, url
);
648 title
= ASCIIToUTF16("foo2");
649 model_
->SetTitle(node
, title
);
650 AssertObserverCount(0, 0, 0, 1, 0, 0, 1, 0, 0);
651 observer_details_
.ExpectEquals(node
, NULL
, -1, -1);
652 EXPECT_EQ(title
, node
->GetTitle());
655 TEST_F(BookmarkModelTest
, SetTitleWithWhitespace
) {
656 for (size_t i
= 0; i
< arraysize(title_whitespace_test_cases
); ++i
) {
657 const BookmarkNode
* root
= model_
->bookmark_bar_node();
658 base::string16
title(ASCIIToUTF16("dummy"));
659 const GURL
url("http://foo.com");
660 const BookmarkNode
* node
= model_
->AddURL(root
, 0, title
, url
);
662 title
= ASCIIToUTF16(title_whitespace_test_cases
[i
].input_title
);
663 model_
->SetTitle(node
, title
);
664 EXPECT_EQ(ASCIIToUTF16(title_whitespace_test_cases
[i
].expected_title
),
669 TEST_F(BookmarkModelTest
, SetURL
) {
670 const BookmarkNode
* root
= model_
->bookmark_bar_node();
671 const base::string16
title(ASCIIToUTF16("foo"));
672 GURL
url("http://foo.com");
673 const BookmarkNode
* node
= model_
->AddURL(root
, 0, title
, url
);
677 url
= GURL("http://foo2.com");
678 model_
->SetURL(node
, url
);
679 AssertObserverCount(0, 0, 0, 1, 0, 0, 1, 0, 0);
680 observer_details_
.ExpectEquals(node
, NULL
, -1, -1);
681 EXPECT_EQ(url
, node
->url());
684 TEST_F(BookmarkModelTest
, SetDateAdded
) {
685 const BookmarkNode
* root
= model_
->bookmark_bar_node();
686 const base::string16
title(ASCIIToUTF16("foo"));
687 GURL
url("http://foo.com");
688 const BookmarkNode
* node
= model_
->AddURL(root
, 0, title
, url
);
692 base::Time new_time
= base::Time::Now() + base::TimeDelta::FromMinutes(20);
693 model_
->SetDateAdded(node
, new_time
);
694 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0);
695 EXPECT_EQ(new_time
, node
->date_added());
696 EXPECT_EQ(new_time
, model_
->bookmark_bar_node()->date_folder_modified());
699 TEST_F(BookmarkModelTest
, Move
) {
700 const BookmarkNode
* root
= model_
->bookmark_bar_node();
701 const base::string16
title(ASCIIToUTF16("foo"));
702 const GURL
url("http://foo.com");
703 const BookmarkNode
* node
= model_
->AddURL(root
, 0, title
, url
);
704 const BookmarkNode
* folder1
= model_
->AddFolder(root
, 0, ASCIIToUTF16("foo"));
707 model_
->Move(node
, folder1
, 0);
709 AssertObserverCount(0, 1, 0, 0, 0, 0, 0, 0, 0);
710 observer_details_
.ExpectEquals(root
, folder1
, 1, 0);
711 EXPECT_TRUE(folder1
== node
->parent());
712 EXPECT_EQ(1, root
->child_count());
713 EXPECT_EQ(folder1
, root
->GetChild(0));
714 EXPECT_EQ(1, folder1
->child_count());
715 EXPECT_EQ(node
, folder1
->GetChild(0));
717 // And remove the folder.
719 model_
->Remove(root
->GetChild(0));
720 AssertObserverCount(0, 0, 1, 0, 0, 1, 0, 0, 0);
721 observer_details_
.ExpectEquals(root
, NULL
, 0, -1);
722 EXPECT_TRUE(model_
->GetMostRecentlyAddedUserNodeForURL(url
) == NULL
);
723 EXPECT_EQ(0, root
->child_count());
726 TEST_F(BookmarkModelTest
, NonMovingMoveCall
) {
727 const BookmarkNode
* root
= model_
->bookmark_bar_node();
728 const base::string16
title(ASCIIToUTF16("foo"));
729 const GURL
url("http://foo.com");
730 const base::Time
old_date(base::Time::Now() - base::TimeDelta::FromDays(1));
732 const BookmarkNode
* node
= model_
->AddURL(root
, 0, title
, url
);
733 model_
->SetDateFolderModified(root
, old_date
);
735 // Since |node| is already at the index 0 of |root|, this is no-op.
736 model_
->Move(node
, root
, 0);
738 // Check that the modification date is kept untouched.
739 EXPECT_EQ(old_date
, root
->date_folder_modified());
742 TEST_F(BookmarkModelTest
, Copy
) {
743 const BookmarkNode
* root
= model_
->bookmark_bar_node();
744 static const std::string
model_string("a 1:[ b c ] d 2:[ e f g ] h ");
745 test::AddNodesFromModelString(model_
.get(), root
, model_string
);
747 // Validate initial model.
748 std::string actual_model_string
= test::ModelStringFromNode(root
);
749 EXPECT_EQ(model_string
, actual_model_string
);
751 // Copy 'd' to be after '1:b': URL item from bar to folder.
752 const BookmarkNode
* node_to_copy
= root
->GetChild(2);
753 const BookmarkNode
* destination
= root
->GetChild(1);
754 model_
->Copy(node_to_copy
, destination
, 1);
755 actual_model_string
= test::ModelStringFromNode(root
);
756 EXPECT_EQ("a 1:[ b d c ] d 2:[ e f g ] h ", actual_model_string
);
758 // Copy '1:d' to be after 'a': URL item from folder to bar.
759 const BookmarkNode
* folder
= root
->GetChild(1);
760 node_to_copy
= folder
->GetChild(1);
761 model_
->Copy(node_to_copy
, root
, 1);
762 actual_model_string
= test::ModelStringFromNode(root
);
763 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e f g ] h ", actual_model_string
);
765 // Copy '1' to be after '2:e': Folder from bar to folder.
766 node_to_copy
= root
->GetChild(2);
767 destination
= root
->GetChild(4);
768 model_
->Copy(node_to_copy
, destination
, 1);
769 actual_model_string
= test::ModelStringFromNode(root
);
770 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f g ] h ",
771 actual_model_string
);
773 // Copy '2:1' to be after '2:f': Folder within same folder.
774 folder
= root
->GetChild(4);
775 node_to_copy
= folder
->GetChild(1);
776 model_
->Copy(node_to_copy
, folder
, 3);
777 actual_model_string
= test::ModelStringFromNode(root
);
778 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h ",
779 actual_model_string
);
781 // Copy first 'd' to be after 'h': URL item within the bar.
782 node_to_copy
= root
->GetChild(1);
783 model_
->Copy(node_to_copy
, root
, 6);
784 actual_model_string
= test::ModelStringFromNode(root
);
785 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h d ",
786 actual_model_string
);
788 // Copy '2' to be after 'a': Folder within the bar.
789 node_to_copy
= root
->GetChild(4);
790 model_
->Copy(node_to_copy
, root
, 1);
791 actual_model_string
= test::ModelStringFromNode(root
);
792 EXPECT_EQ("a 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] d 1:[ b d c ] "
793 "d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h d ",
794 actual_model_string
);
797 // Tests that adding a URL to a folder updates the last modified time.
798 TEST_F(BookmarkModelTest
, ParentForNewNodes
) {
799 ASSERT_EQ(model_
->bookmark_bar_node(), model_
->GetParentForNewNodes());
801 const base::string16
title(ASCIIToUTF16("foo"));
802 const GURL
url("http://foo.com");
804 model_
->AddURL(model_
->other_node(), 0, title
, url
);
805 ASSERT_EQ(model_
->other_node(), model_
->GetParentForNewNodes());
808 // Tests that adding a URL to a folder updates the last modified time.
809 TEST_F(BookmarkModelTest
, ParentForNewMobileNodes
) {
810 ASSERT_EQ(model_
->bookmark_bar_node(), model_
->GetParentForNewNodes());
812 const base::string16
title(ASCIIToUTF16("foo"));
813 const GURL
url("http://foo.com");
815 model_
->AddURL(model_
->mobile_node(), 0, title
, url
);
816 ASSERT_EQ(model_
->mobile_node(), model_
->GetParentForNewNodes());
819 // Make sure recently modified stays in sync when adding a URL.
820 TEST_F(BookmarkModelTest
, MostRecentlyModifiedFolders
) {
822 const BookmarkNode
* folder
=
823 model_
->AddFolder(model_
->other_node(), 0, ASCIIToUTF16("foo"));
825 model_
->AddURL(folder
, 0, ASCIIToUTF16("blah"), GURL("http://foo.com"));
827 // Make sure folder is in the most recently modified.
828 std::vector
<const BookmarkNode
*> most_recent_folders
=
829 GetMostRecentlyModifiedUserFolders(model_
.get(), 1);
830 ASSERT_EQ(1U, most_recent_folders
.size());
831 ASSERT_EQ(folder
, most_recent_folders
[0]);
833 // Nuke the folder and do another fetch, making sure folder isn't in the
835 model_
->Remove(folder
->parent()->GetChild(0));
836 most_recent_folders
= GetMostRecentlyModifiedUserFolders(model_
.get(), 1);
837 ASSERT_EQ(1U, most_recent_folders
.size());
838 ASSERT_TRUE(most_recent_folders
[0] != folder
);
841 // Make sure MostRecentlyAddedEntries stays in sync.
842 TEST_F(BookmarkModelTest
, MostRecentlyAddedEntries
) {
843 // Add a couple of nodes such that the following holds for the time of the
844 // nodes: n1 > n2 > n3 > n4.
845 Time base_time
= Time::Now();
846 BookmarkNode
* n1
= AsMutable(model_
->AddURL(model_
->bookmark_bar_node(),
848 ASCIIToUTF16("blah"),
849 GURL("http://foo.com/0")));
850 BookmarkNode
* n2
= AsMutable(model_
->AddURL(model_
->bookmark_bar_node(),
852 ASCIIToUTF16("blah"),
853 GURL("http://foo.com/1")));
854 BookmarkNode
* n3
= AsMutable(model_
->AddURL(model_
->bookmark_bar_node(),
856 ASCIIToUTF16("blah"),
857 GURL("http://foo.com/2")));
858 BookmarkNode
* n4
= AsMutable(model_
->AddURL(model_
->bookmark_bar_node(),
860 ASCIIToUTF16("blah"),
861 GURL("http://foo.com/3")));
862 n1
->set_date_added(base_time
+ TimeDelta::FromDays(4));
863 n2
->set_date_added(base_time
+ TimeDelta::FromDays(3));
864 n3
->set_date_added(base_time
+ TimeDelta::FromDays(2));
865 n4
->set_date_added(base_time
+ TimeDelta::FromDays(1));
867 // Make sure order is honored.
868 std::vector
<const BookmarkNode
*> recently_added
;
869 GetMostRecentlyAddedEntries(model_
.get(), 2, &recently_added
);
870 ASSERT_EQ(2U, recently_added
.size());
871 ASSERT_TRUE(n1
== recently_added
[0]);
872 ASSERT_TRUE(n2
== recently_added
[1]);
874 // swap 1 and 2, then check again.
875 recently_added
.clear();
876 SwapDateAdded(n1
, n2
);
877 GetMostRecentlyAddedEntries(model_
.get(), 4, &recently_added
);
878 ASSERT_EQ(4U, recently_added
.size());
879 ASSERT_TRUE(n2
== recently_added
[0]);
880 ASSERT_TRUE(n1
== recently_added
[1]);
881 ASSERT_TRUE(n3
== recently_added
[2]);
882 ASSERT_TRUE(n4
== recently_added
[3]);
885 // Makes sure GetMostRecentlyAddedUserNodeForURL stays in sync.
886 TEST_F(BookmarkModelTest
, GetMostRecentlyAddedUserNodeForURL
) {
887 // Add a couple of nodes such that the following holds for the time of the
889 Time base_time
= Time::Now();
890 const GURL
url("http://foo.com/0");
891 BookmarkNode
* n1
= AsMutable(model_
->AddURL(
892 model_
->bookmark_bar_node(), 0, ASCIIToUTF16("blah"), url
));
893 BookmarkNode
* n2
= AsMutable(model_
->AddURL(
894 model_
->bookmark_bar_node(), 1, ASCIIToUTF16("blah"), url
));
895 n1
->set_date_added(base_time
+ TimeDelta::FromDays(4));
896 n2
->set_date_added(base_time
+ TimeDelta::FromDays(3));
898 // Make sure order is honored.
899 ASSERT_EQ(n1
, model_
->GetMostRecentlyAddedUserNodeForURL(url
));
901 // swap 1 and 2, then check again.
902 SwapDateAdded(n1
, n2
);
903 ASSERT_EQ(n2
, model_
->GetMostRecentlyAddedUserNodeForURL(url
));
906 // Makes sure GetBookmarks removes duplicates.
907 TEST_F(BookmarkModelTest
, GetBookmarksWithDups
) {
908 const GURL
url("http://foo.com/0");
909 const base::string16
title(ASCIIToUTF16("blah"));
910 model_
->AddURL(model_
->bookmark_bar_node(), 0, title
, url
);
911 model_
->AddURL(model_
->bookmark_bar_node(), 1, title
, url
);
913 std::vector
<BookmarkModel::URLAndTitle
> bookmarks
;
914 model_
->GetBookmarks(&bookmarks
);
915 ASSERT_EQ(1U, bookmarks
.size());
916 EXPECT_EQ(url
, bookmarks
[0].url
);
917 EXPECT_EQ(title
, bookmarks
[0].title
);
919 model_
->AddURL(model_
->bookmark_bar_node(), 2, ASCIIToUTF16("Title2"), url
);
920 // Only one returned, even titles are different.
922 model_
->GetBookmarks(&bookmarks
);
923 EXPECT_EQ(1U, bookmarks
.size());
926 TEST_F(BookmarkModelTest
, HasBookmarks
) {
927 const GURL
url("http://foo.com/");
928 model_
->AddURL(model_
->bookmark_bar_node(), 0, ASCIIToUTF16("bar"), url
);
930 EXPECT_TRUE(model_
->HasBookmarks());
933 // http://crbug.com/450464
934 TEST_F(BookmarkModelTest
, DISABLED_Sort
) {
935 // Populate the bookmark bar node with nodes for 'B', 'a', 'd' and 'C'.
936 // 'C' and 'a' are folders.
938 PopulateNodeFromString("B [ a ] d [ a ]", &bbn
);
939 const BookmarkNode
* parent
= model_
->bookmark_bar_node();
940 PopulateBookmarkNode(&bbn
, model_
.get(), parent
);
942 BookmarkNode
* child1
= AsMutable(parent
->GetChild(1));
943 child1
->SetTitle(ASCIIToUTF16("a"));
944 delete child1
->Remove(child1
->GetChild(0));
945 BookmarkNode
* child3
= AsMutable(parent
->GetChild(3));
946 child3
->SetTitle(ASCIIToUTF16("C"));
947 delete child3
->Remove(child3
->GetChild(0));
951 // Sort the children of the bookmark bar node.
952 model_
->SortChildren(parent
);
954 // Make sure we were notified.
955 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 1, 0);
957 // Make sure the order matches (remember, 'a' and 'C' are folders and
959 EXPECT_EQ(parent
->GetChild(0)->GetTitle(), ASCIIToUTF16("a"));
960 EXPECT_EQ(parent
->GetChild(1)->GetTitle(), ASCIIToUTF16("C"));
961 EXPECT_EQ(parent
->GetChild(2)->GetTitle(), ASCIIToUTF16("B"));
962 EXPECT_EQ(parent
->GetChild(3)->GetTitle(), ASCIIToUTF16("d"));
965 TEST_F(BookmarkModelTest
, Reorder
) {
966 // Populate the bookmark bar node with nodes 'A', 'B', 'C' and 'D'.
968 PopulateNodeFromString("A B C D", &bbn
);
969 BookmarkNode
* parent
= AsMutable(model_
->bookmark_bar_node());
970 PopulateBookmarkNode(&bbn
, model_
.get(), parent
);
974 // Reorder bar node's bookmarks in reverse order.
975 std::vector
<const BookmarkNode
*> new_order
;
976 new_order
.push_back(parent
->GetChild(3));
977 new_order
.push_back(parent
->GetChild(2));
978 new_order
.push_back(parent
->GetChild(1));
979 new_order
.push_back(parent
->GetChild(0));
980 model_
->ReorderChildren(parent
, new_order
);
982 // Make sure we were notified.
983 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 1, 0);
985 // Make sure the order matches is correct (it should be reversed).
986 ASSERT_EQ(4, parent
->child_count());
987 EXPECT_EQ("D", base::UTF16ToASCII(parent
->GetChild(0)->GetTitle()));
988 EXPECT_EQ("C", base::UTF16ToASCII(parent
->GetChild(1)->GetTitle()));
989 EXPECT_EQ("B", base::UTF16ToASCII(parent
->GetChild(2)->GetTitle()));
990 EXPECT_EQ("A", base::UTF16ToASCII(parent
->GetChild(3)->GetTitle()));
993 TEST_F(BookmarkModelTest
, NodeVisibility
) {
994 // Mobile node invisible by default
995 EXPECT_TRUE(model_
->bookmark_bar_node()->IsVisible());
996 EXPECT_TRUE(model_
->other_node()->IsVisible());
997 EXPECT_FALSE(model_
->mobile_node()->IsVisible());
999 // Visibility of permanent node can only be changed if they are not
1000 // forced to be visible by the client.
1001 model_
->SetPermanentNodeVisible(BookmarkNode::BOOKMARK_BAR
, false);
1002 EXPECT_TRUE(model_
->bookmark_bar_node()->IsVisible());
1003 model_
->SetPermanentNodeVisible(BookmarkNode::OTHER_NODE
, false);
1004 EXPECT_TRUE(model_
->other_node()->IsVisible());
1005 model_
->SetPermanentNodeVisible(BookmarkNode::MOBILE
, true);
1006 EXPECT_TRUE(model_
->mobile_node()->IsVisible());
1007 model_
->SetPermanentNodeVisible(BookmarkNode::MOBILE
, false);
1008 EXPECT_FALSE(model_
->mobile_node()->IsVisible());
1010 // Arbitrary node should be visible
1012 PopulateNodeFromString("B", &bbn
);
1013 const BookmarkNode
* parent
= model_
->mobile_node();
1014 PopulateBookmarkNode(&bbn
, model_
.get(), parent
);
1015 EXPECT_TRUE(parent
->GetChild(0)->IsVisible());
1017 // Mobile folder should be visible now that it has a child.
1018 EXPECT_TRUE(model_
->mobile_node()->IsVisible());
1021 TEST_F(BookmarkModelTest
, MobileNodeVisibileWithChildren
) {
1022 const BookmarkNode
* root
= model_
->mobile_node();
1023 const base::string16
title(ASCIIToUTF16("foo"));
1024 const GURL
url("http://foo.com");
1026 model_
->AddURL(root
, 0, title
, url
);
1027 EXPECT_TRUE(model_
->mobile_node()->IsVisible());
1030 TEST_F(BookmarkModelTest
, ExtensiveChangesObserver
) {
1031 AssertExtensiveChangesObserverCount(0, 0);
1032 EXPECT_FALSE(model_
->IsDoingExtensiveChanges());
1033 model_
->BeginExtensiveChanges();
1034 EXPECT_TRUE(model_
->IsDoingExtensiveChanges());
1035 AssertExtensiveChangesObserverCount(1, 0);
1036 model_
->EndExtensiveChanges();
1037 EXPECT_FALSE(model_
->IsDoingExtensiveChanges());
1038 AssertExtensiveChangesObserverCount(1, 1);
1041 TEST_F(BookmarkModelTest
, MultipleExtensiveChangesObserver
) {
1042 AssertExtensiveChangesObserverCount(0, 0);
1043 EXPECT_FALSE(model_
->IsDoingExtensiveChanges());
1044 model_
->BeginExtensiveChanges();
1045 EXPECT_TRUE(model_
->IsDoingExtensiveChanges());
1046 AssertExtensiveChangesObserverCount(1, 0);
1047 model_
->BeginExtensiveChanges();
1048 EXPECT_TRUE(model_
->IsDoingExtensiveChanges());
1049 AssertExtensiveChangesObserverCount(1, 0);
1050 model_
->EndExtensiveChanges();
1051 EXPECT_TRUE(model_
->IsDoingExtensiveChanges());
1052 AssertExtensiveChangesObserverCount(1, 0);
1053 model_
->EndExtensiveChanges();
1054 EXPECT_FALSE(model_
->IsDoingExtensiveChanges());
1055 AssertExtensiveChangesObserverCount(1, 1);
1058 // Verifies that IsBookmarked is true if any bookmark matches the given URL,
1059 // and that IsBookmarkedByUser is true only if at least one of the matching
1060 // bookmarks can be edited by the user.
1061 TEST_F(BookmarkModelTest
, IsBookmarked
) {
1062 // Reload the model with an extra node that is not editable by the user.
1063 BookmarkPermanentNode
* extra_node
= ReloadModelWithExtraNode();
1065 // "google.com" is a "user" bookmark.
1066 model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("User"),
1067 GURL("http://google.com"));
1068 // "youtube.com" is not.
1069 model_
->AddURL(extra_node
, 0, base::ASCIIToUTF16("Extra"),
1070 GURL("http://youtube.com"));
1072 EXPECT_TRUE(model_
->IsBookmarked(GURL("http://google.com")));
1073 EXPECT_TRUE(model_
->IsBookmarked(GURL("http://youtube.com")));
1074 EXPECT_FALSE(model_
->IsBookmarked(GURL("http://reddit.com")));
1076 EXPECT_TRUE(IsBookmarkedByUser(model_
.get(), GURL("http://google.com")));
1077 EXPECT_FALSE(IsBookmarkedByUser(model_
.get(), GURL("http://youtube.com")));
1078 EXPECT_FALSE(IsBookmarkedByUser(model_
.get(), GURL("http://reddit.com")));
1081 // Verifies that GetMostRecentlyAddedUserNodeForURL skips bookmarks that
1082 // are not owned by the user.
1083 TEST_F(BookmarkModelTest
, GetMostRecentlyAddedUserNodeForURLSkipsManagedNodes
) {
1084 // Reload the model with an extra node that is not editable by the user.
1085 BookmarkPermanentNode
* extra_node
= ReloadModelWithExtraNode();
1087 const base::string16 title
= base::ASCIIToUTF16("Title");
1088 const BookmarkNode
* user_parent
= model_
->other_node();
1089 const BookmarkNode
* managed_parent
= extra_node
;
1090 const GURL
url("http://google.com");
1092 // |url| is not bookmarked yet.
1093 EXPECT_TRUE(model_
->GetMostRecentlyAddedUserNodeForURL(url
) == NULL
);
1095 // Having a managed node doesn't count.
1096 model_
->AddURL(managed_parent
, 0, title
, url
);
1097 EXPECT_TRUE(model_
->GetMostRecentlyAddedUserNodeForURL(url
) == NULL
);
1099 // Now add a user node.
1100 const BookmarkNode
* user
= model_
->AddURL(user_parent
, 0, title
, url
);
1101 EXPECT_EQ(user
, model_
->GetMostRecentlyAddedUserNodeForURL(url
));
1103 // Having a more recent managed node doesn't count either.
1104 const BookmarkNode
* managed
= model_
->AddURL(managed_parent
, 0, title
, url
);
1105 EXPECT_GE(managed
->date_added(), user
->date_added());
1106 EXPECT_EQ(user
, model_
->GetMostRecentlyAddedUserNodeForURL(url
));
1109 TEST(BookmarkNodeTest
, NodeMetaInfo
) {
1111 BookmarkNode
node(url
);
1112 EXPECT_FALSE(node
.GetMetaInfoMap());
1114 EXPECT_TRUE(node
.SetMetaInfo("key1", "value1"));
1115 std::string out_value
;
1116 EXPECT_TRUE(node
.GetMetaInfo("key1", &out_value
));
1117 EXPECT_EQ("value1", out_value
);
1118 EXPECT_FALSE(node
.SetMetaInfo("key1", "value1"));
1120 EXPECT_FALSE(node
.GetMetaInfo("key2.subkey1", &out_value
));
1121 EXPECT_TRUE(node
.SetMetaInfo("key2.subkey1", "value2"));
1122 EXPECT_TRUE(node
.GetMetaInfo("key2.subkey1", &out_value
));
1123 EXPECT_EQ("value2", out_value
);
1125 EXPECT_FALSE(node
.GetMetaInfo("key2.subkey2.leaf", &out_value
));
1126 EXPECT_TRUE(node
.SetMetaInfo("key2.subkey2.leaf", ""));
1127 EXPECT_TRUE(node
.GetMetaInfo("key2.subkey2.leaf", &out_value
));
1128 EXPECT_EQ("", out_value
);
1130 EXPECT_TRUE(node
.DeleteMetaInfo("key1"));
1131 EXPECT_TRUE(node
.DeleteMetaInfo("key2.subkey1"));
1132 EXPECT_TRUE(node
.DeleteMetaInfo("key2.subkey2.leaf"));
1133 EXPECT_FALSE(node
.DeleteMetaInfo("key3"));
1134 EXPECT_FALSE(node
.GetMetaInfo("key1", &out_value
));
1135 EXPECT_FALSE(node
.GetMetaInfo("key2.subkey1", &out_value
));
1136 EXPECT_FALSE(node
.GetMetaInfo("key2.subkey2", &out_value
));
1137 EXPECT_FALSE(node
.GetMetaInfo("key2.subkey2.leaf", &out_value
));
1138 EXPECT_FALSE(node
.GetMetaInfoMap());
1141 // Creates a set of nodes in the bookmark model, and checks that the loaded
1142 // structure is what we first created.
1143 TEST(BookmarkModelTest2
, CreateAndRestore
) {
1145 // Structure of the children of the bookmark model node.
1146 const std::string bbn_contents
;
1147 // Structure of the children of the other node.
1148 const std::string other_contents
;
1149 // Structure of the children of the synced node.
1150 const std::string mobile_contents
;
1152 // See PopulateNodeFromString for a description of these strings.
1156 { "", "[ b ] a [ c [ d e [ f ] ] ]" },
1158 { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"},
1160 TestBookmarkClient client
;
1161 scoped_ptr
<BookmarkModel
> model
;
1162 for (size_t i
= 0; i
< arraysize(data
); ++i
) {
1163 model
= client
.CreateModel();
1166 PopulateNodeFromString(data
[i
].bbn_contents
, &bbn
);
1167 PopulateBookmarkNode(&bbn
, model
.get(), model
->bookmark_bar_node());
1170 PopulateNodeFromString(data
[i
].other_contents
, &other
);
1171 PopulateBookmarkNode(&other
, model
.get(), model
->other_node());
1174 PopulateNodeFromString(data
[i
].mobile_contents
, &mobile
);
1175 PopulateBookmarkNode(&mobile
, model
.get(), model
->mobile_node());
1177 VerifyModelMatchesNode(&bbn
, model
->bookmark_bar_node());
1178 VerifyModelMatchesNode(&other
, model
->other_node());
1179 VerifyModelMatchesNode(&mobile
, model
->mobile_node());
1180 VerifyNoDuplicateIDs(model
.get());
1185 } // namespace bookmarks