1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_helpers.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h"
13 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h"
14 #include "chrome/common/extensions/api/bookmarks.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/managed/managed_bookmark_service.h"
18 #include "components/bookmarks/test/bookmark_test_helpers.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using bookmarks::BookmarkModel
;
23 using bookmarks::BookmarkNode
;
25 namespace extensions
{
27 namespace keys
= bookmark_api_constants
;
28 using api::bookmarks::BookmarkTreeNode
;
30 namespace bookmark_api_helpers
{
32 class ExtensionBookmarksTest
: public testing::Test
{
34 ExtensionBookmarksTest()
41 void SetUp() override
{
42 profile_
.CreateBookmarkModel(false);
43 model_
= BookmarkModelFactory::GetForProfile(&profile_
);
44 managed_
= ManagedBookmarkServiceFactory::GetForProfile(&profile_
);
45 bookmarks::test::WaitForBookmarkModelToLoad(model_
);
47 node_
= model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("Digg"),
48 GURL("http://www.reddit.com"));
49 model_
->SetNodeMetaInfo(node_
, "some_key1", "some_value1");
50 model_
->SetNodeMetaInfo(node_
, "some_key2", "some_value2");
51 model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("News"),
52 GURL("http://www.foxnews.com"));
53 folder_
= model_
->AddFolder(
54 model_
->other_node(), 0, base::ASCIIToUTF16("outer folder"));
55 model_
->SetNodeMetaInfo(folder_
, "some_key1", "some_value1");
56 model_
->AddFolder(folder_
, 0, base::ASCIIToUTF16("inner folder 1"));
57 model_
->AddFolder(folder_
, 0, base::ASCIIToUTF16("inner folder 2"));
58 node2_
= model_
->AddURL(
59 folder_
, 0, base::ASCIIToUTF16("Digg"), GURL("http://reddit.com"));
60 model_
->SetNodeMetaInfo(node2_
, "some_key2", "some_value2");
62 folder_
, 0, base::ASCIIToUTF16("CNet"), GURL("http://cnet.com"));
65 content::TestBrowserThreadBundle thread_bundle_
;
66 TestingProfile profile_
;
67 bookmarks::ManagedBookmarkService
* managed_
;
68 BookmarkModel
* model_
;
69 const BookmarkNode
* node_
;
70 const BookmarkNode
* node2_
;
71 const BookmarkNode
* folder_
;
74 TEST_F(ExtensionBookmarksTest
, GetFullTreeFromRoot
) {
75 scoped_ptr
<BookmarkTreeNode
> tree(
76 GetBookmarkTreeNode(managed_
,
79 false)); // Not only folders.
80 ASSERT_EQ(3U, tree
->children
->size());
83 TEST_F(ExtensionBookmarksTest
, GetFoldersOnlyFromRoot
) {
84 scoped_ptr
<BookmarkTreeNode
> tree(
85 GetBookmarkTreeNode(managed_
,
88 true)); // Only folders.
89 ASSERT_EQ(1U, tree
->children
->size());
92 TEST_F(ExtensionBookmarksTest
, GetSubtree
) {
93 scoped_ptr
<BookmarkTreeNode
> tree(
94 GetBookmarkTreeNode(managed_
,
97 false)); // Not only folders.
98 ASSERT_EQ(4U, tree
->children
->size());
99 linked_ptr
<BookmarkTreeNode
> digg
= tree
->children
->at(1);
100 ASSERT_TRUE(digg
.get());
101 ASSERT_EQ("Digg", digg
->title
);
104 TEST_F(ExtensionBookmarksTest
, GetSubtreeFoldersOnly
) {
105 scoped_ptr
<BookmarkTreeNode
> tree(
106 GetBookmarkTreeNode(managed_
,
109 true)); // Only folders.
110 ASSERT_EQ(2U, tree
->children
->size());
111 linked_ptr
<BookmarkTreeNode
> inner_folder
= tree
->children
->at(1);
112 ASSERT_TRUE(inner_folder
.get());
113 ASSERT_EQ("inner folder 1", inner_folder
->title
);
116 TEST_F(ExtensionBookmarksTest
, GetModifiableNode
) {
117 scoped_ptr
<BookmarkTreeNode
> tree(
118 GetBookmarkTreeNode(managed_
,
121 false)); // Only folders.
122 EXPECT_EQ("Digg", tree
->title
);
123 ASSERT_TRUE(tree
->url
);
124 EXPECT_EQ("http://www.reddit.com/", *tree
->url
);
125 EXPECT_EQ(api::bookmarks::BOOKMARK_TREE_NODE_UNMODIFIABLE_NONE
,
129 TEST_F(ExtensionBookmarksTest
, GetManagedNode
) {
130 const BookmarkNode
* managed_bookmark
=
131 model_
->AddURL(managed_
->managed_node(),
133 base::ASCIIToUTF16("Chromium"),
134 GURL("http://www.chromium.org/"));
135 scoped_ptr
<BookmarkTreeNode
> tree(
136 GetBookmarkTreeNode(managed_
,
139 false)); // Only folders.
140 EXPECT_EQ("Chromium", tree
->title
);
141 EXPECT_EQ("http://www.chromium.org/", *tree
->url
);
142 EXPECT_EQ(api::bookmarks::BOOKMARK_TREE_NODE_UNMODIFIABLE_MANAGED
,
146 TEST_F(ExtensionBookmarksTest
, RemoveNodeInvalidId
) {
147 int64 invalid_id
= model_
->next_node_id();
149 EXPECT_FALSE(RemoveNode(model_
, managed_
, invalid_id
, true, &error
));
150 EXPECT_EQ(keys::kNoNodeError
, error
);
153 TEST_F(ExtensionBookmarksTest
, RemoveNodePermanent
) {
156 RemoveNode(model_
, managed_
, model_
->other_node()->id(), true, &error
));
157 EXPECT_EQ(keys::kModifySpecialError
, error
);
160 TEST_F(ExtensionBookmarksTest
, RemoveNodeManaged
) {
161 const BookmarkNode
* managed_bookmark
=
162 model_
->AddURL(managed_
->managed_node(),
164 base::ASCIIToUTF16("Chromium"),
165 GURL("http://www.chromium.org"));
168 RemoveNode(model_
, managed_
, managed_bookmark
->id(), true, &error
));
169 EXPECT_EQ(keys::kModifyManagedError
, error
);
172 TEST_F(ExtensionBookmarksTest
, RemoveNodeNotRecursive
) {
174 EXPECT_FALSE(RemoveNode(model_
, managed_
, folder_
->id(), false, &error
));
175 EXPECT_EQ(keys::kFolderNotEmptyError
, error
);
178 TEST_F(ExtensionBookmarksTest
, RemoveNodeRecursive
) {
179 EXPECT_EQ(3, model_
->other_node()->child_count());
181 EXPECT_TRUE(RemoveNode(model_
, managed_
, folder_
->id(), true, &error
));
182 EXPECT_EQ(2, model_
->other_node()->child_count());
185 TEST_F(ExtensionBookmarksTest
, GetMetaInfo
) {
186 base::DictionaryValue id_to_meta_info_map
;
187 GetMetaInfo(*model_
->other_node(), &id_to_meta_info_map
);
188 EXPECT_EQ(8u, id_to_meta_info_map
.size());
190 // Verify top level node.
191 const base::Value
* value
= NULL
;
192 EXPECT_TRUE(id_to_meta_info_map
.Get(
193 base::Int64ToString(model_
->other_node()->id()), &value
));
194 ASSERT_TRUE(NULL
!= value
);
195 const base::DictionaryValue
* dictionary_value
= NULL
;
196 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
197 ASSERT_TRUE(NULL
!= dictionary_value
);
198 EXPECT_EQ(0u, dictionary_value
->size());
200 // Verify bookmark with two meta info key/value pairs.
202 EXPECT_TRUE(id_to_meta_info_map
.Get(
203 base::Int64ToString(node_
->id()), &value
));
204 ASSERT_TRUE(NULL
!= value
);
205 dictionary_value
= NULL
;
206 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
207 ASSERT_TRUE(NULL
!= dictionary_value
);
208 EXPECT_EQ(2u, dictionary_value
->size());
209 std::string string_value
;
210 EXPECT_TRUE(dictionary_value
->GetString("some_key1", &string_value
));
211 EXPECT_EQ("some_value1", string_value
);
212 EXPECT_TRUE(dictionary_value
->GetString("some_key2", &string_value
));
213 EXPECT_EQ("some_value2", string_value
);
215 // Verify folder with one meta info key/value pair.
217 EXPECT_TRUE(id_to_meta_info_map
.Get(
218 base::Int64ToString(folder_
->id()), &value
));
219 ASSERT_TRUE(NULL
!= value
);
220 dictionary_value
= NULL
;
221 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
222 ASSERT_TRUE(NULL
!= dictionary_value
);
223 EXPECT_EQ(1u, dictionary_value
->size());
224 EXPECT_TRUE(dictionary_value
->GetString("some_key1", &string_value
));
225 EXPECT_EQ("some_value1", string_value
);
227 // Verify bookmark in a subfolder with one meta info key/value pairs.
229 EXPECT_TRUE(id_to_meta_info_map
.Get(
230 base::Int64ToString(node2_
->id()), &value
));
231 ASSERT_TRUE(NULL
!= value
);
232 dictionary_value
= NULL
;
233 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
234 ASSERT_TRUE(NULL
!= dictionary_value
);
235 EXPECT_EQ(1u, dictionary_value
->size());
236 string_value
.clear();
237 EXPECT_FALSE(dictionary_value
->GetString("some_key1", &string_value
));
238 EXPECT_EQ("", string_value
);
239 EXPECT_TRUE(dictionary_value
->GetString("some_key2", &string_value
));
240 EXPECT_EQ("some_value2", string_value
);
244 } // namespace bookmark_api_helpers
245 } // namespace extensions