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/chrome_bookmark_client.h"
13 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
14 #include "chrome/browser/extensions/api/bookmarks/bookmark_api_constants.h"
15 #include "chrome/common/extensions/api/bookmarks.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/bookmarks/browser/bookmark_model.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
;
24 namespace extensions
{
26 namespace keys
= bookmark_api_constants
;
27 using api::bookmarks::BookmarkTreeNode
;
29 namespace bookmark_api_helpers
{
31 class ExtensionBookmarksTest
: public testing::Test
{
33 ExtensionBookmarksTest()
34 : client_(NULL
), model_(NULL
), node_(NULL
), node2_(NULL
), folder_(NULL
) {}
36 void SetUp() override
{
37 profile_
.CreateBookmarkModel(false);
38 model_
= BookmarkModelFactory::GetForProfile(&profile_
);
39 client_
= ChromeBookmarkClientFactory::GetForProfile(&profile_
);
40 bookmarks::test::WaitForBookmarkModelToLoad(model_
);
42 node_
= model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("Digg"),
43 GURL("http://www.reddit.com"));
44 model_
->SetNodeMetaInfo(node_
, "some_key1", "some_value1");
45 model_
->SetNodeMetaInfo(node_
, "some_key2", "some_value2");
46 model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("News"),
47 GURL("http://www.foxnews.com"));
48 folder_
= model_
->AddFolder(
49 model_
->other_node(), 0, base::ASCIIToUTF16("outer folder"));
50 model_
->SetNodeMetaInfo(folder_
, "some_key1", "some_value1");
51 model_
->AddFolder(folder_
, 0, base::ASCIIToUTF16("inner folder 1"));
52 model_
->AddFolder(folder_
, 0, base::ASCIIToUTF16("inner folder 2"));
53 node2_
= model_
->AddURL(
54 folder_
, 0, base::ASCIIToUTF16("Digg"), GURL("http://reddit.com"));
55 model_
->SetNodeMetaInfo(node2_
, "some_key2", "some_value2");
57 folder_
, 0, base::ASCIIToUTF16("CNet"), GURL("http://cnet.com"));
60 content::TestBrowserThreadBundle thread_bundle_
;
61 TestingProfile profile_
;
62 ChromeBookmarkClient
* client_
;
63 BookmarkModel
* model_
;
64 const BookmarkNode
* node_
;
65 const BookmarkNode
* node2_
;
66 const BookmarkNode
* folder_
;
69 TEST_F(ExtensionBookmarksTest
, GetFullTreeFromRoot
) {
70 scoped_ptr
<BookmarkTreeNode
> tree(
71 GetBookmarkTreeNode(client_
,
74 false)); // Not only folders.
75 ASSERT_EQ(3U, tree
->children
->size());
78 TEST_F(ExtensionBookmarksTest
, GetFoldersOnlyFromRoot
) {
79 scoped_ptr
<BookmarkTreeNode
> tree(
80 GetBookmarkTreeNode(client_
,
83 true)); // Only folders.
84 ASSERT_EQ(1U, tree
->children
->size());
87 TEST_F(ExtensionBookmarksTest
, GetSubtree
) {
88 scoped_ptr
<BookmarkTreeNode
> tree(
89 GetBookmarkTreeNode(client_
,
92 false)); // Not only folders.
93 ASSERT_EQ(4U, tree
->children
->size());
94 linked_ptr
<BookmarkTreeNode
> digg
= tree
->children
->at(1);
95 ASSERT_TRUE(digg
.get());
96 ASSERT_EQ("Digg", digg
->title
);
99 TEST_F(ExtensionBookmarksTest
, GetSubtreeFoldersOnly
) {
100 scoped_ptr
<BookmarkTreeNode
> tree(
101 GetBookmarkTreeNode(client_
,
104 true)); // Only folders.
105 ASSERT_EQ(2U, tree
->children
->size());
106 linked_ptr
<BookmarkTreeNode
> inner_folder
= tree
->children
->at(1);
107 ASSERT_TRUE(inner_folder
.get());
108 ASSERT_EQ("inner folder 1", inner_folder
->title
);
111 TEST_F(ExtensionBookmarksTest
, GetModifiableNode
) {
112 scoped_ptr
<BookmarkTreeNode
> tree(
113 GetBookmarkTreeNode(client_
,
116 false)); // Only folders.
117 EXPECT_EQ("Digg", tree
->title
);
118 ASSERT_TRUE(tree
->url
);
119 EXPECT_EQ("http://www.reddit.com/", *tree
->url
);
120 EXPECT_EQ(BookmarkTreeNode::UNMODIFIABLE_NONE
, tree
->unmodifiable
);
123 TEST_F(ExtensionBookmarksTest
, GetManagedNode
) {
124 const BookmarkNode
* managed_bookmark
=
125 model_
->AddURL(client_
->managed_node(),
127 base::ASCIIToUTF16("Chromium"),
128 GURL("http://www.chromium.org/"));
129 scoped_ptr
<BookmarkTreeNode
> tree(
130 GetBookmarkTreeNode(client_
,
133 false)); // Only folders.
134 EXPECT_EQ("Chromium", tree
->title
);
135 EXPECT_EQ("http://www.chromium.org/", *tree
->url
);
136 EXPECT_EQ(BookmarkTreeNode::UNMODIFIABLE_MANAGED
, tree
->unmodifiable
);
139 TEST_F(ExtensionBookmarksTest
, RemoveNodeInvalidId
) {
140 int64 invalid_id
= model_
->next_node_id();
142 EXPECT_FALSE(RemoveNode(model_
, client_
, invalid_id
, true, &error
));
143 EXPECT_EQ(keys::kNoNodeError
, error
);
146 TEST_F(ExtensionBookmarksTest
, RemoveNodePermanent
) {
149 RemoveNode(model_
, client_
, model_
->other_node()->id(), true, &error
));
150 EXPECT_EQ(keys::kModifySpecialError
, error
);
153 TEST_F(ExtensionBookmarksTest
, RemoveNodeManaged
) {
154 const BookmarkNode
* managed_bookmark
=
155 model_
->AddURL(client_
->managed_node(),
157 base::ASCIIToUTF16("Chromium"),
158 GURL("http://www.chromium.org"));
161 RemoveNode(model_
, client_
, managed_bookmark
->id(), true, &error
));
162 EXPECT_EQ(keys::kModifyManagedError
, error
);
165 TEST_F(ExtensionBookmarksTest
, RemoveNodeNotRecursive
) {
167 EXPECT_FALSE(RemoveNode(model_
, client_
, folder_
->id(), false, &error
));
168 EXPECT_EQ(keys::kFolderNotEmptyError
, error
);
171 TEST_F(ExtensionBookmarksTest
, RemoveNodeRecursive
) {
172 EXPECT_EQ(3, model_
->other_node()->child_count());
174 EXPECT_TRUE(RemoveNode(model_
, client_
, folder_
->id(), true, &error
));
175 EXPECT_EQ(2, model_
->other_node()->child_count());
178 TEST_F(ExtensionBookmarksTest
, GetMetaInfo
) {
179 base::DictionaryValue id_to_meta_info_map
;
180 GetMetaInfo(*model_
->other_node(), &id_to_meta_info_map
);
181 EXPECT_EQ(8u, id_to_meta_info_map
.size());
183 // Verify top level node.
184 const base::Value
* value
= NULL
;
185 EXPECT_TRUE(id_to_meta_info_map
.Get(
186 base::Int64ToString(model_
->other_node()->id()), &value
));
187 ASSERT_TRUE(NULL
!= value
);
188 const base::DictionaryValue
* dictionary_value
= NULL
;
189 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
190 ASSERT_TRUE(NULL
!= dictionary_value
);
191 EXPECT_EQ(0u, dictionary_value
->size());
193 // Verify bookmark with two meta info key/value pairs.
195 EXPECT_TRUE(id_to_meta_info_map
.Get(
196 base::Int64ToString(node_
->id()), &value
));
197 ASSERT_TRUE(NULL
!= value
);
198 dictionary_value
= NULL
;
199 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
200 ASSERT_TRUE(NULL
!= dictionary_value
);
201 EXPECT_EQ(2u, dictionary_value
->size());
202 std::string string_value
;
203 EXPECT_TRUE(dictionary_value
->GetString("some_key1", &string_value
));
204 EXPECT_EQ("some_value1", string_value
);
205 EXPECT_TRUE(dictionary_value
->GetString("some_key2", &string_value
));
206 EXPECT_EQ("some_value2", string_value
);
208 // Verify folder with one meta info key/value pair.
210 EXPECT_TRUE(id_to_meta_info_map
.Get(
211 base::Int64ToString(folder_
->id()), &value
));
212 ASSERT_TRUE(NULL
!= value
);
213 dictionary_value
= NULL
;
214 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
215 ASSERT_TRUE(NULL
!= dictionary_value
);
216 EXPECT_EQ(1u, dictionary_value
->size());
217 EXPECT_TRUE(dictionary_value
->GetString("some_key1", &string_value
));
218 EXPECT_EQ("some_value1", string_value
);
220 // Verify bookmark in a subfolder with one meta info key/value pairs.
222 EXPECT_TRUE(id_to_meta_info_map
.Get(
223 base::Int64ToString(node2_
->id()), &value
));
224 ASSERT_TRUE(NULL
!= value
);
225 dictionary_value
= NULL
;
226 EXPECT_TRUE(value
->GetAsDictionary(&dictionary_value
));
227 ASSERT_TRUE(NULL
!= dictionary_value
);
228 EXPECT_EQ(1u, dictionary_value
->size());
229 string_value
.clear();
230 EXPECT_FALSE(dictionary_value
->GetString("some_key1", &string_value
));
231 EXPECT_EQ("", string_value
);
232 EXPECT_TRUE(dictionary_value
->GetString("some_key2", &string_value
));
233 EXPECT_EQ("some_value2", string_value
);
237 } // namespace bookmark_api_helpers
238 } // namespace extensions