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

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

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