OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "chrome/browser/extensions/api/storage/leveldb_settings_storage_factory
.h" | 11 #include "chrome/browser/extensions/api/storage/leveldb_settings_storage_factory
.h" |
12 #include "chrome/browser/extensions/api/storage/settings_frontend.h" | 12 #include "chrome/browser/extensions/api/storage/settings_frontend.h" |
13 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" | 13 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" |
14 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 14 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" |
15 #include "chrome/browser/extensions/api/storage/settings_test_util.h" | 15 #include "chrome/browser/extensions/api/storage/settings_test_util.h" |
16 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" | 16 #include "chrome/browser/extensions/api/storage/syncable_settings_storage.h" |
17 #include "chrome/browser/extensions/test_extension_service.h" | 17 #include "chrome/browser/extensions/test_extension_service.h" |
18 #include "chrome/browser/value_store/testing_value_store.h" | 18 #include "chrome/browser/value_store/testing_value_store.h" |
19 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
20 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
21 #include "extensions/common/manifest.h" | 21 #include "extensions/common/manifest.h" |
22 #include "sync/api/sync_change_processor.h" | 22 #include "sync/api/sync_change_processor.h" |
| 23 #include "sync/api/sync_change_processor_delegator.h" |
23 #include "sync/api/sync_error_factory.h" | 24 #include "sync/api/sync_error_factory.h" |
24 #include "sync/api/sync_error_factory_mock.h" | 25 #include "sync/api/sync_error_factory_mock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 using base::DictionaryValue; | 28 using base::DictionaryValue; |
28 using base::ListValue; | 29 using base::ListValue; |
29 using base::Value; | 30 using base::Value; |
30 using content::BrowserThread; | 31 using content::BrowserThread; |
31 | 32 |
32 namespace extensions { | 33 namespace extensions { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 extension_id << "/" << key << " (out of " << changes_.size() << ")"; | 151 extension_id << "/" << key << " (out of " << changes_.size() << ")"; |
151 } | 152 } |
152 return matching_changes[0]; | 153 return matching_changes[0]; |
153 } | 154 } |
154 | 155 |
155 private: | 156 private: |
156 SettingSyncDataList changes_; | 157 SettingSyncDataList changes_; |
157 bool fail_all_requests_; | 158 bool fail_all_requests_; |
158 }; | 159 }; |
159 | 160 |
160 class SyncChangeProcessorDelegate : public syncer::SyncChangeProcessor { | |
161 public: | |
162 explicit SyncChangeProcessorDelegate(syncer::SyncChangeProcessor* recipient) | |
163 : recipient_(recipient) { | |
164 DCHECK(recipient_); | |
165 } | |
166 virtual ~SyncChangeProcessorDelegate() {} | |
167 | |
168 // syncer::SyncChangeProcessor implementation. | |
169 virtual syncer::SyncError ProcessSyncChanges( | |
170 const tracked_objects::Location& from_here, | |
171 const syncer::SyncChangeList& change_list) OVERRIDE { | |
172 return recipient_->ProcessSyncChanges(from_here, change_list); | |
173 } | |
174 | |
175 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const | |
176 OVERRIDE { | |
177 return recipient_->GetAllSyncData(type); | |
178 } | |
179 | |
180 private: | |
181 // The recipient of all sync changes. | |
182 syncer::SyncChangeProcessor* recipient_; | |
183 | |
184 DISALLOW_COPY_AND_ASSIGN(SyncChangeProcessorDelegate); | |
185 }; | |
186 | |
187 // SettingsStorageFactory which always returns TestingValueStore objects, | 161 // SettingsStorageFactory which always returns TestingValueStore objects, |
188 // and allows individually created objects to be returned. | 162 // and allows individually created objects to be returned. |
189 class TestingValueStoreFactory : public SettingsStorageFactory { | 163 class TestingValueStoreFactory : public SettingsStorageFactory { |
190 public: | 164 public: |
191 TestingValueStore* GetExisting(const std::string& extension_id) { | 165 TestingValueStore* GetExisting(const std::string& extension_id) { |
192 DCHECK(created_.count(extension_id)); | 166 DCHECK(created_.count(extension_id)); |
193 return created_[extension_id]; | 167 return created_[extension_id]; |
194 } | 168 } |
195 | 169 |
196 // SettingsStorageFactory implementation. | 170 // SettingsStorageFactory implementation. |
(...skipping 16 matching lines...) Expand all Loading... |
213 | 187 |
214 } // namespace | 188 } // namespace |
215 | 189 |
216 class ExtensionSettingsSyncTest : public testing::Test { | 190 class ExtensionSettingsSyncTest : public testing::Test { |
217 public: | 191 public: |
218 ExtensionSettingsSyncTest() | 192 ExtensionSettingsSyncTest() |
219 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), | 193 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), |
220 file_thread_(BrowserThread::FILE, base::MessageLoop::current()), | 194 file_thread_(BrowserThread::FILE, base::MessageLoop::current()), |
221 storage_factory_(new util::ScopedSettingsStorageFactory()), | 195 storage_factory_(new util::ScopedSettingsStorageFactory()), |
222 sync_processor_(new MockSyncChangeProcessor), | 196 sync_processor_(new MockSyncChangeProcessor), |
223 sync_processor_delegate_(new SyncChangeProcessorDelegate( | 197 sync_processor_delegate_( |
224 sync_processor_.get())) {} | 198 new syncer::SyncChangeProcessorDelegator(sync_processor_.get())) {} |
225 | 199 |
226 virtual void SetUp() OVERRIDE { | 200 virtual void SetUp() OVERRIDE { |
227 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 201 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
228 profile_.reset(new util::MockProfile(temp_dir_.path())); | 202 profile_.reset(new util::MockProfile(temp_dir_.path())); |
229 storage_factory_->Reset(new LeveldbSettingsStorageFactory()); | 203 storage_factory_->Reset(new LeveldbSettingsStorageFactory()); |
230 frontend_.reset( | 204 frontend_.reset( |
231 SettingsFrontend::Create(storage_factory_.get(), profile_.get())); | 205 SettingsFrontend::Create(storage_factory_.get(), profile_.get())); |
232 } | 206 } |
233 | 207 |
234 virtual void TearDown() OVERRIDE { | 208 virtual void TearDown() OVERRIDE { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 // Need these so that the DCHECKs for running on FILE or UI threads pass. | 248 // Need these so that the DCHECKs for running on FILE or UI threads pass. |
275 base::MessageLoop message_loop_; | 249 base::MessageLoop message_loop_; |
276 content::TestBrowserThread ui_thread_; | 250 content::TestBrowserThread ui_thread_; |
277 content::TestBrowserThread file_thread_; | 251 content::TestBrowserThread file_thread_; |
278 | 252 |
279 base::ScopedTempDir temp_dir_; | 253 base::ScopedTempDir temp_dir_; |
280 scoped_ptr<util::MockProfile> profile_; | 254 scoped_ptr<util::MockProfile> profile_; |
281 scoped_ptr<SettingsFrontend> frontend_; | 255 scoped_ptr<SettingsFrontend> frontend_; |
282 scoped_refptr<util::ScopedSettingsStorageFactory> storage_factory_; | 256 scoped_refptr<util::ScopedSettingsStorageFactory> storage_factory_; |
283 scoped_ptr<MockSyncChangeProcessor> sync_processor_; | 257 scoped_ptr<MockSyncChangeProcessor> sync_processor_; |
284 scoped_ptr<SyncChangeProcessorDelegate> sync_processor_delegate_; | 258 scoped_ptr<syncer::SyncChangeProcessorDelegator> sync_processor_delegate_; |
285 }; | 259 }; |
286 | 260 |
287 // Get a semblance of coverage for both EXTENSION_SETTINGS and APP_SETTINGS | 261 // Get a semblance of coverage for both EXTENSION_SETTINGS and APP_SETTINGS |
288 // sync by roughly alternative which one to test. | 262 // sync by roughly alternative which one to test. |
289 | 263 |
290 TEST_F(ExtensionSettingsSyncTest, NoDataDoesNotInvokeSync) { | 264 TEST_F(ExtensionSettingsSyncTest, NoDataDoesNotInvokeSync) { |
291 syncer::ModelType model_type = syncer::EXTENSION_SETTINGS; | 265 syncer::ModelType model_type = syncer::EXTENSION_SETTINGS; |
292 Manifest::Type type = Manifest::TYPE_EXTENSION; | 266 Manifest::Type type = Manifest::TYPE_EXTENSION; |
293 | 267 |
294 EXPECT_EQ(0u, GetAllSyncData(model_type).size()); | 268 EXPECT_EQ(0u, GetAllSyncData(model_type).size()); |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), | 660 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), |
687 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 661 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
688 GetSyncableService(syncer::EXTENSION_SETTINGS)-> | 662 GetSyncableService(syncer::EXTENSION_SETTINGS)-> |
689 StopSyncing(syncer::EXTENSION_SETTINGS); | 663 StopSyncing(syncer::EXTENSION_SETTINGS); |
690 EXPECT_EQ(0u, sync_processor_->changes().size()); | 664 EXPECT_EQ(0u, sync_processor_->changes().size()); |
691 | 665 |
692 sync_data.clear(); | 666 sync_data.clear(); |
693 sync_data.push_back(settings_sync_util::CreateData( | 667 sync_data.push_back(settings_sync_util::CreateData( |
694 "s2", "bar", value2, syncer::APP_SETTINGS)); | 668 "s2", "bar", value2, syncer::APP_SETTINGS)); |
695 | 669 |
696 scoped_ptr<SyncChangeProcessorDelegate> app_settings_delegate_( | 670 scoped_ptr<syncer::SyncChangeProcessorDelegator> app_settings_delegate_( |
697 new SyncChangeProcessorDelegate(sync_processor_.get())); | 671 new syncer::SyncChangeProcessorDelegator(sync_processor_.get())); |
698 GetSyncableService(syncer::APP_SETTINGS)->MergeDataAndStartSyncing( | 672 GetSyncableService(syncer::APP_SETTINGS)->MergeDataAndStartSyncing( |
699 syncer::APP_SETTINGS, | 673 syncer::APP_SETTINGS, |
700 sync_data, | 674 sync_data, |
701 app_settings_delegate_.PassAs<syncer::SyncChangeProcessor>(), | 675 app_settings_delegate_.PassAs<syncer::SyncChangeProcessor>(), |
702 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 676 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
703 GetSyncableService(syncer::APP_SETTINGS)-> | 677 GetSyncableService(syncer::APP_SETTINGS)-> |
704 StopSyncing(syncer::APP_SETTINGS); | 678 StopSyncing(syncer::APP_SETTINGS); |
705 EXPECT_EQ(0u, sync_processor_->changes().size()); | 679 EXPECT_EQ(0u, sync_processor_->changes().size()); |
706 } | 680 } |
707 | 681 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 } | 813 } |
840 { | 814 { |
841 base::DictionaryValue dict; | 815 base::DictionaryValue dict; |
842 dict.Set("bar", fooValue.DeepCopy()); | 816 dict.Set("bar", fooValue.DeepCopy()); |
843 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); | 817 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); |
844 } | 818 } |
845 | 819 |
846 // Restarting sync should make bad start syncing again. | 820 // Restarting sync should make bad start syncing again. |
847 sync_processor_->ClearChanges(); | 821 sync_processor_->ClearChanges(); |
848 GetSyncableService(model_type)->StopSyncing(model_type); | 822 GetSyncableService(model_type)->StopSyncing(model_type); |
849 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( | 823 sync_processor_delegate_.reset( |
850 sync_processor_.get())); | 824 new syncer::SyncChangeProcessorDelegator(sync_processor_.get())); |
851 GetSyncableService(model_type)->MergeDataAndStartSyncing( | 825 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
852 model_type, | 826 model_type, |
853 syncer::SyncDataList(), | 827 syncer::SyncDataList(), |
854 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), | 828 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), |
855 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 829 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
856 | 830 |
857 // Local settings will have been pushed to sync, since it's empty (in this | 831 // Local settings will have been pushed to sync, since it's empty (in this |
858 // test; presumably it wouldn't be live, since we've been getting changes). | 832 // test; presumably it wouldn't be live, since we've been getting changes). |
859 EXPECT_EQ( | 833 EXPECT_EQ( |
860 syncer::SyncChange::ACTION_ADD, | 834 syncer::SyncChange::ACTION_ADD, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 base::DictionaryValue dict; | 1094 base::DictionaryValue dict; |
1121 dict.Set("foo", fooValue.DeepCopy()); | 1095 dict.Set("foo", fooValue.DeepCopy()); |
1122 dict.Set("bar", barValue.DeepCopy()); | 1096 dict.Set("bar", barValue.DeepCopy()); |
1123 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); | 1097 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); |
1124 } | 1098 } |
1125 | 1099 |
1126 // Re-enabling sync without failing should cause the local changes from bad | 1100 // Re-enabling sync without failing should cause the local changes from bad |
1127 // to be pushed to sync successfully, as should future changes to bad. | 1101 // to be pushed to sync successfully, as should future changes to bad. |
1128 sync_processor_->ClearChanges(); | 1102 sync_processor_->ClearChanges(); |
1129 GetSyncableService(model_type)->StopSyncing(model_type); | 1103 GetSyncableService(model_type)->StopSyncing(model_type); |
1130 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( | 1104 sync_processor_delegate_.reset( |
1131 sync_processor_.get())); | 1105 new syncer::SyncChangeProcessorDelegator(sync_processor_.get())); |
1132 GetSyncableService(model_type)->MergeDataAndStartSyncing( | 1106 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
1133 model_type, | 1107 model_type, |
1134 syncer::SyncDataList(), | 1108 syncer::SyncDataList(), |
1135 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), | 1109 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), |
1136 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 1110 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
1137 | 1111 |
1138 EXPECT_EQ( | 1112 EXPECT_EQ( |
1139 syncer::SyncChange::ACTION_ADD, | 1113 syncer::SyncChange::ACTION_ADD, |
1140 sync_processor_->GetOnlyChange("good", "foo").change_type()); | 1114 sync_processor_->GetOnlyChange("good", "foo").change_type()); |
1141 EXPECT_EQ( | 1115 EXPECT_EQ( |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1214 } | 1188 } |
1215 { | 1189 { |
1216 base::DictionaryValue dict; | 1190 base::DictionaryValue dict; |
1217 dict.Set("foo", barValue.DeepCopy()); | 1191 dict.Set("foo", barValue.DeepCopy()); |
1218 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); | 1192 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); |
1219 } | 1193 } |
1220 | 1194 |
1221 // Restarting sync makes everything work again. | 1195 // Restarting sync makes everything work again. |
1222 sync_processor_->ClearChanges(); | 1196 sync_processor_->ClearChanges(); |
1223 GetSyncableService(model_type)->StopSyncing(model_type); | 1197 GetSyncableService(model_type)->StopSyncing(model_type); |
1224 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( | 1198 sync_processor_delegate_.reset( |
1225 sync_processor_.get())); | 1199 new syncer::SyncChangeProcessorDelegator(sync_processor_.get())); |
1226 GetSyncableService(model_type)->MergeDataAndStartSyncing( | 1200 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
1227 model_type, | 1201 model_type, |
1228 syncer::SyncDataList(), | 1202 syncer::SyncDataList(), |
1229 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), | 1203 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), |
1230 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 1204 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
1231 | 1205 |
1232 EXPECT_EQ( | 1206 EXPECT_EQ( |
1233 syncer::SyncChange::ACTION_ADD, | 1207 syncer::SyncChange::ACTION_ADD, |
1234 sync_processor_->GetOnlyChange("good", "foo").change_type()); | 1208 sync_processor_->GetOnlyChange("good", "foo").change_type()); |
1235 EXPECT_EQ( | 1209 EXPECT_EQ( |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1311 } | 1285 } |
1312 { | 1286 { |
1313 base::DictionaryValue dict; | 1287 base::DictionaryValue dict; |
1314 dict.Set("foo", barValue.DeepCopy()); | 1288 dict.Set("foo", barValue.DeepCopy()); |
1315 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); | 1289 EXPECT_PRED_FORMAT2(SettingsEq, dict, bad->Get()); |
1316 } | 1290 } |
1317 | 1291 |
1318 // Restarting sync makes everything work again. | 1292 // Restarting sync makes everything work again. |
1319 sync_processor_->ClearChanges(); | 1293 sync_processor_->ClearChanges(); |
1320 GetSyncableService(model_type)->StopSyncing(model_type); | 1294 GetSyncableService(model_type)->StopSyncing(model_type); |
1321 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( | 1295 sync_processor_delegate_.reset( |
1322 sync_processor_.get())); | 1296 new syncer::SyncChangeProcessorDelegator(sync_processor_.get())); |
1323 GetSyncableService(model_type)->MergeDataAndStartSyncing( | 1297 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
1324 model_type, | 1298 model_type, |
1325 syncer::SyncDataList(), | 1299 syncer::SyncDataList(), |
1326 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), | 1300 sync_processor_delegate_.PassAs<syncer::SyncChangeProcessor>(), |
1327 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 1301 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
1328 | 1302 |
1329 EXPECT_EQ( | 1303 EXPECT_EQ( |
1330 syncer::SyncChange::ACTION_ADD, | 1304 syncer::SyncChange::ACTION_ADD, |
1331 sync_processor_->GetOnlyChange("good", "foo").change_type()); | 1305 sync_processor_->GetOnlyChange("good", "foo").change_type()); |
1332 EXPECT_EQ( | 1306 EXPECT_EQ( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1433 ASSERT_EQ(1u, sync_processor_->changes().size()); | 1407 ASSERT_EQ(1u, sync_processor_->changes().size()); |
1434 SettingSyncData sync_data = sync_processor_->changes()[0]; | 1408 SettingSyncData sync_data = sync_processor_->changes()[0]; |
1435 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, sync_data.change_type()); | 1409 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, sync_data.change_type()); |
1436 EXPECT_EQ("ext", sync_data.extension_id()); | 1410 EXPECT_EQ("ext", sync_data.extension_id()); |
1437 EXPECT_EQ("key.with.spot", sync_data.key()); | 1411 EXPECT_EQ("key.with.spot", sync_data.key()); |
1438 EXPECT_TRUE(sync_data.value().Equals(string_value.get())); | 1412 EXPECT_TRUE(sync_data.value().Equals(string_value.get())); |
1439 } | 1413 } |
1440 } | 1414 } |
1441 | 1415 |
1442 } // namespace extensions | 1416 } // namespace extensions |
OLD | NEW |