Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_service_bookmark_unittest.cc
bloba0c1673169ed5fabe4be9f770d46b4afe23b9c00
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // TODO(akalin): This file is basically just a unit test for
6 // BookmarkChangeProcessor. Write unit tests for
7 // BookmarkModelAssociator separately.
9 #include <map>
10 #include <queue>
11 #include <stack>
12 #include <vector>
14 #include "base/command_line.h"
15 #include "base/files/file_path.h"
16 #include "base/location.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/time/time.h"
25 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
26 #include "chrome/browser/bookmarks/chrome_bookmark_client.h"
27 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
28 #include "chrome/browser/sync/glue/bookmark_change_processor.h"
29 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/test/base/testing_profile.h"
32 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
33 #include "components/bookmarks/browser/bookmark_model.h"
34 #include "components/bookmarks/test/bookmark_test_helpers.h"
35 #include "components/sync_driver/data_type_error_handler.h"
36 #include "components/sync_driver/data_type_error_handler_mock.h"
37 #include "content/public/test/test_browser_thread_bundle.h"
38 #include "sync/api/sync_error.h"
39 #include "sync/internal_api/public/change_record.h"
40 #include "sync/internal_api/public/read_node.h"
41 #include "sync/internal_api/public/read_transaction.h"
42 #include "sync/internal_api/public/test/test_user_share.h"
43 #include "sync/internal_api/public/write_node.h"
44 #include "sync/internal_api/public/write_transaction.h"
45 #include "sync/internal_api/syncapi_internal.h"
46 #include "sync/syncable/mutable_entry.h" // TODO(tim): Remove. Bug 131130.
47 #include "testing/gmock/include/gmock/gmock.h"
48 #include "testing/gtest/include/gtest/gtest.h"
50 namespace browser_sync {
52 using bookmarks::BookmarkModel;
53 using bookmarks::BookmarkNode;
54 using syncer::BaseNode;
55 using testing::_;
56 using testing::InvokeWithoutArgs;
57 using testing::Mock;
58 using testing::StrictMock;
60 #if defined(OS_ANDROID) || defined(OS_IOS)
61 static const bool kExpectMobileBookmarks = true;
62 #else
63 static const bool kExpectMobileBookmarks = false;
64 #endif // defined(OS_ANDROID) || defined(OS_IOS)
66 namespace {
68 // FakeServerChange constructs a list of syncer::ChangeRecords while modifying
69 // the sync model, and can pass the ChangeRecord list to a
70 // syncer::SyncObserver (i.e., the ProfileSyncService) to test the client
71 // change-application behavior.
72 // Tests using FakeServerChange should be careful to avoid back-references,
73 // since FakeServerChange will send the edits in the order specified.
74 class FakeServerChange {
75 public:
76 explicit FakeServerChange(syncer::WriteTransaction* trans) : trans_(trans) {
79 // Pretend that the server told the syncer to add a bookmark object.
80 int64 AddWithMetaInfo(const std::string& title,
81 const std::string& url,
82 const BookmarkNode::MetaInfoMap* meta_info_map,
83 bool is_folder,
84 int64 parent_id,
85 int64 predecessor_id) {
86 syncer::ReadNode parent(trans_);
87 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
88 syncer::WriteNode node(trans_);
89 if (predecessor_id == 0) {
90 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
91 } else {
92 syncer::ReadNode predecessor(trans_);
93 EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id));
94 EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
95 EXPECT_TRUE(node.InitBookmarkByCreation(parent, &predecessor));
97 EXPECT_EQ(node.GetPredecessorId(), predecessor_id);
98 EXPECT_EQ(node.GetParentId(), parent_id);
99 node.SetIsFolder(is_folder);
100 node.SetTitle(title);
102 sync_pb::BookmarkSpecifics specifics(node.GetBookmarkSpecifics());
103 const base::Time creation_time(base::Time::Now());
104 specifics.set_creation_time_us(creation_time.ToInternalValue());
105 if (!is_folder)
106 specifics.set_url(url);
107 if (meta_info_map)
108 SetNodeMetaInfo(*meta_info_map, &specifics);
109 node.SetBookmarkSpecifics(specifics);
111 syncer::ChangeRecord record;
112 record.action = syncer::ChangeRecord::ACTION_ADD;
113 record.id = node.GetId();
114 changes_.push_back(record);
115 return node.GetId();
118 int64 Add(const std::string& title,
119 const std::string& url,
120 bool is_folder,
121 int64 parent_id,
122 int64 predecessor_id) {
123 return AddWithMetaInfo(title, url, NULL, is_folder, parent_id,
124 predecessor_id);
127 // Add a bookmark folder.
128 int64 AddFolder(const std::string& title,
129 int64 parent_id,
130 int64 predecessor_id) {
131 return Add(title, std::string(), true, parent_id, predecessor_id);
133 int64 AddFolderWithMetaInfo(const std::string& title,
134 const BookmarkNode::MetaInfoMap* meta_info_map,
135 int64 parent_id,
136 int64 predecessor_id) {
137 return AddWithMetaInfo(title, std::string(), meta_info_map, true, parent_id,
138 predecessor_id);
141 // Add a bookmark.
142 int64 AddURL(const std::string& title,
143 const std::string& url,
144 int64 parent_id,
145 int64 predecessor_id) {
146 return Add(title, url, false, parent_id, predecessor_id);
148 int64 AddURLWithMetaInfo(const std::string& title,
149 const std::string& url,
150 const BookmarkNode::MetaInfoMap* meta_info_map,
151 int64 parent_id,
152 int64 predecessor_id) {
153 return AddWithMetaInfo(title, url, meta_info_map, false, parent_id,
154 predecessor_id);
157 // Pretend that the server told the syncer to delete an object.
158 void Delete(int64 id) {
160 // Delete the sync node.
161 syncer::WriteNode node(trans_);
162 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
163 if (node.GetIsFolder())
164 EXPECT_FALSE(node.GetFirstChildId());
165 node.GetMutableEntryForTest()->PutServerIsDel(true);
166 node.Tombstone();
169 // Verify the deletion.
170 syncer::ReadNode node(trans_);
171 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, node.InitByIdLookup(id));
174 syncer::ChangeRecord record;
175 record.action = syncer::ChangeRecord::ACTION_DELETE;
176 record.id = id;
177 // Deletions are always first in the changelist, but we can't actually do
178 // WriteNode::Remove() on the node until its children are moved. So, as
179 // a practical matter, users of FakeServerChange must move or delete
180 // children before parents. Thus, we must insert the deletion record
181 // at the front of the vector.
182 changes_.insert(changes_.begin(), record);
185 // Set a new title value, and return the old value.
186 std::string ModifyTitle(int64 id, const std::string& new_title) {
187 syncer::WriteNode node(trans_);
188 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
189 std::string old_title = node.GetTitle();
190 node.SetTitle(new_title);
191 SetModified(id);
192 return old_title;
195 // Set a new parent and predecessor value. Return the old parent id.
196 // We could return the old predecessor id, but it turns out not to be
197 // very useful for assertions.
198 int64 ModifyPosition(int64 id, int64 parent_id, int64 predecessor_id) {
199 syncer::ReadNode parent(trans_);
200 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
201 syncer::WriteNode node(trans_);
202 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
203 int64 old_parent_id = node.GetParentId();
204 if (predecessor_id == 0) {
205 EXPECT_TRUE(node.SetPosition(parent, NULL));
206 } else {
207 syncer::ReadNode predecessor(trans_);
208 EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id));
209 EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
210 EXPECT_TRUE(node.SetPosition(parent, &predecessor));
212 SetModified(id);
213 return old_parent_id;
216 void ModifyCreationTime(int64 id, int64 creation_time_us) {
217 syncer::WriteNode node(trans_);
218 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
219 sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics();
220 specifics.set_creation_time_us(creation_time_us);
221 node.SetBookmarkSpecifics(specifics);
222 SetModified(id);
225 void ModifyMetaInfo(int64 id,
226 const BookmarkNode::MetaInfoMap& meta_info_map) {
227 syncer::WriteNode node(trans_);
228 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
229 sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics();
230 SetNodeMetaInfo(meta_info_map, &specifics);
231 node.SetBookmarkSpecifics(specifics);
232 SetModified(id);
235 // Pass the fake change list to |service|.
236 void ApplyPendingChanges(sync_driver::ChangeProcessor* processor) {
237 processor->ApplyChangesFromSyncModel(
238 trans_, 0, syncer::ImmutableChangeRecordList(&changes_));
241 const syncer::ChangeRecordList& changes() {
242 return changes_;
245 private:
246 // Helper function to push an ACTION_UPDATE record onto the back
247 // of the changelist.
248 void SetModified(int64 id) {
249 // Coalesce multi-property edits.
250 if (!changes_.empty() && changes_.back().id == id &&
251 changes_.back().action ==
252 syncer::ChangeRecord::ACTION_UPDATE)
253 return;
254 syncer::ChangeRecord record;
255 record.action = syncer::ChangeRecord::ACTION_UPDATE;
256 record.id = id;
257 changes_.push_back(record);
260 void SetNodeMetaInfo(const BookmarkNode::MetaInfoMap& meta_info_map,
261 sync_pb::BookmarkSpecifics* specifics) {
262 specifics->clear_meta_info();
263 // Deliberatly set MetaInfoMap entries in opposite order (compared
264 // to the implementation in BookmarkChangeProcessor) to ensure that
265 // (a) the implementation isn't sensitive to the order and
266 // (b) the original meta info isn't blindly overwritten by
267 // BookmarkChangeProcessor unless there is a real change.
268 BookmarkNode::MetaInfoMap::const_iterator it = meta_info_map.end();
269 while (it != meta_info_map.begin()) {
270 --it;
271 sync_pb::MetaInfo* meta_info = specifics->add_meta_info();
272 meta_info->set_key(it->first);
273 meta_info->set_value(it->second);
277 // The transaction on which everything happens.
278 syncer::WriteTransaction *trans_;
280 // The change list we construct.
281 syncer::ChangeRecordList changes_;
284 class ExtensiveChangesBookmarkModelObserver
285 : public bookmarks::BaseBookmarkModelObserver {
286 public:
287 ExtensiveChangesBookmarkModelObserver()
288 : started_count_(0),
289 completed_count_at_started_(0),
290 completed_count_(0) {}
292 void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) override {
293 ++started_count_;
294 completed_count_at_started_ = completed_count_;
297 void ExtensiveBookmarkChangesEnded(BookmarkModel* model) override {
298 ++completed_count_;
301 void BookmarkModelChanged() override {}
303 int get_started() const {
304 return started_count_;
307 int get_completed_count_at_started() const {
308 return completed_count_at_started_;
311 int get_completed() const {
312 return completed_count_;
315 private:
316 int started_count_;
317 int completed_count_at_started_;
318 int completed_count_;
320 DISALLOW_COPY_AND_ASSIGN(ExtensiveChangesBookmarkModelObserver);
324 class ProfileSyncServiceBookmarkTest : public testing::Test {
325 protected:
326 enum LoadOption { LOAD_FROM_STORAGE, DELETE_EXISTING_STORAGE };
327 enum SaveOption { SAVE_TO_STORAGE, DONT_SAVE_TO_STORAGE };
329 ProfileSyncServiceBookmarkTest()
330 : model_(NULL),
331 thread_bundle_(content::TestBrowserThreadBundle::DEFAULT),
332 local_merge_result_(syncer::BOOKMARKS),
333 syncer_merge_result_(syncer::BOOKMARKS) {}
335 virtual ~ProfileSyncServiceBookmarkTest() {
336 StopSync();
337 UnloadBookmarkModel();
340 virtual void SetUp() {
341 test_user_share_.SetUp();
344 virtual void TearDown() {
345 test_user_share_.TearDown();
348 bool CanSyncNode(const BookmarkNode* node) {
349 return model_->client()->CanSyncNode(node);
352 // Inserts a folder directly to the share.
353 // Do not use this after model association is complete.
355 // This function differs from the AddFolder() function declared elsewhere in
356 // this file in that it only affects the sync model. It would be invalid to
357 // change the sync model directly after ModelAssociation. This function can
358 // be invoked prior to model association to set up first-time sync model
359 // association scenarios.
360 int64 AddFolderToShare(syncer::WriteTransaction* trans, std::string title) {
361 EXPECT_FALSE(model_associator_);
363 // Be sure to call CreatePermanentBookmarkNodes(), otherwise this will fail.
364 syncer::ReadNode bookmark_bar(trans);
365 EXPECT_EQ(BaseNode::INIT_OK,
366 bookmark_bar.InitByTagLookupForBookmarks("bookmark_bar"));
368 syncer::WriteNode node(trans);
369 EXPECT_TRUE(node.InitBookmarkByCreation(bookmark_bar, NULL));
370 node.SetIsFolder(true);
371 node.SetTitle(title);
373 return node.GetId();
376 // Inserts a bookmark directly to the share.
377 // Do not use this after model association is complete.
379 // This function differs from the AddURL() function declared elsewhere in this
380 // file in that it only affects the sync model. It would be invalid to change
381 // the sync model directly after ModelAssociation. This function can be
382 // invoked prior to model association to set up first-time sync model
383 // association scenarios.
384 int64 AddBookmarkToShare(syncer::WriteTransaction *trans,
385 int64 parent_id,
386 std::string title) {
387 EXPECT_FALSE(model_associator_);
389 syncer::ReadNode parent(trans);
390 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
392 sync_pb::BookmarkSpecifics specifics;
393 specifics.set_url("http://www.google.com/search?q=" + title);
394 specifics.set_title(title);
396 syncer::WriteNode node(trans);
397 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
398 node.SetIsFolder(false);
399 node.SetTitle(title);
400 node.SetBookmarkSpecifics(specifics);
402 return node.GetId();
405 // Load (or re-load) the bookmark model. |load| controls use of the
406 // bookmarks file on disk. |save| controls whether the newly loaded
407 // bookmark model will write out a bookmark file as it goes.
408 void LoadBookmarkModel(LoadOption load, SaveOption save) {
409 bool delete_bookmarks = load == DELETE_EXISTING_STORAGE;
410 profile_.CreateBookmarkModel(delete_bookmarks);
411 model_ = BookmarkModelFactory::GetForProfile(&profile_);
412 bookmarks::test::WaitForBookmarkModelToLoad(model_);
413 // This noticeably speeds up the unit tests that request it.
414 if (save == DONT_SAVE_TO_STORAGE)
415 model_->ClearStore();
416 base::MessageLoop::current()->RunUntilIdle();
419 int GetSyncBookmarkCount() {
420 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
421 syncer::ReadNode node(&trans);
422 if (node.InitTypeRoot(syncer::BOOKMARKS) != syncer::BaseNode::INIT_OK)
423 return 0;
424 return node.GetTotalNodeCount();
427 // Creates the bookmark root node and the permanent nodes if they don't
428 // already exist.
429 bool CreatePermanentBookmarkNodes() {
430 bool root_exists = false;
431 syncer::ModelType type = syncer::BOOKMARKS;
433 syncer::WriteTransaction trans(FROM_HERE,
434 test_user_share_.user_share());
435 syncer::ReadNode uber_root(&trans);
436 uber_root.InitByRootLookup();
438 syncer::ReadNode root(&trans);
439 root_exists = (root.InitTypeRoot(type) == BaseNode::INIT_OK);
442 if (!root_exists) {
443 if (!syncer::TestUserShare::CreateRoot(type,
444 test_user_share_.user_share()))
445 return false;
448 const int kNumPermanentNodes = 3;
449 const std::string permanent_tags[kNumPermanentNodes] = {
450 #if defined(OS_IOS)
451 "synced_bookmarks",
452 #endif
453 "bookmark_bar",
454 "other_bookmarks",
455 #if !defined(OS_IOS)
456 "synced_bookmarks",
457 #endif
459 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
460 syncer::ReadNode root(&trans);
461 EXPECT_EQ(BaseNode::INIT_OK, root.InitTypeRoot(type));
463 // Loop through creating permanent nodes as necessary.
464 int64 last_child_id = syncer::kInvalidId;
465 for (int i = 0; i < kNumPermanentNodes; ++i) {
466 // First check if the node already exists. This is for tests that involve
467 // persistence and set up sync more than once.
468 syncer::ReadNode lookup(&trans);
469 if (lookup.InitByTagLookupForBookmarks(permanent_tags[i]) ==
470 syncer::ReadNode::INIT_OK) {
471 last_child_id = lookup.GetId();
472 continue;
475 // If it doesn't exist, create the permanent node at the end of the
476 // ordering.
477 syncer::ReadNode predecessor_node(&trans);
478 syncer::ReadNode* predecessor = NULL;
479 if (last_child_id != syncer::kInvalidId) {
480 EXPECT_EQ(BaseNode::INIT_OK,
481 predecessor_node.InitByIdLookup(last_child_id));
482 predecessor = &predecessor_node;
484 syncer::WriteNode node(&trans);
485 if (!node.InitBookmarkByCreation(root, predecessor))
486 return false;
487 node.SetIsFolder(true);
488 node.GetMutableEntryForTest()->PutUniqueServerTag(permanent_tags[i]);
489 node.SetTitle(permanent_tags[i]);
490 node.SetExternalId(0);
491 last_child_id = node.GetId();
493 return true;
496 bool AssociateModels() {
497 DCHECK(!model_associator_);
499 // Set up model associator.
500 model_associator_.reset(new BookmarkModelAssociator(
501 BookmarkModelFactory::GetForProfile(&profile_),
502 &profile_,
503 test_user_share_.user_share(),
504 &mock_error_handler_,
505 kExpectMobileBookmarks));
507 local_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS);
508 syncer_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS);
509 int local_count_before = model_->root_node()->GetTotalNodeCount();
510 int syncer_count_before = GetSyncBookmarkCount();
512 syncer::SyncError error = model_associator_->AssociateModels(
513 &local_merge_result_,
514 &syncer_merge_result_);
515 if (error.IsSet())
516 return false;
518 base::MessageLoop::current()->RunUntilIdle();
520 // Verify the merge results were calculated properly.
521 EXPECT_EQ(local_count_before,
522 local_merge_result_.num_items_before_association());
523 EXPECT_EQ(syncer_count_before,
524 syncer_merge_result_.num_items_before_association());
525 EXPECT_EQ(local_merge_result_.num_items_after_association(),
526 local_merge_result_.num_items_before_association() +
527 local_merge_result_.num_items_added() -
528 local_merge_result_.num_items_deleted());
529 EXPECT_EQ(syncer_merge_result_.num_items_after_association(),
530 syncer_merge_result_.num_items_before_association() +
531 syncer_merge_result_.num_items_added() -
532 syncer_merge_result_.num_items_deleted());
533 EXPECT_EQ(model_->root_node()->GetTotalNodeCount(),
534 local_merge_result_.num_items_after_association());
535 EXPECT_EQ(GetSyncBookmarkCount(),
536 syncer_merge_result_.num_items_after_association());
537 return true;
540 void StartSync() {
541 test_user_share_.Reload();
543 ASSERT_TRUE(CreatePermanentBookmarkNodes());
544 ASSERT_TRUE(AssociateModels());
546 // Set up change processor.
547 change_processor_.reset(
548 new BookmarkChangeProcessor(&profile_,
549 model_associator_.get(),
550 &mock_error_handler_));
551 change_processor_->Start(test_user_share_.user_share());
554 void StopSync() {
555 change_processor_.reset();
556 if (model_associator_) {
557 syncer::SyncError error = model_associator_->DisassociateModels();
558 EXPECT_FALSE(error.IsSet());
560 model_associator_.reset();
562 base::MessageLoop::current()->RunUntilIdle();
564 // TODO(akalin): Actually close the database and flush it to disk
565 // (and make StartSync reload from disk). This would require
566 // refactoring TestUserShare.
569 void UnloadBookmarkModel() {
570 profile_.CreateBookmarkModel(false /* delete_bookmarks */);
571 model_ = NULL;
572 base::MessageLoop::current()->RunUntilIdle();
575 bool InitSyncNodeFromChromeNode(const BookmarkNode* bnode,
576 syncer::BaseNode* sync_node) {
577 return model_associator_->InitSyncNodeFromChromeId(bnode->id(),
578 sync_node);
581 void ExpectSyncerNodeMatching(syncer::BaseTransaction* trans,
582 const BookmarkNode* bnode) {
583 std::string truncated_title = base::UTF16ToUTF8(bnode->GetTitle());
584 syncer::SyncAPINameToServerName(truncated_title, &truncated_title);
585 base::TruncateUTF8ToByteSize(truncated_title, 255, &truncated_title);
586 syncer::ServerNameToSyncAPIName(truncated_title, &truncated_title);
588 syncer::ReadNode gnode(trans);
589 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnode, &gnode));
590 // Non-root node titles and parents must match.
591 if (!model_->is_permanent_node(bnode)) {
592 EXPECT_EQ(truncated_title, gnode.GetTitle());
593 EXPECT_EQ(
594 model_associator_->GetChromeNodeFromSyncId(gnode.GetParentId()),
595 bnode->parent());
597 EXPECT_EQ(bnode->is_folder(), gnode.GetIsFolder());
598 if (bnode->is_url())
599 EXPECT_EQ(bnode->url(), GURL(gnode.GetBookmarkSpecifics().url()));
601 // Check that meta info matches.
602 const BookmarkNode::MetaInfoMap* meta_info_map = bnode->GetMetaInfoMap();
603 sync_pb::BookmarkSpecifics specifics = gnode.GetBookmarkSpecifics();
604 if (!meta_info_map) {
605 EXPECT_EQ(0, specifics.meta_info_size());
606 } else {
607 EXPECT_EQ(meta_info_map->size(),
608 static_cast<size_t>(specifics.meta_info_size()));
609 for (int i = 0; i < specifics.meta_info_size(); i++) {
610 BookmarkNode::MetaInfoMap::const_iterator it =
611 meta_info_map->find(specifics.meta_info(i).key());
612 EXPECT_TRUE(it != meta_info_map->end());
613 EXPECT_EQ(it->second, specifics.meta_info(i).value());
617 // Check for position matches.
618 int browser_index = bnode->parent()->GetIndexOf(bnode);
619 if (browser_index == 0) {
620 EXPECT_EQ(gnode.GetPredecessorId(), 0);
621 } else {
622 const BookmarkNode* bprev =
623 bnode->parent()->GetChild(browser_index - 1);
624 syncer::ReadNode gprev(trans);
625 ASSERT_TRUE(InitSyncNodeFromChromeNode(bprev, &gprev));
626 EXPECT_EQ(gnode.GetPredecessorId(), gprev.GetId());
627 EXPECT_EQ(gnode.GetParentId(), gprev.GetParentId());
629 // Note: the managed node is the last child of the root_node but isn't
630 // synced; if CanSyncNode() is false then there is no next node to sync.
631 const BookmarkNode* bnext = NULL;
632 if (browser_index + 1 < bnode->parent()->child_count())
633 bnext = bnode->parent()->GetChild(browser_index + 1);
634 if (!bnext || !CanSyncNode(bnext)) {
635 EXPECT_EQ(gnode.GetSuccessorId(), 0);
636 } else {
637 syncer::ReadNode gnext(trans);
638 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnext, &gnext));
639 EXPECT_EQ(gnode.GetSuccessorId(), gnext.GetId());
640 EXPECT_EQ(gnode.GetParentId(), gnext.GetParentId());
642 if (!bnode->empty())
643 EXPECT_TRUE(gnode.GetFirstChildId());
646 void ExpectSyncerNodeMatching(const BookmarkNode* bnode) {
647 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
648 ExpectSyncerNodeMatching(&trans, bnode);
651 void ExpectBrowserNodeMatching(syncer::BaseTransaction* trans,
652 int64 sync_id) {
653 EXPECT_TRUE(sync_id);
654 const BookmarkNode* bnode =
655 model_associator_->GetChromeNodeFromSyncId(sync_id);
656 ASSERT_TRUE(bnode);
657 ASSERT_TRUE(CanSyncNode(bnode));
659 int64 id = model_associator_->GetSyncIdFromChromeId(bnode->id());
660 EXPECT_EQ(id, sync_id);
661 ExpectSyncerNodeMatching(trans, bnode);
664 void ExpectBrowserNodeUnknown(int64 sync_id) {
665 EXPECT_FALSE(model_associator_->GetChromeNodeFromSyncId(sync_id));
668 void ExpectBrowserNodeKnown(int64 sync_id) {
669 EXPECT_TRUE(model_associator_->GetChromeNodeFromSyncId(sync_id));
672 void ExpectSyncerNodeKnown(const BookmarkNode* node) {
673 int64 sync_id = model_associator_->GetSyncIdFromChromeId(node->id());
674 EXPECT_NE(sync_id, syncer::kInvalidId);
677 void ExpectSyncerNodeUnknown(const BookmarkNode* node) {
678 int64 sync_id = model_associator_->GetSyncIdFromChromeId(node->id());
679 EXPECT_EQ(sync_id, syncer::kInvalidId);
682 void ExpectBrowserNodeTitle(int64 sync_id, const std::string& title) {
683 const BookmarkNode* bnode =
684 model_associator_->GetChromeNodeFromSyncId(sync_id);
685 ASSERT_TRUE(bnode);
686 EXPECT_EQ(bnode->GetTitle(), base::UTF8ToUTF16(title));
689 void ExpectBrowserNodeURL(int64 sync_id, const std::string& url) {
690 const BookmarkNode* bnode =
691 model_associator_->GetChromeNodeFromSyncId(sync_id);
692 ASSERT_TRUE(bnode);
693 EXPECT_EQ(GURL(url), bnode->url());
696 void ExpectBrowserNodeParent(int64 sync_id, int64 parent_sync_id) {
697 const BookmarkNode* node =
698 model_associator_->GetChromeNodeFromSyncId(sync_id);
699 ASSERT_TRUE(node);
700 const BookmarkNode* parent =
701 model_associator_->GetChromeNodeFromSyncId(parent_sync_id);
702 EXPECT_TRUE(parent);
703 EXPECT_EQ(node->parent(), parent);
706 void ExpectModelMatch(syncer::BaseTransaction* trans) {
707 const BookmarkNode* root = model_->root_node();
708 #if defined(OS_IOS)
709 EXPECT_EQ(root->GetIndexOf(model_->mobile_node()), 0);
710 EXPECT_EQ(root->GetIndexOf(model_->bookmark_bar_node()), 1);
711 EXPECT_EQ(root->GetIndexOf(model_->other_node()), 2);
712 #else
713 EXPECT_EQ(root->GetIndexOf(model_->bookmark_bar_node()), 0);
714 EXPECT_EQ(root->GetIndexOf(model_->other_node()), 1);
715 EXPECT_EQ(root->GetIndexOf(model_->mobile_node()), 2);
716 #endif
718 std::stack<int64> stack;
719 stack.push(bookmark_bar_id());
720 while (!stack.empty()) {
721 int64 id = stack.top();
722 stack.pop();
723 if (!id) continue;
725 ExpectBrowserNodeMatching(trans, id);
727 syncer::ReadNode gnode(trans);
728 ASSERT_EQ(BaseNode::INIT_OK, gnode.InitByIdLookup(id));
729 stack.push(gnode.GetSuccessorId());
730 if (gnode.GetIsFolder())
731 stack.push(gnode.GetFirstChildId());
735 void ExpectModelMatch() {
736 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
737 ExpectModelMatch(&trans);
740 int64 mobile_bookmarks_id() {
741 return
742 model_associator_->GetSyncIdFromChromeId(model_->mobile_node()->id());
745 int64 other_bookmarks_id() {
746 return
747 model_associator_->GetSyncIdFromChromeId(model_->other_node()->id());
750 int64 bookmark_bar_id() {
751 return model_associator_->GetSyncIdFromChromeId(
752 model_->bookmark_bar_node()->id());
755 protected:
756 TestingProfile profile_;
757 BookmarkModel* model_;
758 syncer::TestUserShare test_user_share_;
759 scoped_ptr<BookmarkChangeProcessor> change_processor_;
760 StrictMock<sync_driver::DataTypeErrorHandlerMock> mock_error_handler_;
761 scoped_ptr<BookmarkModelAssociator> model_associator_;
763 private:
764 content::TestBrowserThreadBundle thread_bundle_;
765 syncer::SyncMergeResult local_merge_result_;
766 syncer::SyncMergeResult syncer_merge_result_;
769 TEST_F(ProfileSyncServiceBookmarkTest, InitialState) {
770 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
771 StartSync();
773 EXPECT_TRUE(other_bookmarks_id());
774 EXPECT_TRUE(bookmark_bar_id());
775 EXPECT_TRUE(mobile_bookmarks_id());
777 ExpectModelMatch();
780 // Populate the sync database then start model association. Sync's bookmarks
781 // should end up being copied into the native model, resulting in a successful
782 // "ExpectModelMatch()".
784 // This code has some use for verifying correctness. It's also a very useful
785 // for profiling bookmark ModelAssociation, an important part of some first-time
786 // sync scenarios. Simply increase the kNumFolders and kNumBookmarksPerFolder
787 // as desired, then run the test under a profiler to find hot spots in the model
788 // association code.
789 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) {
790 const int kNumBookmarksPerFolder = 10;
791 const int kNumFolders = 10;
793 CreatePermanentBookmarkNodes();
796 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
797 for (int i = 0; i < kNumFolders; ++i) {
798 int64 folder_id = AddFolderToShare(&trans,
799 base::StringPrintf("folder%05d", i));
800 for (int j = 0; j < kNumBookmarksPerFolder; ++j) {
801 AddBookmarkToShare(&trans,
802 folder_id,
803 base::StringPrintf("bookmark%05d", j));
808 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
809 StartSync();
811 ExpectModelMatch();
815 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) {
816 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
817 StartSync();
819 // Test addition.
820 const BookmarkNode* folder =
821 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar"));
822 ExpectSyncerNodeMatching(folder);
823 ExpectModelMatch();
824 const BookmarkNode* folder2 =
825 model_->AddFolder(folder, 0, base::ASCIIToUTF16("nested"));
826 ExpectSyncerNodeMatching(folder2);
827 ExpectModelMatch();
828 const BookmarkNode* url1 = model_->AddURL(
829 folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"),
830 GURL("http://www.easypie.com/"));
831 ExpectSyncerNodeMatching(url1);
832 ExpectModelMatch();
833 const BookmarkNode* url2 = model_->AddURL(
834 folder, 1, base::ASCIIToUTF16("Airplanes"),
835 GURL("http://www.easyjet.com/"));
836 ExpectSyncerNodeMatching(url2);
837 ExpectModelMatch();
838 // Test addition.
839 const BookmarkNode* mobile_folder =
840 model_->AddFolder(model_->mobile_node(), 0, base::ASCIIToUTF16("pie"));
841 ExpectSyncerNodeMatching(mobile_folder);
842 ExpectModelMatch();
844 // Test modification.
845 model_->SetTitle(url2, base::ASCIIToUTF16("EasyJet"));
846 ExpectModelMatch();
847 model_->Move(url1, folder2, 0);
848 ExpectModelMatch();
849 model_->Move(folder2, model_->bookmark_bar_node(), 0);
850 ExpectModelMatch();
851 model_->SetTitle(folder2, base::ASCIIToUTF16("Not Nested"));
852 ExpectModelMatch();
853 model_->Move(folder, folder2, 0);
854 ExpectModelMatch();
855 model_->SetTitle(folder, base::ASCIIToUTF16("who's nested now?"));
856 ExpectModelMatch();
857 model_->Copy(url2, model_->bookmark_bar_node(), 0);
858 ExpectModelMatch();
859 model_->SetTitle(mobile_folder, base::ASCIIToUTF16("strawberry"));
860 ExpectModelMatch();
862 // Test deletion.
863 // Delete a single item.
864 model_->Remove(url2->parent(), url2->parent()->GetIndexOf(url2));
865 ExpectModelMatch();
866 // Delete an item with several children.
867 model_->Remove(folder2->parent(),
868 folder2->parent()->GetIndexOf(folder2));
869 ExpectModelMatch();
870 model_->Remove(model_->mobile_node(), 0);
871 ExpectModelMatch();
874 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeProcessing) {
875 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
876 StartSync();
878 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
880 FakeServerChange adds(&trans);
881 int64 f1 = adds.AddFolder("Server Folder B", bookmark_bar_id(), 0);
882 int64 f2 = adds.AddFolder("Server Folder A", bookmark_bar_id(), f1);
883 int64 u1 = adds.AddURL("Some old site", "ftp://nifty.andrew.cmu.edu/",
884 bookmark_bar_id(), f2);
885 int64 u2 = adds.AddURL("Nifty", "ftp://nifty.andrew.cmu.edu/", f1, 0);
886 // u3 is a duplicate URL
887 int64 u3 = adds.AddURL("Nifty2", "ftp://nifty.andrew.cmu.edu/", f1, u2);
888 // u4 is a duplicate title, different URL.
889 adds.AddURL("Some old site", "http://slog.thestranger.com/",
890 bookmark_bar_id(), u1);
891 // u5 tests an empty-string title.
892 std::string javascript_url(
893 "javascript:(function(){var w=window.open(" \
894 "'about:blank','gnotesWin','location=0,menubar=0," \
895 "scrollbars=0,status=0,toolbar=0,width=300," \
896 "height=300,resizable');});");
897 adds.AddURL(std::string(), javascript_url, other_bookmarks_id(), 0);
898 int64 u6 = adds.AddURL(
899 "Sync1", "http://www.syncable.edu/", mobile_bookmarks_id(), 0);
901 syncer::ChangeRecordList::const_iterator it;
902 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
903 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
904 ExpectBrowserNodeUnknown(it->id);
906 adds.ApplyPendingChanges(change_processor_.get());
908 // Make sure the bookmark model received all of the nodes in |adds|.
909 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
910 ExpectBrowserNodeMatching(&trans, it->id);
911 ExpectModelMatch(&trans);
913 // Part two: test modifications.
914 FakeServerChange mods(&trans);
915 // Mess with u2, and move it into empty folder f2
916 // TODO(ncarter): Determine if we allow ModifyURL ops or not.
917 /* std::string u2_old_url = mods.ModifyURL(u2, "http://www.google.com"); */
918 std::string u2_old_title = mods.ModifyTitle(u2, "The Google");
919 int64 u2_old_parent = mods.ModifyPosition(u2, f2, 0);
921 // Now move f1 after u2.
922 std::string f1_old_title = mods.ModifyTitle(f1, "Server Folder C");
923 int64 f1_old_parent = mods.ModifyPosition(f1, f2, u2);
925 // Then add u3 after f1.
926 int64 u3_old_parent = mods.ModifyPosition(u3, f2, f1);
928 std::string u6_old_title = mods.ModifyTitle(u6, "Mobile Folder A");
930 // Test that the property changes have not yet taken effect.
931 ExpectBrowserNodeTitle(u2, u2_old_title);
932 /* ExpectBrowserNodeURL(u2, u2_old_url); */
933 ExpectBrowserNodeParent(u2, u2_old_parent);
935 ExpectBrowserNodeTitle(f1, f1_old_title);
936 ExpectBrowserNodeParent(f1, f1_old_parent);
938 ExpectBrowserNodeParent(u3, u3_old_parent);
940 ExpectBrowserNodeTitle(u6, u6_old_title);
942 // Apply the changes.
943 mods.ApplyPendingChanges(change_processor_.get());
945 // Check for successful application.
946 for (it = mods.changes().begin(); it != mods.changes().end(); ++it)
947 ExpectBrowserNodeMatching(&trans, it->id);
948 ExpectModelMatch(&trans);
950 // Part 3: Test URL deletion.
951 FakeServerChange dels(&trans);
952 dels.Delete(u2);
953 dels.Delete(u3);
954 dels.Delete(u6);
956 ExpectBrowserNodeKnown(u2);
957 ExpectBrowserNodeKnown(u3);
959 dels.ApplyPendingChanges(change_processor_.get());
961 ExpectBrowserNodeUnknown(u2);
962 ExpectBrowserNodeUnknown(u3);
963 ExpectBrowserNodeUnknown(u6);
964 ExpectModelMatch(&trans);
967 // Tests a specific case in ApplyModelChanges where we move the
968 // children out from under a parent, and then delete the parent
969 // in the same changelist. The delete shows up first in the changelist,
970 // requiring the children to be moved to a temporary location.
971 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeRequiringFosterParent) {
972 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
973 StartSync();
975 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
977 // Stress the immediate children of other_node because that's where
978 // ApplyModelChanges puts a temporary foster parent node.
979 std::string url("http://dev.chromium.org/");
980 FakeServerChange adds(&trans);
981 int64 f0 = other_bookmarks_id(); // + other_node
982 int64 f1 = adds.AddFolder("f1", f0, 0); // + f1
983 int64 f2 = adds.AddFolder("f2", f1, 0); // + f2
984 int64 u3 = adds.AddURL( "u3", url, f2, 0); // + u3 NOLINT
985 int64 u4 = adds.AddURL( "u4", url, f2, u3); // + u4 NOLINT
986 int64 u5 = adds.AddURL( "u5", url, f1, f2); // + u5 NOLINT
987 int64 f6 = adds.AddFolder("f6", f1, u5); // + f6
988 int64 u7 = adds.AddURL( "u7", url, f0, f1); // + u7 NOLINT
990 syncer::ChangeRecordList::const_iterator it;
991 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
992 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
993 ExpectBrowserNodeUnknown(it->id);
995 adds.ApplyPendingChanges(change_processor_.get());
997 // Make sure the bookmark model received all of the nodes in |adds|.
998 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
999 ExpectBrowserNodeMatching(&trans, it->id);
1000 ExpectModelMatch(&trans);
1002 // We have to do the moves before the deletions, but FakeServerChange will
1003 // put the deletion at the front of the changelist.
1004 FakeServerChange ops(&trans);
1005 ops.ModifyPosition(f6, other_bookmarks_id(), 0);
1006 ops.ModifyPosition(u3, other_bookmarks_id(), f1); // Prev == f1 is OK here.
1007 ops.ModifyPosition(f2, other_bookmarks_id(), u7);
1008 ops.ModifyPosition(u7, f2, 0);
1009 ops.ModifyPosition(u4, other_bookmarks_id(), f2);
1010 ops.ModifyPosition(u5, f6, 0);
1011 ops.Delete(f1);
1013 ops.ApplyPendingChanges(change_processor_.get());
1015 ExpectModelMatch(&trans);
1018 // Simulate a server change record containing a valid but non-canonical URL.
1019 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeWithNonCanonicalURL) {
1020 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1021 StartSync();
1024 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1026 FakeServerChange adds(&trans);
1027 std::string url("http://dev.chromium.org");
1028 EXPECT_NE(GURL(url).spec(), url);
1029 adds.AddURL("u1", url, other_bookmarks_id(), 0);
1031 adds.ApplyPendingChanges(change_processor_.get());
1033 EXPECT_EQ(1, model_->other_node()->child_count());
1034 ExpectModelMatch(&trans);
1037 // Now reboot the sync service, forcing a merge step.
1038 StopSync();
1039 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1040 StartSync();
1042 // There should still be just the one bookmark.
1043 EXPECT_EQ(1, model_->other_node()->child_count());
1044 ExpectModelMatch();
1047 // Simulate a server change record containing an invalid URL (per GURL).
1048 // TODO(ncarter): Disabled due to crashes. Fix bug 1677563.
1049 TEST_F(ProfileSyncServiceBookmarkTest, DISABLED_ServerChangeWithInvalidURL) {
1050 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1051 StartSync();
1053 int child_count = 0;
1055 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1057 FakeServerChange adds(&trans);
1058 std::string url("x");
1059 EXPECT_FALSE(GURL(url).is_valid());
1060 adds.AddURL("u1", url, other_bookmarks_id(), 0);
1062 adds.ApplyPendingChanges(change_processor_.get());
1064 // We're lenient about what should happen -- the model could wind up with
1065 // the node or without it; but things should be consistent, and we
1066 // shouldn't crash.
1067 child_count = model_->other_node()->child_count();
1068 EXPECT_TRUE(child_count == 0 || child_count == 1);
1069 ExpectModelMatch(&trans);
1072 // Now reboot the sync service, forcing a merge step.
1073 StopSync();
1074 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1075 StartSync();
1077 // Things ought not to have changed.
1078 EXPECT_EQ(model_->other_node()->child_count(), child_count);
1079 ExpectModelMatch();
1083 // Test strings that might pose a problem if the titles ever became used as
1084 // file names in the sync backend.
1085 TEST_F(ProfileSyncServiceBookmarkTest, CornerCaseNames) {
1086 // TODO(ncarter): Bug 1570238 explains the failure of this test.
1087 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1088 StartSync();
1090 const char* names[] = {
1091 // The empty string.
1093 // Illegal Windows filenames.
1094 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",
1095 "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3",
1096 "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
1097 // Current/parent directory markers.
1098 ".", "..", "...",
1099 // Files created automatically by the Windows shell.
1100 "Thumbs.db", ".DS_Store",
1101 // Names including Win32-illegal characters, and path separators.
1102 "foo/bar", "foo\\bar", "foo?bar", "foo:bar", "foo|bar", "foo\"bar",
1103 "foo'bar", "foo<bar", "foo>bar", "foo%bar", "foo*bar", "foo]bar",
1104 "foo[bar",
1105 // A name with title > 255 characters
1106 "012345678901234567890123456789012345678901234567890123456789012345678901"
1107 "234567890123456789012345678901234567890123456789012345678901234567890123"
1108 "456789012345678901234567890123456789012345678901234567890123456789012345"
1109 "678901234567890123456789012345678901234567890123456789012345678901234567"
1110 "890123456789"
1112 // Create both folders and bookmarks using each name.
1113 GURL url("http://www.doublemint.com");
1114 for (size_t i = 0; i < arraysize(names); ++i) {
1115 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16(names[i]));
1116 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16(names[i]), url);
1119 // Verify that the browser model matches the sync model.
1120 EXPECT_EQ(static_cast<size_t>(model_->other_node()->child_count()),
1121 2*arraysize(names));
1122 ExpectModelMatch();
1124 // Restart and re-associate. Verify things still match.
1125 StopSync();
1126 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1127 StartSync();
1128 EXPECT_EQ(static_cast<size_t>(model_->other_node()->child_count()),
1129 2*arraysize(names));
1130 ExpectModelMatch();
1133 // Stress the internal representation of position by sparse numbers. We want
1134 // to repeatedly bisect the range of available positions, to force the
1135 // syncer code to renumber its ranges. Pick a number big enough so that it
1136 // would exhaust 32bits of room between items a couple of times.
1137 TEST_F(ProfileSyncServiceBookmarkTest, RepeatedMiddleInsertion) {
1138 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1139 StartSync();
1141 static const int kTimesToInsert = 256;
1143 // Create two book-end nodes to insert between.
1144 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("Alpha"));
1145 model_->AddFolder(model_->other_node(), 1, base::ASCIIToUTF16("Omega"));
1146 int count = 2;
1148 // Test insertion in first half of range by repeatedly inserting in second
1149 // position.
1150 for (int i = 0; i < kTimesToInsert; ++i) {
1151 base::string16 title =
1152 base::ASCIIToUTF16("Pre-insertion ") + base::IntToString16(i);
1153 model_->AddFolder(model_->other_node(), 1, title);
1154 count++;
1157 // Test insertion in second half of range by repeatedly inserting in
1158 // second-to-last position.
1159 for (int i = 0; i < kTimesToInsert; ++i) {
1160 base::string16 title =
1161 base::ASCIIToUTF16("Post-insertion ") + base::IntToString16(i);
1162 model_->AddFolder(model_->other_node(), count - 1, title);
1163 count++;
1166 // Verify that the browser model matches the sync model.
1167 EXPECT_EQ(model_->other_node()->child_count(), count);
1168 ExpectModelMatch();
1171 // Introduce a consistency violation into the model, and see that it
1172 // puts itself into a lame, error state.
1173 TEST_F(ProfileSyncServiceBookmarkTest, UnrecoverableErrorSuspendsService) {
1174 EXPECT_CALL(mock_error_handler_,
1175 OnSingleDataTypeUnrecoverableError(_));
1177 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1178 StartSync();
1180 // Add a node which will be the target of the consistency violation.
1181 const BookmarkNode* node =
1182 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("node"));
1183 ExpectSyncerNodeMatching(node);
1185 // Now destroy the syncer node as if we were the ProfileSyncService without
1186 // updating the ProfileSyncService state. This should introduce
1187 // inconsistency between the two models.
1189 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1190 syncer::WriteNode sync_node(&trans);
1191 ASSERT_TRUE(InitSyncNodeFromChromeNode(node, &sync_node));
1192 sync_node.Tombstone();
1194 // The models don't match at this point, but the ProfileSyncService
1195 // doesn't know it yet.
1196 ExpectSyncerNodeKnown(node);
1198 // Add a child to the inconsistent node. This should cause detection of the
1199 // problem and the syncer should stop processing changes.
1200 model_->AddFolder(node, 0, base::ASCIIToUTF16("nested"));
1203 // See what happens if we run model association when there are two exact URL
1204 // duplicate bookmarks. The BookmarkModelAssociator should not fall over when
1205 // this happens.
1206 TEST_F(ProfileSyncServiceBookmarkTest, MergeDuplicates) {
1207 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1208 StartSync();
1210 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("Dup"),
1211 GURL("http://dup.com/"));
1212 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("Dup"),
1213 GURL("http://dup.com/"));
1215 EXPECT_EQ(2, model_->other_node()->child_count());
1217 // Restart the sync service to trigger model association.
1218 StopSync();
1219 StartSync();
1221 EXPECT_EQ(2, model_->other_node()->child_count());
1222 ExpectModelMatch();
1225 TEST_F(ProfileSyncServiceBookmarkTest, ApplySyncDeletesFromJournal) {
1226 // Initialize sync model and bookmark model as:
1227 // URL 0
1228 // Folder 1
1229 // |-- URL 1
1230 // +-- Folder 2
1231 // +-- URL 2
1232 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1233 int64 u0 = 0;
1234 int64 f1 = 0;
1235 int64 u1 = 0;
1236 int64 f2 = 0;
1237 int64 u2 = 0;
1238 StartSync();
1239 int fixed_sync_bk_count = GetSyncBookmarkCount();
1241 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1242 FakeServerChange adds(&trans);
1243 u0 = adds.AddURL("URL 0", "http://plus.google.com/", bookmark_bar_id(), 0);
1244 f1 = adds.AddFolder("Folder 1", bookmark_bar_id(), u0);
1245 u1 = adds.AddURL("URL 1", "http://www.google.com/", f1, 0);
1246 f2 = adds.AddFolder("Folder 2", f1, u1);
1247 u2 = adds.AddURL("URL 2", "http://mail.google.com/", f2, 0);
1248 adds.ApplyPendingChanges(change_processor_.get());
1250 StopSync();
1252 // Reload bookmark model and disable model saving to make sync changes not
1253 // persisted.
1254 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1255 EXPECT_EQ(6, model_->bookmark_bar_node()->GetTotalNodeCount());
1256 EXPECT_EQ(fixed_sync_bk_count + 5, GetSyncBookmarkCount());
1257 StartSync();
1259 // Remove all folders/bookmarks except u3 added above.
1260 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1261 FakeServerChange dels(&trans);
1262 dels.Delete(u2);
1263 dels.Delete(f2);
1264 dels.Delete(u1);
1265 dels.Delete(f1);
1266 dels.ApplyPendingChanges(change_processor_.get());
1268 StopSync();
1269 // Bookmark bar itself and u0 remain.
1270 EXPECT_EQ(2, model_->bookmark_bar_node()->GetTotalNodeCount());
1272 // Reload bookmarks including ones deleted in sync model from storage.
1273 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1274 EXPECT_EQ(6, model_->bookmark_bar_node()->GetTotalNodeCount());
1275 // Add a bookmark under f1 when sync is off so that f1 will not be
1276 // deleted even when f1 matches delete journal because it's not empty.
1277 model_->AddURL(model_->bookmark_bar_node()->GetChild(1),
1278 0, base::UTF8ToUTF16("local"), GURL("http://www.youtube.com"));
1279 // Sync model has fixed bookmarks nodes and u3.
1280 EXPECT_EQ(fixed_sync_bk_count + 1, GetSyncBookmarkCount());
1281 StartSync();
1282 // Expect 4 bookmarks after model association because u2, f2, u1 are removed
1283 // by delete journal, f1 is not removed by delete journal because it's
1284 // not empty due to www.youtube.com added above.
1285 EXPECT_EQ(4, model_->bookmark_bar_node()->GetTotalNodeCount());
1286 EXPECT_EQ(base::UTF8ToUTF16("URL 0"),
1287 model_->bookmark_bar_node()->GetChild(0)->GetTitle());
1288 EXPECT_EQ(base::UTF8ToUTF16("Folder 1"),
1289 model_->bookmark_bar_node()->GetChild(1)->GetTitle());
1290 EXPECT_EQ(base::UTF8ToUTF16("local"),
1291 model_->bookmark_bar_node()->GetChild(1)->GetChild(0)->GetTitle());
1292 StopSync();
1294 // Verify purging of delete journals.
1295 // Delete journals for u2, f2, u1 remains because they are used in last
1296 // association.
1297 EXPECT_EQ(3u, test_user_share_.GetDeleteJournalSize());
1298 StartSync();
1299 StopSync();
1300 // Reload again and all delete journals should be gone because none is used
1301 // in last association.
1302 ASSERT_TRUE(test_user_share_.Reload());
1303 EXPECT_EQ(0u, test_user_share_.GetDeleteJournalSize());
1306 struct TestData {
1307 const char* title;
1308 const char* url;
1311 // Map from bookmark node ID to its version.
1312 typedef std::map<int64, int64> BookmarkNodeVersionMap;
1314 // TODO(ncarter): Integrate the existing TestNode/PopulateNodeFromString code
1315 // in the bookmark model unittest, to make it simpler to set up test data
1316 // here (and reduce the amount of duplication among tests), and to reduce the
1317 // duplication.
1318 class ProfileSyncServiceBookmarkTestWithData
1319 : public ProfileSyncServiceBookmarkTest {
1320 public:
1321 ProfileSyncServiceBookmarkTestWithData();
1323 protected:
1324 // Populates or compares children of the given bookmark node from/with the
1325 // given test data array with the given size. |running_count| is updated as
1326 // urls are added. It is used to set the creation date (or test the creation
1327 // date for CompareWithTestData()).
1328 void PopulateFromTestData(const BookmarkNode* node,
1329 const TestData* data,
1330 int size,
1331 int* running_count);
1332 void CompareWithTestData(const BookmarkNode* node,
1333 const TestData* data,
1334 int size,
1335 int* running_count);
1337 void ExpectBookmarkModelMatchesTestData();
1338 void WriteTestDataToBookmarkModel();
1340 // Output transaction versions of |node| and nodes under it to
1341 // |node_versions|.
1342 void GetTransactionVersions(const BookmarkNode* root,
1343 BookmarkNodeVersionMap* node_versions);
1345 // Verify transaction versions of bookmark nodes and sync nodes are equal
1346 // recursively. If node is in |version_expected|, versions should match
1347 // there, too.
1348 void ExpectTransactionVersionMatch(
1349 const BookmarkNode* node,
1350 const BookmarkNodeVersionMap& version_expected);
1352 private:
1353 const base::Time start_time_;
1355 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceBookmarkTestWithData);
1358 namespace {
1360 // Constants for bookmark model that looks like:
1361 // |-- Bookmark bar
1362 // | |-- u2, http://www.u2.com/
1363 // | |-- f1
1364 // | | |-- f1u4, http://www.f1u4.com/
1365 // | | |-- f1u2, http://www.f1u2.com/
1366 // | | |-- f1u3, http://www.f1u3.com/
1367 // | | +-- f1u1, http://www.f1u1.com/
1368 // | |-- u1, http://www.u1.com/
1369 // | +-- f2
1370 // | |-- f2u2, http://www.f2u2.com/
1371 // | |-- f2u4, http://www.f2u4.com/
1372 // | |-- f2u3, http://www.f2u3.com/
1373 // | +-- f2u1, http://www.f2u1.com/
1374 // +-- Other bookmarks
1375 // | |-- f3
1376 // | | |-- f3u4, http://www.f3u4.com/
1377 // | | |-- f3u2, http://www.f3u2.com/
1378 // | | |-- f3u3, http://www.f3u3.com/
1379 // | | +-- f3u1, http://www.f3u1.com/
1380 // | |-- u4, http://www.u4.com/
1381 // | |-- u3, http://www.u3.com/
1382 // | --- f4
1383 // | | |-- f4u1, http://www.f4u1.com/
1384 // | | |-- f4u2, http://www.f4u2.com/
1385 // | | |-- f4u3, http://www.f4u3.com/
1386 // | | +-- f4u4, http://www.f4u4.com/
1387 // | |-- dup
1388 // | | +-- dupu1, http://www.dupu1.com/
1389 // | +-- dup
1390 // | | +-- dupu2, http://www.dupu1.com/
1391 // | +-- ls , http://www.ls.com/
1392 // |
1393 // +-- Mobile bookmarks
1394 // |-- f5
1395 // | |-- f5u1, http://www.f5u1.com/
1396 // |-- f6
1397 // | |-- f6u1, http://www.f6u1.com/
1398 // | |-- f6u2, http://www.f6u2.com/
1399 // +-- u5, http://www.u5.com/
1401 static TestData kBookmarkBarChildren[] = {
1402 { "u2", "http://www.u2.com/" },
1403 { "f1", NULL },
1404 { "u1", "http://www.u1.com/" },
1405 { "f2", NULL },
1407 static TestData kF1Children[] = {
1408 { "f1u4", "http://www.f1u4.com/" },
1409 { "f1u2", "http://www.f1u2.com/" },
1410 { "f1u3", "http://www.f1u3.com/" },
1411 { "f1u1", "http://www.f1u1.com/" },
1413 static TestData kF2Children[] = {
1414 { "f2u2", "http://www.f2u2.com/" },
1415 { "f2u4", "http://www.f2u4.com/" },
1416 { "f2u3", "http://www.f2u3.com/" },
1417 { "f2u1", "http://www.f2u1.com/" },
1420 static TestData kOtherBookmarkChildren[] = {
1421 { "f3", NULL },
1422 { "u4", "http://www.u4.com/" },
1423 { "u3", "http://www.u3.com/" },
1424 { "f4", NULL },
1425 { "dup", NULL },
1426 { "dup", NULL },
1427 { " ls ", "http://www.ls.com/" }
1429 static TestData kF3Children[] = {
1430 { "f3u4", "http://www.f3u4.com/" },
1431 { "f3u2", "http://www.f3u2.com/" },
1432 { "f3u3", "http://www.f3u3.com/" },
1433 { "f3u1", "http://www.f3u1.com/" },
1435 static TestData kF4Children[] = {
1436 { "f4u1", "http://www.f4u1.com/" },
1437 { "f4u2", "http://www.f4u2.com/" },
1438 { "f4u3", "http://www.f4u3.com/" },
1439 { "f4u4", "http://www.f4u4.com/" },
1441 static TestData kDup1Children[] = {
1442 { "dupu1", "http://www.dupu1.com/" },
1444 static TestData kDup2Children[] = {
1445 { "dupu2", "http://www.dupu2.com/" },
1448 static TestData kMobileBookmarkChildren[] = {
1449 { "f5", NULL },
1450 { "f6", NULL },
1451 { "u5", "http://www.u5.com/" },
1453 static TestData kF5Children[] = {
1454 { "f5u1", "http://www.f5u1.com/" },
1455 { "f5u2", "http://www.f5u2.com/" },
1457 static TestData kF6Children[] = {
1458 { "f6u1", "http://www.f6u1.com/" },
1459 { "f6u2", "http://www.f6u2.com/" },
1462 } // anonymous namespace.
1464 ProfileSyncServiceBookmarkTestWithData::
1465 ProfileSyncServiceBookmarkTestWithData()
1466 : start_time_(base::Time::Now()) {
1469 void ProfileSyncServiceBookmarkTestWithData::PopulateFromTestData(
1470 const BookmarkNode* node,
1471 const TestData* data,
1472 int size,
1473 int* running_count) {
1474 DCHECK(node);
1475 DCHECK(data);
1476 DCHECK(node->is_folder());
1477 for (int i = 0; i < size; ++i) {
1478 const TestData& item = data[i];
1479 if (item.url) {
1480 const base::Time add_time =
1481 start_time_ + base::TimeDelta::FromMinutes(*running_count);
1482 model_->AddURLWithCreationTimeAndMetaInfo(node,
1484 base::UTF8ToUTF16(item.title),
1485 GURL(item.url),
1486 add_time,
1487 NULL);
1488 } else {
1489 model_->AddFolder(node, i, base::UTF8ToUTF16(item.title));
1491 (*running_count)++;
1495 void ProfileSyncServiceBookmarkTestWithData::CompareWithTestData(
1496 const BookmarkNode* node,
1497 const TestData* data,
1498 int size,
1499 int* running_count) {
1500 DCHECK(node);
1501 DCHECK(data);
1502 DCHECK(node->is_folder());
1503 ASSERT_EQ(size, node->child_count());
1504 for (int i = 0; i < size; ++i) {
1505 const BookmarkNode* child_node = node->GetChild(i);
1506 const TestData& item = data[i];
1507 GURL url = GURL(item.url == NULL ? "" : item.url);
1508 BookmarkNode test_node(url);
1509 test_node.SetTitle(base::UTF8ToUTF16(item.title));
1510 EXPECT_EQ(child_node->GetTitle(), test_node.GetTitle());
1511 if (item.url) {
1512 EXPECT_FALSE(child_node->is_folder());
1513 EXPECT_TRUE(child_node->is_url());
1514 EXPECT_EQ(child_node->url(), test_node.url());
1515 const base::Time expected_time =
1516 start_time_ + base::TimeDelta::FromMinutes(*running_count);
1517 EXPECT_EQ(expected_time.ToInternalValue(),
1518 child_node->date_added().ToInternalValue());
1519 } else {
1520 EXPECT_TRUE(child_node->is_folder());
1521 EXPECT_FALSE(child_node->is_url());
1523 (*running_count)++;
1527 // TODO(munjal): We should implement some way of generating random data and can
1528 // use the same seed to generate the same sequence.
1529 void ProfileSyncServiceBookmarkTestWithData::WriteTestDataToBookmarkModel() {
1530 const BookmarkNode* bookmarks_bar_node = model_->bookmark_bar_node();
1531 int count = 0;
1532 PopulateFromTestData(bookmarks_bar_node,
1533 kBookmarkBarChildren,
1534 arraysize(kBookmarkBarChildren),
1535 &count);
1537 ASSERT_GE(bookmarks_bar_node->child_count(), 4);
1538 const BookmarkNode* f1_node = bookmarks_bar_node->GetChild(1);
1539 PopulateFromTestData(f1_node, kF1Children, arraysize(kF1Children), &count);
1540 const BookmarkNode* f2_node = bookmarks_bar_node->GetChild(3);
1541 PopulateFromTestData(f2_node, kF2Children, arraysize(kF2Children), &count);
1543 const BookmarkNode* other_bookmarks_node = model_->other_node();
1544 PopulateFromTestData(other_bookmarks_node,
1545 kOtherBookmarkChildren,
1546 arraysize(kOtherBookmarkChildren),
1547 &count);
1549 ASSERT_GE(other_bookmarks_node->child_count(), 6);
1550 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
1551 PopulateFromTestData(f3_node, kF3Children, arraysize(kF3Children), &count);
1552 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
1553 PopulateFromTestData(f4_node, kF4Children, arraysize(kF4Children), &count);
1554 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
1555 PopulateFromTestData(dup_node, kDup1Children, arraysize(kDup1Children),
1556 &count);
1557 dup_node = other_bookmarks_node->GetChild(5);
1558 PopulateFromTestData(dup_node, kDup2Children, arraysize(kDup2Children),
1559 &count);
1561 const BookmarkNode* mobile_bookmarks_node = model_->mobile_node();
1562 PopulateFromTestData(mobile_bookmarks_node,
1563 kMobileBookmarkChildren,
1564 arraysize(kMobileBookmarkChildren),
1565 &count);
1567 ASSERT_GE(mobile_bookmarks_node->child_count(), 3);
1568 const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0);
1569 PopulateFromTestData(f5_node, kF5Children, arraysize(kF5Children), &count);
1570 const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1);
1571 PopulateFromTestData(f6_node, kF6Children, arraysize(kF6Children), &count);
1573 ExpectBookmarkModelMatchesTestData();
1576 void ProfileSyncServiceBookmarkTestWithData::
1577 ExpectBookmarkModelMatchesTestData() {
1578 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1579 int count = 0;
1580 CompareWithTestData(bookmark_bar_node,
1581 kBookmarkBarChildren,
1582 arraysize(kBookmarkBarChildren),
1583 &count);
1585 ASSERT_GE(bookmark_bar_node->child_count(), 4);
1586 const BookmarkNode* f1_node = bookmark_bar_node->GetChild(1);
1587 CompareWithTestData(f1_node, kF1Children, arraysize(kF1Children), &count);
1588 const BookmarkNode* f2_node = bookmark_bar_node->GetChild(3);
1589 CompareWithTestData(f2_node, kF2Children, arraysize(kF2Children), &count);
1591 const BookmarkNode* other_bookmarks_node = model_->other_node();
1592 CompareWithTestData(other_bookmarks_node,
1593 kOtherBookmarkChildren,
1594 arraysize(kOtherBookmarkChildren),
1595 &count);
1597 ASSERT_GE(other_bookmarks_node->child_count(), 6);
1598 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
1599 CompareWithTestData(f3_node, kF3Children, arraysize(kF3Children), &count);
1600 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
1601 CompareWithTestData(f4_node, kF4Children, arraysize(kF4Children), &count);
1602 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
1603 CompareWithTestData(dup_node, kDup1Children, arraysize(kDup1Children),
1604 &count);
1605 dup_node = other_bookmarks_node->GetChild(5);
1606 CompareWithTestData(dup_node, kDup2Children, arraysize(kDup2Children),
1607 &count);
1609 const BookmarkNode* mobile_bookmarks_node = model_->mobile_node();
1610 CompareWithTestData(mobile_bookmarks_node,
1611 kMobileBookmarkChildren,
1612 arraysize(kMobileBookmarkChildren),
1613 &count);
1615 ASSERT_GE(mobile_bookmarks_node->child_count(), 3);
1616 const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0);
1617 CompareWithTestData(f5_node, kF5Children, arraysize(kF5Children), &count);
1618 const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1);
1619 CompareWithTestData(f6_node, kF6Children, arraysize(kF6Children), &count);
1622 // Tests persistence of the profile sync service by unloading the
1623 // database and then reloading it from disk.
1624 TEST_F(ProfileSyncServiceBookmarkTestWithData, Persistence) {
1625 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1626 StartSync();
1628 WriteTestDataToBookmarkModel();
1630 ExpectModelMatch();
1632 // Force both models to discard their data and reload from disk. This
1633 // simulates what would happen if the browser were to shutdown normally,
1634 // and then relaunch.
1635 StopSync();
1636 UnloadBookmarkModel();
1637 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1638 StartSync();
1640 ExpectBookmarkModelMatchesTestData();
1642 // With the BookmarkModel contents verified, ExpectModelMatch will
1643 // verify the contents of the sync model.
1644 ExpectModelMatch();
1647 // Tests the merge case when the BookmarkModel is non-empty but the
1648 // sync model is empty. This corresponds to uploading browser
1649 // bookmarks to an initially empty, new account.
1650 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptySyncModel) {
1651 // Don't start the sync service until we've populated the bookmark model.
1652 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1654 WriteTestDataToBookmarkModel();
1656 // Restart sync. This should trigger a merge step during
1657 // initialization -- we expect the browser bookmarks to be written
1658 // to the sync service during this call.
1659 StartSync();
1661 // Verify that the bookmark model hasn't changed, and that the sync model
1662 // matches it exactly.
1663 ExpectBookmarkModelMatchesTestData();
1664 ExpectModelMatch();
1667 // Tests the merge case when the BookmarkModel is empty but the sync model is
1668 // non-empty. This corresponds (somewhat) to a clean install of the browser,
1669 // with no bookmarks, connecting to a sync account that has some bookmarks.
1670 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptyBookmarkModel) {
1671 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1672 StartSync();
1674 WriteTestDataToBookmarkModel();
1676 ExpectModelMatch();
1678 // Force the databse to unload and write itself to disk.
1679 StopSync();
1681 // Blow away the bookmark model -- it should be empty afterwards.
1682 UnloadBookmarkModel();
1683 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1684 EXPECT_EQ(model_->bookmark_bar_node()->child_count(), 0);
1685 EXPECT_EQ(model_->other_node()->child_count(), 0);
1686 EXPECT_EQ(model_->mobile_node()->child_count(), 0);
1688 // Now restart the sync service. Starting it should populate the bookmark
1689 // model -- test for consistency.
1690 StartSync();
1691 ExpectBookmarkModelMatchesTestData();
1692 ExpectModelMatch();
1695 // Tests the merge cases when both the models are expected to be identical
1696 // after the merge.
1697 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeExpectedIdenticalModels) {
1698 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1699 StartSync();
1700 WriteTestDataToBookmarkModel();
1701 ExpectModelMatch();
1702 StopSync();
1703 UnloadBookmarkModel();
1705 // At this point both the bookmark model and the server should have the
1706 // exact same data and it should match the test data.
1707 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1708 StartSync();
1709 ExpectBookmarkModelMatchesTestData();
1710 ExpectModelMatch();
1711 StopSync();
1712 UnloadBookmarkModel();
1714 // Now reorder some bookmarks in the bookmark model and then merge. Make
1715 // sure we get the order of the server after merge.
1716 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1717 ExpectBookmarkModelMatchesTestData();
1718 const BookmarkNode* bookmark_bar = model_->bookmark_bar_node();
1719 ASSERT_TRUE(bookmark_bar);
1720 ASSERT_GT(bookmark_bar->child_count(), 1);
1721 model_->Move(bookmark_bar->GetChild(0), bookmark_bar, 1);
1722 StartSync();
1723 ExpectModelMatch();
1724 ExpectBookmarkModelMatchesTestData();
1727 // Tests the merge cases when both the models are expected to be identical
1728 // after the merge.
1729 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeModelsWithSomeExtras) {
1730 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1731 WriteTestDataToBookmarkModel();
1732 ExpectBookmarkModelMatchesTestData();
1734 // Remove some nodes and reorder some nodes.
1735 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1736 int remove_index = 2;
1737 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1738 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
1739 ASSERT_TRUE(child_node);
1740 ASSERT_TRUE(child_node->is_url());
1741 model_->Remove(bookmark_bar_node, remove_index);
1742 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1743 child_node = bookmark_bar_node->GetChild(remove_index);
1744 ASSERT_TRUE(child_node);
1745 ASSERT_TRUE(child_node->is_folder());
1746 model_->Remove(bookmark_bar_node, remove_index);
1748 const BookmarkNode* other_node = model_->other_node();
1749 ASSERT_GE(other_node->child_count(), 1);
1750 const BookmarkNode* f3_node = other_node->GetChild(0);
1751 ASSERT_TRUE(f3_node);
1752 ASSERT_TRUE(f3_node->is_folder());
1753 remove_index = 2;
1754 ASSERT_GT(f3_node->child_count(), remove_index);
1755 model_->Remove(f3_node, remove_index);
1756 ASSERT_GT(f3_node->child_count(), remove_index);
1757 model_->Remove(f3_node, remove_index);
1759 StartSync();
1760 ExpectModelMatch();
1761 StopSync();
1763 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1764 WriteTestDataToBookmarkModel();
1765 ExpectBookmarkModelMatchesTestData();
1767 // Remove some nodes and reorder some nodes.
1768 bookmark_bar_node = model_->bookmark_bar_node();
1769 remove_index = 0;
1770 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1771 child_node = bookmark_bar_node->GetChild(remove_index);
1772 ASSERT_TRUE(child_node);
1773 ASSERT_TRUE(child_node->is_url());
1774 model_->Remove(bookmark_bar_node, remove_index);
1775 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1776 child_node = bookmark_bar_node->GetChild(remove_index);
1777 ASSERT_TRUE(child_node);
1778 ASSERT_TRUE(child_node->is_folder());
1779 model_->Remove(bookmark_bar_node, remove_index);
1781 ASSERT_GE(bookmark_bar_node->child_count(), 2);
1782 model_->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 1);
1784 other_node = model_->other_node();
1785 ASSERT_GE(other_node->child_count(), 1);
1786 f3_node = other_node->GetChild(0);
1787 ASSERT_TRUE(f3_node);
1788 ASSERT_TRUE(f3_node->is_folder());
1789 remove_index = 0;
1790 ASSERT_GT(f3_node->child_count(), remove_index);
1791 model_->Remove(f3_node, remove_index);
1792 ASSERT_GT(f3_node->child_count(), remove_index);
1793 model_->Remove(f3_node, remove_index);
1795 ASSERT_GE(other_node->child_count(), 4);
1796 model_->Move(other_node->GetChild(0), other_node, 1);
1797 model_->Move(other_node->GetChild(2), other_node, 3);
1799 StartSync();
1800 ExpectModelMatch();
1802 // After the merge, the model should match the test data.
1803 ExpectBookmarkModelMatchesTestData();
1806 // Tests that when persisted model associations are used, things work fine.
1807 TEST_F(ProfileSyncServiceBookmarkTestWithData, ModelAssociationPersistence) {
1808 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1809 WriteTestDataToBookmarkModel();
1810 StartSync();
1811 ExpectModelMatch();
1812 // Force sync to shut down and write itself to disk.
1813 StopSync();
1814 // Now restart sync. This time it should use the persistent
1815 // associations.
1816 StartSync();
1817 ExpectModelMatch();
1820 // Tests that when persisted model associations are used, things work fine.
1821 TEST_F(ProfileSyncServiceBookmarkTestWithData,
1822 ModelAssociationInvalidPersistence) {
1823 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1824 WriteTestDataToBookmarkModel();
1825 StartSync();
1826 ExpectModelMatch();
1827 // Force sync to shut down and write itself to disk.
1828 StopSync();
1829 // Change the bookmark model before restarting sync service to simulate
1830 // the situation where bookmark model is different from sync model and
1831 // make sure model associator correctly rebuilds associations.
1832 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1833 model_->AddURL(bookmark_bar_node, 0, base::ASCIIToUTF16("xtra"),
1834 GURL("http://www.xtra.com"));
1835 // Now restart sync. This time it will try to use the persistent
1836 // associations and realize that they are invalid and hence will rebuild
1837 // associations.
1838 StartSync();
1839 ExpectModelMatch();
1842 TEST_F(ProfileSyncServiceBookmarkTestWithData, SortChildren) {
1843 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1844 StartSync();
1846 // Write test data to bookmark model and verify that the models match.
1847 WriteTestDataToBookmarkModel();
1848 const BookmarkNode* folder_added = model_->other_node()->GetChild(0);
1849 ASSERT_TRUE(folder_added);
1850 ASSERT_TRUE(folder_added->is_folder());
1852 ExpectModelMatch();
1854 // Sort the other-bookmarks children and expect that the models match.
1855 model_->SortChildren(folder_added);
1856 ExpectModelMatch();
1859 // See what happens if we enable sync but then delete the "Sync Data"
1860 // folder.
1861 TEST_F(ProfileSyncServiceBookmarkTestWithData,
1862 RecoverAfterDeletingSyncDataDirectory) {
1863 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1864 StartSync();
1866 WriteTestDataToBookmarkModel();
1868 StopSync();
1870 // Nuke the sync DB and reload.
1871 TearDown();
1872 SetUp();
1874 // First attempt fails due to a persistence error.
1875 EXPECT_TRUE(CreatePermanentBookmarkNodes());
1876 EXPECT_FALSE(AssociateModels());
1878 // Second attempt succeeds due to the previous error resetting the native
1879 // transaction version.
1880 model_associator_.reset();
1881 EXPECT_TRUE(CreatePermanentBookmarkNodes());
1882 EXPECT_TRUE(AssociateModels());
1884 // Make sure we're back in sync. In real life, the user would need
1885 // to reauthenticate before this happens, but in the test, authentication
1886 // is sidestepped.
1887 ExpectBookmarkModelMatchesTestData();
1888 ExpectModelMatch();
1891 // Verify that the bookmark model is updated about whether the
1892 // associator is currently running.
1893 TEST_F(ProfileSyncServiceBookmarkTest, AssociationState) {
1894 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1896 ExtensiveChangesBookmarkModelObserver observer;
1897 model_->AddObserver(&observer);
1899 StartSync();
1901 EXPECT_EQ(1, observer.get_started());
1902 EXPECT_EQ(0, observer.get_completed_count_at_started());
1903 EXPECT_EQ(1, observer.get_completed());
1905 model_->RemoveObserver(&observer);
1908 // Verify that the creation_time_us changes are applied in the local model at
1909 // association time and update time.
1910 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateDateAdded) {
1911 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1912 WriteTestDataToBookmarkModel();
1914 // Start and stop sync in order to create bookmark nodes in the sync db.
1915 StartSync();
1916 StopSync();
1918 // Modify the date_added field of a bookmark so it doesn't match with
1919 // the sync data.
1920 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1921 int remove_index = 2;
1922 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1923 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
1924 ASSERT_TRUE(child_node);
1925 EXPECT_TRUE(child_node->is_url());
1926 model_->SetDateAdded(child_node, base::Time::FromInternalValue(10));
1928 StartSync();
1930 // Everything should be back in sync after model association.
1931 ExpectBookmarkModelMatchesTestData();
1932 ExpectModelMatch();
1934 // Now trigger a change while syncing. We add a new bookmark, sync it, then
1935 // updates it's creation time.
1936 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1937 FakeServerChange adds(&trans);
1938 const std::string kTitle = "Some site";
1939 const std::string kUrl = "http://www.whatwhat.yeah/";
1940 const int kCreationTime = 30;
1941 int64 id = adds.AddURL(kTitle, kUrl,
1942 bookmark_bar_id(), 0);
1943 adds.ApplyPendingChanges(change_processor_.get());
1944 FakeServerChange updates(&trans);
1945 updates.ModifyCreationTime(id, kCreationTime);
1946 updates.ApplyPendingChanges(change_processor_.get());
1948 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(0);
1949 ASSERT_TRUE(node);
1950 EXPECT_TRUE(node->is_url());
1951 EXPECT_EQ(base::UTF8ToUTF16(kTitle), node->GetTitle());
1952 EXPECT_EQ(kUrl, node->url().possibly_invalid_spec());
1953 EXPECT_EQ(node->date_added(), base::Time::FromInternalValue(30));
1956 // Tests that changes to the sync nodes meta info gets reflected in the local
1957 // bookmark model.
1958 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromSync) {
1959 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1960 WriteTestDataToBookmarkModel();
1961 StartSync();
1963 // Create bookmark nodes containing meta info.
1964 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1965 FakeServerChange adds(&trans);
1966 BookmarkNode::MetaInfoMap folder_meta_info;
1967 folder_meta_info["folder"] = "foldervalue";
1968 int64 folder_id = adds.AddFolderWithMetaInfo(
1969 "folder title", &folder_meta_info, bookmark_bar_id(), 0);
1970 BookmarkNode::MetaInfoMap node_meta_info;
1971 node_meta_info["node"] = "nodevalue";
1972 node_meta_info["other"] = "othervalue";
1973 int64 id = adds.AddURLWithMetaInfo("node title", "http://www.foo.com",
1974 &node_meta_info, folder_id, 0);
1975 adds.ApplyPendingChanges(change_processor_.get());
1977 // Verify that the nodes are created with the correct meta info.
1978 ASSERT_LT(0, model_->bookmark_bar_node()->child_count());
1979 const BookmarkNode* folder_node = model_->bookmark_bar_node()->GetChild(0);
1980 ASSERT_TRUE(folder_node->GetMetaInfoMap());
1981 EXPECT_EQ(folder_meta_info, *folder_node->GetMetaInfoMap());
1982 ASSERT_LT(0, folder_node->child_count());
1983 const BookmarkNode* node = folder_node->GetChild(0);
1984 ASSERT_TRUE(node->GetMetaInfoMap());
1985 EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap());
1987 // Update meta info on nodes on server
1988 FakeServerChange updates(&trans);
1989 folder_meta_info.erase("folder");
1990 updates.ModifyMetaInfo(folder_id, folder_meta_info);
1991 node_meta_info["node"] = "changednodevalue";
1992 node_meta_info.erase("other");
1993 node_meta_info["newkey"] = "newkeyvalue";
1994 updates.ModifyMetaInfo(id, node_meta_info);
1995 updates.ApplyPendingChanges(change_processor_.get());
1997 // Confirm that the updated values are reflected in the bookmark nodes.
1998 EXPECT_FALSE(folder_node->GetMetaInfoMap());
1999 ASSERT_TRUE(node->GetMetaInfoMap());
2000 EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap());
2003 // Tests that changes to the local bookmark nodes meta info gets reflected in
2004 // the sync nodes.
2005 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromModel) {
2006 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2007 WriteTestDataToBookmarkModel();
2008 StartSync();
2009 ExpectBookmarkModelMatchesTestData();
2011 const BookmarkNode* folder_node =
2012 model_->AddFolder(model_->bookmark_bar_node(), 0,
2013 base::ASCIIToUTF16("folder title"));
2014 const BookmarkNode* node = model_->AddURL(folder_node, 0,
2015 base::ASCIIToUTF16("node title"),
2016 GURL("http://www.foo.com"));
2017 ExpectModelMatch();
2019 // Add some meta info and verify sync model matches the changes.
2020 model_->SetNodeMetaInfo(folder_node, "folder", "foldervalue");
2021 model_->SetNodeMetaInfo(node, "node", "nodevalue");
2022 model_->SetNodeMetaInfo(node, "other", "othervalue");
2023 ExpectModelMatch();
2025 // Change/delete existing meta info and verify.
2026 model_->DeleteNodeMetaInfo(folder_node, "folder");
2027 model_->SetNodeMetaInfo(node, "node", "changednodevalue");
2028 model_->DeleteNodeMetaInfo(node, "other");
2029 model_->SetNodeMetaInfo(node, "newkey", "newkeyvalue");
2030 ExpectModelMatch();
2033 // Tests that node's specifics doesn't get unnecessarily overwritten (causing
2034 // a subsequent commit) when BookmarkChangeProcessor handles a notification
2035 // (such as BookmarkMetaInfoChanged) without an actual data change.
2036 TEST_F(ProfileSyncServiceBookmarkTestWithData, MetaInfoPreservedOnNonChange) {
2037 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2038 WriteTestDataToBookmarkModel();
2039 StartSync();
2041 std::string orig_specifics;
2042 int64 sync_id;
2043 const BookmarkNode* bookmark;
2045 // Create bookmark folder node containing meta info.
2047 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
2048 FakeServerChange adds(&trans);
2050 int64 folder_id = adds.AddFolder("folder title", bookmark_bar_id(), 0);
2052 BookmarkNode::MetaInfoMap node_meta_info;
2053 node_meta_info["one"] = "1";
2054 node_meta_info["two"] = "2";
2055 node_meta_info["three"] = "3";
2057 sync_id = adds.AddURLWithMetaInfo("node title", "http://www.foo.com/",
2058 &node_meta_info, folder_id, 0);
2060 // Verify that the node propagates to the bookmark model
2061 adds.ApplyPendingChanges(change_processor_.get());
2063 bookmark = model_->bookmark_bar_node()->GetChild(0)->GetChild(0);
2064 EXPECT_EQ(node_meta_info, *bookmark->GetMetaInfoMap());
2066 syncer::ReadNode sync_node(&trans);
2067 EXPECT_EQ(BaseNode::INIT_OK, sync_node.InitByIdLookup(sync_id));
2068 orig_specifics = sync_node.GetBookmarkSpecifics().SerializeAsString();
2071 // Force change processor to update the sync node.
2072 change_processor_->BookmarkMetaInfoChanged(model_, bookmark);
2074 // Read bookmark specifics again and verify that there is no change.
2076 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2077 syncer::ReadNode sync_node(&trans);
2078 EXPECT_EQ(BaseNode::INIT_OK, sync_node.InitByIdLookup(sync_id));
2079 std::string new_specifics =
2080 sync_node.GetBookmarkSpecifics().SerializeAsString();
2081 ASSERT_EQ(orig_specifics, new_specifics);
2085 void ProfileSyncServiceBookmarkTestWithData::GetTransactionVersions(
2086 const BookmarkNode* root,
2087 BookmarkNodeVersionMap* node_versions) {
2088 node_versions->clear();
2089 std::queue<const BookmarkNode*> nodes;
2090 nodes.push(root);
2091 while (!nodes.empty()) {
2092 const BookmarkNode* n = nodes.front();
2093 nodes.pop();
2095 int64 version = n->sync_transaction_version();
2096 EXPECT_NE(BookmarkNode::kInvalidSyncTransactionVersion, version);
2098 (*node_versions)[n->id()] = version;
2099 for (int i = 0; i < n->child_count(); ++i) {
2100 if (!CanSyncNode(n->GetChild(i)))
2101 continue;
2102 nodes.push(n->GetChild(i));
2107 void ProfileSyncServiceBookmarkTestWithData::ExpectTransactionVersionMatch(
2108 const BookmarkNode* node,
2109 const BookmarkNodeVersionMap& version_expected) {
2110 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2112 BookmarkNodeVersionMap bnodes_versions;
2113 GetTransactionVersions(node, &bnodes_versions);
2114 for (BookmarkNodeVersionMap::const_iterator it = bnodes_versions.begin();
2115 it != bnodes_versions.end(); ++it) {
2116 syncer::ReadNode sync_node(&trans);
2117 ASSERT_TRUE(model_associator_->InitSyncNodeFromChromeId(it->first,
2118 &sync_node));
2119 EXPECT_EQ(sync_node.GetEntry()->GetTransactionVersion(), it->second);
2120 BookmarkNodeVersionMap::const_iterator expected_ver_it =
2121 version_expected.find(it->first);
2122 if (expected_ver_it != version_expected.end())
2123 EXPECT_EQ(expected_ver_it->second, it->second);
2127 // Test transaction versions of model and nodes are incremented after changes
2128 // are applied.
2129 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateTransactionVersion) {
2130 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2131 StartSync();
2132 WriteTestDataToBookmarkModel();
2133 base::MessageLoop::current()->RunUntilIdle();
2135 BookmarkNodeVersionMap initial_versions;
2137 // Verify transaction versions in sync model and bookmark model (saved as
2138 // transaction version of root node) are equal after
2139 // WriteTestDataToBookmarkModel() created bookmarks.
2141 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2142 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0);
2143 GetTransactionVersions(model_->root_node(), &initial_versions);
2144 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS),
2145 initial_versions[model_->root_node()->id()]);
2147 ExpectTransactionVersionMatch(model_->bookmark_bar_node(),
2148 BookmarkNodeVersionMap());
2149 ExpectTransactionVersionMatch(model_->other_node(),
2150 BookmarkNodeVersionMap());
2151 ExpectTransactionVersionMatch(model_->mobile_node(),
2152 BookmarkNodeVersionMap());
2154 // Verify model version is incremented and bookmark node versions remain
2155 // the same.
2156 const BookmarkNode* bookmark_bar = model_->bookmark_bar_node();
2157 model_->Remove(bookmark_bar, 0);
2158 base::MessageLoop::current()->RunUntilIdle();
2159 BookmarkNodeVersionMap new_versions;
2160 GetTransactionVersions(model_->root_node(), &new_versions);
2161 EXPECT_EQ(initial_versions[model_->root_node()->id()] + 1,
2162 new_versions[model_->root_node()->id()]);
2163 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions);
2164 ExpectTransactionVersionMatch(model_->other_node(), initial_versions);
2165 ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions);
2167 // Verify model version and version of changed bookmark are incremented and
2168 // versions of others remain same.
2169 const BookmarkNode* changed_bookmark =
2170 model_->bookmark_bar_node()->GetChild(0);
2171 model_->SetTitle(changed_bookmark, base::ASCIIToUTF16("test"));
2172 base::MessageLoop::current()->RunUntilIdle();
2173 GetTransactionVersions(model_->root_node(), &new_versions);
2174 EXPECT_EQ(initial_versions[model_->root_node()->id()] + 2,
2175 new_versions[model_->root_node()->id()]);
2176 EXPECT_LT(initial_versions[changed_bookmark->id()],
2177 new_versions[changed_bookmark->id()]);
2178 initial_versions.erase(changed_bookmark->id());
2179 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions);
2180 ExpectTransactionVersionMatch(model_->other_node(), initial_versions);
2181 ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions);
2184 // Test that sync persistence errors are detected and trigger a failed
2185 // association.
2186 TEST_F(ProfileSyncServiceBookmarkTestWithData, PersistenceError) {
2187 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2188 StartSync();
2189 WriteTestDataToBookmarkModel();
2190 base::MessageLoop::current()->RunUntilIdle();
2192 BookmarkNodeVersionMap initial_versions;
2194 // Verify transaction versions in sync model and bookmark model (saved as
2195 // transaction version of root node) are equal after
2196 // WriteTestDataToBookmarkModel() created bookmarks.
2198 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2199 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0);
2200 GetTransactionVersions(model_->root_node(), &initial_versions);
2201 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS),
2202 initial_versions[model_->root_node()->id()]);
2204 ExpectTransactionVersionMatch(model_->bookmark_bar_node(),
2205 BookmarkNodeVersionMap());
2206 ExpectTransactionVersionMatch(model_->other_node(),
2207 BookmarkNodeVersionMap());
2208 ExpectTransactionVersionMatch(model_->mobile_node(),
2209 BookmarkNodeVersionMap());
2211 // Now shut down sync and artificially increment the native model's version.
2212 StopSync();
2213 int64 root_version = initial_versions[model_->root_node()->id()];
2214 model_->SetNodeSyncTransactionVersion(model_->root_node(), root_version + 1);
2216 // Upon association, bookmarks should fail to associate.
2217 EXPECT_FALSE(AssociateModels());
2220 // It's possible for update/add calls from the bookmark model to be out of
2221 // order, or asynchronous. Handle that without triggering an error.
2222 TEST_F(ProfileSyncServiceBookmarkTest, UpdateThenAdd) {
2223 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2224 StartSync();
2226 EXPECT_TRUE(other_bookmarks_id());
2227 EXPECT_TRUE(bookmark_bar_id());
2228 EXPECT_TRUE(mobile_bookmarks_id());
2230 ExpectModelMatch();
2232 // Now destroy the change processor then add a bookmark, to simulate
2233 // missing the Update call.
2234 change_processor_.reset();
2235 const BookmarkNode* node = model_->AddURL(model_->bookmark_bar_node(),
2237 base::ASCIIToUTF16("title"),
2238 GURL("http://www.url.com"));
2240 // Recreate the change processor then update that bookmark. Sync should
2241 // receive the update call and gracefully treat that as if it were an add.
2242 change_processor_.reset(new BookmarkChangeProcessor(
2243 &profile_, model_associator_.get(), &mock_error_handler_));
2244 change_processor_->Start(test_user_share_.user_share());
2245 model_->SetTitle(node, base::ASCIIToUTF16("title2"));
2246 ExpectModelMatch();
2248 // Then simulate the add call arriving late.
2249 change_processor_->BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 0);
2250 ExpectModelMatch();
2253 } // namespace
2255 } // namespace browser_sync