| 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 "components/sync/device_info/device_info_data_type_controller.h" | 5 #include "components/sync/device_info/device_info_data_type_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "components/sync/base/model_type.h" | 12 #include "components/sync/base/model_type.h" |
| 13 #include "components/sync/device_info/local_device_info_provider_mock.h" | 13 #include "components/sync/device_info/local_device_info_provider_mock.h" |
| 14 #include "components/sync/driver/fake_sync_client.h" | 14 #include "components/sync/driver/fake_sync_client.h" |
| 15 #include "components/sync/driver/sync_api_component_factory.h" | 15 #include "components/sync/driver/sync_api_component_factory.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace sync_driver { | 18 namespace syncer { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class DeviceInfoDataTypeControllerTest : public testing::Test { | 22 class DeviceInfoDataTypeControllerTest : public testing::Test { |
| 23 public: | 23 public: |
| 24 DeviceInfoDataTypeControllerTest() | 24 DeviceInfoDataTypeControllerTest() |
| 25 : load_finished_(false), | 25 : load_finished_(false), |
| 26 last_type_(syncer::UNSPECIFIED), | 26 last_type_(UNSPECIFIED), |
| 27 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
| 28 ~DeviceInfoDataTypeControllerTest() override {} | 28 ~DeviceInfoDataTypeControllerTest() override {} |
| 29 | 29 |
| 30 void SetUp() override { | 30 void SetUp() override { |
| 31 local_device_.reset(new LocalDeviceInfoProviderMock( | 31 local_device_.reset(new LocalDeviceInfoProviderMock( |
| 32 "cache_guid", "Wayne Gretzky's Hacking Box", "Chromium 10k", | 32 "cache_guid", "Wayne Gretzky's Hacking Box", "Chromium 10k", |
| 33 "Chrome 10k", sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id")); | 33 "Chrome 10k", sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id")); |
| 34 | 34 |
| 35 controller_.reset(new DeviceInfoDataTypeController( | 35 controller_.reset(new DeviceInfoDataTypeController( |
| 36 base::Closure(), &sync_client_, local_device_.get())); | 36 base::Closure(), &sync_client_, local_device_.get())); |
| 37 | 37 |
| 38 load_finished_ = false; | 38 load_finished_ = false; |
| 39 last_type_ = syncer::UNSPECIFIED; | 39 last_type_ = UNSPECIFIED; |
| 40 last_error_ = syncer::SyncError(); | 40 last_error_ = SyncError(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void TearDown() override { | 43 void TearDown() override { |
| 44 controller_ = NULL; | 44 controller_ = NULL; |
| 45 local_device_.reset(); | 45 local_device_.reset(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void Start() { | 48 void Start() { |
| 49 controller_->LoadModels( | 49 controller_->LoadModels( |
| 50 base::Bind(&DeviceInfoDataTypeControllerTest::OnLoadFinished, | 50 base::Bind(&DeviceInfoDataTypeControllerTest::OnLoadFinished, |
| 51 weak_ptr_factory_.GetWeakPtr())); | 51 weak_ptr_factory_.GetWeakPtr())); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void OnLoadFinished(syncer::ModelType type, const syncer::SyncError& error) { | 54 void OnLoadFinished(ModelType type, const SyncError& error) { |
| 55 load_finished_ = true; | 55 load_finished_ = true; |
| 56 last_type_ = type; | 56 last_type_ = type; |
| 57 last_error_ = error; | 57 last_error_ = error; |
| 58 } | 58 } |
| 59 | 59 |
| 60 testing::AssertionResult LoadResult() { | 60 testing::AssertionResult LoadResult() { |
| 61 if (!load_finished_) { | 61 if (!load_finished_) { |
| 62 return testing::AssertionFailure() << "OnLoadFinished wasn't called"; | 62 return testing::AssertionFailure() << "OnLoadFinished wasn't called"; |
| 63 } | 63 } |
| 64 | 64 |
| 65 if (last_error_.IsSet()) { | 65 if (last_error_.IsSet()) { |
| 66 return testing::AssertionFailure() | 66 return testing::AssertionFailure() |
| 67 << "OnLoadFinished was called with a SyncError: " | 67 << "OnLoadFinished was called with a SyncError: " |
| 68 << last_error_.ToString(); | 68 << last_error_.ToString(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 if (last_type_ != syncer::DEVICE_INFO) { | 71 if (last_type_ != DEVICE_INFO) { |
| 72 return testing::AssertionFailure() | 72 return testing::AssertionFailure() |
| 73 << "OnLoadFinished was called with a wrong sync type: " | 73 << "OnLoadFinished was called with a wrong sync type: " |
| 74 << last_type_; | 74 << last_type_; |
| 75 } | 75 } |
| 76 | 76 |
| 77 return testing::AssertionSuccess(); | 77 return testing::AssertionSuccess(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 protected: | 80 protected: |
| 81 std::unique_ptr<DeviceInfoDataTypeController> controller_; | 81 std::unique_ptr<DeviceInfoDataTypeController> controller_; |
| 82 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; | 82 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; |
| 83 bool load_finished_; | 83 bool load_finished_; |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 base::MessageLoopForUI message_loop_; | 86 base::MessageLoopForUI message_loop_; |
| 87 syncer::ModelType last_type_; | 87 ModelType last_type_; |
| 88 syncer::SyncError last_error_; | 88 SyncError last_error_; |
| 89 FakeSyncClient sync_client_; | 89 FakeSyncClient sync_client_; |
| 90 base::WeakPtrFactory<DeviceInfoDataTypeControllerTest> weak_ptr_factory_; | 90 base::WeakPtrFactory<DeviceInfoDataTypeControllerTest> weak_ptr_factory_; |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 TEST_F(DeviceInfoDataTypeControllerTest, StartModels) { | 93 TEST_F(DeviceInfoDataTypeControllerTest, StartModels) { |
| 94 Start(); | 94 Start(); |
| 95 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state()); | 95 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state()); |
| 96 EXPECT_TRUE(LoadResult()); | 96 EXPECT_TRUE(LoadResult()); |
| 97 } | 97 } |
| 98 | 98 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 115 | 115 |
| 116 controller_->Stop(); | 116 controller_->Stop(); |
| 117 // Destroy |local_device_| and |controller_| out of order | 117 // Destroy |local_device_| and |controller_| out of order |
| 118 // to verify that the controller doesn't crash in the destructor. | 118 // to verify that the controller doesn't crash in the destructor. |
| 119 local_device_.reset(); | 119 local_device_.reset(); |
| 120 controller_ = NULL; | 120 controller_ = NULL; |
| 121 } | 121 } |
| 122 | 122 |
| 123 } // namespace | 123 } // namespace |
| 124 | 124 |
| 125 } // namespace sync_driver | 125 } // namespace syncer |
| OLD | NEW |