123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- // Copyright 2014 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "components/bookmarks/browser/bookmark_utils.h"
- #include <stddef.h>
- #include <memory>
- #include <utility>
- #include <vector>
- #include "base/strings/utf_string_conversions.h"
- #include "base/test/task_environment.h"
- #include "build/build_config.h"
- #include "build/chromeos_buildflags.h"
- #include "components/bookmarks/browser/base_bookmark_model_observer.h"
- #include "components/bookmarks/browser/bookmark_model.h"
- #include "components/bookmarks/browser/bookmark_node_data.h"
- #include "components/bookmarks/test/test_bookmark_client.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "ui/base/clipboard/clipboard.h"
- #include "ui/base/clipboard/scoped_clipboard_writer.h"
- using base::ASCIIToUTF16;
- using std::string;
- namespace bookmarks {
- namespace {
- class BookmarkUtilsTest : public testing::Test,
- public BaseBookmarkModelObserver {
- public:
- BookmarkUtilsTest()
- : task_environment_(base::test::TaskEnvironment::MainThreadType::UI),
- grouped_changes_beginning_count_(0),
- grouped_changes_ended_count_(0) {}
- BookmarkUtilsTest(const BookmarkUtilsTest&) = delete;
- BookmarkUtilsTest& operator=(const BookmarkUtilsTest&) = delete;
- ~BookmarkUtilsTest() override {}
- // Copy and paste is not yet supported on iOS. http://crbug.com/228147
- #if !BUILDFLAG(IS_IOS)
- void TearDown() override {
- ui::Clipboard::DestroyClipboardForCurrentThread();
- }
- #endif // !BUILDFLAG(IS_IOS)
- // Certain user actions require multiple changes to the bookmark model,
- // however these modifications need to be atomic for the undo framework. The
- // BaseBookmarkModelObserver is used to inform the boundaries of the user
- // action. For example, when multiple bookmarks are cut to the clipboard we
- // expect one call each to GroupedBookmarkChangesBeginning/Ended.
- void ExpectGroupedChangeCount(int expected_beginning_count,
- int expected_ended_count) {
- // The undo framework is not used under Android. Thus the group change
- // events will not be fired and so should not be tested for Android.
- #if !BUILDFLAG(IS_ANDROID)
- EXPECT_EQ(grouped_changes_beginning_count_, expected_beginning_count);
- EXPECT_EQ(grouped_changes_ended_count_, expected_ended_count);
- #endif
- }
- private:
- // BaseBookmarkModelObserver:
- void BookmarkModelChanged() override {}
- void GroupedBookmarkChangesBeginning(BookmarkModel* model) override {
- ++grouped_changes_beginning_count_;
- }
- void GroupedBookmarkChangesEnded(BookmarkModel* model) override {
- ++grouped_changes_ended_count_;
- }
- // Clipboard requires a full TaskEnvironment.
- base::test::TaskEnvironment task_environment_;
- int grouped_changes_beginning_count_;
- int grouped_changes_ended_count_;
- };
- TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesWordPhraseQuery) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"foo bar",
- GURL("http://www.google.com"));
- const BookmarkNode* node2 = model->AddURL(model->other_node(), 0, u"baz buz",
- GURL("http://www.cnn.com"));
- const BookmarkNode* folder1 =
- model->AddFolder(model->other_node(), 0, u"foo");
- std::vector<const BookmarkNode*> nodes;
- QueryFields query;
- query.word_phrase_query = std::make_unique<std::u16string>();
- // No nodes are returned for empty string.
- *query.word_phrase_query = u"";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- EXPECT_TRUE(nodes.empty());
- nodes.clear();
- // No nodes are returned for space-only string.
- *query.word_phrase_query = u" ";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- EXPECT_TRUE(nodes.empty());
- nodes.clear();
- // Node "foo bar" and folder "foo" are returned in search results.
- *query.word_phrase_query = u"foo";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(2U, nodes.size());
- EXPECT_TRUE(nodes[0] == folder1);
- EXPECT_TRUE(nodes[1] == node1);
- nodes.clear();
- // Ensure url matches return in search results.
- *query.word_phrase_query = u"cnn";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == node2);
- nodes.clear();
- // Ensure folder "foo" is not returned in more specific search.
- *query.word_phrase_query = u"foo bar";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == node1);
- nodes.clear();
- // Bookmark Bar and Other Bookmarks are not returned in search results.
- *query.word_phrase_query = u"Bookmark";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(0U, nodes.size());
- nodes.clear();
- }
- // Check exact matching against a URL query.
- TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesUrl) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"Google",
- GURL("https://www.google.com/"));
- model->AddURL(model->other_node(), 0, u"Google Calendar",
- GURL("https://www.google.com/calendar"));
- model->AddFolder(model->other_node(), 0, u"Folder");
- std::vector<const BookmarkNode*> nodes;
- QueryFields query;
- query.url = std::make_unique<std::u16string>();
- *query.url = u"https://www.google.com/";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == node1);
- nodes.clear();
- *query.url = u"calendar";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(0U, nodes.size());
- nodes.clear();
- // Empty URL should not match folders.
- *query.url = u"";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(0U, nodes.size());
- nodes.clear();
- }
- // Check exact matching against a title query.
- TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesTitle) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"Google",
- GURL("https://www.google.com/"));
- model->AddURL(model->other_node(), 0, u"Google Calendar",
- GURL("https://www.google.com/calendar"));
- const BookmarkNode* folder1 =
- model->AddFolder(model->other_node(), 0, u"Folder");
- std::vector<const BookmarkNode*> nodes;
- QueryFields query;
- query.title = std::make_unique<std::u16string>();
- *query.title = u"Google";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == node1);
- nodes.clear();
- *query.title = u"Calendar";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(0U, nodes.size());
- nodes.clear();
- // Title should match folders.
- *query.title = u"Folder";
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == folder1);
- nodes.clear();
- }
- // Check matching against a query with multiple predicates.
- TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesConjunction) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const BookmarkNode* node1 = model->AddURL(model->other_node(), 0, u"Google",
- GURL("https://www.google.com/"));
- model->AddURL(model->other_node(), 0, u"Google Calendar",
- GURL("https://www.google.com/calendar"));
- model->AddFolder(model->other_node(), 0, u"Folder");
- std::vector<const BookmarkNode*> nodes;
- QueryFields query;
- // Test all fields matching.
- query.word_phrase_query = std::make_unique<std::u16string>(u"www");
- query.url = std::make_unique<std::u16string>(u"https://www.google.com/");
- query.title = std::make_unique<std::u16string>(u"Google");
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == node1);
- nodes.clear();
- std::unique_ptr<std::u16string>* fields[] = {&query.word_phrase_query,
- &query.url, &query.title};
- // Test two fields matching.
- for (size_t i = 0; i < std::size(fields); i++) {
- std::unique_ptr<std::u16string> original_value(fields[i]->release());
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(1U, nodes.size());
- EXPECT_TRUE(nodes[0] == node1);
- nodes.clear();
- *fields[i] = std::move(original_value);
- }
- // Test two fields matching with one non-matching field.
- for (size_t i = 0; i < std::size(fields); i++) {
- std::unique_ptr<std::u16string> original_value(fields[i]->release());
- *fields[i] = std::make_unique<std::u16string>(u"fjdkslafjkldsa");
- GetBookmarksMatchingProperties(model.get(), query, 100, &nodes);
- ASSERT_EQ(0U, nodes.size());
- nodes.clear();
- *fields[i] = std::move(original_value);
- }
- }
- // Copy and paste is not yet supported on iOS. http://crbug.com/228147
- #if !BUILDFLAG(IS_IOS)
- TEST_F(BookmarkUtilsTest, DISABLED_PasteBookmarkFromURL) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const std::u16string url_text = u"http://www.google.com/";
- const BookmarkNode* new_folder =
- model->AddFolder(model->bookmark_bar_node(), 0, u"New_Folder");
- // Write blank text to clipboard.
- {
- ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste);
- clipboard_writer.WriteText(std::u16string());
- }
- // Now we shouldn't be able to paste from the clipboard.
- EXPECT_FALSE(CanPasteFromClipboard(model.get(), new_folder));
- // Write some valid url to the clipboard.
- {
- ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste);
- clipboard_writer.WriteText(url_text);
- }
- // Now we should be able to paste from the clipboard.
- EXPECT_TRUE(CanPasteFromClipboard(model.get(), new_folder));
- PasteFromClipboard(model.get(), new_folder, 0);
- ASSERT_EQ(1u, new_folder->children().size());
- // Url for added node should be same as url_text.
- EXPECT_EQ(url_text,
- ASCIIToUTF16(new_folder->children().front()->url().spec()));
- }
- // TODO(https://crbug.com/1010182): Fix flakes and re-enable this test.
- #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
- #define MAYBE_CopyPaste DISABLED_CopyPaste
- #else
- #define MAYBE_CopyPaste CopyPaste
- #endif
- TEST_F(BookmarkUtilsTest, MAYBE_CopyPaste) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const BookmarkNode* node = model->AddURL(model->other_node(), 0, u"foo bar",
- GURL("http://www.google.com"));
- // Copy a node to the clipboard.
- std::vector<const BookmarkNode*> nodes;
- nodes.push_back(node);
- CopyToClipboard(model.get(), nodes, false);
- // And make sure we can paste a bookmark from the clipboard.
- EXPECT_TRUE(CanPasteFromClipboard(model.get(), model->bookmark_bar_node()));
- // Write some text to the clipboard.
- {
- ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste);
- clipboard_writer.WriteText(u"foo");
- }
- // Now we shouldn't be able to paste from the clipboard.
- EXPECT_FALSE(CanPasteFromClipboard(model.get(), model->bookmark_bar_node()));
- }
- // Test for updating title such that url and title pair are unique among the
- // children of parent.
- TEST_F(BookmarkUtilsTest, DISABLED_MakeTitleUnique) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const std::u16string url_text = u"http://www.google.com/";
- const std::u16string title_text = u"foobar";
- const BookmarkNode* bookmark_bar_node = model->bookmark_bar_node();
- const BookmarkNode* node =
- model->AddURL(bookmark_bar_node, 0, title_text, GURL(url_text));
- EXPECT_EQ(url_text,
- ASCIIToUTF16(bookmark_bar_node->children()[0]->url().spec()));
- EXPECT_EQ(title_text, bookmark_bar_node->children()[0]->GetTitle());
- // Copy a node to the clipboard.
- std::vector<const BookmarkNode*> nodes;
- nodes.push_back(node);
- CopyToClipboard(model.get(), nodes, false);
- // Now we should be able to paste from the clipboard.
- EXPECT_TRUE(CanPasteFromClipboard(model.get(), bookmark_bar_node));
- PasteFromClipboard(model.get(), bookmark_bar_node, 1);
- ASSERT_EQ(2u, bookmark_bar_node->children().size());
- // Url for added node should be same as url_text.
- EXPECT_EQ(url_text,
- ASCIIToUTF16(bookmark_bar_node->children()[1]->url().spec()));
- // Title for added node should be numeric subscript suffix with copied node
- // title.
- EXPECT_EQ(u"foobar (1)", bookmark_bar_node->children()[1]->GetTitle());
- }
- TEST_F(BookmarkUtilsTest, DISABLED_CopyPasteMetaInfo) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- const BookmarkNode* node = model->AddURL(model->other_node(), 0, u"foo bar",
- GURL("http://www.google.com"));
- model->SetNodeMetaInfo(node, "somekey", "somevalue");
- model->SetNodeMetaInfo(node, "someotherkey", "someothervalue");
- // Copy a node to the clipboard.
- std::vector<const BookmarkNode*> nodes;
- nodes.push_back(node);
- CopyToClipboard(model.get(), nodes, false);
- // Paste node to a different folder.
- const BookmarkNode* folder =
- model->AddFolder(model->bookmark_bar_node(), 0, u"Folder");
- EXPECT_EQ(0u, folder->children().size());
- // And make sure we can paste a bookmark from the clipboard.
- EXPECT_TRUE(CanPasteFromClipboard(model.get(), folder));
- PasteFromClipboard(model.get(), folder, 0);
- ASSERT_EQ(1u, folder->children().size());
- // Verify that the pasted node contains the same meta info.
- const BookmarkNode* pasted = folder->children().front().get();
- ASSERT_TRUE(pasted->GetMetaInfoMap());
- EXPECT_EQ(2u, pasted->GetMetaInfoMap()->size());
- std::string value;
- EXPECT_TRUE(pasted->GetMetaInfo("somekey", &value));
- EXPECT_EQ("somevalue", value);
- EXPECT_TRUE(pasted->GetMetaInfo("someotherkey", &value));
- EXPECT_EQ("someothervalue", value);
- }
- #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC)
- // http://crbug.com/396472
- #define MAYBE_CutToClipboard DISABLED_CutToClipboard
- #else
- #define MAYBE_CutToClipboard CutToClipboard
- #endif
- TEST_F(BookmarkUtilsTest, MAYBE_CutToClipboard) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- model->AddObserver(this);
- std::u16string title(u"foo");
- GURL url("http://foo.com");
- const BookmarkNode* n1 = model->AddURL(model->other_node(), 0, title, url);
- const BookmarkNode* n2 = model->AddURL(model->other_node(), 1, title, url);
- // Cut the nodes to the clipboard.
- std::vector<const BookmarkNode*> nodes;
- nodes.push_back(n1);
- nodes.push_back(n2);
- CopyToClipboard(model.get(), nodes, true);
- // Make sure the nodes were removed.
- EXPECT_EQ(0u, model->other_node()->children().size());
- // Make sure observers were notified the set of changes should be grouped.
- ExpectGroupedChangeCount(1, 1);
- // And make sure we can paste from the clipboard.
- EXPECT_TRUE(CanPasteFromClipboard(model.get(), model->other_node()));
- }
- // Test is flaky on Mac and LaCros: crbug.com/1236362
- #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS_LACROS)
- #define MAYBE_PasteNonEditableNodes DISABLED_PasteNonEditableNodes
- #else
- #define MAYBE_PasteNonEditableNodes PasteNonEditableNodes
- #endif
- TEST_F(BookmarkUtilsTest, MAYBE_PasteNonEditableNodes) {
- // Load a model with an managed node that is not editable.
- auto client = std::make_unique<TestBookmarkClient>();
- BookmarkNode* managed_node = client->EnableManagedNode();
- std::unique_ptr<BookmarkModel> model(
- TestBookmarkClient::CreateModelWithClient(std::move(client)));
- const BookmarkNode* node = model->AddURL(model->other_node(), 0, u"foo bar",
- GURL("http://www.google.com"));
- // Copy a node to the clipboard.
- std::vector<const BookmarkNode*> nodes;
- nodes.push_back(node);
- CopyToClipboard(model.get(), nodes, false);
- // And make sure we can paste a bookmark from the clipboard.
- EXPECT_TRUE(CanPasteFromClipboard(model.get(), model->bookmark_bar_node()));
- // But it can't be pasted into a non-editable folder.
- BookmarkClient* upcast = model->client();
- EXPECT_FALSE(upcast->CanBeEditedByUser(managed_node));
- EXPECT_FALSE(CanPasteFromClipboard(model.get(), managed_node));
- }
- #endif // !BUILDFLAG(IS_IOS)
- TEST_F(BookmarkUtilsTest, GetParentForNewNodes) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- // This tests the case where selection contains one item and that item is a
- // folder.
- std::vector<const BookmarkNode*> nodes;
- nodes.push_back(model->bookmark_bar_node());
- size_t index = static_cast<size_t>(-1);
- const BookmarkNode* real_parent =
- GetParentForNewNodes(model->bookmark_bar_node(), nodes, &index);
- EXPECT_EQ(real_parent, model->bookmark_bar_node());
- EXPECT_EQ(0u, index);
- nodes.clear();
- // This tests the case where selection contains one item and that item is an
- // url.
- const BookmarkNode* page1 = model->AddURL(
- model->bookmark_bar_node(), 0, u"Google", GURL("http://google.com"));
- nodes.push_back(page1);
- real_parent = GetParentForNewNodes(model->bookmark_bar_node(), nodes, &index);
- EXPECT_EQ(real_parent, model->bookmark_bar_node());
- EXPECT_EQ(1u, index);
- // This tests the case where selection has more than one item.
- const BookmarkNode* folder1 =
- model->AddFolder(model->bookmark_bar_node(), 1, u"Folder 1");
- nodes.push_back(folder1);
- real_parent = GetParentForNewNodes(model->bookmark_bar_node(), nodes, &index);
- EXPECT_EQ(real_parent, model->bookmark_bar_node());
- EXPECT_EQ(2u, index);
- // This tests the case where selection doesn't contain any items.
- nodes.clear();
- real_parent = GetParentForNewNodes(model->bookmark_bar_node(), nodes, &index);
- EXPECT_EQ(real_parent, model->bookmark_bar_node());
- EXPECT_EQ(2u, index);
- }
- // Verifies that meta info is copied when nodes are cloned.
- TEST_F(BookmarkUtilsTest, CloneMetaInfo) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- // Add a node containing meta info.
- const BookmarkNode* node = model->AddURL(model->other_node(), 0, u"foo bar",
- GURL("http://www.google.com"));
- model->SetNodeMetaInfo(node, "somekey", "somevalue");
- model->SetNodeMetaInfo(node, "someotherkey", "someothervalue");
- // Clone node to a different folder.
- const BookmarkNode* folder =
- model->AddFolder(model->bookmark_bar_node(), 0, u"Folder");
- std::vector<BookmarkNodeData::Element> elements;
- BookmarkNodeData::Element node_data(node);
- elements.push_back(node_data);
- EXPECT_EQ(0u, folder->children().size());
- CloneBookmarkNode(model.get(), elements, folder, 0, false);
- ASSERT_EQ(1u, folder->children().size());
- // Verify that the cloned node contains the same meta info.
- const BookmarkNode* clone = folder->children().front().get();
- ASSERT_TRUE(clone->GetMetaInfoMap());
- EXPECT_EQ(2u, clone->GetMetaInfoMap()->size());
- std::string value;
- EXPECT_TRUE(clone->GetMetaInfo("somekey", &value));
- EXPECT_EQ("somevalue", value);
- EXPECT_TRUE(clone->GetMetaInfo("someotherkey", &value));
- EXPECT_EQ("someothervalue", value);
- }
- // Verifies that meta info fields in the non cloned set are not copied when
- // cloning a bookmark.
- TEST_F(BookmarkUtilsTest, CloneBookmarkResetsNonClonedKey) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- model->AddNonClonedKey("foo");
- const BookmarkNode* parent = model->other_node();
- const BookmarkNode* node =
- model->AddURL(parent, 0, u"title", GURL("http://www.google.com"));
- model->SetNodeMetaInfo(node, "foo", "ignored value");
- model->SetNodeMetaInfo(node, "bar", "kept value");
- std::vector<BookmarkNodeData::Element> elements;
- BookmarkNodeData::Element node_data(node);
- elements.push_back(node_data);
- // Cloning a bookmark should clear the non cloned key.
- CloneBookmarkNode(model.get(), elements, parent, 0, true);
- ASSERT_EQ(2u, parent->children().size());
- std::string value;
- EXPECT_FALSE(parent->children().front()->GetMetaInfo("foo", &value));
- // Other keys should still be cloned.
- EXPECT_TRUE(parent->children().front()->GetMetaInfo("bar", &value));
- EXPECT_EQ("kept value", value);
- }
- // Verifies that meta info fields in the non cloned set are not copied when
- // cloning a folder.
- TEST_F(BookmarkUtilsTest, CloneFolderResetsNonClonedKey) {
- std::unique_ptr<BookmarkModel> model(TestBookmarkClient::CreateModel());
- model->AddNonClonedKey("foo");
- const BookmarkNode* parent = model->other_node();
- const BookmarkNode* node = model->AddFolder(parent, 0, u"title");
- model->SetNodeMetaInfo(node, "foo", "ignored value");
- model->SetNodeMetaInfo(node, "bar", "kept value");
- std::vector<BookmarkNodeData::Element> elements;
- BookmarkNodeData::Element node_data(node);
- elements.push_back(node_data);
- // Cloning a folder should clear the non cloned key.
- CloneBookmarkNode(model.get(), elements, parent, 0, true);
- ASSERT_EQ(2u, parent->children().size());
- std::string value;
- EXPECT_FALSE(parent->children().front()->GetMetaInfo("foo", &value));
- // Other keys should still be cloned.
- EXPECT_TRUE(parent->children().front()->GetMetaInfo("bar", &value));
- EXPECT_EQ("kept value", value);
- }
- TEST_F(BookmarkUtilsTest, RemoveAllBookmarks) {
- // Load a model with an managed node that is not editable.
- auto client = std::make_unique<TestBookmarkClient>();
- BookmarkNode* managed_node = client->EnableManagedNode();
- std::unique_ptr<BookmarkModel> model(
- TestBookmarkClient::CreateModelWithClient(std::move(client)));
- EXPECT_TRUE(model->bookmark_bar_node()->children().empty());
- EXPECT_TRUE(model->other_node()->children().empty());
- EXPECT_TRUE(model->mobile_node()->children().empty());
- EXPECT_TRUE(managed_node->children().empty());
- const std::u16string title = u"Title";
- const GURL url("http://google.com");
- model->AddURL(model->bookmark_bar_node(), 0, title, url);
- model->AddURL(model->other_node(), 0, title, url);
- model->AddURL(model->mobile_node(), 0, title, url);
- model->AddURL(managed_node, 0, title, url);
- std::vector<const BookmarkNode*> nodes;
- model->GetNodesByURL(url, &nodes);
- ASSERT_EQ(4u, nodes.size());
- RemoveAllBookmarks(model.get(), url);
- nodes.clear();
- model->GetNodesByURL(url, &nodes);
- ASSERT_EQ(1u, nodes.size());
- EXPECT_TRUE(model->bookmark_bar_node()->children().empty());
- EXPECT_TRUE(model->other_node()->children().empty());
- EXPECT_TRUE(model->mobile_node()->children().empty());
- EXPECT_EQ(1u, managed_node->children().size());
- }
- } // namespace
- } // namespace bookmarks
|