This source file includes following definitions.
- AddWithMetaInfo
- Add
- AddFolder
- AddFolderWithMetaInfo
- AddURL
- AddURLWithMetaInfo
- Delete
- ModifyTitle
- ModifyPosition
- ModifyCreationTime
- ModifyMetaInfo
- ApplyPendingChanges
- changes
- SetModified
- SetNodeMetaInfo
- completed_count_
- ExtensiveBookmarkChangesBeginning
- ExtensiveBookmarkChangesEnded
- BookmarkModelChanged
- get_started
- get_completed_count_at_started
- get_completed
- syncer_merge_result_
- SetUp
- TearDown
- AddFolderToShare
- AddBookmarkToShare
- LoadBookmarkModel
- GetSyncBookmarkCount
- CreatePermanentBookmarkNodes
- AssociateModels
- StartSync
- StopSync
- UnloadBookmarkModel
- InitSyncNodeFromChromeNode
- ExpectSyncerNodeMatching
- ExpectSyncerNodeMatching
- ExpectBrowserNodeMatching
- ExpectBrowserNodeUnknown
- ExpectBrowserNodeKnown
- ExpectSyncerNodeKnown
- ExpectSyncerNodeUnknown
- ExpectBrowserNodeTitle
- ExpectBrowserNodeURL
- ExpectBrowserNodeParent
- ExpectModelMatch
- ExpectModelMatch
- mobile_bookmarks_id
- other_bookmarks_id
- bookmark_bar_id
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- ProfileSyncServiceBookmarkTestWithData
- PopulateFromTestData
- CompareWithTestData
- WriteTestDataToBookmarkModel
- ExpectBookmarkModelMatchesTestData
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- GetTransactionVersions
- ExpectTransactionVersionMatch
- TEST_F
- TEST_F
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/bookmark_test_helpers.h"
#include "chrome/browser/sync/glue/bookmark_change_processor.h"
#include "chrome/browser/sync/glue/bookmark_model_associator.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_driver/data_type_error_handler.h"
#include "components/sync_driver/data_type_error_handler_mock.h"
#include "content/public/test/test_browser_thread.h"
#include "sync/api/sync_error.h"
#include "sync/internal_api/public/change_record.h"
#include "sync/internal_api/public/read_node.h"
#include "sync/internal_api/public/read_transaction.h"
#include "sync/internal_api/public/test/test_user_share.h"
#include "sync/internal_api/public/write_node.h"
#include "sync/internal_api/public/write_transaction.h"
#include "sync/internal_api/syncapi_internal.h"
#include "sync/syncable/mutable_entry.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace browser_sync {
using content::BrowserThread;
using syncer::BaseNode;
using testing::_;
using testing::InvokeWithoutArgs;
using testing::Mock;
using testing::StrictMock;
#if defined(OS_ANDROID)
static const bool kExpectMobileBookmarks = true;
#else
static const bool kExpectMobileBookmarks = false;
#endif
namespace {
class FakeServerChange {
public:
explicit FakeServerChange(syncer::WriteTransaction* trans) : trans_(trans) {
}
int64 AddWithMetaInfo(const std::wstring& title,
const std::string& url,
const BookmarkNode::MetaInfoMap* meta_info_map,
bool is_folder,
int64 parent_id,
int64 predecessor_id) {
syncer::ReadNode parent(trans_);
EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
syncer::WriteNode node(trans_);
if (predecessor_id == 0) {
EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
} else {
syncer::ReadNode predecessor(trans_);
EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id));
EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
EXPECT_TRUE(node.InitBookmarkByCreation(parent, &predecessor));
}
EXPECT_EQ(node.GetPredecessorId(), predecessor_id);
EXPECT_EQ(node.GetParentId(), parent_id);
node.SetIsFolder(is_folder);
node.SetTitle(title);
sync_pb::BookmarkSpecifics specifics(node.GetBookmarkSpecifics());
if (!is_folder)
specifics.set_url(url);
if (meta_info_map)
SetNodeMetaInfo(*meta_info_map, &specifics);
node.SetBookmarkSpecifics(specifics);
syncer::ChangeRecord record;
record.action = syncer::ChangeRecord::ACTION_ADD;
record.id = node.GetId();
changes_.push_back(record);
return node.GetId();
}
int64 Add(const std::wstring& title,
const std::string& url,
bool is_folder,
int64 parent_id,
int64 predecessor_id) {
return AddWithMetaInfo(title, url, NULL, is_folder, parent_id,
predecessor_id);
}
int64 AddFolder(const std::wstring& title,
int64 parent_id,
int64 predecessor_id) {
return Add(title, std::string(), true, parent_id, predecessor_id);
}
int64 AddFolderWithMetaInfo(const std::wstring& title,
const BookmarkNode::MetaInfoMap* meta_info_map,
int64 parent_id,
int64 predecessor_id) {
return AddWithMetaInfo(title, std::string(), meta_info_map, true, parent_id,
predecessor_id);
}
int64 AddURL(const std::wstring& title,
const std::string& url,
int64 parent_id,
int64 predecessor_id) {
return Add(title, url, false, parent_id, predecessor_id);
}
int64 AddURLWithMetaInfo(const std::wstring& title,
const std::string& url,
const BookmarkNode::MetaInfoMap* meta_info_map,
int64 parent_id,
int64 predecessor_id) {
return AddWithMetaInfo(title, url, meta_info_map, false, parent_id,
predecessor_id);
}
void Delete(int64 id) {
{
syncer::WriteNode node(trans_);
EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
if (node.GetIsFolder())
EXPECT_FALSE(node.GetFirstChildId());
node.GetMutableEntryForTest()->PutServerIsDel(true);
node.Tombstone();
}
{
syncer::ReadNode node(trans_);
EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, node.InitByIdLookup(id));
}
syncer::ChangeRecord record;
record.action = syncer::ChangeRecord::ACTION_DELETE;
record.id = id;
changes_.insert(changes_.begin(), record);
}
std::wstring ModifyTitle(int64 id, const std::wstring& new_title) {
syncer::WriteNode node(trans_);
EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
std::string old_title = node.GetTitle();
node.SetTitle(new_title);
SetModified(id);
return base::UTF8ToWide(old_title);
}
int64 ModifyPosition(int64 id, int64 parent_id, int64 predecessor_id) {
syncer::ReadNode parent(trans_);
EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
syncer::WriteNode node(trans_);
EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
int64 old_parent_id = node.GetParentId();
if (predecessor_id == 0) {
EXPECT_TRUE(node.SetPosition(parent, NULL));
} else {
syncer::ReadNode predecessor(trans_);
EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id));
EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
EXPECT_TRUE(node.SetPosition(parent, &predecessor));
}
SetModified(id);
return old_parent_id;
}
void ModifyCreationTime(int64 id, int64 creation_time_us) {
syncer::WriteNode node(trans_);
ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics();
specifics.set_creation_time_us(creation_time_us);
node.SetBookmarkSpecifics(specifics);
SetModified(id);
}
void ModifyMetaInfo(int64 id,
const BookmarkNode::MetaInfoMap& meta_info_map) {
syncer::WriteNode node(trans_);
ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics();
SetNodeMetaInfo(meta_info_map, &specifics);
node.SetBookmarkSpecifics(specifics);
SetModified(id);
}
void ApplyPendingChanges(ChangeProcessor* processor) {
processor->ApplyChangesFromSyncModel(
trans_, 0, syncer::ImmutableChangeRecordList(&changes_));
}
const syncer::ChangeRecordList& changes() {
return changes_;
}
private:
void SetModified(int64 id) {
if (!changes_.empty() && changes_.back().id == id &&
changes_.back().action ==
syncer::ChangeRecord::ACTION_UPDATE)
return;
syncer::ChangeRecord record;
record.action = syncer::ChangeRecord::ACTION_UPDATE;
record.id = id;
changes_.push_back(record);
}
void SetNodeMetaInfo(const BookmarkNode::MetaInfoMap& meta_info_map,
sync_pb::BookmarkSpecifics* specifics) {
specifics->clear_meta_info();
for (BookmarkNode::MetaInfoMap::const_iterator it =
meta_info_map.begin(); it != meta_info_map.end(); ++it) {
sync_pb::MetaInfo* meta_info = specifics->add_meta_info();
meta_info->set_key(it->first);
meta_info->set_value(it->second);
}
}
syncer::WriteTransaction *trans_;
syncer::ChangeRecordList changes_;
};
class ExtensiveChangesBookmarkModelObserver : public BaseBookmarkModelObserver {
public:
explicit ExtensiveChangesBookmarkModelObserver()
: started_count_(0),
completed_count_at_started_(0),
completed_count_(0) {}
virtual void ExtensiveBookmarkChangesBeginning(
BookmarkModel* model) OVERRIDE {
++started_count_;
completed_count_at_started_ = completed_count_;
}
virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE {
++completed_count_;
}
virtual void BookmarkModelChanged() OVERRIDE {}
int get_started() const {
return started_count_;
}
int get_completed_count_at_started() const {
return completed_count_at_started_;
}
int get_completed() const {
return completed_count_;
}
private:
int started_count_;
int completed_count_at_started_;
int completed_count_;
DISALLOW_COPY_AND_ASSIGN(ExtensiveChangesBookmarkModelObserver);
};
class ProfileSyncServiceBookmarkTest : public testing::Test {
protected:
enum LoadOption { LOAD_FROM_STORAGE, DELETE_EXISTING_STORAGE };
enum SaveOption { SAVE_TO_STORAGE, DONT_SAVE_TO_STORAGE };
ProfileSyncServiceBookmarkTest()
: model_(NULL),
ui_thread_(BrowserThread::UI, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_),
local_merge_result_(syncer::BOOKMARKS),
syncer_merge_result_(syncer::BOOKMARKS) {
}
virtual ~ProfileSyncServiceBookmarkTest() {
StopSync();
UnloadBookmarkModel();
}
virtual void SetUp() {
test_user_share_.SetUp();
}
virtual void TearDown() {
test_user_share_.TearDown();
}
int64 AddFolderToShare(syncer::WriteTransaction* trans, std::string title) {
EXPECT_FALSE(model_associator_);
syncer::ReadNode bookmark_bar(trans);
EXPECT_EQ(BaseNode::INIT_OK, bookmark_bar.InitByTagLookup("bookmark_bar"));
syncer::WriteNode node(trans);
EXPECT_TRUE(node.InitBookmarkByCreation(bookmark_bar, NULL));
node.SetIsFolder(true);
node.SetTitle(base::ASCIIToWide(title));
return node.GetId();
}
int64 AddBookmarkToShare(syncer::WriteTransaction *trans,
int64 parent_id,
std::string title) {
EXPECT_FALSE(model_associator_);
syncer::ReadNode parent(trans);
EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
sync_pb::BookmarkSpecifics specifics;
specifics.set_url("http://www.google.com/search?q=" + title);
specifics.set_title(title);
syncer::WriteNode node(trans);
EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
node.SetIsFolder(false);
node.SetTitle(base::ASCIIToWide(title));
node.SetBookmarkSpecifics(specifics);
return node.GetId();
}
void LoadBookmarkModel(LoadOption load, SaveOption save) {
bool delete_bookmarks = load == DELETE_EXISTING_STORAGE;
profile_.CreateBookmarkModel(delete_bookmarks);
model_ = BookmarkModelFactory::GetForProfile(&profile_);
test::WaitForBookmarkModelToLoad(model_);
if (save == DONT_SAVE_TO_STORAGE)
model_->ClearStore();
message_loop_.RunUntilIdle();
}
int GetSyncBookmarkCount() {
syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
syncer::ReadNode node(&trans);
if (node.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::BOOKMARKS)) !=
syncer::BaseNode::INIT_OK)
return 0;
return node.GetTotalNodeCount();
}
bool CreatePermanentBookmarkNodes() {
bool root_exists = false;
syncer::ModelType type = syncer::BOOKMARKS;
{
syncer::WriteTransaction trans(FROM_HERE,
test_user_share_.user_share());
syncer::ReadNode uber_root(&trans);
uber_root.InitByRootLookup();
syncer::ReadNode root(&trans);
root_exists = (root.InitByTagLookup(syncer::ModelTypeToRootTag(type)) ==
BaseNode::INIT_OK);
}
if (!root_exists) {
if (!syncer::TestUserShare::CreateRoot(type,
test_user_share_.user_share()))
return false;
}
const int kNumPermanentNodes = 3;
const std::string permanent_tags[kNumPermanentNodes] = {
"bookmark_bar", "other_bookmarks", "synced_bookmarks"
};
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
syncer::ReadNode root(&trans);
EXPECT_EQ(BaseNode::INIT_OK, root.InitByTagLookup(
syncer::ModelTypeToRootTag(type)));
int64 last_child_id = syncer::kInvalidId;
for (int i = 0; i < kNumPermanentNodes; ++i) {
syncer::ReadNode lookup(&trans);
if (lookup.InitByTagLookup(permanent_tags[i]) ==
syncer::ReadNode::INIT_OK) {
last_child_id = lookup.GetId();
continue;
}
syncer::ReadNode predecessor_node(&trans);
syncer::ReadNode* predecessor = NULL;
if (last_child_id != syncer::kInvalidId) {
EXPECT_EQ(BaseNode::INIT_OK,
predecessor_node.InitByIdLookup(last_child_id));
predecessor = &predecessor_node;
}
syncer::WriteNode node(&trans);
if (!node.InitBookmarkByCreation(root, predecessor))
return false;
node.SetIsFolder(true);
node.GetMutableEntryForTest()->PutUniqueServerTag(permanent_tags[i]);
node.SetTitle(base::UTF8ToWide(permanent_tags[i]));
node.SetExternalId(0);
last_child_id = node.GetId();
}
return true;
}
bool AssociateModels() {
DCHECK(!model_associator_);
model_associator_.reset(new BookmarkModelAssociator(
BookmarkModelFactory::GetForProfile(&profile_),
&profile_,
test_user_share_.user_share(),
&mock_error_handler_,
kExpectMobileBookmarks));
local_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS);
syncer_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS);
int local_count_before = model_->root_node()->GetTotalNodeCount();
int syncer_count_before = GetSyncBookmarkCount();
syncer::SyncError error = model_associator_->AssociateModels(
&local_merge_result_,
&syncer_merge_result_);
if (error.IsSet())
return false;
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(local_count_before,
local_merge_result_.num_items_before_association());
EXPECT_EQ(syncer_count_before,
syncer_merge_result_.num_items_before_association());
EXPECT_EQ(local_merge_result_.num_items_after_association(),
local_merge_result_.num_items_before_association() +
local_merge_result_.num_items_added() -
local_merge_result_.num_items_deleted());
EXPECT_EQ(syncer_merge_result_.num_items_after_association(),
syncer_merge_result_.num_items_before_association() +
syncer_merge_result_.num_items_added() -
syncer_merge_result_.num_items_deleted());
EXPECT_EQ(model_->root_node()->GetTotalNodeCount(),
local_merge_result_.num_items_after_association());
EXPECT_EQ(GetSyncBookmarkCount(),
syncer_merge_result_.num_items_after_association());
return true;
}
void StartSync() {
test_user_share_.Reload();
ASSERT_TRUE(CreatePermanentBookmarkNodes());
ASSERT_TRUE(AssociateModels());
change_processor_.reset(
new BookmarkChangeProcessor(model_associator_.get(),
&mock_error_handler_));
change_processor_->Start(&profile_, test_user_share_.user_share());
}
void StopSync() {
change_processor_.reset();
if (model_associator_) {
syncer::SyncError error = model_associator_->DisassociateModels();
EXPECT_FALSE(error.IsSet());
}
model_associator_.reset();
message_loop_.RunUntilIdle();
}
void UnloadBookmarkModel() {
profile_.CreateBookmarkModel(false );
model_ = NULL;
message_loop_.RunUntilIdle();
}
bool InitSyncNodeFromChromeNode(const BookmarkNode* bnode,
syncer::BaseNode* sync_node) {
return model_associator_->InitSyncNodeFromChromeId(bnode->id(),
sync_node);
}
void ExpectSyncerNodeMatching(syncer::BaseTransaction* trans,
const BookmarkNode* bnode) {
std::string truncated_title = base::UTF16ToUTF8(bnode->GetTitle());
syncer::SyncAPINameToServerName(truncated_title, &truncated_title);
base::TruncateUTF8ToByteSize(truncated_title, 255, &truncated_title);
syncer::ServerNameToSyncAPIName(truncated_title, &truncated_title);
syncer::ReadNode gnode(trans);
ASSERT_TRUE(InitSyncNodeFromChromeNode(bnode, &gnode));
if (!model_->is_permanent_node(bnode)) {
EXPECT_EQ(truncated_title, gnode.GetTitle());
EXPECT_EQ(
model_associator_->GetChromeNodeFromSyncId(gnode.GetParentId()),
bnode->parent());
}
EXPECT_EQ(bnode->is_folder(), gnode.GetIsFolder());
if (bnode->is_url())
EXPECT_EQ(bnode->url(), GURL(gnode.GetBookmarkSpecifics().url()));
const BookmarkNode::MetaInfoMap* meta_info_map = bnode->GetMetaInfoMap();
sync_pb::BookmarkSpecifics specifics = gnode.GetBookmarkSpecifics();
if (!meta_info_map) {
EXPECT_EQ(0, specifics.meta_info_size());
} else {
EXPECT_EQ(meta_info_map->size(),
static_cast<size_t>(specifics.meta_info_size()));
for (int i = 0; i < specifics.meta_info_size(); i++) {
BookmarkNode::MetaInfoMap::const_iterator it =
meta_info_map->find(specifics.meta_info(i).key());
EXPECT_TRUE(it != meta_info_map->end());
EXPECT_EQ(it->second, specifics.meta_info(i).value());
}
}
int browser_index = bnode->parent()->GetIndexOf(bnode);
if (browser_index == 0) {
EXPECT_EQ(gnode.GetPredecessorId(), 0);
} else {
const BookmarkNode* bprev =
bnode->parent()->GetChild(browser_index - 1);
syncer::ReadNode gprev(trans);
ASSERT_TRUE(InitSyncNodeFromChromeNode(bprev, &gprev));
EXPECT_EQ(gnode.GetPredecessorId(), gprev.GetId());
EXPECT_EQ(gnode.GetParentId(), gprev.GetParentId());
}
if (browser_index == bnode->parent()->child_count() - 1) {
EXPECT_EQ(gnode.GetSuccessorId(), 0);
} else {
const BookmarkNode* bnext =
bnode->parent()->GetChild(browser_index + 1);
syncer::ReadNode gnext(trans);
ASSERT_TRUE(InitSyncNodeFromChromeNode(bnext, &gnext));
EXPECT_EQ(gnode.GetSuccessorId(), gnext.GetId());
EXPECT_EQ(gnode.GetParentId(), gnext.GetParentId());
}
if (!bnode->empty())
EXPECT_TRUE(gnode.GetFirstChildId());
}
void ExpectSyncerNodeMatching(const BookmarkNode* bnode) {
syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
ExpectSyncerNodeMatching(&trans, bnode);
}
void ExpectBrowserNodeMatching(syncer::BaseTransaction* trans,
int64 sync_id) {
EXPECT_TRUE(sync_id);
const BookmarkNode* bnode =
model_associator_->GetChromeNodeFromSyncId(sync_id);
ASSERT_TRUE(bnode);
int64 id = model_associator_->GetSyncIdFromChromeId(bnode->id());
EXPECT_EQ(id, sync_id);
ExpectSyncerNodeMatching(trans, bnode);
}
void ExpectBrowserNodeUnknown(int64 sync_id) {
EXPECT_FALSE(model_associator_->GetChromeNodeFromSyncId(sync_id));
}
void ExpectBrowserNodeKnown(int64 sync_id) {
EXPECT_TRUE(model_associator_->GetChromeNodeFromSyncId(sync_id));
}
void ExpectSyncerNodeKnown(const BookmarkNode* node) {
int64 sync_id = model_associator_->GetSyncIdFromChromeId(node->id());
EXPECT_NE(sync_id, syncer::kInvalidId);
}
void ExpectSyncerNodeUnknown(const BookmarkNode* node) {
int64 sync_id = model_associator_->GetSyncIdFromChromeId(node->id());
EXPECT_EQ(sync_id, syncer::kInvalidId);
}
void ExpectBrowserNodeTitle(int64 sync_id, const std::wstring& title) {
const BookmarkNode* bnode =
model_associator_->GetChromeNodeFromSyncId(sync_id);
ASSERT_TRUE(bnode);
EXPECT_EQ(bnode->GetTitle(), base::WideToUTF16Hack(title));
}
void ExpectBrowserNodeURL(int64 sync_id, const std::string& url) {
const BookmarkNode* bnode =
model_associator_->GetChromeNodeFromSyncId(sync_id);
ASSERT_TRUE(bnode);
EXPECT_EQ(GURL(url), bnode->url());
}
void ExpectBrowserNodeParent(int64 sync_id, int64 parent_sync_id) {
const BookmarkNode* node =
model_associator_->GetChromeNodeFromSyncId(sync_id);
ASSERT_TRUE(node);
const BookmarkNode* parent =
model_associator_->GetChromeNodeFromSyncId(parent_sync_id);
EXPECT_TRUE(parent);
EXPECT_EQ(node->parent(), parent);
}
void ExpectModelMatch(syncer::BaseTransaction* trans) {
const BookmarkNode* root = model_->root_node();
EXPECT_EQ(root->GetIndexOf(model_->bookmark_bar_node()), 0);
EXPECT_EQ(root->GetIndexOf(model_->other_node()), 1);
EXPECT_EQ(root->GetIndexOf(model_->mobile_node()), 2);
std::stack<int64> stack;
stack.push(bookmark_bar_id());
while (!stack.empty()) {
int64 id = stack.top();
stack.pop();
if (!id) continue;
ExpectBrowserNodeMatching(trans, id);
syncer::ReadNode gnode(trans);
ASSERT_EQ(BaseNode::INIT_OK, gnode.InitByIdLookup(id));
stack.push(gnode.GetSuccessorId());
if (gnode.GetIsFolder())
stack.push(gnode.GetFirstChildId());
}
}
void ExpectModelMatch() {
syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
ExpectModelMatch(&trans);
}
int64 mobile_bookmarks_id() {
return
model_associator_->GetSyncIdFromChromeId(model_->mobile_node()->id());
}
int64 other_bookmarks_id() {
return
model_associator_->GetSyncIdFromChromeId(model_->other_node()->id());
}
int64 bookmark_bar_id() {
return model_associator_->GetSyncIdFromChromeId(
model_->bookmark_bar_node()->id());
}
protected:
BookmarkModel* model_;
syncer::TestUserShare test_user_share_;
scoped_ptr<BookmarkChangeProcessor> change_processor_;
StrictMock<DataTypeErrorHandlerMock> mock_error_handler_;
scoped_ptr<BookmarkModelAssociator> model_associator_;
private:
base::MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
syncer::SyncMergeResult local_merge_result_;
syncer::SyncMergeResult syncer_merge_result_;
TestingProfile profile_;
};
TEST_F(ProfileSyncServiceBookmarkTest, InitialState) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
EXPECT_TRUE(other_bookmarks_id());
EXPECT_TRUE(bookmark_bar_id());
EXPECT_TRUE(mobile_bookmarks_id());
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) {
const int kNumBookmarksPerFolder = 10;
const int kNumFolders = 10;
CreatePermanentBookmarkNodes();
{
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
for (int i = 0; i < kNumFolders; ++i) {
int64 folder_id = AddFolderToShare(&trans,
base::StringPrintf("folder%05d", i));
for (int j = 0; j < kNumBookmarksPerFolder; ++j) {
AddBookmarkToShare(&trans,
folder_id,
base::StringPrintf("bookmark%05d", j));
}
}
}
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
const BookmarkNode* folder =
model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar"));
ExpectSyncerNodeMatching(folder);
ExpectModelMatch();
const BookmarkNode* folder2 =
model_->AddFolder(folder, 0, base::ASCIIToUTF16("nested"));
ExpectSyncerNodeMatching(folder2);
ExpectModelMatch();
const BookmarkNode* url1 = model_->AddURL(
folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"),
GURL("http://www.easypie.com/"));
ExpectSyncerNodeMatching(url1);
ExpectModelMatch();
const BookmarkNode* url2 = model_->AddURL(
folder, 1, base::ASCIIToUTF16("Airplanes"),
GURL("http://www.easyjet.com/"));
ExpectSyncerNodeMatching(url2);
ExpectModelMatch();
const BookmarkNode* mobile_folder =
model_->AddFolder(model_->mobile_node(), 0, base::ASCIIToUTF16("pie"));
ExpectSyncerNodeMatching(mobile_folder);
ExpectModelMatch();
model_->SetTitle(url2, base::ASCIIToUTF16("EasyJet"));
ExpectModelMatch();
model_->Move(url1, folder2, 0);
ExpectModelMatch();
model_->Move(folder2, model_->bookmark_bar_node(), 0);
ExpectModelMatch();
model_->SetTitle(folder2, base::ASCIIToUTF16("Not Nested"));
ExpectModelMatch();
model_->Move(folder, folder2, 0);
ExpectModelMatch();
model_->SetTitle(folder, base::ASCIIToUTF16("who's nested now?"));
ExpectModelMatch();
model_->Copy(url2, model_->bookmark_bar_node(), 0);
ExpectModelMatch();
model_->SetTitle(mobile_folder, base::ASCIIToUTF16("strawberry"));
ExpectModelMatch();
model_->Remove(url2->parent(), url2->parent()->GetIndexOf(url2));
ExpectModelMatch();
model_->Remove(folder2->parent(),
folder2->parent()->GetIndexOf(folder2));
ExpectModelMatch();
model_->Remove(model_->mobile_node(), 0);
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeProcessing) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange adds(&trans);
int64 f1 = adds.AddFolder(L"Server Folder B", bookmark_bar_id(), 0);
int64 f2 = adds.AddFolder(L"Server Folder A", bookmark_bar_id(), f1);
int64 u1 = adds.AddURL(L"Some old site", "ftp://nifty.andrew.cmu.edu/",
bookmark_bar_id(), f2);
int64 u2 = adds.AddURL(L"Nifty", "ftp://nifty.andrew.cmu.edu/", f1, 0);
int64 u3 = adds.AddURL(L"Nifty2", "ftp://nifty.andrew.cmu.edu/", f1, u2);
adds.AddURL(L"Some old site", "http://slog.thestranger.com/",
bookmark_bar_id(), u1);
std::string javascript_url(
"javascript:(function(){var w=window.open(" \
"'about:blank','gnotesWin','location=0,menubar=0," \
"scrollbars=0,status=0,toolbar=0,width=300," \
"height=300,resizable');});");
adds.AddURL(std::wstring(), javascript_url, other_bookmarks_id(), 0);
int64 u6 = adds.AddURL(
L"Sync1", "http://www.syncable.edu/", mobile_bookmarks_id(), 0);
syncer::ChangeRecordList::const_iterator it;
for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
ExpectBrowserNodeUnknown(it->id);
adds.ApplyPendingChanges(change_processor_.get());
for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
ExpectBrowserNodeMatching(&trans, it->id);
ExpectModelMatch(&trans);
FakeServerChange mods(&trans);
std::wstring u2_old_title = mods.ModifyTitle(u2, L"The Google");
int64 u2_old_parent = mods.ModifyPosition(u2, f2, 0);
std::wstring f1_old_title = mods.ModifyTitle(f1, L"Server Folder C");
int64 f1_old_parent = mods.ModifyPosition(f1, f2, u2);
int64 u3_old_parent = mods.ModifyPosition(u3, f2, f1);
std::wstring u6_old_title = mods.ModifyTitle(u6, L"Mobile Folder A");
ExpectBrowserNodeTitle(u2, u2_old_title);
ExpectBrowserNodeParent(u2, u2_old_parent);
ExpectBrowserNodeTitle(f1, f1_old_title);
ExpectBrowserNodeParent(f1, f1_old_parent);
ExpectBrowserNodeParent(u3, u3_old_parent);
ExpectBrowserNodeTitle(u6, u6_old_title);
mods.ApplyPendingChanges(change_processor_.get());
for (it = mods.changes().begin(); it != mods.changes().end(); ++it)
ExpectBrowserNodeMatching(&trans, it->id);
ExpectModelMatch(&trans);
FakeServerChange dels(&trans);
dels.Delete(u2);
dels.Delete(u3);
dels.Delete(u6);
ExpectBrowserNodeKnown(u2);
ExpectBrowserNodeKnown(u3);
dels.ApplyPendingChanges(change_processor_.get());
ExpectBrowserNodeUnknown(u2);
ExpectBrowserNodeUnknown(u3);
ExpectBrowserNodeUnknown(u6);
ExpectModelMatch(&trans);
}
TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeRequiringFosterParent) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
std::string url("http://dev.chromium.org/");
FakeServerChange adds(&trans);
int64 f0 = other_bookmarks_id();
int64 f1 = adds.AddFolder(L"f1", f0, 0);
int64 f2 = adds.AddFolder(L"f2", f1, 0);
int64 u3 = adds.AddURL( L"u3", url, f2, 0);
int64 u4 = adds.AddURL( L"u4", url, f2, u3);
int64 u5 = adds.AddURL( L"u5", url, f1, f2);
int64 f6 = adds.AddFolder(L"f6", f1, u5);
int64 u7 = adds.AddURL( L"u7", url, f0, f1);
syncer::ChangeRecordList::const_iterator it;
for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
ExpectBrowserNodeUnknown(it->id);
adds.ApplyPendingChanges(change_processor_.get());
for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
ExpectBrowserNodeMatching(&trans, it->id);
ExpectModelMatch(&trans);
FakeServerChange ops(&trans);
ops.ModifyPosition(f6, other_bookmarks_id(), 0);
ops.ModifyPosition(u3, other_bookmarks_id(), f1);
ops.ModifyPosition(f2, other_bookmarks_id(), u7);
ops.ModifyPosition(u7, f2, 0);
ops.ModifyPosition(u4, other_bookmarks_id(), f2);
ops.ModifyPosition(u5, f6, 0);
ops.Delete(f1);
ops.ApplyPendingChanges(change_processor_.get());
ExpectModelMatch(&trans);
}
TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeWithNonCanonicalURL) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
{
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange adds(&trans);
std::string url("http://dev.chromium.org");
EXPECT_NE(GURL(url).spec(), url);
adds.AddURL(L"u1", url, other_bookmarks_id(), 0);
adds.ApplyPendingChanges(change_processor_.get());
EXPECT_EQ(1, model_->other_node()->child_count());
ExpectModelMatch(&trans);
}
StopSync();
LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
StartSync();
EXPECT_EQ(1, model_->other_node()->child_count());
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, DISABLED_ServerChangeWithInvalidURL) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
int child_count = 0;
{
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange adds(&trans);
std::string url("x");
EXPECT_FALSE(GURL(url).is_valid());
adds.AddURL(L"u1", url, other_bookmarks_id(), 0);
adds.ApplyPendingChanges(change_processor_.get());
child_count = model_->other_node()->child_count();
EXPECT_TRUE(child_count == 0 || child_count == 1);
ExpectModelMatch(&trans);
}
StopSync();
LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
StartSync();
EXPECT_EQ(model_->other_node()->child_count(), child_count);
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, CornerCaseNames) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
const char* names[] = {
"",
"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",
"COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3",
"LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
".", "..", "...",
"Thumbs.db", ".DS_Store",
"foo/bar", "foo\\bar", "foo?bar", "foo:bar", "foo|bar", "foo\"bar",
"foo'bar", "foo<bar", "foo>bar", "foo%bar", "foo*bar", "foo]bar",
"foo[bar",
"012345678901234567890123456789012345678901234567890123456789012345678901"
"234567890123456789012345678901234567890123456789012345678901234567890123"
"456789012345678901234567890123456789012345678901234567890123456789012345"
"678901234567890123456789012345678901234567890123456789012345678901234567"
"890123456789"
};
GURL url("http://www.doublemint.com");
for (size_t i = 0; i < arraysize(names); ++i) {
model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16(names[i]));
model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16(names[i]), url);
}
EXPECT_EQ(static_cast<size_t>(model_->other_node()->child_count()),
2*arraysize(names));
ExpectModelMatch();
StopSync();
LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
StartSync();
EXPECT_EQ(static_cast<size_t>(model_->other_node()->child_count()),
2*arraysize(names));
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, RepeatedMiddleInsertion) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
static const int kTimesToInsert = 256;
model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("Alpha"));
model_->AddFolder(model_->other_node(), 1, base::ASCIIToUTF16("Omega"));
int count = 2;
for (int i = 0; i < kTimesToInsert; ++i) {
base::string16 title =
base::ASCIIToUTF16("Pre-insertion ") + base::IntToString16(i);
model_->AddFolder(model_->other_node(), 1, title);
count++;
}
for (int i = 0; i < kTimesToInsert; ++i) {
base::string16 title =
base::ASCIIToUTF16("Post-insertion ") + base::IntToString16(i);
model_->AddFolder(model_->other_node(), count - 1, title);
count++;
}
EXPECT_EQ(model_->other_node()->child_count(), count);
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, UnrecoverableErrorSuspendsService) {
EXPECT_CALL(mock_error_handler_,
OnSingleDatatypeUnrecoverableError(_, _));
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
const BookmarkNode* node =
model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("node"));
ExpectSyncerNodeMatching(node);
{
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
syncer::WriteNode sync_node(&trans);
ASSERT_TRUE(InitSyncNodeFromChromeNode(node, &sync_node));
sync_node.Tombstone();
}
ExpectSyncerNodeKnown(node);
model_->AddFolder(node, 0, base::ASCIIToUTF16("nested"));
}
TEST_F(ProfileSyncServiceBookmarkTest, MergeDuplicates) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("Dup"),
GURL("http://dup.com/"));
model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("Dup"),
GURL("http://dup.com/"));
EXPECT_EQ(2, model_->other_node()->child_count());
StopSync();
StartSync();
EXPECT_EQ(2, model_->other_node()->child_count());
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, ApplySyncDeletesFromJournal) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
int64 u0 = 0;
int64 f1 = 0;
int64 u1 = 0;
int64 f2 = 0;
int64 u2 = 0;
StartSync();
int fixed_sync_bk_count = GetSyncBookmarkCount();
{
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange adds(&trans);
u0 = adds.AddURL(L"URL 0", "http://plus.google.com/", bookmark_bar_id(), 0);
f1 = adds.AddFolder(L"Folder 1", bookmark_bar_id(), u0);
u1 = adds.AddURL(L"URL 1", "http://www.google.com/", f1, 0);
f2 = adds.AddFolder(L"Folder 2", f1, u1);
u2 = adds.AddURL(L"URL 2", "http://mail.google.com/", f2, 0);
adds.ApplyPendingChanges(change_processor_.get());
}
StopSync();
LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
EXPECT_EQ(6, model_->bookmark_bar_node()->GetTotalNodeCount());
EXPECT_EQ(fixed_sync_bk_count + 5, GetSyncBookmarkCount());
StartSync();
{
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange dels(&trans);
dels.Delete(u2);
dels.Delete(f2);
dels.Delete(u1);
dels.Delete(f1);
dels.ApplyPendingChanges(change_processor_.get());
}
StopSync();
EXPECT_EQ(2, model_->bookmark_bar_node()->GetTotalNodeCount());
LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
EXPECT_EQ(6, model_->bookmark_bar_node()->GetTotalNodeCount());
model_->AddURL(model_->bookmark_bar_node()->GetChild(1),
0, base::UTF8ToUTF16("local"), GURL("http://www.youtube.com"));
EXPECT_EQ(fixed_sync_bk_count + 1, GetSyncBookmarkCount());
StartSync();
EXPECT_EQ(4, model_->bookmark_bar_node()->GetTotalNodeCount());
EXPECT_EQ(base::UTF8ToUTF16("URL 0"),
model_->bookmark_bar_node()->GetChild(0)->GetTitle());
EXPECT_EQ(base::UTF8ToUTF16("Folder 1"),
model_->bookmark_bar_node()->GetChild(1)->GetTitle());
EXPECT_EQ(base::UTF8ToUTF16("local"),
model_->bookmark_bar_node()->GetChild(1)->GetChild(0)->GetTitle());
StopSync();
EXPECT_EQ(3u, test_user_share_.GetDeleteJournalSize());
StartSync();
StopSync();
ASSERT_TRUE(test_user_share_.Reload());
EXPECT_EQ(0u, test_user_share_.GetDeleteJournalSize());
}
struct TestData {
const wchar_t* title;
const char* url;
};
typedef std::map<int64, int64> BookmarkNodeVersionMap;
class ProfileSyncServiceBookmarkTestWithData
: public ProfileSyncServiceBookmarkTest {
public:
ProfileSyncServiceBookmarkTestWithData();
protected:
void PopulateFromTestData(const BookmarkNode* node,
const TestData* data,
int size,
int* running_count);
void CompareWithTestData(const BookmarkNode* node,
const TestData* data,
int size,
int* running_count);
void ExpectBookmarkModelMatchesTestData();
void WriteTestDataToBookmarkModel();
void ExpectTransactionVersionMatch(
const BookmarkNode* node,
const BookmarkNodeVersionMap& version_expected);
private:
const base::Time start_time_;
DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceBookmarkTestWithData);
};
namespace {
static TestData kBookmarkBarChildren[] = {
{ L"u2", "http://www.u2.com/" },
{ L"f1", NULL },
{ L"u1", "http://www.u1.com/" },
{ L"f2", NULL },
};
static TestData kF1Children[] = {
{ L"f1u4", "http://www.f1u4.com/" },
{ L"f1u2", "http://www.f1u2.com/" },
{ L"f1u3", "http://www.f1u3.com/" },
{ L"f1u1", "http://www.f1u1.com/" },
};
static TestData kF2Children[] = {
{ L"f2u2", "http://www.f2u2.com/" },
{ L"f2u4", "http://www.f2u4.com/" },
{ L"f2u3", "http://www.f2u3.com/" },
{ L"f2u1", "http://www.f2u1.com/" },
};
static TestData kOtherBookmarkChildren[] = {
{ L"f3", NULL },
{ L"u4", "http://www.u4.com/" },
{ L"u3", "http://www.u3.com/" },
{ L"f4", NULL },
{ L"dup", NULL },
{ L"dup", NULL },
{ L" ls ", "http://www.ls.com/" }
};
static TestData kF3Children[] = {
{ L"f3u4", "http://www.f3u4.com/" },
{ L"f3u2", "http://www.f3u2.com/" },
{ L"f3u3", "http://www.f3u3.com/" },
{ L"f3u1", "http://www.f3u1.com/" },
};
static TestData kF4Children[] = {
{ L"f4u1", "http://www.f4u1.com/" },
{ L"f4u2", "http://www.f4u2.com/" },
{ L"f4u3", "http://www.f4u3.com/" },
{ L"f4u4", "http://www.f4u4.com/" },
};
static TestData kDup1Children[] = {
{ L"dupu1", "http://www.dupu1.com/" },
};
static TestData kDup2Children[] = {
{ L"dupu2", "http://www.dupu2.com/" },
};
static TestData kMobileBookmarkChildren[] = {
{ L"f5", NULL },
{ L"f6", NULL },
{ L"u5", "http://www.u5.com/" },
};
static TestData kF5Children[] = {
{ L"f5u1", "http://www.f5u1.com/" },
{ L"f5u2", "http://www.f5u2.com/" },
};
static TestData kF6Children[] = {
{ L"f6u1", "http://www.f6u1.com/" },
{ L"f6u2", "http://www.f6u2.com/" },
};
}
ProfileSyncServiceBookmarkTestWithData::
ProfileSyncServiceBookmarkTestWithData()
: start_time_(base::Time::Now()) {
}
void ProfileSyncServiceBookmarkTestWithData::PopulateFromTestData(
const BookmarkNode* node,
const TestData* data,
int size,
int* running_count) {
DCHECK(node);
DCHECK(data);
DCHECK(node->is_folder());
for (int i = 0; i < size; ++i) {
const TestData& item = data[i];
if (item.url) {
const base::Time add_time =
start_time_ + base::TimeDelta::FromMinutes(*running_count);
model_->AddURLWithCreationTime(node, i, base::WideToUTF16Hack(item.title),
GURL(item.url), add_time);
} else {
model_->AddFolder(node, i, base::WideToUTF16Hack(item.title));
}
(*running_count)++;
}
}
void ProfileSyncServiceBookmarkTestWithData::CompareWithTestData(
const BookmarkNode* node,
const TestData* data,
int size,
int* running_count) {
DCHECK(node);
DCHECK(data);
DCHECK(node->is_folder());
ASSERT_EQ(size, node->child_count());
for (int i = 0; i < size; ++i) {
const BookmarkNode* child_node = node->GetChild(i);
const TestData& item = data[i];
GURL url = GURL(item.url == NULL ? "" : item.url);
BookmarkNode test_node(url);
test_node.SetTitle(base::WideToUTF16Hack(item.title));
EXPECT_EQ(child_node->GetTitle(), test_node.GetTitle());
if (item.url) {
EXPECT_FALSE(child_node->is_folder());
EXPECT_TRUE(child_node->is_url());
EXPECT_EQ(child_node->url(), test_node.url());
const base::Time expected_time =
start_time_ + base::TimeDelta::FromMinutes(*running_count);
EXPECT_EQ(expected_time.ToInternalValue(),
child_node->date_added().ToInternalValue());
} else {
EXPECT_TRUE(child_node->is_folder());
EXPECT_FALSE(child_node->is_url());
}
(*running_count)++;
}
}
void ProfileSyncServiceBookmarkTestWithData::WriteTestDataToBookmarkModel() {
const BookmarkNode* bookmarks_bar_node = model_->bookmark_bar_node();
int count = 0;
PopulateFromTestData(bookmarks_bar_node,
kBookmarkBarChildren,
arraysize(kBookmarkBarChildren),
&count);
ASSERT_GE(bookmarks_bar_node->child_count(), 4);
const BookmarkNode* f1_node = bookmarks_bar_node->GetChild(1);
PopulateFromTestData(f1_node, kF1Children, arraysize(kF1Children), &count);
const BookmarkNode* f2_node = bookmarks_bar_node->GetChild(3);
PopulateFromTestData(f2_node, kF2Children, arraysize(kF2Children), &count);
const BookmarkNode* other_bookmarks_node = model_->other_node();
PopulateFromTestData(other_bookmarks_node,
kOtherBookmarkChildren,
arraysize(kOtherBookmarkChildren),
&count);
ASSERT_GE(other_bookmarks_node->child_count(), 6);
const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
PopulateFromTestData(f3_node, kF3Children, arraysize(kF3Children), &count);
const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
PopulateFromTestData(f4_node, kF4Children, arraysize(kF4Children), &count);
const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
PopulateFromTestData(dup_node, kDup1Children, arraysize(kDup1Children),
&count);
dup_node = other_bookmarks_node->GetChild(5);
PopulateFromTestData(dup_node, kDup2Children, arraysize(kDup2Children),
&count);
const BookmarkNode* mobile_bookmarks_node = model_->mobile_node();
PopulateFromTestData(mobile_bookmarks_node,
kMobileBookmarkChildren,
arraysize(kMobileBookmarkChildren),
&count);
ASSERT_GE(mobile_bookmarks_node->child_count(), 3);
const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0);
PopulateFromTestData(f5_node, kF5Children, arraysize(kF5Children), &count);
const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1);
PopulateFromTestData(f6_node, kF6Children, arraysize(kF6Children), &count);
ExpectBookmarkModelMatchesTestData();
}
void ProfileSyncServiceBookmarkTestWithData::
ExpectBookmarkModelMatchesTestData() {
const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
int count = 0;
CompareWithTestData(bookmark_bar_node,
kBookmarkBarChildren,
arraysize(kBookmarkBarChildren),
&count);
ASSERT_GE(bookmark_bar_node->child_count(), 4);
const BookmarkNode* f1_node = bookmark_bar_node->GetChild(1);
CompareWithTestData(f1_node, kF1Children, arraysize(kF1Children), &count);
const BookmarkNode* f2_node = bookmark_bar_node->GetChild(3);
CompareWithTestData(f2_node, kF2Children, arraysize(kF2Children), &count);
const BookmarkNode* other_bookmarks_node = model_->other_node();
CompareWithTestData(other_bookmarks_node,
kOtherBookmarkChildren,
arraysize(kOtherBookmarkChildren),
&count);
ASSERT_GE(other_bookmarks_node->child_count(), 6);
const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
CompareWithTestData(f3_node, kF3Children, arraysize(kF3Children), &count);
const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
CompareWithTestData(f4_node, kF4Children, arraysize(kF4Children), &count);
const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
CompareWithTestData(dup_node, kDup1Children, arraysize(kDup1Children),
&count);
dup_node = other_bookmarks_node->GetChild(5);
CompareWithTestData(dup_node, kDup2Children, arraysize(kDup2Children),
&count);
const BookmarkNode* mobile_bookmarks_node = model_->mobile_node();
CompareWithTestData(mobile_bookmarks_node,
kMobileBookmarkChildren,
arraysize(kMobileBookmarkChildren),
&count);
ASSERT_GE(mobile_bookmarks_node->child_count(), 3);
const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0);
CompareWithTestData(f5_node, kF5Children, arraysize(kF5Children), &count);
const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1);
CompareWithTestData(f6_node, kF6Children, arraysize(kF6Children), &count);
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, Persistence) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
ExpectModelMatch();
StopSync();
UnloadBookmarkModel();
LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
StartSync();
ExpectBookmarkModelMatchesTestData();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptySyncModel) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
StartSync();
ExpectBookmarkModelMatchesTestData();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptyBookmarkModel) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
ExpectModelMatch();
StopSync();
UnloadBookmarkModel();
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
EXPECT_EQ(model_->bookmark_bar_node()->child_count(), 0);
EXPECT_EQ(model_->other_node()->child_count(), 0);
EXPECT_EQ(model_->mobile_node()->child_count(), 0);
StartSync();
ExpectBookmarkModelMatchesTestData();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeExpectedIdenticalModels) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
ExpectModelMatch();
StopSync();
UnloadBookmarkModel();
LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
ExpectBookmarkModelMatchesTestData();
ExpectModelMatch();
StopSync();
UnloadBookmarkModel();
LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
ExpectBookmarkModelMatchesTestData();
const BookmarkNode* bookmark_bar = model_->bookmark_bar_node();
ASSERT_TRUE(bookmark_bar);
ASSERT_GT(bookmark_bar->child_count(), 1);
model_->Move(bookmark_bar->GetChild(0), bookmark_bar, 1);
StartSync();
ExpectModelMatch();
ExpectBookmarkModelMatchesTestData();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeModelsWithSomeExtras) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
ExpectBookmarkModelMatchesTestData();
const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
int remove_index = 2;
ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
ASSERT_TRUE(child_node);
ASSERT_TRUE(child_node->is_url());
model_->Remove(bookmark_bar_node, remove_index);
ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
child_node = bookmark_bar_node->GetChild(remove_index);
ASSERT_TRUE(child_node);
ASSERT_TRUE(child_node->is_folder());
model_->Remove(bookmark_bar_node, remove_index);
const BookmarkNode* other_node = model_->other_node();
ASSERT_GE(other_node->child_count(), 1);
const BookmarkNode* f3_node = other_node->GetChild(0);
ASSERT_TRUE(f3_node);
ASSERT_TRUE(f3_node->is_folder());
remove_index = 2;
ASSERT_GT(f3_node->child_count(), remove_index);
model_->Remove(f3_node, remove_index);
ASSERT_GT(f3_node->child_count(), remove_index);
model_->Remove(f3_node, remove_index);
StartSync();
ExpectModelMatch();
StopSync();
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
ExpectBookmarkModelMatchesTestData();
bookmark_bar_node = model_->bookmark_bar_node();
remove_index = 0;
ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
child_node = bookmark_bar_node->GetChild(remove_index);
ASSERT_TRUE(child_node);
ASSERT_TRUE(child_node->is_url());
model_->Remove(bookmark_bar_node, remove_index);
ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
child_node = bookmark_bar_node->GetChild(remove_index);
ASSERT_TRUE(child_node);
ASSERT_TRUE(child_node->is_folder());
model_->Remove(bookmark_bar_node, remove_index);
ASSERT_GE(bookmark_bar_node->child_count(), 2);
model_->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 1);
other_node = model_->other_node();
ASSERT_GE(other_node->child_count(), 1);
f3_node = other_node->GetChild(0);
ASSERT_TRUE(f3_node);
ASSERT_TRUE(f3_node->is_folder());
remove_index = 0;
ASSERT_GT(f3_node->child_count(), remove_index);
model_->Remove(f3_node, remove_index);
ASSERT_GT(f3_node->child_count(), remove_index);
model_->Remove(f3_node, remove_index);
ASSERT_GE(other_node->child_count(), 4);
model_->Move(other_node->GetChild(0), other_node, 1);
model_->Move(other_node->GetChild(2), other_node, 3);
StartSync();
ExpectModelMatch();
ExpectBookmarkModelMatchesTestData();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, ModelAssociationPersistence) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
StartSync();
ExpectModelMatch();
StopSync();
StartSync();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData,
ModelAssociationInvalidPersistence) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
StartSync();
ExpectModelMatch();
StopSync();
const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
model_->AddURL(bookmark_bar_node, 0, base::ASCIIToUTF16("xtra"),
GURL("http://www.xtra.com"));
StartSync();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, SortChildren) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
const BookmarkNode* folder_added = model_->other_node()->GetChild(0);
ASSERT_TRUE(folder_added);
ASSERT_TRUE(folder_added->is_folder());
ExpectModelMatch();
model_->SortChildren(folder_added);
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTestWithData,
RecoverAfterDeletingSyncDataDirectory) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
StopSync();
TearDown();
SetUp();
EXPECT_TRUE(CreatePermanentBookmarkNodes());
EXPECT_FALSE(AssociateModels());
model_associator_.reset();
EXPECT_TRUE(CreatePermanentBookmarkNodes());
EXPECT_TRUE(AssociateModels());
ExpectBookmarkModelMatchesTestData();
ExpectModelMatch();
}
TEST_F(ProfileSyncServiceBookmarkTest, AssociationState) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
ExtensiveChangesBookmarkModelObserver observer;
model_->AddObserver(&observer);
StartSync();
EXPECT_EQ(1, observer.get_started());
EXPECT_EQ(0, observer.get_completed_count_at_started());
EXPECT_EQ(1, observer.get_completed());
model_->RemoveObserver(&observer);
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateDateAdded) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
StartSync();
StopSync();
const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
int remove_index = 2;
ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
ASSERT_TRUE(child_node);
EXPECT_TRUE(child_node->is_url());
model_->SetDateAdded(child_node, base::Time::FromInternalValue(10));
StartSync();
ExpectBookmarkModelMatchesTestData();
ExpectModelMatch();
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange adds(&trans);
const std::wstring kTitle = L"Some site";
const std::string kUrl = "http://www.whatwhat.yeah/";
const int kCreationTime = 30;
int64 id = adds.AddURL(kTitle, kUrl,
bookmark_bar_id(), 0);
adds.ApplyPendingChanges(change_processor_.get());
FakeServerChange updates(&trans);
updates.ModifyCreationTime(id, kCreationTime);
updates.ApplyPendingChanges(change_processor_.get());
const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(0);
ASSERT_TRUE(node);
EXPECT_TRUE(node->is_url());
EXPECT_EQ(base::WideToUTF16Hack(kTitle), node->GetTitle());
EXPECT_EQ(kUrl, node->url().possibly_invalid_spec());
EXPECT_EQ(node->date_added(), base::Time::FromInternalValue(30));
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromSync) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
StartSync();
syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
FakeServerChange adds(&trans);
BookmarkNode::MetaInfoMap folder_meta_info;
folder_meta_info["folder"] = "foldervalue";
int64 folder_id = adds.AddFolderWithMetaInfo(
L"folder title", &folder_meta_info, bookmark_bar_id(), 0);
BookmarkNode::MetaInfoMap node_meta_info;
node_meta_info["node"] = "nodevalue";
node_meta_info["other"] = "othervalue";
int64 id = adds.AddURLWithMetaInfo(L"node title", "http://www.foo.com",
&node_meta_info, folder_id, 0);
adds.ApplyPendingChanges(change_processor_.get());
ASSERT_LT(0, model_->bookmark_bar_node()->child_count());
const BookmarkNode* folder_node = model_->bookmark_bar_node()->GetChild(0);
ASSERT_TRUE(folder_node->GetMetaInfoMap());
EXPECT_EQ(folder_meta_info, *folder_node->GetMetaInfoMap());
ASSERT_LT(0, folder_node->child_count());
const BookmarkNode* node = folder_node->GetChild(0);
ASSERT_TRUE(node->GetMetaInfoMap());
EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap());
FakeServerChange updates(&trans);
folder_meta_info.erase("folder");
updates.ModifyMetaInfo(folder_id, folder_meta_info);
node_meta_info["node"] = "changednodevalue";
node_meta_info.erase("other");
node_meta_info["newkey"] = "newkeyvalue";
updates.ModifyMetaInfo(id, node_meta_info);
updates.ApplyPendingChanges(change_processor_.get());
EXPECT_FALSE(folder_node->GetMetaInfoMap());
ASSERT_TRUE(node->GetMetaInfoMap());
EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap());
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromModel) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
WriteTestDataToBookmarkModel();
StartSync();
ExpectBookmarkModelMatchesTestData();
const BookmarkNode* folder_node =
model_->AddFolder(model_->bookmark_bar_node(), 0,
base::ASCIIToUTF16("folder title"));
const BookmarkNode* node = model_->AddURL(folder_node, 0,
base::ASCIIToUTF16("node title"),
GURL("http://www.foo.com"));
ExpectModelMatch();
model_->SetNodeMetaInfo(folder_node, "folder", "foldervalue");
model_->SetNodeMetaInfo(node, "node", "nodevalue");
model_->SetNodeMetaInfo(node, "other", "othervalue");
ExpectModelMatch();
model_->DeleteNodeMetaInfo(folder_node, "folder");
model_->SetNodeMetaInfo(node, "node", "changednodevalue");
model_->DeleteNodeMetaInfo(node, "other");
model_->SetNodeMetaInfo(node, "newkey", "newkeyvalue");
ExpectModelMatch();
}
void GetTransactionVersions(
const BookmarkNode* root,
BookmarkNodeVersionMap* node_versions) {
node_versions->clear();
std::queue<const BookmarkNode*> nodes;
nodes.push(root);
while (!nodes.empty()) {
const BookmarkNode* n = nodes.front();
nodes.pop();
int64 version = n->sync_transaction_version();
EXPECT_NE(BookmarkNode::kInvalidSyncTransactionVersion, version);
(*node_versions)[n->id()] = version;
for (int i = 0; i < n->child_count(); ++i)
nodes.push(n->GetChild(i));
}
}
void ProfileSyncServiceBookmarkTestWithData::ExpectTransactionVersionMatch(
const BookmarkNode* node,
const BookmarkNodeVersionMap& version_expected) {
syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
BookmarkNodeVersionMap bnodes_versions;
GetTransactionVersions(node, &bnodes_versions);
for (BookmarkNodeVersionMap::const_iterator it = bnodes_versions.begin();
it != bnodes_versions.end(); ++it) {
syncer::ReadNode sync_node(&trans);
ASSERT_TRUE(model_associator_->InitSyncNodeFromChromeId(it->first,
&sync_node));
EXPECT_EQ(sync_node.GetEntry()->GetTransactionVersion(), it->second);
BookmarkNodeVersionMap::const_iterator expected_ver_it =
version_expected.find(it->first);
if (expected_ver_it != version_expected.end())
EXPECT_EQ(expected_ver_it->second, it->second);
}
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateTransactionVersion) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
base::MessageLoop::current()->RunUntilIdle();
BookmarkNodeVersionMap initial_versions;
{
syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0);
GetTransactionVersions(model_->root_node(), &initial_versions);
EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS),
initial_versions[model_->root_node()->id()]);
}
ExpectTransactionVersionMatch(model_->bookmark_bar_node(),
BookmarkNodeVersionMap());
ExpectTransactionVersionMatch(model_->other_node(),
BookmarkNodeVersionMap());
ExpectTransactionVersionMatch(model_->mobile_node(),
BookmarkNodeVersionMap());
const BookmarkNode* bookmark_bar = model_->bookmark_bar_node();
model_->Remove(bookmark_bar, 0);
base::MessageLoop::current()->RunUntilIdle();
BookmarkNodeVersionMap new_versions;
GetTransactionVersions(model_->root_node(), &new_versions);
EXPECT_EQ(initial_versions[model_->root_node()->id()] + 1,
new_versions[model_->root_node()->id()]);
ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions);
ExpectTransactionVersionMatch(model_->other_node(), initial_versions);
ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions);
const BookmarkNode* changed_bookmark =
model_->bookmark_bar_node()->GetChild(0);
model_->SetTitle(changed_bookmark, base::ASCIIToUTF16("test"));
base::MessageLoop::current()->RunUntilIdle();
GetTransactionVersions(model_->root_node(), &new_versions);
EXPECT_EQ(initial_versions[model_->root_node()->id()] + 2,
new_versions[model_->root_node()->id()]);
EXPECT_LT(initial_versions[changed_bookmark->id()],
new_versions[changed_bookmark->id()]);
initial_versions.erase(changed_bookmark->id());
ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions);
ExpectTransactionVersionMatch(model_->other_node(), initial_versions);
ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions);
}
TEST_F(ProfileSyncServiceBookmarkTestWithData, PersistenceError) {
LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
StartSync();
WriteTestDataToBookmarkModel();
base::MessageLoop::current()->RunUntilIdle();
BookmarkNodeVersionMap initial_versions;
{
syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0);
GetTransactionVersions(model_->root_node(), &initial_versions);
EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS),
initial_versions[model_->root_node()->id()]);
}
ExpectTransactionVersionMatch(model_->bookmark_bar_node(),
BookmarkNodeVersionMap());
ExpectTransactionVersionMatch(model_->other_node(),
BookmarkNodeVersionMap());
ExpectTransactionVersionMatch(model_->mobile_node(),
BookmarkNodeVersionMap());
StopSync();
int64 root_version = initial_versions[model_->root_node()->id()];
model_->SetNodeSyncTransactionVersion(model_->root_node(), root_version + 1);
EXPECT_FALSE(AssociateModels());
}
}
}