| 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/internal_api/sync_backup_manager.h" | 5 #include "sync/internal_api/sync_backup_manager.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/run_loop.h" |
| 8 #include "sync/internal_api/public/read_node.h" | 9 #include "sync/internal_api/public/read_node.h" |
| 9 #include "sync/internal_api/public/read_transaction.h" | 10 #include "sync/internal_api/public/read_transaction.h" |
| 11 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
| 10 #include "sync/internal_api/public/test/test_internal_components_factory.h" | 12 #include "sync/internal_api/public/test/test_internal_components_factory.h" |
| 11 #include "sync/internal_api/public/write_node.h" | 13 #include "sync/internal_api/public/write_node.h" |
| 12 #include "sync/internal_api/public/write_transaction.h" | 14 #include "sync/internal_api/public/write_transaction.h" |
| 13 #include "sync/syncable/entry.h" | 15 #include "sync/syncable/entry.h" |
| 14 #include "sync/test/test_directory_backing_store.h" | 16 #include "sync/test/test_directory_backing_store.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 19 |
| 20 using ::testing::_; |
| 21 using ::testing::Invoke; |
| 22 using ::testing::WithArgs; |
| 23 |
| 17 namespace syncer { | 24 namespace syncer { |
| 18 | 25 |
| 19 namespace { | 26 namespace { |
| 20 | 27 |
| 21 void OnConfigDone(bool success) { | 28 void OnConfigDone(bool success) { |
| 22 EXPECT_TRUE(success); | 29 EXPECT_TRUE(success); |
| 23 } | 30 } |
| 24 | 31 |
| 25 class SyncBackupManagerTest : public testing::Test { | 32 class SyncBackupManagerTest : public syncer::SyncManager::Observer, |
| 33 public testing::Test { |
| 34 public: |
| 35 MOCK_METHOD1(OnSyncCycleCompleted, |
| 36 void(const sessions::SyncSessionSnapshot&)); |
| 37 MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus)); |
| 38 MOCK_METHOD1(OnActionableError, void(const SyncProtocolError&)); |
| 39 MOCK_METHOD1(OnMigrationRequested, void(ModelTypeSet));; |
| 40 MOCK_METHOD1(OnProtocolEvent, void(const ProtocolEvent&)); |
| 41 MOCK_METHOD4(OnInitializationComplete, |
| 42 void(const WeakHandle<JsBackend>&, |
| 43 const WeakHandle<DataTypeDebugInfoListener>&, |
| 44 bool, ModelTypeSet)); |
| 45 |
| 26 protected: | 46 protected: |
| 27 virtual void SetUp() OVERRIDE { | 47 virtual void SetUp() OVERRIDE { |
| 28 CHECK(temp_dir_.CreateUniqueTempDir()); | 48 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 29 } | 49 } |
| 30 | 50 |
| 31 void InitManager(SyncManager* manager) { | 51 void InitManager(SyncManager* manager, StorageOption storage_option) { |
| 52 manager_ = manager; |
| 53 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _)) |
| 54 .WillOnce(WithArgs<2>(Invoke(this, |
| 55 &SyncBackupManagerTest::HandleInit))); |
| 56 |
| 32 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), | 57 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(), |
| 33 STORAGE_ON_DISK); | 58 storage_option); |
| 59 manager->AddObserver(this); |
| 34 | 60 |
| 61 base::RunLoop run_loop; |
| 35 manager->Init(temp_dir_.path(), | 62 manager->Init(temp_dir_.path(), |
| 36 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), | 63 MakeWeakHandle(base::WeakPtr<JsEventHandler>()), |
| 37 "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), | 64 "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(), |
| 38 std::vector<scoped_refptr<ModelSafeWorker> >(), | 65 std::vector<scoped_refptr<ModelSafeWorker> >(), |
| 39 NULL, NULL, SyncCredentials(), "", "", "", &factory, | 66 NULL, NULL, SyncCredentials(), "", "", "", &factory, |
| 40 NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), | 67 NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(), |
| 41 NULL, NULL); | 68 NULL, NULL); |
| 42 manager->ConfigureSyncer( | 69 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 43 CONFIGURE_REASON_NEW_CLIENT, | 70 run_loop.Run(); |
| 44 ModelTypeSet(SEARCH_ENGINES), | |
| 45 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), | |
| 46 ModelSafeRoutingInfo(), | |
| 47 base::Bind(&OnConfigDone, true), | |
| 48 base::Bind(&OnConfigDone, false)); | |
| 49 } | 71 } |
| 50 | 72 |
| 51 void CreateEntry(UserShare* user_share, ModelType type, | 73 void CreateEntry(UserShare* user_share, ModelType type, |
| 52 const std::string& client_tag) { | 74 const std::string& client_tag) { |
| 53 WriteTransaction trans(FROM_HERE, user_share); | 75 WriteTransaction trans(FROM_HERE, user_share); |
| 54 ReadNode type_root(&trans); | 76 ReadNode type_root(&trans); |
| 55 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); | 77 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); |
| 56 | 78 |
| 57 WriteNode node(&trans); | 79 WriteNode node(&trans); |
| 58 EXPECT_EQ(WriteNode::INIT_SUCCESS, | 80 EXPECT_EQ(WriteNode::INIT_SUCCESS, |
| 59 node.InitUniqueByCreation(type, type_root, client_tag)); | 81 node.InitUniqueByCreation(type, type_root, client_tag)); |
| 60 } | 82 } |
| 61 | 83 |
| 84 private: |
| 85 void ConfigureSyncer() { |
| 86 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT, |
| 87 ModelTypeSet(SEARCH_ENGINES), |
| 88 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), |
| 89 ModelSafeRoutingInfo(), |
| 90 base::Bind(&OnConfigDone, true), |
| 91 base::Bind(&OnConfigDone, false)); |
| 92 } |
| 93 |
| 94 void HandleInit(bool success) { |
| 95 if (success) { |
| 96 loop_.PostTask(FROM_HERE, |
| 97 base::Bind(&SyncBackupManagerTest::ConfigureSyncer, |
| 98 base::Unretained(this))); |
| 99 } else { |
| 100 manager_->ShutdownOnSyncThread(); |
| 101 } |
| 102 } |
| 103 |
| 62 base::ScopedTempDir temp_dir_; | 104 base::ScopedTempDir temp_dir_; |
| 63 base::MessageLoop loop_; // Needed for WeakHandle | 105 base::MessageLoop loop_; // Needed for WeakHandle |
| 106 SyncManager* manager_; |
| 64 }; | 107 }; |
| 65 | 108 |
| 66 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) { | 109 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) { |
| 67 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); | 110 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 68 InitManager(manager.get()); | 111 InitManager(manager.get(), STORAGE_ON_DISK); |
| 69 | 112 |
| 70 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); | 113 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 71 | 114 |
| 72 { | 115 { |
| 73 // New entry is local and unsynced at first. | 116 // New entry is local and unsynced at first. |
| 74 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 117 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 75 ReadNode pref(&trans); | 118 ReadNode pref(&trans); |
| 76 EXPECT_EQ(BaseNode::INIT_OK, | 119 EXPECT_EQ(BaseNode::INIT_OK, |
| 77 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 120 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 78 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); | 121 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); |
| 79 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); | 122 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); |
| 80 } | 123 } |
| 81 | 124 |
| 82 manager->SaveChanges(); | 125 manager->SaveChanges(); |
| 83 | 126 |
| 84 { | 127 { |
| 85 // New entry has server ID and unsynced bit is cleared after saving. | 128 // New entry has server ID and unsynced bit is cleared after saving. |
| 86 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 129 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 87 ReadNode pref(&trans); | 130 ReadNode pref(&trans); |
| 88 EXPECT_EQ(BaseNode::INIT_OK, | 131 EXPECT_EQ(BaseNode::INIT_OK, |
| 89 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 132 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 90 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); | 133 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| 91 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); | 134 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| 92 } | 135 } |
| 93 manager->ShutdownOnSyncThread(); | 136 manager->ShutdownOnSyncThread(); |
| 94 | 137 |
| 95 // Reopen db to verify entry is persisted. | 138 // Reopen db to verify entry is persisted. |
| 96 manager.reset(new SyncBackupManager); | 139 manager.reset(new SyncBackupManager); |
| 97 InitManager(manager.get()); | 140 InitManager(manager.get(), STORAGE_ON_DISK); |
| 98 { | 141 { |
| 99 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 142 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 100 ReadNode pref(&trans); | 143 ReadNode pref(&trans); |
| 101 EXPECT_EQ(BaseNode::INIT_OK, | 144 EXPECT_EQ(BaseNode::INIT_OK, |
| 102 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 145 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 103 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); | 146 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| 104 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); | 147 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| 105 } | 148 } |
| 106 } | 149 } |
| 107 | 150 |
| 151 TEST_F(SyncBackupManagerTest, FailToInitialize) { |
| 152 // Test graceful shutdown on initialization failure. |
| 153 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 154 InitManager(manager.get(), STORAGE_INVALID); |
| 155 } |
| 156 |
| 108 } // anonymous namespace | 157 } // anonymous namespace |
| 109 | 158 |
| 110 } // namespace syncer | 159 } // namespace syncer |
| 111 | 160 |
| OLD | NEW |