| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "chrome/browser/sync/engine/apply_updates_command.h" | 5 #include "chrome/browser/sync/engine/apply_updates_command.h" |
| 6 #include "chrome/browser/sync/engine/sync_cycle_state.h" | 6 #include "chrome/browser/sync/sessions/sync_session.h" |
| 7 #include "chrome/browser/sync/engine/sync_process_state.h" | |
| 8 #include "chrome/browser/sync/engine/syncer_session.h" | |
| 9 #include "chrome/browser/sync/syncable/directory_manager.h" | 7 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 10 #include "chrome/browser/sync/syncable/syncable.h" | 8 #include "chrome/browser/sync/syncable/syncable.h" |
| 11 #include "chrome/browser/sync/syncable/syncable_id.h" | 9 #include "chrome/browser/sync/syncable/syncable_id.h" |
| 12 #include "chrome/test/sync/engine/test_directory_setter_upper.h" | 10 #include "chrome/test/sync/engine/test_directory_setter_upper.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 12 |
| 15 using std::string; | 13 using std::string; |
| 16 using syncable::ScopedDirLookup; | 14 using syncable::ScopedDirLookup; |
| 17 using syncable::WriteTransaction; | 15 using syncable::WriteTransaction; |
| 18 using syncable::ReadTransaction; | 16 using syncable::ReadTransaction; |
| 19 using syncable::MutableEntry; | 17 using syncable::MutableEntry; |
| 20 using syncable::Entry; | 18 using syncable::Entry; |
| 21 using syncable::Id; | 19 using syncable::Id; |
| 22 using syncable::UNITTEST; | 20 using syncable::UNITTEST; |
| 23 | 21 |
| 24 namespace browser_sync { | 22 namespace browser_sync { |
| 23 using sessions::SyncSessionContext; |
| 24 using sessions::SyncSession; |
| 25 | 25 |
| 26 // A test fixture for tests exercising ApplyUpdatesCommand. | 26 // A test fixture for tests exercising ApplyUpdatesCommand. |
| 27 class ApplyUpdatesCommandTest : public testing::Test { | 27 class ApplyUpdatesCommandTest : public testing::Test, |
| 28 public SyncSession::Delegate { |
| 29 public: |
| 30 // SyncSession::Delegate implementation. |
| 31 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until) { |
| 32 FAIL() << "Should not get silenced."; |
| 33 } |
| 34 virtual bool IsSyncingCurrentlySilenced() { |
| 35 ADD_FAILURE() << "No requests for silenced state should be made."; |
| 36 return false; |
| 37 } |
| 38 virtual void OnReceivedLongPollIntervalUpdate( |
| 39 const base::TimeDelta& new_interval) { |
| 40 FAIL() << "Should not get poll interval update."; |
| 41 } |
| 42 virtual void OnReceivedShortPollIntervalUpdate( |
| 43 const base::TimeDelta& new_interval) { |
| 44 FAIL() << "Should not get poll interval update."; |
| 45 } |
| 46 |
| 28 protected: | 47 protected: |
| 29 ApplyUpdatesCommandTest() : next_revision_(1) {} | 48 ApplyUpdatesCommandTest() : next_revision_(1) {} |
| 30 virtual ~ApplyUpdatesCommandTest() {} | 49 virtual ~ApplyUpdatesCommandTest() {} |
| 31 virtual void SetUp() { | 50 virtual void SetUp() { |
| 32 syncdb_.SetUp(); | 51 syncdb_.SetUp(); |
| 52 context_.reset(new SyncSessionContext(NULL, syncdb_.manager(), NULL)); |
| 53 context_->set_account_name(syncdb_.name()); |
| 33 } | 54 } |
| 34 virtual void TearDown() { | 55 virtual void TearDown() { |
| 35 syncdb_.TearDown(); | 56 syncdb_.TearDown(); |
| 36 } | 57 } |
| 37 | 58 |
| 38 protected: | 59 protected: |
| 60 |
| 39 // Create a new unapplied update. | 61 // Create a new unapplied update. |
| 40 void CreateUnappliedNewItemWithParent(const string& item_id, | 62 void CreateUnappliedNewItemWithParent(const string& item_id, |
| 41 const string& parent_id) { | 63 const string& parent_id) { |
| 42 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 64 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 43 ASSERT_TRUE(dir.good()); | 65 ASSERT_TRUE(dir.good()); |
| 44 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); | 66 WriteTransaction trans(dir, UNITTEST, __FILE__, __LINE__); |
| 45 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | 67 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 46 Id::CreateFromServerId(item_id)); | 68 Id::CreateFromServerId(item_id)); |
| 47 ASSERT_TRUE(entry.good()); | 69 ASSERT_TRUE(entry.good()); |
| 48 entry.Put(syncable::SERVER_VERSION, next_revision_++); | 70 entry.Put(syncable::SERVER_VERSION, next_revision_++); |
| 49 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 71 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 50 | 72 |
| 51 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); | 73 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 52 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); | 74 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); |
| 53 entry.Put(syncable::SERVER_IS_DIR, true); | 75 entry.Put(syncable::SERVER_IS_DIR, true); |
| 54 } | 76 } |
| 55 | 77 |
| 56 TestDirectorySetterUpper syncdb_; | 78 TestDirectorySetterUpper syncdb_; |
| 57 ApplyUpdatesCommand apply_updates_command_; | 79 ApplyUpdatesCommand apply_updates_command_; |
| 58 | 80 scoped_ptr<SyncSessionContext> context_; |
| 59 private: | 81 private: |
| 60 int64 next_revision_; | 82 int64 next_revision_; |
| 61 DISALLOW_COPY_AND_ASSIGN(ApplyUpdatesCommandTest); | 83 DISALLOW_COPY_AND_ASSIGN(ApplyUpdatesCommandTest); |
| 62 }; | 84 }; |
| 63 | 85 |
| 64 TEST_F(ApplyUpdatesCommandTest, Simple) { | 86 TEST_F(ApplyUpdatesCommandTest, Simple) { |
| 65 string root_server_id = syncable::kNullId.GetServerId(); | 87 string root_server_id = syncable::kNullId.GetServerId(); |
| 66 CreateUnappliedNewItemWithParent("parent", root_server_id); | 88 CreateUnappliedNewItemWithParent("parent", root_server_id); |
| 67 CreateUnappliedNewItemWithParent("child", "parent"); | 89 CreateUnappliedNewItemWithParent("child", "parent"); |
| 68 | 90 |
| 69 SyncCycleState cycle_state; | 91 SyncSession session(context_.get(), this); |
| 70 SyncProcessState process_state(syncdb_.manager(), syncdb_.name(), | |
| 71 NULL, NULL, NULL, NULL); | |
| 72 SyncerSession session(&cycle_state, &process_state); | |
| 73 | |
| 74 apply_updates_command_.ModelChangingExecuteImpl(&session); | 92 apply_updates_command_.ModelChangingExecuteImpl(&session); |
| 75 | 93 |
| 76 EXPECT_EQ(2, cycle_state.AppliedUpdatesSize()) | 94 sessions::StatusController* status = session.status_controller(); |
| 95 EXPECT_EQ(2, status->update_progress().AppliedUpdatesSize()) |
| 77 << "All updates should have been attempted"; | 96 << "All updates should have been attempted"; |
| 78 EXPECT_EQ(0, process_state.ConflictingItemsSize()) | 97 EXPECT_EQ(0, status->conflict_progress()->ConflictingItemsSize()) |
| 79 << "Simple update shouldn't result in conflicts"; | 98 << "Simple update shouldn't result in conflicts"; |
| 80 EXPECT_EQ(2, cycle_state.SuccessfullyAppliedUpdateCount()) | 99 EXPECT_EQ(2, status->update_progress().SuccessfullyAppliedUpdateCount()) |
| 81 << "All items should have been successfully applied"; | 100 << "All items should have been successfully applied"; |
| 82 } | 101 } |
| 83 | 102 |
| 84 TEST_F(ApplyUpdatesCommandTest, UpdateWithChildrenBeforeParents) { | 103 TEST_F(ApplyUpdatesCommandTest, UpdateWithChildrenBeforeParents) { |
| 85 // Set a bunch of updates which are difficult to apply in the order | 104 // Set a bunch of updates which are difficult to apply in the order |
| 86 // they're received due to dependencies on other unseen items. | 105 // they're received due to dependencies on other unseen items. |
| 87 string root_server_id = syncable::kNullId.GetServerId(); | 106 string root_server_id = syncable::kNullId.GetServerId(); |
| 88 CreateUnappliedNewItemWithParent("a_child_created_first", "parent"); | 107 CreateUnappliedNewItemWithParent("a_child_created_first", "parent"); |
| 89 CreateUnappliedNewItemWithParent("x_child_created_first", "parent"); | 108 CreateUnappliedNewItemWithParent("x_child_created_first", "parent"); |
| 90 CreateUnappliedNewItemWithParent("parent", root_server_id); | 109 CreateUnappliedNewItemWithParent("parent", root_server_id); |
| 91 CreateUnappliedNewItemWithParent("a_child_created_second", "parent"); | 110 CreateUnappliedNewItemWithParent("a_child_created_second", "parent"); |
| 92 CreateUnappliedNewItemWithParent("x_child_created_second", "parent"); | 111 CreateUnappliedNewItemWithParent("x_child_created_second", "parent"); |
| 93 | 112 |
| 94 SyncCycleState cycle_state; | 113 SyncSession session(context_.get(), this); |
| 95 SyncProcessState process_state(syncdb_.manager(), syncdb_.name(), | |
| 96 NULL, NULL, NULL, NULL); | |
| 97 SyncerSession session(&cycle_state, &process_state); | |
| 98 | |
| 99 apply_updates_command_.ModelChangingExecuteImpl(&session); | 114 apply_updates_command_.ModelChangingExecuteImpl(&session); |
| 100 | 115 |
| 101 EXPECT_EQ(5, cycle_state.AppliedUpdatesSize()) | 116 sessions::StatusController* status = session.status_controller(); |
| 117 EXPECT_EQ(5, status->update_progress().AppliedUpdatesSize()) |
| 102 << "All updates should have been attempted"; | 118 << "All updates should have been attempted"; |
| 103 EXPECT_EQ(0, process_state.ConflictingItemsSize()) | 119 EXPECT_EQ(0, status->conflict_progress()->ConflictingItemsSize()) |
| 104 << "Simple update shouldn't result in conflicts, even if out-of-order"; | 120 << "Simple update shouldn't result in conflicts, even if out-of-order"; |
| 105 EXPECT_EQ(5, cycle_state.SuccessfullyAppliedUpdateCount()) | 121 EXPECT_EQ(5, status->update_progress().SuccessfullyAppliedUpdateCount()) |
| 106 << "All updates should have been successfully applied"; | 122 << "All updates should have been successfully applied"; |
| 107 } | 123 } |
| 108 | 124 |
| 109 TEST_F(ApplyUpdatesCommandTest, NestedItemsWithUnknownParent) { | 125 TEST_F(ApplyUpdatesCommandTest, NestedItemsWithUnknownParent) { |
| 110 // We shouldn't be able to do anything with either of these items. | 126 // We shouldn't be able to do anything with either of these items. |
| 111 CreateUnappliedNewItemWithParent("some_item", "unknown_parent"); | 127 CreateUnappliedNewItemWithParent("some_item", "unknown_parent"); |
| 112 CreateUnappliedNewItemWithParent("some_other_item", "some_item"); | 128 CreateUnappliedNewItemWithParent("some_other_item", "some_item"); |
| 113 | 129 |
| 114 SyncCycleState cycle_state; | 130 SyncSession session(context_.get(), this); |
| 115 SyncProcessState process_state(syncdb_.manager(), syncdb_.name(), | |
| 116 NULL, NULL, NULL, NULL); | |
| 117 SyncerSession session(&cycle_state, &process_state); | |
| 118 | |
| 119 apply_updates_command_.ModelChangingExecuteImpl(&session); | 131 apply_updates_command_.ModelChangingExecuteImpl(&session); |
| 120 | 132 |
| 121 EXPECT_EQ(2, cycle_state.AppliedUpdatesSize()) | 133 sessions::StatusController* status = session.status_controller(); |
| 134 EXPECT_EQ(2, status->update_progress().AppliedUpdatesSize()) |
| 122 << "All updates should have been attempted"; | 135 << "All updates should have been attempted"; |
| 123 EXPECT_EQ(2, process_state.ConflictingItemsSize()) | 136 EXPECT_EQ(2, status->conflict_progress()->ConflictingItemsSize()) |
| 124 << "All updates with an unknown ancestors should be in conflict"; | 137 << "All updates with an unknown ancestors should be in conflict"; |
| 125 EXPECT_EQ(0, cycle_state.SuccessfullyAppliedUpdateCount()) | 138 EXPECT_EQ(0, status->update_progress().SuccessfullyAppliedUpdateCount()) |
| 126 << "No item with an unknown ancestor should be applied"; | 139 << "No item with an unknown ancestor should be applied"; |
| 127 } | 140 } |
| 128 | 141 |
| 129 TEST_F(ApplyUpdatesCommandTest, ItemsBothKnownAndUnknown) { | 142 TEST_F(ApplyUpdatesCommandTest, ItemsBothKnownAndUnknown) { |
| 130 // See what happens when there's a mixture of good and bad updates. | 143 // See what happens when there's a mixture of good and bad updates. |
| 131 string root_server_id = syncable::kNullId.GetServerId(); | 144 string root_server_id = syncable::kNullId.GetServerId(); |
| 132 CreateUnappliedNewItemWithParent("first_unknown_item", "unknown_parent"); | 145 CreateUnappliedNewItemWithParent("first_unknown_item", "unknown_parent"); |
| 133 CreateUnappliedNewItemWithParent("first_known_item", root_server_id); | 146 CreateUnappliedNewItemWithParent("first_known_item", root_server_id); |
| 134 CreateUnappliedNewItemWithParent("second_unknown_item", "unknown_parent"); | 147 CreateUnappliedNewItemWithParent("second_unknown_item", "unknown_parent"); |
| 135 CreateUnappliedNewItemWithParent("second_known_item", "first_known_item"); | 148 CreateUnappliedNewItemWithParent("second_known_item", "first_known_item"); |
| 136 CreateUnappliedNewItemWithParent("third_known_item", "fourth_known_item"); | 149 CreateUnappliedNewItemWithParent("third_known_item", "fourth_known_item"); |
| 137 CreateUnappliedNewItemWithParent("fourth_known_item", root_server_id); | 150 CreateUnappliedNewItemWithParent("fourth_known_item", root_server_id); |
| 138 | 151 |
| 139 SyncCycleState cycle_state; | 152 SyncSession session(context_.get(), this); |
| 140 SyncProcessState process_state(syncdb_.manager(), syncdb_.name(), | |
| 141 NULL, NULL, NULL, NULL); | |
| 142 SyncerSession session(&cycle_state, &process_state); | |
| 143 | |
| 144 apply_updates_command_.ModelChangingExecuteImpl(&session); | 153 apply_updates_command_.ModelChangingExecuteImpl(&session); |
| 145 | 154 |
| 146 EXPECT_EQ(6, cycle_state.AppliedUpdatesSize()) | 155 sessions::StatusController* status = session.status_controller(); |
| 156 EXPECT_EQ(6, status->update_progress().AppliedUpdatesSize()) |
| 147 << "All updates should have been attempted"; | 157 << "All updates should have been attempted"; |
| 148 EXPECT_EQ(2, process_state.ConflictingItemsSize()) | 158 EXPECT_EQ(2, status->conflict_progress()->ConflictingItemsSize()) |
| 149 << "The updates with unknown ancestors should be in conflict"; | 159 << "The updates with unknown ancestors should be in conflict"; |
| 150 EXPECT_EQ(4, cycle_state.SuccessfullyAppliedUpdateCount()) | 160 EXPECT_EQ(4, status->update_progress().SuccessfullyAppliedUpdateCount()) |
| 151 << "The updates with known ancestors should be successfully applied"; | 161 << "The updates with known ancestors should be successfully applied"; |
| 152 } | 162 } |
| 153 | 163 |
| 154 } // namespace browser_sync | 164 } // namespace browser_sync |
| OLD | NEW |