Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_model_unittest.cc
blobac22fbf35892ef6a175ebe61cd0e9a1b23f0eb38
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"
7 #include <set>
8 #include <string>
10 #include "base/base_paths.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_split.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h"
20 #include "components/bookmarks/browser/bookmark_model_observer.h"
21 #include "components/bookmarks/browser/bookmark_utils.h"
22 #include "components/bookmarks/test/bookmark_test_helpers.h"
23 #include "components/bookmarks/test/test_bookmark_client.h"
24 #include "components/favicon_base/favicon_callback.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/skia/include/core/SkBitmap.h"
27 #include "ui/base/models/tree_node_iterator.h"
28 #include "ui/base/models/tree_node_model.h"
29 #include "ui/gfx/image/image.h"
30 #include "url/gurl.h"
32 using base::ASCIIToUTF16;
33 using base::Time;
34 using base::TimeDelta;
36 namespace bookmarks {
37 namespace {
39 // Test cases used to test the removal of extra whitespace when adding
40 // a new folder/bookmark or updating a title of a folder/bookmark.
41 static struct {
42 const std::string input_title;
43 const std::string expected_title;
44 } url_whitespace_test_cases[] = {
45 {"foobar", "foobar"},
46 // Newlines.
47 {"foo\nbar", "foo bar"},
48 {"foo\n\nbar", "foo bar"},
49 {"foo\n\n\nbar", "foo bar"},
50 {"foo\r\nbar", "foo bar"},
51 {"foo\r\n\r\nbar", "foo bar"},
52 {"\nfoo\nbar\n", "foo bar"},
53 // Spaces.
54 {"foo bar", "foo bar"},
55 {" foo bar ", "foo bar"},
56 {" foo bar ", "foo bar"},
57 // Tabs.
58 {"\tfoo\tbar\t", "foo bar"},
59 {"\tfoo bar\t", "foo bar"},
60 // Mixed cases.
61 {"\tfoo\nbar\t", "foo bar"},
62 {"\tfoo\r\nbar\t", "foo bar"},
63 {" foo\tbar\n", "foo bar"},
64 {"\t foo \t bar \t", "foo bar"},
65 {"\n foo\r\n\tbar\n \t", "foo bar"},
68 // Test cases used to test the removal of extra whitespace when adding
69 // a new folder/bookmark or updating a title of a folder/bookmark.
70 static struct {
71 const std::string input_title;
72 const std::string expected_title;
73 } title_whitespace_test_cases[] = {
74 {"foobar", "foobar"},
75 // Newlines.
76 {"foo\nbar", "foo bar"},
77 {"foo\n\nbar", "foo bar"},
78 {"foo\n\n\nbar", "foo bar"},
79 {"foo\r\nbar", "foo bar"},
80 {"foo\r\n\r\nbar", "foo bar"},
81 {"\nfoo\nbar\n", " foo bar "},
82 // Spaces.
83 {"foo bar", "foo bar"},
84 {" foo bar ", " foo bar "},
85 {" foo bar ", " foo bar "},
86 // Tabs.
87 {"\tfoo\tbar\t", " foo bar "},
88 {"\tfoo bar\t", " foo bar "},
89 // Mixed cases.
90 {"\tfoo\nbar\t", " foo bar "},
91 {"\tfoo\r\nbar\t", " foo bar "},
92 {" foo\tbar\n", " foo bar "},
93 {"\t foo \t bar \t", " foo bar "},
94 {"\n foo\r\n\tbar\n \t", " foo bar "},
97 // Helper to get a mutable bookmark node.
98 BookmarkNode* AsMutable(const BookmarkNode* node) {
99 return const_cast<BookmarkNode*>(node);
102 void SwapDateAdded(BookmarkNode* n1, BookmarkNode* n2) {
103 Time tmp = n1->date_added();
104 n1->set_date_added(n2->date_added());
105 n2->set_date_added(tmp);
108 // See comment in PopulateNodeFromString.
109 using TestNode = ui::TreeNodeWithValue<BookmarkNode::Type>;
111 // Does the work of PopulateNodeFromString. index gives the index of the current
112 // element in description to process.
113 void PopulateNodeImpl(const std::vector<std::string>& description,
114 size_t* index,
115 TestNode* parent) {
116 while (*index < description.size()) {
117 const std::string& element = description[*index];
118 (*index)++;
119 if (element == "[") {
120 // Create a new folder and recurse to add all the children.
121 // Folders are given a unique named by way of an ever increasing integer
122 // value. The folders need not have a name, but one is assigned to help
123 // in debugging.
124 static int next_folder_id = 1;
125 TestNode* new_node = new TestNode(base::IntToString16(next_folder_id++),
126 BookmarkNode::FOLDER);
127 parent->Add(new_node, parent->child_count());
128 PopulateNodeImpl(description, index, new_node);
129 } else if (element == "]") {
130 // End the current folder.
131 return;
132 } else {
133 // Add a new URL.
135 // All tokens must be space separated. If there is a [ or ] in the name it
136 // likely means a space was forgotten.
137 DCHECK(element.find('[') == std::string::npos);
138 DCHECK(element.find(']') == std::string::npos);
139 parent->Add(new TestNode(base::UTF8ToUTF16(element), BookmarkNode::URL),
140 parent->child_count());
145 // Creates and adds nodes to parent based on description. description consists
146 // of the following tokens (all space separated):
147 // [ : creates a new USER_FOLDER node. All elements following the [ until the
148 // next balanced ] is encountered are added as children to the node.
149 // ] : closes the last folder created by [ so that any further nodes are added
150 // to the current folders parent.
151 // text: creates a new URL node.
152 // For example, "a [b] c" creates the following nodes:
153 // a 1 c
154 // |
155 // b
156 // In words: a node of type URL with the title a, followed by a folder node with
157 // the title 1 having the single child of type url with name b, followed by
158 // the url node with the title c.
160 // NOTE: each name must be unique, and folders are assigned a unique title by
161 // way of an increasing integer.
162 void PopulateNodeFromString(const std::string& description, TestNode* parent) {
163 std::vector<std::string> elements = base::SplitString(
164 description, base::kWhitespaceASCII,
165 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
166 size_t index = 0;
167 PopulateNodeImpl(elements, &index, parent);
170 // Populates the BookmarkNode with the children of parent.
171 void PopulateBookmarkNode(TestNode* parent,
172 BookmarkModel* model,
173 const BookmarkNode* bb_node) {
174 for (int i = 0; i < parent->child_count(); ++i) {
175 TestNode* child = parent->GetChild(i);
176 if (child->value == BookmarkNode::FOLDER) {
177 const BookmarkNode* new_bb_node =
178 model->AddFolder(bb_node, i, child->GetTitle());
179 PopulateBookmarkNode(child, model, new_bb_node);
180 } else {
181 model->AddURL(bb_node, i, child->GetTitle(),
182 GURL("http://" + base::UTF16ToASCII(child->GetTitle())));
187 // Verifies the contents of the bookmark bar node match the contents of the
188 // TestNode.
189 void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) {
190 ASSERT_EQ(expected->child_count(), actual->child_count());
191 for (int i = 0; i < expected->child_count(); ++i) {
192 TestNode* expected_child = expected->GetChild(i);
193 const BookmarkNode* actual_child = actual->GetChild(i);
194 ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle());
195 if (expected_child->value == BookmarkNode::FOLDER) {
196 ASSERT_TRUE(actual_child->type() == BookmarkNode::FOLDER);
197 // Recurse throught children.
198 VerifyModelMatchesNode(expected_child, actual_child);
199 } else {
200 // No need to check the URL, just the title is enough.
201 ASSERT_TRUE(actual_child->is_url());
206 void VerifyNoDuplicateIDs(BookmarkModel* model) {
207 ui::TreeNodeIterator<const BookmarkNode> it(model->root_node());
208 base::hash_set<int64_t> ids;
209 while (it.has_next())
210 ASSERT_TRUE(ids.insert(it.Next()->id()).second);
213 class BookmarkModelTest : public testing::Test,
214 public BookmarkModelObserver {
215 public:
216 struct ObserverDetails {
217 ObserverDetails() {
218 Set(NULL, NULL, -1, -1);
221 void Set(const BookmarkNode* node1,
222 const BookmarkNode* node2,
223 int index1,
224 int index2) {
225 node1_ = node1;
226 node2_ = node2;
227 index1_ = index1;
228 index2_ = index2;
231 void ExpectEquals(const BookmarkNode* node1,
232 const BookmarkNode* node2,
233 int index1,
234 int index2) {
235 EXPECT_EQ(node1_, node1);
236 EXPECT_EQ(node2_, node2);
237 EXPECT_EQ(index1_, index1);
238 EXPECT_EQ(index2_, index2);
241 private:
242 const BookmarkNode* node1_;
243 const BookmarkNode* node2_;
244 int index1_;
245 int index2_;
248 BookmarkModelTest() : model_(client_.CreateModel()) {
249 model_->AddObserver(this);
250 ClearCounts();
253 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override {
254 // We never load from the db, so that this should never get invoked.
255 NOTREACHED();
258 void BookmarkNodeMoved(BookmarkModel* model,
259 const BookmarkNode* old_parent,
260 int old_index,
261 const BookmarkNode* new_parent,
262 int new_index) override {
263 ++moved_count_;
264 observer_details_.Set(old_parent, new_parent, old_index, new_index);
267 void BookmarkNodeAdded(BookmarkModel* model,
268 const BookmarkNode* parent,
269 int index) override {
270 ++added_count_;
271 observer_details_.Set(parent, NULL, index, -1);
274 void OnWillRemoveBookmarks(BookmarkModel* model,
275 const BookmarkNode* parent,
276 int old_index,
277 const BookmarkNode* node) override {
278 ++before_remove_count_;
281 void BookmarkNodeRemoved(BookmarkModel* model,
282 const BookmarkNode* parent,
283 int old_index,
284 const BookmarkNode* node,
285 const std::set<GURL>& removed_urls) override {
286 ++removed_count_;
287 observer_details_.Set(parent, NULL, old_index, -1);
290 void BookmarkNodeChanged(BookmarkModel* model,
291 const BookmarkNode* node) override {
292 ++changed_count_;
293 observer_details_.Set(node, NULL, -1, -1);
296 void OnWillChangeBookmarkNode(BookmarkModel* model,
297 const BookmarkNode* node) override {
298 ++before_change_count_;
301 void BookmarkNodeChildrenReordered(BookmarkModel* model,
302 const BookmarkNode* node) override {
303 ++reordered_count_;
306 void OnWillReorderBookmarkNode(BookmarkModel* model,
307 const BookmarkNode* node) override {
308 ++before_reorder_count_;
311 void BookmarkNodeFaviconChanged(BookmarkModel* model,
312 const BookmarkNode* node) override {
313 // We never attempt to load favicons, so that this method never
314 // gets invoked.
317 void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) override {
318 ++extensive_changes_beginning_count_;
321 void ExtensiveBookmarkChangesEnded(BookmarkModel* model) override {
322 ++extensive_changes_ended_count_;
325 void BookmarkAllUserNodesRemoved(
326 BookmarkModel* model,
327 const std::set<GURL>& removed_urls) override {
328 ++all_bookmarks_removed_;
331 void OnWillRemoveAllUserBookmarks(BookmarkModel* model) override {
332 ++before_remove_all_count_;
335 void ClearCounts() {
336 added_count_ = moved_count_ = removed_count_ = changed_count_ =
337 reordered_count_ = extensive_changes_beginning_count_ =
338 extensive_changes_ended_count_ = all_bookmarks_removed_ =
339 before_remove_count_ = before_change_count_ = before_reorder_count_ =
340 before_remove_all_count_ = 0;
343 void AssertObserverCount(int added_count,
344 int moved_count,
345 int removed_count,
346 int changed_count,
347 int reordered_count,
348 int before_remove_count,
349 int before_change_count,
350 int before_reorder_count,
351 int before_remove_all_count) {
352 EXPECT_EQ(added_count, added_count_);
353 EXPECT_EQ(moved_count, moved_count_);
354 EXPECT_EQ(removed_count, removed_count_);
355 EXPECT_EQ(changed_count, changed_count_);
356 EXPECT_EQ(reordered_count, reordered_count_);
357 EXPECT_EQ(before_remove_count, before_remove_count_);
358 EXPECT_EQ(before_change_count, before_change_count_);
359 EXPECT_EQ(before_reorder_count, before_reorder_count_);
360 EXPECT_EQ(before_remove_all_count, before_remove_all_count_);
363 void AssertExtensiveChangesObserverCount(
364 int extensive_changes_beginning_count,
365 int extensive_changes_ended_count) {
366 EXPECT_EQ(extensive_changes_beginning_count,
367 extensive_changes_beginning_count_);
368 EXPECT_EQ(extensive_changes_ended_count, extensive_changes_ended_count_);
371 int AllNodesRemovedObserverCount() const { return all_bookmarks_removed_; }
373 BookmarkPermanentNode* ReloadModelWithExtraNode() {
374 BookmarkPermanentNode* extra_node = new BookmarkPermanentNode(100);
375 BookmarkPermanentNodeList extra_nodes;
376 extra_nodes.push_back(extra_node);
377 client_.SetExtraNodesToLoad(extra_nodes.Pass());
379 model_->RemoveObserver(this);
380 model_ = client_.CreateModel();
381 model_->AddObserver(this);
382 ClearCounts();
384 if (model_->root_node()->GetIndexOf(extra_node) == -1)
385 ADD_FAILURE();
387 return extra_node;
390 protected:
391 TestBookmarkClient client_;
392 scoped_ptr<BookmarkModel> model_;
393 ObserverDetails observer_details_;
395 private:
396 int added_count_;
397 int moved_count_;
398 int removed_count_;
399 int changed_count_;
400 int reordered_count_;
401 int extensive_changes_beginning_count_;
402 int extensive_changes_ended_count_;
403 int all_bookmarks_removed_;
404 int before_remove_count_;
405 int before_change_count_;
406 int before_reorder_count_;
407 int before_remove_all_count_;
409 DISALLOW_COPY_AND_ASSIGN(BookmarkModelTest);
412 TEST_F(BookmarkModelTest, InitialState) {
413 const BookmarkNode* bb_node = model_->bookmark_bar_node();
414 ASSERT_TRUE(bb_node != NULL);
415 EXPECT_EQ(0, bb_node->child_count());
416 EXPECT_EQ(BookmarkNode::BOOKMARK_BAR, bb_node->type());
418 const BookmarkNode* other_node = model_->other_node();
419 ASSERT_TRUE(other_node != NULL);
420 EXPECT_EQ(0, other_node->child_count());
421 EXPECT_EQ(BookmarkNode::OTHER_NODE, other_node->type());
423 const BookmarkNode* mobile_node = model_->mobile_node();
424 ASSERT_TRUE(mobile_node != NULL);
425 EXPECT_EQ(0, mobile_node->child_count());
426 EXPECT_EQ(BookmarkNode::MOBILE, mobile_node->type());
428 EXPECT_TRUE(bb_node->id() != other_node->id());
429 EXPECT_TRUE(bb_node->id() != mobile_node->id());
430 EXPECT_TRUE(other_node->id() != mobile_node->id());
433 TEST_F(BookmarkModelTest, AddURL) {
434 const BookmarkNode* root = model_->bookmark_bar_node();
435 const base::string16 title(ASCIIToUTF16("foo"));
436 const GURL url("http://foo.com");
438 const BookmarkNode* new_node = model_->AddURL(root, 0, title, url);
439 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
440 observer_details_.ExpectEquals(root, NULL, 0, -1);
442 ASSERT_EQ(1, root->child_count());
443 ASSERT_EQ(title, new_node->GetTitle());
444 ASSERT_TRUE(url == new_node->url());
445 ASSERT_EQ(BookmarkNode::URL, new_node->type());
446 ASSERT_TRUE(new_node == model_->GetMostRecentlyAddedUserNodeForURL(url));
448 EXPECT_TRUE(new_node->id() != root->id() &&
449 new_node->id() != model_->other_node()->id() &&
450 new_node->id() != model_->mobile_node()->id());
453 TEST_F(BookmarkModelTest, AddURLWithUnicodeTitle) {
454 const BookmarkNode* root = model_->bookmark_bar_node();
455 const base::string16 title(base::WideToUTF16(
456 L"\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053"));
457 const GURL url("https://www.baidu.com/");
459 const BookmarkNode* new_node = model_->AddURL(root, 0, title, url);
460 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
461 observer_details_.ExpectEquals(root, NULL, 0, -1);
463 ASSERT_EQ(1, root->child_count());
464 ASSERT_EQ(title, new_node->GetTitle());
465 ASSERT_TRUE(url == new_node->url());
466 ASSERT_EQ(BookmarkNode::URL, new_node->type());
467 ASSERT_TRUE(new_node == model_->GetMostRecentlyAddedUserNodeForURL(url));
469 EXPECT_TRUE(new_node->id() != root->id() &&
470 new_node->id() != model_->other_node()->id() &&
471 new_node->id() != model_->mobile_node()->id());
474 TEST_F(BookmarkModelTest, AddURLWithWhitespaceTitle) {
475 for (size_t i = 0; i < arraysize(url_whitespace_test_cases); ++i) {
476 const BookmarkNode* root = model_->bookmark_bar_node();
477 const base::string16 title(
478 ASCIIToUTF16(url_whitespace_test_cases[i].input_title));
479 const GURL url("http://foo.com");
481 const BookmarkNode* new_node = model_->AddURL(root, i, title, url);
483 int size = i + 1;
484 EXPECT_EQ(size, root->child_count());
485 EXPECT_EQ(ASCIIToUTF16(url_whitespace_test_cases[i].expected_title),
486 new_node->GetTitle());
487 EXPECT_EQ(BookmarkNode::URL, new_node->type());
491 TEST_F(BookmarkModelTest, AddURLWithCreationTimeAndMetaInfo) {
492 const BookmarkNode* root = model_->bookmark_bar_node();
493 const base::string16 title(ASCIIToUTF16("foo"));
494 const GURL url("http://foo.com");
495 const Time time = Time::Now() - TimeDelta::FromDays(1);
496 BookmarkNode::MetaInfoMap meta_info;
497 meta_info["foo"] = "bar";
499 const BookmarkNode* new_node = model_->AddURLWithCreationTimeAndMetaInfo(
500 root, 0, title, url, time, &meta_info);
501 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
502 observer_details_.ExpectEquals(root, NULL, 0, -1);
504 ASSERT_EQ(1, root->child_count());
505 ASSERT_EQ(title, new_node->GetTitle());
506 ASSERT_TRUE(url == new_node->url());
507 ASSERT_EQ(BookmarkNode::URL, new_node->type());
508 ASSERT_EQ(time, new_node->date_added());
509 ASSERT_TRUE(new_node->GetMetaInfoMap());
510 ASSERT_EQ(meta_info, *new_node->GetMetaInfoMap());
511 ASSERT_TRUE(new_node == model_->GetMostRecentlyAddedUserNodeForURL(url));
513 EXPECT_TRUE(new_node->id() != root->id() &&
514 new_node->id() != model_->other_node()->id() &&
515 new_node->id() != model_->mobile_node()->id());
518 TEST_F(BookmarkModelTest, AddURLToMobileBookmarks) {
519 const BookmarkNode* root = model_->mobile_node();
520 const base::string16 title(ASCIIToUTF16("foo"));
521 const GURL url("http://foo.com");
523 const BookmarkNode* new_node = model_->AddURL(root, 0, title, url);
524 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
525 observer_details_.ExpectEquals(root, NULL, 0, -1);
527 ASSERT_EQ(1, root->child_count());
528 ASSERT_EQ(title, new_node->GetTitle());
529 ASSERT_TRUE(url == new_node->url());
530 ASSERT_EQ(BookmarkNode::URL, new_node->type());
531 ASSERT_TRUE(new_node == model_->GetMostRecentlyAddedUserNodeForURL(url));
533 EXPECT_TRUE(new_node->id() != root->id() &&
534 new_node->id() != model_->other_node()->id() &&
535 new_node->id() != model_->mobile_node()->id());
538 TEST_F(BookmarkModelTest, AddFolder) {
539 const BookmarkNode* root = model_->bookmark_bar_node();
540 const base::string16 title(ASCIIToUTF16("foo"));
542 const BookmarkNode* new_node = model_->AddFolder(root, 0, title);
543 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
544 observer_details_.ExpectEquals(root, NULL, 0, -1);
546 ASSERT_EQ(1, root->child_count());
547 ASSERT_EQ(title, new_node->GetTitle());
548 ASSERT_EQ(BookmarkNode::FOLDER, new_node->type());
550 EXPECT_TRUE(new_node->id() != root->id() &&
551 new_node->id() != model_->other_node()->id() &&
552 new_node->id() != model_->mobile_node()->id());
554 // Add another folder, just to make sure folder_ids are incremented correctly.
555 ClearCounts();
556 model_->AddFolder(root, 0, title);
557 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0);
558 observer_details_.ExpectEquals(root, NULL, 0, -1);
561 TEST_F(BookmarkModelTest, AddFolderWithWhitespaceTitle) {
562 for (size_t i = 0; i < arraysize(title_whitespace_test_cases); ++i) {
563 const BookmarkNode* root = model_->bookmark_bar_node();
564 const base::string16 title(
565 ASCIIToUTF16(title_whitespace_test_cases[i].input_title));
567 const BookmarkNode* new_node = model_->AddFolder(root, i, title);
569 int size = i + 1;
570 EXPECT_EQ(size, root->child_count());
571 EXPECT_EQ(ASCIIToUTF16(title_whitespace_test_cases[i].expected_title),
572 new_node->GetTitle());
573 EXPECT_EQ(BookmarkNode::FOLDER, new_node->type());
577 TEST_F(BookmarkModelTest, RemoveURL) {
578 const BookmarkNode* root = model_->bookmark_bar_node();
579 const base::string16 title(ASCIIToUTF16("foo"));
580 const GURL url("http://foo.com");
581 model_->AddURL(root, 0, title, url);
582 ClearCounts();
584 model_->Remove(root->GetChild(0));
585 ASSERT_EQ(0, root->child_count());
586 AssertObserverCount(0, 0, 1, 0, 0, 1, 0, 0, 0);
587 observer_details_.ExpectEquals(root, NULL, 0, -1);
589 // Make sure there is no mapping for the URL.
590 ASSERT_TRUE(model_->GetMostRecentlyAddedUserNodeForURL(url) == NULL);
593 TEST_F(BookmarkModelTest, RemoveFolder) {
594 const BookmarkNode* root = model_->bookmark_bar_node();
595 const BookmarkNode* folder = model_->AddFolder(root, 0, ASCIIToUTF16("foo"));
597 ClearCounts();
599 // Add a URL as a child.
600 const base::string16 title(ASCIIToUTF16("foo"));
601 const GURL url("http://foo.com");
602 model_->AddURL(folder, 0, title, url);
604 ClearCounts();
606 // Now remove the folder.
607 model_->Remove(root->GetChild(0));
608 ASSERT_EQ(0, root->child_count());
609 AssertObserverCount(0, 0, 1, 0, 0, 1, 0, 0, 0);
610 observer_details_.ExpectEquals(root, NULL, 0, -1);
612 // Make sure there is no mapping for the URL.
613 ASSERT_TRUE(model_->GetMostRecentlyAddedUserNodeForURL(url) == NULL);
616 TEST_F(BookmarkModelTest, RemoveAllUserBookmarks) {
617 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
619 ClearCounts();
621 // Add a url to bookmark bar.
622 base::string16 title(ASCIIToUTF16("foo"));
623 GURL url("http://foo.com");
624 model_->AddURL(bookmark_bar_node, 0, title, url);
626 // Add a folder with child URL.
627 const BookmarkNode* folder = model_->AddFolder(bookmark_bar_node, 0, title);
628 model_->AddURL(folder, 0, title, url);
630 AssertObserverCount(3, 0, 0, 0, 0, 0, 0, 0, 0);
631 ClearCounts();
633 model_->RemoveAllUserBookmarks();
635 EXPECT_EQ(0, bookmark_bar_node->child_count());
636 // No individual BookmarkNodeRemoved events are fired, so removed count
637 // should be 0.
638 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 1);
639 AssertExtensiveChangesObserverCount(1, 1);
640 EXPECT_EQ(1, AllNodesRemovedObserverCount());
643 TEST_F(BookmarkModelTest, SetTitle) {
644 const BookmarkNode* root = model_->bookmark_bar_node();
645 base::string16 title(ASCIIToUTF16("foo"));
646 const GURL url("http://foo.com");
647 const BookmarkNode* node = model_->AddURL(root, 0, title, url);
649 ClearCounts();
651 title = ASCIIToUTF16("foo2");
652 model_->SetTitle(node, title);
653 AssertObserverCount(0, 0, 0, 1, 0, 0, 1, 0, 0);
654 observer_details_.ExpectEquals(node, NULL, -1, -1);
655 EXPECT_EQ(title, node->GetTitle());
658 TEST_F(BookmarkModelTest, SetTitleWithWhitespace) {
659 for (size_t i = 0; i < arraysize(title_whitespace_test_cases); ++i) {
660 const BookmarkNode* root = model_->bookmark_bar_node();
661 base::string16 title(ASCIIToUTF16("dummy"));
662 const GURL url("http://foo.com");
663 const BookmarkNode* node = model_->AddURL(root, 0, title, url);
665 title = ASCIIToUTF16(title_whitespace_test_cases[i].input_title);
666 model_->SetTitle(node, title);
667 EXPECT_EQ(ASCIIToUTF16(title_whitespace_test_cases[i].expected_title),
668 node->GetTitle());
672 TEST_F(BookmarkModelTest, SetURL) {
673 const BookmarkNode* root = model_->bookmark_bar_node();
674 const base::string16 title(ASCIIToUTF16("foo"));
675 GURL url("http://foo.com");
676 const BookmarkNode* node = model_->AddURL(root, 0, title, url);
678 ClearCounts();
680 url = GURL("http://foo2.com");
681 model_->SetURL(node, url);
682 AssertObserverCount(0, 0, 0, 1, 0, 0, 1, 0, 0);
683 observer_details_.ExpectEquals(node, NULL, -1, -1);
684 EXPECT_EQ(url, node->url());
687 TEST_F(BookmarkModelTest, SetDateAdded) {
688 const BookmarkNode* root = model_->bookmark_bar_node();
689 const base::string16 title(ASCIIToUTF16("foo"));
690 GURL url("http://foo.com");
691 const BookmarkNode* node = model_->AddURL(root, 0, title, url);
693 ClearCounts();
695 base::Time new_time = base::Time::Now() + base::TimeDelta::FromMinutes(20);
696 model_->SetDateAdded(node, new_time);
697 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0);
698 EXPECT_EQ(new_time, node->date_added());
699 EXPECT_EQ(new_time, model_->bookmark_bar_node()->date_folder_modified());
702 TEST_F(BookmarkModelTest, Move) {
703 const BookmarkNode* root = model_->bookmark_bar_node();
704 const base::string16 title(ASCIIToUTF16("foo"));
705 const GURL url("http://foo.com");
706 const BookmarkNode* node = model_->AddURL(root, 0, title, url);
707 const BookmarkNode* folder1 = model_->AddFolder(root, 0, ASCIIToUTF16("foo"));
708 ClearCounts();
710 model_->Move(node, folder1, 0);
712 AssertObserverCount(0, 1, 0, 0, 0, 0, 0, 0, 0);
713 observer_details_.ExpectEquals(root, folder1, 1, 0);
714 EXPECT_TRUE(folder1 == node->parent());
715 EXPECT_EQ(1, root->child_count());
716 EXPECT_EQ(folder1, root->GetChild(0));
717 EXPECT_EQ(1, folder1->child_count());
718 EXPECT_EQ(node, folder1->GetChild(0));
720 // And remove the folder.
721 ClearCounts();
722 model_->Remove(root->GetChild(0));
723 AssertObserverCount(0, 0, 1, 0, 0, 1, 0, 0, 0);
724 observer_details_.ExpectEquals(root, NULL, 0, -1);
725 EXPECT_TRUE(model_->GetMostRecentlyAddedUserNodeForURL(url) == NULL);
726 EXPECT_EQ(0, root->child_count());
729 TEST_F(BookmarkModelTest, NonMovingMoveCall) {
730 const BookmarkNode* root = model_->bookmark_bar_node();
731 const base::string16 title(ASCIIToUTF16("foo"));
732 const GURL url("http://foo.com");
733 const base::Time old_date(base::Time::Now() - base::TimeDelta::FromDays(1));
735 const BookmarkNode* node = model_->AddURL(root, 0, title, url);
736 model_->SetDateFolderModified(root, old_date);
738 // Since |node| is already at the index 0 of |root|, this is no-op.
739 model_->Move(node, root, 0);
741 // Check that the modification date is kept untouched.
742 EXPECT_EQ(old_date, root->date_folder_modified());
745 TEST_F(BookmarkModelTest, Copy) {
746 const BookmarkNode* root = model_->bookmark_bar_node();
747 static const std::string model_string("a 1:[ b c ] d 2:[ e f g ] h ");
748 test::AddNodesFromModelString(model_.get(), root, model_string);
750 // Validate initial model.
751 std::string actual_model_string = test::ModelStringFromNode(root);
752 EXPECT_EQ(model_string, actual_model_string);
754 // Copy 'd' to be after '1:b': URL item from bar to folder.
755 const BookmarkNode* node_to_copy = root->GetChild(2);
756 const BookmarkNode* destination = root->GetChild(1);
757 model_->Copy(node_to_copy, destination, 1);
758 actual_model_string = test::ModelStringFromNode(root);
759 EXPECT_EQ("a 1:[ b d c ] d 2:[ e f g ] h ", actual_model_string);
761 // Copy '1:d' to be after 'a': URL item from folder to bar.
762 const BookmarkNode* folder = root->GetChild(1);
763 node_to_copy = folder->GetChild(1);
764 model_->Copy(node_to_copy, root, 1);
765 actual_model_string = test::ModelStringFromNode(root);
766 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e f g ] h ", actual_model_string);
768 // Copy '1' to be after '2:e': Folder from bar to folder.
769 node_to_copy = root->GetChild(2);
770 destination = root->GetChild(4);
771 model_->Copy(node_to_copy, destination, 1);
772 actual_model_string = test::ModelStringFromNode(root);
773 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f g ] h ",
774 actual_model_string);
776 // Copy '2:1' to be after '2:f': Folder within same folder.
777 folder = root->GetChild(4);
778 node_to_copy = folder->GetChild(1);
779 model_->Copy(node_to_copy, folder, 3);
780 actual_model_string = test::ModelStringFromNode(root);
781 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h ",
782 actual_model_string);
784 // Copy first 'd' to be after 'h': URL item within the bar.
785 node_to_copy = root->GetChild(1);
786 model_->Copy(node_to_copy, root, 6);
787 actual_model_string = test::ModelStringFromNode(root);
788 EXPECT_EQ("a d 1:[ b d c ] d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h d ",
789 actual_model_string);
791 // Copy '2' to be after 'a': Folder within the bar.
792 node_to_copy = root->GetChild(4);
793 model_->Copy(node_to_copy, root, 1);
794 actual_model_string = test::ModelStringFromNode(root);
795 EXPECT_EQ("a 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] d 1:[ b d c ] "
796 "d 2:[ e 1:[ b d c ] f 1:[ b d c ] g ] h d ",
797 actual_model_string);
800 // Tests that adding a URL to a folder updates the last modified time.
801 TEST_F(BookmarkModelTest, ParentForNewNodes) {
802 ASSERT_EQ(model_->bookmark_bar_node(), model_->GetParentForNewNodes());
804 const base::string16 title(ASCIIToUTF16("foo"));
805 const GURL url("http://foo.com");
807 model_->AddURL(model_->other_node(), 0, title, url);
808 ASSERT_EQ(model_->other_node(), model_->GetParentForNewNodes());
811 // Tests that adding a URL to a folder updates the last modified time.
812 TEST_F(BookmarkModelTest, ParentForNewMobileNodes) {
813 ASSERT_EQ(model_->bookmark_bar_node(), model_->GetParentForNewNodes());
815 const base::string16 title(ASCIIToUTF16("foo"));
816 const GURL url("http://foo.com");
818 model_->AddURL(model_->mobile_node(), 0, title, url);
819 ASSERT_EQ(model_->mobile_node(), model_->GetParentForNewNodes());
822 // Make sure recently modified stays in sync when adding a URL.
823 TEST_F(BookmarkModelTest, MostRecentlyModifiedFolders) {
824 // Add a folder.
825 const BookmarkNode* folder =
826 model_->AddFolder(model_->other_node(), 0, ASCIIToUTF16("foo"));
827 // Add a URL to it.
828 model_->AddURL(folder, 0, ASCIIToUTF16("blah"), GURL("http://foo.com"));
830 // Make sure folder is in the most recently modified.
831 std::vector<const BookmarkNode*> most_recent_folders =
832 GetMostRecentlyModifiedUserFolders(model_.get(), 1);
833 ASSERT_EQ(1U, most_recent_folders.size());
834 ASSERT_EQ(folder, most_recent_folders[0]);
836 // Nuke the folder and do another fetch, making sure folder isn't in the
837 // returned list.
838 model_->Remove(folder->parent()->GetChild(0));
839 most_recent_folders = GetMostRecentlyModifiedUserFolders(model_.get(), 1);
840 ASSERT_EQ(1U, most_recent_folders.size());
841 ASSERT_TRUE(most_recent_folders[0] != folder);
844 // Make sure MostRecentlyAddedEntries stays in sync.
845 TEST_F(BookmarkModelTest, MostRecentlyAddedEntries) {
846 // Add a couple of nodes such that the following holds for the time of the
847 // nodes: n1 > n2 > n3 > n4.
848 Time base_time = Time::Now();
849 BookmarkNode* n1 = AsMutable(model_->AddURL(model_->bookmark_bar_node(),
851 ASCIIToUTF16("blah"),
852 GURL("http://foo.com/0")));
853 BookmarkNode* n2 = AsMutable(model_->AddURL(model_->bookmark_bar_node(),
855 ASCIIToUTF16("blah"),
856 GURL("http://foo.com/1")));
857 BookmarkNode* n3 = AsMutable(model_->AddURL(model_->bookmark_bar_node(),
859 ASCIIToUTF16("blah"),
860 GURL("http://foo.com/2")));
861 BookmarkNode* n4 = AsMutable(model_->AddURL(model_->bookmark_bar_node(),
863 ASCIIToUTF16("blah"),
864 GURL("http://foo.com/3")));
865 n1->set_date_added(base_time + TimeDelta::FromDays(4));
866 n2->set_date_added(base_time + TimeDelta::FromDays(3));
867 n3->set_date_added(base_time + TimeDelta::FromDays(2));
868 n4->set_date_added(base_time + TimeDelta::FromDays(1));
870 // Make sure order is honored.
871 std::vector<const BookmarkNode*> recently_added;
872 GetMostRecentlyAddedEntries(model_.get(), 2, &recently_added);
873 ASSERT_EQ(2U, recently_added.size());
874 ASSERT_TRUE(n1 == recently_added[0]);
875 ASSERT_TRUE(n2 == recently_added[1]);
877 // swap 1 and 2, then check again.
878 recently_added.clear();
879 SwapDateAdded(n1, n2);
880 GetMostRecentlyAddedEntries(model_.get(), 4, &recently_added);
881 ASSERT_EQ(4U, recently_added.size());
882 ASSERT_TRUE(n2 == recently_added[0]);
883 ASSERT_TRUE(n1 == recently_added[1]);
884 ASSERT_TRUE(n3 == recently_added[2]);
885 ASSERT_TRUE(n4 == recently_added[3]);
888 // Makes sure GetMostRecentlyAddedUserNodeForURL stays in sync.
889 TEST_F(BookmarkModelTest, GetMostRecentlyAddedUserNodeForURL) {
890 // Add a couple of nodes such that the following holds for the time of the
891 // nodes: n1 > n2
892 Time base_time = Time::Now();
893 const GURL url("http://foo.com/0");
894 BookmarkNode* n1 = AsMutable(model_->AddURL(
895 model_->bookmark_bar_node(), 0, ASCIIToUTF16("blah"), url));
896 BookmarkNode* n2 = AsMutable(model_->AddURL(
897 model_->bookmark_bar_node(), 1, ASCIIToUTF16("blah"), url));
898 n1->set_date_added(base_time + TimeDelta::FromDays(4));
899 n2->set_date_added(base_time + TimeDelta::FromDays(3));
901 // Make sure order is honored.
902 ASSERT_EQ(n1, model_->GetMostRecentlyAddedUserNodeForURL(url));
904 // swap 1 and 2, then check again.
905 SwapDateAdded(n1, n2);
906 ASSERT_EQ(n2, model_->GetMostRecentlyAddedUserNodeForURL(url));
909 // Makes sure GetBookmarks removes duplicates.
910 TEST_F(BookmarkModelTest, GetBookmarksWithDups) {
911 const GURL url("http://foo.com/0");
912 const base::string16 title(ASCIIToUTF16("blah"));
913 model_->AddURL(model_->bookmark_bar_node(), 0, title, url);
914 model_->AddURL(model_->bookmark_bar_node(), 1, title, url);
916 std::vector<BookmarkModel::URLAndTitle> bookmarks;
917 model_->GetBookmarks(&bookmarks);
918 ASSERT_EQ(1U, bookmarks.size());
919 EXPECT_EQ(url, bookmarks[0].url);
920 EXPECT_EQ(title, bookmarks[0].title);
922 model_->AddURL(model_->bookmark_bar_node(), 2, ASCIIToUTF16("Title2"), url);
923 // Only one returned, even titles are different.
924 bookmarks.clear();
925 model_->GetBookmarks(&bookmarks);
926 EXPECT_EQ(1U, bookmarks.size());
929 TEST_F(BookmarkModelTest, HasBookmarks) {
930 const GURL url("http://foo.com/");
931 model_->AddURL(model_->bookmark_bar_node(), 0, ASCIIToUTF16("bar"), url);
933 EXPECT_TRUE(model_->HasBookmarks());
936 // http://crbug.com/450464
937 TEST_F(BookmarkModelTest, DISABLED_Sort) {
938 // Populate the bookmark bar node with nodes for 'B', 'a', 'd' and 'C'.
939 // 'C' and 'a' are folders.
940 TestNode bbn;
941 PopulateNodeFromString("B [ a ] d [ a ]", &bbn);
942 const BookmarkNode* parent = model_->bookmark_bar_node();
943 PopulateBookmarkNode(&bbn, model_.get(), parent);
945 BookmarkNode* child1 = AsMutable(parent->GetChild(1));
946 child1->SetTitle(ASCIIToUTF16("a"));
947 delete child1->Remove(child1->GetChild(0));
948 BookmarkNode* child3 = AsMutable(parent->GetChild(3));
949 child3->SetTitle(ASCIIToUTF16("C"));
950 delete child3->Remove(child3->GetChild(0));
952 ClearCounts();
954 // Sort the children of the bookmark bar node.
955 model_->SortChildren(parent);
957 // Make sure we were notified.
958 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 1, 0);
960 // Make sure the order matches (remember, 'a' and 'C' are folders and
961 // come first).
962 EXPECT_EQ(parent->GetChild(0)->GetTitle(), ASCIIToUTF16("a"));
963 EXPECT_EQ(parent->GetChild(1)->GetTitle(), ASCIIToUTF16("C"));
964 EXPECT_EQ(parent->GetChild(2)->GetTitle(), ASCIIToUTF16("B"));
965 EXPECT_EQ(parent->GetChild(3)->GetTitle(), ASCIIToUTF16("d"));
968 TEST_F(BookmarkModelTest, Reorder) {
969 // Populate the bookmark bar node with nodes 'A', 'B', 'C' and 'D'.
970 TestNode bbn;
971 PopulateNodeFromString("A B C D", &bbn);
972 BookmarkNode* parent = AsMutable(model_->bookmark_bar_node());
973 PopulateBookmarkNode(&bbn, model_.get(), parent);
975 ClearCounts();
977 // Reorder bar node's bookmarks in reverse order.
978 std::vector<const BookmarkNode*> new_order;
979 new_order.push_back(parent->GetChild(3));
980 new_order.push_back(parent->GetChild(2));
981 new_order.push_back(parent->GetChild(1));
982 new_order.push_back(parent->GetChild(0));
983 model_->ReorderChildren(parent, new_order);
985 // Make sure we were notified.
986 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 1, 0);
988 // Make sure the order matches is correct (it should be reversed).
989 ASSERT_EQ(4, parent->child_count());
990 EXPECT_EQ("D", base::UTF16ToASCII(parent->GetChild(0)->GetTitle()));
991 EXPECT_EQ("C", base::UTF16ToASCII(parent->GetChild(1)->GetTitle()));
992 EXPECT_EQ("B", base::UTF16ToASCII(parent->GetChild(2)->GetTitle()));
993 EXPECT_EQ("A", base::UTF16ToASCII(parent->GetChild(3)->GetTitle()));
996 TEST_F(BookmarkModelTest, NodeVisibility) {
997 // Mobile node invisible by default
998 EXPECT_TRUE(model_->bookmark_bar_node()->IsVisible());
999 EXPECT_TRUE(model_->other_node()->IsVisible());
1000 EXPECT_FALSE(model_->mobile_node()->IsVisible());
1002 // Visibility of permanent node can only be changed if they are not
1003 // forced to be visible by the client.
1004 model_->SetPermanentNodeVisible(BookmarkNode::BOOKMARK_BAR, false);
1005 EXPECT_TRUE(model_->bookmark_bar_node()->IsVisible());
1006 model_->SetPermanentNodeVisible(BookmarkNode::OTHER_NODE, false);
1007 EXPECT_TRUE(model_->other_node()->IsVisible());
1008 model_->SetPermanentNodeVisible(BookmarkNode::MOBILE, true);
1009 EXPECT_TRUE(model_->mobile_node()->IsVisible());
1010 model_->SetPermanentNodeVisible(BookmarkNode::MOBILE, false);
1011 EXPECT_FALSE(model_->mobile_node()->IsVisible());
1013 // Arbitrary node should be visible
1014 TestNode bbn;
1015 PopulateNodeFromString("B", &bbn);
1016 const BookmarkNode* parent = model_->mobile_node();
1017 PopulateBookmarkNode(&bbn, model_.get(), parent);
1018 EXPECT_TRUE(parent->GetChild(0)->IsVisible());
1020 // Mobile folder should be visible now that it has a child.
1021 EXPECT_TRUE(model_->mobile_node()->IsVisible());
1024 TEST_F(BookmarkModelTest, MobileNodeVisibileWithChildren) {
1025 const BookmarkNode* root = model_->mobile_node();
1026 const base::string16 title(ASCIIToUTF16("foo"));
1027 const GURL url("http://foo.com");
1029 model_->AddURL(root, 0, title, url);
1030 EXPECT_TRUE(model_->mobile_node()->IsVisible());
1033 TEST_F(BookmarkModelTest, ExtensiveChangesObserver) {
1034 AssertExtensiveChangesObserverCount(0, 0);
1035 EXPECT_FALSE(model_->IsDoingExtensiveChanges());
1036 model_->BeginExtensiveChanges();
1037 EXPECT_TRUE(model_->IsDoingExtensiveChanges());
1038 AssertExtensiveChangesObserverCount(1, 0);
1039 model_->EndExtensiveChanges();
1040 EXPECT_FALSE(model_->IsDoingExtensiveChanges());
1041 AssertExtensiveChangesObserverCount(1, 1);
1044 TEST_F(BookmarkModelTest, MultipleExtensiveChangesObserver) {
1045 AssertExtensiveChangesObserverCount(0, 0);
1046 EXPECT_FALSE(model_->IsDoingExtensiveChanges());
1047 model_->BeginExtensiveChanges();
1048 EXPECT_TRUE(model_->IsDoingExtensiveChanges());
1049 AssertExtensiveChangesObserverCount(1, 0);
1050 model_->BeginExtensiveChanges();
1051 EXPECT_TRUE(model_->IsDoingExtensiveChanges());
1052 AssertExtensiveChangesObserverCount(1, 0);
1053 model_->EndExtensiveChanges();
1054 EXPECT_TRUE(model_->IsDoingExtensiveChanges());
1055 AssertExtensiveChangesObserverCount(1, 0);
1056 model_->EndExtensiveChanges();
1057 EXPECT_FALSE(model_->IsDoingExtensiveChanges());
1058 AssertExtensiveChangesObserverCount(1, 1);
1061 // Verifies that IsBookmarked is true if any bookmark matches the given URL,
1062 // and that IsBookmarkedByUser is true only if at least one of the matching
1063 // bookmarks can be edited by the user.
1064 TEST_F(BookmarkModelTest, IsBookmarked) {
1065 // Reload the model with an extra node that is not editable by the user.
1066 BookmarkPermanentNode* extra_node = ReloadModelWithExtraNode();
1068 // "google.com" is a "user" bookmark.
1069 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("User"),
1070 GURL("http://google.com"));
1071 // "youtube.com" is not.
1072 model_->AddURL(extra_node, 0, base::ASCIIToUTF16("Extra"),
1073 GURL("http://youtube.com"));
1075 EXPECT_TRUE(model_->IsBookmarked(GURL("http://google.com")));
1076 EXPECT_TRUE(model_->IsBookmarked(GURL("http://youtube.com")));
1077 EXPECT_FALSE(model_->IsBookmarked(GURL("http://reddit.com")));
1079 EXPECT_TRUE(IsBookmarkedByUser(model_.get(), GURL("http://google.com")));
1080 EXPECT_FALSE(IsBookmarkedByUser(model_.get(), GURL("http://youtube.com")));
1081 EXPECT_FALSE(IsBookmarkedByUser(model_.get(), GURL("http://reddit.com")));
1084 // Verifies that GetMostRecentlyAddedUserNodeForURL skips bookmarks that
1085 // are not owned by the user.
1086 TEST_F(BookmarkModelTest, GetMostRecentlyAddedUserNodeForURLSkipsManagedNodes) {
1087 // Reload the model with an extra node that is not editable by the user.
1088 BookmarkPermanentNode* extra_node = ReloadModelWithExtraNode();
1090 const base::string16 title = base::ASCIIToUTF16("Title");
1091 const BookmarkNode* user_parent = model_->other_node();
1092 const BookmarkNode* managed_parent = extra_node;
1093 const GURL url("http://google.com");
1095 // |url| is not bookmarked yet.
1096 EXPECT_TRUE(model_->GetMostRecentlyAddedUserNodeForURL(url) == NULL);
1098 // Having a managed node doesn't count.
1099 model_->AddURL(managed_parent, 0, title, url);
1100 EXPECT_TRUE(model_->GetMostRecentlyAddedUserNodeForURL(url) == NULL);
1102 // Now add a user node.
1103 const BookmarkNode* user = model_->AddURL(user_parent, 0, title, url);
1104 EXPECT_EQ(user, model_->GetMostRecentlyAddedUserNodeForURL(url));
1106 // Having a more recent managed node doesn't count either.
1107 const BookmarkNode* managed = model_->AddURL(managed_parent, 0, title, url);
1108 EXPECT_GE(managed->date_added(), user->date_added());
1109 EXPECT_EQ(user, model_->GetMostRecentlyAddedUserNodeForURL(url));
1112 TEST(BookmarkNodeTest, NodeMetaInfo) {
1113 GURL url;
1114 BookmarkNode node(url);
1115 EXPECT_FALSE(node.GetMetaInfoMap());
1117 EXPECT_TRUE(node.SetMetaInfo("key1", "value1"));
1118 std::string out_value;
1119 EXPECT_TRUE(node.GetMetaInfo("key1", &out_value));
1120 EXPECT_EQ("value1", out_value);
1121 EXPECT_FALSE(node.SetMetaInfo("key1", "value1"));
1123 EXPECT_FALSE(node.GetMetaInfo("key2.subkey1", &out_value));
1124 EXPECT_TRUE(node.SetMetaInfo("key2.subkey1", "value2"));
1125 EXPECT_TRUE(node.GetMetaInfo("key2.subkey1", &out_value));
1126 EXPECT_EQ("value2", out_value);
1128 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2.leaf", &out_value));
1129 EXPECT_TRUE(node.SetMetaInfo("key2.subkey2.leaf", ""));
1130 EXPECT_TRUE(node.GetMetaInfo("key2.subkey2.leaf", &out_value));
1131 EXPECT_EQ("", out_value);
1133 EXPECT_TRUE(node.DeleteMetaInfo("key1"));
1134 EXPECT_TRUE(node.DeleteMetaInfo("key2.subkey1"));
1135 EXPECT_TRUE(node.DeleteMetaInfo("key2.subkey2.leaf"));
1136 EXPECT_FALSE(node.DeleteMetaInfo("key3"));
1137 EXPECT_FALSE(node.GetMetaInfo("key1", &out_value));
1138 EXPECT_FALSE(node.GetMetaInfo("key2.subkey1", &out_value));
1139 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2", &out_value));
1140 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2.leaf", &out_value));
1141 EXPECT_FALSE(node.GetMetaInfoMap());
1144 // Creates a set of nodes in the bookmark model, and checks that the loaded
1145 // structure is what we first created.
1146 TEST(BookmarkModelTest2, CreateAndRestore) {
1147 struct TestData {
1148 // Structure of the children of the bookmark model node.
1149 const std::string bbn_contents;
1150 // Structure of the children of the other node.
1151 const std::string other_contents;
1152 // Structure of the children of the synced node.
1153 const std::string mobile_contents;
1154 } data[] = {
1155 // See PopulateNodeFromString for a description of these strings.
1156 { "", "" },
1157 { "a", "b" },
1158 { "a [ b ]", "" },
1159 { "", "[ b ] a [ c [ d e [ f ] ] ]" },
1160 { "a [ b ]", "" },
1161 { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"},
1163 TestBookmarkClient client;
1164 scoped_ptr<BookmarkModel> model;
1165 for (size_t i = 0; i < arraysize(data); ++i) {
1166 model = client.CreateModel();
1168 TestNode bbn;
1169 PopulateNodeFromString(data[i].bbn_contents, &bbn);
1170 PopulateBookmarkNode(&bbn, model.get(), model->bookmark_bar_node());
1172 TestNode other;
1173 PopulateNodeFromString(data[i].other_contents, &other);
1174 PopulateBookmarkNode(&other, model.get(), model->other_node());
1176 TestNode mobile;
1177 PopulateNodeFromString(data[i].mobile_contents, &mobile);
1178 PopulateBookmarkNode(&mobile, model.get(), model->mobile_node());
1180 VerifyModelMatchesNode(&bbn, model->bookmark_bar_node());
1181 VerifyModelMatchesNode(&other, model->other_node());
1182 VerifyModelMatchesNode(&mobile, model->mobile_node());
1183 VerifyNoDuplicateIDs(model.get());
1187 } // namespace
1189 class BookmarkModelFaviconTest : public testing::Test,
1190 public BookmarkModelObserver {
1191 public:
1192 BookmarkModelFaviconTest() : model_(client_.CreateModel()) {
1193 model_->AddObserver(this);
1196 // Emulates the favicon getting asynchronously loaded. In production, the
1197 // favicon is asynchronously loaded when BookmarkModel::GetFavicon() is
1198 // called.
1199 void OnFaviconLoaded(BookmarkNode* node, const GURL& icon_url) {
1200 SkBitmap bitmap;
1201 bitmap.allocN32Pixels(16, 16);
1202 bitmap.eraseColor(SK_ColorBLUE);
1203 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap);
1205 favicon_base::FaviconImageResult image_result;
1206 image_result.image = image;
1207 image_result.icon_url = icon_url;
1208 model_->OnFaviconDataAvailable(node, favicon_base::IconType::FAVICON,
1209 image_result);
1212 bool WasNodeUpdated(const BookmarkNode* node) {
1213 return std::find(updated_nodes_.begin(), updated_nodes_.end(), node) !=
1214 updated_nodes_.end();
1217 void ClearUpdatedNodes() {
1218 updated_nodes_.clear();
1221 protected:
1222 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override {
1225 void BookmarkNodeMoved(BookmarkModel* model,
1226 const BookmarkNode* old_parent,
1227 int old_index,
1228 const BookmarkNode* new_parent,
1229 int new_index) override {}
1231 void BookmarkNodeAdded(BookmarkModel* model,
1232 const BookmarkNode* parent,
1233 int index) override {}
1235 void BookmarkNodeRemoved(BookmarkModel* model,
1236 const BookmarkNode* parent,
1237 int old_index,
1238 const BookmarkNode* node,
1239 const std::set<GURL>& removed_urls) override {}
1241 void BookmarkNodeChanged(BookmarkModel* model,
1242 const BookmarkNode* node) override {}
1244 void BookmarkNodeFaviconChanged(BookmarkModel* model,
1245 const BookmarkNode* node) override {
1246 updated_nodes_.push_back(node);
1249 void BookmarkNodeChildrenReordered(BookmarkModel* model,
1250 const BookmarkNode* node) override {}
1252 void BookmarkAllUserNodesRemoved(
1253 BookmarkModel* model,
1254 const std::set<GURL>& removed_urls) override {
1257 TestBookmarkClient client_;
1258 scoped_ptr<BookmarkModel> model_;
1259 std::vector<const BookmarkNode*> updated_nodes_;
1261 private:
1262 DISALLOW_COPY_AND_ASSIGN(BookmarkModelFaviconTest);
1265 // Test that BookmarkModel::OnFaviconsChanged() sends a notification that the
1266 // favicon changed to each BookmarkNode which has either a matching page URL
1267 // (e.g. http://www.google.com) or a matching icon URL
1268 // (e.g. http://www.google.com/favicon.ico).
1269 TEST_F(BookmarkModelFaviconTest, FaviconsChangedObserver) {
1270 const BookmarkNode* root = model_->bookmark_bar_node();
1271 base::string16 kTitle(ASCIIToUTF16("foo"));
1272 GURL kPageURL1("http://www.google.com");
1273 GURL kPageURL2("http://www.google.ca");
1274 GURL kPageURL3("http://www.amazon.com");
1275 GURL kFaviconURL12("http://www.google.com/favicon.ico");
1276 GURL kFaviconURL3("http://www.amazon.com/favicon.ico");
1278 const BookmarkNode* node1 = model_->AddURL(root, 0, kTitle, kPageURL1);
1279 const BookmarkNode* node2 = model_->AddURL(root, 0, kTitle, kPageURL2);
1280 const BookmarkNode* node3 = model_->AddURL(root, 0, kTitle, kPageURL3);
1281 const BookmarkNode* node4 = model_->AddURL(root, 0, kTitle, kPageURL3);
1284 OnFaviconLoaded(AsMutable(node1), kFaviconURL12);
1285 OnFaviconLoaded(AsMutable(node2), kFaviconURL12);
1286 OnFaviconLoaded(AsMutable(node3), kFaviconURL3);
1287 OnFaviconLoaded(AsMutable(node4), kFaviconURL3);
1289 ClearUpdatedNodes();
1290 std::set<GURL> changed_page_urls;
1291 changed_page_urls.insert(kPageURL2);
1292 changed_page_urls.insert(kPageURL3);
1293 model_->OnFaviconsChanged(changed_page_urls, GURL());
1294 ASSERT_EQ(3u, updated_nodes_.size());
1295 EXPECT_TRUE(WasNodeUpdated(node2));
1296 EXPECT_TRUE(WasNodeUpdated(node3));
1297 EXPECT_TRUE(WasNodeUpdated(node4));
1301 // Reset the favicon data because BookmarkModel::OnFaviconsChanged() clears
1302 // the BookmarkNode's favicon data for all of the BookmarkNodes whose
1303 // favicon data changed.
1304 OnFaviconLoaded(AsMutable(node1), kFaviconURL12);
1305 OnFaviconLoaded(AsMutable(node2), kFaviconURL12);
1306 OnFaviconLoaded(AsMutable(node3), kFaviconURL3);
1307 OnFaviconLoaded(AsMutable(node4), kFaviconURL3);
1309 ClearUpdatedNodes();
1310 model_->OnFaviconsChanged(std::set<GURL>(), kFaviconURL12);
1311 ASSERT_EQ(2u, updated_nodes_.size());
1312 EXPECT_TRUE(WasNodeUpdated(node1));
1313 EXPECT_TRUE(WasNodeUpdated(node2));
1317 OnFaviconLoaded(AsMutable(node1), kFaviconURL12);
1318 OnFaviconLoaded(AsMutable(node2), kFaviconURL12);
1319 OnFaviconLoaded(AsMutable(node3), kFaviconURL3);
1320 OnFaviconLoaded(AsMutable(node4), kFaviconURL3);
1322 ClearUpdatedNodes();
1323 std::set<GURL> changed_page_urls;
1324 changed_page_urls.insert(kPageURL1);
1325 model_->OnFaviconsChanged(changed_page_urls, kFaviconURL12);
1326 ASSERT_EQ(2u, updated_nodes_.size());
1327 EXPECT_TRUE(WasNodeUpdated(node1));
1328 EXPECT_TRUE(WasNodeUpdated(node2));
1332 } // namespace bookmarks