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