| 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 "base/run_loop.h" |
| 9 #include "sync/internal_api/public/read_node.h" | 9 #include "sync/internal_api/public/read_node.h" |
| 10 #include "sync/internal_api/public/read_transaction.h" | 10 #include "sync/internal_api/public/read_transaction.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 MOCK_METHOD4(OnInitializationComplete, | 42 MOCK_METHOD4(OnInitializationComplete, |
| 43 void(const WeakHandle<JsBackend>&, | 43 void(const WeakHandle<JsBackend>&, |
| 44 const WeakHandle<DataTypeDebugInfoListener>&, | 44 const WeakHandle<DataTypeDebugInfoListener>&, |
| 45 bool, ModelTypeSet)); | 45 bool, ModelTypeSet)); |
| 46 | 46 |
| 47 protected: | 47 protected: |
| 48 virtual void SetUp() OVERRIDE { | 48 virtual void SetUp() OVERRIDE { |
| 49 CHECK(temp_dir_.CreateUniqueTempDir()); | 49 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void InitManager(SyncManager* manager, StorageOption storage_option) { | 52 void InitManager(SyncManager* manager, |
| 53 InternalComponentsFactory::StorageOption storage_option) { |
| 53 manager_ = manager; | 54 manager_ = manager; |
| 54 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _)) | 55 EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _)) |
| 55 .WillOnce(WithArgs<2>(Invoke(this, | 56 .WillOnce(WithArgs<2>(Invoke(this, |
| 56 &SyncBackupManagerTest::HandleInit))); | 57 &SyncBackupManagerTest::HandleInit))); |
| 57 | 58 |
| 58 manager->AddObserver(this); | 59 manager->AddObserver(this); |
| 59 | 60 |
| 60 base::RunLoop run_loop; | 61 base::RunLoop run_loop; |
| 61 | 62 |
| 62 SyncManager::InitArgs args; | 63 SyncManager::InitArgs args; |
| 63 args.database_location = temp_dir_.path(); | 64 args.database_location = temp_dir_.path(); |
| 64 args.event_handler = MakeWeakHandle(base::WeakPtr<JsEventHandler>()); | 65 args.event_handler = MakeWeakHandle(base::WeakPtr<JsEventHandler>()); |
| 65 args.service_url = GURL("https://example.com/"); | 66 args.service_url = GURL("https://example.com/"); |
| 66 args.post_factory = scoped_ptr<HttpPostProviderFactory>().Pass(); | 67 args.post_factory = scoped_ptr<HttpPostProviderFactory>().Pass(); |
| 67 args.internal_components_factory.reset(new TestInternalComponentsFactory( | 68 args.internal_components_factory.reset(new TestInternalComponentsFactory( |
| 68 InternalComponentsFactory::Switches(), storage_option)); | 69 InternalComponentsFactory::Switches(), storage_option, |
| 70 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED)); |
| 69 manager->Init(&args); | 71 manager->Init(&args); |
| 70 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); | 72 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 71 run_loop.Run(); | 73 run_loop.Run(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void CreateEntry(UserShare* user_share, ModelType type, | 76 void CreateEntry(UserShare* user_share, ModelType type, |
| 75 const std::string& client_tag) { | 77 const std::string& client_tag) { |
| 76 WriteTransaction trans(FROM_HERE, user_share); | 78 WriteTransaction trans(FROM_HERE, user_share); |
| 77 ReadNode type_root(&trans); | 79 ReadNode type_root(&trans); |
| 78 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); | 80 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); |
| 79 | 81 |
| 80 WriteNode node(&trans); | 82 WriteNode node(&trans); |
| 81 EXPECT_EQ(WriteNode::INIT_SUCCESS, | 83 EXPECT_EQ(WriteNode::INIT_SUCCESS, |
| 82 node.InitUniqueByCreation(type, type_root, client_tag)); | 84 node.InitUniqueByCreation(type, type_root, client_tag)); |
| 83 } | 85 } |
| 84 | 86 |
| 85 private: | |
| 86 void ConfigureSyncer() { | 87 void ConfigureSyncer() { |
| 87 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT, | 88 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT, |
| 88 ModelTypeSet(SEARCH_ENGINES), | 89 ModelTypeSet(SEARCH_ENGINES), |
| 89 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), | 90 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), |
| 90 ModelSafeRoutingInfo(), | 91 ModelSafeRoutingInfo(), |
| 91 base::Bind(&OnConfigDone, true), | 92 base::Bind(&OnConfigDone, true), |
| 92 base::Bind(&OnConfigDone, false)); | 93 base::Bind(&OnConfigDone, false)); |
| 93 } | 94 } |
| 94 | 95 |
| 95 void HandleInit(bool success) { | 96 void HandleInit(bool success) { |
| 96 if (success) { | 97 if (success) { |
| 97 loop_.PostTask(FROM_HERE, | 98 loop_.PostTask(FROM_HERE, |
| 98 base::Bind(&SyncBackupManagerTest::ConfigureSyncer, | 99 base::Bind(&SyncBackupManagerTest::ConfigureSyncer, |
| 99 base::Unretained(this))); | 100 base::Unretained(this))); |
| 100 } else { | 101 } else { |
| 101 manager_->ShutdownOnSyncThread(STOP_SYNC); | 102 manager_->ShutdownOnSyncThread(STOP_SYNC); |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 | 105 |
| 105 base::ScopedTempDir temp_dir_; | 106 base::ScopedTempDir temp_dir_; |
| 106 base::MessageLoop loop_; // Needed for WeakHandle | 107 base::MessageLoop loop_; // Needed for WeakHandle |
| 107 SyncManager* manager_; | 108 SyncManager* manager_; |
| 108 }; | 109 }; |
| 109 | 110 |
| 110 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) { | 111 TEST_F(SyncBackupManagerTest, NormalizeEntry) { |
| 111 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); | 112 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 112 InitManager(manager.get(), STORAGE_ON_DISK); | 113 InitManager(manager.get(), InternalComponentsFactory::STORAGE_IN_MEMORY); |
| 113 | 114 |
| 114 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); | 115 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 115 | 116 |
| 116 { | 117 { |
| 117 // New entry is local and unsynced at first. | 118 // New entry is local and unsynced at first. |
| 118 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 119 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 119 ReadNode pref(&trans); | 120 ReadNode pref(&trans); |
| 120 EXPECT_EQ(BaseNode::INIT_OK, | 121 EXPECT_EQ(BaseNode::INIT_OK, |
| 121 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 122 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 122 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); | 123 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); |
| 123 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); | 124 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); |
| 124 } | 125 } |
| 125 | 126 |
| 126 manager->SaveChanges(); | 127 manager->SaveChanges(); |
| 127 | 128 |
| 128 { | 129 { |
| 129 // New entry has server ID and unsynced bit is cleared after saving. | 130 // New entry has server ID and unsynced bit is cleared after saving. |
| 130 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 131 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 131 ReadNode pref(&trans); | 132 ReadNode pref(&trans); |
| 132 EXPECT_EQ(BaseNode::INIT_OK, | 133 EXPECT_EQ(BaseNode::INIT_OK, |
| 133 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 134 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 134 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); | 135 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| 135 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); | 136 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| 136 } | 137 } |
| 137 manager->ShutdownOnSyncThread(STOP_SYNC); | 138 } |
| 139 |
| 140 TEST_F(SyncBackupManagerTest, PersistWithSwitchToSyncShutdown) { |
| 141 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 142 InitManager(manager.get(), |
| 143 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED); |
| 144 |
| 145 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 146 manager->SaveChanges(); |
| 147 manager->ShutdownOnSyncThread(SWITCH_MODE_SYNC); |
| 138 | 148 |
| 139 // Reopen db to verify entry is persisted. | 149 // Reopen db to verify entry is persisted. |
| 140 manager.reset(new SyncBackupManager); | 150 manager.reset(new SyncBackupManager); |
| 141 InitManager(manager.get(), STORAGE_ON_DISK); | 151 InitManager(manager.get(), InternalComponentsFactory::STORAGE_ON_DISK); |
| 142 { | 152 { |
| 143 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 153 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 144 ReadNode pref(&trans); | 154 ReadNode pref(&trans); |
| 145 EXPECT_EQ(BaseNode::INIT_OK, | 155 EXPECT_EQ(BaseNode::INIT_OK, |
| 146 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 156 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 147 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); | 157 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| 148 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); | 158 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| 149 } | 159 } |
| 150 } | 160 } |
| 151 | 161 |
| 162 TEST_F(SyncBackupManagerTest, DontPersistWithOtherShutdown) { |
| 163 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 164 InitManager(manager.get(), |
| 165 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED); |
| 166 |
| 167 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 168 manager->SaveChanges(); |
| 169 manager->ShutdownOnSyncThread(STOP_SYNC); |
| 170 EXPECT_FALSE(base::PathExists( |
| 171 temp_dir_.path().Append(syncable::Directory::kSyncDatabaseFilename))); |
| 172 } |
| 173 |
| 152 TEST_F(SyncBackupManagerTest, FailToInitialize) { | 174 TEST_F(SyncBackupManagerTest, FailToInitialize) { |
| 153 // Test graceful shutdown on initialization failure. | 175 // Test graceful shutdown on initialization failure. |
| 154 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); | 176 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 155 InitManager(manager.get(), STORAGE_INVALID); | 177 InitManager(manager.get(), InternalComponentsFactory::STORAGE_INVALID); |
| 156 } | 178 } |
| 157 | 179 |
| 158 } // anonymous namespace | 180 } // anonymous namespace |
| 159 | 181 |
| 160 } // namespace syncer | 182 } // namespace syncer |
| OLD | NEW |