OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/enhanced_bookmarks/metadata_accessor.h" |
| 6 |
| 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" |
| 13 |
| 14 namespace { |
| 15 |
| 16 using namespace image::collections; |
| 17 |
| 18 const std::string BOOKMARK_URL("http://example.com/index.html"); |
| 19 |
| 20 class MetadataAccessorTest : public testing::Test { |
| 21 public: |
| 22 MetadataAccessorTest() {} |
| 23 virtual ~MetadataAccessorTest() {} |
| 24 |
| 25 protected: |
| 26 DISALLOW_COPY_AND_ASSIGN(MetadataAccessorTest); |
| 27 |
| 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(), |
| 33 0, // index. |
| 34 base::ASCIIToUTF16(name), |
| 35 GURL(BOOKMARK_URL)); |
| 36 } |
| 37 }; |
| 38 |
| 39 TEST_F(MetadataAccessorTest, TestEmptySnippet) { |
| 40 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 41 |
| 42 std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
| 43 CHECK_EQ(snippet, ""); |
| 44 }; |
| 45 |
| 46 TEST_F(MetadataAccessorTest, TestSnippet) { |
| 47 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 48 |
| 49 // Binary serialize the protobuf. |
| 50 PageData data; |
| 51 data.set_snippet("I'm happy!"); |
| 52 ASSERT_TRUE(data.IsInitialized()); |
| 53 std::string output; |
| 54 bool result = data.SerializeToString(&output); |
| 55 ASSERT_TRUE(result); |
| 56 |
| 57 // base64 encode the output. |
| 58 std::string encoded; |
| 59 base::Base64Encode(output, &encoded); |
| 60 node->SetMetaInfo(enhanced_bookmarks::kPageDataKey, encoded); |
| 61 |
| 62 std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
| 63 CHECK_EQ(snippet, "I'm happy!"); |
| 64 }; |
| 65 |
| 66 TEST_F(MetadataAccessorTest, TestBadEncodingSnippet) { |
| 67 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 68 |
| 69 // Binary serialize the protobuf. |
| 70 PageData data; |
| 71 data.set_snippet("You are happy!"); |
| 72 ASSERT_TRUE(data.IsInitialized()); |
| 73 std::string output; |
| 74 bool result = data.SerializeToString(&output); |
| 75 ASSERT_TRUE(result); |
| 76 |
| 77 // don't base 64 encode the output. |
| 78 node->SetMetaInfo(enhanced_bookmarks::kPageDataKey, output); |
| 79 |
| 80 std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
| 81 CHECK_EQ(snippet, ""); |
| 82 }; |
| 83 |
| 84 TEST_F(MetadataAccessorTest, TestOriginalImage) { |
| 85 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 86 |
| 87 ImageData data; |
| 88 // Intentionally make raw pointer. |
| 89 ImageData_ImageInfo* info = new ImageData_ImageInfo; |
| 90 info->set_url("http://example.com/foobar"); |
| 91 info->set_width(15); |
| 92 info->set_height(55); |
| 93 // This method consumes the pointer. |
| 94 data.set_allocated_original_info(info); |
| 95 |
| 96 std::string output; |
| 97 bool result = data.SerializePartialToString(&output); |
| 98 ASSERT_TRUE(result); |
| 99 |
| 100 // base64 encode the output. |
| 101 std::string encoded; |
| 102 base::Base64Encode(output, &encoded); |
| 103 node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
| 104 |
| 105 GURL url; |
| 106 int width; |
| 107 int height; |
| 108 result = enhanced_bookmarks::OriginalImageFromBookmark( |
| 109 node.get(), &url, &width, &height); |
| 110 ASSERT_TRUE(result); |
| 111 CHECK_EQ(url, GURL("http://example.com/foobar")); |
| 112 CHECK_EQ(width, 15); |
| 113 CHECK_EQ(height, 55); |
| 114 }; |
| 115 |
| 116 TEST_F(MetadataAccessorTest, TestThumbnailImage) { |
| 117 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 118 |
| 119 ImageData data; |
| 120 // Intentionally make raw pointer. |
| 121 ImageData_ImageInfo* info = new ImageData_ImageInfo; |
| 122 info->set_url("http://example.com/foobar"); |
| 123 info->set_width(15); |
| 124 info->set_height(55); |
| 125 // This method consumes the pointer. |
| 126 data.set_allocated_thumbnail_info(info); |
| 127 |
| 128 std::string output; |
| 129 bool result = data.SerializePartialToString(&output); |
| 130 ASSERT_TRUE(result); |
| 131 |
| 132 // base64 encode the output. |
| 133 std::string encoded; |
| 134 base::Base64Encode(output, &encoded); |
| 135 node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
| 136 |
| 137 GURL url; |
| 138 int width; |
| 139 int height; |
| 140 result = enhanced_bookmarks::ThumbnailImageFromBookmark( |
| 141 node.get(), &url, &width, &height); |
| 142 ASSERT_TRUE(result); |
| 143 CHECK_EQ(url, GURL("http://example.com/foobar")); |
| 144 CHECK_EQ(width, 15); |
| 145 CHECK_EQ(height, 55); |
| 146 }; |
| 147 |
| 148 TEST_F(MetadataAccessorTest, TestOriginalImageMissingDimensions) { |
| 149 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 150 |
| 151 ImageData data; |
| 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); |
| 157 |
| 158 std::string output; |
| 159 bool result = data.SerializePartialToString(&output); |
| 160 ASSERT_TRUE(result); |
| 161 |
| 162 // base64 encode the output. |
| 163 std::string encoded; |
| 164 base::Base64Encode(output, &encoded); |
| 165 node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
| 166 |
| 167 GURL url; |
| 168 int width; |
| 169 int height; |
| 170 result = enhanced_bookmarks::OriginalImageFromBookmark( |
| 171 node.get(), &url, &width, &height); |
| 172 ASSERT_FALSE(result); |
| 173 }; |
| 174 |
| 175 TEST_F(MetadataAccessorTest, TestOriginalImageBadUrl) { |
| 176 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 177 |
| 178 ImageData data; |
| 179 // Intentionally make raw pointer. |
| 180 ImageData_ImageInfo* info = new ImageData_ImageInfo; |
| 181 info->set_url("asdf. 13r"); |
| 182 info->set_width(15); |
| 183 info->set_height(55); |
| 184 // This method consumes the pointer. |
| 185 data.set_allocated_original_info(info); |
| 186 |
| 187 std::string output; |
| 188 bool result = data.SerializePartialToString(&output); |
| 189 ASSERT_TRUE(result); |
| 190 |
| 191 // base64 encode the output. |
| 192 std::string encoded; |
| 193 base::Base64Encode(output, &encoded); |
| 194 node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
| 195 |
| 196 GURL url; |
| 197 int width; |
| 198 int height; |
| 199 result = enhanced_bookmarks::OriginalImageFromBookmark( |
| 200 node.get(), &url, &width, &height); |
| 201 ASSERT_FALSE(result); |
| 202 }; |
| 203 |
| 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(), |
| 209 0, // index. |
| 210 base::ASCIIToUTF16("whatever"), |
| 211 GURL(BOOKMARK_URL)); |
| 212 |
| 213 bool result = enhanced_bookmarks::SetOriginalImageForBookmark( |
| 214 bookmark_model.get(), node, GURL("http://example.com/i.jpg"), 22, 33); |
| 215 ASSERT_TRUE(result); |
| 216 |
| 217 GURL url; |
| 218 int width; |
| 219 int height; |
| 220 result = enhanced_bookmarks::OriginalImageFromBookmark( |
| 221 node, &url, &width, &height); |
| 222 ASSERT_TRUE(result); |
| 223 CHECK_EQ(url, GURL("http://example.com/i.jpg")); |
| 224 CHECK_EQ(width, 22); |
| 225 CHECK_EQ(height, 33); |
| 226 }; |
| 227 |
| 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(), |
| 233 0, // index. |
| 234 base::ASCIIToUTF16("whatever"), |
| 235 GURL(BOOKMARK_URL)); |
| 236 |
| 237 // Encode some information. |
| 238 bool result = enhanced_bookmarks::SetOriginalImageForBookmark( |
| 239 bookmark_model.get(), node, GURL("http://example.com/i.jpg"), 22, 33); |
| 240 ASSERT_TRUE(result); |
| 241 // Encode some different information. |
| 242 result = enhanced_bookmarks::SetOriginalImageForBookmark( |
| 243 bookmark_model.get(), node, GURL("http://example.com/i.jpg"), 33, 44); |
| 244 ASSERT_TRUE(result); |
| 245 |
| 246 GURL url; |
| 247 int width; |
| 248 int height; |
| 249 result = enhanced_bookmarks::OriginalImageFromBookmark( |
| 250 node, &url, &width, &height); |
| 251 ASSERT_TRUE(result); |
| 252 CHECK_EQ(url, GURL("http://example.com/i.jpg")); |
| 253 CHECK_EQ(width, 33); |
| 254 CHECK_EQ(height, 44); |
| 255 }; |
| 256 |
| 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(), |
| 262 0, // index. |
| 263 base::ASCIIToUTF16("whatever"), |
| 264 GURL(BOOKMARK_URL)); |
| 265 |
| 266 // Encode some information. |
| 267 ASSERT_TRUE(enhanced_bookmarks::SetAllImagesForBookmark( |
| 268 bookmark_model.get(), |
| 269 node, |
| 270 GURL(), |
| 271 0, |
| 272 0, |
| 273 GURL("http://google.com/img/thumb.jpg"), |
| 274 33, |
| 275 44)); |
| 276 GURL url; |
| 277 int width; |
| 278 int height; |
| 279 bool result = enhanced_bookmarks::ThumbnailImageFromBookmark( |
| 280 node, &url, &width, &height); |
| 281 ASSERT_TRUE(result); |
| 282 CHECK_EQ(url, GURL("http://google.com/img/thumb.jpg")); |
| 283 CHECK_EQ(width, 33); |
| 284 CHECK_EQ(height, 44); |
| 285 }; |
| 286 |
| 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"); |
| 291 |
| 292 // First call creates the UUID, second call should return the same. |
| 293 ASSERT_EQ( |
| 294 enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model.get(), node), |
| 295 enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model.get(), node)); |
| 296 } |
| 297 |
| 298 TEST_F(MetadataAccessorTest, TestEmptyDescription) { |
| 299 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
| 300 |
| 301 std::string description( |
| 302 enhanced_bookmarks::DescriptionFromBookmark(node.get())); |
| 303 CHECK_EQ(description, ""); |
| 304 } |
| 305 |
| 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."); |
| 311 |
| 312 // Set the description. |
| 313 enhanced_bookmarks::SetDescriptionForBookmark( |
| 314 bookmark_model.get(), node.get(), description); |
| 315 |
| 316 // Check the description is the one that was set. |
| 317 CHECK_EQ(enhanced_bookmarks::DescriptionFromBookmark(node.get()), |
| 318 description); |
| 319 } |
| 320 |
| 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))); |
| 326 |
| 327 // Binary serialize the protobuf. |
| 328 PageData data; |
| 329 data.set_snippet("Joe Bar Team"); |
| 330 ASSERT_TRUE(data.IsInitialized()); |
| 331 std::string output; |
| 332 bool result = data.SerializeToString(&output); |
| 333 ASSERT_TRUE(result); |
| 334 |
| 335 // base64 encode the output. |
| 336 std::string encoded; |
| 337 base::Base64Encode(output, &encoded); |
| 338 node->SetMetaInfo(enhanced_bookmarks::kPageDataKey, encoded); |
| 339 |
| 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())); |
| 344 |
| 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); |
| 349 |
| 350 // Check the description is the one that was set. |
| 351 CHECK_EQ(enhanced_bookmarks::DescriptionFromBookmark(node.get()), |
| 352 description); |
| 353 } |
| 354 } // namespace |
OLD | NEW |