Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: chrome/browser/sync/profile_sync_service_unittest.cc

Issue 160542: Rolling back 22317 (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/trunk/src/
Patch Set: Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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 #ifdef CHROME_PERSONALIZATION
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 #include <stack>
10 #include <vector>
11
12 #include "base/command_line.h"
13 #include "base/string_util.h"
14 #include "base/string16.h"
15 #include "chrome/browser/bookmarks/bookmark_model.h"
16 #include "chrome/browser/profile.h"
17 #include "chrome/common/pref_service.h"
18 #include "chrome/browser/sync/engine/syncapi.h"
19 #include "chrome/browser/sync/glue/model_associator.h"
20 #include "chrome/browser/sync/profile_sync_service.h"
21 #include "chrome/test/testing_profile.h"
22
23 using std::vector;
24 using browser_sync::ModelAssociator;
25 using browser_sync::SyncBackendHost;
26
27 class TestModelAssociator : public ModelAssociator {
28 public:
29 TestModelAssociator(ProfileSyncService* service)
30 : ModelAssociator(service) {
31 }
32
33 virtual bool GetSyncIdForTaggedNode(const string16& tag, int64* sync_id) {
34 sync_api::WriteTransaction trans(
35 sync_service()->backend()->GetUserShareHandle());
36 sync_api::ReadNode root(&trans);
37 root.InitByRootLookup();
38
39 // First, try to find a node with the title among the root's children.
40 // This will be the case if we are testing model persistence, and
41 // are reloading a sync repository created earlier in the test.
42 for (int64 id = root.GetFirstChildId(); id != sync_api::kInvalidId; /***/) {
43 sync_api::ReadNode child(&trans);
44 if (!child.InitByIdLookup(id)) {
45 NOTREACHED();
46 break;
47 }
48 if (tag == child.GetTitle()) {
49 *sync_id = id;
50 return true;
51 }
52 id = child.GetSuccessorId();
53 }
54
55 sync_api::WriteNode node(&trans);
56 if (!node.InitByCreation(root, NULL))
57 return false;
58 node.SetIsFolder(true);
59 node.SetTitle(tag.c_str());
60 node.SetExternalId(0);
61 *sync_id = node.GetId();
62 return true;
63 }
64 };
65
66 class TestProfileSyncService : public ProfileSyncService {
67 public:
68 TestProfileSyncService(Profile* profile) : ProfileSyncService(profile) {
69 PrefService* pref_service = profile->GetPrefs();
70 if (pref_service->IsPrefRegistered(prefs::kSyncUserName))
71 return;
72 pref_service->RegisterStringPref(prefs::kSyncUserName, string16());
73 pref_service->RegisterStringPref(prefs::kSyncLastSyncedTime, string16());
74 pref_service->RegisterBooleanPref(prefs::kSyncHasSetupCompleted, true);
75 }
76 virtual ~TestProfileSyncService() {
77 }
78
79 virtual void InitializeBackend() {
80 set_model_associator(new TestModelAssociator(this));
81 backend()->InitializeForTestMode(L"testuser");
82 // The SyncBackend posts a task to the current loop when initialization
83 // completes.
84 MessageLoop::current()->Run();
85 // Initialization is synchronous for test mode, so we should be good to go.
86 DCHECK(sync_initialized());
87 }
88
89 virtual void OnBackendInitialized() {
90 ProfileSyncService::OnBackendInitialized();
91 MessageLoop::current()->Quit();
92 }
93
94 virtual bool MergeAndSyncAcceptanceNeeded() {
95 // Never show the dialog.
96 return false;
97 }
98 };
99
100 // FakeServerChange constructs a list of sync_api::ChangeRecords while modifying
101 // the sync model, and can pass the ChangeRecord list to a
102 // sync_api::SyncObserver (i.e., the ProfileSyncService) to test the client
103 // change-application behavior.
104 // Tests using FakeServerChange should be careful to avoid back-references,
105 // since FakeServerChange will send the edits in the order specified.
106 class FakeServerChange {
107 public:
108 explicit FakeServerChange(sync_api::WriteTransaction* trans) : trans_(trans) {
109 }
110
111 // Pretend that the server told the syncer to add a bookmark object.
112 int64 Add(const string16& title,
113 const string16& url,
114 bool is_folder,
115 int64 parent_id,
116 int64 predecessor_id) {
117 sync_api::ReadNode parent(trans_);
118 EXPECT_TRUE(parent.InitByIdLookup(parent_id));
119 sync_api::WriteNode node(trans_);
120 if (predecessor_id == 0) {
121 EXPECT_TRUE(node.InitByCreation(parent, NULL));
122 } else {
123 sync_api::ReadNode predecessor(trans_);
124 EXPECT_TRUE(predecessor.InitByIdLookup(predecessor_id));
125 EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
126 EXPECT_TRUE(node.InitByCreation(parent, &predecessor));
127 }
128 EXPECT_EQ(node.GetPredecessorId(), predecessor_id);
129 EXPECT_EQ(node.GetParentId(), parent_id);
130 node.SetIsFolder(is_folder);
131 node.SetTitle(title.c_str());
132 if (!is_folder) {
133 GURL gurl(url);
134 node.SetURL(url.c_str());
135 }
136 sync_api::SyncManager::ChangeRecord record;
137 record.action = sync_api::SyncManager::ChangeRecord::ACTION_ADD;
138 record.id = node.GetId();
139 changes_.push_back(record);
140 return node.GetId();
141 }
142
143 // Add a bookmark folder.
144 int64 AddFolder(const string16& title,
145 int64 parent_id,
146 int64 predecessor_id) {
147 return Add(title, string16(), true, parent_id, predecessor_id);
148 }
149
150 // Add a bookmark.
151 int64 AddURL(const string16& title,
152 const string16& url,
153 int64 parent_id,
154 int64 predecessor_id) {
155 return Add(title, url, false, parent_id, predecessor_id);
156 }
157
158 // Pretend that the server told the syncer to delete an object.
159 void Delete(int64 id) {
160 {
161 // Delete the sync node.
162 sync_api::WriteNode node(trans_);
163 EXPECT_TRUE(node.InitByIdLookup(id));
164 EXPECT_FALSE(node.GetFirstChildId());
165 node.Remove();
166 }
167 {
168 // Verify the deletion.
169 sync_api::ReadNode node(trans_);
170 EXPECT_FALSE(node.InitByIdLookup(id));
171 }
172
173 sync_api::SyncManager::ChangeRecord record;
174 record.action = sync_api::SyncManager::ChangeRecord::ACTION_DELETE;
175 record.id = id;
176 // Deletions are always first in the changelist, but we can't actually do
177 // WriteNode::Remove() on the node until its children are moved. So, as
178 // a practical matter, users of FakeServerChange must move or delete
179 // children before parents. Thus, we must insert the deletion record
180 // at the front of the vector.
181 changes_.insert(changes_.begin(), record);
182 }
183
184 // Set a new title value, and return the old value.
185 string16 ModifyTitle(int64 id, const string16& new_title) {
186 sync_api::WriteNode node(trans_);
187 EXPECT_TRUE(node.InitByIdLookup(id));
188 string16 old_title = node.GetTitle();
189 node.SetTitle(new_title.c_str());
190 SetModified(id);
191 return old_title;
192 }
193
194 // Set a new URL value, and return the old value.
195 // TODO(ncarter): Determine if URL modifications are even legal.
196 string16 ModifyURL(int64 id, const string16& new_url) {
197 sync_api::WriteNode node(trans_);
198 EXPECT_TRUE(node.InitByIdLookup(id));
199 EXPECT_FALSE(node.GetIsFolder());
200 string16 old_url = node.GetURL();
201 node.SetURL(new_url.c_str());
202 SetModified(id);
203 return old_url;
204 }
205
206 // Set a new parent and predecessor value. Return the old parent id.
207 // We could return the old predecessor id, but it turns out not to be
208 // very useful for assertions.
209 int64 ModifyPosition(int64 id, int64 parent_id, int64 predecessor_id) {
210 sync_api::ReadNode parent(trans_);
211 EXPECT_TRUE(parent.InitByIdLookup(parent_id));
212 sync_api::WriteNode node(trans_);
213 EXPECT_TRUE(node.InitByIdLookup(id));
214 int64 old_parent_id = node.GetParentId();
215 if (predecessor_id == 0) {
216 EXPECT_TRUE(node.SetPosition(parent, NULL));
217 } else {
218 sync_api::ReadNode predecessor(trans_);
219 EXPECT_TRUE(predecessor.InitByIdLookup(predecessor_id));
220 EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
221 EXPECT_TRUE(node.SetPosition(parent, &predecessor));
222 }
223 SetModified(id);
224 return old_parent_id;
225 }
226
227 // Pass the fake change list to |service|.
228 void ApplyPendingChanges(ProfileSyncService* service) {
229 service->ApplyModelChanges(trans_, changes_.size() ? &changes_[0] : NULL,
230 changes_.size());
231 }
232
233 const vector<sync_api::SyncManager::ChangeRecord>& changes() {
234 return changes_;
235 }
236
237 private:
238 // Helper function to push an ACTION_UPDATE record onto the back
239 // of the changelist.
240 void SetModified(int64 id) {
241 // Coalesce multi-property edits.
242 if (changes_.size() > 0 && changes_.back().id == id &&
243 changes_.back().action ==
244 sync_api::SyncManager::ChangeRecord::ACTION_UPDATE)
245 return;
246 sync_api::SyncManager::ChangeRecord record;
247 record.action = sync_api::SyncManager::ChangeRecord::ACTION_UPDATE;
248 record.id = id;
249 changes_.push_back(record);
250 }
251
252 // The transaction on which everything happens.
253 sync_api::WriteTransaction *trans_;
254
255 // The change list we construct.
256 vector<sync_api::SyncManager::ChangeRecord> changes_;
257 };
258
259 class ProfileSyncServiceTest : public testing::Test {
260 protected:
261 enum LoadOption { LOAD_FROM_STORAGE, DELETE_EXISTING_STORAGE };
262 enum SaveOption { SAVE_TO_STORAGE, DONT_SAVE_TO_STORAGE };
263 ProfileSyncServiceTest() : model_(NULL) {
264 profile_.reset(new TestingProfile());
265 profile_->set_has_history_service(true);
266 }
267 virtual ~ProfileSyncServiceTest() {
268 // Kill the service before the profile.
269 service_.reset();
270 profile_.reset();
271 }
272
273 ModelAssociator* associator() {
274 DCHECK(service_.get());
275 return service_->model_associator_;
276 }
277
278 void StartSyncService() {
279 if (!service_.get()) {
280 service_.reset(new TestProfileSyncService(profile_.get()));
281 service_->Initialize();
282 }
283 // The service may have already started sync automatically if it's already
284 // enabled by user once.
285 if (!service_->IsSyncEnabledByUser())
286 service_->EnableForUser();
287 }
288 void StopSyncService(SaveOption save) {
289 if (save == DONT_SAVE_TO_STORAGE)
290 service_->DisableForUser();
291 service_.reset();
292 }
293
294 // Load (or re-load) the bookmark model. |load| controls use of the
295 // bookmarks file on disk. |save| controls whether the newly loaded
296 // bookmark model will write out a bookmark file as it goes.
297 void LoadBookmarkModel(LoadOption load, SaveOption save) {
298 bool delete_bookmarks = load == DELETE_EXISTING_STORAGE;
299 profile_->CreateBookmarkModel(delete_bookmarks);
300 model_ = profile_->GetBookmarkModel();
301 // Wait for the bookmarks model to load.
302 profile_->BlockUntilBookmarkModelLoaded();
303 // This noticeably speeds up the unit tests that request it.
304 if (save == DONT_SAVE_TO_STORAGE)
305 model_->ClearStore();
306 }
307
308 void ExpectSyncerNodeMatching(sync_api::BaseTransaction* trans,
309 const BookmarkNode* bnode) {
310 sync_api::ReadNode gnode(trans);
311 EXPECT_TRUE(associator()->InitSyncNodeFromBookmarkId(bnode->id(), &gnode));
312 // Non-root node titles and parents must match.
313 if (bnode != model_->GetBookmarkBarNode() &&
314 bnode != model_->other_node()) {
315 EXPECT_EQ(bnode->GetTitle(), gnode.GetTitle());
316 EXPECT_EQ(associator()->GetBookmarkNodeFromSyncId(gnode.GetParentId()),
317 bnode->GetParent());
318 }
319 EXPECT_EQ(bnode->is_folder(), gnode.GetIsFolder());
320 if (bnode->is_url())
321 EXPECT_EQ(bnode->GetURL(), GURL(gnode.GetURL()));
322
323 // Check for position matches.
324 int browser_index = bnode->GetParent()->IndexOfChild(bnode);
325 if (browser_index == 0) {
326 EXPECT_EQ(gnode.GetPredecessorId(), 0);
327 } else {
328 const BookmarkNode* bprev =
329 bnode->GetParent()->GetChild(browser_index - 1);
330 sync_api::ReadNode gprev(trans);
331 ASSERT_TRUE(associator()->InitSyncNodeFromBookmarkId(bprev->id(),
332 &gprev));
333 EXPECT_EQ(gnode.GetPredecessorId(), gprev.GetId());
334 EXPECT_EQ(gnode.GetParentId(), gprev.GetParentId());
335 }
336 if (browser_index == bnode->GetParent()->GetChildCount() - 1) {
337 EXPECT_EQ(gnode.GetSuccessorId(), 0);
338 } else {
339 const BookmarkNode* bnext =
340 bnode->GetParent()->GetChild(browser_index + 1);
341 sync_api::ReadNode gnext(trans);
342 ASSERT_TRUE(associator()->InitSyncNodeFromBookmarkId(bnext->id(),
343 &gnext));
344 EXPECT_EQ(gnode.GetSuccessorId(), gnext.GetId());
345 EXPECT_EQ(gnode.GetParentId(), gnext.GetParentId());
346 }
347 if (bnode->GetChildCount()) {
348 EXPECT_TRUE(gnode.GetFirstChildId());
349 }
350 }
351
352 void ExpectSyncerNodeMatching(const BookmarkNode* bnode) {
353 sync_api::ReadTransaction trans(service_->backend_->GetUserShareHandle());
354 ExpectSyncerNodeMatching(&trans, bnode);
355 }
356
357 void ExpectBrowserNodeMatching(sync_api::BaseTransaction* trans,
358 int64 sync_id) {
359 EXPECT_TRUE(sync_id);
360 const BookmarkNode* bnode =
361 associator()->GetBookmarkNodeFromSyncId(sync_id);
362 ASSERT_TRUE(bnode);
363 int64 id = associator()->GetSyncIdFromBookmarkId(bnode->id());
364 EXPECT_EQ(id, sync_id);
365 ExpectSyncerNodeMatching(trans, bnode);
366 }
367
368 void ExpectBrowserNodeUnknown(int64 sync_id) {
369 EXPECT_FALSE(associator()->GetBookmarkNodeFromSyncId(sync_id));
370 }
371
372 void ExpectBrowserNodeKnown(int64 sync_id) {
373 EXPECT_TRUE(associator()->GetBookmarkNodeFromSyncId(sync_id));
374 }
375
376 void ExpectSyncerNodeKnown(const BookmarkNode* node) {
377 int64 sync_id = associator()->GetSyncIdFromBookmarkId(node->id());
378 EXPECT_NE(sync_id, sync_api::kInvalidId);
379 }
380
381 void ExpectSyncerNodeUnknown(const BookmarkNode* node) {
382 int64 sync_id = associator()->GetSyncIdFromBookmarkId(node->id());
383 EXPECT_EQ(sync_id, sync_api::kInvalidId);
384 }
385
386 void ExpectBrowserNodeTitle(int64 sync_id, const string16& title) {
387 const BookmarkNode* bnode =
388 associator()->GetBookmarkNodeFromSyncId(sync_id);
389 ASSERT_TRUE(bnode);
390 EXPECT_EQ(bnode->GetTitle(), title);
391 }
392
393 void ExpectBrowserNodeURL(int64 sync_id, const string16& url) {
394 const BookmarkNode* bnode =
395 associator()->GetBookmarkNodeFromSyncId(sync_id);
396 ASSERT_TRUE(bnode);
397 GURL url2(url);
398 EXPECT_EQ(url2, bnode->GetURL());
399 }
400
401 void ExpectBrowserNodeParent(int64 sync_id, int64 parent_sync_id) {
402 const BookmarkNode* node = associator()->GetBookmarkNodeFromSyncId(sync_id);
403 ASSERT_TRUE(node);
404 const BookmarkNode* parent =
405 associator()->GetBookmarkNodeFromSyncId(parent_sync_id);
406 EXPECT_TRUE(parent);
407 EXPECT_EQ(node->GetParent(), parent);
408 }
409
410 void ExpectModelMatch(sync_api::BaseTransaction* trans) {
411 const BookmarkNode* root = model_->root_node();
412 EXPECT_EQ(root->IndexOfChild(model_->GetBookmarkBarNode()), 0);
413 EXPECT_EQ(root->IndexOfChild(model_->other_node()), 1);
414
415 std::stack<int64> stack;
416 stack.push(bookmark_bar_id());
417 while (!stack.empty()) {
418 int64 id = stack.top();
419 stack.pop();
420 if (!id) continue;
421
422 ExpectBrowserNodeMatching(trans, id);
423
424 sync_api::ReadNode gnode(trans);
425 ASSERT_TRUE(gnode.InitByIdLookup(id));
426 stack.push(gnode.GetFirstChildId());
427 stack.push(gnode.GetSuccessorId());
428 }
429 }
430
431 void ExpectModelMatch() {
432 sync_api::ReadTransaction trans(service_->backend_->GetUserShareHandle());
433 ExpectModelMatch(&trans);
434 }
435
436 int64 other_bookmarks_id() {
437 return associator()->GetSyncIdFromBookmarkId(model_->other_node()->id());
438 }
439
440 int64 bookmark_bar_id() {
441 return associator()->GetSyncIdFromBookmarkId(
442 model_->GetBookmarkBarNode()->id());
443 }
444
445 SyncBackendHost* backend() { return service_->backend_.get(); }
446
447 // This serves as the "UI loop" on which the ProfileSyncService lives and
448 // operates. It is needed because the SyncBackend can post tasks back to
449 // the service, meaning it can't be null. It doesn't have to be running,
450 // though -- OnInitializationCompleted is the only example (so far) in this
451 // test where we need to Run the loop to swallow a task and then quit, to
452 // avoid leaking the ProfileSyncService (the PostTask will retain the callee
453 // and caller until the task is run).
454 MessageLoop message_loop_;
455
456 scoped_ptr<ProfileSyncService> service_;
457 scoped_ptr<TestingProfile> profile_;
458 BookmarkModel* model_;
459 };
460
461 TEST_F(ProfileSyncServiceTest, InitialState) {
462 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
463 StartSyncService();
464
465 EXPECT_TRUE(other_bookmarks_id());
466 EXPECT_TRUE(bookmark_bar_id());
467
468 ExpectModelMatch();
469 }
470
471 TEST_F(ProfileSyncServiceTest, BookmarkModelOperations) {
472 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
473 StartSyncService();
474
475 // Test addition.
476 const BookmarkNode* folder =
477 model_->AddGroup(model_->other_node(), 0, L"foobar");
478 ExpectSyncerNodeMatching(folder);
479 ExpectModelMatch();
480 const BookmarkNode* folder2 = model_->AddGroup(folder, 0, L"nested");
481 ExpectSyncerNodeMatching(folder2);
482 ExpectModelMatch();
483 const BookmarkNode* url1 = model_->AddURL(
484 folder, 0, L"Internets #1 Pies Site", GURL(L"http://www.easypie.com/"));
485 ExpectSyncerNodeMatching(url1);
486 ExpectModelMatch();
487 const BookmarkNode* url2 = model_->AddURL(
488 folder, 1, L"Airplanes", GURL(L"http://www.easyjet.com/"));
489 ExpectSyncerNodeMatching(url2);
490 ExpectModelMatch();
491
492 // Test modification.
493 model_->SetTitle(url2, L"EasyJet");
494 ExpectModelMatch();
495 model_->Move(url1, folder2, 0);
496 ExpectModelMatch();
497 model_->Move(folder2, model_->GetBookmarkBarNode(), 0);
498 ExpectModelMatch();
499 model_->SetTitle(folder2, L"Not Nested");
500 ExpectModelMatch();
501 model_->Move(folder, folder2, 0);
502 ExpectModelMatch();
503 model_->SetTitle(folder, L"who's nested now?");
504 ExpectModelMatch();
505
506 // Test deletion.
507 // Delete a single item.
508 model_->Remove(url2->GetParent(), url2->GetParent()->IndexOfChild(url2));
509 ExpectModelMatch();
510 // Delete an item with several children.
511 model_->Remove(folder2->GetParent(),
512 folder2->GetParent()->IndexOfChild(folder2));
513 ExpectModelMatch();
514 }
515
516 TEST_F(ProfileSyncServiceTest, ServerChangeProcessing) {
517 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
518 StartSyncService();
519
520 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
521
522 FakeServerChange adds(&trans);
523 int64 f1 = adds.AddFolder(L"Server Folder B", bookmark_bar_id(), 0);
524 int64 f2 = adds.AddFolder(L"Server Folder A", bookmark_bar_id(), f1);
525 int64 u1 = adds.AddURL(L"Some old site", L"ftp://nifty.andrew.cmu.edu/",
526 bookmark_bar_id(), f2);
527 int64 u2 = adds.AddURL(L"Nifty", L"ftp://nifty.andrew.cmu.edu/", f1, 0);
528 // u3 is a duplicate URL
529 int64 u3 = adds.AddURL(L"Nifty2", L"ftp://nifty.andrew.cmu.edu/", f1, u2);
530 // u4 is a duplicate title, different URL.
531 int64 u4 = adds.AddURL(L"Some old site", L"http://slog.thestranger.com/",
532 bookmark_bar_id(), u1);
533 // u5 tests an empty-string title.
534 string16 javascript_url(L"javascript:(function(){var w=window.open(" \
535 L"'about:blank','gnotesWin','location=0,menubar=0," \
536 L"scrollbars=0,status=0,toolbar=0,width=300," \
537 L"height=300,resizable');});");
538 int64 u5 = adds.AddURL(L"", javascript_url, other_bookmarks_id(), 0);
539
540 vector<sync_api::SyncManager::ChangeRecord>::const_iterator it;
541 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
542 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
543 ExpectBrowserNodeUnknown(it->id);
544
545 adds.ApplyPendingChanges(service_.get());
546
547 // Make sure the bookmark model received all of the nodes in |adds|.
548 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
549 ExpectBrowserNodeMatching(&trans, it->id);
550 ExpectModelMatch(&trans);
551
552 // Part two: test modifications.
553 FakeServerChange mods(&trans);
554 // Mess with u2, and move it into empty folder f2
555 // TODO(ncarter): Determine if we allow ModifyURL ops or not.
556 /* string16 u2_old_url = mods.ModifyURL(u2, L"http://www.google.com"); */
557 string16 u2_old_title = mods.ModifyTitle(u2, L"The Google");
558 int64 u2_old_parent = mods.ModifyPosition(u2, f2, 0);
559
560 // Now move f1 after u2.
561 string16 f1_old_title = mods.ModifyTitle(f1, L"Server Folder C");
562 int64 f1_old_parent = mods.ModifyPosition(f1, f2, u2);
563
564 // Then add u3 after f1.
565 int64 u3_old_parent = mods.ModifyPosition(u3, f2, f1);
566
567 // Test that the property changes have not yet taken effect.
568 ExpectBrowserNodeTitle(u2, u2_old_title);
569 /* ExpectBrowserNodeURL(u2, u2_old_url); */
570 ExpectBrowserNodeParent(u2, u2_old_parent);
571
572 ExpectBrowserNodeTitle(f1, f1_old_title);
573 ExpectBrowserNodeParent(f1, f1_old_parent);
574
575 ExpectBrowserNodeParent(u3, u3_old_parent);
576
577 // Apply the changes.
578 mods.ApplyPendingChanges(service_.get());
579
580 // Check for successful application.
581 for (it = mods.changes().begin(); it != mods.changes().end(); ++it)
582 ExpectBrowserNodeMatching(&trans, it->id);
583 ExpectModelMatch(&trans);
584
585 // Part 3: Test URL deletion.
586 FakeServerChange dels(&trans);
587 dels.Delete(u2);
588 dels.Delete(u3);
589
590 ExpectBrowserNodeKnown(u2);
591 ExpectBrowserNodeKnown(u3);
592
593 dels.ApplyPendingChanges(service_.get());
594
595 ExpectBrowserNodeUnknown(u2);
596 ExpectBrowserNodeUnknown(u3);
597 ExpectModelMatch(&trans);
598 }
599
600 // Tests a specific case in ApplyModelChanges where we move the
601 // children out from under a parent, and then delete the parent
602 // in the same changelist. The delete shows up first in the changelist,
603 // requiring the children to be moved to a temporary location.
604 TEST_F(ProfileSyncServiceTest, ServerChangeRequiringFosterParent) {
605 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
606 StartSyncService();
607
608 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
609
610 // Stress the immediate children of other_node because that's where
611 // ApplyModelChanges puts a temporary foster parent node.
612 string16 url(L"http://dev.chromium.org/");
613 FakeServerChange adds(&trans);
614 int64 f0 = other_bookmarks_id(); // + other_node
615 int64 f1 = adds.AddFolder(L"f1", f0, 0); // + f1
616 int64 f2 = adds.AddFolder(L"f2", f1, 0); // + f2
617 int64 u3 = adds.AddURL( L"u3", url, f2, 0); // + u3
618 int64 u4 = adds.AddURL( L"u4", url, f2, u3); // + u4
619 int64 u5 = adds.AddURL( L"u5", url, f1, f2); // + u5
620 int64 f6 = adds.AddFolder(L"f6", f1, u5); // + f6
621 int64 u7 = adds.AddURL( L"u7", url, f0, f1); // + u7
622
623 vector<sync_api::SyncManager::ChangeRecord>::const_iterator it;
624 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
625 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
626 ExpectBrowserNodeUnknown(it->id);
627
628 adds.ApplyPendingChanges(service_.get());
629
630 // Make sure the bookmark model received all of the nodes in |adds|.
631 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
632 ExpectBrowserNodeMatching(&trans, it->id);
633 ExpectModelMatch(&trans);
634
635 // We have to do the moves before the deletions, but FakeServerChange will
636 // put the deletion at the front of the changelist.
637 FakeServerChange ops(&trans);
638 ops.ModifyPosition(f6, other_bookmarks_id(), 0);
639 ops.ModifyPosition(u3, other_bookmarks_id(), f1); // Prev == f1 is OK here.
640 ops.ModifyPosition(f2, other_bookmarks_id(), u7);
641 ops.ModifyPosition(u7, f2, 0);
642 ops.ModifyPosition(u4, other_bookmarks_id(), f2);
643 ops.ModifyPosition(u5, f6, 0);
644 ops.Delete(f1);
645
646 ops.ApplyPendingChanges(service_.get());
647
648 ExpectModelMatch(&trans);
649 }
650
651 // Simulate a server change record containing a valid but non-canonical URL.
652 TEST_F(ProfileSyncServiceTest, ServerChangeWithNonCanonicalURL) {
653 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
654 StartSyncService();
655
656 {
657 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
658
659 FakeServerChange adds(&trans);
660 std::string url("http://dev.chromium.org");
661 EXPECT_NE(GURL(url).spec(), url);
662 int64 u1 = adds.AddURL(L"u1", UTF8ToWide(url), other_bookmarks_id(), 0);
663
664 adds.ApplyPendingChanges(service_.get());
665
666 EXPECT_TRUE(model_->other_node()->GetChildCount() == 1);
667 ExpectModelMatch(&trans);
668 }
669
670 // Now reboot the sync service, forcing a merge step.
671 StopSyncService(SAVE_TO_STORAGE);
672 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
673 StartSyncService();
674
675 // There should still be just the one bookmark.
676 EXPECT_TRUE(model_->other_node()->GetChildCount() == 1);
677 ExpectModelMatch();
678 }
679
680 // Simulate a server change record containing an invalid URL (per GURL).
681 // TODO(ncarter): Disabled due to crashes. Fix bug 1677563.
682 TEST_F(ProfileSyncServiceTest, DISABLED_ServerChangeWithInvalidURL) {
683 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
684 StartSyncService();
685
686 int child_count = 0;
687 {
688 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
689
690 FakeServerChange adds(&trans);
691 EXPECT_FALSE(GURL("x").is_valid());
692 int64 u1 = adds.AddURL(L"u1", L"x", other_bookmarks_id(), 0);
693
694 adds.ApplyPendingChanges(service_.get());
695
696 // We're lenient about what should happen -- the model could wind up with
697 // the node or without it; but things should be consistent, and we
698 // shouldn't crash.
699 child_count = model_->other_node()->GetChildCount();
700 EXPECT_TRUE(child_count == 0 || child_count == 1);
701 ExpectModelMatch(&trans);
702 }
703
704 // Now reboot the sync service, forcing a merge step.
705 StopSyncService(SAVE_TO_STORAGE);
706 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
707 StartSyncService();
708
709 // Things ought not to have changed.
710 EXPECT_EQ(model_->other_node()->GetChildCount(), child_count);
711 ExpectModelMatch();
712 }
713
714 // Test strings that might pose a problem if the titles ever became used as
715 // file names in the sync backend.
716 TEST_F(ProfileSyncServiceTest, CornerCaseNames) {
717 // TODO(ncarter): Bug 1570238 explains the failure of this test.
718 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
719 StartSyncService();
720
721 char16* names[] = {
722 // The empty string.
723 L"",
724 // Illegal Windows filenames.
725 L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3", L"COM4",
726 L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"LPT1", L"LPT2", L"LPT3",
727 L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9",
728 // Current/parent directory markers.
729 L".", L"..", L"...",
730 // Files created automatically by the Windows shell.
731 L"Thumbs.db", L".DS_Store",
732 // Names including Win32-illegal characters, and path separators.
733 L"foo/bar", L"foo\\bar", L"foo?bar", L"foo:bar", L"foo|bar", L"foo\"bar",
734 L"foo'bar", L"foo<bar", L"foo>bar", L"foo%bar", L"foo*bar", L"foo]bar",
735 L"foo[bar",
736 };
737 // Create both folders and bookmarks using each name.
738 GURL url("http://www.doublemint.com");
739 for (int i = 0; i < arraysize(names); ++i) {
740 model_->AddGroup(model_->other_node(), 0, names[i]);
741 model_->AddURL(model_->other_node(), 0, names[i], url);
742 }
743
744 // Verify that the browser model matches the sync model.
745 EXPECT_EQ(model_->other_node()->GetChildCount(), 2*arraysize(names));
746 ExpectModelMatch();
747 }
748
749 // Stress the internal representation of position by sparse numbers. We want
750 // to repeatedly bisect the range of available positions, to force the
751 // syncer code to renumber its ranges. Pick a number big enough so that it
752 // would exhaust 32bits of room between items a couple of times.
753 TEST_F(ProfileSyncServiceTest, RepeatedMiddleInsertion) {
754 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
755 StartSyncService();
756
757 static const int kTimesToInsert = 256;
758
759 // Create two book-end nodes to insert between.
760 model_->AddGroup(model_->other_node(), 0, L"Alpha");
761 model_->AddGroup(model_->other_node(), 1, L"Omega");
762 int count = 2;
763
764 // Test insertion in first half of range by repeatedly inserting in second
765 // position.
766 for (int i = 0; i < kTimesToInsert; ++i) {
767 string16 title = string16(L"Pre-insertion ") + IntToWString(i);
768 model_->AddGroup(model_->other_node(), 1, title);
769 count++;
770 }
771
772 // Test insertion in second half of range by repeatedly inserting in
773 // second-to-last position.
774 for (int i = 0; i < kTimesToInsert; ++i) {
775 string16 title = string16(L"Post-insertion ") + IntToWString(i);
776 model_->AddGroup(model_->other_node(), count - 1, title);
777 count++;
778 }
779
780 // Verify that the browser model matches the sync model.
781 EXPECT_EQ(model_->other_node()->GetChildCount(), count);
782 ExpectModelMatch();
783 }
784
785 // Introduce a consistency violation into the model, and see that it
786 // puts itself into a lame, error state.
787 TEST_F(ProfileSyncServiceTest, UnrecoverableErrorSuspendsService) {
788 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
789 StartSyncService();
790
791 // Synchronization should be up and running at this point.
792 EXPECT_TRUE(service_->ShouldPushChanges());
793
794 // Add a node which will be the target of the consistency violation.
795 const BookmarkNode* node =
796 model_->AddGroup(model_->other_node(), 0, L"node");
797 ExpectSyncerNodeMatching(node);
798
799 // Now destroy the syncer node as if we were the ProfileSyncService without
800 // updating the ProfileSyncService state. This should introduce
801 // inconsistency between the two models.
802 {
803 sync_api::WriteTransaction trans(service_->backend_->GetUserShareHandle());
804 sync_api::WriteNode sync_node(&trans);
805 EXPECT_TRUE(associator()->InitSyncNodeFromBookmarkId(node->id(),
806 &sync_node));
807 sync_node.Remove();
808 }
809 // The models don't match at this point, but the ProfileSyncService
810 // doesn't know it yet.
811 ExpectSyncerNodeKnown(node);
812 EXPECT_TRUE(service_->ShouldPushChanges());
813
814 // Add a child to the inconsistent node. This should cause detection of the
815 // problem.
816 const BookmarkNode* nested = model_->AddGroup(node, 0, L"nested");
817 EXPECT_FALSE(service_->ShouldPushChanges());
818 ExpectSyncerNodeUnknown(nested);
819
820 // Try to add a node under a totally different parent. This should also
821 // fail -- the ProfileSyncService should stop processing changes after
822 // encountering a consistency violation.
823 const BookmarkNode* unrelated = model_->AddGroup(
824 model_->GetBookmarkBarNode(), 0, L"unrelated");
825 EXPECT_FALSE(service_->ShouldPushChanges());
826 ExpectSyncerNodeUnknown(unrelated);
827
828 // TODO(ncarter): We ought to test the ProfileSyncService state machine
829 // directly here once that's formalized and exposed.
830 }
831
832 struct TestData {
833 const char16* title;
834 const char16* url;
835 };
836
837 // TODO(ncarter): Integrate the existing TestNode/PopulateNodeFromString code
838 // in the bookmark model unittest, to make it simpler to set up test data
839 // here (and reduce the amount of duplication among tests), and to reduce the
840 // duplication.
841 class ProfileSyncServiceTestWithData : public ProfileSyncServiceTest {
842 protected:
843 // Populates or compares children of the given bookmark node from/with the
844 // given test data array with the given size.
845 void PopulateFromTestData(const BookmarkNode* node,
846 const TestData* data,
847 int size);
848 void CompareWithTestData(const BookmarkNode* node,
849 const TestData* data,
850 int size);
851
852 void ExpectBookmarkModelMatchesTestData();
853 void WriteTestDataToBookmarkModel();
854 };
855
856 namespace {
857
858 // Constants for bookmark model that looks like:
859 // |-- Bookmark bar
860 // | |-- u2, http://www.u2.com/
861 // | |-- f1
862 // | | |-- f1u4, http://www.f1u4.com/
863 // | | |-- f1u2, http://www.f1u2.com/
864 // | | |-- f1u3, http://www.f1u3.com/
865 // | | +-- f1u1, http://www.f1u1.com/
866 // | |-- u1, http://www.u1.com/
867 // | +-- f2
868 // | |-- f2u2, http://www.f2u2.com/
869 // | |-- f2u4, http://www.f2u4.com/
870 // | |-- f2u3, http://www.f2u3.com/
871 // | +-- f2u1, http://www.f2u1.com/
872 // +-- Other bookmarks
873 // |-- f3
874 // | |-- f3u4, http://www.f3u4.com/
875 // | |-- f3u2, http://www.f3u2.com/
876 // | |-- f3u3, http://www.f3u3.com/
877 // | +-- f3u1, http://www.f3u1.com/
878 // |-- u4, http://www.u4.com/
879 // |-- u3, http://www.u3.com/
880 // --- f4
881 // | |-- f4u1, http://www.f4u1.com/
882 // | |-- f4u2, http://www.f4u2.com/
883 // | |-- f4u3, http://www.f4u3.com/
884 // | +-- f4u4, http://www.f4u4.com/
885 // |-- dup
886 // | +-- dupu1, http://www.dupu1.com/
887 // +-- dup
888 // +-- dupu2, http://www.dupu1.com/
889 //
890 static TestData kBookmarkBarChildren[] = {
891 { L"u2", L"http://www.u2.com/" },
892 { L"f1", NULL },
893 { L"u1", L"http://www.u1.com/" },
894 { L"f2", NULL },
895 };
896 static TestData kF1Children[] = {
897 { L"f1u4", L"http://www.f1u4.com/" },
898 { L"f1u2", L"http://www.f1u2.com/" },
899 { L"f1u3", L"http://www.f1u3.com/" },
900 { L"f1u1", L"http://www.f1u1.com/" },
901 };
902 static TestData kF2Children[] = {
903 { L"f2u2", L"http://www.f2u2.com/" },
904 { L"f2u4", L"http://www.f2u4.com/" },
905 { L"f2u3", L"http://www.f2u3.com/" },
906 { L"f2u1", L"http://www.f2u1.com/" },
907 };
908
909 static TestData kOtherBookmarksChildren[] = {
910 { L"f3", NULL },
911 { L"u4", L"http://www.u4.com/" },
912 { L"u3", L"http://www.u3.com/" },
913 { L"f4", NULL },
914 { L"dup", NULL },
915 { L"dup", NULL },
916 };
917 static TestData kF3Children[] = {
918 { L"f3u4", L"http://www.f3u4.com/" },
919 { L"f3u2", L"http://www.f3u2.com/" },
920 { L"f3u3", L"http://www.f3u3.com/" },
921 { L"f3u1", L"http://www.f3u1.com/" },
922 };
923 static TestData kF4Children[] = {
924 { L"f4u1", L"http://www.f4u1.com/" },
925 { L"f4u2", L"http://www.f4u2.com/" },
926 { L"f4u3", L"http://www.f4u3.com/" },
927 { L"f4u4", L"http://www.f4u4.com/" },
928 };
929 static TestData kDup1Children[] = {
930 { L"dupu1", L"http://www.dupu1.com/" },
931 };
932 static TestData kDup2Children[] = {
933 { L"dupu2", L"http://www.dupu2.com/" },
934 };
935
936 } // anonymous namespace.
937
938 void ProfileSyncServiceTestWithData::PopulateFromTestData(
939 const BookmarkNode* node, const TestData* data, int size) {
940 DCHECK(node);
941 DCHECK(data);
942 DCHECK(node->is_folder());
943 for (int i = 0; i < size; ++i) {
944 const TestData& item = data[i];
945 if (item.url) {
946 model_->AddURL(node, i, item.title, GURL(item.url));
947 } else {
948 model_->AddGroup(node, i, item.title);
949 }
950 }
951 }
952
953 void ProfileSyncServiceTestWithData::CompareWithTestData(
954 const BookmarkNode* node, const TestData* data, int size) {
955 DCHECK(node);
956 DCHECK(data);
957 DCHECK(node->is_folder());
958 for (int i = 0; i < size; ++i) {
959 const BookmarkNode* child_node = node->GetChild(i);
960 const TestData& item = data[i];
961 EXPECT_TRUE(child_node->GetTitle() == item.title);
962 if (item.url) {
963 EXPECT_FALSE(child_node->is_folder());
964 EXPECT_TRUE(child_node->is_url());
965 EXPECT_TRUE(child_node->GetURL() == GURL(item.url));
966 } else {
967 EXPECT_TRUE(child_node->is_folder());
968 EXPECT_FALSE(child_node->is_url());
969 }
970 }
971 }
972
973 // TODO(munjal): We should implement some way of generating random data and can
974 // use the same seed to generate the same sequence.
975 void ProfileSyncServiceTestWithData::WriteTestDataToBookmarkModel() {
976 const BookmarkNode* bookmarks_bar_node = model_->GetBookmarkBarNode();
977 PopulateFromTestData(bookmarks_bar_node,
978 kBookmarkBarChildren,
979 arraysize(kBookmarkBarChildren));
980
981 ASSERT_GE(bookmarks_bar_node->GetChildCount(), 4);
982 const BookmarkNode* f1_node = bookmarks_bar_node->GetChild(1);
983 PopulateFromTestData(f1_node, kF1Children, arraysize(kF1Children));
984 const BookmarkNode* f2_node = bookmarks_bar_node->GetChild(3);
985 PopulateFromTestData(f2_node, kF2Children, arraysize(kF2Children));
986
987 const BookmarkNode* other_bookmarks_node = model_->other_node();
988 PopulateFromTestData(other_bookmarks_node,
989 kOtherBookmarksChildren,
990 arraysize(kOtherBookmarksChildren));
991
992 ASSERT_GE(other_bookmarks_node->GetChildCount(), 6);
993 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
994 PopulateFromTestData(f3_node, kF3Children, arraysize(kF3Children));
995 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
996 PopulateFromTestData(f4_node, kF4Children, arraysize(kF4Children));
997 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
998 PopulateFromTestData(dup_node, kDup1Children, arraysize(kDup1Children));
999 dup_node = other_bookmarks_node->GetChild(5);
1000 PopulateFromTestData(dup_node, kDup2Children, arraysize(kDup2Children));
1001
1002 ExpectBookmarkModelMatchesTestData();
1003 }
1004
1005 void ProfileSyncServiceTestWithData::ExpectBookmarkModelMatchesTestData() {
1006 const BookmarkNode* bookmark_bar_node = model_->GetBookmarkBarNode();
1007 CompareWithTestData(bookmark_bar_node,
1008 kBookmarkBarChildren,
1009 arraysize(kBookmarkBarChildren));
1010
1011 ASSERT_GE(bookmark_bar_node->GetChildCount(), 4);
1012 const BookmarkNode* f1_node = bookmark_bar_node->GetChild(1);
1013 CompareWithTestData(f1_node, kF1Children, arraysize(kF1Children));
1014 const BookmarkNode* f2_node = bookmark_bar_node->GetChild(3);
1015 CompareWithTestData(f2_node, kF2Children, arraysize(kF2Children));
1016
1017 const BookmarkNode* other_bookmarks_node = model_->other_node();
1018 CompareWithTestData(other_bookmarks_node,
1019 kOtherBookmarksChildren,
1020 arraysize(kOtherBookmarksChildren));
1021
1022 ASSERT_GE(other_bookmarks_node->GetChildCount(), 6);
1023 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
1024 CompareWithTestData(f3_node, kF3Children, arraysize(kF3Children));
1025 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
1026 CompareWithTestData(f4_node, kF4Children, arraysize(kF4Children));
1027 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
1028 CompareWithTestData(dup_node, kDup1Children, arraysize(kDup1Children));
1029 dup_node = other_bookmarks_node->GetChild(5);
1030 CompareWithTestData(dup_node, kDup2Children, arraysize(kDup2Children));
1031 }
1032
1033 // Tests persistence of the profile sync service by destroying the
1034 // profile sync service and then reloading it from disk.
1035 TEST_F(ProfileSyncServiceTestWithData, Persistence) {
1036 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1037 StartSyncService();
1038
1039 WriteTestDataToBookmarkModel();
1040
1041 ExpectModelMatch();
1042
1043 // Force both models to discard their data and reload from disk. This
1044 // simulates what would happen if the browser were to shutdown normally,
1045 // and then relaunch.
1046 StopSyncService(SAVE_TO_STORAGE);
1047 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1048 StartSyncService();
1049
1050 ExpectBookmarkModelMatchesTestData();
1051
1052 // With the BookmarkModel contents verified, ExpectModelMatch will
1053 // verify the contents of the sync model.
1054 ExpectModelMatch();
1055 }
1056
1057 // Tests the merge case when the BookmarkModel is non-empty but the
1058 // sync model is empty. This corresponds to uploading browser
1059 // bookmarks to an initially empty, new account.
1060 TEST_F(ProfileSyncServiceTestWithData, MergeWithEmptySyncModel) {
1061 // Don't start the sync service until we've populated the bookmark model.
1062 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1063
1064 WriteTestDataToBookmarkModel();
1065
1066 // Restart the profile sync service. This should trigger a merge step
1067 // during initialization -- we expect the browser bookmarks to be written
1068 // to the sync service during this call.
1069 StartSyncService();
1070
1071 // Verify that the bookmark model hasn't changed, and that the sync model
1072 // matches it exactly.
1073 ExpectBookmarkModelMatchesTestData();
1074 ExpectModelMatch();
1075 }
1076
1077 // Tests the merge case when the BookmarkModel is empty but the sync model is
1078 // non-empty. This corresponds (somewhat) to a clean install of the browser,
1079 // with no bookmarks, connecting to a sync account that has some bookmarks.
1080 TEST_F(ProfileSyncServiceTestWithData, MergeWithEmptyBookmarkModel) {
1081 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1082 StartSyncService();
1083
1084 WriteTestDataToBookmarkModel();
1085
1086 ExpectModelMatch();
1087
1088 // Force the sync service to shut down and write itself to disk.
1089 StopSyncService(SAVE_TO_STORAGE);
1090
1091 // Blow away the bookmark model -- it should be empty afterwards.
1092 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1093 EXPECT_EQ(model_->GetBookmarkBarNode()->GetChildCount(), 0);
1094 EXPECT_EQ(model_->other_node()->GetChildCount(), 0);
1095
1096 // Now restart the sync service. Starting it should populate the bookmark
1097 // model -- test for consistency.
1098 StartSyncService();
1099 ExpectBookmarkModelMatchesTestData();
1100 ExpectModelMatch();
1101 }
1102
1103 // Tests the merge cases when both the models are expected to be identical
1104 // after the merge.
1105 TEST_F(ProfileSyncServiceTestWithData, MergeExpectedIdenticalModels) {
1106 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1107 StartSyncService();
1108 WriteTestDataToBookmarkModel();
1109 ExpectModelMatch();
1110 StopSyncService(SAVE_TO_STORAGE);
1111
1112 // At this point both the bookmark model and the server should have the
1113 // exact same data and it should match the test data.
1114 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1115 StartSyncService();
1116 ExpectBookmarkModelMatchesTestData();
1117 ExpectModelMatch();
1118 StopSyncService(SAVE_TO_STORAGE);
1119
1120 // Now reorder some bookmarks in the bookmark model and then merge. Make
1121 // sure we get the order of the server after merge.
1122 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1123 ExpectBookmarkModelMatchesTestData();
1124 const BookmarkNode* bookmark_bar = model_->GetBookmarkBarNode();
1125 ASSERT_TRUE(bookmark_bar);
1126 ASSERT_GT(bookmark_bar->GetChildCount(), 1);
1127 model_->Move(bookmark_bar->GetChild(0), bookmark_bar, 1);
1128 StartSyncService();
1129 ExpectModelMatch();
1130 ExpectBookmarkModelMatchesTestData();
1131 }
1132
1133 // Tests the merge cases when both the models are expected to be identical
1134 // after the merge.
1135 TEST_F(ProfileSyncServiceTestWithData, MergeModelsWithSomeExtras) {
1136 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1137 WriteTestDataToBookmarkModel();
1138 ExpectBookmarkModelMatchesTestData();
1139
1140 // Remove some nodes and reorder some nodes.
1141 const BookmarkNode* bookmark_bar_node = model_->GetBookmarkBarNode();
1142 int remove_index = 2;
1143 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index);
1144 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
1145 ASSERT_TRUE(child_node);
1146 ASSERT_TRUE(child_node->is_url());
1147 model_->Remove(bookmark_bar_node, remove_index);
1148 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index);
1149 child_node = bookmark_bar_node->GetChild(remove_index);
1150 ASSERT_TRUE(child_node);
1151 ASSERT_TRUE(child_node->is_folder());
1152 model_->Remove(bookmark_bar_node, remove_index);
1153
1154 const BookmarkNode* other_node = model_->other_node();
1155 ASSERT_GE(other_node->GetChildCount(), 1);
1156 const BookmarkNode* f3_node = other_node->GetChild(0);
1157 ASSERT_TRUE(f3_node);
1158 ASSERT_TRUE(f3_node->is_folder());
1159 remove_index = 2;
1160 ASSERT_GT(f3_node->GetChildCount(), remove_index);
1161 model_->Remove(f3_node, remove_index);
1162 ASSERT_GT(f3_node->GetChildCount(), remove_index);
1163 model_->Remove(f3_node, remove_index);
1164
1165 StartSyncService();
1166 ExpectModelMatch();
1167 StopSyncService(SAVE_TO_STORAGE);
1168
1169 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1170 WriteTestDataToBookmarkModel();
1171 ExpectBookmarkModelMatchesTestData();
1172
1173 // Remove some nodes and reorder some nodes.
1174 bookmark_bar_node = model_->GetBookmarkBarNode();
1175 remove_index = 0;
1176 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index);
1177 child_node = bookmark_bar_node->GetChild(remove_index);
1178 ASSERT_TRUE(child_node);
1179 ASSERT_TRUE(child_node->is_url());
1180 model_->Remove(bookmark_bar_node, remove_index);
1181 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index);
1182 child_node = bookmark_bar_node->GetChild(remove_index);
1183 ASSERT_TRUE(child_node);
1184 ASSERT_TRUE(child_node->is_folder());
1185 model_->Remove(bookmark_bar_node, remove_index);
1186
1187 ASSERT_GE(bookmark_bar_node->GetChildCount(), 2);
1188 model_->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 1);
1189
1190 other_node = model_->other_node();
1191 ASSERT_GE(other_node->GetChildCount(), 1);
1192 f3_node = other_node->GetChild(0);
1193 ASSERT_TRUE(f3_node);
1194 ASSERT_TRUE(f3_node->is_folder());
1195 remove_index = 0;
1196 ASSERT_GT(f3_node->GetChildCount(), remove_index);
1197 model_->Remove(f3_node, remove_index);
1198 ASSERT_GT(f3_node->GetChildCount(), remove_index);
1199 model_->Remove(f3_node, remove_index);
1200
1201 ASSERT_GE(other_node->GetChildCount(), 4);
1202 model_->Move(other_node->GetChild(0), other_node, 1);
1203 model_->Move(other_node->GetChild(2), other_node, 3);
1204
1205 StartSyncService();
1206 ExpectModelMatch();
1207
1208 // AFter the merge, the model should match the test data.
1209 ExpectBookmarkModelMatchesTestData();
1210 }
1211
1212 // Tests that when persisted model assocations are used, things work fine.
1213 TEST_F(ProfileSyncServiceTestWithData, ModelAssociationPersistence) {
1214 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1215 WriteTestDataToBookmarkModel();
1216 StartSyncService();
1217 ExpectModelMatch();
1218 // Force the sync service to shut down and write itself to disk.
1219 StopSyncService(SAVE_TO_STORAGE);
1220 // Now restart the sync service. This time it should use the persistent
1221 // assocations.
1222 StartSyncService();
1223 ExpectModelMatch();
1224 }
1225
1226 TEST_F(ProfileSyncServiceTestWithData, SortChildren) {
1227 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1228 StartSyncService();
1229
1230 // Write test data to bookmark model and verify that the models match.
1231 WriteTestDataToBookmarkModel();
1232 const BookmarkNode* folder_added = model_->other_node()->GetChild(0);
1233 ASSERT_TRUE(folder_added);
1234 ASSERT_TRUE(folder_added->is_folder());
1235
1236 ExpectModelMatch();
1237
1238 // Sort the other-bookmarks children and expect that hte models match.
1239 model_->SortChildren(folder_added);
1240 ExpectModelMatch();
1241 }
1242
1243 // See what happens if we enable sync but then delete the "Sync Data"
1244 // folder.
1245 TEST_F(ProfileSyncServiceTestWithData, RecoverAfterDeletingSyncDataDirectory) {
1246 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1247 StartSyncService();
1248
1249 WriteTestDataToBookmarkModel();
1250
1251 // While the service is running.
1252 FilePath sync_data_directory = backend()->sync_data_folder_path();
1253
1254 // Simulate a normal shutdown for the sync service (don't disable it for
1255 // the user, which would reset the preferences and delete the sync data
1256 // directory).
1257 StopSyncService(SAVE_TO_STORAGE);
1258
1259 // Now pretend that the user has deleted this directory from the disk.
1260 file_util::Delete(sync_data_directory, true);
1261
1262 // Restart the sync service.
1263 StartSyncService();
1264
1265 // Make sure we're back in sync. In real life, the user would need
1266 // to reauthenticate before this happens, but in the test, authentication
1267 // is sidestepped.
1268 ExpectBookmarkModelMatchesTestData();
1269 ExpectModelMatch();
1270 }
1271
1272 #endif // CHROME_PERSONALIZATION
OLDNEW
« no previous file with comments | « chrome/browser/sync/profile_sync_service.cc ('k') | chrome/browser/sync/resources/about_sync.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698