Index: components/enhanced_bookmarks/enhanced_bookmark_model_unittest.cc |
diff --git a/components/enhanced_bookmarks/metadata_accessor_unittest.cc b/components/enhanced_bookmarks/enhanced_bookmark_model_unittest.cc |
similarity index 35% |
rename from components/enhanced_bookmarks/metadata_accessor_unittest.cc |
rename to components/enhanced_bookmarks/enhanced_bookmark_model_unittest.cc |
index 3ce02eaf48c38ac595ead388d3b1f5c5eddfce2e..76940839340462bf4d8d621c772accbc4aedd2d0 100644 |
--- a/components/enhanced_bookmarks/metadata_accessor_unittest.cc |
+++ b/components/enhanced_bookmarks/enhanced_bookmark_model_unittest.cc |
@@ -2,52 +2,83 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "components/enhanced_bookmarks/metadata_accessor.h" |
+#include "components/enhanced_bookmarks/enhanced_bookmark_model.h" |
#include "base/base64.h" |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/strings/string_util.h" |
#include "base/strings/utf_string_conversions.h" |
#include "components/bookmarks/browser/bookmark_model.h" |
+#include "components/bookmarks/browser/bookmark_node.h" |
#include "components/bookmarks/test/test_bookmark_client.h" |
#include "components/enhanced_bookmarks/proto/metadata.pb.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
namespace { |
-using namespace image::collections; |
- |
const std::string BOOKMARK_URL("http://example.com/index.html"); |
-class MetadataAccessorTest : public testing::Test { |
+class EnhancedBookmarkModelTest : public testing::Test { |
public: |
- MetadataAccessorTest() {} |
- virtual ~MetadataAccessorTest() {} |
+ EnhancedBookmarkModelTest() {} |
+ virtual ~EnhancedBookmarkModelTest() {} |
+ |
+ virtual void SetUp() OVERRIDE { |
+ test::TestBookmarkClient bookmark_client; |
+ bookmark_model_.reset(bookmark_client.CreateModel().release()); |
+ model_.reset( |
+ new enhanced_bookmarks::EnhancedBookmarkModel(bookmark_model_.get())); |
+ model_->Initialize("v1.0"); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ model_.reset(); |
+ bookmark_model_.reset(); |
+ } |
protected: |
- DISALLOW_COPY_AND_ASSIGN(MetadataAccessorTest); |
- |
- // Adds a bookmark as the subnode at index 0 to other_node. |
- // |name| should be ASCII encoded. |
- // Returns the newly added bookmark. |
- const BookmarkNode* AddBookmark(BookmarkModel* model, std::string name) { |
- return model->AddURL(model->other_node(), |
- 0, // index. |
- base::ASCIIToUTF16(name), |
- GURL(BOOKMARK_URL)); |
+ const BookmarkNode* AddBookmark() { |
+ return AddBookmark("Some title", bookmark_model_->other_node()); |
+ } |
+ |
+ const BookmarkNode* AddFolder() { |
+ return AddFolder("Some title", bookmark_model_->other_node()); |
} |
+ |
+ const BookmarkNode* AddBookmark(const std::string& name, |
+ const BookmarkNode* parent) { |
+ return bookmark_model_->AddURL(parent, |
+ 0, // index. |
+ base::ASCIIToUTF16(name), |
+ GURL(BOOKMARK_URL)); |
+ } |
+ |
+ const BookmarkNode* AddFolder(const std::string& name, |
+ const BookmarkNode* parent) { |
+ return bookmark_model_->AddFolder(parent, 0, base::ASCIIToUTF16(name)); |
+ } |
+ |
+ scoped_ptr<BookmarkModel> bookmark_model_; |
+ scoped_ptr<enhanced_bookmarks::EnhancedBookmarkModel> model_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(EnhancedBookmarkModelTest); |
}; |
-TEST_F(MetadataAccessorTest, TestEmptySnippet) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestEmptySnippet) { |
+ const BookmarkNode* node = AddBookmark(); |
- std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
- CHECK_EQ(snippet, ""); |
+ std::string snippet(model_->GetSnippetForNode(node)); |
+ EXPECT_EQ(snippet, ""); |
}; |
-TEST_F(MetadataAccessorTest, TestSnippet) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestSnippet) { |
+ const BookmarkNode* node = AddBookmark(); |
// Binary serialize the protobuf. |
- PageData data; |
+ image::collections::PageData data; |
data.set_snippet("I'm happy!"); |
ASSERT_TRUE(data.IsInitialized()); |
std::string output; |
@@ -57,17 +88,17 @@ TEST_F(MetadataAccessorTest, TestSnippet) { |
// base64 encode the output. |
std::string encoded; |
base::Base64Encode(output, &encoded); |
- node->SetMetaInfo(enhanced_bookmarks::kPageDataKey, encoded); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.pageData", encoded); |
- std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
- CHECK_EQ(snippet, "I'm happy!"); |
-}; |
+ std::string snippet(model_->GetSnippetForNode(node)); |
+ EXPECT_EQ(snippet, "I'm happy!"); |
+} |
-TEST_F(MetadataAccessorTest, TestBadEncodingSnippet) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestBadEncodingSnippet) { |
+ const BookmarkNode* node = AddBookmark(); |
// Binary serialize the protobuf. |
- PageData data; |
+ image::collections::PageData data; |
data.set_snippet("You are happy!"); |
ASSERT_TRUE(data.IsInitialized()); |
std::string output; |
@@ -75,18 +106,19 @@ TEST_F(MetadataAccessorTest, TestBadEncodingSnippet) { |
ASSERT_TRUE(result); |
// don't base 64 encode the output. |
- node->SetMetaInfo(enhanced_bookmarks::kPageDataKey, output); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.pageData", output); |
- std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
- CHECK_EQ(snippet, ""); |
-}; |
+ std::string snippet(model_->GetSnippetForNode(node)); |
+ EXPECT_EQ(snippet, ""); |
+} |
-TEST_F(MetadataAccessorTest, TestOriginalImage) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestOriginalImage) { |
+ const BookmarkNode* node = AddBookmark(); |
- ImageData data; |
+ image::collections::ImageData data; |
// Intentionally make raw pointer. |
- ImageData_ImageInfo* info = new ImageData_ImageInfo; |
+ image::collections::ImageData_ImageInfo* info = |
+ new image::collections::ImageData_ImageInfo; |
info->set_url("http://example.com/foobar"); |
info->set_width(15); |
info->set_height(55); |
@@ -100,25 +132,25 @@ TEST_F(MetadataAccessorTest, TestOriginalImage) { |
// base64 encode the output. |
std::string encoded; |
base::Base64Encode(output, &encoded); |
- node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded); |
GURL url; |
int width; |
int height; |
- result = enhanced_bookmarks::OriginalImageFromBookmark( |
- node.get(), &url, &width, &height); |
+ result = model_->GetOriginalImageForNode(node, &url, &width, &height); |
ASSERT_TRUE(result); |
- CHECK_EQ(url, GURL("http://example.com/foobar")); |
- CHECK_EQ(width, 15); |
- CHECK_EQ(height, 55); |
-}; |
+ EXPECT_EQ(url, GURL("http://example.com/foobar")); |
+ EXPECT_EQ(width, 15); |
+ EXPECT_EQ(height, 55); |
+} |
-TEST_F(MetadataAccessorTest, TestThumbnailImage) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestThumbnailImage) { |
+ const BookmarkNode* node = AddBookmark(); |
- ImageData data; |
+ image::collections::ImageData data; |
// Intentionally make raw pointer. |
- ImageData_ImageInfo* info = new ImageData_ImageInfo; |
+ image::collections::ImageData_ImageInfo* info = |
+ new image::collections::ImageData_ImageInfo; |
info->set_url("http://example.com/foobar"); |
info->set_width(15); |
info->set_height(55); |
@@ -132,25 +164,25 @@ TEST_F(MetadataAccessorTest, TestThumbnailImage) { |
// base64 encode the output. |
std::string encoded; |
base::Base64Encode(output, &encoded); |
- node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded); |
GURL url; |
int width; |
int height; |
- result = enhanced_bookmarks::ThumbnailImageFromBookmark( |
- node.get(), &url, &width, &height); |
+ result = model_->GetThumbnailImageForNode(node, &url, &width, &height); |
ASSERT_TRUE(result); |
- CHECK_EQ(url, GURL("http://example.com/foobar")); |
- CHECK_EQ(width, 15); |
- CHECK_EQ(height, 55); |
-}; |
+ EXPECT_EQ(url, GURL("http://example.com/foobar")); |
+ EXPECT_EQ(width, 15); |
+ EXPECT_EQ(height, 55); |
+} |
-TEST_F(MetadataAccessorTest, TestOriginalImageMissingDimensions) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestOriginalImageMissingDimensions) { |
+ const BookmarkNode* node = AddBookmark(); |
- ImageData data; |
+ image::collections::ImageData data; |
// Intentionally make raw pointer. |
- ImageData_ImageInfo* info = new ImageData_ImageInfo; |
+ image::collections::ImageData_ImageInfo* info = |
+ new image::collections::ImageData_ImageInfo; |
info->set_url("http://example.com/foobar"); |
// This method consumes the pointer. |
data.set_allocated_original_info(info); |
@@ -162,22 +194,22 @@ TEST_F(MetadataAccessorTest, TestOriginalImageMissingDimensions) { |
// base64 encode the output. |
std::string encoded; |
base::Base64Encode(output, &encoded); |
- node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded); |
GURL url; |
int width; |
int height; |
- result = enhanced_bookmarks::OriginalImageFromBookmark( |
- node.get(), &url, &width, &height); |
+ result = model_->GetOriginalImageForNode(node, &url, &width, &height); |
ASSERT_FALSE(result); |
-}; |
+} |
-TEST_F(MetadataAccessorTest, TestOriginalImageBadUrl) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestOriginalImageBadUrl) { |
+ const BookmarkNode* node = AddBookmark(); |
- ImageData data; |
+ image::collections::ImageData data; |
// Intentionally make raw pointer. |
- ImageData_ImageInfo* info = new ImageData_ImageInfo; |
+ image::collections::ImageData_ImageInfo* info = |
+ new image::collections::ImageData_ImageInfo; |
info->set_url("asdf. 13r"); |
info->set_width(15); |
info->set_height(55); |
@@ -191,141 +223,99 @@ TEST_F(MetadataAccessorTest, TestOriginalImageBadUrl) { |
// base64 encode the output. |
std::string encoded; |
base::Base64Encode(output, &encoded); |
- node->SetMetaInfo(enhanced_bookmarks::kImageDataKey, encoded); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.imageData", encoded); |
GURL url; |
int width; |
int height; |
- result = enhanced_bookmarks::OriginalImageFromBookmark( |
- node.get(), &url, &width, &height); |
+ result = model_->GetOriginalImageForNode(node, &url, &width, &height); |
ASSERT_FALSE(result); |
-}; |
+} |
+ |
+TEST_F(EnhancedBookmarkModelTest, TestEncodeDecode) { |
+ const BookmarkNode* node = AddBookmark(); |
-TEST_F(MetadataAccessorTest, TestEncodeDecode) { |
- test::TestBookmarkClient bookmark_client; |
- scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel()); |
- const BookmarkNode* node = |
- bookmark_model->AddURL(bookmark_model->other_node(), |
- 0, // index. |
- base::ASCIIToUTF16("whatever"), |
- GURL(BOOKMARK_URL)); |
- |
- bool result = enhanced_bookmarks::SetOriginalImageForBookmark( |
- bookmark_model.get(), node, GURL("http://example.com/i.jpg"), 22, 33); |
+ bool result = model_->SetOriginalImageForNode( |
+ node, GURL("http://example.com/i.jpg"), 22, 33); |
ASSERT_TRUE(result); |
GURL url; |
int width; |
int height; |
- result = enhanced_bookmarks::OriginalImageFromBookmark( |
- node, &url, &width, &height); |
+ result = model_->GetOriginalImageForNode(node, &url, &width, &height); |
ASSERT_TRUE(result); |
- CHECK_EQ(url, GURL("http://example.com/i.jpg")); |
- CHECK_EQ(width, 22); |
- CHECK_EQ(height, 33); |
-}; |
+ EXPECT_EQ(url, GURL("http://example.com/i.jpg")); |
+ EXPECT_EQ(width, 22); |
+ EXPECT_EQ(height, 33); |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(node)); |
+} |
-TEST_F(MetadataAccessorTest, TestDoubleEncodeDecode) { |
- test::TestBookmarkClient bookmark_client; |
- scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel()); |
- const BookmarkNode* node = |
- bookmark_model->AddURL(bookmark_model->other_node(), |
- 0, // index. |
- base::ASCIIToUTF16("whatever"), |
- GURL(BOOKMARK_URL)); |
+TEST_F(EnhancedBookmarkModelTest, TestDoubleEncodeDecode) { |
+ const BookmarkNode* node = AddBookmark(); |
// Encode some information. |
- bool result = enhanced_bookmarks::SetOriginalImageForBookmark( |
- bookmark_model.get(), node, GURL("http://example.com/i.jpg"), 22, 33); |
+ bool result = model_->SetOriginalImageForNode( |
+ node, GURL("http://example.com/i.jpg"), 22, 33); |
ASSERT_TRUE(result); |
// Encode some different information. |
- result = enhanced_bookmarks::SetOriginalImageForBookmark( |
- bookmark_model.get(), node, GURL("http://example.com/i.jpg"), 33, 44); |
+ result = model_->SetOriginalImageForNode( |
+ node, GURL("http://example.com/i.jpg"), 33, 44); |
ASSERT_TRUE(result); |
GURL url; |
int width; |
int height; |
- result = enhanced_bookmarks::OriginalImageFromBookmark( |
- node, &url, &width, &height); |
+ result = model_->GetOriginalImageForNode(node, &url, &width, &height); |
ASSERT_TRUE(result); |
- CHECK_EQ(url, GURL("http://example.com/i.jpg")); |
- CHECK_EQ(width, 33); |
- CHECK_EQ(height, 44); |
-}; |
+ EXPECT_EQ(url, GURL("http://example.com/i.jpg")); |
+ EXPECT_EQ(width, 33); |
+ EXPECT_EQ(height, 44); |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(node)); |
+} |
-TEST_F(MetadataAccessorTest, TestThumbnail) { |
- test::TestBookmarkClient bookmark_client; |
- scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel()); |
- const BookmarkNode* node = |
- bookmark_model->AddURL(bookmark_model->other_node(), |
- 0, // index. |
- base::ASCIIToUTF16("whatever"), |
- GURL(BOOKMARK_URL)); |
+TEST_F(EnhancedBookmarkModelTest, TestRemoteId) { |
+ const BookmarkNode* node = AddBookmark(); |
+ const BookmarkNode* folder_node = AddFolder(); |
- // Encode some information. |
- ASSERT_TRUE(enhanced_bookmarks::SetAllImagesForBookmark( |
- bookmark_model.get(), |
- node, |
- GURL(), |
- 0, |
- 0, |
- GURL("http://google.com/img/thumb.jpg"), |
- 33, |
- 44)); |
- GURL url; |
- int width; |
- int height; |
- bool result = enhanced_bookmarks::ThumbnailImageFromBookmark( |
- node, &url, &width, &height); |
- ASSERT_TRUE(result); |
- CHECK_EQ(url, GURL("http://google.com/img/thumb.jpg")); |
- CHECK_EQ(width, 33); |
- CHECK_EQ(height, 44); |
-}; |
+ std::string remote_id = model_->GetRemoteIdForNode(node); |
+ // First call creates the UUID, second call should return the same. |
+ EXPECT_EQ(remote_id, model_->GetRemoteIdForNode(node)); |
-TEST_F(MetadataAccessorTest, TestRemoteId) { |
- test::TestBookmarkClient bookmark_client; |
- scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel()); |
- const BookmarkNode* node = AddBookmark(bookmark_model.get(), "Aga Khan"); |
+ // Verify that the remote id starts with the correct prefix. |
+ EXPECT_TRUE(StartsWithASCII(remote_id, "ebc_", true)); |
+ std::string folder_remote_id = model_->GetRemoteIdForNode(folder_node); |
+ EXPECT_TRUE(StartsWithASCII(folder_remote_id, "ebf_", true)); |
- // First call creates the UUID, second call should return the same. |
- ASSERT_EQ( |
- enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model.get(), node), |
- enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model.get(), node)); |
+ // Verifiy version field was set. |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(node)); |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(folder_node)); |
} |
-TEST_F(MetadataAccessorTest, TestEmptyDescription) { |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestEmptyDescription) { |
+ const BookmarkNode* node = AddBookmark(); |
- std::string description( |
- enhanced_bookmarks::DescriptionFromBookmark(node.get())); |
- CHECK_EQ(description, ""); |
+ std::string description(model_->GetDescriptionForNode(node)); |
+ EXPECT_EQ(description, ""); |
} |
-TEST_F(MetadataAccessorTest, TestDescription) { |
- test::TestBookmarkClient bookmark_client; |
- scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel()); |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestDescription) { |
+ const BookmarkNode* node = AddBookmark(); |
const std::string description("This is the most useful description of all."); |
// Set the description. |
- enhanced_bookmarks::SetDescriptionForBookmark( |
- bookmark_model.get(), node.get(), description); |
+ model_->SetDescriptionForNode(node, description); |
// Check the description is the one that was set. |
- CHECK_EQ(enhanced_bookmarks::DescriptionFromBookmark(node.get()), |
- description); |
+ EXPECT_EQ(model_->GetDescriptionForNode(node), description); |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(node)); |
} |
// If there is no notes field, the description should fall back on the snippet. |
-TEST_F(MetadataAccessorTest, TestDescriptionFallback) { |
- test::TestBookmarkClient bookmark_client; |
- scoped_ptr<BookmarkModel> bookmark_model(bookmark_client.CreateModel()); |
- scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(BOOKMARK_URL))); |
+TEST_F(EnhancedBookmarkModelTest, TestDescriptionFallback) { |
+ const BookmarkNode* node = AddBookmark(); |
// Binary serialize the protobuf. |
- PageData data; |
+ image::collections::PageData data; |
data.set_snippet("Joe Bar Team"); |
ASSERT_TRUE(data.IsInitialized()); |
std::string output; |
@@ -335,20 +325,39 @@ TEST_F(MetadataAccessorTest, TestDescriptionFallback) { |
// base64 encode the output. |
std::string encoded; |
base::Base64Encode(output, &encoded); |
- node->SetMetaInfo(enhanced_bookmarks::kPageDataKey, encoded); |
+ bookmark_model_->SetNodeMetaInfo(node, "stars.pageData", encoded); |
// The snippet is used as the description. |
- std::string snippet(enhanced_bookmarks::SnippetFromBookmark(node.get())); |
- CHECK_EQ("Joe Bar Team", |
- enhanced_bookmarks::DescriptionFromBookmark(node.get())); |
+ std::string snippet(model_->GetSnippetForNode(node)); |
+ EXPECT_EQ("Joe Bar Team", model_->GetDescriptionForNode(node)); |
// Set the description. |
const std::string description("This is the most useful description of all."); |
- enhanced_bookmarks::SetDescriptionForBookmark( |
- bookmark_model.get(), node.get(), description); |
+ model_->SetDescriptionForNode(node, description); |
// Check the description is the one that was set. |
- CHECK_EQ(enhanced_bookmarks::DescriptionFromBookmark(node.get()), |
- description); |
+ EXPECT_EQ(model_->GetDescriptionForNode(node), description); |
} |
+ |
+// Makes sure that the stars.version field is set every time |
+// EnhancedBookmarkModel makes a change to a node. |
+TEST_F(EnhancedBookmarkModelTest, TestVersionField) { |
+ const BookmarkNode* node = AddBookmark(); |
+ EXPECT_EQ("", model_->GetVersionForNode(node)); |
+ |
+ model_->SetDescriptionForNode(node, "foo"); |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(node)); |
+ |
+ // Change the version to set. |
+ model_->Initialize("v1.1"); |
+ |
+ model_->SetDescriptionForNode(node, "foo"); |
+ // Since the description didn't actually change, the version field should |
+ // not either. |
+ EXPECT_EQ("v1.0", model_->GetVersionForNode(node)); |
+ |
+ model_->SetDescriptionForNode(node, "bar"); |
+ EXPECT_EQ("v1.1", model_->GetVersionForNode(node)); |
+} |
+ |
} // namespace |