| 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 <list> | 5 #include <list> |
| 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/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/test/test_simple_task_runner.h" | 12 #include "base/test/test_simple_task_runner.h" |
| 13 #include "components/sync_driver/non_blocking_data_type_controller.h" | 13 #include "components/sync_driver/non_blocking_data_type_controller.h" |
| 14 #include "sync/engine/model_type_sync_proxy_impl.h" | 14 #include "sync/engine/model_type_sync_proxy_impl.h" |
| 15 #include "sync/engine/model_type_sync_worker.h" | 15 #include "sync/engine/model_type_sync_worker.h" |
| 16 #include "sync/internal_api/public/base/model_type.h" | 16 #include "sync/internal_api/public/base/model_type.h" |
| 17 #include "sync/internal_api/public/sync_context_proxy.h" | 17 #include "sync/internal_api/public/sync_context_proxy.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace syncer { | 20 namespace sync_driver { |
| 21 | 21 |
| 22 class ModelTypeSyncWorker; | 22 class ModelTypeSyncWorker; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // A useless instance of ModelTypeSyncWorker. | 26 // A useless instance of ModelTypeSyncWorker. |
| 27 class NullModelTypeSyncWorker : public ModelTypeSyncWorker { | 27 class NullModelTypeSyncWorker : public syncer::ModelTypeSyncWorker { |
| 28 public: | 28 public: |
| 29 NullModelTypeSyncWorker(); | 29 NullModelTypeSyncWorker(); |
| 30 virtual ~NullModelTypeSyncWorker(); | 30 virtual ~NullModelTypeSyncWorker(); |
| 31 | 31 |
| 32 virtual void EnqueueForCommit(const CommitRequestDataList& list) OVERRIDE; | 32 virtual void EnqueueForCommit( |
| 33 const syncer::CommitRequestDataList& list) OVERRIDE; |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 NullModelTypeSyncWorker::NullModelTypeSyncWorker() { | 36 NullModelTypeSyncWorker::NullModelTypeSyncWorker() { |
| 36 } | 37 } |
| 37 | 38 |
| 38 NullModelTypeSyncWorker::~NullModelTypeSyncWorker() { | 39 NullModelTypeSyncWorker::~NullModelTypeSyncWorker() { |
| 39 } | 40 } |
| 40 | 41 |
| 41 void NullModelTypeSyncWorker::EnqueueForCommit( | 42 void NullModelTypeSyncWorker::EnqueueForCommit( |
| 42 const CommitRequestDataList& list) { | 43 const syncer::CommitRequestDataList& list) { |
| 43 NOTREACHED() << "Not implemented."; | 44 NOTREACHED() << "Not implemented."; |
| 44 } | 45 } |
| 45 | 46 |
| 46 // A class that pretends to be the sync backend. | 47 // A class that pretends to be the sync backend. |
| 47 class MockSyncContext { | 48 class MockSyncContext { |
| 48 public: | 49 public: |
| 49 void Connect( | 50 void Connect( |
| 50 syncer::ModelType type, | 51 syncer::ModelType type, |
| 51 const scoped_refptr<base::SingleThreadTaskRunner>& model_task_runner, | 52 const scoped_refptr<base::SingleThreadTaskRunner>& model_task_runner, |
| 52 const base::WeakPtr<syncer::ModelTypeSyncProxyImpl>& type_proxy) { | 53 const base::WeakPtr<syncer::ModelTypeSyncProxyImpl>& type_proxy) { |
| 53 enabled_types_.Put(type); | 54 enabled_types_.Put(type); |
| 54 model_task_runner->PostTask( | 55 model_task_runner->PostTask( |
| 55 FROM_HERE, | 56 FROM_HERE, |
| 56 base::Bind(&syncer::ModelTypeSyncProxyImpl::OnConnect, | 57 base::Bind(&syncer::ModelTypeSyncProxyImpl::OnConnect, |
| 57 type_proxy, | 58 type_proxy, |
| 58 base::Passed(scoped_ptr<ModelTypeSyncWorker>( | 59 base::Passed(scoped_ptr<syncer::ModelTypeSyncWorker>( |
| 59 new NullModelTypeSyncWorker()).Pass()))); | 60 new NullModelTypeSyncWorker()).Pass()))); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void Disconnect(syncer::ModelType type) { | 63 void Disconnect(syncer::ModelType type) { |
| 63 DCHECK(enabled_types_.Has(type)); | 64 DCHECK(enabled_types_.Has(type)); |
| 64 enabled_types_.Remove(type); | 65 enabled_types_.Remove(type); |
| 65 } | 66 } |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 std::list<base::Closure> tasks_; | 69 std::list<base::Closure> tasks_; |
| 69 syncer::ModelTypeSet enabled_types_; | 70 syncer::ModelTypeSet enabled_types_; |
| 70 }; | 71 }; |
| 71 | 72 |
| 72 // A proxy to the MockSyncContext that implements SyncContextProxy. | 73 // A proxy to the MockSyncContext that implements SyncContextProxy. |
| 73 class MockSyncContextProxy : public syncer::SyncContextProxy { | 74 class MockSyncContextProxy : public syncer::SyncContextProxy { |
| 74 public: | 75 public: |
| 75 MockSyncContextProxy( | 76 MockSyncContextProxy( |
| 76 MockSyncContext* sync_context, | 77 MockSyncContext* sync_context, |
| 77 const scoped_refptr<base::TestSimpleTaskRunner>& model_task_runner, | 78 const scoped_refptr<base::TestSimpleTaskRunner>& model_task_runner, |
| 78 const scoped_refptr<base::TestSimpleTaskRunner>& sync_task_runner) | 79 const scoped_refptr<base::TestSimpleTaskRunner>& sync_task_runner) |
| 79 : mock_sync_context_(sync_context), | 80 : mock_sync_context_(sync_context), |
| 80 model_task_runner_(model_task_runner), | 81 model_task_runner_(model_task_runner), |
| 81 sync_task_runner_(sync_task_runner) {} | 82 sync_task_runner_(sync_task_runner) {} |
| 82 virtual ~MockSyncContextProxy() {} | 83 virtual ~MockSyncContextProxy() {} |
| 83 | 84 |
| 84 virtual void ConnectTypeToSync( | 85 virtual void ConnectTypeToSync( |
| 85 syncer::ModelType type, | 86 syncer::ModelType type, |
| 86 const DataTypeState& data_type_state, | 87 const syncer::DataTypeState& data_type_state, |
| 87 const base::WeakPtr<syncer::ModelTypeSyncProxyImpl>& type_proxy) | 88 const base::WeakPtr<syncer::ModelTypeSyncProxyImpl>& type_proxy) |
| 88 OVERRIDE { | 89 OVERRIDE { |
| 89 // Normally we'd use MessageLoopProxy::current() as the TaskRunner argument | 90 // Normally we'd use MessageLoopProxy::current() as the TaskRunner argument |
| 90 // to Connect(). That won't work here in this test, so we use the | 91 // to Connect(). That won't work here in this test, so we use the |
| 91 // model_task_runner_ that was injected for this purpose instead. | 92 // model_task_runner_ that was injected for this purpose instead. |
| 92 sync_task_runner_->PostTask(FROM_HERE, | 93 sync_task_runner_->PostTask(FROM_HERE, |
| 93 base::Bind(&MockSyncContext::Connect, | 94 base::Bind(&MockSyncContext::Connect, |
| 94 base::Unretained(mock_sync_context_), | 95 base::Unretained(mock_sync_context_), |
| 95 type, | 96 type, |
| 96 model_task_runner_, | 97 model_task_runner_, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 180 |
| 180 void SetAutoRunTasks(bool auto_run_tasks) { | 181 void SetAutoRunTasks(bool auto_run_tasks) { |
| 181 auto_run_tasks_ = auto_run_tasks; | 182 auto_run_tasks_ = auto_run_tasks; |
| 182 } | 183 } |
| 183 | 184 |
| 184 protected: | 185 protected: |
| 185 syncer::ModelTypeSyncProxyImpl type_sync_proxy_; | 186 syncer::ModelTypeSyncProxyImpl type_sync_proxy_; |
| 186 scoped_refptr<base::TestSimpleTaskRunner> model_thread_; | 187 scoped_refptr<base::TestSimpleTaskRunner> model_thread_; |
| 187 scoped_refptr<base::TestSimpleTaskRunner> sync_thread_; | 188 scoped_refptr<base::TestSimpleTaskRunner> sync_thread_; |
| 188 | 189 |
| 189 browser_sync::NonBlockingDataTypeController controller_; | 190 NonBlockingDataTypeController controller_; |
| 190 | 191 |
| 191 MockSyncContext mock_sync_context_; | 192 MockSyncContext mock_sync_context_; |
| 192 MockSyncContextProxy mock_context_proxy_; | 193 MockSyncContextProxy mock_context_proxy_; |
| 193 | 194 |
| 194 bool auto_run_tasks_; | 195 bool auto_run_tasks_; |
| 195 }; | 196 }; |
| 196 | 197 |
| 197 // Initialization when the user has disabled syncing for this type. | 198 // Initialization when the user has disabled syncing for this type. |
| 198 TEST_F(NonBlockingDataTypeControllerTest, UserDisabled) { | 199 TEST_F(NonBlockingDataTypeControllerTest, UserDisabled) { |
| 199 SetIsPreferred(false); | 200 SetIsPreferred(false); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 // The sync thread has three messages related to those enables and | 430 // The sync thread has three messages related to those enables and |
| 430 // disables sittin in its queue. Let's allow it to process them. | 431 // disables sittin in its queue. Let's allow it to process them. |
| 431 RunQueuedSyncThreadTasks(); | 432 RunQueuedSyncThreadTasks(); |
| 432 | 433 |
| 433 // Let the model thread process any messages from the sync thread. | 434 // Let the model thread process any messages from the sync thread. |
| 434 RunQueuedModelThreadTasks(); | 435 RunQueuedModelThreadTasks(); |
| 435 EXPECT_TRUE(type_sync_proxy_.IsPreferred()); | 436 EXPECT_TRUE(type_sync_proxy_.IsPreferred()); |
| 436 EXPECT_TRUE(type_sync_proxy_.IsConnected()); | 437 EXPECT_TRUE(type_sync_proxy_.IsConnected()); |
| 437 } | 438 } |
| 438 | 439 |
| 439 } // namespace syncer | 440 } // namespace sync_driver |
| OLD | NEW |