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/enhanced_bookmarks/metadata_accessor.h"
7 #include "base/base64.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/bookmarks/browser/bookmark_model.h"
10 #include "components/bookmarks/test/test_bookmark_client.h"
11 #include "components/enhanced_bookmarks/proto/metadata.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 using namespace image::collections
;
18 const std::string
BOOKMARK_URL("http://example.com/index.html");
20 class MetadataAccessorTest
: public testing::Test
{
22 MetadataAccessorTest() {}
23 virtual ~MetadataAccessorTest() {}
26 DISALLOW_COPY_AND_ASSIGN(MetadataAccessorTest
);
28 // Adds a bookmark as the subnode at index 0 to other_node.
29 // |name| should be ASCII encoded.
30 // Returns the newly added bookmark.
31 const BookmarkNode
* AddBookmark(BookmarkModel
* model
, std::string name
) {
32 return model
->AddURL(model
->other_node(),
34 base::ASCIIToUTF16(name
),
39 TEST_F(MetadataAccessorTest
, TestEmptySnippet
) {
40 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
42 std::string
snippet(enhanced_bookmarks::SnippetFromBookmark(node
.get()));
43 CHECK_EQ(snippet
, "");
46 TEST_F(MetadataAccessorTest
, TestSnippet
) {
47 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
49 // Binary serialize the protobuf.
51 data
.set_snippet("I'm happy!");
52 ASSERT_TRUE(data
.IsInitialized());
54 bool result
= data
.SerializeToString(&output
);
57 // base64 encode the output.
59 base::Base64Encode(output
, &encoded
);
60 node
->SetMetaInfo(enhanced_bookmarks::kPageDataKey
, encoded
);
62 std::string
snippet(enhanced_bookmarks::SnippetFromBookmark(node
.get()));
63 CHECK_EQ(snippet
, "I'm happy!");
66 TEST_F(MetadataAccessorTest
, TestBadEncodingSnippet
) {
67 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
69 // Binary serialize the protobuf.
71 data
.set_snippet("You are happy!");
72 ASSERT_TRUE(data
.IsInitialized());
74 bool result
= data
.SerializeToString(&output
);
77 // don't base 64 encode the output.
78 node
->SetMetaInfo(enhanced_bookmarks::kPageDataKey
, output
);
80 std::string
snippet(enhanced_bookmarks::SnippetFromBookmark(node
.get()));
81 CHECK_EQ(snippet
, "");
84 TEST_F(MetadataAccessorTest
, TestOriginalImage
) {
85 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
88 // Intentionally make raw pointer.
89 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
90 info
->set_url("http://example.com/foobar");
93 // This method consumes the pointer.
94 data
.set_allocated_original_info(info
);
97 bool result
= data
.SerializePartialToString(&output
);
100 // base64 encode the output.
102 base::Base64Encode(output
, &encoded
);
103 node
->SetMetaInfo(enhanced_bookmarks::kImageDataKey
, encoded
);
108 result
= enhanced_bookmarks::OriginalImageFromBookmark(
109 node
.get(), &url
, &width
, &height
);
111 CHECK_EQ(url
, GURL("http://example.com/foobar"));
113 CHECK_EQ(height
, 55);
116 TEST_F(MetadataAccessorTest
, TestThumbnailImage
) {
117 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
120 // Intentionally make raw pointer.
121 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
122 info
->set_url("http://example.com/foobar");
124 info
->set_height(55);
125 // This method consumes the pointer.
126 data
.set_allocated_thumbnail_info(info
);
129 bool result
= data
.SerializePartialToString(&output
);
132 // base64 encode the output.
134 base::Base64Encode(output
, &encoded
);
135 node
->SetMetaInfo(enhanced_bookmarks::kImageDataKey
, encoded
);
140 result
= enhanced_bookmarks::ThumbnailImageFromBookmark(
141 node
.get(), &url
, &width
, &height
);
143 CHECK_EQ(url
, GURL("http://example.com/foobar"));
145 CHECK_EQ(height
, 55);
148 TEST_F(MetadataAccessorTest
, TestOriginalImageMissingDimensions
) {
149 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
152 // Intentionally make raw pointer.
153 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
154 info
->set_url("http://example.com/foobar");
155 // This method consumes the pointer.
156 data
.set_allocated_original_info(info
);
159 bool result
= data
.SerializePartialToString(&output
);
162 // base64 encode the output.
164 base::Base64Encode(output
, &encoded
);
165 node
->SetMetaInfo(enhanced_bookmarks::kImageDataKey
, encoded
);
170 result
= enhanced_bookmarks::OriginalImageFromBookmark(
171 node
.get(), &url
, &width
, &height
);
172 ASSERT_FALSE(result
);
175 TEST_F(MetadataAccessorTest
, TestOriginalImageBadUrl
) {
176 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
179 // Intentionally make raw pointer.
180 ImageData_ImageInfo
* info
= new ImageData_ImageInfo
;
181 info
->set_url("asdf. 13r");
183 info
->set_height(55);
184 // This method consumes the pointer.
185 data
.set_allocated_original_info(info
);
188 bool result
= data
.SerializePartialToString(&output
);
191 // base64 encode the output.
193 base::Base64Encode(output
, &encoded
);
194 node
->SetMetaInfo(enhanced_bookmarks::kImageDataKey
, encoded
);
199 result
= enhanced_bookmarks::OriginalImageFromBookmark(
200 node
.get(), &url
, &width
, &height
);
201 ASSERT_FALSE(result
);
204 TEST_F(MetadataAccessorTest
, TestEncodeDecode
) {
205 test::TestBookmarkClient bookmark_client
;
206 scoped_ptr
<BookmarkModel
> bookmark_model(bookmark_client
.CreateModel(false));
207 const BookmarkNode
* node
=
208 bookmark_model
->AddURL(bookmark_model
->other_node(),
210 base::ASCIIToUTF16("whatever"),
213 bool result
= enhanced_bookmarks::SetOriginalImageForBookmark(
214 bookmark_model
.get(), node
, GURL("http://example.com/i.jpg"), 22, 33);
220 result
= enhanced_bookmarks::OriginalImageFromBookmark(
221 node
, &url
, &width
, &height
);
223 CHECK_EQ(url
, GURL("http://example.com/i.jpg"));
225 CHECK_EQ(height
, 33);
228 TEST_F(MetadataAccessorTest
, TestDoubleEncodeDecode
) {
229 test::TestBookmarkClient bookmark_client
;
230 scoped_ptr
<BookmarkModel
> bookmark_model(bookmark_client
.CreateModel(false));
231 const BookmarkNode
* node
=
232 bookmark_model
->AddURL(bookmark_model
->other_node(),
234 base::ASCIIToUTF16("whatever"),
237 // Encode some information.
238 bool result
= enhanced_bookmarks::SetOriginalImageForBookmark(
239 bookmark_model
.get(), node
, GURL("http://example.com/i.jpg"), 22, 33);
241 // Encode some different information.
242 result
= enhanced_bookmarks::SetOriginalImageForBookmark(
243 bookmark_model
.get(), node
, GURL("http://example.com/i.jpg"), 33, 44);
249 result
= enhanced_bookmarks::OriginalImageFromBookmark(
250 node
, &url
, &width
, &height
);
252 CHECK_EQ(url
, GURL("http://example.com/i.jpg"));
254 CHECK_EQ(height
, 44);
257 TEST_F(MetadataAccessorTest
, TestThumbnail
) {
258 test::TestBookmarkClient bookmark_client
;
259 scoped_ptr
<BookmarkModel
> bookmark_model(bookmark_client
.CreateModel(false));
260 const BookmarkNode
* node
=
261 bookmark_model
->AddURL(bookmark_model
->other_node(),
263 base::ASCIIToUTF16("whatever"),
266 // Encode some information.
267 ASSERT_TRUE(enhanced_bookmarks::SetAllImagesForBookmark(
268 bookmark_model
.get(),
273 GURL("http://google.com/img/thumb.jpg"),
279 bool result
= enhanced_bookmarks::ThumbnailImageFromBookmark(
280 node
, &url
, &width
, &height
);
282 CHECK_EQ(url
, GURL("http://google.com/img/thumb.jpg"));
284 CHECK_EQ(height
, 44);
287 TEST_F(MetadataAccessorTest
, TestRemoteId
) {
288 test::TestBookmarkClient bookmark_client
;
289 scoped_ptr
<BookmarkModel
> bookmark_model(bookmark_client
.CreateModel(false));
290 const BookmarkNode
* node
= AddBookmark(bookmark_model
.get(), "Aga Khan");
292 // First call creates the UUID, second call should return the same.
294 enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model
.get(), node
),
295 enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model
.get(), node
));
298 TEST_F(MetadataAccessorTest
, TestEmptyDescription
) {
299 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
301 std::string
description(
302 enhanced_bookmarks::DescriptionFromBookmark(node
.get()));
303 CHECK_EQ(description
, "");
306 TEST_F(MetadataAccessorTest
, TestDescription
) {
307 test::TestBookmarkClient bookmark_client
;
308 scoped_ptr
<BookmarkModel
> bookmark_model(bookmark_client
.CreateModel(false));
309 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
310 const std::string
description("This is the most useful description of all.");
312 // Set the description.
313 enhanced_bookmarks::SetDescriptionForBookmark(
314 bookmark_model
.get(), node
.get(), description
);
316 // Check the description is the one that was set.
317 CHECK_EQ(enhanced_bookmarks::DescriptionFromBookmark(node
.get()),
321 // If there is no notes field, the description should fall back on the snippet.
322 TEST_F(MetadataAccessorTest
, TestDescriptionFallback
) {
323 test::TestBookmarkClient bookmark_client
;
324 scoped_ptr
<BookmarkModel
> bookmark_model(bookmark_client
.CreateModel(false));
325 scoped_ptr
<BookmarkNode
> node(new BookmarkNode(GURL(BOOKMARK_URL
)));
327 // Binary serialize the protobuf.
329 data
.set_snippet("Joe Bar Team");
330 ASSERT_TRUE(data
.IsInitialized());
332 bool result
= data
.SerializeToString(&output
);
335 // base64 encode the output.
337 base::Base64Encode(output
, &encoded
);
338 node
->SetMetaInfo(enhanced_bookmarks::kPageDataKey
, encoded
);
340 // The snippet is used as the description.
341 std::string
snippet(enhanced_bookmarks::SnippetFromBookmark(node
.get()));
342 CHECK_EQ("Joe Bar Team",
343 enhanced_bookmarks::DescriptionFromBookmark(node
.get()));
345 // Set the description.
346 const std::string
description("This is the most useful description of all.");
347 enhanced_bookmarks::SetDescriptionForBookmark(
348 bookmark_model
.get(), node
.get(), description
);
350 // Check the description is the one that was set.
351 CHECK_EQ(enhanced_bookmarks::DescriptionFromBookmark(node
.get()),