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.
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"
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
;
56 using testing::InvokeWithoutArgs
;
58 using testing::StrictMock
;
60 #if defined(OS_ANDROID) || defined(OS_IOS)
61 static const bool kExpectMobileBookmarks
= true;
63 static const bool kExpectMobileBookmarks
= false;
64 #endif // defined(OS_ANDROID) || defined(OS_IOS)
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
{
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
,
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
));
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());
106 specifics
.set_url(url
);
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
);
118 int64
Add(const std::string
& title
,
119 const std::string
& url
,
122 int64 predecessor_id
) {
123 return AddWithMetaInfo(title
, url
, NULL
, is_folder
, parent_id
,
127 // Add a bookmark folder.
128 int64
AddFolder(const std::string
& title
,
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
,
136 int64 predecessor_id
) {
137 return AddWithMetaInfo(title
, std::string(), meta_info_map
, true, parent_id
,
142 int64
AddURL(const std::string
& title
,
143 const std::string
& url
,
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
,
152 int64 predecessor_id
) {
153 return AddWithMetaInfo(title
, url
, meta_info_map
, false, parent_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);
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
;
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
);
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
));
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
));
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
);
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
);
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() {
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
)
254 syncer::ChangeRecord record
;
255 record
.action
= syncer::ChangeRecord::ACTION_UPDATE
;
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()) {
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
{
287 ExtensiveChangesBookmarkModelObserver()
289 completed_count_at_started_(0),
290 completed_count_(0) {}
292 void ExtensiveBookmarkChangesBeginning(BookmarkModel
* model
) override
{
294 completed_count_at_started_
= completed_count_
;
297 void ExtensiveBookmarkChangesEnded(BookmarkModel
* model
) override
{
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_
;
317 int completed_count_at_started_
;
318 int completed_count_
;
320 DISALLOW_COPY_AND_ASSIGN(ExtensiveChangesBookmarkModelObserver
);
324 class ProfileSyncServiceBookmarkTest
: public testing::Test
{
326 enum LoadOption
{ LOAD_FROM_STORAGE
, DELETE_EXISTING_STORAGE
};
327 enum SaveOption
{ SAVE_TO_STORAGE
, DONT_SAVE_TO_STORAGE
};
329 ProfileSyncServiceBookmarkTest()
331 thread_bundle_(content::TestBrowserThreadBundle::DEFAULT
),
332 local_merge_result_(syncer::BOOKMARKS
),
333 syncer_merge_result_(syncer::BOOKMARKS
) {}
335 virtual ~ProfileSyncServiceBookmarkTest() {
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
);
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
,
386 const std::string
& title
,
387 const std::string
& url
) {
388 EXPECT_FALSE(model_associator_
);
390 syncer::ReadNode
parent(trans
);
391 EXPECT_EQ(BaseNode::INIT_OK
, parent
.InitByIdLookup(parent_id
));
393 sync_pb::BookmarkSpecifics specifics
;
394 specifics
.set_url(url
);
395 specifics
.set_title(title
);
397 syncer::WriteNode
node(trans
);
398 EXPECT_TRUE(node
.InitBookmarkByCreation(parent
, NULL
));
399 node
.SetIsFolder(false);
400 node
.SetTitle(title
);
401 node
.SetBookmarkSpecifics(specifics
);
406 // Load (or re-load) the bookmark model. |load| controls use of the
407 // bookmarks file on disk. |save| controls whether the newly loaded
408 // bookmark model will write out a bookmark file as it goes.
409 void LoadBookmarkModel(LoadOption load
, SaveOption save
) {
410 bool delete_bookmarks
= load
== DELETE_EXISTING_STORAGE
;
411 profile_
.CreateBookmarkModel(delete_bookmarks
);
412 model_
= BookmarkModelFactory::GetForProfile(&profile_
);
413 bookmarks::test::WaitForBookmarkModelToLoad(model_
);
414 // This noticeably speeds up the unit tests that request it.
415 if (save
== DONT_SAVE_TO_STORAGE
)
416 model_
->ClearStore();
417 base::MessageLoop::current()->RunUntilIdle();
420 int GetSyncBookmarkCount() {
421 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
422 syncer::ReadNode
node(&trans
);
423 if (node
.InitTypeRoot(syncer::BOOKMARKS
) != syncer::BaseNode::INIT_OK
)
425 return node
.GetTotalNodeCount();
428 // Creates the bookmark root node and the permanent nodes if they don't
430 bool CreatePermanentBookmarkNodes() {
431 bool root_exists
= false;
432 syncer::ModelType type
= syncer::BOOKMARKS
;
434 syncer::WriteTransaction
trans(FROM_HERE
,
435 test_user_share_
.user_share());
436 syncer::ReadNode
uber_root(&trans
);
437 uber_root
.InitByRootLookup();
439 syncer::ReadNode
root(&trans
);
440 root_exists
= (root
.InitTypeRoot(type
) == BaseNode::INIT_OK
);
444 if (!syncer::TestUserShare::CreateRoot(type
,
445 test_user_share_
.user_share()))
449 const int kNumPermanentNodes
= 3;
450 const std::string permanent_tags
[kNumPermanentNodes
] = {
460 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
461 syncer::ReadNode
root(&trans
);
462 EXPECT_EQ(BaseNode::INIT_OK
, root
.InitTypeRoot(type
));
464 // Loop through creating permanent nodes as necessary.
465 int64 last_child_id
= syncer::kInvalidId
;
466 for (int i
= 0; i
< kNumPermanentNodes
; ++i
) {
467 // First check if the node already exists. This is for tests that involve
468 // persistence and set up sync more than once.
469 syncer::ReadNode
lookup(&trans
);
470 if (lookup
.InitByTagLookupForBookmarks(permanent_tags
[i
]) ==
471 syncer::ReadNode::INIT_OK
) {
472 last_child_id
= lookup
.GetId();
476 // If it doesn't exist, create the permanent node at the end of the
478 syncer::ReadNode
predecessor_node(&trans
);
479 syncer::ReadNode
* predecessor
= NULL
;
480 if (last_child_id
!= syncer::kInvalidId
) {
481 EXPECT_EQ(BaseNode::INIT_OK
,
482 predecessor_node
.InitByIdLookup(last_child_id
));
483 predecessor
= &predecessor_node
;
485 syncer::WriteNode
node(&trans
);
486 if (!node
.InitBookmarkByCreation(root
, predecessor
))
488 node
.SetIsFolder(true);
489 node
.GetMutableEntryForTest()->PutUniqueServerTag(permanent_tags
[i
]);
490 node
.SetTitle(permanent_tags
[i
]);
491 node
.SetExternalId(0);
492 last_child_id
= node
.GetId();
497 bool AssociateModels() {
498 DCHECK(!model_associator_
);
500 // Set up model associator.
501 model_associator_
.reset(new BookmarkModelAssociator(
502 BookmarkModelFactory::GetForProfile(&profile_
),
504 test_user_share_
.user_share(),
505 &mock_error_handler_
,
506 kExpectMobileBookmarks
));
508 local_merge_result_
= syncer::SyncMergeResult(syncer::BOOKMARKS
);
509 syncer_merge_result_
= syncer::SyncMergeResult(syncer::BOOKMARKS
);
510 int local_count_before
= model_
->root_node()->GetTotalNodeCount();
511 int syncer_count_before
= GetSyncBookmarkCount();
513 syncer::SyncError error
= model_associator_
->AssociateModels(
514 &local_merge_result_
,
515 &syncer_merge_result_
);
519 base::MessageLoop::current()->RunUntilIdle();
521 // Verify the merge results were calculated properly.
522 EXPECT_EQ(local_count_before
,
523 local_merge_result_
.num_items_before_association());
524 EXPECT_EQ(syncer_count_before
,
525 syncer_merge_result_
.num_items_before_association());
526 EXPECT_EQ(local_merge_result_
.num_items_after_association(),
527 local_merge_result_
.num_items_before_association() +
528 local_merge_result_
.num_items_added() -
529 local_merge_result_
.num_items_deleted());
530 EXPECT_EQ(syncer_merge_result_
.num_items_after_association(),
531 syncer_merge_result_
.num_items_before_association() +
532 syncer_merge_result_
.num_items_added() -
533 syncer_merge_result_
.num_items_deleted());
534 EXPECT_EQ(model_
->root_node()->GetTotalNodeCount(),
535 local_merge_result_
.num_items_after_association());
536 EXPECT_EQ(GetSyncBookmarkCount(),
537 syncer_merge_result_
.num_items_after_association());
542 test_user_share_
.Reload();
544 ASSERT_TRUE(CreatePermanentBookmarkNodes());
545 ASSERT_TRUE(AssociateModels());
547 // Set up change processor.
548 change_processor_
.reset(
549 new BookmarkChangeProcessor(&profile_
,
550 model_associator_
.get(),
551 &mock_error_handler_
));
552 change_processor_
->Start(test_user_share_
.user_share());
556 change_processor_
.reset();
557 if (model_associator_
) {
558 syncer::SyncError error
= model_associator_
->DisassociateModels();
559 EXPECT_FALSE(error
.IsSet());
561 model_associator_
.reset();
563 base::MessageLoop::current()->RunUntilIdle();
565 // TODO(akalin): Actually close the database and flush it to disk
566 // (and make StartSync reload from disk). This would require
567 // refactoring TestUserShare.
570 void UnloadBookmarkModel() {
571 profile_
.CreateBookmarkModel(false /* delete_bookmarks */);
573 base::MessageLoop::current()->RunUntilIdle();
576 bool InitSyncNodeFromChromeNode(const BookmarkNode
* bnode
,
577 syncer::BaseNode
* sync_node
) {
578 return model_associator_
->InitSyncNodeFromChromeId(bnode
->id(),
582 void ExpectSyncerNodeMatching(syncer::BaseTransaction
* trans
,
583 const BookmarkNode
* bnode
) {
584 std::string truncated_title
= base::UTF16ToUTF8(bnode
->GetTitle());
585 syncer::SyncAPINameToServerName(truncated_title
, &truncated_title
);
586 base::TruncateUTF8ToByteSize(truncated_title
, 255, &truncated_title
);
587 syncer::ServerNameToSyncAPIName(truncated_title
, &truncated_title
);
589 syncer::ReadNode
gnode(trans
);
590 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnode
, &gnode
));
591 // Non-root node titles and parents must match.
592 if (!model_
->is_permanent_node(bnode
)) {
593 EXPECT_EQ(truncated_title
, gnode
.GetTitle());
595 model_associator_
->GetChromeNodeFromSyncId(gnode
.GetParentId()),
598 EXPECT_EQ(bnode
->is_folder(), gnode
.GetIsFolder());
600 EXPECT_EQ(bnode
->url(), GURL(gnode
.GetBookmarkSpecifics().url()));
602 // Check that meta info matches.
603 const BookmarkNode::MetaInfoMap
* meta_info_map
= bnode
->GetMetaInfoMap();
604 sync_pb::BookmarkSpecifics specifics
= gnode
.GetBookmarkSpecifics();
605 if (!meta_info_map
) {
606 EXPECT_EQ(0, specifics
.meta_info_size());
608 EXPECT_EQ(meta_info_map
->size(),
609 static_cast<size_t>(specifics
.meta_info_size()));
610 for (int i
= 0; i
< specifics
.meta_info_size(); i
++) {
611 BookmarkNode::MetaInfoMap::const_iterator it
=
612 meta_info_map
->find(specifics
.meta_info(i
).key());
613 EXPECT_TRUE(it
!= meta_info_map
->end());
614 EXPECT_EQ(it
->second
, specifics
.meta_info(i
).value());
618 // Check for position matches.
619 int browser_index
= bnode
->parent()->GetIndexOf(bnode
);
620 if (browser_index
== 0) {
621 EXPECT_EQ(gnode
.GetPredecessorId(), 0);
623 const BookmarkNode
* bprev
=
624 bnode
->parent()->GetChild(browser_index
- 1);
625 syncer::ReadNode
gprev(trans
);
626 ASSERT_TRUE(InitSyncNodeFromChromeNode(bprev
, &gprev
));
627 EXPECT_EQ(gnode
.GetPredecessorId(), gprev
.GetId());
628 EXPECT_EQ(gnode
.GetParentId(), gprev
.GetParentId());
630 // Note: the managed node is the last child of the root_node but isn't
631 // synced; if CanSyncNode() is false then there is no next node to sync.
632 const BookmarkNode
* bnext
= NULL
;
633 if (browser_index
+ 1 < bnode
->parent()->child_count())
634 bnext
= bnode
->parent()->GetChild(browser_index
+ 1);
635 if (!bnext
|| !CanSyncNode(bnext
)) {
636 EXPECT_EQ(gnode
.GetSuccessorId(), 0);
638 syncer::ReadNode
gnext(trans
);
639 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnext
, &gnext
));
640 EXPECT_EQ(gnode
.GetSuccessorId(), gnext
.GetId());
641 EXPECT_EQ(gnode
.GetParentId(), gnext
.GetParentId());
644 EXPECT_TRUE(gnode
.GetFirstChildId());
647 void ExpectSyncerNodeMatching(const BookmarkNode
* bnode
) {
648 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
649 ExpectSyncerNodeMatching(&trans
, bnode
);
652 void ExpectBrowserNodeMatching(syncer::BaseTransaction
* trans
,
654 EXPECT_TRUE(sync_id
);
655 const BookmarkNode
* bnode
=
656 model_associator_
->GetChromeNodeFromSyncId(sync_id
);
658 ASSERT_TRUE(CanSyncNode(bnode
));
660 int64 id
= model_associator_
->GetSyncIdFromChromeId(bnode
->id());
661 EXPECT_EQ(id
, sync_id
);
662 ExpectSyncerNodeMatching(trans
, bnode
);
665 void ExpectBrowserNodeUnknown(int64 sync_id
) {
666 EXPECT_FALSE(model_associator_
->GetChromeNodeFromSyncId(sync_id
));
669 void ExpectBrowserNodeKnown(int64 sync_id
) {
670 EXPECT_TRUE(model_associator_
->GetChromeNodeFromSyncId(sync_id
));
673 void ExpectSyncerNodeKnown(const BookmarkNode
* node
) {
674 int64 sync_id
= model_associator_
->GetSyncIdFromChromeId(node
->id());
675 EXPECT_NE(sync_id
, syncer::kInvalidId
);
678 void ExpectSyncerNodeUnknown(const BookmarkNode
* node
) {
679 int64 sync_id
= model_associator_
->GetSyncIdFromChromeId(node
->id());
680 EXPECT_EQ(sync_id
, syncer::kInvalidId
);
683 void ExpectBrowserNodeTitle(int64 sync_id
, const std::string
& title
) {
684 const BookmarkNode
* bnode
=
685 model_associator_
->GetChromeNodeFromSyncId(sync_id
);
687 EXPECT_EQ(bnode
->GetTitle(), base::UTF8ToUTF16(title
));
690 void ExpectBrowserNodeURL(int64 sync_id
, const std::string
& url
) {
691 const BookmarkNode
* bnode
=
692 model_associator_
->GetChromeNodeFromSyncId(sync_id
);
694 EXPECT_EQ(GURL(url
), bnode
->url());
697 void ExpectBrowserNodeParent(int64 sync_id
, int64 parent_sync_id
) {
698 const BookmarkNode
* node
=
699 model_associator_
->GetChromeNodeFromSyncId(sync_id
);
701 const BookmarkNode
* parent
=
702 model_associator_
->GetChromeNodeFromSyncId(parent_sync_id
);
704 EXPECT_EQ(node
->parent(), parent
);
707 void ExpectModelMatch(syncer::BaseTransaction
* trans
) {
708 const BookmarkNode
* root
= model_
->root_node();
710 EXPECT_EQ(root
->GetIndexOf(model_
->mobile_node()), 0);
711 EXPECT_EQ(root
->GetIndexOf(model_
->bookmark_bar_node()), 1);
712 EXPECT_EQ(root
->GetIndexOf(model_
->other_node()), 2);
714 EXPECT_EQ(root
->GetIndexOf(model_
->bookmark_bar_node()), 0);
715 EXPECT_EQ(root
->GetIndexOf(model_
->other_node()), 1);
716 EXPECT_EQ(root
->GetIndexOf(model_
->mobile_node()), 2);
719 std::stack
<int64
> stack
;
720 stack
.push(bookmark_bar_id());
721 while (!stack
.empty()) {
722 int64 id
= stack
.top();
726 ExpectBrowserNodeMatching(trans
, id
);
728 syncer::ReadNode
gnode(trans
);
729 ASSERT_EQ(BaseNode::INIT_OK
, gnode
.InitByIdLookup(id
));
730 stack
.push(gnode
.GetSuccessorId());
731 if (gnode
.GetIsFolder())
732 stack
.push(gnode
.GetFirstChildId());
736 void ExpectModelMatch() {
737 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
738 ExpectModelMatch(&trans
);
741 int64
mobile_bookmarks_id() {
743 model_associator_
->GetSyncIdFromChromeId(model_
->mobile_node()->id());
746 int64
other_bookmarks_id() {
748 model_associator_
->GetSyncIdFromChromeId(model_
->other_node()->id());
751 int64
bookmark_bar_id() {
752 return model_associator_
->GetSyncIdFromChromeId(
753 model_
->bookmark_bar_node()->id());
757 TestingProfile profile_
;
758 BookmarkModel
* model_
;
759 syncer::TestUserShare test_user_share_
;
760 scoped_ptr
<BookmarkChangeProcessor
> change_processor_
;
761 StrictMock
<sync_driver::DataTypeErrorHandlerMock
> mock_error_handler_
;
762 scoped_ptr
<BookmarkModelAssociator
> model_associator_
;
765 content::TestBrowserThreadBundle thread_bundle_
;
766 syncer::SyncMergeResult local_merge_result_
;
767 syncer::SyncMergeResult syncer_merge_result_
;
770 TEST_F(ProfileSyncServiceBookmarkTest
, InitialState
) {
771 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
774 EXPECT_TRUE(other_bookmarks_id());
775 EXPECT_TRUE(bookmark_bar_id());
776 EXPECT_TRUE(mobile_bookmarks_id());
781 // Populate the sync database then start model association. Sync's bookmarks
782 // should end up being copied into the native model, resulting in a successful
783 // "ExpectModelMatch()".
785 // This code has some use for verifying correctness. It's also a very useful
786 // for profiling bookmark ModelAssociation, an important part of some first-time
787 // sync scenarios. Simply increase the kNumFolders and kNumBookmarksPerFolder
788 // as desired, then run the test under a profiler to find hot spots in the model
790 TEST_F(ProfileSyncServiceBookmarkTest
, InitialModelAssociate
) {
791 const int kNumBookmarksPerFolder
= 10;
792 const int kNumFolders
= 10;
794 CreatePermanentBookmarkNodes();
797 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
798 for (int i
= 0; i
< kNumFolders
; ++i
) {
800 AddFolderToShare(&trans
, base::StringPrintf("folder%05d", i
));
801 for (int j
= 0; j
< kNumBookmarksPerFolder
; ++j
) {
803 &trans
, folder_id
, base::StringPrintf("bookmark%05d", j
),
804 base::StringPrintf("http://www.google.com/search?q=%05d", j
));
809 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
815 // Tests bookmark association when nodes exists in the native model only.
816 // These entries should be copied to Sync directory during association process.
817 TEST_F(ProfileSyncServiceBookmarkTest
,
818 InitialModelAssociateWithBookmarkModelNodes
) {
819 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
820 const BookmarkNode
* folder
=
821 model_
->AddFolder(model_
->other_node(), 0, base::ASCIIToUTF16("foobar"));
822 model_
->AddFolder(folder
, 0, base::ASCIIToUTF16("nested"));
823 model_
->AddURL(folder
, 0, base::ASCIIToUTF16("Internets #1 Pies Site"),
824 GURL("http://www.easypie.com/"));
825 model_
->AddURL(folder
, 1, base::ASCIIToUTF16("Airplanes"),
826 GURL("http://www.easyjet.com/"));
832 // Tests bookmark association case when there is an entry in the delete journal
833 // that matches one of native bookmarks.
834 TEST_F(ProfileSyncServiceBookmarkTest
, InitialModelAssociateWithDeleteJournal
) {
835 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
836 const BookmarkNode
* folder
= model_
->AddFolder(model_
->bookmark_bar_node(), 0,
837 base::ASCIIToUTF16("foobar"));
838 const BookmarkNode
* bookmark
=
839 model_
->AddURL(folder
, 0, base::ASCIIToUTF16("Airplanes"),
840 GURL("http://www.easyjet.com/"));
842 CreatePermanentBookmarkNodes();
844 // Create entries matching the folder and the bookmark above.
845 int64 folder_id
, bookmark_id
;
847 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
848 folder_id
= AddFolderToShare(&trans
, "foobar");
849 bookmark_id
= AddBookmarkToShare(&trans
, folder_id
, "Airplanes",
850 "http://www.easyjet.com/");
853 // Associate the bookmark sync node with the native model one and make
856 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
857 syncer::WriteNode
node(&trans
);
858 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(bookmark_id
));
860 node
.GetMutableEntryForTest()->PutLocalExternalId(bookmark
->id());
861 node
.GetMutableEntryForTest()->PutServerIsDel(true);
862 node
.GetMutableEntryForTest()->PutIsDel(true);
865 ASSERT_TRUE(AssociateModels());
868 // The bookmark node should be deleted.
869 EXPECT_EQ(0, folder
->child_count());
872 // Tests that the external ID is used to match the right folder amoung
873 // multiple folders with the same name during the native model traversal.
874 // Also tests that the external ID is used to match the right bookmark
875 // among multiple identical bookmarks when dealing with the delete journal.
876 TEST_F(ProfileSyncServiceBookmarkTest
,
877 InitialModelAssociateVerifyExternalIdMatch
) {
878 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
879 const int kNumFolders
= 10;
880 const int kNumBookmarks
= 10;
881 const int kFolderToIncludeBookmarks
= 7;
882 const int kBookmarkToDelete
= 4;
884 int64 folder_ids
[kNumFolders
];
885 int64 bookmark_ids
[kNumBookmarks
];
887 // Create native folders and bookmarks with identical names. Only
888 // one of the folders contains bookmarks and others are empty. Here is the
889 // expected tree shape:
894 // +- folder (#7) <-- Only this folder contains bookmarks.
898 // +- bookmark (#4) <-- Only this one bookmark should be removed later.
904 const BookmarkNode
* parent_folder
= nullptr;
905 for (int i
= 0; i
< kNumFolders
; i
++) {
906 const BookmarkNode
* folder
= model_
->AddFolder(
907 model_
->bookmark_bar_node(), i
, base::ASCIIToUTF16("folder"));
908 folder_ids
[i
] = folder
->id();
909 if (i
== kFolderToIncludeBookmarks
) {
910 parent_folder
= folder
;
914 for (int i
= 0; i
< kNumBookmarks
; i
++) {
915 const BookmarkNode
* bookmark
=
916 model_
->AddURL(parent_folder
, i
, base::ASCIIToUTF16("bookmark"),
917 GURL("http://www.google.com/"));
918 bookmark_ids
[i
] = bookmark
->id();
921 // Number of nodes in bookmark bar before association.
922 int total_node_count
= model_
->bookmark_bar_node()->GetTotalNodeCount();
924 CreatePermanentBookmarkNodes();
926 int64 sync_bookmark_id_to_delete
= 0;
928 // Create sync folders matching native folders above.
930 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
931 // Create in reverse order because AddFolderToShare passes NULL for
932 // |predecessor| argument.
933 for (int i
= kNumFolders
- 1; i
>= 0; i
--) {
934 int64 id
= AddFolderToShare(&trans
, "folder");
936 // Pre-map sync folders to native folders by setting
937 // external ID. This will verify that the association algorithm picks
938 // the right ones despite all of them having identical names.
939 // More specifically this will help to avoid cloning bookmarks from
941 syncer::WriteNode
node(&trans
);
942 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(id
));
943 node
.GetMutableEntryForTest()->PutLocalExternalId(folder_ids
[i
]);
945 if (i
== kFolderToIncludeBookmarks
) {
950 // Create sync bookmark matching native bookmarks above in reverse order
951 // because AddBookmarkToShare passes NULL for |predecessor| argument.
952 for (int i
= kNumBookmarks
- 1; i
>= 0; i
--) {
953 int id
= AddBookmarkToShare(&trans
, parent_id
, "bookmark",
954 "http://www.google.com/");
956 // Pre-map sync bookmarks to native bookmarks by setting
957 // external ID. This will verify that the association algorithm picks
958 // the right ones despite all of them having identical names and URLs.
959 syncer::WriteNode
node(&trans
);
960 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(id
));
961 node
.GetMutableEntryForTest()->PutLocalExternalId(bookmark_ids
[i
]);
963 if (i
== kBookmarkToDelete
) {
964 sync_bookmark_id_to_delete
= id
;
969 // Make one bookmark deleted.
971 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
972 syncer::WriteNode
node(&trans
);
973 EXPECT_EQ(BaseNode::INIT_OK
,
974 node
.InitByIdLookup(sync_bookmark_id_to_delete
));
976 node
.GetMutableEntryForTest()->PutServerIsDel(true);
977 node
.GetMutableEntryForTest()->PutIsDel(true);
980 // Perform association.
981 ASSERT_TRUE(AssociateModels());
984 // Only one native node should have been deleted and no nodes cloned due to
985 // matching folder names.
986 EXPECT_EQ(kNumFolders
, model_
->bookmark_bar_node()->child_count());
987 EXPECT_EQ(kNumBookmarks
- 1, parent_folder
->child_count());
988 EXPECT_EQ(total_node_count
- 1,
989 model_
->bookmark_bar_node()->GetTotalNodeCount());
991 // Verify that the right bookmark got deleted and no bookmarks reordered.
992 for (int i
= 0; i
< parent_folder
->child_count(); i
++) {
993 int index_in_bookmark_ids
= (i
< kBookmarkToDelete
) ? i
: i
+ 1;
994 EXPECT_EQ(bookmark_ids
[index_in_bookmark_ids
],
995 parent_folder
->GetChild(i
)->id());
999 TEST_F(ProfileSyncServiceBookmarkTest
, BookmarkModelOperations
) {
1000 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1004 const BookmarkNode
* folder
=
1005 model_
->AddFolder(model_
->other_node(), 0, base::ASCIIToUTF16("foobar"));
1006 ExpectSyncerNodeMatching(folder
);
1008 const BookmarkNode
* folder2
=
1009 model_
->AddFolder(folder
, 0, base::ASCIIToUTF16("nested"));
1010 ExpectSyncerNodeMatching(folder2
);
1012 const BookmarkNode
* url1
= model_
->AddURL(
1013 folder
, 0, base::ASCIIToUTF16("Internets #1 Pies Site"),
1014 GURL("http://www.easypie.com/"));
1015 ExpectSyncerNodeMatching(url1
);
1017 const BookmarkNode
* url2
= model_
->AddURL(
1018 folder
, 1, base::ASCIIToUTF16("Airplanes"),
1019 GURL("http://www.easyjet.com/"));
1020 ExpectSyncerNodeMatching(url2
);
1023 const BookmarkNode
* mobile_folder
=
1024 model_
->AddFolder(model_
->mobile_node(), 0, base::ASCIIToUTF16("pie"));
1025 ExpectSyncerNodeMatching(mobile_folder
);
1028 // Test modification.
1029 model_
->SetTitle(url2
, base::ASCIIToUTF16("EasyJet"));
1031 model_
->Move(url1
, folder2
, 0);
1033 model_
->Move(folder2
, model_
->bookmark_bar_node(), 0);
1035 model_
->SetTitle(folder2
, base::ASCIIToUTF16("Not Nested"));
1037 model_
->Move(folder
, folder2
, 0);
1039 model_
->SetTitle(folder
, base::ASCIIToUTF16("who's nested now?"));
1041 model_
->Copy(url2
, model_
->bookmark_bar_node(), 0);
1043 model_
->SetTitle(mobile_folder
, base::ASCIIToUTF16("strawberry"));
1047 // Delete a single item.
1048 model_
->Remove(url2
->parent(), url2
->parent()->GetIndexOf(url2
));
1050 // Delete an item with several children.
1051 model_
->Remove(folder2
->parent(),
1052 folder2
->parent()->GetIndexOf(folder2
));
1054 model_
->Remove(model_
->mobile_node(), 0);
1058 TEST_F(ProfileSyncServiceBookmarkTest
, ServerChangeProcessing
) {
1059 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1062 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1064 FakeServerChange
adds(&trans
);
1065 int64 f1
= adds
.AddFolder("Server Folder B", bookmark_bar_id(), 0);
1066 int64 f2
= adds
.AddFolder("Server Folder A", bookmark_bar_id(), f1
);
1067 int64 u1
= adds
.AddURL("Some old site", "ftp://nifty.andrew.cmu.edu/",
1068 bookmark_bar_id(), f2
);
1069 int64 u2
= adds
.AddURL("Nifty", "ftp://nifty.andrew.cmu.edu/", f1
, 0);
1070 // u3 is a duplicate URL
1071 int64 u3
= adds
.AddURL("Nifty2", "ftp://nifty.andrew.cmu.edu/", f1
, u2
);
1072 // u4 is a duplicate title, different URL.
1073 adds
.AddURL("Some old site", "http://slog.thestranger.com/",
1074 bookmark_bar_id(), u1
);
1075 // u5 tests an empty-string title.
1076 std::string
javascript_url(
1077 "javascript:(function(){var w=window.open(" \
1078 "'about:blank','gnotesWin','location=0,menubar=0," \
1079 "scrollbars=0,status=0,toolbar=0,width=300," \
1080 "height=300,resizable');});");
1081 adds
.AddURL(std::string(), javascript_url
, other_bookmarks_id(), 0);
1082 int64 u6
= adds
.AddURL(
1083 "Sync1", "http://www.syncable.edu/", mobile_bookmarks_id(), 0);
1085 syncer::ChangeRecordList::const_iterator it
;
1086 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
1087 for (it
= adds
.changes().begin(); it
!= adds
.changes().end(); ++it
)
1088 ExpectBrowserNodeUnknown(it
->id
);
1090 adds
.ApplyPendingChanges(change_processor_
.get());
1092 // Make sure the bookmark model received all of the nodes in |adds|.
1093 for (it
= adds
.changes().begin(); it
!= adds
.changes().end(); ++it
)
1094 ExpectBrowserNodeMatching(&trans
, it
->id
);
1095 ExpectModelMatch(&trans
);
1097 // Part two: test modifications.
1098 FakeServerChange
mods(&trans
);
1099 // Mess with u2, and move it into empty folder f2
1100 // TODO(ncarter): Determine if we allow ModifyURL ops or not.
1101 /* std::string u2_old_url = mods.ModifyURL(u2, "http://www.google.com"); */
1102 std::string u2_old_title
= mods
.ModifyTitle(u2
, "The Google");
1103 int64 u2_old_parent
= mods
.ModifyPosition(u2
, f2
, 0);
1105 // Now move f1 after u2.
1106 std::string f1_old_title
= mods
.ModifyTitle(f1
, "Server Folder C");
1107 int64 f1_old_parent
= mods
.ModifyPosition(f1
, f2
, u2
);
1109 // Then add u3 after f1.
1110 int64 u3_old_parent
= mods
.ModifyPosition(u3
, f2
, f1
);
1112 std::string u6_old_title
= mods
.ModifyTitle(u6
, "Mobile Folder A");
1114 // Test that the property changes have not yet taken effect.
1115 ExpectBrowserNodeTitle(u2
, u2_old_title
);
1116 /* ExpectBrowserNodeURL(u2, u2_old_url); */
1117 ExpectBrowserNodeParent(u2
, u2_old_parent
);
1119 ExpectBrowserNodeTitle(f1
, f1_old_title
);
1120 ExpectBrowserNodeParent(f1
, f1_old_parent
);
1122 ExpectBrowserNodeParent(u3
, u3_old_parent
);
1124 ExpectBrowserNodeTitle(u6
, u6_old_title
);
1126 // Apply the changes.
1127 mods
.ApplyPendingChanges(change_processor_
.get());
1129 // Check for successful application.
1130 for (it
= mods
.changes().begin(); it
!= mods
.changes().end(); ++it
)
1131 ExpectBrowserNodeMatching(&trans
, it
->id
);
1132 ExpectModelMatch(&trans
);
1134 // Part 3: Test URL deletion.
1135 FakeServerChange
dels(&trans
);
1140 ExpectBrowserNodeKnown(u2
);
1141 ExpectBrowserNodeKnown(u3
);
1143 dels
.ApplyPendingChanges(change_processor_
.get());
1145 ExpectBrowserNodeUnknown(u2
);
1146 ExpectBrowserNodeUnknown(u3
);
1147 ExpectBrowserNodeUnknown(u6
);
1148 ExpectModelMatch(&trans
);
1151 // Tests a specific case in ApplyModelChanges where we move the
1152 // children out from under a parent, and then delete the parent
1153 // in the same changelist. The delete shows up first in the changelist,
1154 // requiring the children to be moved to a temporary location.
1155 TEST_F(ProfileSyncServiceBookmarkTest
, ServerChangeRequiringFosterParent
) {
1156 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1159 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1161 // Stress the immediate children of other_node because that's where
1162 // ApplyModelChanges puts a temporary foster parent node.
1163 std::string
url("http://dev.chromium.org/");
1164 FakeServerChange
adds(&trans
);
1165 int64 f0
= other_bookmarks_id(); // + other_node
1166 int64 f1
= adds
.AddFolder("f1", f0
, 0); // + f1
1167 int64 f2
= adds
.AddFolder("f2", f1
, 0); // + f2
1168 int64 u3
= adds
.AddURL( "u3", url
, f2
, 0); // + u3 NOLINT
1169 int64 u4
= adds
.AddURL( "u4", url
, f2
, u3
); // + u4 NOLINT
1170 int64 u5
= adds
.AddURL( "u5", url
, f1
, f2
); // + u5 NOLINT
1171 int64 f6
= adds
.AddFolder("f6", f1
, u5
); // + f6
1172 int64 u7
= adds
.AddURL( "u7", url
, f0
, f1
); // + u7 NOLINT
1174 syncer::ChangeRecordList::const_iterator it
;
1175 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
1176 for (it
= adds
.changes().begin(); it
!= adds
.changes().end(); ++it
)
1177 ExpectBrowserNodeUnknown(it
->id
);
1179 adds
.ApplyPendingChanges(change_processor_
.get());
1181 // Make sure the bookmark model received all of the nodes in |adds|.
1182 for (it
= adds
.changes().begin(); it
!= adds
.changes().end(); ++it
)
1183 ExpectBrowserNodeMatching(&trans
, it
->id
);
1184 ExpectModelMatch(&trans
);
1186 // We have to do the moves before the deletions, but FakeServerChange will
1187 // put the deletion at the front of the changelist.
1188 FakeServerChange
ops(&trans
);
1189 ops
.ModifyPosition(f6
, other_bookmarks_id(), 0);
1190 ops
.ModifyPosition(u3
, other_bookmarks_id(), f1
); // Prev == f1 is OK here.
1191 ops
.ModifyPosition(f2
, other_bookmarks_id(), u7
);
1192 ops
.ModifyPosition(u7
, f2
, 0);
1193 ops
.ModifyPosition(u4
, other_bookmarks_id(), f2
);
1194 ops
.ModifyPosition(u5
, f6
, 0);
1197 ops
.ApplyPendingChanges(change_processor_
.get());
1199 ExpectModelMatch(&trans
);
1202 // Simulate a server change record containing a valid but non-canonical URL.
1203 TEST_F(ProfileSyncServiceBookmarkTest
, ServerChangeWithNonCanonicalURL
) {
1204 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1208 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1210 FakeServerChange
adds(&trans
);
1211 std::string
url("http://dev.chromium.org");
1212 EXPECT_NE(GURL(url
).spec(), url
);
1213 adds
.AddURL("u1", url
, other_bookmarks_id(), 0);
1215 adds
.ApplyPendingChanges(change_processor_
.get());
1217 EXPECT_EQ(1, model_
->other_node()->child_count());
1218 ExpectModelMatch(&trans
);
1221 // Now reboot the sync service, forcing a merge step.
1223 LoadBookmarkModel(LOAD_FROM_STORAGE
, SAVE_TO_STORAGE
);
1226 // There should still be just the one bookmark.
1227 EXPECT_EQ(1, model_
->other_node()->child_count());
1231 // Simulate a server change record containing an invalid URL (per GURL).
1232 // TODO(ncarter): Disabled due to crashes. Fix bug 1677563.
1233 TEST_F(ProfileSyncServiceBookmarkTest
, DISABLED_ServerChangeWithInvalidURL
) {
1234 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1237 int child_count
= 0;
1239 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1241 FakeServerChange
adds(&trans
);
1242 std::string
url("x");
1243 EXPECT_FALSE(GURL(url
).is_valid());
1244 adds
.AddURL("u1", url
, other_bookmarks_id(), 0);
1246 adds
.ApplyPendingChanges(change_processor_
.get());
1248 // We're lenient about what should happen -- the model could wind up with
1249 // the node or without it; but things should be consistent, and we
1251 child_count
= model_
->other_node()->child_count();
1252 EXPECT_TRUE(child_count
== 0 || child_count
== 1);
1253 ExpectModelMatch(&trans
);
1256 // Now reboot the sync service, forcing a merge step.
1258 LoadBookmarkModel(LOAD_FROM_STORAGE
, SAVE_TO_STORAGE
);
1261 // Things ought not to have changed.
1262 EXPECT_EQ(model_
->other_node()->child_count(), child_count
);
1267 // Test strings that might pose a problem if the titles ever became used as
1268 // file names in the sync backend.
1269 TEST_F(ProfileSyncServiceBookmarkTest
, CornerCaseNames
) {
1270 // TODO(ncarter): Bug 1570238 explains the failure of this test.
1271 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1274 const char* names
[] = {
1275 // The empty string.
1277 // Illegal Windows filenames.
1278 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",
1279 "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3",
1280 "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
1281 // Current/parent directory markers.
1283 // Files created automatically by the Windows shell.
1284 "Thumbs.db", ".DS_Store",
1285 // Names including Win32-illegal characters, and path separators.
1286 "foo/bar", "foo\\bar", "foo?bar", "foo:bar", "foo|bar", "foo\"bar",
1287 "foo'bar", "foo<bar", "foo>bar", "foo%bar", "foo*bar", "foo]bar",
1289 // A name with title > 255 characters
1290 "012345678901234567890123456789012345678901234567890123456789012345678901"
1291 "234567890123456789012345678901234567890123456789012345678901234567890123"
1292 "456789012345678901234567890123456789012345678901234567890123456789012345"
1293 "678901234567890123456789012345678901234567890123456789012345678901234567"
1296 // Create both folders and bookmarks using each name.
1297 GURL
url("http://www.doublemint.com");
1298 for (size_t i
= 0; i
< arraysize(names
); ++i
) {
1299 model_
->AddFolder(model_
->other_node(), 0, base::ASCIIToUTF16(names
[i
]));
1300 model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16(names
[i
]), url
);
1303 // Verify that the browser model matches the sync model.
1304 EXPECT_EQ(static_cast<size_t>(model_
->other_node()->child_count()),
1305 2*arraysize(names
));
1308 // Restart and re-associate. Verify things still match.
1310 LoadBookmarkModel(LOAD_FROM_STORAGE
, SAVE_TO_STORAGE
);
1312 EXPECT_EQ(static_cast<size_t>(model_
->other_node()->child_count()),
1313 2*arraysize(names
));
1317 // Stress the internal representation of position by sparse numbers. We want
1318 // to repeatedly bisect the range of available positions, to force the
1319 // syncer code to renumber its ranges. Pick a number big enough so that it
1320 // would exhaust 32bits of room between items a couple of times.
1321 TEST_F(ProfileSyncServiceBookmarkTest
, RepeatedMiddleInsertion
) {
1322 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1325 static const int kTimesToInsert
= 256;
1327 // Create two book-end nodes to insert between.
1328 model_
->AddFolder(model_
->other_node(), 0, base::ASCIIToUTF16("Alpha"));
1329 model_
->AddFolder(model_
->other_node(), 1, base::ASCIIToUTF16("Omega"));
1332 // Test insertion in first half of range by repeatedly inserting in second
1334 for (int i
= 0; i
< kTimesToInsert
; ++i
) {
1335 base::string16 title
=
1336 base::ASCIIToUTF16("Pre-insertion ") + base::IntToString16(i
);
1337 model_
->AddFolder(model_
->other_node(), 1, title
);
1341 // Test insertion in second half of range by repeatedly inserting in
1342 // second-to-last position.
1343 for (int i
= 0; i
< kTimesToInsert
; ++i
) {
1344 base::string16 title
=
1345 base::ASCIIToUTF16("Post-insertion ") + base::IntToString16(i
);
1346 model_
->AddFolder(model_
->other_node(), count
- 1, title
);
1350 // Verify that the browser model matches the sync model.
1351 EXPECT_EQ(model_
->other_node()->child_count(), count
);
1355 // Introduce a consistency violation into the model, and see that it
1356 // puts itself into a lame, error state.
1357 TEST_F(ProfileSyncServiceBookmarkTest
, UnrecoverableErrorSuspendsService
) {
1358 EXPECT_CALL(mock_error_handler_
,
1359 OnSingleDataTypeUnrecoverableError(_
));
1361 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1364 // Add a node which will be the target of the consistency violation.
1365 const BookmarkNode
* node
=
1366 model_
->AddFolder(model_
->other_node(), 0, base::ASCIIToUTF16("node"));
1367 ExpectSyncerNodeMatching(node
);
1369 // Now destroy the syncer node as if we were the ProfileSyncService without
1370 // updating the ProfileSyncService state. This should introduce
1371 // inconsistency between the two models.
1373 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1374 syncer::WriteNode
sync_node(&trans
);
1375 ASSERT_TRUE(InitSyncNodeFromChromeNode(node
, &sync_node
));
1376 sync_node
.Tombstone();
1378 // The models don't match at this point, but the ProfileSyncService
1379 // doesn't know it yet.
1380 ExpectSyncerNodeKnown(node
);
1382 // Add a child to the inconsistent node. This should cause detection of the
1383 // problem and the syncer should stop processing changes.
1384 model_
->AddFolder(node
, 0, base::ASCIIToUTF16("nested"));
1387 // See what happens if we run model association when there are two exact URL
1388 // duplicate bookmarks. The BookmarkModelAssociator should not fall over when
1390 TEST_F(ProfileSyncServiceBookmarkTest
, MergeDuplicates
) {
1391 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1394 model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("Dup"),
1395 GURL("http://dup.com/"));
1396 model_
->AddURL(model_
->other_node(), 0, base::ASCIIToUTF16("Dup"),
1397 GURL("http://dup.com/"));
1399 EXPECT_EQ(2, model_
->other_node()->child_count());
1401 // Restart the sync service to trigger model association.
1405 EXPECT_EQ(2, model_
->other_node()->child_count());
1409 TEST_F(ProfileSyncServiceBookmarkTest
, ApplySyncDeletesFromJournal
) {
1410 // Initialize sync model and bookmark model as:
1416 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1423 int fixed_sync_bk_count
= GetSyncBookmarkCount();
1425 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1426 FakeServerChange
adds(&trans
);
1427 u0
= adds
.AddURL("URL 0", "http://plus.google.com/", bookmark_bar_id(), 0);
1428 f1
= adds
.AddFolder("Folder 1", bookmark_bar_id(), u0
);
1429 u1
= adds
.AddURL("URL 1", "http://www.google.com/", f1
, 0);
1430 f2
= adds
.AddFolder("Folder 2", f1
, u1
);
1431 u2
= adds
.AddURL("URL 2", "http://mail.google.com/", f2
, 0);
1432 adds
.ApplyPendingChanges(change_processor_
.get());
1436 // Reload bookmark model and disable model saving to make sync changes not
1438 LoadBookmarkModel(LOAD_FROM_STORAGE
, DONT_SAVE_TO_STORAGE
);
1439 EXPECT_EQ(6, model_
->bookmark_bar_node()->GetTotalNodeCount());
1440 EXPECT_EQ(fixed_sync_bk_count
+ 5, GetSyncBookmarkCount());
1443 // Remove all folders/bookmarks except u3 added above.
1444 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
1445 FakeServerChange
dels(&trans
);
1450 dels
.ApplyPendingChanges(change_processor_
.get());
1453 // Bookmark bar itself and u0 remain.
1454 EXPECT_EQ(2, model_
->bookmark_bar_node()->GetTotalNodeCount());
1456 // Reload bookmarks including ones deleted in sync model from storage.
1457 LoadBookmarkModel(LOAD_FROM_STORAGE
, DONT_SAVE_TO_STORAGE
);
1458 EXPECT_EQ(6, model_
->bookmark_bar_node()->GetTotalNodeCount());
1459 // Add a bookmark under f1 when sync is off so that f1 will not be
1460 // deleted even when f1 matches delete journal because it's not empty.
1461 model_
->AddURL(model_
->bookmark_bar_node()->GetChild(1),
1462 0, base::UTF8ToUTF16("local"), GURL("http://www.youtube.com"));
1463 // Sync model has fixed bookmarks nodes and u3.
1464 EXPECT_EQ(fixed_sync_bk_count
+ 1, GetSyncBookmarkCount());
1466 // Expect 4 bookmarks after model association because u2, f2, u1 are removed
1467 // by delete journal, f1 is not removed by delete journal because it's
1468 // not empty due to www.youtube.com added above.
1469 EXPECT_EQ(4, model_
->bookmark_bar_node()->GetTotalNodeCount());
1470 EXPECT_EQ(base::UTF8ToUTF16("URL 0"),
1471 model_
->bookmark_bar_node()->GetChild(0)->GetTitle());
1472 EXPECT_EQ(base::UTF8ToUTF16("Folder 1"),
1473 model_
->bookmark_bar_node()->GetChild(1)->GetTitle());
1474 EXPECT_EQ(base::UTF8ToUTF16("local"),
1475 model_
->bookmark_bar_node()->GetChild(1)->GetChild(0)->GetTitle());
1478 // Verify purging of delete journals.
1479 // Delete journals for u2, f2, u1 remains because they are used in last
1481 EXPECT_EQ(3u, test_user_share_
.GetDeleteJournalSize());
1484 // Reload again and all delete journals should be gone because none is used
1485 // in last association.
1486 ASSERT_TRUE(test_user_share_
.Reload());
1487 EXPECT_EQ(0u, test_user_share_
.GetDeleteJournalSize());
1495 // Map from bookmark node ID to its version.
1496 typedef std::map
<int64
, int64
> BookmarkNodeVersionMap
;
1498 // TODO(ncarter): Integrate the existing TestNode/PopulateNodeFromString code
1499 // in the bookmark model unittest, to make it simpler to set up test data
1500 // here (and reduce the amount of duplication among tests), and to reduce the
1502 class ProfileSyncServiceBookmarkTestWithData
1503 : public ProfileSyncServiceBookmarkTest
{
1505 ProfileSyncServiceBookmarkTestWithData();
1508 // Populates or compares children of the given bookmark node from/with the
1509 // given test data array with the given size. |running_count| is updated as
1510 // urls are added. It is used to set the creation date (or test the creation
1511 // date for CompareWithTestData()).
1512 void PopulateFromTestData(const BookmarkNode
* node
,
1513 const TestData
* data
,
1515 int* running_count
);
1516 void CompareWithTestData(const BookmarkNode
* node
,
1517 const TestData
* data
,
1519 int* running_count
);
1521 void ExpectBookmarkModelMatchesTestData();
1522 void WriteTestDataToBookmarkModel();
1524 // Output transaction versions of |node| and nodes under it to
1526 void GetTransactionVersions(const BookmarkNode
* root
,
1527 BookmarkNodeVersionMap
* node_versions
);
1529 // Verify transaction versions of bookmark nodes and sync nodes are equal
1530 // recursively. If node is in |version_expected|, versions should match
1532 void ExpectTransactionVersionMatch(
1533 const BookmarkNode
* node
,
1534 const BookmarkNodeVersionMap
& version_expected
);
1537 const base::Time start_time_
;
1539 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceBookmarkTestWithData
);
1544 // Constants for bookmark model that looks like:
1546 // | |-- u2, http://www.u2.com/
1548 // | | |-- f1u4, http://www.f1u4.com/
1549 // | | |-- f1u2, http://www.f1u2.com/
1550 // | | |-- f1u3, http://www.f1u3.com/
1551 // | | +-- f1u1, http://www.f1u1.com/
1552 // | |-- u1, http://www.u1.com/
1554 // | |-- f2u2, http://www.f2u2.com/
1555 // | |-- f2u4, http://www.f2u4.com/
1556 // | |-- f2u3, http://www.f2u3.com/
1557 // | +-- f2u1, http://www.f2u1.com/
1558 // +-- Other bookmarks
1560 // | | |-- f3u4, http://www.f3u4.com/
1561 // | | |-- f3u2, http://www.f3u2.com/
1562 // | | |-- f3u3, http://www.f3u3.com/
1563 // | | +-- f3u1, http://www.f3u1.com/
1564 // | |-- u4, http://www.u4.com/
1565 // | |-- u3, http://www.u3.com/
1567 // | | |-- f4u1, http://www.f4u1.com/
1568 // | | |-- f4u2, http://www.f4u2.com/
1569 // | | |-- f4u3, http://www.f4u3.com/
1570 // | | +-- f4u4, http://www.f4u4.com/
1572 // | | +-- dupu1, http://www.dupu1.com/
1574 // | | +-- dupu2, http://www.dupu1.com/
1575 // | +-- ls , http://www.ls.com/
1577 // +-- Mobile bookmarks
1579 // | |-- f5u1, http://www.f5u1.com/
1581 // | |-- f6u1, http://www.f6u1.com/
1582 // | |-- f6u2, http://www.f6u2.com/
1583 // +-- u5, http://www.u5.com/
1585 static TestData kBookmarkBarChildren
[] = {
1586 { "u2", "http://www.u2.com/" },
1588 { "u1", "http://www.u1.com/" },
1591 static TestData kF1Children
[] = {
1592 { "f1u4", "http://www.f1u4.com/" },
1593 { "f1u2", "http://www.f1u2.com/" },
1594 { "f1u3", "http://www.f1u3.com/" },
1595 { "f1u1", "http://www.f1u1.com/" },
1597 static TestData kF2Children
[] = {
1598 { "f2u2", "http://www.f2u2.com/" },
1599 { "f2u4", "http://www.f2u4.com/" },
1600 { "f2u3", "http://www.f2u3.com/" },
1601 { "f2u1", "http://www.f2u1.com/" },
1604 static TestData kOtherBookmarkChildren
[] = {
1606 { "u4", "http://www.u4.com/" },
1607 { "u3", "http://www.u3.com/" },
1611 { " ls ", "http://www.ls.com/" }
1613 static TestData kF3Children
[] = {
1614 { "f3u4", "http://www.f3u4.com/" },
1615 { "f3u2", "http://www.f3u2.com/" },
1616 { "f3u3", "http://www.f3u3.com/" },
1617 { "f3u1", "http://www.f3u1.com/" },
1619 static TestData kF4Children
[] = {
1620 { "f4u1", "http://www.f4u1.com/" },
1621 { "f4u2", "http://www.f4u2.com/" },
1622 { "f4u3", "http://www.f4u3.com/" },
1623 { "f4u4", "http://www.f4u4.com/" },
1625 static TestData kDup1Children
[] = {
1626 { "dupu1", "http://www.dupu1.com/" },
1628 static TestData kDup2Children
[] = {
1629 { "dupu2", "http://www.dupu2.com/" },
1632 static TestData kMobileBookmarkChildren
[] = {
1635 { "u5", "http://www.u5.com/" },
1637 static TestData kF5Children
[] = {
1638 { "f5u1", "http://www.f5u1.com/" },
1639 { "f5u2", "http://www.f5u2.com/" },
1641 static TestData kF6Children
[] = {
1642 { "f6u1", "http://www.f6u1.com/" },
1643 { "f6u2", "http://www.f6u2.com/" },
1646 } // anonymous namespace.
1648 ProfileSyncServiceBookmarkTestWithData::
1649 ProfileSyncServiceBookmarkTestWithData()
1650 : start_time_(base::Time::Now()) {
1653 void ProfileSyncServiceBookmarkTestWithData::PopulateFromTestData(
1654 const BookmarkNode
* node
,
1655 const TestData
* data
,
1657 int* running_count
) {
1660 DCHECK(node
->is_folder());
1661 for (int i
= 0; i
< size
; ++i
) {
1662 const TestData
& item
= data
[i
];
1664 const base::Time add_time
=
1665 start_time_
+ base::TimeDelta::FromMinutes(*running_count
);
1666 model_
->AddURLWithCreationTimeAndMetaInfo(node
,
1668 base::UTF8ToUTF16(item
.title
),
1673 model_
->AddFolder(node
, i
, base::UTF8ToUTF16(item
.title
));
1679 void ProfileSyncServiceBookmarkTestWithData::CompareWithTestData(
1680 const BookmarkNode
* node
,
1681 const TestData
* data
,
1683 int* running_count
) {
1686 DCHECK(node
->is_folder());
1687 ASSERT_EQ(size
, node
->child_count());
1688 for (int i
= 0; i
< size
; ++i
) {
1689 const BookmarkNode
* child_node
= node
->GetChild(i
);
1690 const TestData
& item
= data
[i
];
1691 GURL url
= GURL(item
.url
== NULL
? "" : item
.url
);
1692 BookmarkNode
test_node(url
);
1693 test_node
.SetTitle(base::UTF8ToUTF16(item
.title
));
1694 EXPECT_EQ(child_node
->GetTitle(), test_node
.GetTitle());
1696 EXPECT_FALSE(child_node
->is_folder());
1697 EXPECT_TRUE(child_node
->is_url());
1698 EXPECT_EQ(child_node
->url(), test_node
.url());
1699 const base::Time expected_time
=
1700 start_time_
+ base::TimeDelta::FromMinutes(*running_count
);
1701 EXPECT_EQ(expected_time
.ToInternalValue(),
1702 child_node
->date_added().ToInternalValue());
1704 EXPECT_TRUE(child_node
->is_folder());
1705 EXPECT_FALSE(child_node
->is_url());
1711 // TODO(munjal): We should implement some way of generating random data and can
1712 // use the same seed to generate the same sequence.
1713 void ProfileSyncServiceBookmarkTestWithData::WriteTestDataToBookmarkModel() {
1714 const BookmarkNode
* bookmarks_bar_node
= model_
->bookmark_bar_node();
1716 PopulateFromTestData(bookmarks_bar_node
,
1717 kBookmarkBarChildren
,
1718 arraysize(kBookmarkBarChildren
),
1721 ASSERT_GE(bookmarks_bar_node
->child_count(), 4);
1722 const BookmarkNode
* f1_node
= bookmarks_bar_node
->GetChild(1);
1723 PopulateFromTestData(f1_node
, kF1Children
, arraysize(kF1Children
), &count
);
1724 const BookmarkNode
* f2_node
= bookmarks_bar_node
->GetChild(3);
1725 PopulateFromTestData(f2_node
, kF2Children
, arraysize(kF2Children
), &count
);
1727 const BookmarkNode
* other_bookmarks_node
= model_
->other_node();
1728 PopulateFromTestData(other_bookmarks_node
,
1729 kOtherBookmarkChildren
,
1730 arraysize(kOtherBookmarkChildren
),
1733 ASSERT_GE(other_bookmarks_node
->child_count(), 6);
1734 const BookmarkNode
* f3_node
= other_bookmarks_node
->GetChild(0);
1735 PopulateFromTestData(f3_node
, kF3Children
, arraysize(kF3Children
), &count
);
1736 const BookmarkNode
* f4_node
= other_bookmarks_node
->GetChild(3);
1737 PopulateFromTestData(f4_node
, kF4Children
, arraysize(kF4Children
), &count
);
1738 const BookmarkNode
* dup_node
= other_bookmarks_node
->GetChild(4);
1739 PopulateFromTestData(dup_node
, kDup1Children
, arraysize(kDup1Children
),
1741 dup_node
= other_bookmarks_node
->GetChild(5);
1742 PopulateFromTestData(dup_node
, kDup2Children
, arraysize(kDup2Children
),
1745 const BookmarkNode
* mobile_bookmarks_node
= model_
->mobile_node();
1746 PopulateFromTestData(mobile_bookmarks_node
,
1747 kMobileBookmarkChildren
,
1748 arraysize(kMobileBookmarkChildren
),
1751 ASSERT_GE(mobile_bookmarks_node
->child_count(), 3);
1752 const BookmarkNode
* f5_node
= mobile_bookmarks_node
->GetChild(0);
1753 PopulateFromTestData(f5_node
, kF5Children
, arraysize(kF5Children
), &count
);
1754 const BookmarkNode
* f6_node
= mobile_bookmarks_node
->GetChild(1);
1755 PopulateFromTestData(f6_node
, kF6Children
, arraysize(kF6Children
), &count
);
1757 ExpectBookmarkModelMatchesTestData();
1760 void ProfileSyncServiceBookmarkTestWithData::
1761 ExpectBookmarkModelMatchesTestData() {
1762 const BookmarkNode
* bookmark_bar_node
= model_
->bookmark_bar_node();
1764 CompareWithTestData(bookmark_bar_node
,
1765 kBookmarkBarChildren
,
1766 arraysize(kBookmarkBarChildren
),
1769 ASSERT_GE(bookmark_bar_node
->child_count(), 4);
1770 const BookmarkNode
* f1_node
= bookmark_bar_node
->GetChild(1);
1771 CompareWithTestData(f1_node
, kF1Children
, arraysize(kF1Children
), &count
);
1772 const BookmarkNode
* f2_node
= bookmark_bar_node
->GetChild(3);
1773 CompareWithTestData(f2_node
, kF2Children
, arraysize(kF2Children
), &count
);
1775 const BookmarkNode
* other_bookmarks_node
= model_
->other_node();
1776 CompareWithTestData(other_bookmarks_node
,
1777 kOtherBookmarkChildren
,
1778 arraysize(kOtherBookmarkChildren
),
1781 ASSERT_GE(other_bookmarks_node
->child_count(), 6);
1782 const BookmarkNode
* f3_node
= other_bookmarks_node
->GetChild(0);
1783 CompareWithTestData(f3_node
, kF3Children
, arraysize(kF3Children
), &count
);
1784 const BookmarkNode
* f4_node
= other_bookmarks_node
->GetChild(3);
1785 CompareWithTestData(f4_node
, kF4Children
, arraysize(kF4Children
), &count
);
1786 const BookmarkNode
* dup_node
= other_bookmarks_node
->GetChild(4);
1787 CompareWithTestData(dup_node
, kDup1Children
, arraysize(kDup1Children
),
1789 dup_node
= other_bookmarks_node
->GetChild(5);
1790 CompareWithTestData(dup_node
, kDup2Children
, arraysize(kDup2Children
),
1793 const BookmarkNode
* mobile_bookmarks_node
= model_
->mobile_node();
1794 CompareWithTestData(mobile_bookmarks_node
,
1795 kMobileBookmarkChildren
,
1796 arraysize(kMobileBookmarkChildren
),
1799 ASSERT_GE(mobile_bookmarks_node
->child_count(), 3);
1800 const BookmarkNode
* f5_node
= mobile_bookmarks_node
->GetChild(0);
1801 CompareWithTestData(f5_node
, kF5Children
, arraysize(kF5Children
), &count
);
1802 const BookmarkNode
* f6_node
= mobile_bookmarks_node
->GetChild(1);
1803 CompareWithTestData(f6_node
, kF6Children
, arraysize(kF6Children
), &count
);
1806 // Tests persistence of the profile sync service by unloading the
1807 // database and then reloading it from disk.
1808 TEST_F(ProfileSyncServiceBookmarkTestWithData
, Persistence
) {
1809 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1812 WriteTestDataToBookmarkModel();
1816 // Force both models to discard their data and reload from disk. This
1817 // simulates what would happen if the browser were to shutdown normally,
1818 // and then relaunch.
1820 UnloadBookmarkModel();
1821 LoadBookmarkModel(LOAD_FROM_STORAGE
, SAVE_TO_STORAGE
);
1824 ExpectBookmarkModelMatchesTestData();
1826 // With the BookmarkModel contents verified, ExpectModelMatch will
1827 // verify the contents of the sync model.
1831 // Tests the merge case when the BookmarkModel is non-empty but the
1832 // sync model is empty. This corresponds to uploading browser
1833 // bookmarks to an initially empty, new account.
1834 TEST_F(ProfileSyncServiceBookmarkTestWithData
, MergeWithEmptySyncModel
) {
1835 // Don't start the sync service until we've populated the bookmark model.
1836 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1838 WriteTestDataToBookmarkModel();
1840 // Restart sync. This should trigger a merge step during
1841 // initialization -- we expect the browser bookmarks to be written
1842 // to the sync service during this call.
1845 // Verify that the bookmark model hasn't changed, and that the sync model
1846 // matches it exactly.
1847 ExpectBookmarkModelMatchesTestData();
1851 // Tests the merge case when the BookmarkModel is empty but the sync model is
1852 // non-empty. This corresponds (somewhat) to a clean install of the browser,
1853 // with no bookmarks, connecting to a sync account that has some bookmarks.
1854 TEST_F(ProfileSyncServiceBookmarkTestWithData
, MergeWithEmptyBookmarkModel
) {
1855 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1858 WriteTestDataToBookmarkModel();
1862 // Force the databse to unload and write itself to disk.
1865 // Blow away the bookmark model -- it should be empty afterwards.
1866 UnloadBookmarkModel();
1867 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1868 EXPECT_EQ(model_
->bookmark_bar_node()->child_count(), 0);
1869 EXPECT_EQ(model_
->other_node()->child_count(), 0);
1870 EXPECT_EQ(model_
->mobile_node()->child_count(), 0);
1872 // Now restart the sync service. Starting it should populate the bookmark
1873 // model -- test for consistency.
1875 ExpectBookmarkModelMatchesTestData();
1879 // Tests the merge cases when both the models are expected to be identical
1881 TEST_F(ProfileSyncServiceBookmarkTestWithData
, MergeExpectedIdenticalModels
) {
1882 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
1884 WriteTestDataToBookmarkModel();
1887 UnloadBookmarkModel();
1889 // At this point both the bookmark model and the server should have the
1890 // exact same data and it should match the test data.
1891 LoadBookmarkModel(LOAD_FROM_STORAGE
, DONT_SAVE_TO_STORAGE
);
1893 ExpectBookmarkModelMatchesTestData();
1896 UnloadBookmarkModel();
1898 // Now reorder some bookmarks in the bookmark model and then merge. Make
1899 // sure we get the order of the server after merge.
1900 LoadBookmarkModel(LOAD_FROM_STORAGE
, DONT_SAVE_TO_STORAGE
);
1901 ExpectBookmarkModelMatchesTestData();
1902 const BookmarkNode
* bookmark_bar
= model_
->bookmark_bar_node();
1903 ASSERT_TRUE(bookmark_bar
);
1904 ASSERT_GT(bookmark_bar
->child_count(), 1);
1905 model_
->Move(bookmark_bar
->GetChild(0), bookmark_bar
, 1);
1908 ExpectBookmarkModelMatchesTestData();
1911 // Tests the merge cases when both the models are expected to be identical
1913 TEST_F(ProfileSyncServiceBookmarkTestWithData
, MergeModelsWithSomeExtras
) {
1914 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1915 WriteTestDataToBookmarkModel();
1916 ExpectBookmarkModelMatchesTestData();
1918 // Remove some nodes and reorder some nodes.
1919 const BookmarkNode
* bookmark_bar_node
= model_
->bookmark_bar_node();
1920 int remove_index
= 2;
1921 ASSERT_GT(bookmark_bar_node
->child_count(), remove_index
);
1922 const BookmarkNode
* child_node
= bookmark_bar_node
->GetChild(remove_index
);
1923 ASSERT_TRUE(child_node
);
1924 ASSERT_TRUE(child_node
->is_url());
1925 model_
->Remove(bookmark_bar_node
, remove_index
);
1926 ASSERT_GT(bookmark_bar_node
->child_count(), remove_index
);
1927 child_node
= bookmark_bar_node
->GetChild(remove_index
);
1928 ASSERT_TRUE(child_node
);
1929 ASSERT_TRUE(child_node
->is_folder());
1930 model_
->Remove(bookmark_bar_node
, remove_index
);
1932 const BookmarkNode
* other_node
= model_
->other_node();
1933 ASSERT_GE(other_node
->child_count(), 1);
1934 const BookmarkNode
* f3_node
= other_node
->GetChild(0);
1935 ASSERT_TRUE(f3_node
);
1936 ASSERT_TRUE(f3_node
->is_folder());
1938 ASSERT_GT(f3_node
->child_count(), remove_index
);
1939 model_
->Remove(f3_node
, remove_index
);
1940 ASSERT_GT(f3_node
->child_count(), remove_index
);
1941 model_
->Remove(f3_node
, remove_index
);
1947 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1948 WriteTestDataToBookmarkModel();
1949 ExpectBookmarkModelMatchesTestData();
1951 // Remove some nodes and reorder some nodes.
1952 bookmark_bar_node
= model_
->bookmark_bar_node();
1954 ASSERT_GT(bookmark_bar_node
->child_count(), remove_index
);
1955 child_node
= bookmark_bar_node
->GetChild(remove_index
);
1956 ASSERT_TRUE(child_node
);
1957 ASSERT_TRUE(child_node
->is_url());
1958 model_
->Remove(bookmark_bar_node
, remove_index
);
1959 ASSERT_GT(bookmark_bar_node
->child_count(), remove_index
);
1960 child_node
= bookmark_bar_node
->GetChild(remove_index
);
1961 ASSERT_TRUE(child_node
);
1962 ASSERT_TRUE(child_node
->is_folder());
1963 model_
->Remove(bookmark_bar_node
, remove_index
);
1965 ASSERT_GE(bookmark_bar_node
->child_count(), 2);
1966 model_
->Move(bookmark_bar_node
->GetChild(0), bookmark_bar_node
, 1);
1968 other_node
= model_
->other_node();
1969 ASSERT_GE(other_node
->child_count(), 1);
1970 f3_node
= other_node
->GetChild(0);
1971 ASSERT_TRUE(f3_node
);
1972 ASSERT_TRUE(f3_node
->is_folder());
1974 ASSERT_GT(f3_node
->child_count(), remove_index
);
1975 model_
->Remove(f3_node
, remove_index
);
1976 ASSERT_GT(f3_node
->child_count(), remove_index
);
1977 model_
->Remove(f3_node
, remove_index
);
1979 ASSERT_GE(other_node
->child_count(), 4);
1980 model_
->Move(other_node
->GetChild(0), other_node
, 1);
1981 model_
->Move(other_node
->GetChild(2), other_node
, 3);
1986 // After the merge, the model should match the test data.
1987 ExpectBookmarkModelMatchesTestData();
1990 // Tests that when persisted model associations are used, things work fine.
1991 TEST_F(ProfileSyncServiceBookmarkTestWithData
, ModelAssociationPersistence
) {
1992 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
1993 WriteTestDataToBookmarkModel();
1996 // Force sync to shut down and write itself to disk.
1998 // Now restart sync. This time it should use the persistent
2004 // Tests that when persisted model associations are used, things work fine.
2005 TEST_F(ProfileSyncServiceBookmarkTestWithData
,
2006 ModelAssociationInvalidPersistence
) {
2007 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2008 WriteTestDataToBookmarkModel();
2011 // Force sync to shut down and write itself to disk.
2013 // Change the bookmark model before restarting sync service to simulate
2014 // the situation where bookmark model is different from sync model and
2015 // make sure model associator correctly rebuilds associations.
2016 const BookmarkNode
* bookmark_bar_node
= model_
->bookmark_bar_node();
2017 model_
->AddURL(bookmark_bar_node
, 0, base::ASCIIToUTF16("xtra"),
2018 GURL("http://www.xtra.com"));
2019 // Now restart sync. This time it will try to use the persistent
2020 // associations and realize that they are invalid and hence will rebuild
2026 TEST_F(ProfileSyncServiceBookmarkTestWithData
, SortChildren
) {
2027 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2030 // Write test data to bookmark model and verify that the models match.
2031 WriteTestDataToBookmarkModel();
2032 const BookmarkNode
* folder_added
= model_
->other_node()->GetChild(0);
2033 ASSERT_TRUE(folder_added
);
2034 ASSERT_TRUE(folder_added
->is_folder());
2038 // Sort the other-bookmarks children and expect that the models match.
2039 model_
->SortChildren(folder_added
);
2043 // See what happens if we enable sync but then delete the "Sync Data"
2045 TEST_F(ProfileSyncServiceBookmarkTestWithData
,
2046 RecoverAfterDeletingSyncDataDirectory
) {
2047 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, SAVE_TO_STORAGE
);
2050 WriteTestDataToBookmarkModel();
2054 // Nuke the sync DB and reload.
2058 // First attempt fails due to a persistence error.
2059 EXPECT_TRUE(CreatePermanentBookmarkNodes());
2060 EXPECT_FALSE(AssociateModels());
2062 // Second attempt succeeds due to the previous error resetting the native
2063 // transaction version.
2064 model_associator_
.reset();
2065 EXPECT_TRUE(CreatePermanentBookmarkNodes());
2066 EXPECT_TRUE(AssociateModels());
2068 // Make sure we're back in sync. In real life, the user would need
2069 // to reauthenticate before this happens, but in the test, authentication
2071 ExpectBookmarkModelMatchesTestData();
2075 // Verify that the bookmark model is updated about whether the
2076 // associator is currently running.
2077 TEST_F(ProfileSyncServiceBookmarkTest
, AssociationState
) {
2078 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2080 ExtensiveChangesBookmarkModelObserver observer
;
2081 model_
->AddObserver(&observer
);
2085 EXPECT_EQ(1, observer
.get_started());
2086 EXPECT_EQ(0, observer
.get_completed_count_at_started());
2087 EXPECT_EQ(1, observer
.get_completed());
2089 model_
->RemoveObserver(&observer
);
2092 // Verify that the creation_time_us changes are applied in the local model at
2093 // association time and update time.
2094 TEST_F(ProfileSyncServiceBookmarkTestWithData
, UpdateDateAdded
) {
2095 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2096 WriteTestDataToBookmarkModel();
2098 // Start and stop sync in order to create bookmark nodes in the sync db.
2102 // Modify the date_added field of a bookmark so it doesn't match with
2104 const BookmarkNode
* bookmark_bar_node
= model_
->bookmark_bar_node();
2105 int remove_index
= 2;
2106 ASSERT_GT(bookmark_bar_node
->child_count(), remove_index
);
2107 const BookmarkNode
* child_node
= bookmark_bar_node
->GetChild(remove_index
);
2108 ASSERT_TRUE(child_node
);
2109 EXPECT_TRUE(child_node
->is_url());
2110 model_
->SetDateAdded(child_node
, base::Time::FromInternalValue(10));
2114 // Everything should be back in sync after model association.
2115 ExpectBookmarkModelMatchesTestData();
2118 // Now trigger a change while syncing. We add a new bookmark, sync it, then
2119 // updates it's creation time.
2120 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2121 FakeServerChange
adds(&trans
);
2122 const std::string kTitle
= "Some site";
2123 const std::string kUrl
= "http://www.whatwhat.yeah/";
2124 const int kCreationTime
= 30;
2125 int64 id
= adds
.AddURL(kTitle
, kUrl
,
2126 bookmark_bar_id(), 0);
2127 adds
.ApplyPendingChanges(change_processor_
.get());
2128 FakeServerChange
updates(&trans
);
2129 updates
.ModifyCreationTime(id
, kCreationTime
);
2130 updates
.ApplyPendingChanges(change_processor_
.get());
2132 const BookmarkNode
* node
= model_
->bookmark_bar_node()->GetChild(0);
2134 EXPECT_TRUE(node
->is_url());
2135 EXPECT_EQ(base::UTF8ToUTF16(kTitle
), node
->GetTitle());
2136 EXPECT_EQ(kUrl
, node
->url().possibly_invalid_spec());
2137 EXPECT_EQ(node
->date_added(), base::Time::FromInternalValue(30));
2140 // Tests that changes to the sync nodes meta info gets reflected in the local
2142 TEST_F(ProfileSyncServiceBookmarkTestWithData
, UpdateMetaInfoFromSync
) {
2143 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2144 WriteTestDataToBookmarkModel();
2147 // Create bookmark nodes containing meta info.
2148 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2149 FakeServerChange
adds(&trans
);
2150 BookmarkNode::MetaInfoMap folder_meta_info
;
2151 folder_meta_info
["folder"] = "foldervalue";
2152 int64 folder_id
= adds
.AddFolderWithMetaInfo(
2153 "folder title", &folder_meta_info
, bookmark_bar_id(), 0);
2154 BookmarkNode::MetaInfoMap node_meta_info
;
2155 node_meta_info
["node"] = "nodevalue";
2156 node_meta_info
["other"] = "othervalue";
2157 int64 id
= adds
.AddURLWithMetaInfo("node title", "http://www.foo.com",
2158 &node_meta_info
, folder_id
, 0);
2159 adds
.ApplyPendingChanges(change_processor_
.get());
2161 // Verify that the nodes are created with the correct meta info.
2162 ASSERT_LT(0, model_
->bookmark_bar_node()->child_count());
2163 const BookmarkNode
* folder_node
= model_
->bookmark_bar_node()->GetChild(0);
2164 ASSERT_TRUE(folder_node
->GetMetaInfoMap());
2165 EXPECT_EQ(folder_meta_info
, *folder_node
->GetMetaInfoMap());
2166 ASSERT_LT(0, folder_node
->child_count());
2167 const BookmarkNode
* node
= folder_node
->GetChild(0);
2168 ASSERT_TRUE(node
->GetMetaInfoMap());
2169 EXPECT_EQ(node_meta_info
, *node
->GetMetaInfoMap());
2171 // Update meta info on nodes on server
2172 FakeServerChange
updates(&trans
);
2173 folder_meta_info
.erase("folder");
2174 updates
.ModifyMetaInfo(folder_id
, folder_meta_info
);
2175 node_meta_info
["node"] = "changednodevalue";
2176 node_meta_info
.erase("other");
2177 node_meta_info
["newkey"] = "newkeyvalue";
2178 updates
.ModifyMetaInfo(id
, node_meta_info
);
2179 updates
.ApplyPendingChanges(change_processor_
.get());
2181 // Confirm that the updated values are reflected in the bookmark nodes.
2182 EXPECT_FALSE(folder_node
->GetMetaInfoMap());
2183 ASSERT_TRUE(node
->GetMetaInfoMap());
2184 EXPECT_EQ(node_meta_info
, *node
->GetMetaInfoMap());
2187 // Tests that changes to the local bookmark nodes meta info gets reflected in
2189 TEST_F(ProfileSyncServiceBookmarkTestWithData
, UpdateMetaInfoFromModel
) {
2190 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2191 WriteTestDataToBookmarkModel();
2193 ExpectBookmarkModelMatchesTestData();
2195 const BookmarkNode
* folder_node
=
2196 model_
->AddFolder(model_
->bookmark_bar_node(), 0,
2197 base::ASCIIToUTF16("folder title"));
2198 const BookmarkNode
* node
= model_
->AddURL(folder_node
, 0,
2199 base::ASCIIToUTF16("node title"),
2200 GURL("http://www.foo.com"));
2203 // Add some meta info and verify sync model matches the changes.
2204 model_
->SetNodeMetaInfo(folder_node
, "folder", "foldervalue");
2205 model_
->SetNodeMetaInfo(node
, "node", "nodevalue");
2206 model_
->SetNodeMetaInfo(node
, "other", "othervalue");
2209 // Change/delete existing meta info and verify.
2210 model_
->DeleteNodeMetaInfo(folder_node
, "folder");
2211 model_
->SetNodeMetaInfo(node
, "node", "changednodevalue");
2212 model_
->DeleteNodeMetaInfo(node
, "other");
2213 model_
->SetNodeMetaInfo(node
, "newkey", "newkeyvalue");
2217 // Tests that node's specifics doesn't get unnecessarily overwritten (causing
2218 // a subsequent commit) when BookmarkChangeProcessor handles a notification
2219 // (such as BookmarkMetaInfoChanged) without an actual data change.
2220 TEST_F(ProfileSyncServiceBookmarkTestWithData
, MetaInfoPreservedOnNonChange
) {
2221 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2222 WriteTestDataToBookmarkModel();
2225 std::string orig_specifics
;
2227 const BookmarkNode
* bookmark
;
2229 // Create bookmark folder node containing meta info.
2231 syncer::WriteTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2232 FakeServerChange
adds(&trans
);
2234 int64 folder_id
= adds
.AddFolder("folder title", bookmark_bar_id(), 0);
2236 BookmarkNode::MetaInfoMap node_meta_info
;
2237 node_meta_info
["one"] = "1";
2238 node_meta_info
["two"] = "2";
2239 node_meta_info
["three"] = "3";
2241 sync_id
= adds
.AddURLWithMetaInfo("node title", "http://www.foo.com/",
2242 &node_meta_info
, folder_id
, 0);
2244 // Verify that the node propagates to the bookmark model
2245 adds
.ApplyPendingChanges(change_processor_
.get());
2247 bookmark
= model_
->bookmark_bar_node()->GetChild(0)->GetChild(0);
2248 EXPECT_EQ(node_meta_info
, *bookmark
->GetMetaInfoMap());
2250 syncer::ReadNode
sync_node(&trans
);
2251 EXPECT_EQ(BaseNode::INIT_OK
, sync_node
.InitByIdLookup(sync_id
));
2252 orig_specifics
= sync_node
.GetBookmarkSpecifics().SerializeAsString();
2255 // Force change processor to update the sync node.
2256 change_processor_
->BookmarkMetaInfoChanged(model_
, bookmark
);
2258 // Read bookmark specifics again and verify that there is no change.
2260 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2261 syncer::ReadNode
sync_node(&trans
);
2262 EXPECT_EQ(BaseNode::INIT_OK
, sync_node
.InitByIdLookup(sync_id
));
2263 std::string new_specifics
=
2264 sync_node
.GetBookmarkSpecifics().SerializeAsString();
2265 ASSERT_EQ(orig_specifics
, new_specifics
);
2269 void ProfileSyncServiceBookmarkTestWithData::GetTransactionVersions(
2270 const BookmarkNode
* root
,
2271 BookmarkNodeVersionMap
* node_versions
) {
2272 node_versions
->clear();
2273 std::queue
<const BookmarkNode
*> nodes
;
2275 while (!nodes
.empty()) {
2276 const BookmarkNode
* n
= nodes
.front();
2279 int64 version
= n
->sync_transaction_version();
2280 EXPECT_NE(BookmarkNode::kInvalidSyncTransactionVersion
, version
);
2282 (*node_versions
)[n
->id()] = version
;
2283 for (int i
= 0; i
< n
->child_count(); ++i
) {
2284 if (!CanSyncNode(n
->GetChild(i
)))
2286 nodes
.push(n
->GetChild(i
));
2291 void ProfileSyncServiceBookmarkTestWithData::ExpectTransactionVersionMatch(
2292 const BookmarkNode
* node
,
2293 const BookmarkNodeVersionMap
& version_expected
) {
2294 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2296 BookmarkNodeVersionMap bnodes_versions
;
2297 GetTransactionVersions(node
, &bnodes_versions
);
2298 for (BookmarkNodeVersionMap::const_iterator it
= bnodes_versions
.begin();
2299 it
!= bnodes_versions
.end(); ++it
) {
2300 syncer::ReadNode
sync_node(&trans
);
2301 ASSERT_TRUE(model_associator_
->InitSyncNodeFromChromeId(it
->first
,
2303 EXPECT_EQ(sync_node
.GetEntry()->GetTransactionVersion(), it
->second
);
2304 BookmarkNodeVersionMap::const_iterator expected_ver_it
=
2305 version_expected
.find(it
->first
);
2306 if (expected_ver_it
!= version_expected
.end())
2307 EXPECT_EQ(expected_ver_it
->second
, it
->second
);
2311 // Test transaction versions of model and nodes are incremented after changes
2313 TEST_F(ProfileSyncServiceBookmarkTestWithData
, UpdateTransactionVersion
) {
2314 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2316 WriteTestDataToBookmarkModel();
2317 base::MessageLoop::current()->RunUntilIdle();
2319 BookmarkNodeVersionMap initial_versions
;
2321 // Verify transaction versions in sync model and bookmark model (saved as
2322 // transaction version of root node) are equal after
2323 // WriteTestDataToBookmarkModel() created bookmarks.
2325 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2326 EXPECT_GT(trans
.GetModelVersion(syncer::BOOKMARKS
), 0);
2327 GetTransactionVersions(model_
->root_node(), &initial_versions
);
2328 EXPECT_EQ(trans
.GetModelVersion(syncer::BOOKMARKS
),
2329 initial_versions
[model_
->root_node()->id()]);
2331 ExpectTransactionVersionMatch(model_
->bookmark_bar_node(),
2332 BookmarkNodeVersionMap());
2333 ExpectTransactionVersionMatch(model_
->other_node(),
2334 BookmarkNodeVersionMap());
2335 ExpectTransactionVersionMatch(model_
->mobile_node(),
2336 BookmarkNodeVersionMap());
2338 // Verify model version is incremented and bookmark node versions remain
2340 const BookmarkNode
* bookmark_bar
= model_
->bookmark_bar_node();
2341 model_
->Remove(bookmark_bar
, 0);
2342 base::MessageLoop::current()->RunUntilIdle();
2343 BookmarkNodeVersionMap new_versions
;
2344 GetTransactionVersions(model_
->root_node(), &new_versions
);
2345 EXPECT_EQ(initial_versions
[model_
->root_node()->id()] + 1,
2346 new_versions
[model_
->root_node()->id()]);
2347 ExpectTransactionVersionMatch(model_
->bookmark_bar_node(), initial_versions
);
2348 ExpectTransactionVersionMatch(model_
->other_node(), initial_versions
);
2349 ExpectTransactionVersionMatch(model_
->mobile_node(), initial_versions
);
2351 // Verify model version and version of changed bookmark are incremented and
2352 // versions of others remain same.
2353 const BookmarkNode
* changed_bookmark
=
2354 model_
->bookmark_bar_node()->GetChild(0);
2355 model_
->SetTitle(changed_bookmark
, base::ASCIIToUTF16("test"));
2356 base::MessageLoop::current()->RunUntilIdle();
2357 GetTransactionVersions(model_
->root_node(), &new_versions
);
2358 EXPECT_EQ(initial_versions
[model_
->root_node()->id()] + 2,
2359 new_versions
[model_
->root_node()->id()]);
2360 EXPECT_LT(initial_versions
[changed_bookmark
->id()],
2361 new_versions
[changed_bookmark
->id()]);
2362 initial_versions
.erase(changed_bookmark
->id());
2363 ExpectTransactionVersionMatch(model_
->bookmark_bar_node(), initial_versions
);
2364 ExpectTransactionVersionMatch(model_
->other_node(), initial_versions
);
2365 ExpectTransactionVersionMatch(model_
->mobile_node(), initial_versions
);
2368 // Test that sync persistence errors are detected and trigger a failed
2370 TEST_F(ProfileSyncServiceBookmarkTestWithData
, PersistenceError
) {
2371 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2373 WriteTestDataToBookmarkModel();
2374 base::MessageLoop::current()->RunUntilIdle();
2376 BookmarkNodeVersionMap initial_versions
;
2378 // Verify transaction versions in sync model and bookmark model (saved as
2379 // transaction version of root node) are equal after
2380 // WriteTestDataToBookmarkModel() created bookmarks.
2382 syncer::ReadTransaction
trans(FROM_HERE
, test_user_share_
.user_share());
2383 EXPECT_GT(trans
.GetModelVersion(syncer::BOOKMARKS
), 0);
2384 GetTransactionVersions(model_
->root_node(), &initial_versions
);
2385 EXPECT_EQ(trans
.GetModelVersion(syncer::BOOKMARKS
),
2386 initial_versions
[model_
->root_node()->id()]);
2388 ExpectTransactionVersionMatch(model_
->bookmark_bar_node(),
2389 BookmarkNodeVersionMap());
2390 ExpectTransactionVersionMatch(model_
->other_node(),
2391 BookmarkNodeVersionMap());
2392 ExpectTransactionVersionMatch(model_
->mobile_node(),
2393 BookmarkNodeVersionMap());
2395 // Now shut down sync and artificially increment the native model's version.
2397 int64 root_version
= initial_versions
[model_
->root_node()->id()];
2398 model_
->SetNodeSyncTransactionVersion(model_
->root_node(), root_version
+ 1);
2400 // Upon association, bookmarks should fail to associate.
2401 EXPECT_FALSE(AssociateModels());
2404 // It's possible for update/add calls from the bookmark model to be out of
2405 // order, or asynchronous. Handle that without triggering an error.
2406 TEST_F(ProfileSyncServiceBookmarkTest
, UpdateThenAdd
) {
2407 LoadBookmarkModel(DELETE_EXISTING_STORAGE
, DONT_SAVE_TO_STORAGE
);
2410 EXPECT_TRUE(other_bookmarks_id());
2411 EXPECT_TRUE(bookmark_bar_id());
2412 EXPECT_TRUE(mobile_bookmarks_id());
2416 // Now destroy the change processor then add a bookmark, to simulate
2417 // missing the Update call.
2418 change_processor_
.reset();
2419 const BookmarkNode
* node
= model_
->AddURL(model_
->bookmark_bar_node(),
2421 base::ASCIIToUTF16("title"),
2422 GURL("http://www.url.com"));
2424 // Recreate the change processor then update that bookmark. Sync should
2425 // receive the update call and gracefully treat that as if it were an add.
2426 change_processor_
.reset(new BookmarkChangeProcessor(
2427 &profile_
, model_associator_
.get(), &mock_error_handler_
));
2428 change_processor_
->Start(test_user_share_
.user_share());
2429 model_
->SetTitle(node
, base::ASCIIToUTF16("title2"));
2432 // Then simulate the add call arriving late.
2433 change_processor_
->BookmarkNodeAdded(model_
, model_
->bookmark_bar_node(), 0);
2439 } // namespace browser_sync