OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/engine/directory_commit_contribution.h" | 5 #include "sync/engine/directory_commit_contribution.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "sync/internal_api/public/base/attachment_id_proto.h" |
8 #include "sync/sessions/status_controller.h" | 9 #include "sync/sessions/status_controller.h" |
9 #include "sync/syncable/entry.h" | 10 #include "sync/syncable/entry.h" |
10 #include "sync/syncable/mutable_entry.h" | 11 #include "sync/syncable/mutable_entry.h" |
11 #include "sync/syncable/syncable_read_transaction.h" | 12 #include "sync/syncable/syncable_read_transaction.h" |
12 #include "sync/syncable/syncable_write_transaction.h" | 13 #include "sync/syncable/syncable_write_transaction.h" |
13 #include "sync/test/engine/test_directory_setter_upper.h" | 14 #include "sync/test/engine/test_directory_setter_upper.h" |
14 #include "sync/test/engine/test_id_factory.h" | 15 #include "sync/test/engine/test_id_factory.h" |
15 #include "sync/test/engine/test_syncable_utils.h" | 16 #include "sync/test/engine/test_syncable_utils.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace syncer { | 19 namespace syncer { |
19 | 20 |
20 class DirectoryCommitContributionTest : public ::testing::Test { | 21 class DirectoryCommitContributionTest : public ::testing::Test { |
21 public: | 22 public: |
22 virtual void SetUp() OVERRIDE { | 23 virtual void SetUp() OVERRIDE { |
23 dir_maker_.SetUp(); | 24 dir_maker_.SetUp(); |
24 | 25 |
25 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | 26 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); |
26 CreateTypeRoot(&trans, dir(), PREFERENCES); | 27 CreateTypeRoot(&trans, dir(), PREFERENCES); |
27 CreateTypeRoot(&trans, dir(), EXTENSIONS); | 28 CreateTypeRoot(&trans, dir(), EXTENSIONS); |
| 29 CreateTypeRoot(&trans, dir(), ARTICLES); |
28 CreateTypeRoot(&trans, dir(), BOOKMARKS); | 30 CreateTypeRoot(&trans, dir(), BOOKMARKS); |
29 } | 31 } |
30 | 32 |
31 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
32 dir_maker_.TearDown(); | 34 dir_maker_.TearDown(); |
33 } | 35 } |
34 | 36 |
35 protected: | 37 protected: |
36 int64 CreateUnsyncedItem(syncable::WriteTransaction* trans, | 38 int64 CreateUnsyncedItemWithAttachments( |
37 ModelType type, | 39 syncable::WriteTransaction* trans, |
38 const std::string& tag) { | 40 ModelType type, |
| 41 const std::string& tag, |
| 42 const sync_pb::AttachmentMetadata& attachment_metadata) { |
39 syncable::Entry parent_entry(trans, syncable::GET_TYPE_ROOT, type); | 43 syncable::Entry parent_entry(trans, syncable::GET_TYPE_ROOT, type); |
40 syncable::MutableEntry entry( | 44 syncable::MutableEntry entry( |
41 trans, | 45 trans, |
42 syncable::CREATE, | 46 syncable::CREATE, |
43 type, | 47 type, |
44 parent_entry.GetId(), | 48 parent_entry.GetId(), |
45 tag); | 49 tag); |
| 50 if (attachment_metadata.record_size() > 0) { |
| 51 entry.PutAttachmentMetadata(attachment_metadata); |
| 52 } |
46 entry.PutIsUnsynced(true); | 53 entry.PutIsUnsynced(true); |
47 return entry.GetMetahandle(); | 54 return entry.GetMetahandle(); |
48 } | 55 } |
49 | 56 |
| 57 int64 CreateUnsyncedItem(syncable::WriteTransaction* trans, |
| 58 ModelType type, |
| 59 const std::string& tag) { |
| 60 return CreateUnsyncedItemWithAttachments( |
| 61 trans, type, tag, sync_pb::AttachmentMetadata()); |
| 62 } |
| 63 |
50 int64 CreateSyncedItem(syncable::WriteTransaction* trans, | 64 int64 CreateSyncedItem(syncable::WriteTransaction* trans, |
51 ModelType type, | 65 ModelType type, |
52 const std::string& tag) { | 66 const std::string& tag) { |
53 syncable::Entry parent_entry(trans, syncable::GET_TYPE_ROOT, type); | 67 syncable::Entry parent_entry(trans, syncable::GET_TYPE_ROOT, type); |
54 syncable::MutableEntry entry( | 68 syncable::MutableEntry entry( |
55 trans, | 69 trans, |
56 syncable::CREATE, | 70 syncable::CREATE, |
57 type, | 71 type, |
58 parent_entry.GetId(), | 72 parent_entry.GetId(), |
59 tag); | 73 tag); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 sync_pb::ClientToServerMessage message; | 343 sync_pb::ClientToServerMessage message; |
330 pref_cc->AddToCommitMessage(&message); | 344 pref_cc->AddToCommitMessage(&message); |
331 const sync_pb::CommitMessage& commit_message = message.commit(); | 345 const sync_pb::CommitMessage& commit_message = message.commit(); |
332 pref_cc->CleanUp(); | 346 pref_cc->CleanUp(); |
333 | 347 |
334 ASSERT_EQ(1, commit_message.entries_size()); | 348 ASSERT_EQ(1, commit_message.entries_size()); |
335 EXPECT_FALSE(commit_message.entries(0).has_parent_id_string()); | 349 EXPECT_FALSE(commit_message.entries(0).has_parent_id_string()); |
336 EXPECT_TRUE(commit_message.entries(0).parent_id_string().empty()); | 350 EXPECT_TRUE(commit_message.entries(0).parent_id_string().empty()); |
337 } | 351 } |
338 | 352 |
| 353 void AddAttachment(sync_pb::AttachmentMetadata* metadata, bool is_on_server) { |
| 354 sync_pb::AttachmentMetadataRecord record; |
| 355 *record.mutable_id() = CreateAttachmentIdProto(); |
| 356 record.set_is_on_server(is_on_server); |
| 357 *metadata->add_record() = record; |
| 358 } |
| 359 |
339 // Creates some unsynced items, pretends to commit them, and hands back a | 360 // Creates some unsynced items, pretends to commit them, and hands back a |
340 // specially crafted response to the syncer in order to test commit response | 361 // specially crafted response to the syncer in order to test commit response |
341 // processing. The response simulates a succesful commit scenario. | 362 // processing. The response simulates a succesful commit scenario. |
342 TEST_F(DirectoryCommitContributionTest, ProcessCommitResponse) { | 363 TEST_F(DirectoryCommitContributionTest, ProcessCommitResponse) { |
343 int64 pref1_handle; | 364 int64 pref1_handle; |
344 int64 pref2_handle; | 365 int64 pref2_handle; |
345 int64 ext1_handle; | 366 int64 ext1_handle; |
346 { | 367 { |
347 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | 368 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); |
348 pref1_handle = CreateUnsyncedItem(&trans, PREFERENCES, "pref1"); | 369 pref1_handle = CreateUnsyncedItem(&trans, PREFERENCES, "pref1"); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 syncable::Entry e1(&trans, syncable::GET_BY_HANDLE, ext1_handle); | 414 syncable::Entry e1(&trans, syncable::GET_BY_HANDLE, ext1_handle); |
394 EXPECT_TRUE(e1.GetId().ServerKnows()); | 415 EXPECT_TRUE(e1.GetId().ServerKnows()); |
395 EXPECT_FALSE(e1.GetSyncing()); | 416 EXPECT_FALSE(e1.GetSyncing()); |
396 EXPECT_LT(0, e1.GetServerVersion()); | 417 EXPECT_LT(0, e1.GetServerVersion()); |
397 } | 418 } |
398 | 419 |
399 pref_cc->CleanUp(); | 420 pref_cc->CleanUp(); |
400 ext_cc->CleanUp(); | 421 ext_cc->CleanUp(); |
401 } | 422 } |
402 | 423 |
| 424 // Creates some unsynced items with attachments and verifies that only items |
| 425 // where all attachments have been uploaded to the server are eligible to be |
| 426 // committed. |
| 427 TEST_F(DirectoryCommitContributionTest, ProcessCommitResponseWithAttachments) { |
| 428 int64 art1_handle; |
| 429 int64 art2_handle; |
| 430 int64 art3_handle; |
| 431 { |
| 432 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); |
| 433 |
| 434 // art1 has two attachments, both have been uploaded to the server. art1 is |
| 435 // eligible to be committed. |
| 436 sync_pb::AttachmentMetadata art1_attachments; |
| 437 AddAttachment(&art1_attachments, true /* is_on_server */); |
| 438 AddAttachment(&art1_attachments, true /* is_on_server */); |
| 439 art1_handle = CreateUnsyncedItemWithAttachments( |
| 440 &trans, ARTICLES, "art1", art1_attachments); |
| 441 |
| 442 // art2 has two attachments, one of which has been uploaded to the |
| 443 // server. art2 is not eligible to be committed. |
| 444 sync_pb::AttachmentMetadata art2_attachments; |
| 445 AddAttachment(&art2_attachments, false /* is_on_server */); |
| 446 AddAttachment(&art2_attachments, true /* is_on_server */); |
| 447 art2_handle = CreateUnsyncedItemWithAttachments( |
| 448 &trans, ARTICLES, "art2", art2_attachments); |
| 449 |
| 450 // art3 has two attachments, neither of which have been uploaded to the |
| 451 // server. art2 is not eligible to be committed. |
| 452 sync_pb::AttachmentMetadata art3_attachments; |
| 453 AddAttachment(&art3_attachments, false /* is_on_server */); |
| 454 AddAttachment(&art3_attachments, false /* is_on_server */); |
| 455 art3_handle = CreateUnsyncedItemWithAttachments( |
| 456 &trans, ARTICLES, "art3", art3_attachments); |
| 457 } |
| 458 |
| 459 DirectoryTypeDebugInfoEmitter emitter(ARTICLES, &type_observers_); |
| 460 scoped_ptr<DirectoryCommitContribution> art_cc( |
| 461 DirectoryCommitContribution::Build(dir(), ARTICLES, 25, &emitter)); |
| 462 |
| 463 // Only art1 is ready. |
| 464 EXPECT_EQ(1U, art_cc->GetNumEntries()); |
| 465 |
| 466 sync_pb::ClientToServerMessage message; |
| 467 art_cc->AddToCommitMessage(&message); |
| 468 |
| 469 const sync_pb::CommitMessage& commit_message = message.commit(); |
| 470 ASSERT_EQ(1, commit_message.entries_size()); |
| 471 |
| 472 sync_pb::ClientToServerResponse response; |
| 473 for (int i = 0; i < commit_message.entries_size(); ++i) { |
| 474 sync_pb::SyncEntity entity = commit_message.entries(i); |
| 475 sync_pb::CommitResponse_EntryResponse* entry_response = |
| 476 response.mutable_commit()->add_entryresponse(); |
| 477 CreateSuccessfulCommitResponse(entity, entry_response); |
| 478 } |
| 479 |
| 480 sessions::StatusController status; |
| 481 art_cc->ProcessCommitResponse(response, &status); |
| 482 { |
| 483 syncable::ReadTransaction trans(FROM_HERE, dir()); |
| 484 |
| 485 syncable::Entry a1(&trans, syncable::GET_BY_HANDLE, art1_handle); |
| 486 EXPECT_TRUE(a1.GetId().ServerKnows()); |
| 487 EXPECT_FALSE(a1.GetSyncing()); |
| 488 EXPECT_LT(0, a1.GetServerVersion()); |
| 489 |
| 490 syncable::Entry a2(&trans, syncable::GET_BY_HANDLE, art2_handle); |
| 491 EXPECT_FALSE(a2.GetId().ServerKnows()); |
| 492 EXPECT_FALSE(a2.GetSyncing()); |
| 493 EXPECT_EQ(0, a2.GetServerVersion()); |
| 494 |
| 495 syncable::Entry a3(&trans, syncable::GET_BY_HANDLE, art3_handle); |
| 496 EXPECT_FALSE(a3.GetId().ServerKnows()); |
| 497 EXPECT_FALSE(a3.GetSyncing()); |
| 498 EXPECT_EQ(0, a3.GetServerVersion()); |
| 499 } |
| 500 |
| 501 art_cc->CleanUp(); |
| 502 } |
| 503 |
403 } // namespace syncer | 504 } // namespace syncer |
OLD | NEW |