| 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 &storage_used_)); |
| 69 manager->Init(&args); | 71 manager->Init(&args); |
| 72 EXPECT_EQ(InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED, |
| 73 storage_used_); |
| 70 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); | 74 loop_.PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 71 run_loop.Run(); | 75 run_loop.Run(); |
| 72 } | 76 } |
| 73 | 77 |
| 74 void CreateEntry(UserShare* user_share, ModelType type, | 78 void CreateEntry(UserShare* user_share, ModelType type, |
| 75 const std::string& client_tag) { | 79 const std::string& client_tag) { |
| 76 WriteTransaction trans(FROM_HERE, user_share); | 80 WriteTransaction trans(FROM_HERE, user_share); |
| 77 ReadNode type_root(&trans); | 81 ReadNode type_root(&trans); |
| 78 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); | 82 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type)); |
| 79 | 83 |
| 80 WriteNode node(&trans); | 84 WriteNode node(&trans); |
| 81 EXPECT_EQ(WriteNode::INIT_SUCCESS, | 85 EXPECT_EQ(WriteNode::INIT_SUCCESS, |
| 82 node.InitUniqueByCreation(type, type_root, client_tag)); | 86 node.InitUniqueByCreation(type, type_root, client_tag)); |
| 83 } | 87 } |
| 84 | 88 |
| 85 private: | |
| 86 void ConfigureSyncer() { | 89 void ConfigureSyncer() { |
| 87 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT, | 90 manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT, |
| 88 ModelTypeSet(SEARCH_ENGINES), | 91 ModelTypeSet(SEARCH_ENGINES), |
| 89 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), | 92 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(), |
| 90 ModelSafeRoutingInfo(), | 93 ModelSafeRoutingInfo(), |
| 91 base::Bind(&OnConfigDone, true), | 94 base::Bind(&OnConfigDone, true), |
| 92 base::Bind(&OnConfigDone, false)); | 95 base::Bind(&OnConfigDone, false)); |
| 93 } | 96 } |
| 94 | 97 |
| 95 void HandleInit(bool success) { | 98 void HandleInit(bool success) { |
| 96 if (success) { | 99 if (success) { |
| 97 loop_.PostTask(FROM_HERE, | 100 loop_.PostTask(FROM_HERE, |
| 98 base::Bind(&SyncBackupManagerTest::ConfigureSyncer, | 101 base::Bind(&SyncBackupManagerTest::ConfigureSyncer, |
| 99 base::Unretained(this))); | 102 base::Unretained(this))); |
| 100 } else { | 103 } else { |
| 101 manager_->ShutdownOnSyncThread(STOP_SYNC); | 104 manager_->ShutdownOnSyncThread(STOP_SYNC); |
| 102 } | 105 } |
| 103 } | 106 } |
| 104 | 107 |
| 105 base::ScopedTempDir temp_dir_; | 108 base::ScopedTempDir temp_dir_; |
| 106 base::MessageLoop loop_; // Needed for WeakHandle | 109 base::MessageLoop loop_; // Needed for WeakHandle |
| 107 SyncManager* manager_; | 110 SyncManager* manager_; |
| 111 InternalComponentsFactory::StorageOption storage_used_; |
| 108 }; | 112 }; |
| 109 | 113 |
| 110 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) { | 114 TEST_F(SyncBackupManagerTest, NormalizeEntry) { |
| 111 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); | 115 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 112 InitManager(manager.get(), STORAGE_ON_DISK); | 116 InitManager(manager.get(), InternalComponentsFactory::STORAGE_IN_MEMORY); |
| 113 | 117 |
| 114 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); | 118 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 115 | 119 |
| 116 { | 120 { |
| 117 // New entry is local and unsynced at first. | 121 // New entry is local and unsynced at first. |
| 118 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 122 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 119 ReadNode pref(&trans); | 123 ReadNode pref(&trans); |
| 120 EXPECT_EQ(BaseNode::INIT_OK, | 124 EXPECT_EQ(BaseNode::INIT_OK, |
| 121 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 125 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 122 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); | 126 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows()); |
| 123 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); | 127 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced()); |
| 124 } | 128 } |
| 125 | 129 |
| 126 manager->SaveChanges(); | 130 manager->SaveChanges(); |
| 127 | 131 |
| 128 { | 132 { |
| 129 // New entry has server ID and unsynced bit is cleared after saving. | 133 // New entry has server ID and unsynced bit is cleared after saving. |
| 130 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 134 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 131 ReadNode pref(&trans); | 135 ReadNode pref(&trans); |
| 132 EXPECT_EQ(BaseNode::INIT_OK, | 136 EXPECT_EQ(BaseNode::INIT_OK, |
| 133 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 137 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 134 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); | 138 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| 135 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); | 139 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| 136 } | 140 } |
| 137 manager->ShutdownOnSyncThread(STOP_SYNC); | 141 } |
| 142 |
| 143 TEST_F(SyncBackupManagerTest, PersistWithSwitchToSyncShutdown) { |
| 144 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 145 InitManager(manager.get(), |
| 146 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED); |
| 147 |
| 148 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 149 manager->SaveChanges(); |
| 150 manager->ShutdownOnSyncThread(SWITCH_MODE_SYNC); |
| 138 | 151 |
| 139 // Reopen db to verify entry is persisted. | 152 // Reopen db to verify entry is persisted. |
| 140 manager.reset(new SyncBackupManager); | 153 manager.reset(new SyncBackupManager); |
| 141 InitManager(manager.get(), STORAGE_ON_DISK); | 154 InitManager(manager.get(), InternalComponentsFactory::STORAGE_ON_DISK); |
| 142 { | 155 { |
| 143 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); | 156 ReadTransaction trans(FROM_HERE, manager->GetUserShare()); |
| 144 ReadNode pref(&trans); | 157 ReadNode pref(&trans); |
| 145 EXPECT_EQ(BaseNode::INIT_OK, | 158 EXPECT_EQ(BaseNode::INIT_OK, |
| 146 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); | 159 pref.InitByClientTagLookup(SEARCH_ENGINES, "test")); |
| 147 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); | 160 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows()); |
| 148 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); | 161 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced()); |
| 149 } | 162 } |
| 150 } | 163 } |
| 151 | 164 |
| 165 TEST_F(SyncBackupManagerTest, DontPersistWithOtherShutdown) { |
| 166 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 167 InitManager(manager.get(), |
| 168 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED); |
| 169 |
| 170 CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test"); |
| 171 manager->SaveChanges(); |
| 172 manager->ShutdownOnSyncThread(STOP_SYNC); |
| 173 EXPECT_FALSE(base::PathExists( |
| 174 temp_dir_.path().Append(syncable::Directory::kSyncDatabaseFilename))); |
| 175 } |
| 176 |
| 152 TEST_F(SyncBackupManagerTest, FailToInitialize) { | 177 TEST_F(SyncBackupManagerTest, FailToInitialize) { |
| 153 // Test graceful shutdown on initialization failure. | 178 // Test graceful shutdown on initialization failure. |
| 154 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); | 179 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager); |
| 155 InitManager(manager.get(), STORAGE_INVALID); | 180 InitManager(manager.get(), InternalComponentsFactory::STORAGE_INVALID); |
| 156 } | 181 } |
| 157 | 182 |
| 158 } // anonymous namespace | 183 } // anonymous namespace |
| 159 | 184 |
| 160 } // namespace syncer | 185 } // namespace syncer |
| OLD | NEW |