| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/sync_driver/device_info_service.h" | 5 #include "components/sync_driver/device_info_service.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "components/sync_driver/local_device_info_provider_mock.h" | 17 #include "components/sync_driver/local_device_info_provider_mock.h" |
| 18 #include "sync/api/data_batch.h" | 18 #include "sync/api/data_batch.h" |
| 19 #include "sync/api/entity_data.h" | 19 #include "sync/api/entity_data.h" |
| 20 #include "sync/api/metadata_batch.h" | 20 #include "sync/api/metadata_batch.h" |
| 21 #include "sync/api/model_type_store.h" | 21 #include "sync/api/model_type_store.h" |
| 22 #include "sync/internal_api/public/shared_model_type_processor.h" |
| 22 #include "sync/internal_api/public/test/model_type_store_test_util.h" | 23 #include "sync/internal_api/public/test/model_type_store_test_util.h" |
| 23 #include "sync/protocol/data_type_state.pb.h" | 24 #include "sync/protocol/data_type_state.pb.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 namespace sync_driver_v2 { | 27 namespace sync_driver_v2 { |
| 27 | 28 |
| 28 using syncer::SyncError; | 29 using syncer::SyncError; |
| 29 using syncer_v2::DataBatch; | 30 using syncer_v2::DataBatch; |
| 30 using syncer_v2::EntityChange; | 31 using syncer_v2::EntityChange; |
| 31 using syncer_v2::EntityChangeList; | 32 using syncer_v2::EntityChangeList; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 129 |
| 129 void Delete(const std::string& client_tag, | 130 void Delete(const std::string& client_tag, |
| 130 MetadataChangeList* metadata_changes) override { | 131 MetadataChangeList* metadata_changes) override { |
| 131 delete_set_.insert(client_tag); | 132 delete_set_.insert(client_tag); |
| 132 } | 133 } |
| 133 | 134 |
| 134 void OnMetadataLoaded(scoped_ptr<MetadataBatch> batch) override { | 135 void OnMetadataLoaded(scoped_ptr<MetadataBatch> batch) override { |
| 135 std::swap(metadata_, batch); | 136 std::swap(metadata_, batch); |
| 136 } | 137 } |
| 137 | 138 |
| 139 void OnSyncStarting(const StartCallback& callback) override {} |
| 140 |
| 138 const std::map<std::string, scoped_ptr<EntityData>>& put_map() const { | 141 const std::map<std::string, scoped_ptr<EntityData>>& put_map() const { |
| 139 return put_map_; | 142 return put_map_; |
| 140 } | 143 } |
| 141 const std::set<std::string>& delete_set() const { return delete_set_; } | 144 const std::set<std::string>& delete_set() const { return delete_set_; } |
| 142 const MetadataBatch* metadata() const { return metadata_.get(); } | 145 const MetadataBatch* metadata() const { return metadata_.get(); } |
| 143 | 146 |
| 144 private: | 147 private: |
| 145 std::map<std::string, scoped_ptr<EntityData>> put_map_; | 148 std::map<std::string, scoped_ptr<EntityData>> put_map_; |
| 146 std::set<std::string> delete_set_; | 149 std::set<std::string> delete_set_; |
| 147 scoped_ptr<MetadataBatch> metadata_; | 150 scoped_ptr<MetadataBatch> metadata_; |
| 148 }; | 151 }; |
| 149 | 152 |
| 150 } // namespace | 153 } // namespace |
| 151 | 154 |
| 152 class DeviceInfoServiceTest : public testing::Test, | 155 class DeviceInfoServiceTest : public testing::Test, |
| 153 public DeviceInfoTracker::Observer { | 156 public DeviceInfoTracker::Observer { |
| 154 protected: | 157 protected: |
| 155 ~DeviceInfoServiceTest() override { | 158 ~DeviceInfoServiceTest() override { |
| 156 // Some tests may never initialize the service. | 159 // Some tests may never initialize the service. |
| 157 if (service_) | 160 if (service_) |
| 158 service_->RemoveObserver(this); | 161 service_->RemoveObserver(this); |
| 159 | 162 |
| 160 // Force all remaining (store) tasks to execute so we don't leak memory. | 163 // Force all remaining (store) tasks to execute so we don't leak memory. |
| 161 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
| 162 } | 165 } |
| 163 | 166 |
| 164 void OnDeviceInfoChange() override { change_count_++; } | 167 void OnDeviceInfoChange() override { change_count_++; } |
| 165 | 168 |
| 166 protected: | 169 scoped_ptr<ModelTypeChangeProcessor> CreateSharedModelTypeProcessor( |
| 170 syncer::ModelType type, |
| 171 ModelTypeService* service) { |
| 172 processor_ = new FakeModelTypeChangeProcessor(); |
| 173 return make_scoped_ptr(processor_); |
| 174 } |
| 175 |
| 167 DeviceInfoServiceTest() | 176 DeviceInfoServiceTest() |
| 168 : change_count_(0), | 177 : change_count_(0), |
| 169 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), | 178 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), |
| 170 local_device_(new LocalDeviceInfoProviderMock( | 179 local_device_(new LocalDeviceInfoProviderMock( |
| 171 "guid_1", | 180 "guid_1", |
| 172 "client_1", | 181 "client_1", |
| 173 "Chromium 10k", | 182 "Chromium 10k", |
| 174 "Chrome 10k", | 183 "Chrome 10k", |
| 175 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | 184 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, |
| 176 "device_id")) {} | 185 "device_id")) {} |
| 177 | 186 |
| 178 // Initialized the service based on the current local device and store. Can | 187 // Initialized the service based on the current local device and store. Can |
| 179 // only be called once per run, as it passes |store_|. | 188 // only be called once per run, as it passes |store_|. |
| 180 void InitializeService() { | 189 void InitializeService() { |
| 181 ASSERT_TRUE(store_); | 190 ASSERT_TRUE(store_); |
| 182 service_.reset(new DeviceInfoService( | 191 service_.reset(new DeviceInfoService( |
| 183 local_device_.get(), | 192 local_device_.get(), |
| 184 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, | 193 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, |
| 185 base::Passed(&store_)))); | 194 base::Passed(&store_)), |
| 195 base::Bind(&DeviceInfoServiceTest::CreateSharedModelTypeProcessor, |
| 196 base::Unretained(this)))); |
| 186 service_->AddObserver(this); | 197 service_->AddObserver(this); |
| 187 } | 198 } |
| 188 | 199 |
| 189 // Creates the service and runs any outstanding tasks. This will typically | 200 // Creates the service and runs any outstanding tasks. This will typically |
| 190 // cause all initialization callbacks between the sevice and store to fire. | 201 // cause all initialization callbacks between the sevice and store to fire. |
| 191 void InitializeAndPump() { | 202 void InitializeAndPump() { |
| 192 InitializeService(); | 203 InitializeService(); |
| 193 base::RunLoop().RunUntilIdle(); | 204 base::RunLoop().RunUntilIdle(); |
| 194 } | 205 } |
| 195 | 206 |
| 196 // Creates a new processor and sets it on the service. We typically need to | 207 // Creates a new processor and sets it on the service. We typically need to |
| 197 // pump in this scenario because metadata is going to need to be loading from | 208 // pump in this scenario because metadata is going to need to be loading from |
| 198 // the store and given to the processor, which is async. | 209 // the store and given to the processor, which is async. |
| 199 void SetProcessorAndPump() { | 210 void SetProcessorAndPump() { |
| 200 processor_ = new FakeModelTypeChangeProcessor(); | 211 EXPECT_EQ(processor_, service_->GetOrCreateChangeProcessor()); |
| 201 service()->set_change_processor(make_scoped_ptr(processor_)); | 212 ASSERT_TRUE(processor_); |
| 202 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
| 203 } | 214 } |
| 204 | 215 |
| 205 // Generates a specifics object with slightly differing values. Will generate | 216 // Generates a specifics object with slightly differing values. Will generate |
| 206 // the same values on each run of a test because a simple counter is used to | 217 // the same values on each run of a test because a simple counter is used to |
| 207 // vary field values. | 218 // vary field values. |
| 208 DeviceInfoSpecifics GenerateTestSpecifics() { | 219 DeviceInfoSpecifics GenerateTestSpecifics() { |
| 209 int label = ++generated_count_; | 220 int label = ++generated_count_; |
| 210 DeviceInfoSpecifics specifics; | 221 DeviceInfoSpecifics specifics; |
| 211 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); | 222 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 EXPECT_FALSE(error.IsSet()); | 670 EXPECT_FALSE(error.IsSet()); |
| 660 EXPECT_EQ(0, change_count()); | 671 EXPECT_EQ(0, change_count()); |
| 661 EXPECT_TRUE(service()->GetAllDeviceInfo().empty()); | 672 EXPECT_TRUE(service()->GetAllDeviceInfo().empty()); |
| 662 EXPECT_TRUE(processor()->delete_set().empty()); | 673 EXPECT_TRUE(processor()->delete_set().empty()); |
| 663 EXPECT_TRUE(processor()->put_map().empty()); | 674 EXPECT_TRUE(processor()->put_map().empty()); |
| 664 } | 675 } |
| 665 | 676 |
| 666 } // namespace | 677 } // namespace |
| 667 | 678 |
| 668 } // namespace sync_driver_v2 | 679 } // namespace sync_driver_v2 |
| OLD | NEW |