| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 #include "chrome/browser/extensions/extension_settings_backend.h" | |
| 15 #include "chrome/browser/extensions/extension_settings_frontend.h" | 14 #include "chrome/browser/extensions/extension_settings_frontend.h" |
| 16 #include "chrome/browser/extensions/extension_settings_storage_cache.h" | 15 #include "chrome/browser/extensions/extension_settings_storage_cache.h" |
| 17 #include "chrome/browser/extensions/extension_settings_sync_util.h" | 16 #include "chrome/browser/extensions/extension_settings_sync_util.h" |
| 18 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" | 17 #include "chrome/browser/extensions/syncable_extension_settings_storage.h" |
| 18 #include "chrome/browser/extensions/extension_settings_test_util.h" |
| 19 #include "chrome/browser/sync/api/sync_change_processor.h" | 19 #include "chrome/browser/sync/api/sync_change_processor.h" |
| 20 #include "chrome/test/base/testing_profile.h" | |
| 21 #include "content/test/test_browser_thread.h" | 20 #include "content/test/test_browser_thread.h" |
| 22 | 21 |
| 23 // TODO(kalman): Integration tests for sync. | 22 // TODO(kalman): Integration tests for sync. |
| 24 | 23 |
| 24 using namespace extension_settings_test_util; |
| 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // Gets the pretty-printed JSON for a value. | 28 // Gets the pretty-printed JSON for a value. |
| 28 static std::string GetJson(const Value& value) { | 29 static std::string GetJson(const Value& value) { |
| 29 std::string json; | 30 std::string json; |
| 30 base::JSONWriter::Write(&value, true, &json); | 31 base::JSONWriter::Write(&value, true, &json); |
| 31 return json; | 32 return json; |
| 32 } | 33 } |
| 33 | 34 |
| 34 // Returns whether two Values are equal. | 35 // Returns whether two Values are equal. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 ADD_FAILURE() << matching_changes.size() << " matching changes for " << | 113 ADD_FAILURE() << matching_changes.size() << " matching changes for " << |
| 113 extension_id << "/" << key << " (out of " << changes_.size() << ")"; | 114 extension_id << "/" << key << " (out of " << changes_.size() << ")"; |
| 114 } | 115 } |
| 115 return matching_changes[0]; | 116 return matching_changes[0]; |
| 116 } | 117 } |
| 117 | 118 |
| 118 private: | 119 private: |
| 119 ExtensionSettingSyncDataList changes_; | 120 ExtensionSettingSyncDataList changes_; |
| 120 }; | 121 }; |
| 121 | 122 |
| 122 // To be called as a callback from ExtensionSettingsFrontend::RunWithSettings. | 123 void AssignSettingsService(SyncableService** dst, SyncableService* src) { |
| 123 void AssignSettings( | |
| 124 ExtensionSettingsBackend** dst, ExtensionSettingsBackend* src) { | |
| 125 *dst = src; | 124 *dst = src; |
| 126 } | 125 } |
| 127 | 126 |
| 128 } // namespace | 127 } // namespace |
| 129 | 128 |
| 130 class ExtensionSettingsSyncTest : public testing::Test { | 129 class ExtensionSettingsSyncTest : public testing::Test { |
| 131 public: | 130 public: |
| 132 ExtensionSettingsSyncTest() | 131 ExtensionSettingsSyncTest() |
| 133 : ui_thread_(BrowserThread::UI, MessageLoop::current()), | 132 : ui_thread_(BrowserThread::UI, MessageLoop::current()), |
| 134 file_thread_(BrowserThread::FILE, MessageLoop::current()), | 133 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} |
| 135 frontend_(&profile_), | 134 |
| 136 backend_(NULL) { | 135 virtual void SetUp() OVERRIDE { |
| 136 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 137 profile_.reset(new MockProfile(temp_dir_.path())); |
| 138 frontend_.reset(new ExtensionSettingsFrontend(profile_.get())); |
| 137 } | 139 } |
| 138 | 140 |
| 139 virtual void SetUp() OVERRIDE { | 141 virtual void TearDown() OVERRIDE { |
| 140 frontend_.RunWithBackend(base::Bind(&AssignSettings, &backend_)); | 142 frontend_.reset(); |
| 141 MessageLoop::current()->RunAllPending(); | 143 profile_.reset(); |
| 142 ASSERT_TRUE(backend_ != NULL); | |
| 143 } | 144 } |
| 144 | 145 |
| 145 protected: | 146 protected: |
| 146 // Creates a new extension storage object and adds a record of the extension | 147 // Adds a record of an extension or app to the extension service, then returns |
| 147 // to the extension service. | 148 // its storage area. |
| 148 SyncableExtensionSettingsStorage* GetStorage( | 149 ExtensionSettingsStorage* AddExtensionAndGetStorage( |
| 149 const std::string& extension_id) { | 150 const std::string& id, Extension::Type type) { |
| 150 return static_cast<SyncableExtensionSettingsStorage*>( | 151 profile_->GetMockExtensionService()->AddExtension(id, type); |
| 151 backend_->GetStorage(extension_id)); | 152 return GetStorage(id, frontend_.get()); |
| 152 } | 153 } |
| 153 | 154 |
| 154 // Gets all the sync data from |backend_| as a map from extension id to its | 155 // Gets the SyncableService for the given sync type. |
| 155 // sync data. | 156 SyncableService* GetSyncableService(syncable::ModelType model_type) { |
| 156 std::map<std::string, ExtensionSettingSyncDataList> GetAllSyncData() { | 157 SyncableService* settings_service = NULL; |
| 158 frontend_->RunWithSyncableService( |
| 159 model_type, base::Bind(&AssignSettingsService, &settings_service)); |
| 160 MessageLoop::current()->RunAllPending(); |
| 161 return settings_service; |
| 162 } |
| 163 |
| 164 // Gets all the sync data from the SyncableService for a sync type as a map |
| 165 // from extension id to its sync data. |
| 166 std::map<std::string, ExtensionSettingSyncDataList> GetAllSyncData( |
| 167 syncable::ModelType model_type) { |
| 157 SyncDataList as_list = | 168 SyncDataList as_list = |
| 158 backend_->GetAllSyncData(syncable::EXTENSION_SETTINGS); | 169 GetSyncableService(model_type)->GetAllSyncData(model_type); |
| 159 std::map<std::string, ExtensionSettingSyncDataList> as_map; | 170 std::map<std::string, ExtensionSettingSyncDataList> as_map; |
| 160 for (SyncDataList::iterator it = as_list.begin(); | 171 for (SyncDataList::iterator it = as_list.begin(); |
| 161 it != as_list.end(); ++it) { | 172 it != as_list.end(); ++it) { |
| 162 ExtensionSettingSyncData sync_data(*it); | 173 ExtensionSettingSyncData sync_data(*it); |
| 163 as_map[sync_data.extension_id()].push_back(sync_data); | 174 as_map[sync_data.extension_id()].push_back(sync_data); |
| 164 } | 175 } |
| 165 return as_map; | 176 return as_map; |
| 166 } | 177 } |
| 167 | 178 |
| 168 // Need these so that the DCHECKs for running on FILE or UI threads pass. | 179 // Need these so that the DCHECKs for running on FILE or UI threads pass. |
| 169 MessageLoop message_loop_; | 180 MessageLoop message_loop_; |
| 170 content::TestBrowserThread ui_thread_; | 181 content::TestBrowserThread ui_thread_; |
| 171 content::TestBrowserThread file_thread_; | 182 content::TestBrowserThread file_thread_; |
| 172 | 183 |
| 184 ScopedTempDir temp_dir_; |
| 173 MockSyncChangeProcessor sync_; | 185 MockSyncChangeProcessor sync_; |
| 174 TestingProfile profile_; | 186 scoped_ptr<MockProfile> profile_; |
| 175 ExtensionSettingsFrontend frontend_; | 187 scoped_ptr<ExtensionSettingsFrontend> frontend_; |
| 176 | |
| 177 // Get from frontend_->RunWithBackend, so weak reference. | |
| 178 ExtensionSettingsBackend* backend_; | |
| 179 }; | 188 }; |
| 180 | 189 |
| 190 // Get a semblance of coverage for both EXTENSION_SETTINGS and APP_SETTINGS |
| 191 // sync by roughly alternative which one to test. |
| 192 |
| 181 TEST_F(ExtensionSettingsSyncTest, NoDataDoesNotInvokeSync) { | 193 TEST_F(ExtensionSettingsSyncTest, NoDataDoesNotInvokeSync) { |
| 182 ASSERT_EQ(0u, GetAllSyncData().size()); | 194 syncable::ModelType model_type = syncable::EXTENSION_SETTINGS; |
| 195 Extension::Type type = Extension::TYPE_EXTENSION; |
| 196 |
| 197 ASSERT_EQ(0u, GetAllSyncData(model_type).size()); |
| 183 | 198 |
| 184 // Have one extension created before sync is set up, the other created after. | 199 // Have one extension created before sync is set up, the other created after. |
| 185 GetStorage("s1"); | 200 AddExtensionAndGetStorage("s1", type); |
| 186 ASSERT_EQ(0u, GetAllSyncData().size()); | 201 ASSERT_EQ(0u, GetAllSyncData(model_type).size()); |
| 187 | 202 |
| 188 backend_->MergeDataAndStartSyncing( | 203 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
| 189 syncable::EXTENSION_SETTINGS, | 204 model_type, SyncDataList(), &sync_); |
| 190 SyncDataList(), | |
| 191 &sync_); | |
| 192 | 205 |
| 193 GetStorage("s2"); | 206 AddExtensionAndGetStorage("s2", type); |
| 194 ASSERT_EQ(0u, GetAllSyncData().size()); | 207 ASSERT_EQ(0u, GetAllSyncData(model_type).size()); |
| 195 | 208 |
| 196 backend_->StopSyncing(syncable::EXTENSION_SETTINGS); | 209 GetSyncableService(model_type)->StopSyncing(model_type); |
| 197 | 210 |
| 198 ASSERT_EQ(0u, sync_.changes().size()); | 211 ASSERT_EQ(0u, sync_.changes().size()); |
| 199 ASSERT_EQ(0u, GetAllSyncData().size()); | 212 ASSERT_EQ(0u, GetAllSyncData(model_type).size()); |
| 200 } | 213 } |
| 201 | 214 |
| 202 TEST_F(ExtensionSettingsSyncTest, InSyncDataDoesNotInvokeSync) { | 215 TEST_F(ExtensionSettingsSyncTest, InSyncDataDoesNotInvokeSync) { |
| 216 syncable::ModelType model_type = syncable::APP_SETTINGS; |
| 217 Extension::Type type = Extension::TYPE_PACKAGED_APP; |
| 218 |
| 203 StringValue value1("fooValue"); | 219 StringValue value1("fooValue"); |
| 204 ListValue value2; | 220 ListValue value2; |
| 205 value2.Append(StringValue::CreateStringValue("barValue")); | 221 value2.Append(StringValue::CreateStringValue("barValue")); |
| 206 | 222 |
| 207 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1"); | 223 ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type); |
| 208 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2"); | 224 ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type); |
| 209 | 225 |
| 210 storage1->Set("foo", value1); | 226 storage1->Set("foo", value1); |
| 211 storage2->Set("bar", value2); | 227 storage2->Set("bar", value2); |
| 212 | 228 |
| 213 std::map<std::string, ExtensionSettingSyncDataList> all_sync_data = | 229 std::map<std::string, ExtensionSettingSyncDataList> all_sync_data = |
| 214 GetAllSyncData(); | 230 GetAllSyncData(model_type); |
| 215 ASSERT_EQ(2u, all_sync_data.size()); | 231 ASSERT_EQ(2u, all_sync_data.size()); |
| 216 ASSERT_EQ(1u, all_sync_data["s1"].size()); | 232 ASSERT_EQ(1u, all_sync_data["s1"].size()); |
| 217 ASSERT_PRED_FORMAT2(ValuesEq, &value1, &all_sync_data["s1"][0].value()); | 233 ASSERT_PRED_FORMAT2(ValuesEq, &value1, &all_sync_data["s1"][0].value()); |
| 218 ASSERT_EQ(1u, all_sync_data["s2"].size()); | 234 ASSERT_EQ(1u, all_sync_data["s2"].size()); |
| 219 ASSERT_PRED_FORMAT2(ValuesEq, &value2, &all_sync_data["s2"][0].value()); | 235 ASSERT_PRED_FORMAT2(ValuesEq, &value2, &all_sync_data["s2"][0].value()); |
| 220 | 236 |
| 221 SyncDataList sync_data; | 237 SyncDataList sync_data; |
| 222 sync_data.push_back(extension_settings_sync_util::CreateData( | 238 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 223 "s1", "foo", value1)); | 239 "s1", "foo", value1)); |
| 224 sync_data.push_back(extension_settings_sync_util::CreateData( | 240 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 225 "s2", "bar", value2)); | 241 "s2", "bar", value2)); |
| 226 | 242 |
| 227 backend_->MergeDataAndStartSyncing( | 243 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
| 228 syncable::EXTENSION_SETTINGS, sync_data, &sync_); | 244 model_type, sync_data, &sync_); |
| 229 | 245 |
| 230 // Already in sync, so no changes. | 246 // Already in sync, so no changes. |
| 231 ASSERT_EQ(0u, sync_.changes().size()); | 247 ASSERT_EQ(0u, sync_.changes().size()); |
| 232 | 248 |
| 233 // Regression test: not-changing the synced value shouldn't result in a sync | 249 // Regression test: not-changing the synced value shouldn't result in a sync |
| 234 // change, and changing the synced value should result in an update. | 250 // change, and changing the synced value should result in an update. |
| 235 storage1->Set("foo", value1); | 251 storage1->Set("foo", value1); |
| 236 ASSERT_EQ(0u, sync_.changes().size()); | 252 ASSERT_EQ(0u, sync_.changes().size()); |
| 237 | 253 |
| 238 storage1->Set("foo", value2); | 254 storage1->Set("foo", value2); |
| 239 ASSERT_EQ(1u, sync_.changes().size()); | 255 ASSERT_EQ(1u, sync_.changes().size()); |
| 240 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo"); | 256 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo"); |
| 241 ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type()); | 257 ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type()); |
| 242 ASSERT_TRUE(value2.Equals(&change.value())); | 258 ASSERT_TRUE(value2.Equals(&change.value())); |
| 243 | 259 |
| 244 backend_->StopSyncing(syncable::EXTENSION_SETTINGS); | 260 GetSyncableService(model_type)->StopSyncing(model_type); |
| 245 } | 261 } |
| 246 | 262 |
| 247 TEST_F(ExtensionSettingsSyncTest, LocalDataWithNoSyncDataIsPushedToSync) { | 263 TEST_F(ExtensionSettingsSyncTest, LocalDataWithNoSyncDataIsPushedToSync) { |
| 264 syncable::ModelType model_type = syncable::EXTENSION_SETTINGS; |
| 265 Extension::Type type = Extension::TYPE_EXTENSION; |
| 266 |
| 248 StringValue value1("fooValue"); | 267 StringValue value1("fooValue"); |
| 249 ListValue value2; | 268 ListValue value2; |
| 250 value2.Append(StringValue::CreateStringValue("barValue")); | 269 value2.Append(StringValue::CreateStringValue("barValue")); |
| 251 | 270 |
| 252 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1"); | 271 ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type); |
| 253 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2"); | 272 ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type); |
| 254 | 273 |
| 255 storage1->Set("foo", value1); | 274 storage1->Set("foo", value1); |
| 256 storage2->Set("bar", value2); | 275 storage2->Set("bar", value2); |
| 257 | 276 |
| 258 backend_->MergeDataAndStartSyncing( | 277 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
| 259 syncable::EXTENSION_SETTINGS, SyncDataList(), &sync_); | 278 model_type, SyncDataList(), &sync_); |
| 260 | 279 |
| 261 // All settings should have been pushed to sync. | 280 // All settings should have been pushed to sync. |
| 262 ASSERT_EQ(2u, sync_.changes().size()); | 281 ASSERT_EQ(2u, sync_.changes().size()); |
| 263 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo"); | 282 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo"); |
| 264 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type()); | 283 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type()); |
| 265 ASSERT_TRUE(value1.Equals(&change.value())); | 284 ASSERT_TRUE(value1.Equals(&change.value())); |
| 266 change = sync_.GetOnlyChange("s2", "bar"); | 285 change = sync_.GetOnlyChange("s2", "bar"); |
| 267 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type()); | 286 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type()); |
| 268 ASSERT_TRUE(value2.Equals(&change.value())); | 287 ASSERT_TRUE(value2.Equals(&change.value())); |
| 269 | 288 |
| 270 backend_->StopSyncing(syncable::EXTENSION_SETTINGS); | 289 GetSyncableService(model_type)->StopSyncing(model_type); |
| 271 } | 290 } |
| 272 | 291 |
| 273 TEST_F(ExtensionSettingsSyncTest, AnySyncDataOverwritesLocalData) { | 292 TEST_F(ExtensionSettingsSyncTest, AnySyncDataOverwritesLocalData) { |
| 293 syncable::ModelType model_type = syncable::APP_SETTINGS; |
| 294 Extension::Type type = Extension::TYPE_PACKAGED_APP; |
| 295 |
| 274 StringValue value1("fooValue"); | 296 StringValue value1("fooValue"); |
| 275 ListValue value2; | 297 ListValue value2; |
| 276 value2.Append(StringValue::CreateStringValue("barValue")); | 298 value2.Append(StringValue::CreateStringValue("barValue")); |
| 277 | 299 |
| 278 // Maintain dictionaries mirrored to the expected values of the settings in | 300 // Maintain dictionaries mirrored to the expected values of the settings in |
| 279 // each storage area. | 301 // each storage area. |
| 280 DictionaryValue expected1, expected2; | 302 DictionaryValue expected1, expected2; |
| 281 | 303 |
| 282 // Pre-populate one of the storage areas. | 304 // Pre-populate one of the storage areas. |
| 283 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1"); | 305 ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type); |
| 284 storage1->Set("overwriteMe", value1); | 306 storage1->Set("overwriteMe", value1); |
| 285 | 307 |
| 286 SyncDataList sync_data; | 308 SyncDataList sync_data; |
| 287 sync_data.push_back(extension_settings_sync_util::CreateData( | 309 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 288 "s1", "foo", value1)); | 310 "s1", "foo", value1)); |
| 289 sync_data.push_back(extension_settings_sync_util::CreateData( | 311 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 290 "s2", "bar", value2)); | 312 "s2", "bar", value2)); |
| 291 backend_->MergeDataAndStartSyncing( | 313 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
| 292 syncable::EXTENSION_SETTINGS, sync_data, &sync_); | 314 model_type, sync_data, &sync_); |
| 293 expected1.Set("foo", value1.DeepCopy()); | 315 expected1.Set("foo", value1.DeepCopy()); |
| 294 expected2.Set("bar", value2.DeepCopy()); | 316 expected2.Set("bar", value2.DeepCopy()); |
| 295 | 317 |
| 296 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2"); | 318 ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type); |
| 297 | 319 |
| 298 // All changes should be local, so no sync changes. | 320 // All changes should be local, so no sync changes. |
| 299 ASSERT_EQ(0u, sync_.changes().size()); | 321 ASSERT_EQ(0u, sync_.changes().size()); |
| 300 | 322 |
| 301 // Sync settings should have been pushed to local settings. | 323 // Sync settings should have been pushed to local settings. |
| 302 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); | 324 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); |
| 303 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); | 325 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); |
| 304 | 326 |
| 305 backend_->StopSyncing(syncable::EXTENSION_SETTINGS); | 327 GetSyncableService(model_type)->StopSyncing(model_type); |
| 306 } | 328 } |
| 307 | 329 |
| 308 TEST_F(ExtensionSettingsSyncTest, ProcessSyncChanges) { | 330 TEST_F(ExtensionSettingsSyncTest, ProcessSyncChanges) { |
| 331 syncable::ModelType model_type = syncable::EXTENSION_SETTINGS; |
| 332 Extension::Type type = Extension::TYPE_EXTENSION; |
| 333 |
| 309 StringValue value1("fooValue"); | 334 StringValue value1("fooValue"); |
| 310 ListValue value2; | 335 ListValue value2; |
| 311 value2.Append(StringValue::CreateStringValue("barValue")); | 336 value2.Append(StringValue::CreateStringValue("barValue")); |
| 312 | 337 |
| 313 // Maintain dictionaries mirrored to the expected values of the settings in | 338 // Maintain dictionaries mirrored to the expected values of the settings in |
| 314 // each storage area. | 339 // each storage area. |
| 315 DictionaryValue expected1, expected2; | 340 DictionaryValue expected1, expected2; |
| 316 | 341 |
| 317 // Make storage1 initialised from local data, storage2 initialised from sync. | 342 // Make storage1 initialised from local data, storage2 initialised from sync. |
| 318 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1"); | 343 ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type); |
| 319 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2"); | 344 ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type); |
| 320 | 345 |
| 321 storage1->Set("foo", value1); | 346 storage1->Set("foo", value1); |
| 322 expected1.Set("foo", value1.DeepCopy()); | 347 expected1.Set("foo", value1.DeepCopy()); |
| 323 | 348 |
| 324 SyncDataList sync_data; | 349 SyncDataList sync_data; |
| 325 sync_data.push_back(extension_settings_sync_util::CreateData( | 350 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 326 "s2", "bar", value2)); | 351 "s2", "bar", value2)); |
| 327 | 352 |
| 328 backend_->MergeDataAndStartSyncing( | 353 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
| 329 syncable::EXTENSION_SETTINGS, sync_data, &sync_); | 354 model_type, sync_data, &sync_); |
| 330 expected2.Set("bar", value2.DeepCopy()); | 355 expected2.Set("bar", value2.DeepCopy()); |
| 331 | 356 |
| 332 // Make sync add some settings. | 357 // Make sync add some settings. |
| 333 SyncChangeList change_list; | 358 SyncChangeList change_list; |
| 334 change_list.push_back(extension_settings_sync_util::CreateAdd( | 359 change_list.push_back(extension_settings_sync_util::CreateAdd( |
| 335 "s1", "bar", value2)); | 360 "s1", "bar", value2)); |
| 336 change_list.push_back(extension_settings_sync_util::CreateAdd( | 361 change_list.push_back(extension_settings_sync_util::CreateAdd( |
| 337 "s2", "foo", value1)); | 362 "s2", "foo", value1)); |
| 338 backend_->ProcessSyncChanges(FROM_HERE, change_list); | 363 GetSyncableService(model_type)->ProcessSyncChanges(FROM_HERE, change_list); |
| 339 expected1.Set("bar", value2.DeepCopy()); | 364 expected1.Set("bar", value2.DeepCopy()); |
| 340 expected2.Set("foo", value1.DeepCopy()); | 365 expected2.Set("foo", value1.DeepCopy()); |
| 341 | 366 |
| 342 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); | 367 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); |
| 343 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); | 368 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); |
| 344 | 369 |
| 345 // Make sync update some settings, storage1 the new setting, storage2 the | 370 // Make sync update some settings, storage1 the new setting, storage2 the |
| 346 // initial setting. | 371 // initial setting. |
| 347 change_list.clear(); | 372 change_list.clear(); |
| 348 change_list.push_back(extension_settings_sync_util::CreateUpdate( | 373 change_list.push_back(extension_settings_sync_util::CreateUpdate( |
| 349 "s1", "bar", value2)); | 374 "s1", "bar", value2)); |
| 350 change_list.push_back(extension_settings_sync_util::CreateUpdate( | 375 change_list.push_back(extension_settings_sync_util::CreateUpdate( |
| 351 "s2", "bar", value1)); | 376 "s2", "bar", value1)); |
| 352 backend_->ProcessSyncChanges(FROM_HERE, change_list); | 377 GetSyncableService(model_type)->ProcessSyncChanges(FROM_HERE, change_list); |
| 353 expected1.Set("bar", value2.DeepCopy()); | 378 expected1.Set("bar", value2.DeepCopy()); |
| 354 expected2.Set("bar", value1.DeepCopy()); | 379 expected2.Set("bar", value1.DeepCopy()); |
| 355 | 380 |
| 356 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); | 381 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); |
| 357 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); | 382 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); |
| 358 | 383 |
| 359 // Make sync remove some settings, storage1 the initial setting, storage2 the | 384 // Make sync remove some settings, storage1 the initial setting, storage2 the |
| 360 // new setting. | 385 // new setting. |
| 361 change_list.clear(); | 386 change_list.clear(); |
| 362 change_list.push_back(extension_settings_sync_util::CreateDelete( | 387 change_list.push_back(extension_settings_sync_util::CreateDelete( |
| 363 "s1", "foo")); | 388 "s1", "foo")); |
| 364 change_list.push_back(extension_settings_sync_util::CreateDelete( | 389 change_list.push_back(extension_settings_sync_util::CreateDelete( |
| 365 "s2", "foo")); | 390 "s2", "foo")); |
| 366 backend_->ProcessSyncChanges(FROM_HERE, change_list); | 391 GetSyncableService(model_type)->ProcessSyncChanges(FROM_HERE, change_list); |
| 367 expected1.Remove("foo", NULL); | 392 expected1.Remove("foo", NULL); |
| 368 expected2.Remove("foo", NULL); | 393 expected2.Remove("foo", NULL); |
| 369 | 394 |
| 370 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); | 395 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get()); |
| 371 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); | 396 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get()); |
| 372 | 397 |
| 373 backend_->StopSyncing(syncable::EXTENSION_SETTINGS); | 398 GetSyncableService(model_type)->StopSyncing(model_type); |
| 374 } | 399 } |
| 375 | 400 |
| 376 TEST_F(ExtensionSettingsSyncTest, PushToSync) { | 401 TEST_F(ExtensionSettingsSyncTest, PushToSync) { |
| 402 syncable::ModelType model_type = syncable::APP_SETTINGS; |
| 403 Extension::Type type = Extension::TYPE_PACKAGED_APP; |
| 404 |
| 377 StringValue value1("fooValue"); | 405 StringValue value1("fooValue"); |
| 378 ListValue value2; | 406 ListValue value2; |
| 379 value2.Append(StringValue::CreateStringValue("barValue")); | 407 value2.Append(StringValue::CreateStringValue("barValue")); |
| 380 | 408 |
| 381 // Make storage1/2 initialised from local data, storage3/4 initialised from | 409 // Make storage1/2 initialised from local data, storage3/4 initialised from |
| 382 // sync. | 410 // sync. |
| 383 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1"); | 411 ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type); |
| 384 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2"); | 412 ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type); |
| 385 SyncableExtensionSettingsStorage* storage3 = GetStorage("s3"); | 413 ExtensionSettingsStorage* storage3 = AddExtensionAndGetStorage("s3", type); |
| 386 SyncableExtensionSettingsStorage* storage4 = GetStorage("s4"); | 414 ExtensionSettingsStorage* storage4 = AddExtensionAndGetStorage("s4", type); |
| 387 | 415 |
| 388 storage1->Set("foo", value1); | 416 storage1->Set("foo", value1); |
| 389 storage2->Set("foo", value1); | 417 storage2->Set("foo", value1); |
| 390 | 418 |
| 391 SyncDataList sync_data; | 419 SyncDataList sync_data; |
| 392 sync_data.push_back(extension_settings_sync_util::CreateData( | 420 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 393 "s3", "bar", value2)); | 421 "s3", "bar", value2)); |
| 394 sync_data.push_back(extension_settings_sync_util::CreateData( | 422 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 395 "s4", "bar", value2)); | 423 "s4", "bar", value2)); |
| 396 | 424 |
| 397 backend_->MergeDataAndStartSyncing( | 425 GetSyncableService(model_type)->MergeDataAndStartSyncing( |
| 398 syncable::EXTENSION_SETTINGS, sync_data, &sync_); | 426 model_type, sync_data, &sync_); |
| 399 | 427 |
| 400 // Add something locally. | 428 // Add something locally. |
| 401 storage1->Set("bar", value2); | 429 storage1->Set("bar", value2); |
| 402 storage2->Set("bar", value2); | 430 storage2->Set("bar", value2); |
| 403 storage3->Set("foo", value1); | 431 storage3->Set("foo", value1); |
| 404 storage4->Set("foo", value1); | 432 storage4->Set("foo", value1); |
| 405 | 433 |
| 406 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "bar"); | 434 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "bar"); |
| 407 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type()); | 435 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type()); |
| 408 ASSERT_TRUE(value2.Equals(&change.value())); | 436 ASSERT_TRUE(value2.Equals(&change.value())); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 ASSERT_EQ( | 528 ASSERT_EQ( |
| 501 SyncChange::ACTION_DELETE, | 529 SyncChange::ACTION_DELETE, |
| 502 sync_.GetOnlyChange("s3", "bar").change_type()); | 530 sync_.GetOnlyChange("s3", "bar").change_type()); |
| 503 ASSERT_EQ( | 531 ASSERT_EQ( |
| 504 SyncChange::ACTION_DELETE, | 532 SyncChange::ACTION_DELETE, |
| 505 sync_.GetOnlyChange("s4", "foo").change_type()); | 533 sync_.GetOnlyChange("s4", "foo").change_type()); |
| 506 ASSERT_EQ( | 534 ASSERT_EQ( |
| 507 SyncChange::ACTION_DELETE, | 535 SyncChange::ACTION_DELETE, |
| 508 sync_.GetOnlyChange("s4", "bar").change_type()); | 536 sync_.GetOnlyChange("s4", "bar").change_type()); |
| 509 | 537 |
| 510 backend_->StopSyncing(syncable::EXTENSION_SETTINGS); | 538 GetSyncableService(model_type)->StopSyncing(model_type); |
| 511 } | 539 } |
| 540 |
| 541 TEST_F(ExtensionSettingsSyncTest, ExtensionAndAppSettingsSyncSeparately) { |
| 542 StringValue value1("fooValue"); |
| 543 ListValue value2; |
| 544 value2.Append(StringValue::CreateStringValue("barValue")); |
| 545 |
| 546 // storage1 is an extension, storage2 is an app. |
| 547 ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage( |
| 548 "s1", Extension::TYPE_EXTENSION); |
| 549 ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage( |
| 550 "s2", Extension::TYPE_PACKAGED_APP); |
| 551 |
| 552 storage1->Set("foo", value1); |
| 553 storage2->Set("bar", value2); |
| 554 |
| 555 std::map<std::string, ExtensionSettingSyncDataList> extension_sync_data = |
| 556 GetAllSyncData(syncable::EXTENSION_SETTINGS); |
| 557 ASSERT_EQ(1u, extension_sync_data.size()); |
| 558 ASSERT_EQ(1u, extension_sync_data["s1"].size()); |
| 559 ASSERT_PRED_FORMAT2(ValuesEq, &value1, &extension_sync_data["s1"][0].value()); |
| 560 |
| 561 std::map<std::string, ExtensionSettingSyncDataList> app_sync_data = |
| 562 GetAllSyncData(syncable::APP_SETTINGS); |
| 563 ASSERT_EQ(1u, app_sync_data.size()); |
| 564 ASSERT_EQ(1u, app_sync_data["s2"].size()); |
| 565 ASSERT_PRED_FORMAT2(ValuesEq, &value2, &app_sync_data["s2"][0].value()); |
| 566 |
| 567 // Stop each separately, there should be no changes either time. |
| 568 SyncDataList sync_data; |
| 569 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 570 "s1", "foo", value1)); |
| 571 |
| 572 GetSyncableService(syncable::EXTENSION_SETTINGS)-> |
| 573 MergeDataAndStartSyncing(syncable::EXTENSION_SETTINGS, sync_data, &sync_); |
| 574 GetSyncableService(syncable::EXTENSION_SETTINGS)-> |
| 575 StopSyncing(syncable::EXTENSION_SETTINGS); |
| 576 ASSERT_EQ(0u, sync_.changes().size()); |
| 577 |
| 578 sync_data.clear(); |
| 579 sync_data.push_back(extension_settings_sync_util::CreateData( |
| 580 "s2", "bar", value2)); |
| 581 |
| 582 GetSyncableService(syncable::APP_SETTINGS)-> |
| 583 MergeDataAndStartSyncing(syncable::APP_SETTINGS, sync_data, &sync_); |
| 584 GetSyncableService(syncable::APP_SETTINGS)-> |
| 585 StopSyncing(syncable::APP_SETTINGS); |
| 586 ASSERT_EQ(0u, sync_.changes().size()); |
| 587 } |
| OLD | NEW |